This commit is contained in:
ahmadbilalkhalid 2019-09-05 15:16:26 +05:00
commit e09e5fd298
3 changed files with 28 additions and 29 deletions

View file

@ -19,6 +19,7 @@ host_prefix = config("HOST_PREFIX")
request_prefix = config("REQUEST_PREFIX") request_prefix = config("REQUEST_PREFIX")
etcd_client = Etcd3Wrapper(host=config("ETCD_URL")) etcd_client = Etcd3Wrapper(host=config("ETCD_URL"))
vm_pool = VmPool(etcd_client, vm_prefix) vm_pool = VmPool(etcd_client, vm_prefix)
host_pool = HostPool(etcd_client, host_prefix) host_pool = HostPool(etcd_client, host_prefix)
request_pool = RequestPool(etcd_client, request_prefix) request_pool = RequestPool(etcd_client, request_prefix)

View file

@ -28,6 +28,11 @@ def remaining_resources(host_specs, vms_specs):
return remaining return remaining
class NoSuitableHostFound(Exception):
"""Raise this exception when no host found
that can host a VM"""
def get_suitable_host(vm_specs, hosts=None): def get_suitable_host(vm_specs, hosts=None):
if hosts is None: if hosts is None:
hosts = host_pool.by_status(HostStatus.alive) hosts = host_pool.by_status(HostStatus.alive)
@ -62,7 +67,7 @@ def get_suitable_host(vm_specs, hosts=None):
): ):
return host.key return host.key
return None raise NoSuitableHostFound
def dead_host_detection(): def dead_host_detection():
@ -92,23 +97,13 @@ def dead_host_mitigation(dead_hosts_keys):
def assign_host(vm): def assign_host(vm):
host_name = get_suitable_host(vm.specs) vm.hostname = get_suitable_host(vm.specs)
if host_name: vm_pool.put(vm)
# if vm.status == VMStatus.requested_new:
# vm.status = VMStatus.scheduled_deploy
#
# if vm.status == VMStatus.killed:
# vm.status = VMStatus.requested_start
vm.hostname = host_name r = RequestEntry.from_scratch(type=RequestType.StartVM,
vm_pool.put(vm) uuid=vm.uuid,
hostname=vm.hostname)
request_pool.put(r)
r = RequestEntry.from_scratch(type=RequestType.StartVM, vm.log.append("VM scheduled for starting")
uuid=vm.uuid, return vm.hostname
hostname=vm.hostname)
request_pool.put(r)
vm.log.append("VM scheduled for starting")
return host_name
return None

23
main.py
View file

@ -10,7 +10,7 @@ from config import etcd_client as client
from config import (host_pool, request_pool, vm_pool, request_prefix) from config import (host_pool, request_pool, vm_pool, request_prefix)
from ucloud_common.request import RequestEntry, RequestType from ucloud_common.request import RequestEntry, RequestType
from helper import (get_suitable_host, dead_host_mitigation, dead_host_detection, from helper import (get_suitable_host, dead_host_mitigation, dead_host_detection,
assign_host) assign_host, NoSuitableHostFound)
pending_vms = [] pending_vms = []
@ -51,29 +51,32 @@ def main():
hostname=pending_vm_entry.hostname) hostname=pending_vm_entry.hostname)
request_pool.put(r) request_pool.put(r)
logging.debug(f"Remaining Pending: {pending_vms}")
elif request_entry.type == RequestType.ScheduleVM: elif request_entry.type == RequestType.ScheduleVM:
vm_entry = vm_pool.get(request_entry.uuid) vm_entry = vm_pool.get(request_entry.uuid)
client.client.delete(request_entry.key) # consume Request client.client.delete(request_entry.key) # consume Request
# If Request is about a VM which is labelled as "migration" # If the Request is about a VM which is labelled as "migration"
# and has a destination # and has a destination
if hasattr(request_entry, "migration") and request_entry.migration \ if hasattr(request_entry, "migration") and request_entry.migration \
and hasattr(request_entry, "destination") and request_entry.destination: and hasattr(request_entry, "destination") and request_entry.destination:
host = get_suitable_host(vm_entry.specs, [host_pool.get(request_entry.destination)]) try:
if host: get_suitable_host(vm_entry.specs, [host_pool.get(request_entry.destination)])
except NoSuitableHostFound:
logging.info("Requested destination host doesn't have enough capacity"
f"to hold {vm_entry.uuid}")
else:
r = RequestEntry.from_scratch(type=RequestType.InitVMMigration, r = RequestEntry.from_scratch(type=RequestType.InitVMMigration,
uuid=request_entry.uuid, uuid=request_entry.uuid,
destination=request_entry.destination) destination=request_entry.destination)
request_pool.put(r) request_pool.put(r)
else:
logging.info("Requested destination host doesn't have enough capacity" # If the Request is about a VM that just want to get started/created
f"to hold {vm_entry.uuid}")
else: else:
# assign_host only returns None when we couldn't be able to assign # assign_host only returns None when we couldn't be able to assign
# a host to a VM because of resource constraints # a host to a VM because of resource constraints
if assign_host(vm_entry) is None: try:
assign_host(vm_entry)
except NoSuitableHostFound:
vm_entry.log.append("Can't schedule VM. No Resource Left.") vm_entry.log.append("Can't schedule VM. No Resource Left.")
vm_pool.put(vm_entry) vm_pool.put(vm_entry)