Add InstanceAction/Event create() method

During a cross-cell resize, the instance and its
related records are created in the target cell. Since
the resize is initiated in the source cell, instance
actions and associated events will be created in the
source cell and need to be re-created in the target cell
where the resize completes. Not only that, but we want
to also make sure the history of actions performed on
an instance are copied into the target cell database
in case the resize is confirmed there.

This adds a create() method for the InstanceAction
and InstanceActionEvent objects to allow directly
re-creating those resources from the source cell into
the target cell.

The existing action_start() and event_start() methods
will not work for this scenario since those depend on the
current context but we need to clone actions/events created
with an older/different context.

Part of blueprint cross-cell-resize

Change-Id: I43db251f708c18eb35efe1fdbcdada35ae7e792c
This commit is contained in:
Matt Riedemann 2018-10-29 16:51:09 -04:00
parent af40e3d1a6
commit b92276d4ed
3 changed files with 75 additions and 4 deletions

View File

@ -16,6 +16,7 @@ from oslo_utils import timeutils
from oslo_utils import versionutils
from nova.db import api as db
from nova import exception
from nova import objects
from nova.objects import base
from nova.objects import fields
@ -27,7 +28,8 @@ class InstanceAction(base.NovaPersistentObject, base.NovaObject,
base.NovaObjectDictCompat):
# Version 1.0: Initial version
# Version 1.1: String attributes updated to support unicode
VERSION = '1.1'
# Version 1.2: Add create() method.
VERSION = '1.2'
fields = {
'id': fields.IntegerField(),
@ -97,6 +99,19 @@ class InstanceAction(base.NovaPersistentObject, base.NovaObject,
db_action = db.action_finish(self._context, values)
self._from_db_object(self._context, self, db_action)
# NOTE(mriedem): In most cases, the action_start() method should be used
# to create new InstanceAction records. This method should only be used
# in specific exceptional cases like when cloning actions from one cell
# database to another.
@base.remotable
def create(self):
if 'id' in self:
raise exception.ObjectActionError(action='create',
reason='already created')
updates = self.obj_get_changes()
db_action = db.action_start(self._context, updates)
self._from_db_object(self._context, self, db_action)
@base.NovaObjectRegistry.register
class InstanceActionList(base.ObjectListBase, base.NovaObject):
@ -122,7 +137,8 @@ class InstanceActionEvent(base.NovaPersistentObject, base.NovaObject,
# Version 1.0: Initial version
# Version 1.1: event_finish_with_failure decorated with serialize_args
# Version 1.2: Add 'host' field
VERSION = '1.2'
# Version 1.3: Add create() method.
VERSION = '1.3'
fields = {
'id': fields.IntegerField(),
'event': fields.StringField(nullable=True),
@ -218,6 +234,26 @@ class InstanceActionEvent(base.NovaPersistentObject, base.NovaObject,
def finish(self):
self.finish_with_failure(self._context, exc_val=None, exc_tb=None)
# NOTE(mriedem): In most cases, the event_start() method should be used
# to create new InstanceActionEvent records. This method should only be
# used in specific exceptional cases like when cloning events from one cell
# database to another.
@base.remotable
def create(self, instance_uuid, request_id):
if 'id' in self:
raise exception.ObjectActionError(action='create',
reason='already created')
updates = self.obj_get_changes()
# The instance_uuid and request_id uniquely identify the "parent"
# InstanceAction for this event and are used in action_event_start().
# TODO(mriedem): This could be optimized if we just didn't use
# db.action_event_start and inserted the record ourselves and passed
# in the action_id.
updates['instance_uuid'] = instance_uuid
updates['request_id'] = request_id
db_event = db.action_event_start(self._context, updates)
self._from_db_object(self._context, self, db_event)
@base.NovaObjectRegistry.register
class InstanceActionEventList(base.ObjectListBase, base.NovaObject):

View File

@ -21,6 +21,7 @@ from oslo_utils import timeutils
import six
from nova.db import api as db
from nova import exception
from nova.objects import instance_action
from nova import test
from nova.tests.unit.objects import test_objects
@ -180,6 +181,21 @@ class _TestInstanceActionObject(object):
mock_get.assert_called_once_with(self.context, 'fake-uuid', None,
None, None)
def test_create_id_in_updates_error(self):
action = instance_action.InstanceAction(self.context, id=1)
ex = self.assertRaises(exception.ObjectActionError, action.create)
self.assertIn('already created', six.text_type(ex))
@mock.patch('nova.db.api.action_start')
def test_create(self, mock_action_start):
mock_action_start.return_value = fake_action
action = instance_action.InstanceAction(self.context)
expected_updates = action.obj_get_changes()
action.create()
mock_action_start.assert_called_once_with(
self.context, expected_updates)
self.compare_obj(action, fake_action)
class TestInstanceActionObject(test_objects._LocalTest,
_TestInstanceActionObject):
@ -362,6 +378,25 @@ class _TestInstanceActionEventObject(object):
exc_tb='traceback')
mock_format.assert_called_once_with(mock.sentinel.exc_tb)
def test_create_id_in_updates_error(self):
event = instance_action.InstanceActionEvent(self.context, id=1)
ex = self.assertRaises(
exception.ObjectActionError, event.create,
fake_action['instance_uuid'], fake_action['request_id'])
self.assertIn('already created', six.text_type(ex))
@mock.patch('nova.db.api.action_event_start')
def test_create(self, mock_action_event_start):
mock_action_event_start.return_value = fake_event
event = instance_action.InstanceActionEvent(self.context)
expected_updates = event.obj_get_changes()
expected_updates['instance_uuid'] = fake_action['instance_uuid']
expected_updates['request_id'] = fake_action['request_id']
event.create(fake_action['instance_uuid'], fake_action['request_id'])
mock_action_event_start.assert_called_once_with(
self.context, expected_updates)
self.compare_obj(event, fake_event)
class TestInstanceActionEventObject(test_objects._LocalTest,
_TestInstanceActionEventObject):

View File

@ -1071,8 +1071,8 @@ object_data = {
'ImageMeta': '1.8-642d1b2eb3e880a367f37d72dd76162d',
'ImageMetaProps': '1.22-b1c9ea78c43e8d7989a7d1f0f34d149a',
'Instance': '2.5-94e3881f0b9a06c2c3640e44816b51de',
'InstanceAction': '1.1-f9f293e526b66fca0d05c3b3a2d13914',
'InstanceActionEvent': '1.2-b2f368b8a29d8d872b1f6ea841e820a0',
'InstanceAction': '1.2-9a5abc87fdd3af46f45731960651efb5',
'InstanceActionEvent': '1.3-c749e1b3589e7117c81cb2aa6ac438d5',
'InstanceActionEventList': '1.1-13d92fb953030cdbfee56481756e02be',
'InstanceActionList': '1.1-a2b2fb6006b47c27076d3a1d48baa759',
'InstanceDeviceMetadata': '1.0-74d78dd36aa32d26d2769a1b57caf186',