mirror of
https://github.com/retspen/webvirtcloud
synced 2026-12-03 17:36:05 +00:00
Bulk Migrate sent every chosen VM to one destination, without looking at
its memory. With several other hosts it now spreads them by default
(computes.balance.spread): the largest VMs first, each to the host that
has the least memory, then vCPUs, with it; a VM that holds its memory
(running or paused) only where it fits the memory the host's running and
paused VMs leave. The page shows where
each VM goes and makes the spread again when a VM is ticked or unticked;
a VM no host has room for says so. One destination stays a choice. The
host.bulk_migrate job takes a destination per VM ("moves").
The Balance and Bulk Migrate pages asked, for every VM and destination,
the host's identity and whether both hosts share each disk directory,
which writes a marker file on the shared storage: about 86 ms a pair,
sequential, so minutes with a few hundred VMs on ten hosts. A Reach
(computes/reach.py) asks each host pair and each directory once and
reads a VM's definitions once. vrtManager: wvmInstances.shares and
file_disks, also used by disk_plan.
read_fleet reads the hosts, their VMs and loads for both pages.
88 lines
3.9 KiB
Python
88 lines
3.9 KiB
Python
"""Where VMs migrate: the Balance page's proposal (propose), which running
|
|
VMs to migrate so that the hosts give their running VMs about the same
|
|
memory, then the same vCPUs; and a bulk migration's spread over the other
|
|
hosts (spread)."""
|
|
|
|
|
|
def propose(loads, vms, room):
|
|
"""The moves [(VM, source, target)] and the loads after them.
|
|
|
|
loads: {host: (memory KiB, vCPUs)} that each host gives its running VMs,
|
|
all of them, also those that cannot move. vms: the VMs that may move,
|
|
(VM, host, memory KiB, vCPUs, the hosts it may go to). room: {host: its
|
|
memory KiB its running and paused VMs leave}; the VMs moved to a host
|
|
fit in it without those moved away, so the moves may run in any order.
|
|
|
|
The proposal is made twice, step by step, each step taking the move
|
|
that most lowers the sum of the squared memory loads, then of the
|
|
squared vCPU loads: once the move that lowers it most, once the move
|
|
that lowers it most for the memory it moves. The one that evens out the
|
|
hosts more is taken, and of two that do it as well, the one that moves
|
|
less memory: small VMs rather than a large one."""
|
|
sizes = {vm[0]: vm[2] for vm in vms}
|
|
|
|
def rank(plan):
|
|
moves, after = plan
|
|
squares = tuple(sum(load[i] ** 2 for load in after.values()) for i in (0, 1))
|
|
return squares, sum(sizes[pk] for pk, _source, _target in moves)
|
|
|
|
return min((_steps(loads, vms, room, per_memory) for per_memory in (False, True)), key=rank)
|
|
|
|
|
|
def _steps(loads, vms, room, per_memory):
|
|
"""The moves and the loads after them, a move at a time while one lowers
|
|
the sums of squares; each VM moves at most once."""
|
|
loads = {host: list(load) for host, load in loads.items()}
|
|
room = dict(room)
|
|
left, moves = sorted(vms), []
|
|
while True:
|
|
best = None
|
|
for vm in left:
|
|
pk, source, ram, vcpus, targets = vm
|
|
for target in sorted(targets):
|
|
if ram > room[target]:
|
|
continue
|
|
# the change of each sum of squares: (s - x)² + (t + x)² - s² - t²
|
|
change = tuple(2 * size * (loads[target][i] - loads[source][i] + size)
|
|
for i, size in enumerate((ram, vcpus)))
|
|
if change >= (0, 0):
|
|
continue
|
|
gain = (change[0] / ram, change[1]) if per_memory and ram else change
|
|
if best is None or gain < best[0]:
|
|
best = (gain, vm, target)
|
|
if best is None:
|
|
return moves, loads
|
|
_gain, vm, target = best
|
|
pk, source, ram, vcpus, _targets = vm
|
|
for i, size in enumerate((ram, vcpus)):
|
|
loads[source][i] -= size
|
|
loads[target][i] += size
|
|
room[target] -= ram
|
|
left.remove(vm)
|
|
moves.append((pk, source, target))
|
|
|
|
|
|
def spread(loads, vms, room):
|
|
"""Where each VM of a host being emptied goes: {VM: target}, and the
|
|
loads after. loads and room as propose's, of the hosts the VMs may go
|
|
to. vms: (VM, memory KiB, vCPUs, whether it holds its memory (not shut
|
|
off), the hosts it may go to).
|
|
|
|
The largest VMs first, each to the host it may go to that has the least
|
|
memory, then vCPUs, with it; one that holds its memory only where it
|
|
fits the room. A shut-off VM counts too: it takes its memory when it
|
|
starts. A VM no host takes is left out."""
|
|
loads = {host: list(load) for host, load in loads.items()}
|
|
room = dict(room)
|
|
placed = {}
|
|
for pk, ram, vcpus, holds, targets in sorted(vms, key=lambda vm: (-vm[1], -vm[2], vm[0])):
|
|
fits = sorted(target for target in targets if not holds or ram <= room[target])
|
|
if not fits:
|
|
continue
|
|
target = min(fits, key=lambda host: (loads[host][0] + ram, loads[host][1] + vcpus))
|
|
loads[target][0] += ram
|
|
loads[target][1] += vcpus
|
|
if holds:
|
|
room[target] -= ram
|
|
placed[pk] = target
|
|
return placed, loads
|