De-complicate some of the instance delete path

Now that we are using separate targeted contexts in the delete path,
we can avoid some of the switch back and forth in that code.

Change-Id: I40ad635d8d361c6b1dccf7324d852f0c655a8c61
This commit is contained in:
Dan Smith 2017-04-20 12:51:35 -07:00
parent a5f800b024
commit ea923441d2
2 changed files with 8 additions and 44 deletions

View File

@ -1874,11 +1874,7 @@ class API(base.Base):
# We have to get the flavor from the instance while the
# context is still targeted to where the instance lives.
with nova_context.target_cell(context, cell) as cctxt:
# FIXME: If the instance has the targeted context in
# it then we don't need the context manager.
quota_flavor = self._get_flavor_for_reservation(
instance)
quota_flavor = self._get_flavor_for_reservation(instance)
with nova_context.target_cell(context, None) as cctxt:
# This is confusing but actually decrements quota usage
@ -1888,22 +1884,13 @@ class API(base.Base):
try:
# Now destroy the instance from the cell it lives in.
with nova_context.target_cell(context, cell) as cctxt:
# If the instance has the targeted context in it
# then we don't need the context manager.
with compute_utils.notify_about_instance_delete(
self.notifier, cctxt, instance):
instance.destroy()
with compute_utils.notify_about_instance_delete(
self.notifier, context, instance):
instance.destroy()
# Now commit the quota reservation to decrement usage.
# NOTE(danms): When target_cell yields a context copy,
# we can remove this targeting.
with nova_context.target_cell(context, None) as cctxt:
quotas.commit()
quotas.commit()
except exception.InstanceNotFound:
# NOTE(danms): When target_cell yields a context copy,
# we can remove this targeting.
with nova_context.target_cell(context, None) as cctxt:
quotas.rollback()
quotas.rollback()
# The instance was deleted or is already gone.
return
if not instance:

View File

@ -1468,22 +1468,14 @@ class _ComputeAPIUnitTestMixIn(object):
flavor=instance.flavor)
quota_mock.commit.assert_called_once_with()
expected_target_cell_calls = [
# Get the instance.flavor.
mock.call(self.context, cell0),
mock.call().__enter__(),
mock.call().__exit__(None, None, None),
# Create the quota reservation.
mock.call(self.context, None),
mock.call().__enter__(),
mock.call().__exit__(None, None, None),
# Destroy the instance.
mock.call(self.context, cell0),
mock.call().__enter__(),
mock.call().__exit__(None, None, None),
]
target_cell_mock.assert_has_calls(expected_target_cell_calls)
notify_mock.assert_called_once_with(
self.compute_api.notifier, mock.sentinel.cctxt, instance)
self.compute_api.notifier, self.context, instance)
destroy_mock.assert_called_once_with()
@mock.patch('nova.context.target_cell')
@ -1531,28 +1523,13 @@ class _ComputeAPIUnitTestMixIn(object):
self.context.project_id, instance.user_id,
flavor=instance.flavor)
notify_mock.assert_called_once_with(
self.compute_api.notifier, mock.sentinel.cctxt, instance)
self.compute_api.notifier, self.context, instance)
destroy_mock.assert_called_once_with()
expected_target_cell_calls = [
# Get the instance.flavor.
mock.call(self.context, cell0),
mock.call().__enter__(),
mock.call().__exit__(None, None, None),
# Create the quota reservation.
mock.call(self.context, None),
mock.call().__enter__(),
mock.call().__exit__(None, None, None),
# Destroy the instance.
mock.call(self.context, cell0),
mock.call().__enter__(),
mock.call().__exit__(
exception.InstanceNotFound,
destroy_mock.side_effect,
mock.ANY),
# Rollback the quota reservation.
mock.call(self.context, None),
mock.call().__enter__(),
mock.call().__exit__(None, None, None),
]
target_cell_mock.assert_has_calls(expected_target_cell_calls)
quota_mock.rollback.assert_called_once_with()