# 1、冷遷移和resize前端
遷移是指將虛擬機從一個計算節點遷移到另一個節點上。冷遷移是相對熱遷移而言,區別在於冷遷移過程當中虛擬機時關機或者是處於不可用的狀態,而熱遷移則須要保證虛擬機時刻運行。 Resize則是根據需求從新調整虛擬機的計算能力和資源。Resize和冷遷移的工做流程相同,區別只是Resize時必須保持新的Flavor配置大於舊的配置,而冷遷移則要求二者相同。Resize的工做流程如圖所示:api
nova-api在收到虛擬機resize的請求後,會去調用/nova/api/openstack/compute/servers.py中的_action_resize()方法,部分代碼和註釋以下: ui
@wsgi.response(202) @extensions.expected_errors((400, 401, 403, 404, 409)) @wsgi.action('resize') @validation.schema(schema_server_resize) def _action_resize(self, req, id, body): """Resizes a given instance to the flavor size requested.""" resize_dict = body['resize'] #獲取升級flavor flavor_ref = str(resize_dict["flavorRef"])orm
resize_kwargs = {}server
if list(self.resize_extension_manager): self.resize_extension_manager.map(self._resize_extension_point, resize_dict, resize_kwargs)接口
self._resize(req, id, flavor_ref, **resize_kwargs)資源
接着調用_resize()方法,部分代碼和註釋以下:get
def _resize(self, req, instance_id, flavor_id, **kwargs): """Begin the resize process with given instance/flavor.""" context = req.environ["nova.context"] authorize(context, action='resize') #經過instance_id獲取虛擬機實例的具體信息 instance = self._get_server(context, req, instance_id)虛擬機
try: #傳遞instance和flavor_id兩個參數 self.compute_api.resize(context, instance, flavor_id, **kwargs) except exception.InstanceUnknownCell as e: raise exc.HTTPNotFound(explanation=e.format_message()) except exception.QuotaError as error: raise exc.HTTPForbidden( explanation=error.format_message()) except exception.FlavorNotFound: msg = _("Unable to locate requested flavor.") raise exc.HTTPBadRequest(explanation=msg) except exception.CannotResizeToSameFlavor: msg = _("Resize requires a flavor change.") raise exc.HTTPBadRequest(explanation=msg) except (exception.CannotResizeDisk, exception.AutoDiskConfigDisabledByImage) as e: raise exc.HTTPBadRequest(explanation=e.format_message()) except exception.InstanceIsLocked as e: raise exc.HTTPConflict(explanation=e.format_message()) except exception.InstanceInvalidState as state_error: common.raise_http_conflict_for_instance_invalid_state(state_error, 'resize', instance_id) except exception.ImageNotAuthorized: msg = _("You are not authorized to access the image " "the instance was started with.") raise exc.HTTPUnauthorized(explanation=msg) except exception.ImageNotFound: msg = _("Image that the instance was started " "with could not be found.") raise exc.HTTPBadRequest(explanation=msg) except (exception.NoValidHost, exception.AutoDiskConfigDisabledByImage) as e: raise exc.HTTPBadRequest(explanation=e.format_message()) except exception.Invalid: msg = _("Invalid instance image.") raise exc.HTTPBadRequest(explanation=msg)工作流
nova-api在收到虛擬機冷遷移的請求後,會去調用/nova/api/openstack/compute/migrate_server.py中的_migrate()方法,部分代碼和註釋以下:
@wsgi.response(202) @extensions.expected_errors((400, 403, 404, 409)) @wsgi.action('migrate') def _migrate(self, req, id, body): """Permit admins to migrate a server to a new host.""" context = req.environ['nova.context'] authorize(context, action='migrate') #得到虛擬機信息 instance = common.get_instance(self.compute_api, context, id) try: #僅傳遞instance一個參數 self.compute_api.resize(req.environ['nova.context'], instance) except (exception.TooManyInstances, exception.QuotaError) as e: raise exc.HTTPForbidden(explanation=e.format_message()) except exception.InstanceIsLocked as e: raise exc.HTTPConflict(explanation=e.format_message()) except exception.InstanceInvalidState as state_error: common.raise_http_conflict_for_instance_invalid_state(state_error, 'migrate', id) except exception.InstanceNotFound as e: raise exc.HTTPNotFound(explanation=e.format_message()) except exception.NoValidHost as e: raise exc.HTTPBadRequest(explanation=e.format_message())
從上面的分析能夠看出,冷遷移和resize底層接口一致,假如前端傳入了新的flavor,則是resize,新的flavor被傳入底層。遷移時傳入底層的flavor則爲自身實例相同的flavor。底層根據傳入的flavor參數執行相同的邏輯。resize和冷遷移的區別是在遷移的同時,是否改變虛擬機的flavor。 後面將繼續介紹二者底層相同的調用邏輯。