Merge "objects: Add MigrationContext object"
This commit is contained in:
commit
b3f07e8c1f
|
@ -1803,6 +1803,11 @@ class NumaTopologyNotFound(NotFound):
|
|||
msg_fmt = _("Instance %(instance_uuid)s does not specify a NUMA topology")
|
||||
|
||||
|
||||
class MigrationContextNotFound(NotFound):
|
||||
msg_fmt = _("Instance %(instance_uuid)s does not specify a migration "
|
||||
"context.")
|
||||
|
||||
|
||||
class SocketPortRangeExhaustedException(NovaException):
|
||||
msg_fmt = _("Not able to acquire a free port for %(host)s")
|
||||
|
||||
|
|
|
@ -49,6 +49,7 @@ def register_all():
|
|||
__import__('nova.objects.instance_pci_requests')
|
||||
__import__('nova.objects.keypair')
|
||||
__import__('nova.objects.migration')
|
||||
__import__('nova.objects.migration_context')
|
||||
__import__('nova.objects.monitor_metric')
|
||||
__import__('nova.objects.network')
|
||||
__import__('nova.objects.network_request')
|
||||
|
|
|
@ -0,0 +1,82 @@
|
|||
# Copyright 2015 Red Hat Inc.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
# not use this file except in compliance with the License. You may obtain
|
||||
# a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
from oslo_serialization import jsonutils
|
||||
|
||||
from nova import db
|
||||
from nova import exception
|
||||
from nova.objects import base
|
||||
from nova.objects import fields
|
||||
|
||||
|
||||
@base.NovaObjectRegistry.register
|
||||
class MigrationContext(base.NovaPersistentObject, base.NovaObject):
|
||||
"""Data representing additional resources related to a migration.
|
||||
|
||||
Some resources cannot be calculated from knowing the flavor alone for the
|
||||
purpose of resources tracking, but need to be persisted at the time the
|
||||
claim was made, for subsequent resource tracking runs to be consistent.
|
||||
MigrationContext objects are created when the claim is done and are there
|
||||
to facilitate resource tracking and final provisioning of the instance on
|
||||
the destination host.
|
||||
"""
|
||||
|
||||
# Version 1.0: Initial version
|
||||
VERSION = '1.0'
|
||||
|
||||
fields = {
|
||||
'instance_uuid': fields.UUIDField(),
|
||||
'migration_id': fields.IntegerField(),
|
||||
'new_numa_topology': fields.ObjectField('InstanceNUMATopology',
|
||||
nullable=True),
|
||||
'old_numa_topology': fields.ObjectField('InstanceNUMATopology',
|
||||
nullable=True),
|
||||
}
|
||||
|
||||
obj_relationships = {
|
||||
'new_numa_topology': [('1.0', '1.2')],
|
||||
'old_numa_topology': [('1.0', '1.2')],
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def obj_from_db_obj(cls, db_obj):
|
||||
primitive = jsonutils.loads(db_obj)
|
||||
return cls.obj_from_primitive(primitive)
|
||||
|
||||
def _save(self):
|
||||
primitive = self.obj_to_primitive()
|
||||
payload = jsonutils.dumps(primitive)
|
||||
|
||||
values = {'migration_context': payload}
|
||||
db.instance_extra_update_by_uuid(self._context, self.instance_uuid,
|
||||
values)
|
||||
self.obj_reset_changes()
|
||||
|
||||
@classmethod
|
||||
def _destroy(cls, context, instance_uuid):
|
||||
values = {'migration_context': None}
|
||||
db.instance_extra_update_by_uuid(context, instance_uuid, values)
|
||||
|
||||
@base.remotable_classmethod
|
||||
def get_by_instance_uuid(cls, context, instance_uuid):
|
||||
db_extra = db.instance_extra_get_by_instance_uuid(
|
||||
context, instance_uuid, columns=['migration_context'])
|
||||
if not db_extra:
|
||||
raise exception.MigrationContextNotFound(
|
||||
instance_uuid=instance_uuid)
|
||||
|
||||
if db_extra['migration_context'] is None:
|
||||
return None
|
||||
|
||||
return cls.obj_from_db_obj(db_extra['migration_context'])
|
|
@ -0,0 +1,119 @@
|
|||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
# not use this file except in compliance with the License. You may obtain
|
||||
# a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
import uuid
|
||||
|
||||
import mock
|
||||
from oslo_serialization import jsonutils
|
||||
|
||||
from nova import exception
|
||||
from nova import objects
|
||||
from nova.tests.unit.objects import test_instance_numa_topology
|
||||
from nova.tests.unit.objects import test_objects
|
||||
|
||||
|
||||
fake_instance_uuid = str(uuid.uuid4())
|
||||
|
||||
fake_migration_context_obj = objects.MigrationContext()
|
||||
fake_migration_context_obj.instance_uuid = fake_instance_uuid
|
||||
fake_migration_context_obj.migration_id = 42
|
||||
fake_migration_context_obj.new_numa_topology = (
|
||||
test_instance_numa_topology.fake_obj_numa_topology.obj_clone())
|
||||
fake_migration_context_obj.old_numa_topology = None
|
||||
|
||||
fake_db_context = {
|
||||
'created_at': None,
|
||||
'updated_at': None,
|
||||
'deleted_at': None,
|
||||
'deleted': 0,
|
||||
'instance_uuid': fake_instance_uuid,
|
||||
'migration_context': jsonutils.dumps(
|
||||
fake_migration_context_obj.obj_to_primitive()),
|
||||
}
|
||||
|
||||
|
||||
def get_fake_migration_context_obj(ctxt):
|
||||
obj = fake_migration_context_obj.obj_clone()
|
||||
obj._context = ctxt
|
||||
return obj
|
||||
|
||||
|
||||
class _TestMigrationContext(object):
|
||||
@mock.patch('nova.db.instance_extra_update_by_uuid')
|
||||
def test_create(self, mock_update):
|
||||
ctxt_obj = get_fake_migration_context_obj(self.context)
|
||||
ctxt_obj._save()
|
||||
self.assertEqual(1, len(mock_update.call_args_list))
|
||||
update_call = mock_update.call_args
|
||||
self.assertEqual(self.context, update_call[0][0])
|
||||
self.assertEqual(fake_instance_uuid, update_call[0][1])
|
||||
self.assertIsInstance(ctxt_obj.new_numa_topology,
|
||||
objects.InstanceNUMATopology)
|
||||
self.assertIsNone(ctxt_obj.old_numa_topology)
|
||||
|
||||
@mock.patch('nova.db.instance_extra_update_by_uuid')
|
||||
def test_destroy(self, mock_update):
|
||||
objects.MigrationContext._destroy(self.context, fake_instance_uuid)
|
||||
self.assertEqual(1, len(mock_update.call_args_list))
|
||||
update_call = mock_update.call_args
|
||||
self.assertEqual(self.context, update_call[0][0])
|
||||
self.assertEqual(fake_instance_uuid, update_call[0][1])
|
||||
self.assertEqual({'migration_context': None}, update_call[0][2])
|
||||
|
||||
def _test_get_by_instance_uuid(self, db_data):
|
||||
mig_context = objects.MigrationContext.get_by_instance_uuid(
|
||||
self.context, fake_db_context['instance_uuid'])
|
||||
if mig_context:
|
||||
self.assertEqual(fake_db_context['instance_uuid'],
|
||||
mig_context.instance_uuid)
|
||||
expected_mig_context = db_data and db_data.get('migration_context')
|
||||
expected_mig_context = objects.MigrationContext.obj_from_db_obj(
|
||||
expected_mig_context)
|
||||
self.assertEqual(expected_mig_context.instance_uuid,
|
||||
mig_context.instance_uuid)
|
||||
self.assertEqual(expected_mig_context.migration_id,
|
||||
mig_context.migration_id)
|
||||
self.assertIsInstance(expected_mig_context.new_numa_topology,
|
||||
mig_context.new_numa_topology.__class__)
|
||||
self.assertIsInstance(expected_mig_context.old_numa_topology,
|
||||
mig_context.old_numa_topology.__class__)
|
||||
else:
|
||||
self.assertIsNone(mig_context)
|
||||
|
||||
@mock.patch('nova.db.instance_extra_get_by_instance_uuid')
|
||||
def test_get_by_instance_uuid(self, mock_get):
|
||||
mock_get.return_value = fake_db_context
|
||||
self._test_get_by_instance_uuid(fake_db_context)
|
||||
|
||||
@mock.patch('nova.db.instance_extra_get_by_instance_uuid')
|
||||
def test_get_by_instance_uuid_none(self, mock_get):
|
||||
db_context = fake_db_context.copy()
|
||||
db_context['migration_context'] = None
|
||||
mock_get.return_value = db_context
|
||||
self._test_get_by_instance_uuid(db_context)
|
||||
|
||||
@mock.patch('nova.db.instance_extra_get_by_instance_uuid')
|
||||
def test_get_by_instance_uuid_missing(self, mock_get):
|
||||
mock_get.return_value = None
|
||||
self.assertRaises(
|
||||
exception.MigrationContextNotFound,
|
||||
objects.MigrationContext.get_by_instance_uuid,
|
||||
self.context, 'fake_uuid')
|
||||
|
||||
|
||||
class TestMigrationContext(test_objects._LocalTest, _TestMigrationContext):
|
||||
pass
|
||||
|
||||
|
||||
class TestMigrationContextRemote(test_objects._RemoteTest,
|
||||
_TestMigrationContext):
|
||||
pass
|
|
@ -1128,6 +1128,7 @@ object_data = {
|
|||
'KeyPair': '1.3-bfaa2a8b148cdf11e0c72435d9dd097a',
|
||||
'KeyPairList': '1.2-58b94f96e776bedaf1e192ddb2a24c4e',
|
||||
'Migration': '1.2-8784125bedcea0a9227318511904e853',
|
||||
'MigrationContext': '1.0-d8c2f10069e410f639c49082b5932c92',
|
||||
'MigrationList': '1.2-02c0ec0c50b75ca86a2a74c5e8c911cc',
|
||||
'MonitorMetric': '1.1-53b1db7c4ae2c531db79761e7acc52ba',
|
||||
'MonitorMetricList': '1.1-15ecf022a68ddbb8c2a6739cfc9f8f5e',
|
||||
|
|
Loading…
Reference in New Issue