diff --git a/doc/notification_samples/metrics-update.json b/doc/notification_samples/metrics-update.json new file mode 100644 index 0000000000..b91ab9c7e6 --- /dev/null +++ b/doc/notification_samples/metrics-update.json @@ -0,0 +1,137 @@ +{ + "event_type": "metrics.update", + "payload": { + "nova_object.version": "1.0", + "nova_object.name": "MetricsPayload", + "nova_object.namespace": "nova", + "nova_object.data": { + "host_ip": "10.0.2.15", + "host": "compute", + "nodename": "fake-mini", + "metrics":[ + { + "nova_object.version": "1.0", + "nova_object.name": "MetricPayload", + "nova_object.namespace": "nova", + "nova_object.data": { + "timestamp": "2012-10-29T13:42:11Z", + "source": "fake.SmallFakeDriver", + "numa_membw_values": null, + "name": "cpu.iowait.percent", + "value": 0 + } + }, + { + "nova_object.version": "1.0", + "nova_object.name": "MetricPayload", + "nova_object.namespace": "nova", + "nova_object.data": { + "timestamp": "2012-10-29T13:42:11Z", + "source": "fake.SmallFakeDriver", + "numa_membw_values": null, + "name": "cpu.frequency", + "value": 800 + } + }, + { + "nova_object.version": "1.0", + "nova_object.name": "MetricPayload", + "nova_object.namespace": "nova", + "nova_object.data": { + "timestamp": "2012-10-29T13:42:11Z", + "source": "fake.SmallFakeDriver", + "numa_membw_values": null, + "name": "cpu.idle.percent", + "value": 97 + } + }, + { + "nova_object.version": "1.0", + "nova_object.name": "MetricPayload", + "nova_object.namespace": "nova", + "nova_object.data": { + "timestamp": "2012-10-29T13:42:11Z", + "source": "fake.SmallFakeDriver", + "numa_membw_values": null, + "name": "cpu.iowait.time", + "value": 6121490000000 + } + }, + { + "nova_object.version": "1.0", + "nova_object.name": "MetricPayload", + "nova_object.namespace": "nova", + "nova_object.data": { + "timestamp": "2012-10-29T13:42:11Z", + "source": "fake.SmallFakeDriver", + "numa_membw_values": null, + "name": "cpu.kernel.percent", + "value": 0 + } + }, + { + "nova_object.version": "1.0", + "nova_object.name": "MetricPayload", + "nova_object.namespace": "nova", + "nova_object.data": { + "timestamp": "2012-10-29T13:42:11Z", + "source": "fake.SmallFakeDriver", + "numa_membw_values": null, + "name": "cpu.kernel.time", + "value": 5664160000000 + } + }, + { + "nova_object.version": "1.0", + "nova_object.name": "MetricPayload", + "nova_object.namespace": "nova", + "nova_object.data": { + "timestamp": "2012-10-29T13:42:11Z", + "source": "fake.SmallFakeDriver", + "numa_membw_values": null, + "name": "cpu.percent", + "value": 2 + } + }, + { + "nova_object.version": "1.0", + "nova_object.name": "MetricPayload", + "nova_object.namespace": "nova", + "nova_object.data": { + "timestamp": "2012-10-29T13:42:11Z", + "source": "fake.SmallFakeDriver", + "numa_membw_values": null, + "name": "cpu.user.percent", + "value": 1 + } + }, + { + "nova_object.version": "1.0", + "nova_object.name": "MetricPayload", + "nova_object.namespace": "nova", + "nova_object.data": { + "timestamp": "2012-10-29T13:42:11Z", + "source": "fake.SmallFakeDriver", + "numa_membw_values": null, + "name": "cpu.user.time", + "value": 26728850000000 + } + }, + { + "nova_object.version": "1.0", + "nova_object.name": "MetricPayload", + "nova_object.namespace": "nova", + "nova_object.data": { + "timestamp": "2012-10-29T13:42:11Z", + "source": "fake.SmallFakeDriver", + "numa_membw_values": null, + "name": "cpu.idle.time", + "value": 1592705190000000 + } + } + ] + } + }, + "priority": "INFO", + "publisher_id": "nova-compute:compute" +} diff --git a/nova/compute/resource_tracker.py b/nova/compute/resource_tracker.py index 7084548b20..d7dc52288d 100644 --- a/nova/compute/resource_tracker.py +++ b/nova/compute/resource_tracker.py @@ -640,15 +640,17 @@ class ResourceTracker(object): {'mon': monitor, 'exc': exc}) # TODO(jaypipes): Remove this when compute_node.metrics doesn't need # to be populated as a JSONified string. - metrics = metrics.to_list() - if len(metrics): + metric_list = metrics.to_list() + if len(metric_list): metrics_info['nodename'] = nodename - metrics_info['metrics'] = metrics + metrics_info['metrics'] = metric_list metrics_info['host'] = self.host metrics_info['host_ip'] = CONF.my_ip notifier = rpc.get_notifier(service='compute', host=nodename) notifier.info(context, 'compute.metrics.update', metrics_info) - return metrics + compute_utils.notify_about_metrics_update( + context, self.host, CONF.my_ip, nodename, metrics) + return metric_list def update_available_resource(self, context, nodename): """Override in-memory calculations of compute node resource usage based diff --git a/nova/compute/utils.py b/nova/compute/utils.py index 82029d034d..c7aa45ff0d 100644 --- a/nova/compute/utils.py +++ b/nova/compute/utils.py @@ -40,6 +40,7 @@ from nova.notifications.objects import exception as notification_exception from nova.notifications.objects import flavor as flavor_notification from nova.notifications.objects import instance as instance_notification from nova.notifications.objects import keypair as keypair_notification +from nova.notifications.objects import metrics as metrics_notification from nova.notifications.objects import server_group as sg_notification from nova import objects from nova.objects import fields @@ -759,6 +760,34 @@ def notify_about_instance_rebuild(context, instance, host, notification.emit(context) +@rpc.if_notifications_enabled +def notify_about_metrics_update(context, host, host_ip, nodename, + monitor_metric_list): + """Send versioned notification about updating metrics + + :param context: the request context + :param host: the host emitting the notification + :param host_ip: the IP address of the host + :param nodename: the node name + :param monitor_metric_list: the MonitorMetricList object + """ + payload = metrics_notification.MetricsPayload( + host=host, + host_ip=host_ip, + nodename=nodename, + monitor_metric_list=monitor_metric_list) + notification = metrics_notification.MetricsNotification( + context=context, + priority=fields.NotificationPriority.INFO, + publisher=notification_base.NotificationPublisher( + host=host, source=fields.NotificationSource.COMPUTE), + event_type=notification_base.EventType( + object='metrics', + action=fields.NotificationAction.UPDATE), + payload=payload) + notification.emit(context) + + def refresh_info_cache_for_instance(context, instance): """Refresh the info cache for an instance. diff --git a/nova/notifications/objects/metrics.py b/nova/notifications/objects/metrics.py new file mode 100644 index 0000000000..0a04243ae9 --- /dev/null +++ b/nova/notifications/objects/metrics.py @@ -0,0 +1,85 @@ +# Copyright 2018 NTT Corporation +# +# 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 nova.notifications.objects import base +from nova.objects import base as nova_base +from nova.objects import fields + + +@base.notification_sample('metrics-update.json') +@nova_base.NovaObjectRegistry.register_notification +class MetricsNotification(base.NotificationBase): + # Version 1.0: Initial version + VERSION = '1.0' + + fields = { + 'payload': fields.ObjectField('MetricsPayload') + } + + +@nova_base.NovaObjectRegistry.register_notification +class MetricPayload(base.NotificationPayloadBase): + # Version 1.0: Initial version + VERSION = '1.0' + + SCHEMA = { + 'name': ('monitor_metric', 'name'), + 'value': ('monitor_metric', 'value'), + 'numa_membw_values': ('monitor_metric', 'numa_membw_values'), + 'timestamp': ('monitor_metric', 'timestamp'), + 'source': ('monitor_metric', 'source'), + } + + fields = { + 'name': fields.MonitorMetricTypeField(), + 'value': fields.IntegerField(), + 'numa_membw_values': fields.DictOfIntegersField(nullable=True), + 'timestamp': fields.DateTimeField(), + 'source': fields.StringField(), + } + + def __init__(self, monitor_metric): + super(MetricPayload, self).__init__() + self.populate_schema(monitor_metric=monitor_metric) + + @classmethod + def from_monitor_metric_list_obj(cls, monitor_metric_list): + """Returns a list of MetricPayload objects based on the passed + MonitorMetricList object. + """ + payloads = [] + for monitor_metric in monitor_metric_list: + payloads.append(cls(monitor_metric)) + return payloads + + +@nova_base.NovaObjectRegistry.register_notification +class MetricsPayload(base.NotificationPayloadBase): + # Version 1.0: Initial version + VERSION = '1.0' + + fields = { + 'host': fields.StringField(), + 'host_ip': fields.StringField(), + 'nodename': fields.StringField(), + 'metrics': fields.ListOfObjectsField('MetricPayload'), + } + + def __init__(self, host, host_ip, nodename, monitor_metric_list): + super(MetricsPayload, self).__init__() + self.host = host + self.host_ip = host_ip + self.nodename = nodename + self.metrics = MetricPayload.from_monitor_metric_list_obj( + monitor_metric_list) diff --git a/nova/tests/functional/notification_sample_tests/test_metrics.py b/nova/tests/functional/notification_sample_tests/test_metrics.py new file mode 100644 index 0000000000..59ce17c68e --- /dev/null +++ b/nova/tests/functional/notification_sample_tests/test_metrics.py @@ -0,0 +1,42 @@ +# Copyright 2018 NTT Corporation +# +# 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 nova.conf +from nova import context +from nova.tests.functional.notification_sample_tests \ + import notification_sample_base +from nova.tests.unit import fake_notifier + + +CONF = nova.conf.CONF + + +class TestMetricsNotificationSample( + notification_sample_base.NotificationSampleTestBase): + + def setUp(self): + self.flags(compute_monitors=['cpu.virt_driver']) + super(TestMetricsNotificationSample, self).setUp() + # Reset the cpu stats of the 'cpu.virt_driver' monitor + self.compute.manager._resource_tracker.monitors[0]._cpu_stats = {} + + def test_metrics_update(self): + self.compute.manager.update_available_resource( + context.get_admin_context()) + + self.assertEqual(1, len(fake_notifier.VERSIONED_NOTIFICATIONS)) + self._verify_notification( + 'metrics-update', + replacements={'host_ip': CONF.my_ip}, + actual=fake_notifier.VERSIONED_NOTIFICATIONS[0]) diff --git a/nova/tests/unit/compute/test_resource_tracker.py b/nova/tests/unit/compute/test_resource_tracker.py index 32f6f233ab..7c6f39e90c 100644 --- a/nova/tests/unit/compute/test_resource_tracker.py +++ b/nova/tests/unit/compute/test_resource_tracker.py @@ -2995,7 +2995,8 @@ class ComputeMonitorTestCase(BaseTestCase): u'Cannot get the metrics from %(mon)s; error: %(exc)s', mock.ANY) self.assertEqual(0, len(metrics)) - def test_get_host_metrics(self): + @mock.patch('nova.compute.utils.notify_about_metrics_update') + def test_get_host_metrics(self, mock_notify): fake_notifier.stub_notifier(self) self.addCleanup(fake_notifier.reset) @@ -3022,6 +3023,10 @@ class ComputeMonitorTestCase(BaseTestCase): metrics = self.rt._get_host_metrics(self.context, _NODENAME) + mock_notify.assert_called_once_with( + self.context, _HOSTNAME, '1.1.1.1', _NODENAME, + test.MatchType(objects.MonitorMetricList)) + expected_metrics = [ { 'timestamp': FakeCPUMonitor.NOW_TS.isoformat(), diff --git a/nova/tests/unit/notifications/objects/test_notification.py b/nova/tests/unit/notifications/objects/test_notification.py index eacb7de8b8..ca1cd785e6 100644 --- a/nova/tests/unit/notifications/objects/test_notification.py +++ b/nova/tests/unit/notifications/objects/test_notification.py @@ -404,6 +404,9 @@ notification_object_data = { 'IpPayload': '1.0-8ecf567a99e516d4af094439a7632d34', 'KeypairNotification': '1.0-a73147b93b520ff0061865849d3dfa56', 'KeypairPayload': '1.0-6daebbbde0e1bf35c1556b1ecd9385c1', + 'MetricPayload': '1.0-bcdbe85048f335132e4c82a1b8fa3da8', + 'MetricsNotification': '1.0-a73147b93b520ff0061865849d3dfa56', + 'MetricsPayload': '1.0-65c69b15b4de5a8c01971cb5bb9ab650', 'NotificationPublisher': '2.2-b6ad48126247e10b46b6b0240e52e614', 'ServerGroupNotification': '1.0-a73147b93b520ff0061865849d3dfa56', 'ServerGroupPayload': '1.1-4ded2997ea1b07038f7af33ef5c45f7f',