diff --git a/computes/tests/test_list_hosts.py b/computes/tests/test_list_hosts.py new file mode 100644 index 00000000..ae532790 --- /dev/null +++ b/computes/tests/test_list_hosts.py @@ -0,0 +1,45 @@ +"""The Computes page reads its hosts side by side: a host that does not +answer holds back only itself, and only up to the page's wait.""" + +import threading +import time +from unittest.mock import patch + +from django.contrib.auth import get_user_model +from django.test import TestCase +from django.urls import reverse + +from computes.models import Compute + + +class ListHostsTests(TestCase): + def setUp(self): + self.client.force_login(get_user_model().objects.create_superuser("lh-admin", "lh@example.com", "pw")) + for number, name in enumerate(("fast", "slow-1", "slow-2")): + Compute.objects.create(name=name, hostname="192.0.2.1", login="wvc", password="", type=1, + host_uuid=f"{number}" * 8 + "-0000-0000-0000-000000000000") + self.release = threading.Event() + self.addCleanup(self.release.set) + + def read(self, compute): + if compute.name.startswith("slow"): + self.release.wait(5) + return True, None, None + + def test_hosts_that_do_not_answer_are_waited_for_together_and_shown_so(self): + with patch("computes.views._read_host", side_effect=self.read), patch("computes.views.LIST_WAIT", 0.5): + started = time.monotonic() + response = self.client.get(reverse("computes")) + took = time.monotonic() - started + self.assertLess(took, 2) # one wait for both, not one each + shown = {compute.name: (compute.status, compute.connection_error) for compute in response.context["computes"]} + self.assertEqual(shown["fast"], (True, None)) + self.assertEqual(shown["slow-1"], (False, "The host did not answer within 0.5 seconds")) + self.assertEqual(response.context["totals"]["connected"], 1) + + def test_a_host_still_being_read_is_not_read_again(self): + with patch("computes.views._read_host", side_effect=self.read) as read, patch("computes.views.LIST_WAIT", 0.2): + self.client.get(reverse("computes")) + self.client.get(reverse("computes")) + self.assertEqual(sorted(call.args[0].name for call in read.call_args_list), + ["fast", "fast", "slow-1", "slow-2"]) diff --git a/computes/views.py b/computes/views.py index 11a0ca2e..edc6cf61 100644 --- a/computes/views.py +++ b/computes/views.py @@ -1,6 +1,9 @@ import json import logging +import threading from contextlib import suppress +from concurrent import futures +from copy import copy from admin.decorators import superuser_only from django.conf import settings @@ -79,6 +82,46 @@ def host_pools(compute): CONNECTIONS = {CONN_TCP: "TCP", CONN_SSH: "SSH", CONN_TLS: "TLS", CONN_SOCKET: "Socket"} +# the Computes page reads its hosts side by side, and waits this long for them in all +LIST_WAIT = 10 +LIST_WORKERS = 8 + + +def _read_host(compute): + """Whether the panel reaches the compute, why not, and its capacity (shown_capacity).""" + return compute.status, compute.connection_error, shown_capacity(compute) + + +# the reads of the Computes pages of this process: one pool, and one read of a host at a time, +# so a host that hangs holds one thread however often the page is loaded +_pool = None +_reading = {} # compute pk: its last read +_reading_lock = threading.Lock() + + +def read_hosts(computes): + """Reads each of computes (_read_host) on its own thread: a slow host + holds back only itself, and one not read within LIST_WAIT is shown as + not answering. A host whose read of an earlier load still runs is not + read again: that read is waited for. Each thread reads a copy: one still + running later does not change what the page shows.""" + global _pool + with _reading_lock: + if _pool is None: # in the process that serves: a pool's threads do not survive a fork + _pool = futures.ThreadPoolExecutor(max_workers=LIST_WORKERS, thread_name_prefix="wvc-hosts") + for compute in computes: + if compute.pk not in _reading or _reading[compute.pk].done(): + _reading[compute.pk] = _pool.submit(_read_host, copy(compute)) + reads = {compute: _reading[compute.pk] for compute in computes} + futures.wait(reads.values(), timeout=LIST_WAIT) + for compute, read in reads.items(): + if read.done(): + compute.status, compute.connection_error, compute.shown_capacity = read.result() + else: + compute.status, compute.shown_capacity = False, None + compute.connection_error = _("The host did not answer within %(seconds)s seconds") % {"seconds": LIST_WAIT} + + @superuser_only def computes(request): """The computes, with what each has given its VMs, and the totals of the @@ -86,13 +129,14 @@ def computes(request): computes = list(Compute.objects.order_by("name")) totals = {"hosts": len(computes), "connected": 0, "vms": 0, "running": 0, "vcpus": 0, "cpus": 0, "assigned": 0, "memory": 0} + read_hosts(computes) for compute in computes: compute.connection_name = CONNECTIONS.get(compute.type, "") if compute.status is True and not compute.host_uuid: # one added before host UUIDs were kept (Compute.clean); none for a second record of a host with suppress(IntegrityError): compute.record_host() - compute.shown_capacity = given = shown_capacity(compute) + given = compute.shown_capacity totals["connected"] += compute.status is True if given: totals["vms"] += given["vm_count"] diff --git a/locale/tr/LC_MESSAGES/django.mo b/locale/tr/LC_MESSAGES/django.mo index 96391066..2a3c4370 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 706aaf45..1657c497 100644 --- a/locale/tr/LC_MESSAGES/django.po +++ b/locale/tr/LC_MESSAGES/django.po @@ -11,7 +11,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2026-11-12 06:19+0000\n" +"POT-Creation-Date: 2026-11-12 06:17+0000\n" "PO-Revision-Date: 2020-11-04 12:00+0000\n" "Last-Translator: catborise , 2020\n" "Language-Team: Turkish (https://www.transifex.com/catborise/teams/110663/tr/)\n" @@ -1907,12 +1907,12 @@ msgstr "" "sanal makinenin root parolasını ve SSH anahtarını bununla ayarlar; boş: bu " "sunucuda ayarlanamazlar" -#: computes/models.py:179 +#: computes/models.py:176 #, python-format msgid "This host is already added as %(name)s" msgstr "Bu sunucu zaten %(name)s olarak ekli" -#: computes/models.py:182 +#: computes/models.py:179 #, python-format msgid "" "The panel cannot reach the host to tell that its VMs are there: %(error)s" @@ -1920,7 +1920,7 @@ msgstr "" "Panel sunucuya ulaşamadığı için VM'lerinin orada olduğunu doğrulayamıyor: " "%(error)s" -#: computes/models.py:189 +#: computes/models.py:186 msgid "" "The host its VMs are on is not known yet: change the connection when the " "panel has reached the host at its current one" @@ -1928,7 +1928,7 @@ msgstr "" "VM'lerinin bulunduğu sunucu henüz bilinmiyor: bağlantıyı, panel sunucuya " "mevcut bağlantısından ulaştıktan sonra değiştirin" -#: computes/models.py:192 +#: computes/models.py:189 msgid "This is another host than the one its VMs are on: add it as a new host" msgstr "" "Bu, VM'lerinin bulunduğu sunucudan başka bir sunucu: yeni bir sunucu olarak " @@ -2437,12 +2437,17 @@ msgstr "" "Sunucudaki /etc/default/webvirt-gstfsd içindeki GSTFSD_KEY değerinin 64 " "onaltılık karakterini girin" -#: computes/views.py:166 +#: computes/views.py:108 +#, python-format +msgid "The host did not answer within %(seconds)s seconds" +msgstr "Sunucu %(seconds)s saniye içinde yanıt vermedi" + +#: computes/views.py:196 #, python-format msgid "The host was saved, but the panel cannot connect to it: %(error)s" msgstr "Sunucu kaydedildi, ancak panel ona bağlanamıyor: %(error)s" -#: computes/views.py:222 +#: computes/views.py:252 #, python-format msgid "" "Its VMs keep running on the host and leave the panel; their owners are kept " diff --git a/locale/zh/LC_MESSAGES/django.mo b/locale/zh/LC_MESSAGES/django.mo index 1f6929a3..1ba49743 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 78ee5ec7..fea01355 100644 --- a/locale/zh/LC_MESSAGES/django.po +++ b/locale/zh/LC_MESSAGES/django.po @@ -7,7 +7,7 @@ msgid "" msgstr "" "Project-Id-Version: \n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2026-11-12 06:19+0000\n" +"POT-Creation-Date: 2026-11-12 06:17+0000\n" "PO-Revision-Date: \n" "Last-Translator: \n" "Language-Team: \n" @@ -1847,24 +1847,24 @@ msgstr "" "主机 /etc/default/webvirt-gstfsd 中的 GSTFSD_KEY:面板用它为已关机的虚拟机设置 root 密码和 SSH " "密钥;留空:无法在此设置" -#: computes/models.py:179 +#: computes/models.py:176 #, python-format msgid "This host is already added as %(name)s" msgstr "此主机已作为 %(name)s 添加" -#: computes/models.py:182 +#: computes/models.py:179 #, python-format msgid "" "The panel cannot reach the host to tell that its VMs are there: %(error)s" msgstr "面板无法连接该主机,因此无法确认其虚拟机在那里:%(error)s" -#: computes/models.py:189 +#: computes/models.py:186 msgid "" "The host its VMs are on is not known yet: change the connection when the " "panel has reached the host at its current one" msgstr "其虚拟机所在的主机尚不明确:请在面板通过当前连接访问到该主机后再更改连接" -#: computes/models.py:192 +#: computes/models.py:189 msgid "This is another host than the one its VMs are on: add it as a new host" msgstr "这不是其虚拟机所在的主机:请将其添加为新主机" @@ -2351,12 +2351,17 @@ msgid "" "/etc/default/webvirt-gstfsd" msgstr "请输入主机 /etc/default/webvirt-gstfsd 中 GSTFSD_KEY 的 64 个十六进制字符" -#: computes/views.py:166 +#: computes/views.py:108 +#, python-format +msgid "The host did not answer within %(seconds)s seconds" +msgstr "主机未在 %(seconds)s 秒内响应" + +#: computes/views.py:196 #, python-format msgid "The host was saved, but the panel cannot connect to it: %(error)s" msgstr "主机已保存,但面板无法连接到它:%(error)s" -#: computes/views.py:222 +#: computes/views.py:252 #, python-format msgid "" "Its VMs keep running on the host and leave the panel; their owners are kept "