Skip to content

Commit e7cd63c

Browse files
committed
Fix inactive session error in compute node creation
In the fix for bug 1839560 [1][2], soft-deleted compute nodes may be restored, to ensure we can reuse ironic node UUIDs as compute node UUIDs. While this seems to largely work, it results in some nasty errors being generated [3]: InvalidRequestError This session is in 'inactive' state, due to the SQL transaction being rolled back; no further SQL can be emitted within this transaction. This happens because compute_node_create is decorated with pick_context_manager_writer, which begins a transaction. While _compute_node_get_and_update_deleted claims that calling a second pick_context_manager_writer decorated function will begin a new subtransaction, this does not appear to be the case. This change removes pick_context_manager_writer from the compute_node_create function, and adds a new _compute_node_create function which ensures the transaction is finished if _compute_node_get_and_update_deleted is called. The new unit test added here fails without this change. This change marks the removal of the final FIXME from the functional test added in [4]. [1] https://bugs.launchpad.net/nova/+bug/1839560 [2] https://git.openstack.org/cgit/openstack/nova/commit/?id=89dd74ac7f1028daadf86cb18948e27fe9d1d411 [3] http://paste.openstack.org/show/786350/ [4] https://review.opendev.org/#/c/695012/ Change-Id: Iae119ea8776bc7f2e5dbe2e502a743217beded73 Closes-Bug: #1853159 Related-Bug: #1853009
1 parent 903ae5e commit e7cd63c

File tree

3 files changed

+30
-30
lines changed

3 files changed

+30
-30
lines changed

nova/db/sqlalchemy/api.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -685,16 +685,29 @@ def compute_node_search_by_hypervisor(context, hypervisor_match):
685685

686686

687687
@pick_context_manager_writer
688-
def compute_node_create(context, values):
688+
def _compute_node_create(context, values):
689689
"""Creates a new ComputeNode and populates the capacity fields
690690
with the most recent data.
691691
"""
692692
convert_objects_related_datetimes(values)
693693

694694
compute_node_ref = models.ComputeNode()
695695
compute_node_ref.update(values)
696+
compute_node_ref.save(context.session)
697+
return compute_node_ref
698+
699+
700+
# NOTE(mgoddard): We avoid decorating this with @pick_context_manager_writer,
701+
# so that we get a separate transaction in the exception handler. This avoids
702+
# an error message about inactive DB sessions during a transaction rollback.
703+
# See https://bugs.launchpad.net/nova/+bug/1853159.
704+
def compute_node_create(context, values):
705+
"""Creates a new ComputeNode and populates the capacity fields
706+
with the most recent data. Will restore a soft deleted compute node if a
707+
UUID has been explicitly requested.
708+
"""
696709
try:
697-
compute_node_ref.save(context.session)
710+
compute_node_ref = _compute_node_create(context, values)
698711
except db_exc.DBDuplicateEntry:
699712
with excutils.save_and_reraise_exception(logger=LOG) as err_ctx:
700713
# Check to see if we have a (soft) deleted ComputeNode with the

nova/tests/functional/regressions/test_bug_1853009.py

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,6 @@ def test_node_rebalance_deleted_compute_node_race(self):
7979

8080
# host1[1]: Finds no compute record in RT. Tries to create one
8181
# (_init_compute_node).
82-
# FIXME(mgoddard): This shows a traceback with SQL rollback due to
83-
# soft-deleted node. The create seems to succeed but breaks the RT
84-
# update for this node. See
85-
# https://bugs.launchpad.net/nova/+bug/1853159.
8682
host1.manager.update_available_resource(ctxt)
8783
self._assert_hypervisor_api(nodename, 'host1')
8884
# There should only be one resource provider (fake-node).
@@ -156,30 +152,7 @@ def test_node_rebalance_deleted_compute_node_race(self):
156152
# Verify that the node was recreated.
157153
self._assert_hypervisor_api(nodename, 'host1')
158154

159-
rt = host1.manager._get_resource_tracker()
160-
161-
# But due to https://bugs.launchpad.net/nova/+bug/1853159 the compute
162-
# node is not cached in the RT.
163-
self.assertNotIn(nodename, rt.compute_nodes)
164-
165-
# And for the same reason, the provider is not recreated.
166-
rps = self._get_all_providers()
167-
self.assertEqual(0, len(rps), rps)
168-
169-
# host1[1]: Should add compute node to RT cache and recreate resource
170-
# provider.
171-
# FIXME(mgoddard): Resource provider not recreated here, because it
172-
# exists in the provider tree. See
173-
# https://bugs.launchpad.net/nova/+bug/1841481.
174-
host1.manager.update_available_resource(ctxt)
175-
176-
# Verify that the node still exists.
177-
self._assert_hypervisor_api(nodename, 'host1')
178-
179-
# And it is now in the RT cache.
180-
self.assertIn(nodename, rt.compute_nodes)
181-
182-
# There is still no RP.
155+
# The resource provider has now been created.
183156
rps = self._get_all_providers()
184157
self.assertEqual(1, len(rps), rps)
185158
self.assertEqual(nodename, rps[0]['name'])

nova/tests/unit/db/test_db_api.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6924,6 +6924,20 @@ def test_compute_node_create_duplicate_host_hypervisor_hostname(self):
69246924
self.assertRaises(db_exc.DBDuplicateEntry,
69256925
db.compute_node_create, self.ctxt, other_node)
69266926

6927+
def test_compute_node_create_duplicate_uuid(self):
6928+
"""Tests to make sure that no exception is raised when trying to create
6929+
a compute node with the same host, hypervisor_hostname and uuid values
6930+
as another compute node that was previously soft-deleted.
6931+
"""
6932+
# Prior to fixing https://bugs.launchpad.net/nova/+bug/1853159, this
6933+
# raised the following error:
6934+
# sqlalchemy.exc.InvalidRequestError: This session is in 'inactive'
6935+
# state, due to the SQL transaction being rolled back; no further SQL
6936+
# can be emitted within this transaction.
6937+
db.compute_node_delete(self.ctxt, self.item['id'], self.item['host'])
6938+
new_node = db.compute_node_create(self.ctxt, self.compute_node_dict)
6939+
self.assertEqual(self.item['uuid'], new_node['uuid'])
6940+
69276941
def test_compute_node_get_all(self):
69286942
nodes = db.compute_node_get_all(self.ctxt)
69296943
self.assertEqual(1, len(nodes))

0 commit comments

Comments
 (0)