1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-12-02 08:56:07 +00:00
webvirtcloud/operations/api/viewsets.py
catborise 4941a8a048 feat(computes): Balance proposes migrations that even out the hosts
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.
2026-11-16 07:37:10 +03:00

64 lines
3 KiB
Python

from django.db.models import Q
from django.http import Http404
from django.utils import timezone
from drf_spectacular.utils import OpenApiParameter, extend_schema
from rest_framework import mixins, viewsets
from rest_framework.decorators import action
from rest_framework.exceptions import ValidationError
from rest_framework.pagination import PageNumberPagination
from rest_framework.response import Response
from operations import jobs
from operations.models import Claim, Operation, last_seen, worker_gone
from operations.utils import may_see, summary, visible
from .serializers import OperationSerializer, ResourceJobsSerializer
SHOWN_STATES = Operation.ACTIVE + (Operation.UNCERTAIN,)
class Pages(PageNumberPagination):
page_size = 50
class OperationViewSet(mixins.ListModelMixin, mixins.RetrieveModelMixin, viewsets.GenericViewSet):
serializer_class = OperationSerializer
pagination_class = Pages
def get_queryset(self):
if getattr(self, "swagger_fake_view", False): # schema generation has no user
return Operation.objects.none()
user = self.request.user
qs = Operation.objects.all() if user.is_superuser else Operation.objects.filter(user=user)
fields = ("state", "kind", "resource", "username") if user.is_superuser else ("state", "kind", "resource")
for field in fields:
if value := self.request.query_params.get(field):
qs = qs.filter(**{field: value})
return qs
@extend_schema(request=None, responses=OperationSerializer)
@action(detail=True, methods=["post"])
def cancel(self, request, pk=None):
op = self.get_object()
return Response(OperationSerializer(jobs.cancel(op.pk, request.user), context={"request": request}).data)
@extend_schema(parameters=[OpenApiParameter("key", str, required=True)], responses=ResourceJobsSerializer)
@action(detail=False, methods=["get"])
def resource(self, request):
key = request.query_params.get("key", "")
if not key:
raise ValidationError({"key": "required"})
if not visible(request.user, key):
raise Http404
# the page's own jobs and the jobs on its resource; a job another one started (a bulk migration's
# migration) shows on its own page only, the one that started it on its pages
active = list(Operation.objects.filter(Q(resource=key) | Q(claims__resource=key, parent__isnull=True),
state__in=SHOWN_STATES).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()
short = Claim.objects.filter(resource=key, operation__isnull=True, expires_at__gt=timezone.now()).exists()
return Response(ResourceJobsSerializer(
{"jobs": mine, "others": others, "short_operation": short, "worker_last_seen": seen,
"worker_gone": worker_gone(seen)},
context={"request": request}).data)