1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-11-11 12:46:49 +00:00

fix(instances): a VM delete waits at most 60 s for another host's disk check

Deleting a VM with its disks first asks the other computes whether they
use its files. A host that is connected but does not answer (a hung NFS
mount under a pool) held the delete job for good, and with it a slot of
the worker: a few such deletes could fill the pool and stop the jobs of
healthy hosts. The check now waits USED_CHECK_WAIT, as the disk move and
migration checks do; a host that does not answer in time stops the
delete before anything changes, naming the host. Until it answers the
earlier check, a retried delete is refused at once instead of asking it
again, so the calls waiting on it do not pile up.

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 16:12:11 +03:00
parent f6c7f8c85e
commit 8197e645f9
6 changed files with 82 additions and 13 deletions

View file

@ -1,3 +1,5 @@
import threading
import time
from contextlib import contextmanager
from types import SimpleNamespace
from unittest.mock import MagicMock, patch
@ -9,6 +11,7 @@ from django.test import TestCase, TransactionTestCase
from django.urls import reverse
from instances.jobs import DeleteInstance
from instances.models import Instance
from instances import utils
from instances.utils import paths_used_on_other_computes
from libvirt import VIR_DOMAIN_UNDEFINE_KEEP_NVRAM, VIR_DOMAIN_UNDEFINE_NVRAM, VIR_ERR_NO_STORAGE_VOL, libvirtError
from logs.models import Logs
@ -297,3 +300,30 @@ class OtherComputesTestCase(TestCase):
self.assertEqual(caught.exception.args[0], "The disk files cannot be checked on host-b: connection refused")
# users who do not see hosts get neither the host nor libvirt's text
self.assertEqual(public_error_text(caught.exception), "The host of this virtual machine cannot be reached right now.")
def test_a_host_that_does_not_answer_is_an_error_in_bounded_time(self):
# connected, but stuck (a hung NFS mount): the delete job must not wait for it for good
hosts = self.hosts({"192.0.2.61": set(), "192.0.2.62": set()})
stuck = threading.Event()
self.addCleanup(stuck.set)
hosts["192.0.2.62"].paths_in_use.side_effect = lambda own: stuck.wait(10) and set()
with patch("instances.utils.wvmConnect", side_effect=lambda host, *args: hosts[host]), \
patch("instances.utils.USED_CHECK_WAIT", 0.2):
started = time.monotonic()
with self.assertRaises(ConnectionFailed) as caught:
paths_used_on_other_computes(self.source, ["/nfs/a.qcow2"])
self.assertLess(time.monotonic() - started, 2)
self.assertEqual(caught.exception.args[0], "The disk files cannot be checked on host-c: it did not answer within 0.2 seconds")
# a retry while the host still has not answered does not ask it again
with patch("instances.utils.wvmConnect", side_effect=lambda host, *args: hosts[host]):
with self.assertRaises(ConnectionFailed):
paths_used_on_other_computes(self.source, ["/nfs/a.qcow2"])
self.assertEqual(hosts["192.0.2.62"].paths_in_use.call_count, 1)
stuck.set() # it answers: it is asked again
hosts["192.0.2.62"].paths_in_use.side_effect = None
hosts["192.0.2.62"].paths_in_use.return_value = set()
deadline = time.monotonic() + 2
while utils._UNANSWERED and time.monotonic() < deadline:
time.sleep(0.01)
with patch("instances.utils.wvmConnect", side_effect=lambda host, *args: hosts[host]):
self.assertEqual(paths_used_on_other_computes(self.source, ["/nfs/a.qcow2"]), set())

View file

@ -50,28 +50,59 @@ 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()
@other_hosts()
def paths_used_on_other_computes(compute, own):
"""The paths in own that the VMs and volumes of the panel's other computes
use (paths_in_use): shared storage holds a file at the same path on every
host that mounts it, as migration needs. ConnectionFailed naming a compute
that cannot be read, which might use them: users who do not see hosts
learn only that a host cannot be reached."""
used = set()
that cannot be read, or does not answer within USED_CHECK_WAIT (as in
used_anywhere), which might use them: users who do not see hosts learn
only that a host cannot be reached."""
host = wvmConnect(compute.hostname, compute.login, compute.password, compute.type).host_uuid()
for other in Compute.objects.exclude(pk=compute.pk):
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 |= conn.paths_in_use(own=own)
except libvirtError as err:
raise ConnectionFailed(gettext_noop("The disk files cannot be checked on %(host)s: %(err)s"),
host=other.name, err=err) from err
others = list(Compute.objects.exclude(pk=compute.pk))
if waiting := [other.name for other in others if other.pk in _UNANSWERED]:
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(), [], []
def check():
for other in others:
asking[:] = [other.name]
_UNANSWERED.add(other.pk)
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))
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))
return
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)
thread.start()
thread.join(USED_CHECK_WAIT)
if failure:
raise failure[0]
if asking:
raise ConnectionFailed(gettext_noop("The disk files cannot be checked on %(host)s: it did not answer within "
"%(seconds)s seconds"), host=asking[0], seconds=USED_CHECK_WAIT)
return used & set(own)
USED_CHECK_WAIT = 60 # seconds a disk move or a migration waits for the computes to tell whether its old file is used
# seconds a disk move, a migration or a VM delete waits for the computes to tell whether a file is used
USED_CHECK_WAIT = 60
def used_anywhere(path, skip=None):

Binary file not shown.

View file

@ -10017,3 +10017,7 @@ msgstr "Kullanım (RSS)"
#, python-format
msgid "%(label)s: %(vcpu)s vCPU, %(memory)s MiB, new %(disk)s GiB disk"
msgstr "%(label)s: %(vcpu)s vCPU, %(memory)s MiB, yeni %(disk)s GiB disk"
#, python-format
msgid "The disk files cannot be checked on %(host)s: it did not answer within %(seconds)s seconds"
msgstr "Disk dosyaları %(host)s üzerinde denetlenemiyor: %(seconds)s saniye içinde yanıt vermedi"

Binary file not shown.

View file

@ -9436,3 +9436,7 @@ msgstr "使用量 (RSS)"
#, python-format
msgid "%(label)s: %(vcpu)s vCPU, %(memory)s MiB, new %(disk)s GiB disk"
msgstr "%(label)s:%(vcpu)s 个 vCPU,%(memory)s MiB,新建 %(disk)s GiB 磁盘"
#, python-format
msgid "The disk files cannot be checked on %(host)s: it did not answer within %(seconds)s seconds"
msgstr "无法在 %(host)s 上检查磁盘文件:它在 %(seconds)s 秒内没有响应"