From 47061e699b9dbf6fdeea572a5abeaa72e499ec87 Mon Sep 17 00:00:00 2001 From: Matt Riedemann Date: Wed, 27 Feb 2019 16:24:24 -0500 Subject: [PATCH] Optimize populate_queued_for_delete online data migration The data migration was needlessly querying the cell database for instances even if there were no instance mappings in that database that needed to be migrated. This simply continues to the next cell if the instance mappings in the current cell are migrated. While we're in here, the joinedload on 'cell_mapping' can be removed since it's not used. Closes-Bug: #1817961 Change-Id: Idf35ed9d57945bc80fbd47393b7de076330160e6 --- nova/objects/instance_mapping.py | 4 +++- nova/tests/functional/db/test_instance_mapping.py | 10 ++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) 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