Record the host info in EventReporter

The instance action event is recorded in API/Conductor/Compute using
EventReporter. We already had self.host in API/Conductor/Compute
(where the EventReporter would be used) in change
Idf57fb5fbc611abb83943bd7e36d3cebf03b3977.

In this patch, the self.host param would be passed in EventReporter,
and then the host would be recorded in instance action event.

Part of blueprint: add-host-to-instance-action-events

Change-Id: I4436f5c1cc819c55ca9186bda0362bb74555c95a
This commit is contained in:
Yikun Jiang 2018-03-27 12:10:54 +08:00
parent 35e9c2bccf
commit cbf02e050e
7 changed files with 48 additions and 18 deletions

View File

@ -999,15 +999,17 @@ def get_stashed_volume_connector(bdm, instance):
class EventReporter(object):
"""Context manager to report instance action events."""
def __init__(self, context, event_name, *instance_uuids):
def __init__(self, context, event_name, host, *instance_uuids):
self.context = context
self.event_name = event_name
self.instance_uuids = instance_uuids
self.host = host
def __enter__(self):
for uuid in self.instance_uuids:
objects.InstanceActionEvent.event_start(
self.context, uuid, self.event_name, want_result=False)
self.context, uuid, self.event_name, want_result=False,
host=self.host)
return self
@ -1036,7 +1038,8 @@ def wrap_instance_event(prefix):
instance_uuid = keyed_args['instance']['uuid']
event_name = '{0}_{1}'.format(prefix, function.__name__)
with EventReporter(context, event_name, instance_uuid):
host = self.host if hasattr(self, 'host') else None
with EventReporter(context, event_name, host, instance_uuid):
return function(self, context, *args, **kwargs)
return decorated_function
return helper

View File

@ -281,7 +281,7 @@ class ComputeTaskManager(base.Base):
elif not live and not rebuild and flavor:
instance_uuid = instance.uuid
with compute_utils.EventReporter(context, 'cold_migrate',
instance_uuid):
self.host, instance_uuid):
self._cold_migrate(context, instance, flavor,
scheduler_hint['filter_properties'],
clean_shutdown, request_spec,
@ -737,7 +737,7 @@ class ComputeTaskManager(base.Base):
# instance during the shelve process
if image_id:
with compute_utils.EventReporter(
context, 'get_image_info', instance.uuid):
context, 'get_image_info', self.host, instance.uuid):
try:
image = safe_image_show(context, image_id)
except exception.ImageNotFound:
@ -753,7 +753,7 @@ class ComputeTaskManager(base.Base):
try:
with compute_utils.EventReporter(context, 'schedule_instances',
instance.uuid):
self.host, instance.uuid):
if not request_spec:
# NOTE(sbauza): We were unable to find an original
# RequestSpec object - probably because the instance is
@ -883,7 +883,7 @@ class ComputeTaskManager(base.Base):
request_spec=None):
with compute_utils.EventReporter(context, 'rebuild_server',
instance.uuid):
self.host, instance.uuid):
node = limits = None
try:

View File

@ -391,7 +391,8 @@ class ComputeVolumeTestCase(BaseTestCase):
self.compute.attach_volume(self.context, instance, bdm=fake_bdm)
self.assertEqual(self.cinfo.get('serial'), uuids.volume_id)
mock_event.assert_called_once_with(
self.context, 'compute_attach_volume', instance.uuid)
self.context, 'compute_attach_volume', CONF.host,
instance.uuid)
@mock.patch.object(compute_utils, 'EventReporter')
@mock.patch('nova.context.RequestContext.elevated')
@ -430,7 +431,8 @@ class ComputeVolumeTestCase(BaseTestCase):
exception=expected_exception),
])
mock_event.assert_called_once_with(
self.context, 'compute_attach_volume', instance.uuid)
self.context, 'compute_attach_volume', CONF.host,
instance.uuid)
@mock.patch('nova.context.RequestContext.elevated')
@mock.patch('nova.compute.utils.notify_about_volume_attach_detach')
@ -488,7 +490,8 @@ class ComputeVolumeTestCase(BaseTestCase):
self.context, uuids.volume, instance, 'fake_id')
self.assertFalse(mock_destroy.called)
mock_event.assert_called_once_with(
self.context, 'compute_detach_volume', instance.uuid)
self.context, 'compute_detach_volume', CONF.host,
instance.uuid)
@mock.patch.object(compute_utils, 'EventReporter')
def test_detach_volume_bdm_destroyed(self, mock_event):
@ -512,7 +515,8 @@ class ComputeVolumeTestCase(BaseTestCase):
uuids.attachment_id)
self.assertTrue(mock_destroy.called)
mock_event.assert_called_once_with(
self.context, 'compute_detach_volume', instance.uuid)
self.context, 'compute_detach_volume', CONF.host,
instance.uuid)
def test_await_block_device_created_too_slow(self):
self.flags(block_device_allocate_retries=2)
@ -3470,6 +3474,7 @@ class ComputeTestCase(BaseTestCase,
instance=inst_obj)
mock_event.assert_called_once_with(self.context,
'compute_snapshot_instance',
CONF.host,
inst_obj.uuid)
else:
self.assertRaises(test.TestingException,
@ -3479,6 +3484,7 @@ class ComputeTestCase(BaseTestCase,
rotation=1)
mock_event.assert_called_once_with(self.context,
'compute_backup_instance',
CONF.host,
inst_obj.uuid)
self.assertEqual(expected_state, self.fake_image_delete_called)
@ -6267,7 +6273,7 @@ class ComputeTestCase(BaseTestCase,
self.assertIsNone(ret)
mock_event.assert_called_with(
c, 'compute_live_migration', instance.uuid)
c, 'compute_live_migration', CONF.host, instance.uuid)
# cleanup
instance.destroy()
@ -10541,7 +10547,7 @@ class ComputeAPITestCase(BaseTestCase):
instance.uuid,
'/dev/vdb')
mock_event.assert_called_once_with(
self.context, 'api_attach_volume', instance.uuid
self.context, 'api_attach_volume', CONF.host, instance.uuid
)
self.assertTrue(mock_attach.called)
@ -10678,6 +10684,7 @@ class ComputeAPITestCase(BaseTestCase):
self.context, instance, instance_actions.DETACH_VOLUME)
mock_event.assert_called_once_with(self.context,
'api_detach_volume',
CONF.host,
instance.uuid)
self.assertTrue(mock_local_cleanup.called)
@ -10979,6 +10986,7 @@ class ComputeAPITestCase(BaseTestCase):
)
mock_event.assert_called_once_with(ctxt,
'api_lock',
CONF.host,
instance.uuid)
@mock.patch('nova.context.RequestContext.elevated')
@ -10996,6 +11004,7 @@ class ComputeAPITestCase(BaseTestCase):
)
mock_event.assert_called_once_with(ctxt,
'api_unlock',
CONF.host,
instance.uuid)
def test_add_remove_security_group(self):

View File

@ -3124,6 +3124,7 @@ class _ComputeAPIUnitTestMixIn(object):
instance_actions.CREATE_IMAGE)
mock_event.assert_called_once_with(self.context,
'api_snapshot_instance',
CONF.host,
instance.uuid)
bdm = fake_block_device.FakeDbBlockDeviceDict(
@ -3171,6 +3172,7 @@ class _ComputeAPIUnitTestMixIn(object):
instance_actions.CREATE_IMAGE)
mock_event.assert_called_once_with(self.context,
'api_snapshot_instance',
CONF.host,
instance.uuid)
instance.system_metadata['image_mappings'] = jsonutils.dumps(
@ -3229,6 +3231,7 @@ class _ComputeAPIUnitTestMixIn(object):
instance_actions.CREATE_IMAGE)
mock_event.assert_called_once_with(self.context,
'api_snapshot_instance',
CONF.host,
instance.uuid)
def test_snapshot_volume_backed(self):

View File

@ -1873,7 +1873,7 @@ class ComputeManagerUnitTestCase(test.NoDBTestCase):
mock.call(self.context, f_instance, e,
mock.ANY)])
event.assert_called_once_with(
self.context, 'compute_attach_interface',
self.context, 'compute_attach_interface', CONF.host,
f_instance.uuid)
with mock.patch.dict(self.compute.driver.capabilities,
@ -1900,7 +1900,7 @@ class ComputeManagerUnitTestCase(test.NoDBTestCase):
add_fault.assert_has_calls(
[mock.call(self.context, f_instance, mock.ANY, mock.ANY)])
event.assert_called_once_with(
self.context, 'compute_detach_interface',
self.context, 'compute_detach_interface', CONF.host,
f_instance.uuid)
do_test()
@ -2049,6 +2049,7 @@ class ComputeManagerUnitTestCase(test.NoDBTestCase):
uuids.old_volume, uuids.new_volume)
mock_event.assert_called_once_with(self.context,
'compute_swap_volume',
CONF.host,
instance1.uuid)
def _assert_volume_api(self, context, volume, *args):
@ -2493,7 +2494,7 @@ class ComputeManagerUnitTestCase(test.NoDBTestCase):
self.context, instance=instance,
dest_check_data=dest_check_data)
mock_event.assert_called_once_with(
self.context, 'compute_check_can_live_migrate_source',
self.context, 'compute_check_can_live_migrate_source', CONF.host,
instance.uuid)
mock_check.assert_called_once_with(self.context, instance,
dest_check_data,
@ -2559,7 +2560,7 @@ class ComputeManagerUnitTestCase(test.NoDBTestCase):
self.assertEqual(mig_data, result)
mock_event.assert_called_once_with(
self.context, 'compute_check_can_live_migrate_destination',
instance.uuid)
CONF.host, instance.uuid)
def test_check_can_live_migrate_destination_success(self):
self._test_check_can_live_migrate_destination()
@ -4623,7 +4624,7 @@ class ComputeManagerBuildInstanceTestCase(test.NoDBTestCase):
def _instance_action_events(self, mock_start, mock_finish):
mock_start.assert_called_once_with(self.context, self.instance.uuid,
mock.ANY, want_result=False)
mock.ANY, host=CONF.host, want_result=False)
mock_finish.assert_called_once_with(self.context, self.instance.uuid,
mock.ANY, exc_val=mock.ANY, exc_tb=mock.ANY, want_result=False)

View File

@ -993,6 +993,19 @@ class ComputeUtilsTestCase(test.NoDBTestCase):
self.context = context.RequestContext(self.user_id,
self.project_id)
@mock.patch.object(compute_utils, 'EventReporter')
def test_wrap_instance_event_without_host(self, mock_event):
inst = objects.Instance(uuid=uuids.instance)
@compute_utils.wrap_instance_event(prefix='compute')
def fake_event(self, context, instance):
pass
fake_event(self.compute, self.context, instance=inst)
# if the class doesn't include a self.host, the default host is None
mock_event.assert_called_once_with(self.context, 'compute_fake_event',
None, uuids.instance)
@mock.patch.object(objects.InstanceActionEvent, 'event_start')
@mock.patch.object(objects.InstanceActionEvent,
'event_finish_with_failure')

View File

@ -247,6 +247,7 @@ class ShelveComputeManagerTestCase(test_compute.BaseTestCase):
mock_delete_alloc.assert_called_once_with(self.context, instance)
mock_event.assert_called_once_with(self.context,
'compute_shelve_offload_instance',
CONF.host,
instance.uuid)
return instance