From 027896c3d4506269ee2f3f375339073f5f8e5736 Mon Sep 17 00:00:00 2001 From: catborise Date: Sun, 15 Nov 2026 20:15:54 +0300 Subject: [PATCH] fix(computes): a failed bulk migration does not also say it was stopped The worker hands a failed job that made changes to recovery, whose outcome joins the job's error. The bulk migration's recovery said "the bulk migration stopped ... the other VMs stay" after its own "These VMs did not migrate", as if the worker had left it. A job that ends itself now records so, and recovery then adds nothing; it still says so for a job the worker left. --- computes/jobs.py | 16 +++++++++++----- computes/tests/test_bulk_migrate.py | 8 ++++++++ 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/computes/jobs.py b/computes/jobs.py index 5ff29cd7..bc876db6 100644 --- a/computes/jobs.py +++ b/computes/jobs.py @@ -69,6 +69,12 @@ class BulkMigrate(Kind): started, refused = {}, [] # VM: its migrate job; the VMs whose job was refused, and why ctx.record_intent() ctx.set_phase(gettext_noop("Migrating the VMs")) + + def stop(text, **values): + """End the job failed; recovery, which the worker hands such a job, adds nothing.""" + ctx.record_intent(stopped=True) + raise util.OperationError(text, **values) + while True: running = list(Operation.objects.filter(pk__in=started.values(), state__in=Operation.ACTIVE) .values_list("pk", flat=True)) @@ -81,11 +87,9 @@ class BulkMigrate(Kind): # the worker's slots may have been lowered: this job waits in one, its migrations need others; # once it ends, the migrations it queued run if parallel_limit() < 1: - raise util.OperationError(gettext_noop( - "The worker now runs one job at a time: the VMs not yet started stay on the host")) + stop(gettext_noop("The worker now runs one job at a time: the VMs not yet started stay on the host")) if waiting and _where(p) != op.intent["where"]: - raise util.OperationError(gettext_noop( - "A host was changed or deleted: the VMs not yet started stay on the host")) + stop(gettext_noop("A host was changed or deleted: the VMs not yet started stay on the host")) for pk in list(waiting): if len(running) >= min(p["parallel"], parallel_limit()): break @@ -114,7 +118,7 @@ class BulkMigrate(Kind): .values_list("pk", flat=True)) failed = refused + [names[vm] for vm, job in started.items() if job not in migrated] if failed: - raise util.OperationError(gettext_noop("These VMs did not migrate: %(vms)s"), vms=", ".join(failed)) + stop(gettext_noop("These VMs did not migrate: %(vms)s"), vms=", ".join(failed)) return {} def progress(self, op): @@ -125,6 +129,8 @@ class BulkMigrate(Kind): """run sees the cancel and cancels the migrations that run.""" def reconcile(self, op): + if op.intent.get("stopped"): + return Outcome(Operation.FAILED) # its own error says why return Outcome(Operation.FAILED, error=utils.say(gettext_noop( "The bulk migration stopped: the migrations it started go on as their own jobs; the other VMs stay"))) diff --git a/computes/tests/test_bulk_migrate.py b/computes/tests/test_bulk_migrate.py index 0b6effa2..aae1a1a0 100644 --- a/computes/tests/test_bulk_migrate.py +++ b/computes/tests/test_bulk_migrate.py @@ -138,6 +138,7 @@ class BulkMigrateJobTests(TestCase): job.state = ends(job) job.save() + self.op = op with patch("computes.jobs.time.sleep", side_effect=round_ends): try: return BulkMigrate().run(op, Ctx(op)), max(most) @@ -154,6 +155,13 @@ class BulkMigrateJobTests(TestCase): with self.assertRaisesRegex(util.OperationError, "did not migrate"): self.run_job(1, lambda job: Operation.FAILED if job.params["instance"] == fail else Operation.SUCCEEDED) self.assertEqual(self.migrations.count(), 3) + # the worker hands a failed job that made changes to recovery: it adds no "stopped" message + outcome = BulkMigrate().reconcile(self.op) + self.assertEqual((outcome.state, outcome.error), (Operation.FAILED, [])) + + def test_a_bulk_migration_the_worker_left_says_it_stopped(self): + op = Operation(kind="host.bulk_migrate", intent={"migrations": {}}) + self.assertIn("stopped", BulkMigrate().reconcile(op).error[0]["text"]) def test_a_create_waits_for_the_migrations(self): Operation.objects.all().delete()