Merge "Implement host notification monitor plugin"

This commit is contained in:
Zuul 2018-02-05 09:59:48 +00:00 committed by Gerrit Code Review
commit caa8ecbd34
4 changed files with 83 additions and 11 deletions

View File

@ -87,8 +87,8 @@ class NotificationMonitor(base.BaseMonitor):
self.handlers[event_type].append(
# Wrap a notification callback with the update_statuses()
# to manage statuses of leases and reservations.
lambda e_type, message: self.update_statuses(
plugin.notification_callback, e_type, message))
lambda e_type, payload: self.update_statuses(
plugin.notification_callback, e_type, payload))
return [NotificationEndpoint(self)]

View File

@ -116,13 +116,13 @@ class BaseMonitorPlugin():
pass
@abc.abstractmethod
def notification_callback(self, event_type, message):
def notification_callback(self, event_type, payload):
"""Handle a notification message.
It is used as a callback of a notification based resource monitor.
:param event_type: an event type of message.
:param message: a message passed by monitors.
:param event_type: an event type of a notification.
:param payload: a payload of a notification.
:return: a dictionary of {reservation id: flags to update}
e.g. {'de27786d-bd96-46bb-8363-19c13b2c6657':
{'missing_resources': True}}

View File

@ -639,22 +639,31 @@ class PhysicalHostMonitorPlugin(base.BaseMonitorPlugin,
"""Get topics of notification to subscribe to."""
return CONF[plugin.RESOURCE_TYPE].notification_topics
def notification_callback(self, event_type, message):
def notification_callback(self, event_type, payload):
"""Handle a notification message.
It is used as a callback of a notification-based resource monitor.
:param event_type: an event type of message.
:param message: a message passed by monitors.
:param event_type: an event type of a notification.
:param payload: a payload of a notification.
:return: a dictionary of {reservation id: flags to update}
e.g. {'de27786d-bd96-46bb-8363-19c13b2c6657':
{'missing_resources': True}}
"""
LOG.debug('notification_callback() is called.')
LOG.trace('Handling a notification...')
reservation_flags = {}
# TODO(hiro-kobayashi): Implement this method
data = payload.get('nova_object.data', None)
if data:
if data['disabled'] or data['forced_down']:
failed_hosts = db_api.reservable_host_get_all_by_queries(
['hypervisor_hostname == ' + data['host']])
if failed_hosts:
LOG.warn('%s failed.',
failed_hosts[0]['hypervisor_hostname'])
reservation_flags = self._handle_failures(failed_hosts)
return {}
return reservation_flags
def is_polling_enabled(self):
"""Check if the polling monitor is enabled."""

View File

@ -1675,6 +1675,69 @@ class PhysicalHostMonitorPluginTestCase(tests.TestCase):
self.patch(trusts, 'create_ctx_from_trust')
self.host_monitor_plugin = host_plugin.PhysicalHostMonitorPlugin()
def test_notification_callback_disabled_true(self):
failed_host = {'hypervisor_hostname': 'compute-1'}
event_type = 'service.update'
payload = {
'nova_object.namespace': 'nova',
'nova_object.name': 'ServiceStatusPayload',
'nova_object.version': '1.1',
'nova_object.data': {
'host': failed_host['hypervisor_hostname'],
'disabled': True,
'last_seen_up': '2012-10-29T13:42:05Z',
'binary': 'nova-compute',
'topic': 'compute',
'disabled_reason': None,
'report_count': 1,
'forced_down': False,
'version': 22,
'availability_zone': None,
'uuid': 'fa69c544-906b-4a6a-a9c6-c1f7a8078c73'
}
}
host_get_all = self.patch(db_api,
'reservable_host_get_all_by_queries')
host_get_all.return_value = [failed_host]
handle_failures = self.patch(self.host_monitor_plugin,
'_handle_failures')
handle_failures.return_value = {'rsrv-1': {'missing_resources': True}}
result = self.host_monitor_plugin.notification_callback(event_type,
payload)
self.assertEqual({'rsrv-1': {'missing_resources': True}}, result)
def test_notification_callback_no_failure(self):
event_type = 'service.update'
payload = {
'nova_object.namespace': 'nova',
'nova_object.name': 'ServiceStatusPayload',
'nova_object.version': '1.1',
'nova_object.data': {
'host': 'compute-1',
'disabled': False,
'last_seen_up': '2012-10-29T13:42:05Z',
'binary': 'nova-compute',
'topic': 'compute',
'disabled_reason': None,
'report_count': 1,
'forced_down': False,
'version': 22,
'availability_zone': None,
'uuid': 'fa69c544-906b-4a6a-a9c6-c1f7a8078c73'
}
}
host_get_all = self.patch(db_api,
'reservable_host_get_all_by_queries')
handle_failures = self.patch(self.host_monitor_plugin,
'_handle_failures')
result = self.host_monitor_plugin.notification_callback(event_type,
payload)
host_get_all.assert_not_called()
handle_failures.assert_not_called()
self.assertEqual({}, result)
def test_poll_resource_failures_state_down(self):
hosts = [
{'id': '1',