作为个人学习笔记分享,有任何问题欢迎交流!
更新记录:
2013.8.2 增加live_migration还是block_migration底层实现的分析1. live_migrate API请求处理函数
nova/api/openstack/compute/contrib/admin_actions.py
@wsgi.action('os-migrateLive')
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"]#是否block_migration
disk_over_commit = body["os-migrateLive"]["disk_over_commit"]#是否disk_over_commit
host = body["os-migrateLive"]["host"]#目标物理机
except (TypeError, KeyError):
msg = _("host and block_migration must be specified.")
raise exc.HTTPBadRequest(explanation=msg)
#compute_api=nova/compute/api.py:API()
try:
instance = self.compute_api.get(context, id)
self.compute_api.live_migrate(context, instance, block_migration,
disk_over_commit, host)#转到2
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)
2. nova/compute/api.py:API()
@check_instance_state(vm_state=[vm_states.ACTIVE])
def live_migrate(self, context, instance, block_migration,
disk_over_commit, host_name):
"""Migrate a server lively to a new host."""
LOG.debug(_("Going to try to live migrate instance to %s"),
host_name or "another host", instance=instance)
instance = self.update(context, instance,
task_state=task_states.MIGRATING,
expected_task_state=None)
self.scheduler_rpcapi.live_migration(context, block_migration,
disk_over_commit, instance, host_name)#调用3
3. nova/scheduler/rpcapi.py:SchedulerAPI()
def live_migration(self, ctxt, block_migration, disk_over_commit,
instance, dest):
# NOTE(comstud): Call vs cast so we can get exceptions back, otherwise
# this call in the scheduler driver doesn't return anything.
instance_p = jsonutils.to_primitive(instance)
return self.call(ctxt, self.make_msg('live_migration',
block_migration=block_migration,
disk_over_commit=disk_over_commit, instance=instance_p,
dest=dest))#调用4
4. nova/scheduler/manager.py:SchedulerManager()
def live_migration(self, context, instance,dest,
block_migration,disk_over_commit):
try:
return self.driver.schedule_live_migration(
context, instance, dest,
block_migration,disk_over_commit)#调用4.1
except (exception.NoValidHost,
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, {})