diff --git a/instances/tests/test_destroy.py b/instances/tests/test_destroy.py index 8e9ce166..17eb4f93 100644 --- a/instances/tests/test_destroy.py +++ b/instances/tests/test_destroy.py @@ -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()) diff --git a/instances/utils.py b/instances/utils.py index 0d45594f..0fb078ba 100644 --- a/instances/utils.py +++ b/instances/utils.py @@ -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): diff --git a/locale/tr/LC_MESSAGES/django.mo b/locale/tr/LC_MESSAGES/django.mo index d60afdc1..c6ed8edd 100644 Binary files a/locale/tr/LC_MESSAGES/django.mo and b/locale/tr/LC_MESSAGES/django.mo differ diff --git a/locale/tr/LC_MESSAGES/django.po b/locale/tr/LC_MESSAGES/django.po index 1963eea2..ccf0b2c8 100644 --- a/locale/tr/LC_MESSAGES/django.po +++ b/locale/tr/LC_MESSAGES/django.po @@ -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" diff --git a/locale/zh/LC_MESSAGES/django.mo b/locale/zh/LC_MESSAGES/django.mo index f230ee94..abdb27af 100644 Binary files a/locale/zh/LC_MESSAGES/django.mo and b/locale/zh/LC_MESSAGES/django.mo differ diff --git a/locale/zh/LC_MESSAGES/django.po b/locale/zh/LC_MESSAGES/django.po index 3ddbd856..55db0f25 100644 --- a/locale/zh/LC_MESSAGES/django.po +++ b/locale/zh/LC_MESSAGES/django.po @@ -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 秒内没有响应"