From 25e3794cc965a2f4e3e99138238d883fea5207fc Mon Sep 17 00:00:00 2001 From: catborise Date: Sun, 15 Nov 2026 20:00:41 +0300 Subject: [PATCH] fix(computes): a bulk migration shows as one job on the host's page The host's VM list showed every migration of a bulk migration as a job of its own (each holds the host), and kept a card for each when it ended: 20 VMs, 20 cards down the page. The migrations a bulk migration starts now name it as their parent, and a page leaves out a job whose parent is that page's own: the host page shows the bulk migration, with how many of its VMs are done as its progress; each migration still shows on its VM's page and the Jobs page. A page also shows the jobs it is the page of, not only those holding it: the bulk migration holds no claim on the host. --- computes/jobs.py | 6 +++++- computes/tests/test_bulk_migrate.py | 14 ++++++++++++++ operations/api/viewsets.py | 5 ++++- operations/jobs.py | 4 ++-- operations/migrations/0004_parent.py | 19 +++++++++++++++++++ operations/models.py | 2 ++ 6 files changed, 46 insertions(+), 4 deletions(-) create mode 100644 operations/migrations/0004_parent.py diff --git a/computes/jobs.py b/computes/jobs.py index ca32f868..5ff29cd7 100644 --- a/computes/jobs.py +++ b/computes/jobs.py @@ -94,7 +94,7 @@ class BulkMigrate(Kind): continue # it left the host meanwhile raw = {"instance": pk, "source": p["compute"], "target": p["target"], "live": True, "autoconverge": True} try: - child = jobs.submit("instance.migrate", op.user, raw, f"evac-{op.pk.hex}-{pk}") + child = jobs.submit("instance.migrate", op.user, raw, f"bulk-{op.pk.hex}-{pk}", parent=op) except claims.Busy as busy: if busy.blocker is not None and busy.blocker.state == Operation.UNCERTAIN: # it holds its claims until someone resolves it @@ -117,6 +117,10 @@ class BulkMigrate(Kind): raise util.OperationError(gettext_noop("These VMs did not migrate: %(vms)s"), vms=", ".join(failed)) return {} + def progress(self, op): + """(the VMs whose migration ended, the chosen VMs).""" + return op.children.filter(state__in=Operation.TERMINAL).count(), len(op.params["instances"]) + def cancel(self, op): """run sees the cancel and cancels the migrations that run.""" diff --git a/computes/tests/test_bulk_migrate.py b/computes/tests/test_bulk_migrate.py index 0794b0eb..0b6effa2 100644 --- a/computes/tests/test_bulk_migrate.py +++ b/computes/tests/test_bulk_migrate.py @@ -222,3 +222,17 @@ class BulkMigrateJobTests(TestCase): raw = {"instance": self.vms[0].pk, "source": third.pk, "target": self.other.pk} with self.assertRaisesRegex(util.OperationError, "moved to another compute"): jobs.submit("instance.migrate", self.admin, raw, "key-mig00003") + + def test_the_host_page_shows_the_bulk_migration_and_not_its_migrations(self): + params = {"compute": self.compute.pk, "target": self.other.pk, "parallel": 2, "instances": [self.vms[0].pk]} + bulk = jobs.submit("host.bulk_migrate", self.admin, params, "key-bulk0001") + child = jobs.submit("instance.migrate", self.admin, {"instance": self.vms[0].pk, "target": self.other.pk}, + "key-mig00004", parent=bulk) + self.client.force_login(self.admin) + + def shown(key): + return [job["id"] for job in self.client.get(reverse("operation-resource"), {"key": key}).json()["jobs"]] + + self.assertEqual(shown(claims.compute_key(self.compute.pk)), [str(bulk.pk)]) + self.assertEqual(shown(claims.instance_key(self.vms[0].uuid)), [str(child.pk)]) + self.assertEqual(BulkMigrate().progress(bulk), (0, 1)) diff --git a/operations/api/viewsets.py b/operations/api/viewsets.py index 42cfbb52..ccc3d754 100644 --- a/operations/api/viewsets.py +++ b/operations/api/viewsets.py @@ -1,3 +1,4 @@ +from django.db.models import Q from django.http import Http404 from django.utils import timezone from drf_spectacular.utils import OpenApiParameter, extend_schema @@ -49,7 +50,9 @@ class OperationViewSet(mixins.ListModelMixin, mixins.RetrieveModelMixin, viewset raise ValidationError({"key": "required"}) if not visible(request.user, key): raise Http404 - active = list(Operation.objects.filter(claims__resource=key, state__in=SHOWN_STATES).distinct()) + # the jobs on the resource and the page's own; a job another one on this page started shows in that one + active = list(Operation.objects.filter(Q(claims__resource=key) | Q(resource=key), state__in=SHOWN_STATES) + .exclude(parent__resource=key).distinct()) mine = [op for op in active if may_see(op, request.user)] others = [summary(op, request.user) for op in active if not may_see(op, request.user)] seen = last_seen() diff --git a/operations/jobs.py b/operations/jobs.py index 277d7a26..d9433033 100644 --- a/operations/jobs.py +++ b/operations/jobs.py @@ -154,7 +154,7 @@ def _existing(user, key, kind_name, params): return op -def submit(kind_name, user, raw_params, submission_key): +def submit(kind_name, user, raw_params, submission_key, parent=None): if not KEY_RE.fullmatch(submission_key or ""): raise OperationError(gettext_noop("The form has no valid submission key; reload the page")) kind = get_kind(kind_name) @@ -175,7 +175,7 @@ def submit(kind_name, user, raw_params, submission_key): raise op = Operation( kind=kind.name, user=user, username=user.username, submission_key=submission_key, - resource=plan.resource, resource_label=plan.label[:255], params=params, intent=plan.intent, + resource=plan.resource, resource_label=plan.label[:255], params=params, intent=plan.intent, parent=parent, time_limit=kind.time_limit, client_ip=_client_ip(), ) if kind.runs_in_request: diff --git a/operations/migrations/0004_parent.py b/operations/migrations/0004_parent.py new file mode 100644 index 00000000..6b0129ae --- /dev/null +++ b/operations/migrations/0004_parent.py @@ -0,0 +1,19 @@ +# Generated by Django 5.2.18 on 2026-11-15 16:59 + +import django.db.models.deletion +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('operations', '0003_client_ip'), + ] + + operations = [ + migrations.AddField( + model_name='operation', + name='parent', + field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='children', to='operations.operation'), + ), + ] diff --git a/operations/models.py b/operations/models.py index b31e0a00..4b53f418 100644 --- a/operations/models.py +++ b/operations/models.py @@ -35,6 +35,8 @@ class Operation(models.Model): client_ip = models.GenericIPAddressField(null=True) # of the submission, for the job's Logs rows submission_key = models.CharField(max_length=64) resource = models.CharField(max_length=255, db_index=True) # the page that shows the job + # the job that started this one (a bulk migration's migrations): its page shows that one only + parent = models.ForeignKey("self", null=True, on_delete=models.SET_NULL, related_name="children") resource_label = models.CharField(max_length=255) state = models.CharField(max_length=16, choices=STATES, default=QUEUED, db_index=True) params = models.JSONField(default=dict)