1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-12-02 17:06:09 +00:00
webvirtcloud/vmbackups/schedule.py
catborise 819cdada69 feat(vmbackups): schedules back VMs up daily, keep the newest, check targets
The worker's periodic pass (about once a minute) runs the schedules:

- Each enabled schedule's VMs are backed up once a local day, from the
  schedule's time for 6 hours, as its owner. A late time's window goes
  on after midnight; a time a spring-forward gap skips falls an hour
  later, a repeated autumn time is its first. A run row per schedule,
  VM row and day, and a submission key made from them, keep a worker
  that stopped between the job and its row from backing a VM up twice.
- A busy target (one copy at a time) or an unreachable host is tried
  again at the next pass; a host in maintenance is waited for. A VM not
  started within the window, a VM that cannot be backed up (TPM) and a
  schedule whose owner is no longer an active superuser get a run row
  saying why. Times passed before a schedule was made are not run.
- Each compute's submissions are a task of their own: a hung host stops
  no other compute's schedules.
- Retention: each schedule's ready backups of a VM beyond keep, oldest
  first, one deletion a target at a time. The deletion checks again,
  before it changes anything, that the backup is still beyond keep.
  Backups by hand and backups of removed VMs are left alone.
- Each target is checked once a day by a backup.check job holding it: a
  ready backup with a file gone, its manifest included, becomes missing
  (restore refused, delete allowed); one whose files are back is ready
  again. A pool no compute reaches marks nothing.
- A scheduled backup carries the end of its window: one that waited
  past it for a worker slot, or whose host went into maintenance
  meanwhile, ends without starting (checked after the host answered).

The jobs on a target alone (delete, read back, check) hold its computes
too, shared: the worker counts a job's hosts by these, so a hung host
takes no more than its share of the slots.
2026-11-17 11:38:31 +03:00

183 lines
8.6 KiB
Python

"""Backup schedules, run by the worker about once a minute (its periodic
tasks): each schedule's VMs backed up once a local day within WINDOW of its
time, each VM's backups of the schedule beyond keep deleted, and each target
checked once a day. The jobs run as the schedule's owner."""
import datetime
import logging
from concurrent.futures import ThreadPoolExecutor
from django.contrib.auth import get_user_model
from django.db import close_old_connections
from django.core.exceptions import PermissionDenied
from django.utils import timezone
from django.utils.translation import gettext_noop as _
from libvirt import libvirtError
from computes.models import Compute
from operations import claims, jobs
from operations.models import Operation
from operations.utils import safe_text
from vrtManager.util import ConnectionFailed
from .models import Backup, BackupPolicy, BackupRun, BackupTarget
logger = logging.getLogger(__name__)
WINDOW = datetime.timedelta(hours=6) # a run is admitted this long after its time, else skipped
NOT_STARTED = _("Not started within 6 hours of its time")
IN_MAINTENANCE = _("The host was in maintenance")
NO_OWNER = _("The schedule's owner is no longer an active superuser")
RETRY_AFTER = datetime.timedelta(hours=1) # a deletion or check that failed is tried again after this
# each compute's submissions in a task of their own, at most one a compute: with a thread for every
# compute, hung hosts take no thread a healthy host's task needs (threads start only when needed)
THREADS = 64
_pool = ThreadPoolExecutor(THREADS, thread_name_prefix="wvc-schedule")
_tasks = {} # compute id -> the future of its last task
def run_schedules(now=None):
now = now or timezone.now()
due = {} # compute id -> [(policy, vm, day)]
for policy in BackupPolicy.objects.filter(enabled=True).select_related("owner"):
for day, start in occurrences(policy, now):
open_ = now < start + WINDOW
done = set(BackupRun.objects.filter(policy=policy, day=day).values_list("instance_id", flat=True))
for vm in policy.vms():
if vm.pk in done:
continue
in_maintenance = Compute.objects.filter(pk=vm.compute_id, maintenance_since__isnull=False).exists()
if not open_:
_record(policy, vm, day, BackupRun.SKIPPED, IN_MAINTENANCE if in_maintenance else NOT_STARTED)
elif not in_maintenance: # deferred while the window lasts
due.setdefault(vm.compute_id, []).append((policy.pk, vm.pk, day, start))
for compute_id, items in due.items():
task = _tasks.get(compute_id)
if task is None or task.done():
_tasks[compute_id] = _pool.submit(_submit_all, compute_id, items)
retain()
check_targets(now)
def occurrences(policy, now):
"""(local day, instant) of the schedule's times yesterday and today that
have come and are not older than the schedule: a late time's window goes
on after midnight. A time a spring-forward gap skips falls an hour later
(zoneinfo, fold 0); a time repeated in autumn is its first."""
zone = timezone.get_default_timezone()
today = timezone.localtime(now, zone).date()
for day in (today - datetime.timedelta(days=1), today):
# in UTC: the window is real hours, also on a day the clocks change
start = datetime.datetime.combine(day, policy.time, tzinfo=zone).astimezone(datetime.timezone.utc)
if policy.created_at <= start <= now:
yield day, start
def _record(policy, vm, day, state, note="", operation=None):
BackupRun.objects.get_or_create(policy=policy, instance=vm, day=day,
defaults={"state": state, "note": note[:255], "operation": operation})
def _active_superuser(user):
return user is not None and user.is_active and user.is_superuser
def _submit_all(compute_id, items):
"""One compute's due backups, one after another, each checked again as
it comes: the task may have waited for a thread. A busy target or an
unreachable host is tried again at the next pass."""
try:
for policy_id, vm_id, day, start in items:
_submit(policy_id, vm_id, day, start)
finally:
close_old_connections() # a thread of the pool: its connection is its own
def _submit(policy_id, vm_id, day, start):
policy = BackupPolicy.objects.select_related("owner").filter(pk=policy_id, enabled=True).first()
now = timezone.now()
if policy is None or now >= start + WINDOW or (day, start) not in set(occurrences(policy, now)):
return # disabled, deleted or its time moved meanwhile, or its window ended: the next pass decides
vm = policy.vms().filter(pk=vm_id).first()
if vm is None or BackupRun.objects.filter(policy=policy, instance=vm, day=day).exists():
return
if Compute.objects.filter(pk=vm.compute_id, maintenance_since__isnull=False).exists():
return
if not _active_superuser(policy.owner):
_record(policy, vm, day, BackupRun.FAILED, NO_OWNER)
return
# the same key for the same run: a worker stopped before the row was written gets the same job back
key = f"bk-{policy.pk}-{vm.pk}-{day:%Y%m%d}"
raw = {"instance": vm.pk, "target": policy.target_id, "frozen": policy.frozen, "policy": policy.pk,
"deadline": (start + WINDOW).isoformat()} # a job that waited past it for a worker slot does not start
try:
op = jobs.submit("backup.create", policy.owner, raw, key)
except (claims.Busy, ConnectionFailed):
return
except (libvirtError, PermissionDenied, jobs.SubmissionConflict) as err: # it would be refused again
_record(policy, vm, day, BackupRun.FAILED, safe_text(str(err) or type(err).__name__))
return
except Exception as err:
logger.error("schedule %s: backup of %s not submitted: %s", policy.name, vm.name, safe_text(err))
return
_record(policy, vm, day, BackupRun.SUBMITTED, operation=op)
def _attempt_key(base):
"""The submission key of the next attempt of a job named base: the
running or ended one's own, so it is not submitted twice; a new one an
hour after a failed or cancelled one; None before that."""
attempts = Operation.objects.filter(submission_key__startswith=f"{base}-")
last = attempts.order_by("-created_at").first()
if last is None:
return f"{base}-0"
if last.state not in (Operation.FAILED, Operation.CANCELLED):
return last.submission_key
if last.finished_at and timezone.now() - last.finished_at < RETRY_AFTER:
return None
return f"{base}-{attempts.count()}"
def retain():
"""Each schedule's ready backups of a VM beyond keep, oldest first: one
deletion a target at a time (its claim admits one), the rest at the next
passes. Backups by hand and those of removed VMs are left alone."""
tried = set()
for policy in BackupPolicy.objects.select_related("owner"):
if policy.target_id in tried or not _active_superuser(policy.owner):
continue
ready = (Backup.objects.filter(policy=policy, state=Backup.READY, instance__isnull=False)
.order_by("instance_id", "-created_at"))
groups = {}
for backup in ready:
groups.setdefault(backup.instance_id, []).append(backup)
for rows in groups.values():
if len(rows) > policy.keep:
tried.add(policy.target_id)
if (key := _attempt_key(f"rt-{rows[-1].uuid.hex}")) is None:
break
try:
jobs.submit("backup.delete", policy.owner, {"backup": str(rows[-1].uuid), "retention": policy.pk},
key)
except Exception as err: # busy, or refused: the next pass looks again
logger.info("schedule %s: retention waits: %s", policy.name, safe_text(err))
break
def check_targets(now):
"""A backup.check job for each target not checked today, as the first
active superuser; a busy target is checked at a later pass."""
today = timezone.localtime(now).date()
user = get_user_model().objects.filter(is_superuser=True, is_active=True).order_by("pk").first()
if user is None:
return
for target in BackupTarget.objects.filter(pools__isnull=False).distinct():
if target.checked_at and timezone.localtime(target.checked_at).date() >= today:
continue
if (key := _attempt_key(f"ck-{target.pk}-{today:%Y%m%d}")) is None:
continue
try:
jobs.submit("backup.check", user, {"target": target.pk}, key)
except Exception as err:
logger.info("target %s: check waits: %s", target.name, safe_text(err))