1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-12-02 00:46:05 +00:00

fix(operations): a host that does not answer keeps the job worker going for the others

A job's libvirt call to a host that is up but stuck waits as long as
the host does. Measured with the worker's loop: with two jobs stuck on
one host, the other host's jobs ran but their progress was never read
(both progress threads waited on the stuck host); with four stuck, a
new job of another host stayed queued, every slot taken; cancelling
them took all four control threads, so cancels, recovery and
housekeeping stopped for every host.

Now a host's jobs take at most half of the worker's slots (at least
one; a migration counts on both its hosts), and the next queued job of
another host starts instead. A host has at most one progress read and
one control task (a cancel or a recovery inspection) at a time, and
the control tasks of hosts leave one thread to housekeeping.
This commit is contained in:
catborise 2026-11-03 00:22:19 +03:00
parent b2b2386db9
commit 6ca65e291a
5 changed files with 164 additions and 20 deletions

View file

@ -21,6 +21,8 @@ def reset():
BARRIERS.clear()
CANCELLED.clear()
EXPIRED.clear()
STUCK.clear()
HOST_CALLS.clear()
def start_worker(worker):
@ -127,3 +129,46 @@ class RequestKind(NoopKind):
if op.params["fail_expire"]:
raise RuntimeError("expire failed")
EXPIRED.append(str(op.id))
STUCK = {} # compute id -> threading.Event: test.host's libvirt calls on it wait until it is set
HOST_CALLS = [] # (call, compute id) as test.host's progress and cancel begin
@register
class HostKind(NoopKind):
"""A job on compute params["host"], whose calls wait while STUCK holds it,
as calls to a host that is up but does not answer do."""
name = "test.host"
label = "Host"
cancellable = True
def normalize(self, raw):
return {"host": int(raw["host"]), "n": int(raw["n"])}
def plan(self, user, params):
key = f"instance:host-{params['host']}-{params['n']}"
return Plan(claims={f"compute:{params['host']}": Claim.SHARED, key: Claim.EXCLUSIVE}, resource=key,
label="c/vm")
def _call(self, name, op):
HOST_CALLS.append((name, op.params["host"]))
if (stuck := STUCK.get(op.params["host"])) is not None:
stuck.wait(30)
def run(self, op, ctx):
ctx.record_intent(step="effect")
self._call("run", op)
if (release := BARRIERS.get(op.submission_key)) is not None:
release.wait(30)
return {}
def progress(self, op):
self._call("progress", op)
return (1, 2)
def cancel(self, op):
self._call("cancel", op)
if (release := BARRIERS.get(op.submission_key)) is not None:
release.set()

View file

@ -20,6 +20,10 @@ class FakeWorker:
def __init__(self):
self.control = ThreadPoolExecutor(1)
def run_control(self, hosts, fn, *args):
self.control.submit(fn, *args)
return True
class RecoveryTestCase(TransactionTestCase):
def setUp(self):

View file

@ -51,12 +51,43 @@ class WorkerTestCase(TransactionTestCase):
self.thread = fakes.start_worker(self.worker)
def tearDown(self):
for barrier in fakes.BARRIERS.values():
barrier.set()
for event in [*fakes.BARRIERS.values(), *fakes.STUCK.values()]:
event.set()
if self.thread:
fakes.stop_worker(self.worker, self.thread)
connection.close()
def job(self, host, n):
return self.submit("test.host", {"host": host, "n": n}, f"key-h{host}-{n:04d}")
def test_a_stuck_host_takes_half_the_slots_and_one_progress_read(self):
fakes.STUCK[1] = threading.Event()
stuck = [self.job(1, n) for n in range(4)]
self.start(concurrency=4)
self.assertTrue(wait_for(lambda: sum(state(op) == Operation.RUNNING for op in stuck) == 2))
# the other host's job runs beside them, and its progress is read
healthy = self.submit("test.host", {"host": 2, "n": 0}, "key-h2-0000", barrier=True)
self.assertTrue(wait_for(lambda: ("progress", 2) in fakes.HOST_CALLS))
self.assertEqual(sum(state(op) == Operation.RUNNING for op in stuck), 2)
self.assertEqual(fakes.HOST_CALLS.count(("progress", 1)), 1)
fakes.BARRIERS["key-h2-0000"].set()
self.assertTrue(wait_for(lambda: state(healthy) == Operation.SUCCEEDED))
def test_a_stuck_host_takes_one_control_thread(self):
fakes.STUCK[1] = threading.Event()
# they run until cancelled
stuck = [self.submit("test.host", {"host": 1, "n": n}, f"key-h1-{n:04d}", barrier=True) for n in range(2)]
healthy = self.submit("test.host", {"host": 2, "n": 0}, "key-h2-0000", barrier=True)
self.start(concurrency=4)
self.assertTrue(wait_for(lambda: all(state(op) == Operation.RUNNING for op in [*stuck, healthy])))
for op in [*stuck, healthy]:
jobs.cancel(op.pk, self.user)
# the other host's cancel goes through; the stuck host's second cancel waits for its first
self.assertTrue(wait_for(lambda: ("cancel", 2) in fakes.HOST_CALLS))
self.assertEqual(fakes.HOST_CALLS.count(("cancel", 1)), 1)
fakes.STUCK[1].set()
self.assertTrue(wait_for(lambda: fakes.HOST_CALLS.count(("cancel", 1)) == 2))
def submit(self, kind, params, key, barrier=False):
if barrier:
fakes.BARRIERS[key] = threading.Event() # before the worker can see the job
@ -316,3 +347,4 @@ class HostIdentityTestCase(TransactionTestCase):
first = host_identity(self.dir)
with patch("operations.worker.socket.gethostname", return_value="wvc-test-b"):
self.assertEqual(host_identity(self.dir), first)