2019-10-25 06:42:40 +00:00
|
|
|
from collections import Counter
|
|
|
|
from functools import reduce
|
|
|
|
|
2019-11-18 17:39:57 +00:00
|
|
|
import bitmath
|
2019-10-25 06:42:40 +00:00
|
|
|
|
2019-12-03 10:40:41 +00:00
|
|
|
from ucloud.common.host import HostStatus
|
|
|
|
from ucloud.common.request import RequestEntry, RequestType
|
|
|
|
from ucloud.common.vm import VMStatus
|
|
|
|
from ucloud.config import vm_pool, host_pool, request_pool, env_vars
|
2019-10-25 06:42:40 +00:00
|
|
|
|
|
|
|
|
2019-11-18 17:39:57 +00:00
|
|
|
def accumulated_specs(vms_specs):
|
2019-10-25 06:42:40 +00:00
|
|
|
if not vms_specs:
|
|
|
|
return {}
|
|
|
|
return reduce((lambda x, y: Counter(x) + Counter(y)), vms_specs)
|
|
|
|
|
|
|
|
|
|
|
|
def remaining_resources(host_specs, vms_specs):
|
|
|
|
# Return remaining resources host_specs - vms
|
2019-11-18 17:39:57 +00:00
|
|
|
|
2019-10-25 06:42:40 +00:00
|
|
|
_vms_specs = Counter(vms_specs)
|
|
|
|
_remaining = Counter(host_specs)
|
|
|
|
|
|
|
|
for component in _vms_specs:
|
|
|
|
if isinstance(_vms_specs[component], str):
|
2019-11-25 06:52:36 +00:00
|
|
|
_vms_specs[component] = int(bitmath.parse_string_unsafe(_vms_specs[component]).to_MB())
|
2019-10-25 06:42:40 +00:00
|
|
|
elif isinstance(_vms_specs[component], list):
|
2019-11-25 06:52:36 +00:00
|
|
|
_vms_specs[component] = map(lambda x: int(bitmath.parse_string_unsafe(x).to_MB()), _vms_specs[component])
|
2019-10-25 06:42:40 +00:00
|
|
|
_vms_specs[component] = reduce(lambda x, y: x + y, _vms_specs[component], 0)
|
|
|
|
|
|
|
|
for component in _remaining:
|
|
|
|
if isinstance(_remaining[component], str):
|
2019-11-25 06:52:36 +00:00
|
|
|
_remaining[component] = int(bitmath.parse_string_unsafe(_remaining[component]).to_MB())
|
2019-10-25 06:42:40 +00:00
|
|
|
elif isinstance(_remaining[component], list):
|
2019-11-25 06:52:36 +00:00
|
|
|
_remaining[component] = map(lambda x: int(bitmath.parse_string_unsafe(x).to_MB()), _remaining[component])
|
2019-10-25 06:42:40 +00:00
|
|
|
_remaining[component] = reduce(lambda x, y: x + y, _remaining[component], 0)
|
|
|
|
|
|
|
|
_remaining.subtract(_vms_specs)
|
|
|
|
|
|
|
|
return _remaining
|
|
|
|
|
|
|
|
|
|
|
|
class NoSuitableHostFound(Exception):
|
|
|
|
"""Exception when no host found that can host a VM."""
|
|
|
|
|
|
|
|
|
|
|
|
def get_suitable_host(vm_specs, hosts=None):
|
|
|
|
if hosts is None:
|
|
|
|
hosts = host_pool.by_status(HostStatus.alive)
|
|
|
|
|
|
|
|
for host in hosts:
|
|
|
|
# Filter them by host_name
|
|
|
|
vms = vm_pool.by_host(host.key)
|
|
|
|
|
|
|
|
# Filter them by status
|
|
|
|
vms = vm_pool.by_status(VMStatus.running, vms)
|
|
|
|
|
|
|
|
running_vms_specs = [vm.specs for vm in vms]
|
|
|
|
|
|
|
|
# Accumulate all of their combined specs
|
2019-11-11 18:42:57 +00:00
|
|
|
running_vms_accumulated_specs = accumulated_specs(running_vms_specs)
|
2019-10-25 06:42:40 +00:00
|
|
|
|
|
|
|
# Find out remaining resources after
|
|
|
|
# host_specs - already running vm_specs
|
2019-11-11 18:42:57 +00:00
|
|
|
remaining = remaining_resources(host.specs, running_vms_accumulated_specs)
|
2019-11-18 17:39:57 +00:00
|
|
|
|
2019-10-25 06:42:40 +00:00
|
|
|
# Find out remaining - new_vm_specs
|
|
|
|
remaining = remaining_resources(remaining, vm_specs)
|
|
|
|
|
|
|
|
if all(map(lambda x: x >= 0, remaining.values())):
|
|
|
|
return host.key
|
|
|
|
|
|
|
|
raise NoSuitableHostFound
|
|
|
|
|
|
|
|
|
|
|
|
def dead_host_detection():
|
|
|
|
# Bring out your dead! - Monty Python and the Holy Grail
|
|
|
|
hosts = host_pool.by_status(HostStatus.alive)
|
|
|
|
dead_hosts_keys = []
|
|
|
|
|
|
|
|
for host in hosts:
|
|
|
|
# Only check those who claims to be alive
|
|
|
|
if host.status == HostStatus.alive:
|
|
|
|
if not host.is_alive():
|
|
|
|
dead_hosts_keys.append(host.key)
|
|
|
|
|
|
|
|
return dead_hosts_keys
|
|
|
|
|
|
|
|
|
|
|
|
def dead_host_mitigation(dead_hosts_keys):
|
|
|
|
for host_key in dead_hosts_keys:
|
|
|
|
host = host_pool.get(host_key)
|
|
|
|
host.declare_dead()
|
|
|
|
|
|
|
|
vms_hosted_on_dead_host = vm_pool.by_host(host_key)
|
|
|
|
for vm in vms_hosted_on_dead_host:
|
|
|
|
vm.declare_killed()
|
|
|
|
vm_pool.put(vm)
|
|
|
|
host_pool.put(host)
|
|
|
|
|
|
|
|
|
|
|
|
def assign_host(vm):
|
|
|
|
vm.hostname = get_suitable_host(vm.specs)
|
|
|
|
vm_pool.put(vm)
|
|
|
|
|
|
|
|
r = RequestEntry.from_scratch(type=RequestType.StartVM,
|
|
|
|
uuid=vm.uuid,
|
2019-11-18 17:39:57 +00:00
|
|
|
hostname=vm.hostname,
|
|
|
|
request_prefix=env_vars.get("REQUEST_PREFIX"))
|
2019-10-25 06:42:40 +00:00
|
|
|
request_pool.put(r)
|
|
|
|
|
|
|
|
vm.log.append("VM scheduled for starting")
|
|
|
|
return vm.hostname
|