您的位置:首页 > 运维架构

OpenStack基于Libvirt的虚拟化平台调度实现----Nova虚拟机动态迁移源码分析

2016-02-29 00:00 1016 查看
这篇博文开始,我们来解析一下Nova虚拟机动态迁移的实现源码。这里不会再像前面那样对代码进行逐行的详细解析,目的是来整理Nova虚拟机动态迁移的实现步骤和相关源码部分架构。
首先我们来看虚拟机动态迁移在Opentack Nova中的API请求处理函数/nova/api/openstack/compute/contrib/admin_actions.py----def _migrate_live:

[python]
view plain
copy
print
?







def _migrate_live(self, req, id, body):

"""

Permit admins to (live) migrate a server to a new host.

允许管理员实时迁移一个服务到新的主机;

"""

context = req.environ["nova.context"]

authorize(context, 'migrateLive')

try:

block_migration = body["os-migrateLive"]["block_migration"]

disk_over_commit = body["os-migrateLive"]["disk_over_commit"]

# 确定服务迁移的目标主机;

host = body["os-migrateLive"]["host"]

except (TypeError, KeyError):

msg = _("host and block_migration must be specified.")

raise exc.HTTPBadRequest(explanation=msg)

try:

instance = self.compute_api.get(context, id)

# live_migrate:实施地迁移服务到新的主机;

self.compute_api.live_migrate(context, instance, block_migration,

disk_over_commit, host)

except (exception.ComputeServiceUnavailable,

exception.InvalidHypervisorType,

exception.UnableToMigrateToSelf,

exception.DestinationHypervisorTooOld) as ex:

raise exc.HTTPBadRequest(explanation=ex.format_message())

except Exception:

if host is None:

msg = _("Live migration of instance %(id)s to another host"

" failed") % locals()

else:

msg = _("Live migration of instance %(id)s to host %(host)s"

" failed") % locals()

LOG.exception(msg)

# Return messages from scheduler

raise exc.HTTPBadRequest(explanation=msg)

return webob.Response(status_int=202)

在这个方法中我们可以看到,实现虚拟机动态迁移主要实现的语句就是:

self.compute_api.live_migrate(context, instance, block_migration, disk_over_commit, host)
我们接着看方法live_migrate的源码:

[python]
view plain
copy
print
?







# check_instance_state:这是一个装饰器,用于在进入API方法之前,检测虚拟机和/或任务的状态;

# 如果实例处于错误的状态,将会引发异常;

@check_instance_state(vm_state=[vm_states.ACTIVE])

def live_migrate(self, context, instance, block_migration,

disk_over_commit, host_name):

"""

实时地迁移服务到新的主机;

"""

LOG.debug(_("Going to try to live migrate instance to %s"),

host_name or "another host", instance=instance)

# update:更新实例的记录,如果任务或者虚拟机状态发生改变则发送一个状态更新的通知;

# 返回更新后的实例信息;

instance = self.update(context, instance,

task_state=task_states.MIGRATING,

expected_task_state=None)

# 调用call方法实现在主题topic的模式上发送实时迁移虚拟机的消息,并等待回应;

self.scheduler_rpcapi.live_migration(context, block_migration,

disk_over_commit, instance, host_name)

这个方法中,主要实现了两部分内容,更新记录中的实例信息和在主题topic的模式上发送实时迁移虚拟机的消息。

我们先来看跟新记录中的实例信息这部分内容,主要是通过调用方法update方法实现的。具体来看方法update的实现源码:

[python]
view plain
copy
print
?







def update(self, context, instance, **kwargs):

"""

更新数据存储中的实例;

:param context: The security context

安全上下文信息;

:param instance: The instance to update

要更新的实例;

# 更新实例的记录,如果任务或者虚拟机状态发生改变则发送一个状态更新的通知;

# 返回更新后的实例信息;

"""

# 更新实例的记录,如果任务或者虚拟机状态发生改变则发送一个状态更新的通知;

# 返回旧的实例信息和更新后的实例信息;

_, updated = self._update(context, instance, **kwargs)

return updated

[python]
view plain
copy
print
?







def _update(self, context, instance, **kwargs):

# 更新实例的记录,如果任务或者虚拟机状态发生改变则发送一个状态更新的通知;

# 返回旧的实例信息和更新后的实例信息;

# 更新实例的信息;

# 返回旧的实例信息和更新后的实例信息;

old_ref, instance_ref = self.db.instance_update_and_get_original(context, instance['uuid'], kwargs)

# 发送通知,来报告实例中发生的任何改变;

notifications.send_update(context, old_ref, instance_ref, service="api")

return dict(old_ref.iteritems()), dict(instance_ref.iteritems())

方法_update主要实现了两方面的内容,一是实现更新实例信息,二是发送通知,来报告实例中发生的任何改变。

先来看方法instance_update_and_get_original的实现源码:

[python]
view plain
copy
print
?







def instance_update_and_get_original(context, instance_uuid, values):

"""

更新实例的信息;

返回旧的实例信息和更新后的实例信息;

"""

rv = IMPL.instance_update_and_get_original(context, instance_uuid, values)

try:

# 在top等级的cell上,更新实例;

cells_rpcapi.CellsAPI().instance_update_at_top(context, rv[1])

except Exception:

LOG.exception(_("Failed to notify cells of instance update"))

return rv

[python]
view plain
copy
print
?







def instance_update_and_get_original(context, instance_uuid, values):

"""

更新实例的信息;

返回旧的实例信息和更新后的实例信息;

"""

return _instance_update(context, instance_uuid, values,

copy_old_instance=True)

[python]
view plain
copy
print
?







def _instance_update(context, instance_uuid, values, copy_old_instance=False):

"""

更新实例的信息;

返回旧的实例信息和更新后的实例信息;

"""

# 获取db_session的session;

# get_session:返回一个SQLAlchemy session,若没有定义,则新建一个SQLAlchemy session;

session = get_session()

# 如果instance_uuid不是一个UUID值,则引发异常;

# is_uuid_like:判断instance_uuid是否是一个UUID值,返回结果;

if not uuidutils.is_uuid_like(instance_uuid):

raise exception.InvalidUUID(instance_uuid)

with session.begin():

# _instance_get_by_uuid:通过instance_uuid获取一个具体的实例;

instance_ref = _instance_get_by_uuid(context, instance_uuid, session=session)

if "expected_task_state" in values:

# it is not a db column so always pop out

expected = values.pop("expected_task_state")

if not isinstance(expected, (tuple, list, set)):

expected = (expected,)

actual_state = instance_ref["task_state"]

if actual_state not in expected:

raise exception.UnexpectedTaskStateError(actual=actual_state,

expected=expected)

instance_hostname = instance_ref['hostname'] or ''

if ("hostname" in values and

values["hostname"].lower() != instance_hostname.lower()):

_validate_unique_server_name(context,

session,

values['hostname'])

if copy_old_instance:

old_instance_ref = copy.copy(instance_ref)

else:

old_instance_ref = None

metadata = values.get('metadata')

if metadata is not None:

_instance_metadata_update_in_place(context, instance_ref,

'metadata',

models.InstanceMetadata,

values.pop('metadata'),

session)

system_metadata = values.get('system_metadata')

if system_metadata is not None:

_instance_metadata_update_in_place(context, instance_ref,

'system_metadata',

models.InstanceSystemMetadata,

values.pop('system_metadata'),

session)

instance_ref.update(values)

instance_ref.save(session=session)

return (old_instance_ref, instance_ref)

再来看方法_update中实现发送通知,来报告实例中发生的任何改变的实现内容,即调用了方法send_update来实现的:

[python]
view plain
copy
print
?







def send_update(context, old_instance, new_instance, service=None, host=None):

"""

发送通知,来报告实例中发生的任何改变;

"""

if not CONF.notify_on_any_change and not CONF.notify_on_state_change:

return

update_with_state_change = False

old_vm_state = old_instance["vm_state"]

new_vm_state = new_instance["vm_state"]

old_task_state = old_instance["task_state"]

new_task_state = new_instance["task_state"]

if old_vm_state != new_vm_state:

update_with_state_change = True

elif CONF.notify_on_state_change:

if (CONF.notify_on_state_change.lower() == "vm_and_task_state" and

old_task_state != new_task_state):

update_with_state_change = True

if update_with_state_change:

send_update_with_states(context, new_instance, old_vm_state,

new_vm_state, old_task_state, new_task_state, service, host)

else:

try:

_send_instance_update_notification(context, new_instance,

service=service, host=host)

except Exception:

LOG.exception(_("Failed to send state update notification"),

instance=new_instance)

我们再回到方法live_migrate中,来看第二部分主要实现的内容,即调用方法live_migration来实现在主题topic的模式上发送实时迁移虚拟机的消息。来看方法live_migration:

[python]
view plain
copy
print
?







def live_migration(self, ctxt, block_migration, disk_over_commit,

instance, dest):

# 调用call方法实现在主题topic的模式上发送实时迁移虚拟机的消息,并等待回应;

# to_primitive:转换更新后的instance到primitives格式;

instance_p = jsonutils.to_primitive(instance)

# call:在一个主题topic上发送一条消息,并等待响应;rv:多次调用(呼叫)等待者类的对象列表;返回rv[-1]列表的结尾;

return self.call(ctxt, self.make_msg('live_migration',

block_migration=block_migration,

disk_over_commit=disk_over_commit, instance=instance_p,

dest=dest))

这里调用方法call来实现在一个主题topic上发送一条消息(进行实时迁移操作),并等待响应。这里将调用/nova/scheduler/manager.py----live_migration这个方法来执行实时迁移的调度的方法,并返回实例当前运行的主机。具体来看方法的实现源码:

[python]
view plain
copy
print
?







def live_migration(self, context, instance, dest, block_migration, disk_over_commit):

try:

# schedule_live_migration:执行实时迁移的调度方法;

# 返回实例当前运行的主机;

return self.driver.schedule_live_migration(

context, instance, dest,

block_migration, disk_over_commit)

except (exception.ComputeServiceUnavailable,

exception.InvalidHypervisorType,

exception.UnableToMigrateToSelf,

exception.DestinationHypervisorTooOld,

exception.InvalidLocalStorage,

exception.InvalidSharedStorage) as ex:

request_spec = {'instance_properties': {

'uuid': instance['uuid'], },

}

with excutils.save_and_reraise_exception():

self._set_vm_state_and_notify('live_migration',

dict(vm_state=instance['vm_state'],

task_state=None,

expected_task_state=task_states.MIGRATING,),

context, ex, request_spec)

except Exception as ex:

with excutils.save_and_reraise_exception():

self._set_vm_state_and_notify('live_migration',

{'vm_state': vm_states.ERROR},

context, ex, {})

[python]
view plain
copy
print
?







def schedule_live_migration(self, context, instance, dest,

block_migration, disk_over_commit):

"""

执行实时迁移的调度方法;

返回实例当前运行的主机;

"""

# 检测确保能够进行实时迁移;

# _live_migration_src_check:为源主机的实时迁移作例行的检测;

self._live_migration_src_check(context, instance)

# 如果目标主机没有定义,则由调度算法选取一个主机;

if dest is None:

# ignore_hosts:避免作为目标主机的主机;

# 源主机不能作为目标主机;

ignore_hosts = [instance['host']]

while dest is None:

# _live_migration_dest_check:为实时迁移作目标主机的例行检测;

# 返回获取的目标主机;

dest = self._live_migration_dest_check(context, instance, dest, ignore_hosts)

try:

# _live_migration_common_check:实时迁移进行的常规检测;

self._live_migration_common_check(context, instance, dest)

# check_can_live_migrate_destination:检测是否可以进行实时迁移;

# 这个检测将会在目标主机上进行,然后返回检测结果给源主机;

migrate_data = self.compute_rpcapi.\

check_can_live_migrate_destination(context, instance,

dest,

block_migration,

disk_over_commit)

except exception.Invalid:

ignore_hosts.append(dest)

dest = None

continue

else:

# _live_migration_dest_check:为实时迁移作目标主机的例行检测;

# 返回获取的目标主机;

self._live_migration_dest_check(context, instance, dest)

# _live_migration_common_check:实时迁移进行的常规检测;

self._live_migration_common_check(context, instance, dest)

# check_can_live_migrate_destination:检测是否可以进行实时迁移;

# 这个检测将会在目标主机上进行,然后返回检测结果给源主机;

migrate_data = self.compute_rpcapi.\

check_can_live_migrate_destination(context, instance, dest,

block_migration,

disk_over_commit)

# 执行迁移;

src = instance['host']

self.compute_rpcapi.live_migration(context, host=src,

instance=instance, dest=dest,

block_migration=block_migration,

migrate_data=migrate_data)

可以看见,在方法schedule_live_migration中,主要进行了三部分的内容,第一,如果目前主机不存在,则由调度算法选取一个目标主机,并且进行相关的检测,确保能够进行实时迁移操作;第二,如果目标主机存在,则直接进行相关的检测操作,确保能够进行实时迁移操作;第三,执行迁移操作。
在前两部分的内容中,分别调用了三个方法_live_migration_dest_check、_live_migration_common_check和

check_can_live_migrate_destination。我们分别来看这三个方法:
首先来看方法_live_migration_dest_check,具体来看它的源码:

[python]
view plain
copy
print
?







def _live_migration_dest_check(self, context, instance_ref, dest, ignore_hosts=None):

"""

为实时迁移作目标主机的例行检测;

:param dest: destination host

目标主机;

:param ignore_hosts: hosts that should be avoided as dest host

应该避免作为目标主机的主机;

"""

# 如果没有指定目标主机,则选取一个;

if dest is None:

# instance_type_get:通过id值获取实例类型;

instance_type = db.instance_type_get(context, instance_ref['instance_type_id'])

# 以字典的形式返回给定image镜像instance_ref['image_ref']的镜像数据;

# 调用glance客户端,下载镜像数据(此时为JSON格式);

# 转换从glance下载的镜像数据为python可处理的字典格式;

image = self.image_service.show(context, instance_ref['image_ref'])

request_spec = {'instance_properties': instance_ref,

'instance_type': instance_type,

'instance_uuids': [instance_ref['uuid']],

'image': image}

# 过滤器;

filter_properties = {'ignore_hosts': ignore_hosts}

# 调用合适的scheduler算法来获取合适的主机,作为实时迁移的目标主机;

return self.select_hosts(context, request_spec, filter_properties)[0]

# 检测主机上的实例是否在运行,并且目标主机和源主机是不同的;

src = instance_ref['host']

if dest == src:

raise exception.UnableToMigrateToSelf(

instance_id=instance_ref['uuid'], host=dest)

# 检确保目标主机是存在的;

try:

# service_get_by_compute_host:为给定的计算主机获取服务入口(条目);

dservice_ref = db.service_get_by_compute_host(context, dest)

except exception.NotFound:

raise exception.ComputeServiceUnavailable(host=dest)

# 确保目标主机是活跃的;

if not self.servicegroup_api.service_is_up(dservice_ref):

raise exception.ComputeServiceUnavailable(host=dest)

# 检测内存需求;

# 确保目标主机具有足够的内存来进行实时迁移;

self._assert_compute_node_has_enough_memory(context, instance_ref, dest)

return dest

这个方法中,将会判断是否定义了目标主机dest,如果没有定义目标主机,将会调用合适的scheduler算法来获取合适的主机,作为实时迁移的目标主机。然后会针对目标主机进行一系列的检查操作。

再来看方法_live_migration_common_check,看看它的源码实现:

[python]
view plain
copy
print
?







def _live_migration_common_check(self, context, instance_ref, dest):

"""

实时迁移进行的常规检测;

"""

# 获取目标主机信息;

dservice_ref = self._get_compute_info(context, dest)

src = instance_ref['host']

# 获取源主机信息;

oservice_ref = self._get_compute_info(context, src)

# 确保目标主机和源主机的虚拟机管理程序是相同的;

orig_hypervisor = oservice_ref['hypervisor_type']

dest_hypervisor = dservice_ref['hypervisor_type']

if orig_hypervisor != dest_hypervisor:

raise exception.InvalidHypervisorType()

# 确保目标主机的虚拟机管理程序版本大于源主机的虚拟机管理程序版本;

orig_hypervisor = oservice_ref['hypervisor_version']

dest_hypervisor = dservice_ref['hypervisor_version']

if orig_hypervisor > dest_hypervisor:

raise exception.DestinationHypervisorTooOld()

在这个方法中,主要实现了对源主机和目标主机上的虚拟机管理程序的版本进行检查。

最后来看方法check_can_live_migrate_destination,这个方法实现的是在目标主机上检测是否可以进行实时迁移,并将检测结果返回给源主机。来看看它的源码实现:

[python]
view plain
copy
print
?







def check_can_live_migrate_destination(self, ctxt, instance, destination,

block_migration, disk_over_commit):

instance_p = jsonutils.to_primitive(instance)

return self.call(ctxt,

self.make_msg('check_can_live_migrate_destination',

instance=instance_p,

block_migration=block_migration,

disk_over_commit=disk_over_commit),

topic=_compute_topic(self.topic, ctxt, destination, None))

这里调用call方法实现在一个主题topic上发送一条远程消息,实现在目标主机上进行检测是否可以进行实时迁移,并等待响应。

接下来将会执行/nova/compute/manager.py中的方法check_can_live_migrate_destination,这个方法实现了在目标主机上进行检测是否可以进行实时迁移。具体来看方法的实现代码:

[python]
view plain
copy
print
?







@exception .wrap_exception(notifier=notifier, publisher_id=publisher_id())

def check_can_live_migrate_destination(self, ctxt, instance,

block_migration=False,

disk_over_commit=False):

"""

检测是否可以进行实时迁移;

这个检测将会在目标主机上进行,然后返回检测结果给源主机;

"""

# 获取源主机信息;

src_compute_info = self._get_compute_info(ctxt, instance['host'])

# 获取目标主机信息;

dst_compute_info = self._get_compute_info(ctxt, CONF.host)

# check_can_live_migrate_destination:检测是否可以执行实时迁移;

# 这个检测是运行在目标主机上的,然后返回检测结果给源主机;

dest_check_data = self.driver.check_can_live_migrate_destination(ctxt,

instance, src_compute_info, dst_compute_info,

block_migration, disk_over_commit)

migrate_data = {}

try:

# check_can_live_migrate_source:检测是否可以执行实时迁移;

migrate_data = self.compute_rpcapi.check_can_live_migrate_source(ctxt, instance, dest_check_data)

finally:

# check_can_live_migrate_destination_cleanup:目标主机所要做的必要的清理工作;

self.driver.check_can_live_migrate_destination_cleanup(ctxt, dest_check_data)

if dest_check_data and 'migrate_data' in dest_check_data:

migrate_data.update(dest_check_data['migrate_data'])

return migrate_data

这个方法中继而调用了以下方法,这里不再做一一解释:

[python]
view plain
copy
print
?







def check_can_live_migrate_destination(self, ctxt, instance_ref,

src_compute_info, dst_compute_info,

block_migration=False,

disk_over_commit=False):

"""

检测是否可以执行实时迁移;

这个检测是运行在目标主机上的,然后返回检测结果给源主机;

"""

disk_available_mb = None

# 如果是块迁移;

if block_migration:

disk_available_gb = dst_compute_info['disk_available_least']

# disk_available_mb:获取磁盘上可用空间(总的可用空间减去为主机预留的空间);

# disk_available_gb * 1024:获取磁盘上可用空间的总量(MB);

# reserved_host_disk_mb:这个参数定义了在磁盘上为host主机预留的空间总量(MB);

# 参数的默认值为0;

disk_available_mb = (disk_available_gb * 1024) - CONF.reserved_host_disk_mb

# 检测CPU相关信息;

src = instance_ref['host']

source_cpu_info = src_compute_info['cpu_info']

# 检测主机中的CPU和xml文件中给定的CPU是否是兼容的;

self._compare_cpu(source_cpu_info)

# _create_shared_storage_test_file:在CONF.instances_path定义的实例在磁盘disk上的存储路径下创建临时文件;

filename = self._create_shared_storage_test_file()

return {"filename": filename,

"block_migration": block_migration,

"disk_over_commit": disk_over_commit,

"disk_available_mb": disk_available_mb}

[python]
view plain
copy
print
?







def check_can_live_migrate_source(self, ctxt, instance, dest_check_data):

instance_p = jsonutils.to_primitive(instance)

return self.call(ctxt, self.make_msg('check_can_live_migrate_source',

instance=instance_p,

dest_check_data=dest_check_data),

topic=_compute_topic(self.topic, ctxt, None,

instance))

[python]
view plain
copy
print
?







@exception .wrap_exception(notifier=notifier, publisher_id=publisher_id())

def check_can_live_migrate_source(self, ctxt, instance, dest_check_data):

"""

检测是否可以执行实时迁移;

"""

capi = self.conductor_api

bdms = capi.block_device_mapping_get_all_by_instance(ctxt, instance)

is_volume_backed = self.compute_api.is_volume_backed_instance(ctxt,

instance,

bdms)

dest_check_data['is_volume_backed'] = is_volume_backed

# check_can_live_migrate_source:检测是否可以执行实时迁移;

return self.driver.check_can_live_migrate_source(ctxt, instance, dest_check_data)

[python]
view plain
copy
print
?







def check_can_live_migrate_source(self, ctxt, instance_ref,

dest_check_data):

"""

检测是否可以执行实时迁移;

"""

source = CONF.host

filename = dest_check_data["filename"]

block_migration = dest_check_data["block_migration"]

is_volume_backed = dest_check_data.get('is_volume_backed', False)

shared = self._check_shared_storage_test_file(filename)

if block_migration:

if shared:

reason = _("Block migration can not be used "

"with shared storage.")

raise exception.InvalidLocalStorage(reason=reason, path=source)

# 检测目标主机是否有足够的磁盘空间来进行实时迁移;

self._assert_dest_node_has_enough_disk(ctxt, instance_ref,

dest_check_data['disk_available_mb'],

dest_check_data['disk_over_commit'])

elif not shared and not is_volume_backed:

reason = _("Live migration can not be used "

"without shared storage.")

raise exception.InvalidSharedStorage(reason=reason, path=source)

dest_check_data.update({"is_shared_storage": shared})

return dest_check_data

我们再来看方法schedule_live_migration中的第三部分内容,即执行迁移操作。

先来回顾其中实现这部分的源码:

[python]
view plain
copy
print
?







# 执行迁移;

src = instance['host']

self.compute_rpcapi.live_migration(context, host=src,

instance=instance, dest=dest,

block_migration=block_migration,

migrate_data=migrate_data)

[python]
view plain
copy
print
?







def live_migration(self, ctxt, instance, dest, block_migration, host,

migrate_data=None):

instance_p = jsonutils.to_primitive(instance)

self.cast(ctxt, self.make_msg('live_migration', instance=instance_p,

dest=dest, block_migration=block_migration,

migrate_data=migrate_data),

topic=_compute_topic(self.topic, ctxt, host, None))

[python]
view plain
copy
print
?







def live_migration(self, context, instance, dest, block_migration, disk_over_commit):

try:

# schedule_live_migration:执行实时迁移的调度方法;

# 返回实例当前运行的主机;

return self.driver.schedule_live_migration(

context, instance, dest,

block_migration, disk_over_commit)

except (exception.ComputeServiceUnavailable,

exception.InvalidHypervisorType,

exception.UnableToMigrateToSelf,

exception.DestinationHypervisorTooOld,

exception.InvalidLocalStorage,

exception.InvalidSharedStorage) as ex:

request_spec = {'instance_properties': {

'uuid': instance['uuid'], },

}

with excutils.save_and_reraise_exception():

self._set_vm_state_and_notify('live_migration',

dict(vm_state=instance['vm_state'],

task_state=None,

expected_task_state=task_states.MIGRATING,),

context, ex, request_spec)

except Exception as ex:

with excutils.save_and_reraise_exception():

self._set_vm_state_and_notify('live_migration',

{'vm_state': vm_states.ERROR},

context, ex, {})

[python]
view plain
copy
print
?







def live_migration(self, ctxt, instance_ref, dest,

post_method, recover_method, block_migration=False,

migrate_data=None):

"""

建立一个绿色线程来运行方法_live_migration,来执行实时迁移;

主要是调用libvirt python接口方法virDomainMigrateToURI,来实现从当前主机迁移domain对象到给定的目标主机;

"""

# spawn:建立一个绿色线程来运行方法“func(*args, **kwargs)”,这里就是来运行方法_live_migration;

# _live_migration:执行实时迁移;

# 主要是调用libvirt python接口方法virDomainMigrateToURI,来实现从当前主机迁移domain对象到给定的目标主机;

greenthread.spawn(self._live_migration, ctxt, instance_ref, dest,

post_method, recover_method, block_migration,

migrate_data)

[python]
view plain
copy
print
?







def _live_migration(self, ctxt, instance_ref, dest, post_method,

recover_method, block_migration=False,

migrate_data=None):

"""

执行实时迁移;

主要是调用libvirt python接口方法virDomainMigrateToURI,来实现从当前主机迁移domain对象到给定的目标主机;

:params ctxt: security context

安全上下文信息;

:params instance_ref:

nova.db.sqlalchemy.models.Instance object

instance object that is migrated.

被迁移的实例对象;

:params dest: destination host

目标主机;

:params post_method:

post operation method.

expected nova.compute.manager.post_live_migration.

post操作方法;

:params recover_method:

recovery method when any exception occurs.

expected nova.compute.manager.recover_live_migration.

发生任何异常时候的恢复方法;

:params migrate_data: implementation specific params

实时迁移的具体参数;

"""

# 执行迁移操作;

try:

if block_migration:

# 获取块迁移标志列表;

# block_migration_flag:这个参数定义了为块迁移设置迁移标志;

# 参数的默认值为'VIR_MIGRATE_UNDEFINE_SOURCE, VIR_MIGRATE_PEER2PEER, VIR_MIGRATE_NON_SHARED_INC';

flaglist = CONF.block_migration_flag.split(',')

else:

# 获取实时迁移标志列表;

# 这个参数定义了实时迁移的迁移标志;

# 参数的默认值为'VIR_MIGRATE_UNDEFINE_SOURCE, VIR_MIGRATE_PEER2PEER';

flaglist = CONF.live_migration_flag.split(',')

flagvals = [getattr(libvirt, x.strip()) for x in flaglist]

logical_sum = reduce(lambda x, y: x | y, flagvals)

# 根据给定的实例名称检索libvirt域对象;

dom = self._lookup_by_name(instance_ref["name"])

# migrateToURI:调用libvirt python接口方法virDomainMigrateToURI,来实现从当前主机迁移domain对象到给定的目标主机;

# live_migration_uri:这个参数定义了迁移目标URI;

# 参数的默认值为"qemu+tcp://%s/system";

# 这里面的"%s"用迁移目标主机名来代替;

# live_migration_bandwidth:这个参数定义了迁移过程中所使用的最大的带宽;

# 参数的默认值为0;

dom.migrateToURI(CONF.live_migration_uri % dest,

logical_sum,

None,

CONF.live_migration_bandwidth)

except Exception as e:

with excutils.save_and_reraise_exception():

LOG.error(_("Live Migration failure: %(e)s") % locals(),

instance=instance_ref)

recover_method(ctxt, instance_ref, dest, block_migration)

# Waiting for completion of live_migration.

# 获取等待完成实时迁移的时间;

timer = utils.FixedIntervalLoopingCall(f=None)

def wait_for_live_migration():

"""

waiting for live migration completion.

检测虚拟机迁移操作的实施状态;

"""

try:

self.get_info(instance_ref)['state']

except exception.NotFound:

timer.stop()

post_method(ctxt, instance_ref, dest, block_migration,

migrate_data)

# 这两条语句所实现的功能是以一定的时间间隔(0.5)循环调用wait_for_live_migration方法,来检测虚拟机迁移的状态,知道虚拟机成功迁移为止;

timer.f = wait_for_live_migration

timer.start(interval=0.5).wait()

至此,Nova虚拟机动态迁移的实现机制和实现源码解析完成。

博文中不免有不正确的地方,欢迎朋友们不吝批评指正,谢谢大家了!


博客原文地址:http://blog.csdn.net/gaoxingnengjisuan
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: