mirror of
https://github.com/retspen/webvirtcloud
synced 2026-12-03 09:26:03 +00:00
A Balance button on the Computes page opens computes/balance/: the memory and vCPUs each host gives its running VMs, before and after, and the proposed migrations. The proposal (computes/balance.py) takes, step by step, the one move that most lowers the sum of the squared memory loads, then of the squared vCPU loads, and stops when none does; each VM moves once, and the VMs moved to a host fit in the memory its running and paused VMs leave without counting those moved away, so the moves may run in any order. Only running VMs count and move; one that cannot move (no destination shares its disks) still counts on its host and is listed with the reason. Unticking a VM keeps it where it is and makes the proposal again. Migrate runs the moves shown in one host.balance job. It shares the bulk migration's loop (computes.jobs.Migrations): migrate jobs, the chosen number at once, one bulk job at a time. Its migrations take only a running VM (moveto running_only): one stopped since stays where it is. A job another one started now shows only on its own page: a host's page no longer lists a bulk migration's or a balance's migrations, and the Computes page shows the bulk jobs.
41 lines
1.8 KiB
Python
41 lines
1.8 KiB
Python
"""The Balance page's proposal: which running VMs to migrate so that the
|
|
hosts give their running VMs about the same memory, then the same vCPUs."""
|
|
|
|
|
|
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.
|
|
|
|
Each step takes the one move that most lowers the sum of the squared
|
|
memory loads, then of the squared vCPU loads, and stops when none does:
|
|
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) and (best is None or change < best[0]):
|
|
best = (change, vm, target)
|
|
if best is None:
|
|
return moves, loads
|
|
_change, 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))
|