From e48fb84e1246aae8c7f7fd22a9c72ccbfa92d9ee Mon Sep 17 00:00:00 2001 From: Matt Riedemann Date: Mon, 21 Oct 2019 13:00:59 -0400 Subject: [PATCH] Join migration_context and flavor in Migration.instance This builds on Ifc7dcde8a659710acecb1967da15c632c69d675c by joining the Migration.instance migration_context and flavor to avoid lazy-loading those later. When tracking an incoming migration, the ResourceTracker _pair_instances_to_migrations can hit a KeyError since it's not yet tracking the instance on that dest host yet. Then _update_usage_from_migrations will lazy-load the Migration.instance field and access the migration_context and flavor fields on the instance, which get lazy-loaded, which kind of defeats part of the purpose of that optimization. Change-Id: I613ad054f77b1a0a9d2e7718c0c531d11525283c Related-Bug: #1540526 (cherry picked from commit c15e36e5849b6baddebb4b39475a6bf03ec5908b) (cherry picked from commit e2b4e3346e20615473328e7ae90b5083500961ca) (cherry picked from commit 603171bd5c5ce354086ce32e980f9bb8383069bf) --- nova/objects/migration.py | 3 ++- nova/tests/unit/compute/test_resource_tracker.py | 5 ++++- nova/tests/unit/objects/test_migration.py | 9 +++++---- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/nova/objects/migration.py b/nova/objects/migration.py index 03ecfe36b64..6bdce7ee661 100644 --- a/nova/objects/migration.py +++ b/nova/objects/migration.py @@ -169,7 +169,8 @@ def save(self): def instance(self): if not hasattr(self, '_cached_instance'): self._cached_instance = objects.Instance.get_by_uuid( - self._context, self.instance_uuid) + self._context, self.instance_uuid, + expected_attrs=['migration_context', 'flavor']) return self._cached_instance @instance.setter diff --git a/nova/tests/unit/compute/test_resource_tracker.py b/nova/tests/unit/compute/test_resource_tracker.py index 2ab2aac7a39..297513e3cc9 100644 --- a/nova/tests/unit/compute/test_resource_tracker.py +++ b/nova/tests/unit/compute/test_resource_tracker.py @@ -2660,7 +2660,10 @@ def test_instance_not_found(self, mock_get_instance, mock_update_usage): self.rt._update_usage_from_migrations(mock.sentinel.ctx, [migration], _NODENAME) mock_get_instance.assert_called_once_with(mock.sentinel.ctx, - 'some_uuid') + 'some_uuid', + expected_attrs=[ + 'migration_context', + 'flavor']) self.assertFalse(mock_update_usage.called) @mock.patch('nova.compute.resource_tracker.ResourceTracker.' diff --git a/nova/tests/unit/objects/test_migration.py b/nova/tests/unit/objects/test_migration.py index 16b7d2765e6..941c7ad853e 100644 --- a/nova/tests/unit/objects/test_migration.py +++ b/nova/tests/unit/objects/test_migration.py @@ -161,10 +161,11 @@ def test_instance(self, mock_get): fake_migration) mig._context = ctxt self.assertEqual(mig.instance.host, fake_inst['host']) - mock_get.assert_called_once_with(ctxt, - fake_migration['instance_uuid'], - columns_to_join=['info_cache', - 'security_groups']) + mock_get.assert_called_once_with( + ctxt, fake_migration['instance_uuid'], + columns_to_join=['extra', + 'extra.flavor', + 'extra.migration_context']) def test_instance_setter(self): migration = objects.Migration(instance_uuid=uuidsentinel.instance)