Skip to content

Commit a8492e8

Browse files
markgoddardlyarwood
authored andcommitted
Prevent deletion of a compute node belonging to another host
There is a race condition in nova-compute with the ironic virt driver as nodes get rebalanced. It can lead to compute nodes being removed in the DB and not repopulated. Ultimately this prevents these nodes from being scheduled to. The main race condition involved is in update_available_resources in the compute manager. When the list of compute nodes is queried, there is a compute node belonging to the host that it does not expect to be managing, i.e. it is an orphan. Between that time and deleting the orphan, the real owner of the compute node takes ownership of it ( in the resource tracker). However, the node is still deleted as the first host is unaware of the ownership change. This change prevents this from occurring by filtering on the host when deleting a compute node. If another compute host has taken ownership of a node, it will have updated the host field and this will prevent deletion from occurring. The first host sees this has happened via the ComputeHostNotFound exception, and avoids deleting its resource provider. Co-Authored-By: melanie witt <[email protected]> Closes-Bug: #1853009 Related-Bug: #1841481 Change-Id: I260c1fded79a85d4899e94df4d9036a1ee437f02
1 parent c2b965b commit a8492e8

File tree

9 files changed

+127
-25
lines changed

9 files changed

+127
-25
lines changed

nova/compute/manager.py

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10046,17 +10046,30 @@ def update_available_resource(self, context, startup=False):
1004610046
"nodes are %(nodes)s",
1004710047
{'id': cn.id, 'hh': cn.hypervisor_hostname,
1004810048
'nodes': nodenames})
10049-
cn.destroy()
10050-
self.rt.remove_node(cn.hypervisor_hostname)
10051-
# Delete the corresponding resource provider in placement,
10052-
# along with any associated allocations.
1005310049
try:
10054-
self.reportclient.delete_resource_provider(context, cn,
10055-
cascade=True)
10056-
except keystone_exception.ClientException as e:
10057-
LOG.error(
10058-
"Failed to delete compute node resource provider "
10059-
"for compute node %s: %s", cn.uuid, str(e))
10050+
cn.destroy()
10051+
except exception.ObjectActionError:
10052+
# NOTE(mgoddard): it's possible that another compute
10053+
# service took ownership of this compute node since we
10054+
# queried it due to a rebalance, and this will cause the
10055+
# deletion to fail. Ignore the error in that case.
10056+
LOG.info("Ignoring failure to delete orphan compute node "
10057+
"%(id)s on hypervisor host %(hh)s due to "
10058+
"possible node rebalance",
10059+
{'id': cn.id, 'hh': cn.hypervisor_hostname})
10060+
self.rt.remove_node(cn.hypervisor_hostname)
10061+
self.reportclient.invalidate_resource_provider(cn.uuid)
10062+
else:
10063+
self.rt.remove_node(cn.hypervisor_hostname)
10064+
# Delete the corresponding resource provider in placement,
10065+
# along with any associated allocations.
10066+
try:
10067+
self.reportclient.delete_resource_provider(
10068+
context, cn, cascade=True)
10069+
except keystone_exception.ClientException as e:
10070+
LOG.error(
10071+
"Failed to delete compute node resource provider "
10072+
"for compute node %s: %s", cn.uuid, str(e))
1006010073

1006110074
for nodename in nodenames:
1006210075
self._update_available_resource_for_node(context, nodename,

nova/db/main/api.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -855,20 +855,33 @@ def compute_node_update(context, compute_id, values):
855855

856856

857857
@pick_context_manager_writer
858-
def compute_node_delete(context, compute_id):
858+
def compute_node_delete(context, compute_id, constraint=None):
859859
"""Delete a compute node from the database.
860860
861861
:param context: The security context
862862
:param compute_id: ID of the compute node
863+
:param constraint: a constraint object
864+
863865
:raises: ComputeHostNotFound if compute node with the given ID doesn't
864866
exist.
867+
:raises: ConstraintNotMet if a constraint was specified and it was not met.
865868
"""
866-
result = model_query(context, models.ComputeNode).\
867-
filter_by(id=compute_id).\
868-
soft_delete(synchronize_session=False)
869+
query = model_query(context, models.ComputeNode).filter_by(id=compute_id)
870+
871+
if constraint is not None:
872+
query = constraint.apply(models.ComputeNode, query)
873+
874+
result = query.soft_delete(synchronize_session=False)
869875

870876
if not result:
871-
raise exception.ComputeHostNotFound(host=compute_id)
877+
# The soft_delete could fail for one of two reasons:
878+
# 1) The compute node no longer exists
879+
# 2) The constraint, if specified, was not met
880+
# Try to read the compute node and let it raise ComputeHostNotFound if
881+
# 1) happened.
882+
compute_node_get(context, compute_id)
883+
# Else, raise ConstraintNotMet if 2) happened.
884+
raise exception.ConstraintNotMet()
872885

873886

874887
@pick_context_manager_reader

nova/objects/compute_node.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,20 @@ def save(self, prune_stats=False):
358358

359359
@base.remotable
360360
def destroy(self):
361-
db.compute_node_delete(self._context, self.id)
361+
if self.obj_attr_is_set('host') and self.host:
362+
# NOTE(melwitt): If our host is set, avoid a race between
363+
# nova-computes during ironic driver node rebalances which can
364+
# change node ownership.
365+
constraint = db.constraint(host=db.equal_any(self.host))
366+
else:
367+
constraint = None
368+
369+
try:
370+
db.compute_node_delete(
371+
self._context, self.id, constraint=constraint)
372+
except exception.ConstraintNotMet:
373+
raise exception.ObjectActionError(action='destroy',
374+
reason='host changed')
362375

363376
def update_from_virt_driver(self, resources):
364377
# NOTE(pmurray): the virt driver provides a dict of values that

nova/tests/functional/regressions/test_bug_1853009.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -133,24 +133,35 @@ def test_node_rebalance_deleted_compute_node_race(self):
133133
# Mock out the compute node query to simulate a race condition where
134134
# the list includes an orphan compute node that is newly owned by
135135
# host_b by the time host_a attempts to delete it.
136-
# FIXME(mgoddard): Ideally host_a would not delete a node that does not
137-
# belong to it. See https://bugs.launchpad.net/nova/+bug/1853009.
138136
with mock.patch(
139137
'nova.compute.manager.ComputeManager._get_compute_nodes_in_db'
140138
) as mock_get:
141139
mock_get.return_value = [cn]
142140
host_a.manager.update_available_resource(self.ctxt)
143141

144-
# Verify that the node was deleted.
142+
# Verify that the node was almost deleted, but was saved by the host
143+
# check
145144
self.assertIn(
146145
'Deleting orphan compute node %s hypervisor host '
147146
'is fake-node, nodes are' % cn.id,
148147
self.stdlog.logger.output)
149-
hypervisors = self.api.api_get(
150-
'/os-hypervisors/detail').body['hypervisors']
151-
self.assertEqual(0, len(hypervisors), hypervisors)
148+
self.assertIn(
149+
'Ignoring failure to delete orphan compute node %s on '
150+
'hypervisor host fake-node' % cn.id,
151+
self.stdlog.logger.output)
152+
self._assert_hypervisor_api(self.nodename, 'host_b')
152153
rps = self._get_all_providers()
153-
self.assertEqual(0, len(rps), rps)
154+
self.assertEqual(1, len(rps), rps)
155+
self.assertEqual(self.nodename, rps[0]['name'])
156+
157+
# Simulate deletion of an orphan by host_a. It shouldn't happen
158+
# anymore, but let's assume it already did.
159+
cn = objects.ComputeNode.get_by_host_and_nodename(
160+
self.ctxt, 'host_b', self.nodename)
161+
cn.destroy()
162+
host_a.manager.rt.remove_node(cn.hypervisor_hostname)
163+
host_a.manager.reportclient.delete_resource_provider(
164+
self.ctxt, cn, cascade=True)
154165

155166
# host_b[3]: Should recreate compute node and resource provider.
156167
# FIXME(mgoddard): Resource provider not recreated here, due to

nova/tests/unit/compute/test_compute.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,8 @@ def fake_get_compute_nodes_in_db(self, context, *args, **kwargs):
199199
context, objects.ComputeNode(), cn)
200200
for cn in fake_compute_nodes]
201201

202-
def fake_compute_node_delete(context, compute_node_id):
202+
def fake_compute_node_delete(context, compute_node_id,
203+
constraint=None):
203204
self.assertEqual(2, compute_node_id)
204205

205206
self.stub_out(

nova/tests/unit/compute/test_compute_mgr.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,33 @@ def test_update_available_resource_not_ready(self, get_db_nodes,
408408
update_mock.assert_not_called()
409409
del_rp_mock.assert_not_called()
410410

411+
@mock.patch.object(manager.ComputeManager,
412+
'_update_available_resource_for_node')
413+
@mock.patch.object(fake_driver.FakeDriver, 'get_available_nodes')
414+
@mock.patch.object(manager.ComputeManager, '_get_compute_nodes_in_db')
415+
def test_update_available_resource_destroy_rebalance(
416+
self, get_db_nodes, get_avail_nodes, update_mock):
417+
mock_rt = self._mock_rt()
418+
rc_mock = self.useFixture(fixtures.fixtures.MockPatchObject(
419+
self.compute, 'reportclient')).mock
420+
db_nodes = [self._make_compute_node('node1', 1)]
421+
get_db_nodes.return_value = db_nodes
422+
# Destroy can fail if nodes were rebalanced between getting the node
423+
# list and calling destroy.
424+
db_nodes[0].destroy.side_effect = exception.ObjectActionError(
425+
action='destroy', reason='host changed')
426+
get_avail_nodes.return_value = set()
427+
self.compute.update_available_resource(self.context)
428+
get_db_nodes.assert_called_once_with(self.context, set(),
429+
use_slave=True, startup=False)
430+
self.assertEqual(0, update_mock.call_count)
431+
432+
db_nodes[0].destroy.assert_called_once_with()
433+
self.assertEqual(0, rc_mock.delete_resource_provider.call_count)
434+
mock_rt.remove_node.assert_called_once_with('node1')
435+
rc_mock.invalidate_resource_provider.assert_called_once_with(
436+
db_nodes[0].uuid)
437+
411438
@mock.patch('nova.context.get_admin_context')
412439
def test_pre_start_hook(self, get_admin_context):
413440
"""Very simple test just to make sure update_available_resource is

nova/tests/unit/db/main/test_api.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5471,6 +5471,13 @@ def test_compute_node_delete(self):
54715471
nodes = db.compute_node_get_all(self.ctxt)
54725472
self.assertEqual(len(nodes), 0)
54735473

5474+
def test_compute_node_delete_different_host(self):
5475+
compute_node_id = self.item['id']
5476+
constraint = db.constraint(host=db.equal_any('different-host'))
5477+
self.assertRaises(exception.ConstraintNotMet,
5478+
db.compute_node_delete,
5479+
self.ctxt, compute_node_id, constraint=constraint)
5480+
54745481
def test_compute_node_search_by_hypervisor(self):
54755482
nodes_created = []
54765483
new_service = copy.copy(self.service_dict)

nova/tests/unit/objects/test_compute_node.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,17 @@ def test_destroy(self, mock_delete):
419419
compute = compute_node.ComputeNode(context=self.context)
420420
compute.id = 123
421421
compute.destroy()
422-
mock_delete.assert_called_once_with(self.context, 123)
422+
mock_delete.assert_called_once_with(self.context, 123, constraint=None)
423+
424+
def test_destroy_host_constraint(self):
425+
# Create compute node with host='fake'
426+
compute = fake_compute_with_resources.obj_clone()
427+
compute._context = self.context
428+
compute.host = 'fake'
429+
compute.create()
430+
# Simulate a compute node ownership change due to a node rebalance
431+
compute.host = 'different'
432+
self.assertRaises(exception.ObjectActionError, compute.destroy)
423433

424434
@mock.patch.object(db, 'compute_node_get_all')
425435
def test_get_all(self, mock_get_all):
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
fixes:
3+
- |
4+
Fixes an issue with multiple ``nova-compute`` services used with Ironic,
5+
where a rebalance operation could result in a compute node being deleted
6+
from the database and not recreated. See `bug 1853009
7+
<https://bugs.launchpad.net/nova/+bug/1853009>`__ for details.

0 commit comments

Comments
 (0)