nova中利用libvirt 事件来更新vm的DB中的power状态
https://wiki.openstack.org/wiki/ComputeDriverEvents
Nova compute manager每10分钟报告hypervisor上VM的状态,可以让nova检测到vm的真实状态,比如vm被guest OS shutdown或者被Hyper kill掉了。
_sync_power_states,虚拟机状态刷新,spacing=600
获取db中本机上的虚拟机,(会调用driver的get_num_instances方法获取虚拟机个数),根据db中的虚拟机做循环:
a) 忽略有task_state(正在处理)的虚拟机
b) 调用driver的get_info方法获取节点虚拟机信息,若找不到,power_state=NOSTATE
c) 若db中的power_state与driver返回的power_state不一致,以driver为准,更新db
d) 若db中的vm_state=BUILDING|RESCUED|RESIZED|SUSPENDED|PAUSED|ERROR,忽略
e) 若db中的vm_state==ACTIVE,而power_state是[SHUTDOWN, CRASHED, SUSPENDED],调用nova-api的stop接口停止虚拟机;如果power_state是[PAUSED|NOSTATE],忽略
f) 若db中的vm_state==STOPPED,而power_state不是[NOSTATE, SHUTDOWN, CRASHED],调用nova-api的stop接口停止虚拟机
但这会加重hypervisor,而且有延时。
Libvirt有domain events" capability可以及时的检测vm的状态。
RFC讨论:
http://lists.openstack.org/pipermail/openstack-dev/2013-January/004501.html
libvirt中的实现:
- libvirt.virEventRegisterDefaultImpl() 会注册libvirt的默认事件loop implementation,这里使用默认实现
- libvirt.virEventRunDefaultImpl()会对loop做一次迭代,需要放在一个 "while True"的循环中来处理libvirt的事件loop中的事件。
- conn.domainEventRegisterAny()在libvirt connection上注册event callbacks
在nova中实现为:
https://blueprints.launchpad.net/nova/+spec/compute-driver-events
- def queue_event(queue) – 把libvirt中的事件放到queue中以便随后dispatch到compute manager.它运行在一个native thread.
- def emit_event(event) - this will dispatch a single event to the compute manager callback. This is only to be invoked from a greenthread.
- def emit_queued_events() - this will dispatch all events previously queued via the queue_event() method. This is only to be invoked from a greenthread.
- def register_event_listener(callback) - register a callback function to receive events. The callback will be invoked with a single parameter - the event object
The actual data associated with events will be provided via a number of classes
- Event - the (abstract) base class for all events. Simply maintains a timestamp indicating when the event was raised
- InstanceEvent - the (abstract) base class for all events associated with an individual instance. Maintains an instance UUID.
- LifecycleEvent - the class used for reporting changes in an instance state (started/stopped/paused/resumed)
实现细节:
https://review.openstack.org/#/c/21802/
Handle lifecycle events in the compute manager
时间处理中,如果当前收到事件状态如DB中一样,说明DB中的状态没有及时更新,
Self._sync_instance_power_state来更新,(为什么不直接更新DB?)
https://review.openstack.org/#/q/topic:bp/compute-driver-events,n,z
取得libvirt event放到一个pipe中,在compute manager中处理pipe中的事件。
https://review.openstack.org/#/c/21801/5/nova/virt/libvirt/driver.py