From ea923441d236d68556e933c1340789c013825e04 Mon Sep 17 00:00:00 2001 From: Dan Smith Date: Thu, 20 Apr 2017 12:51:35 -0700 Subject: [PATCH] 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 --- nova/compute/api.py | 25 +++++-------------- nova/tests/unit/compute/test_compute_api.py | 27 ++------------------- 2 files changed, 8 insertions(+), 44 deletions(-) diff --git a/nova/compute/api.py b/nova/compute/api.py index 5d63e04398e7..5fc90d07eec3 100644 --- a/nova/compute/api.py +++ b/nova/compute/api.py @@ -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: diff --git a/nova/tests/unit/compute/test_compute_api.py b/nova/tests/unit/compute/test_compute_api.py index 86d6a9542ea3..d1bd018238df 100644 --- a/nova/tests/unit/compute/test_compute_api.py +++ b/nova/tests/unit/compute/test_compute_api.py @@ -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()