通知

与许多其他 OpenStack 服务一样,nova 通过 Notifier 类向消息总线发出通知,该类由 oslo.messaging 提供。从通知消费者角度来看,一个通知包含两部分:一个具有由 oslo.messaging 定义的固定结构的信封,以及一个由发出通知的服务定义的有效载荷。信封格式如下

{
    "priority": <string, selected from a predefined list by the sender>,
    "event_type": <string, defined by the sender>,
    "timestamp": <string, the isotime of when the notification emitted>,
    "publisher_id": <string, defined by the sender>,
    "message_id": <uuid, generated by oslo>,
    "payload": <json serialized dict, defined by the sender>
}

nova 中有两种类型的通知:旧版(unversioned)通知,其有效载荷没有版本号;以及新版(versioned)通知,其有效载荷有版本号。

旧版(无版本)通知

无版本通知存在于 nova 的早期阶段,并且大多是自然增长的。无版本通知的有效载荷结构由发出通知的代码定义,并且不存在针对该格式的文档或强制的向后兼容性契约。

Nova 代码使用 nova.rpc.get_notifier 调用来获取配置的 oslo.messaging Notifier 对象,并使用 oslo 提供的函数在 Notifier 对象上发出通知。返回的 Notifier 对象的配置取决于 get_notifier 调用的参数以及 oslo.messaging 配置选项 oslo_messaging_notifications.driveroslo_messaging_notifications.topics 的值。

版本化通知

版本化通知的概念是为了解决无版本通知的缺点而创建的。发出的通知的信封结构与无版本通知的情况相同,因为它由 oslo.messaging 提供。但是,有效载荷不是一个自由格式的字典,而是一个序列化的 oslo versionedobjects 对象

例如,service.update 通知的线路格式如下

{
    "priority": "INFO",
    "payload": {
        "nova_object.namespace": "nova",
        "nova_object.name": "ServiceStatusPayload",
        "nova_object.version": "1.0",
        "nova_object.data": {
            "host": "host1",
            "disabled": false,
            "last_seen_up": null,
            "binary": "nova-compute",
            "topic": "compute",
            "disabled_reason": null,
            "report_count": 1,
            "forced_down": false,
            "version": 2
        }
    },
    "event_type": "service.update",
    "publisher_id": "nova-compute:host1"
}

序列化的 oslo.versionedobject 作为有效载荷,为消费者提供版本号,以便消费者可以检测有效载荷的结构是否已更改。Nova 提供以下关于版本化通知有效载荷的契约

  • 有效载荷中 nova_object.version 字段定义的有效载荷版本号,仅当有效载荷中 nova_object.data 字段的语法或语义发生更改时才会增加。

  • 次要版本更新表示向后兼容的更改,这意味着仅向有效载荷添加新字段,因此编写良好的消费者无需任何更改即可继续使用新的有效载荷。

  • 主要版本更新表示有效载荷的向后不兼容的更改,这意味着有效载荷中可以删除字段、更改类型等。

  • 除了 nova_object.datanova_object.version 字段外,每个有效载荷还有一个额外的字段 nova_object.name。该字段包含有效载荷类型的 nova 内部表示的名称。客户端代码不应依赖此名称。

来自 Train summit 的 演示 介绍了版本化通知的背景和用法,并提供了一个演示。

配置

配置选项 notifications.notification_format 可用于指定 nova 发出的通知。

版本化通知被发送到与旧版通知不同的主题。默认情况下,它们被发送到 versioned_notifications,但可以使用配置选项 notifications.versioned_notifications_topics 进行配置。

nova 中存在一些特定于某些通知类型的通知配置选项,例如 notifications.notify_on_state_changenotifications.default_level 等。

可以通过将配置选项 oslo_messaging_notifications.driver 设置为 noop 来完全禁用通知。

参考

当前支持的所有版本化通知列表可以在 可用的版本化通知 中找到。