1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-12-03 09:26:03 +00:00

fix(computes): Balance moves small VMs rather than a large one for the same balance

The proposal took, step by step, the move that most lowered the sums of
squared loads, so a large VM went first. On a host with an 8 GiB VM and
two small ones it moved the large VM away and a small one back, where
moving the two small VMs evens the hosts out as well.

The proposal is now made twice: once taking the move that lowers the
sums most, once the move that lowers them most for the memory it moves.
The one that evens the hosts out more is taken, and of two that do it as
well, the one that moves less memory.
This commit is contained in:
catborise 2026-11-16 09:02:30 +03:00
parent 1259c11591
commit 54487052e5
2 changed files with 39 additions and 6 deletions

View file

@ -11,9 +11,25 @@ def propose(loads, vms, room):
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."""
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), []
@ -27,11 +43,14 @@ def propose(loads, vms, room):
# 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 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
_change, vm, target = best
_gain, vm, target = best
pk, source, ram, vcpus, _targets = vm
for i, size in enumerate((ram, vcpus)):
loads[source][i] -= size

View file

@ -54,6 +54,20 @@ class ProposeTests(SimpleTestCase):
vms = [(1, "a", 30, 0, {"b"}), (2, "b", 40, 0, {"c"})]
self.assertEqual(propose(loads, vms, {"a": 0, "b": 0, "c": 100})[0], [(2, "b", "c")])
def test_small_vms_move_rather_than_a_large_one_for_the_same_balance(self):
# a: a large VM (16) and two small ones (1, 2); b: one small VM (1). Moving the large VM to b and
# the small one back ends as even as moving the two small ones, which moves far less memory
loads = {"a": (19, 7), "b": (1, 4)}
vms = [(1, "a", 16, 2, {"b"}), (2, "a", 1, 1, {"b"}), (3, "a", 2, 4, {"b"}), (4, "b", 1, 4, {"a"})]
moves, after = propose(loads, vms, ROOM)
self.assertEqual(moves, [(2, "a", "b"), (3, "a", "b")])
self.assertEqual(after, {"a": [16, 2], "b": [4, 9]})
def test_a_large_vm_moves_when_only_it_evens_out_the_hosts(self):
loads = {"a": (20, 0), "b": (0, 0)}
vms = [(1, "a", 10, 0, {"b"}), (2, "a", 1, 0, {"b"})]
self.assertEqual(propose(loads, vms, {"a": 100, "b": 100})[0], [(1, "a", "b")])
def test_even_hosts_need_no_move(self):
loads = {"a": (4, 2), "b": (4, 2)}
self.assertEqual(propose(loads, [(1, "a", 4, 2, {"b"})], ROOM)[0], [])