diff --git a/nova/objects/instance_mapping.py b/nova/objects/instance_mapping.py index 4bc232f37cee..f60f2fd8a106 100644 --- a/nova/objects/instance_mapping.py +++ b/nova/objects/instance_mapping.py @@ -161,12 +161,14 @@ def populate_queued_for_delete(context, max_count): # have not yet received a defined value decision for # queued_for_delete context.session.query(api_models.InstanceMapping) - .options(joinedload('cell_mapping')) .filter( api_models.InstanceMapping.queued_for_delete == None) # noqa .filter(api_models.InstanceMapping.cell_id == cell.id) .limit(max_count).all()) ims_by_inst = {im.instance_uuid: im for im in ims} + if not ims_by_inst: + # If there is nothing from this cell to migrate, move on. + continue with nova_context.target_cell(context, cell) as cctxt: filters = {'uuid': list(ims_by_inst.keys()), 'deleted': True, diff --git a/nova/tests/functional/db/test_instance_mapping.py b/nova/tests/functional/db/test_instance_mapping.py index b96cfaae56ec..5086094dd717 100644 --- a/nova/tests/functional/db/test_instance_mapping.py +++ b/nova/tests/functional/db/test_instance_mapping.py @@ -10,6 +10,7 @@ # License for the specific language governing permissions and limitations # under the License. +import mock from oslo_utils.fixture import uuidsentinel from oslo_utils import uuidutils @@ -196,6 +197,15 @@ class InstanceMappingTestCase(test.NoDBTestCase): self.assertEqual(4, len( [im for im in mappings if im.queued_for_delete is False])) + # Run it again to make sure we don't query the cell database for + # instances if we didn't get any un-migrated mappings. + with mock.patch('nova.objects.InstanceList.get_by_filters', + new_callable=mock.NonCallableMock): + done, total = instance_mapping.populate_queued_for_delete( + self.context, 1000) + self.assertEqual(0, done) + self.assertEqual(0, total) + class InstanceMappingListTestCase(test.NoDBTestCase): USES_DB_SELF = True