1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-12-01 16:36:06 +00:00

fix(instances): a disk move or migration leaves one check on a stuck host

Whether a moved disk's old file is used elsewhere is asked of every
compute in a thread the job waits on for at most 60 s. A host that did
not answer kept that thread, and each later disk move or migration left
one more. As the VM delete's check does, a compute that does not answer
is not asked again: the file counts as used, so it is kept.

Both checks marked a compute as not answering from the moment they
asked it. A check beside another one (two jobs at once) took a healthy
compute that the other was asking for a stuck one, and refused at once;
and two overlapping checks could both ask a stuck one, the first to end
clearing the mark of the other. Each call to a compute now records when
it started, and a compute counts as not answering only while a call to
it has run for 60 s, checked again just before each compute is asked.

And a VM delete's check whose thread had not yet reached the first
compute when the 60 s ran out found no failure and no compute it was at,
and answered that no file was used: the disks could have been deleted
unchecked. It now counts as not answered.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kk4h2TcUiaWmU5MUszYSea
This commit is contained in:
catborise 2026-11-09 23:04:46 +03:00
parent d69d149d97
commit 5ab3644833
3 changed files with 97 additions and 19 deletions

View file

@ -3,7 +3,9 @@ import random
import re
import string
import threading
import time
from collections import namedtuple
from contextlib import contextmanager
from accounts.models import UserInstance, UserAttributes
from appsettings.settings import app_settings
@ -50,9 +52,32 @@ def get_clone_free_names(size=10):
return free_names
# the computes an earlier check still waits for: a new check does not ask them again, so
# retried deletes do not pile up calls to a host that does not answer
_UNANSWERED = set()
# when each call to a compute that has not returned started (time.monotonic()). A compute
# with one older than USED_CHECK_WAIT does not answer: a new check does not ask it, so
# retried deletes do not pile up calls to it; a check beside another one still asks.
_ASKING = {}
_ASKING_LOCK = threading.Lock()
@contextmanager
def _asking(compute):
started = time.monotonic()
with _ASKING_LOCK:
_ASKING.setdefault(compute.pk, []).append(started)
try:
yield
finally:
with _ASKING_LOCK:
_ASKING[compute.pk].remove(started)
if not _ASKING[compute.pk]:
del _ASKING[compute.pk]
def _unanswered(computes):
"""The first of computes that a call has waited on for USED_CHECK_WAIT, or None."""
now = time.monotonic()
with _ASKING_LOCK:
return next((c for c in computes if any(now - t >= USED_CHECK_WAIT for t in _ASKING.get(c.pk, ()))), None)
@other_hosts()
@ -65,20 +90,24 @@ def paths_used_on_other_computes(compute, own):
only that a host cannot be reached."""
host = wvmConnect(compute.hostname, compute.login, compute.password, compute.type).host_uuid()
others = list(Compute.objects.exclude(pk=compute.pk))
if waiting := [other.name for other in others if other.pk in _UNANSWERED]:
if waiting := _unanswered(others):
raise ConnectionFailed(gettext_noop("The disk files cannot be checked on %(host)s: it did not answer within "
"%(seconds)s seconds"), host=waiting[0], seconds=USED_CHECK_WAIT)
used, failure, asking = set(), [], []
"%(seconds)s seconds"), host=waiting.name, seconds=USED_CHECK_WAIT)
# the compute the check is at: the first one until the thread starts, so that a thread
# started late still counts as one that did not answer
used, failure, asking = set(), [], [other.name for other in others[:1]]
def check():
for other in others:
asking[:] = [other.name]
_UNANSWERED.add(other.pk)
if _unanswered([other]): # it stopped answering another check meanwhile
return
try:
conn = wvmConnect(other.hostname, other.login, other.password, other.type)
if host and conn.host_uuid() == host:
continue # the VM's own host under another record: its VM is the one going
used.update(conn.paths_in_use(own=own))
with _asking(other):
conn = wvmConnect(other.hostname, other.login, other.password, other.type)
if host and conn.host_uuid() == host:
continue # the VM's own host under another record: its VM is the one going
used.update(conn.paths_in_use(own=own))
except libvirtError as err:
failure.append(ConnectionFailed(gettext_noop("The disk files cannot be checked on %(host)s: %(err)s"),
host=other.name, err=err))
@ -86,8 +115,6 @@ def paths_used_on_other_computes(compute, own):
except Exception as err: # raised in the caller's thread, as before
failure.append(err)
return
finally:
_UNANSWERED.discard(other.pk)
asking.clear()
thread = threading.Thread(target=check, daemon=True)
@ -110,16 +137,23 @@ def used_anywhere(path, skip=None):
shared storage holds it for every host that mounts it; a compute that
cannot be read might, and so might one that does not answer within
USED_CHECK_WAIT: libvirt has no call timeout, and a stuck host would
hold the job for as long as it is stuck. skip: a compute not asked."""
hold the job for as long as it is stuck. A compute an earlier check still
has waited on for that long is not asked again (_ASKING). skip: a compute not asked."""
computes = list(Compute.objects.exclude(pk=skip))
if _unanswered(computes):
return True
answer = []
def check():
for c in computes:
if _unanswered([c]): # it stopped answering another check meanwhile
answer.append(True)
return
try:
if path in wvmConnect(c.hostname, c.login, c.password, c.type).paths_in_use():
answer.append(True)
return
with _asking(c):
if path in wvmConnect(c.hostname, c.login, c.password, c.type).paths_in_use():
answer.append(True)
return
except libvirtError:
answer.append(True)
return