From 64ae9416e12929ce1b31343b197b8841d35de72c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A1bor=20Antal?= Date: Thu, 24 Nov 2016 12:50:28 +0100 Subject: [PATCH] Transform instance.volume_attach notification Change-Id: Ie10b3476c1d7cffeeb55d63cc8d9f2a166ec1109 Implements: bp versioned-notification-transformation-pike --- .../instance-volume_attach-end.json | 76 +++++++++++++++++++ .../instance-volume_attach-start.json | 76 +++++++++++++++++++ nova/compute/manager.py | 10 +++ nova/compute/utils.py | 27 +++++++ nova/notifications/objects/instance.py | 28 +++++++ .../test_instance.py | 54 +++++++++---- nova/tests/unit/compute/test_compute.py | 12 ++- .../objects/test_notification.py | 2 + 8 files changed, 269 insertions(+), 16 deletions(-) create mode 100644 doc/notification_samples/instance-volume_attach-end.json create mode 100644 doc/notification_samples/instance-volume_attach-start.json diff --git a/doc/notification_samples/instance-volume_attach-end.json b/doc/notification_samples/instance-volume_attach-end.json new file mode 100644 index 000000000000..a8161b293094 --- /dev/null +++ b/doc/notification_samples/instance-volume_attach-end.json @@ -0,0 +1,76 @@ +{ + "event_type": "instance.volume_attach.end", + "payload": { + "nova_object.data": { + "architecture": null, + "availability_zone": "nova", + "created_at": "2012-10-29T13:42:11Z", + "deleted_at": null, + "display_name": "some-server", + "display_description":"some-server", + "fault": null, + "flavor": { + "nova_object.name": "FlavorPayload", + "nova_object.data": { + "flavorid": "a22d5517-147c-4147-a0d1-e698df5cd4e3", + "name": "test_flavor", + "projects": null, + "root_gb": 1, + "vcpus": 1, + "ephemeral_gb": 0, + "memory_mb": 512, + "disabled": false, + "rxtx_factor": 1.0, + "extra_specs": { + "hw:watchdog_action": "disabled" + }, + "swap": 0, + "is_public": true, + "vcpu_weight": 0 + }, + "nova_object.version": "1.3", + "nova_object.namespace": "nova" + }, + "host": "compute", + "host_name": "some-server", + "image_uuid": "a2459075-d96c-40d5-893e-577ff92e721c", + "ip_addresses": [{ + "nova_object.data": { + "address": "192.168.1.3", + "device_name": "tapce531f90-19", + "label": "private-network", + "meta": {}, + "port_uuid": "ce531f90-199f-48c0-816c-13e38010b442", + "version": 4, + "mac": "fa:16:3e:4c:2c:30" + }, + "nova_object.name": "IpPayload", + "nova_object.namespace": "nova", + "nova_object.version": "1.0" + }], + "kernel_id": "", + "launched_at": "2012-10-29T13:42:11Z", + "metadata": {}, + "locked":false, + "node": "fake-mini", + "volume_id": "a07f71dc-8151-4e7d-a0cc-cd24a3f11113", + "os_type": null, + "power_state":"running", + "progress": 0, + "ramdisk_id": "", + "reservation_id": "r-6w6ruqaz", + "state": "active", + "task_state": null, + "tenant_id": "6f70656e737461636b20342065766572", + "terminated_at": null, + "auto_disk_config": "MANUAL", + "user_id": "fake", + "uuid": "0ab886d0-7443-4107-9265-48371bfa662b" + }, + "nova_object.name": "InstanceActionVolumePayload", + "nova_object.namespace": "nova", + "nova_object.version": "1.0" + }, + "priority": "INFO", + "publisher_id": "nova-compute:compute" +} diff --git a/doc/notification_samples/instance-volume_attach-start.json b/doc/notification_samples/instance-volume_attach-start.json new file mode 100644 index 000000000000..e0fa8cac5929 --- /dev/null +++ b/doc/notification_samples/instance-volume_attach-start.json @@ -0,0 +1,76 @@ +{ + "event_type": "instance.volume_attach.start", + "payload": { + "nova_object.data": { + "architecture": null, + "availability_zone": "nova", + "created_at": "2012-10-29T13:42:11Z", + "deleted_at": null, + "display_name": "some-server", + "display_description":"some-server", + "fault": null, + "flavor": { + "nova_object.name": "FlavorPayload", + "nova_object.data": { + "flavorid": "a22d5517-147c-4147-a0d1-e698df5cd4e3", + "name": "test_flavor", + "projects": null, + "root_gb": 1, + "vcpus": 1, + "ephemeral_gb": 0, + "memory_mb": 512, + "disabled": false, + "rxtx_factor": 1.0, + "extra_specs": { + "hw:watchdog_action": "disabled" + }, + "swap": 0, + "is_public": true, + "vcpu_weight": 0 + }, + "nova_object.version": "1.3", + "nova_object.namespace": "nova" + }, + "host": "compute", + "host_name": "some-server", + "image_uuid": "a2459075-d96c-40d5-893e-577ff92e721c", + "ip_addresses": [{ + "nova_object.data": { + "address": "192.168.1.3", + "device_name": "tapce531f90-19", + "label": "private-network", + "meta": {}, + "port_uuid": "ce531f90-199f-48c0-816c-13e38010b442", + "version": 4, + "mac": "fa:16:3e:4c:2c:30" + }, + "nova_object.name": "IpPayload", + "nova_object.namespace": "nova", + "nova_object.version": "1.0" + }], + "kernel_id": "", + "launched_at": "2012-10-29T13:42:11Z", + "metadata": {}, + "locked":false, + "node": "fake-mini", + "volume_id": "a07f71dc-8151-4e7d-a0cc-cd24a3f11113", + "os_type": null, + "power_state":"running", + "progress": 0, + "ramdisk_id": "", + "reservation_id": "r-6w6ruqaz", + "state": "active", + "task_state": null, + "tenant_id": "6f70656e737461636b20342065766572", + "terminated_at": null, + "auto_disk_config": "MANUAL", + "user_id": "fake", + "uuid": "0ab886d0-7443-4107-9265-48371bfa662b" + }, + "nova_object.name": "InstanceActionVolumePayload", + "nova_object.namespace": "nova", + "nova_object.version": "1.0" + }, + "priority": "INFO", + "publisher_id": "nova-compute:compute" +} diff --git a/nova/compute/manager.py b/nova/compute/manager.py index 9479a4158bf9..30d3a1b5bbcd 100644 --- a/nova/compute/manager.py +++ b/nova/compute/manager.py @@ -4811,6 +4811,11 @@ class ComputeManager(manager.Manager): {'volume_id': bdm.volume_id, 'mountpoint': bdm['mount_device']}, instance=instance) + compute_utils.notify_about_volume_attach_detach( + context, instance, self.host, + action=fields.NotificationAction.VOLUME_ATTACH, + phase=fields.NotificationPhase.START, + volume_id=bdm.volume_id) try: bdm.attach(context, instance, self.volume_api, self.driver, do_driver_attach=True) @@ -4826,6 +4831,11 @@ class ComputeManager(manager.Manager): info = {'volume_id': bdm.volume_id} self._notify_about_instance_usage( context, instance, "volume.attach", extra_usage_info=info) + compute_utils.notify_about_volume_attach_detach( + context, instance, self.host, + action=fields.NotificationAction.VOLUME_ATTACH, + phase=fields.NotificationPhase.END, + volume_id=bdm.volume_id) def _notify_volume_usage_detach(self, context, instance, bdm): if CONF.volume_usage_poll_interval <= 0: diff --git a/nova/compute/utils.py b/nova/compute/utils.py index ca3340d62dfe..1481a0eaa2d5 100644 --- a/nova/compute/utils.py +++ b/nova/compute/utils.py @@ -366,6 +366,33 @@ def notify_about_instance_action(context, instance, host, action, phase=None, notification.emit(context) +def notify_about_volume_attach_detach(context, instance, host, action, phase, + binary='nova-compute', volume_id=None): + """Send versioned notification about the action made on the instance + :param instance: the instance which the action performed on + :param host: the host emitting the notification + :param action: the name of the action + :param phase: the phase of the action + :param binary: the binary emitting the notification + :param volume_id: id of the volume will be attached + """ + payload = instance_notification.InstanceActionVolumePayload( + instance=instance, + fault=None, + volume_id=volume_id) + notification = instance_notification.InstanceActionVolumeNotification( + context=context, + priority=fields.NotificationPriority.INFO, + publisher=notification_base.NotificationPublisher( + context=context, host=host, binary=binary), + event_type=notification_base.EventType( + object='instance', + action=action, + phase=phase), + payload=payload) + notification.emit(context) + + def notify_about_volume_swap(context, instance, host, action, phase, old_volume_id, new_volume_id, exception=None): """Send versioned notification about the volume swap action diff --git a/nova/notifications/objects/instance.py b/nova/notifications/objects/instance.py index e0bd27795a52..780fc86df138 100644 --- a/nova/notifications/objects/instance.py +++ b/nova/notifications/objects/instance.py @@ -129,6 +129,22 @@ class InstanceActionPayload(InstancePayload): **kwargs) +@nova_base.NovaObjectRegistry.register_notification +class InstanceActionVolumePayload(InstanceActionPayload): + # Version 1.0: Initial version + + VERSION = '1.0' + fields = { + 'volume_id': fields.UUIDField() + } + + def __init__(self, instance, fault, volume_id): + super(InstanceActionVolumePayload, self).__init__( + instance=instance, + fault=fault, + volume_id=volume_id) + + @nova_base.NovaObjectRegistry.register_notification class InstanceActionVolumeSwapPayload(InstanceActionPayload): # No SCHEMA as all the additional fields are calculated @@ -343,3 +359,15 @@ class InstanceActionVolumeSwapNotification(base.NotificationBase): fields = { 'payload': fields.ObjectField('InstanceActionVolumeSwapPayload') } + + +@base.notification_sample('instance-volume_attach-start.json') +@base.notification_sample('instance-volume_attach-end.json') +@nova_base.NovaObjectRegistry.register_notification +class InstanceActionVolumeNotification(base.NotificationBase): + # Version 1.0: Initial version + VERSION = '1.0' + + fields = { + 'payload': fields.ObjectField('InstanceActionVolumePayload') + } diff --git a/nova/tests/functional/notification_sample_tests/test_instance.py b/nova/tests/functional/notification_sample_tests/test_instance.py index 276346470670..978bd90787f7 100644 --- a/nova/tests/functional/notification_sample_tests/test_instance.py +++ b/nova/tests/functional/notification_sample_tests/test_instance.py @@ -686,6 +686,10 @@ class TestInstanceNotificationSample( def _attach_volume_to_server(self, server, volume_id): self.api.post_server_volume( server['id'], {"volumeAttachment": {"volumeId": volume_id}}) + self._wait_for_notification('instance.volume_attach.end') + + def _detach_volume_from_server(self, server, volume_id): + self.api.delete_server_volume(server['id'], volume_id) def _volume_swap_server(self, server, attachement_id, volume_id): self.api.put_server_volume(server['id'], attachement_id, volume_id) @@ -702,19 +706,19 @@ class TestInstanceNotificationSample( self.cinder.SWAP_NEW_VOL) self._wait_until_swap_volume(server, self.cinder.SWAP_NEW_VOL) - self.assertEqual(4, len(fake_notifier.VERSIONED_NOTIFICATIONS)) + self.assertEqual(6, len(fake_notifier.VERSIONED_NOTIFICATIONS)) self._verify_notification( 'instance-volume_swap-start', replacements={ 'reservation_id': server['reservation_id'], 'uuid': server['id']}, - actual=fake_notifier.VERSIONED_NOTIFICATIONS[2]) + actual=fake_notifier.VERSIONED_NOTIFICATIONS[4]) self._verify_notification( 'instance-volume_swap-end', replacements={ 'reservation_id': server['reservation_id'], 'uuid': server['id']}, - actual=fake_notifier.VERSIONED_NOTIFICATIONS[3]) + actual=fake_notifier.VERSIONED_NOTIFICATIONS[5]) def test_volume_swap_server_with_error(self): server = self._boot_a_server( @@ -727,19 +731,21 @@ class TestInstanceNotificationSample( self.cinder.SWAP_ERR_NEW_VOL) self._wait_until_swap_volume_error() - # Five versioned notifications are generated. We only rely on the - # first four because _wait_until_swap_volume_error will return True + # Seven versioned notifications are generated. We only rely on the + # first six because _wait_until_swap_volume_error will return True # after volume_api.unreserve is called on the cinder fixture, and that # happens before the instance fault is handled in the compute manager - # which generates the 5th notification (compute.exception). + # which generates the last notification (compute.exception). # 0. instance-create-start - # 1. instance-create-start - # 2. instance-volume_swap-start - # 3. instance-volume_swap-error - # 4. compute.exception - self.assertTrue(len(fake_notifier.VERSIONED_NOTIFICATIONS) >= 4, + # 1. instance-create-end + # 2. instance-volume_attach-start + # 3. instance-volume_attach-end + # 4. instance-volume_swap-start + # 5. instance-volume_swap-error + # 6. compute.exception + self.assertTrue(len(fake_notifier.VERSIONED_NOTIFICATIONS) >= 6, 'Unexpected number of versioned notifications. ' - 'Expected at least 4, got: %s' % + 'Expected at least 6, got: %s' % len(fake_notifier.VERSIONED_NOTIFICATIONS)) self._verify_notification( 'instance-volume_swap-start', @@ -748,13 +754,13 @@ class TestInstanceNotificationSample( 'old_volume_id': self.cinder.SWAP_ERR_OLD_VOL, 'reservation_id': server['reservation_id'], 'uuid': server['id']}, - actual=fake_notifier.VERSIONED_NOTIFICATIONS[2]) + actual=fake_notifier.VERSIONED_NOTIFICATIONS[4]) self._verify_notification( 'instance-volume_swap-error', replacements={ 'reservation_id': server['reservation_id'], 'uuid': server['id']}, - actual=fake_notifier.VERSIONED_NOTIFICATIONS[3]) + actual=fake_notifier.VERSIONED_NOTIFICATIONS[5]) def _test_revert_server(self, server): pass @@ -766,7 +772,25 @@ class TestInstanceNotificationSample( pass def _test_volume_attach_detach_server(self, server): - pass + self._attach_volume_to_server(server, self.cinder.SWAP_OLD_VOL) + + # 0. volume_attach-start + # 1. volume_attach-end + self.assertEqual(2, len(fake_notifier.VERSIONED_NOTIFICATIONS)) + self._verify_notification( + 'instance-volume_attach-start', + replacements={ + 'reservation_id': server['reservation_id'], + 'uuid': server['id']}, + actual=fake_notifier.VERSIONED_NOTIFICATIONS[0]) + self._verify_notification( + 'instance-volume_attach-end', + replacements={ + 'reservation_id': server['reservation_id'], + 'uuid': server['id']}, + actual=fake_notifier.VERSIONED_NOTIFICATIONS[1]) + + self._detach_volume_from_server(server, self.cinder.SWAP_OLD_VOL) def _test_rescue_server(self, server): pass diff --git a/nova/tests/unit/compute/test_compute.py b/nova/tests/unit/compute/test_compute.py index 8cd31e2c61ab..11a5fe891775 100644 --- a/nova/tests/unit/compute/test_compute.py +++ b/nova/tests/unit/compute/test_compute.py @@ -747,6 +747,8 @@ class ComputeVolumeTestCase(BaseTestCase): mock_get_bdms.assert_called_once_with(ctxt, use_slave=True) mock_update.assert_called_once_with(ctxt, [3, 4]) + @mock.patch('nova.context.RequestContext.elevated') + @mock.patch('nova.compute.utils.notify_about_volume_attach_detach') @mock.patch.object(objects.BlockDeviceMapping, 'get_by_volume_and_instance') @mock.patch.object(fake.FakeDriver, 'block_stats') @@ -754,7 +756,9 @@ class ComputeVolumeTestCase(BaseTestCase): @mock.patch.object(fake.FakeDriver, 'get_all_volume_usage') @mock.patch.object(fake.FakeDriver, 'instance_exists') def test_detach_volume_usage(self, mock_exists, mock_get_all, - mock_get_bdms, mock_stats, mock_get): + mock_get_bdms, mock_stats, mock_get, + mock_notify, mock_elevate): + mock_elevate.return_value = self.context # Test that detach volume update the volume usage cache table correctly instance = self._create_fake_instance_obj() bdm = objects.BlockDeviceMapping(context=self.context, @@ -830,6 +834,12 @@ class ComputeVolumeTestCase(BaseTestCase): self.assertEqual(1, volume_usage['tot_writes']) self.assertEqual(20, volume_usage['tot_write_bytes']) + mock_notify.assert_has_calls([ + mock.call(self.context, instance, 'fake-mini', + action='volume_attach', phase='start', + volume_id=uuids.volume_id), + ]) + mock_get.assert_called_once_with(self.context, uuids.volume_id, instance.uuid) mock_stats.assert_called_once_with(instance, 'vdb') diff --git a/nova/tests/unit/notifications/objects/test_notification.py b/nova/tests/unit/notifications/objects/test_notification.py index 96cca0f13caa..2d6b17b2cd4f 100644 --- a/nova/tests/unit/notifications/objects/test_notification.py +++ b/nova/tests/unit/notifications/objects/test_notification.py @@ -358,6 +358,8 @@ notification_object_data = { 'FlavorPayload': '1.3-6335e626893d7df5f96f87e6731fef56', 'InstanceActionNotification': '1.0-a73147b93b520ff0061865849d3dfa56', 'InstanceActionPayload': '1.2-b7b2481bcd0e1edcc1970ef7150df5aa', + 'InstanceActionVolumeNotification': '1.0-a73147b93b520ff0061865849d3dfa56', + 'InstanceActionVolumePayload': '1.0-20c0dca4cfaf1a68d3e8c45e5aca3907', 'InstanceActionVolumeSwapNotification': '1.0-a73147b93b520ff0061865849d3dfa56', 'InstanceActionVolumeSwapPayload': '1.2-d7925b763e0795f8e5c1aa0e95bd67bd',