Skip to content

Commit f203da3

Browse files
committed
objects: Add MigrationTypeField
We use these things many places in the code and it would be good to have constants to reference. Do just that. Note that this results in a change in the object hash. However, there are no actual changes in the output object so that's okay. Change-Id: If02567ce0a3431dda5b2bf6d398bbf7cc954eed0 Signed-off-by: Stephen Finucane <[email protected]>
1 parent 487c4fa commit f203da3

File tree

14 files changed

+81
-49
lines changed

14 files changed

+81
-49
lines changed

nova/api/openstack/compute/migrations.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from nova import exception
2424
from nova.i18n import _
2525
from nova.objects import base as obj_base
26+
from nova.objects import fields
2627
from nova.policies import migrations as migrations_policies
2728

2829

@@ -72,7 +73,9 @@ def _output(self, req, migrations_obj, add_link=False,
7273
# NOTE(Shaohe Feng) above version 2.23, add migration_type for all
7374
# kinds of migration, but we only add links just for in-progress
7475
# live-migration.
75-
if add_link and obj['migration_type'] == "live-migration" and (
76+
if (add_link and
77+
obj['migration_type'] ==
78+
fields.MigrationType.LIVE_MIGRATION and
7679
obj["status"] in live_migration_in_progress):
7780
obj["links"] = self._view_builder._get_links(
7881
req, obj["id"],

nova/api/openstack/compute/server_migrations.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ def show(self, req, server_id, id):
132132
" server %(uuid)s.") % {"id": id, "uuid": server_id}
133133
raise exc.HTTPNotFound(explanation=msg)
134134

135-
if migration.get("migration_type") != "live-migration":
135+
if not migration.is_live_migration:
136136
msg = _("Migration %(id)s for server %(uuid)s is not"
137137
" live-migration.") % {"id": id, "uuid": server_id}
138138
raise exc.HTTPNotFound(explanation=msg)

nova/compute/api.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5036,12 +5036,10 @@ def evacuate(self, context, instance, host, on_shared_storage,
50365036

50375037
# NOTE(danms): Create this as a tombstone for the source compute
50385038
# to find and cleanup. No need to pass it anywhere else.
5039-
migration = objects.Migration(context,
5040-
source_compute=instance.host,
5041-
source_node=instance.node,
5042-
instance_uuid=instance.uuid,
5043-
status='accepted',
5044-
migration_type='evacuation')
5039+
migration = objects.Migration(
5040+
context, source_compute=instance.host, source_node=instance.node,
5041+
instance_uuid=instance.uuid, status='accepted',
5042+
migration_type=fields_obj.MigrationType.EVACUATION)
50455043
if host:
50465044
migration.dest_compute = host
50475045
migration.create()

nova/compute/claims.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,10 @@ def _test_pci(self):
194194
migration, all types of PCI requests are supported, so we just call up
195195
to normal Claim's _test_pci().
196196
"""
197-
if self.migration.migration_type != 'live-migration':
197+
if not self.migration.is_live_migration:
198198
return super(MoveClaim, self)._test_pci()
199-
elif self._pci_requests.requests:
199+
200+
if self._pci_requests.requests:
200201
for pci_request in self._pci_requests.requests:
201202
if (pci_request.source !=
202203
objects.InstancePCIRequest.NEUTRON_PORT):
@@ -225,7 +226,7 @@ def _test_live_migration_page_size(self):
225226
something we want to support, so fail the claim if the page sizes are
226227
different.
227228
"""
228-
if (self.migration.migration_type == 'live-migration' and
229+
if (self.migration.is_live_migration and
229230
self.instance.numa_topology and
230231
# NOTE(artom) We only support a single page size across all
231232
# cells, checking cell 0 is sufficient.

nova/compute/manager.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -731,7 +731,7 @@ def _destroy_evacuated_instances(self, context, node_cache):
731731
# the user can rebuild the instance (in ERROR state) on the source
732732
# host.
733733
'status': ['accepted', 'pre-migrating', 'done'],
734-
'migration_type': 'evacuation',
734+
'migration_type': fields.MigrationType.EVACUATION,
735735
}
736736
with utils.temporary_mutation(context, read_deleted='yes'):
737737
evacuations = objects.MigrationList.get_by_filters(context,
@@ -8552,9 +8552,10 @@ def post_live_migration_at_destination(self, context, instance,
85528552
# NOTE(mriedem): This is a no-op for neutron.
85538553
self.network_api.setup_networks_on_host(context, instance,
85548554
self.host)
8555-
migration = objects.Migration(source_compute=instance.host,
8556-
dest_compute=self.host,
8557-
migration_type='live-migration')
8555+
migration = objects.Migration(
8556+
source_compute=instance.host,
8557+
dest_compute=self.host,
8558+
migration_type=fields.MigrationType.LIVE_MIGRATION)
85588559
self.network_api.migrate_instance_finish(
85598560
context, instance, migration, provider_mappings=None)
85608561

nova/compute/resource_tracker.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
from nova.i18n import _
4040
from nova import objects
4141
from nova.objects import base as obj_base
42+
from nova.objects import fields
4243
from nova.objects import migration as migration_obj
4344
from nova.pci import manager as pci_manager
4445
from nova.pci import request as pci_request
@@ -191,9 +192,10 @@ def rebuild_claim(self, context, instance, nodename, allocations,
191192
limits=None, image_meta=None, migration=None):
192193
"""Create a claim for a rebuild operation."""
193194
instance_type = instance.flavor
194-
return self._move_claim(context, instance, instance_type, nodename,
195-
migration, allocations, move_type='evacuation',
196-
limits=limits, image_meta=image_meta)
195+
return self._move_claim(
196+
context, instance, instance_type, nodename, migration, allocations,
197+
move_type=fields.MigrationType.EVACUATION,
198+
image_meta=image_meta, limits=limits)
197199

198200
@utils.synchronized(COMPUTE_RESOURCE_SEMAPHORE, fair=True)
199201
def resize_claim(self, context, instance, instance_type, nodename,
@@ -225,9 +227,11 @@ def live_migration_claim(self, context, instance, nodename, migration,
225227
# Flavor and image cannot change during a live migration.
226228
instance_type = instance.flavor
227229
image_meta = instance.image_meta
228-
return self._move_claim(context, instance, instance_type, nodename,
229-
migration, allocs, move_type='live-migration',
230-
image_meta=image_meta, limits=limits)
230+
return self._move_claim(
231+
context, instance, instance_type, nodename, migration, allocs,
232+
move_type=fields.MigrationType.LIVE_MIGRATION,
233+
image_meta=image_meta, limits=limits,
234+
)
231235

232236
def _move_claim(self, context, instance, new_instance_type, nodename,
233237
migration, allocations, move_type=None,
@@ -293,7 +297,7 @@ def _move_claim(self, context, instance, new_instance_type, nodename,
293297
# migration to avoid stepping on that code's toes. Ideally,
294298
# MoveClaim/this method would be used for all live migration resource
295299
# claims.
296-
if self.pci_tracker and migration.migration_type != 'live-migration':
300+
if self.pci_tracker and not migration.is_live_migration:
297301
# NOTE(jaypipes): ComputeNode.pci_device_pools is set below
298302
# in _update_usage_from_instance().
299303
claimed_pci_devices_objs = self.pci_tracker.claim_instance(
@@ -369,7 +373,7 @@ def _claim_existing_migration(self, migration, nodename):
369373
# NOTE(artom) Migration objects for live migrations are created with
370374
# status 'accepted' by the conductor in live_migrate_instance() and do
371375
# not have a 'pre-migrating' status.
372-
if migration.migration_type != 'live-migration':
376+
if not migration.is_live_migration:
373377
migration.status = 'pre-migrating'
374378
migration.save()
375379

@@ -1637,8 +1641,7 @@ def _verify_resources(self, resources):
16371641

16381642
def _get_instance_type(self, instance, prefix, migration):
16391643
"""Get the instance type from instance."""
1640-
stashed_flavors = migration.migration_type in ('resize',)
1641-
if stashed_flavors:
1644+
if migration.is_resize:
16421645
return getattr(instance, '%sflavor' % prefix)
16431646
else:
16441647
# NOTE(ndipanov): Certain migration types (all but resize)

nova/conductor/manager.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ def _set_vm_state(context, instance, ex, vm_state=None,
456456
migration.status = 'accepted'
457457
migration.instance_uuid = instance.uuid
458458
migration.source_compute = instance.host
459-
migration.migration_type = 'live-migration'
459+
migration.migration_type = fields.MigrationType.LIVE_MIGRATION
460460
if instance.obj_attr_is_set('flavor'):
461461
migration.old_instance_type_id = instance.flavor.id
462462
migration.new_instance_type_id = instance.flavor.id

nova/network/constants.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
FIP_PORT_DETAILS = 'Floating IP Port Details Extension'
2222
SUBSTR_PORT_FILTERING = 'IP address substring filtering'
2323
PORT_BINDING_EXTENDED = 'Port Bindings Extended'
24-
LIVE_MIGRATION = 'live-migration'
2524
DEFAULT_SECGROUP = 'default'
2625
BINDING_PROFILE = 'binding:profile'
2726
BINDING_HOST_ID = 'binding:host_id'

nova/network/neutron.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3415,7 +3415,7 @@ def _update_port_binding_for_instance(
34153415
vnic_type = p.get('binding:vnic_type')
34163416
if (vnic_type in network_model.VNIC_TYPES_SRIOV and
34173417
migration is not None and
3418-
migration['migration_type'] != constants.LIVE_MIGRATION):
3418+
not migration.is_live_migration):
34193419
# Note(adrianc): for live migration binding profile was already
34203420
# updated in conductor when calling bind_ports_to_host()
34213421
if not pci_mapping:
@@ -3437,9 +3437,9 @@ def _update_port_binding_for_instance(
34373437
# allocation key in the port binding. However during resize, cold
34383438
# migrate, evacuate and unshelve we have to set the binding here.
34393439
# Also note that during unshelve no migration object is created.
3440-
if (p.get('resource_request') and
3441-
(migration is None or
3442-
migration['migration_type'] != constants.LIVE_MIGRATION)):
3440+
if p.get('resource_request') and (
3441+
migration is None or not migration.is_live_migration
3442+
):
34433443
if not provider_mappings:
34443444
# TODO(gibi): Remove this check when compute RPC API is
34453445
# bumped to 6.0

nova/objects/fields.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,16 @@ class ImageSignatureKeyType(BaseNovaEnum):
464464
)
465465

466466

467+
class MigrationType(BaseNovaEnum):
468+
469+
MIGRATION = 'migration' # cold migration
470+
RESIZE = 'resize'
471+
LIVE_MIGRATION = 'live-migration'
472+
EVACUATION = 'evacuation'
473+
474+
ALL = (MIGRATION, RESIZE, LIVE_MIGRATION, EVACUATION)
475+
476+
467477
class OSType(BaseNovaEnum):
468478

469479
LINUX = "linux"
@@ -1207,6 +1217,10 @@ class ImageSignatureKeyTypeField(BaseEnumField):
12071217
AUTO_TYPE = ImageSignatureKeyType()
12081218

12091219

1220+
class MigrationTypeField(BaseEnumField):
1221+
AUTO_TYPE = MigrationType()
1222+
1223+
12101224
class OSTypeField(BaseEnumField):
12111225
AUTO_TYPE = OSType()
12121226

nova/objects/migration.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,7 @@ class Migration(base.NovaPersistentObject, base.NovaObject,
5757
'new_instance_type_id': fields.IntegerField(nullable=True),
5858
'instance_uuid': fields.StringField(nullable=True),
5959
'status': fields.StringField(nullable=True),
60-
'migration_type': fields.EnumField(['migration', 'resize',
61-
'live-migration', 'evacuation'],
62-
nullable=False),
60+
'migration_type': fields.MigrationTypeField(nullable=False),
6361
'hidden': fields.BooleanField(nullable=False, default=False),
6462
'memory_total': fields.IntegerField(nullable=True),
6563
'memory_processed': fields.IntegerField(nullable=True),
@@ -205,6 +203,14 @@ def instance(self, instance):
205203
def is_same_host(self):
206204
return self.source_compute == self.dest_compute
207205

206+
@property
207+
def is_live_migration(self):
208+
return self.migration_type == fields.MigrationType.LIVE_MIGRATION
209+
210+
@property
211+
def is_resize(self):
212+
return self.migration_type == fields.MigrationType.RESIZE
213+
208214

209215
@base.NovaObjectRegistry.register
210216
class MigrationList(base.ObjectListBase, base.NovaObject):

nova/tests/unit/network/test_neutron.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4382,8 +4382,8 @@ def test_update_port_bindings_for_instance_with_pci(self,
43824382
'pci_vendor_info': 'old_pci_vendor_info'}},
43834383
{'id': 'fake-port-2',
43844384
constants.BINDING_HOST_ID: instance.host}]}
4385-
migration = {'status': 'confirmed',
4386-
'migration_type': "migration"}
4385+
migration = objects.Migration(
4386+
status='confirmed', migration_type='migration')
43874387
list_ports_mock = mock.Mock(return_value=fake_ports)
43884388
get_client_mock.return_value.list_ports = list_ports_mock
43894389

@@ -4439,8 +4439,8 @@ def test_update_port_bindings_for_instance_with_pci_fail(self,
44394439
{'pci_slot': '0000:0a:00.1',
44404440
'physical_network': 'old_phys_net',
44414441
'pci_vendor_info': 'old_pci_vendor_info'}}]}
4442-
migration = {'status': 'confirmed',
4443-
'migration_type': "migration"}
4442+
migration = objects.Migration(
4443+
status='confirmed', migration_type='migration')
44444444
list_ports_mock = mock.Mock(return_value=fake_ports)
44454445
get_client_mock.return_value.list_ports = list_ports_mock
44464446

@@ -4595,8 +4595,8 @@ def test_update_port_bindings_for_instance_with_live_migration(
45954595
{'pci_slot': '0000:0a:00.1',
45964596
'physical_network': 'phys_net',
45974597
'pci_vendor_info': 'vendor_info'}}]}
4598-
migration = {'status': 'confirmed',
4599-
'migration_type': "live-migration"}
4598+
migration = objects.Migration(
4599+
status='confirmed', migration_type='live-migration')
46004600
list_ports_mock = mock.Mock(return_value=fake_ports)
46014601
get_client_mock.return_value.list_ports = list_ports_mock
46024602
update_port_mock = mock.Mock()
@@ -4727,7 +4727,7 @@ def test_update_port_bindings_for_instance_with_resource_req_live_mig(
47274727
def test_get_pci_mapping_for_migration(self):
47284728
instance = fake_instance.fake_instance_obj(self.context)
47294729
instance.migration_context = objects.MigrationContext()
4730-
migration = {'status': 'confirmed'}
4730+
migration = objects.Migration(status='confirmed')
47314731

47324732
with mock.patch.object(instance.migration_context,
47334733
'get_pci_mapping_for_migration') as map_func:
@@ -4737,7 +4737,7 @@ def test_get_pci_mapping_for_migration(self):
47374737
def test_get_pci_mapping_for_migration_reverted(self):
47384738
instance = fake_instance.fake_instance_obj(self.context)
47394739
instance.migration_context = objects.MigrationContext()
4740-
migration = {'status': 'reverted'}
4740+
migration = objects.Migration(status='reverted')
47414741

47424742
with mock.patch.object(instance.migration_context,
47434743
'get_pci_mapping_for_migration') as map_func:
@@ -6132,7 +6132,8 @@ def test_migrate_instance_start_activate(self, get_client_mock):
61326132
# Just create a simple instance with a single port.
61336133
instance = objects.Instance(info_cache=objects.InstanceInfoCache(
61346134
network_info=model.NetworkInfo([model.VIF(uuids.port_id)])))
6135-
migration = {'source_compute': 'source', 'dest_compute': 'dest'}
6135+
migration = objects.Migration(
6136+
source_compute='source', dest_compute='dest')
61366137
with mock.patch.object(self.api, 'activate_port_binding') as activate:
61376138
with mock.patch.object(self.api, 'supports_port_binding_extension',
61386139
return_value=True):
@@ -6155,7 +6156,8 @@ def test_migrate_instance_start_already_active(self, get_client_mock):
61556156
# Just create a simple instance with a single port.
61566157
instance = objects.Instance(info_cache=objects.InstanceInfoCache(
61576158
network_info=model.NetworkInfo([model.VIF(uuids.port_id)])))
6158-
migration = {'source_compute': 'source', 'dest_compute': 'dest'}
6159+
migration = objects.Migration(
6160+
source_compute='source', dest_compute='dest')
61596161
with mock.patch.object(self.api, 'activate_port_binding',
61606162
new_callable=mock.NonCallableMock):
61616163
with mock.patch.object(self.api, 'supports_port_binding_extension',
@@ -6180,7 +6182,8 @@ def test_migrate_instance_start_no_bindings(self, get_client_mock):
61806182
instance = objects.Instance(info_cache=objects.InstanceInfoCache(
61816183
network_info=model.NetworkInfo([
61826184
model.VIF(uuids.port1), model.VIF(uuids.port2)])))
6183-
migration = {'source_compute': 'source', 'dest_compute': 'dest'}
6185+
migration = objects.Migration(
6186+
source_compute='source', dest_compute='dest')
61846187
with mock.patch.object(self.api, 'activate_port_binding',
61856188
new_callable=mock.NonCallableMock):
61866189
with mock.patch.object(self.api, 'supports_port_binding_extension',
@@ -6202,7 +6205,8 @@ def test_migrate_instance_start_get_error(self, get_client_mock):
62026205
instance = objects.Instance(info_cache=objects.InstanceInfoCache(
62036206
network_info=model.NetworkInfo([
62046207
model.VIF(uuids.port1), model.VIF(uuids.port2)])))
6205-
migration = {'source_compute': 'source', 'dest_compute': 'dest'}
6208+
migration = objects.Migration(
6209+
source_compute='source', dest_compute='dest')
62066210
with mock.patch.object(self.api, 'activate_port_binding',
62076211
new_callable=mock.NonCallableMock):
62086212
with mock.patch.object(self.api, 'supports_port_binding_extension',

nova/tests/unit/objects/test_objects.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1103,7 +1103,7 @@ def obj_name(cls):
11031103
'LibvirtLiveMigrateData': '1.10-348cf70ea44d3b985f45f64725d6f6a7',
11041104
'LibvirtLiveMigrateNUMAInfo': '1.0-0e777677f3459d0ed1634eabbdb6c22f',
11051105
'MemoryDiagnostics': '1.0-2c995ae0f2223bb0f8e523c5cc0b83da',
1106-
'Migration': '1.7-b77066a88d08bdb0b05d7bc18780c55a',
1106+
'Migration': '1.7-bd45b232fd7c95cd79ae9187e10ef582',
11071107
'MigrationContext': '1.2-89f10a83999f852a489962ae37d8a026',
11081108
'MigrationList': '1.4-983a9c29d4f1e747ce719dc9063b729b',
11091109
'MonitorMetric': '1.1-53b1db7c4ae2c531db79761e7acc52ba',

nova/tests/unit/policies/test_server_migrations.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
from nova.api.openstack.compute import server_migrations
1919
from nova.compute import vm_states
20+
from nova import objects
2021
from nova.policies import base as base_policy
2122
from nova.policies import servers_migrations as policies
2223
from nova.tests.unit.api.openstack import fakes
@@ -82,8 +83,10 @@ def test_list_server_migrations_policy(self, mock_get):
8283
@mock.patch('nova.compute.api.API.get_migration_by_id_and_instance')
8384
def test_show_server_migrations_policy(self, mock_show, mock_output):
8485
rule_name = policies.POLICY_ROOT % 'show'
85-
mock_show.return_value = {"migration_type": "live-migration",
86-
"status": "running"}
86+
mock_show.return_value = objects.Migration(
87+
migration_type='live-migration',
88+
status='running',
89+
)
8790
self.common_policy_check(self.reader_authorized_contexts,
8891
self.reader_unauthorized_contexts,
8992
rule_name, self.controller.show,

0 commit comments

Comments
 (0)