mirror of
https://github.com/retspen/webvirtcloud
synced 2026-12-02 17:06:09 +00:00
fix(instances): a host that does not answer no longer holds the VM lists
The VM list and its poll read each host in turn. A host that is up but stuck (a daemon or an NFS mount that does not answer) kept the call waiting: the list did not open in 200 s (seen live with compute1's virtqemud stopped), and the per-host limit freed it only when gunicorn ended the request after 600 s. The list and the poll now ask all hosts at once, each in a thread counted on its host like a request, and wait 10 s at most: a host that has not answered is shown as not answering, and the VMs of the others are listed. Live: the list opens in 10 s, then in 5 s while the host stays stuck.
This commit is contained in:
parent
b65d2abaf6
commit
cf1f7ad055
9 changed files with 176 additions and 53 deletions
|
|
@ -191,7 +191,7 @@ def instances(request, compute_id):
|
|||
states = vm_states(request.user, [compute])
|
||||
utils.refresh_instance_database(compute)
|
||||
instances = list(Instance.objects.filter(compute=compute).order_by("name").prefetch_related("userinstance_set"))
|
||||
read_list_rows(instances)
|
||||
read_list_rows(instances, [compute])
|
||||
|
||||
return render(
|
||||
request, "computes/instances.html",
|
||||
|
|
|
|||
55
instances/tests/test_list_hung_host.py
Normal file
55
instances/tests/test_list_hung_host.py
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
"""A VM list asks all hosts at once and waits LIST_HOST_WAIT at most: a host
|
||||
that does not answer (a stuck daemon) is shown not answering, and the other
|
||||
hosts' VMs are listed."""
|
||||
|
||||
import threading
|
||||
import time
|
||||
from types import SimpleNamespace
|
||||
from unittest.mock import PropertyMock, patch
|
||||
|
||||
from computes.models import Compute
|
||||
from django.test import TestCase
|
||||
|
||||
from instances import utils
|
||||
|
||||
|
||||
class HungHostTests(TestCase):
|
||||
def test_a_host_that_does_not_answer_is_left_out_in_time(self):
|
||||
hung, ok = (Compute.objects.create(name=name, hostname=f"192.0.2.{n}", login="", password="", type=1)
|
||||
for n, name in ((1, "hung"), (2, "ok")))
|
||||
release = threading.Event()
|
||||
self.addCleanup(release.set)
|
||||
|
||||
def states(proxy_of):
|
||||
def get_domain_states():
|
||||
if proxy_of == "hung":
|
||||
release.wait(5)
|
||||
return {"u-1": ["vm", 1]}
|
||||
return SimpleNamespace(get_domain_states=get_domain_states)
|
||||
|
||||
# each copy of a compute (one per thread) gets the proxy of its host
|
||||
with patch.object(Compute, "status", new_callable=PropertyMock, return_value=True), \
|
||||
patch.object(Compute, "proxy", property(lambda compute: states(compute.name))), \
|
||||
patch.object(utils, "LIST_HOST_WAIT", 0.5):
|
||||
start = time.monotonic()
|
||||
result = utils.host_domain_states([hung, ok])
|
||||
waited = time.monotonic() - start
|
||||
self.assertLess(waited, 2)
|
||||
self.assertEqual(result, {hung.pk: None, ok.pk: {"u-1": ["vm", 1]}})
|
||||
self.assertIs(hung.status, False)
|
||||
self.assertIn("did not answer within", str(hung.connection_error))
|
||||
|
||||
def test_a_host_whose_read_fails_is_marked(self):
|
||||
# connected, but its answer an error: the page says so and does not ask it again
|
||||
from libvirt import libvirtError
|
||||
|
||||
host = Compute.objects.create(name="failing", hostname="192.0.2.3", login="", password="", type=1)
|
||||
|
||||
def fail():
|
||||
raise libvirtError("internal error: no domains for you")
|
||||
|
||||
with patch.object(Compute, "status", new_callable=PropertyMock, return_value=True), \
|
||||
patch.object(Compute, "proxy", property(lambda compute: SimpleNamespace(get_domain_states=fail))):
|
||||
self.assertEqual(utils.host_domain_states([host]), {host.pk: None})
|
||||
self.assertIs(host.status, False)
|
||||
self.assertIn("no domains for you", str(host.connection_error))
|
||||
|
|
@ -5,6 +5,7 @@ import string
|
|||
import threading
|
||||
import time
|
||||
from collections import namedtuple
|
||||
from copy import copy as shallow_copy
|
||||
from contextlib import contextmanager
|
||||
|
||||
from accounts.models import UserInstance, UserAttributes
|
||||
|
|
@ -18,9 +19,9 @@ from django.utils.translation import gettext_noop
|
|||
from libvirt import libvirtError
|
||||
from logs.audit import reason, write
|
||||
from operations.models import Operation
|
||||
from vrtManager.connection import other_hosts, wvmConnect
|
||||
from vrtManager.connection import counted_thread, other_hosts, wvmConnect
|
||||
from vrtManager.instance import wvmInstance, wvmInstances
|
||||
from vrtManager.util import ConnectionFailed, message, vol_dev_type
|
||||
from vrtManager.util import ConnectionFailed, HostBusy, message, vol_dev_type
|
||||
|
||||
from .models import Instance
|
||||
|
||||
|
|
@ -418,12 +419,16 @@ def record_migration(instance, new_compute):
|
|||
instance.save()
|
||||
|
||||
|
||||
def read_list_rows(instances):
|
||||
def read_list_rows(instances, computes):
|
||||
"""Each VM's info and title for a VM list, read once a host
|
||||
(get_domain_rows), not three calls a VM; a VM whose host or whose
|
||||
domain cannot be read has no info: the list shows it unknown."""
|
||||
domain cannot be read has no info: the list shows it unknown. The VMs
|
||||
take the list's computes, on which vm_states marked those that did not
|
||||
answer."""
|
||||
by_pk = {compute.pk: compute for compute in computes}
|
||||
rows, shown = {}, {}
|
||||
for inst in instances:
|
||||
inst.compute = by_pk.get(inst.compute_id, inst.compute)
|
||||
shown.setdefault(inst.compute_id, set()).add(inst.uuid)
|
||||
for compute in {inst.compute_id: inst.compute for inst in instances}.values():
|
||||
try:
|
||||
|
|
@ -434,20 +439,61 @@ def read_list_rows(instances):
|
|||
inst.info, inst.title = rows[inst.compute_id].get(inst.uuid, (None, ""))
|
||||
|
||||
|
||||
# how long a VM list and its poll wait for a host's VMs, all hosts asked at once
|
||||
LIST_HOST_WAIT = 10
|
||||
|
||||
|
||||
def host_domain_states(computes):
|
||||
"""{compute pk: get_domain_states(), or None when it cannot be read}, all
|
||||
hosts asked at once. One that fails, or does not answer within
|
||||
LIST_HOST_WAIT seconds, is None and is marked not answering for the rest
|
||||
of the request (its status and connection): a stuck host no longer holds
|
||||
every list."""
|
||||
answers = {} # compute pk: its states, or the exception that ended the read
|
||||
|
||||
def read(pk, compute):
|
||||
with counted_thread():
|
||||
try:
|
||||
if compute.status is not True:
|
||||
raise compute.connection # the error it connected with
|
||||
answers[pk] = compute.proxy.get_domain_states()
|
||||
except Exception as error:
|
||||
answers[pk] = error
|
||||
|
||||
# each thread a copy of its compute: one that answers late sets nothing on the request's;
|
||||
# daemon threads: one stuck on its host does not hold the process when it exits
|
||||
threads = [threading.Thread(target=read, args=(compute.pk, shallow_copy(compute)), daemon=True)
|
||||
for compute in computes]
|
||||
for thread in threads:
|
||||
thread.start()
|
||||
deadline = time.monotonic() + LIST_HOST_WAIT
|
||||
for thread in threads:
|
||||
thread.join(max(0, deadline - time.monotonic()))
|
||||
states = {}
|
||||
for compute in computes:
|
||||
answer = answers.get(compute.pk)
|
||||
states[compute.pk] = answer if isinstance(answer, dict) else None
|
||||
if isinstance(answer, dict):
|
||||
continue
|
||||
failure = answer or HostBusy(gettext_noop("Connection Failed: %(host)s did not answer within %(seconds)s seconds"),
|
||||
host=compute.hostname, seconds=LIST_HOST_WAIT)
|
||||
# what its cached properties then give for the rest of the request
|
||||
vars(compute).update(connection=failure, status=False)
|
||||
return states
|
||||
|
||||
|
||||
def vm_states(user, computes):
|
||||
"""{compute pk: {"ok": True, "vms": {uuid: [name, state]}}, or {"ok": False}
|
||||
for a host that cannot be read}: what the VM lists poll, read from libvirt;
|
||||
for a user who does not see hosts only their own VMs (a VM is its host and
|
||||
UUID: one UUID may be defined on two hosts)."""
|
||||
for a host that cannot be read}: what the VM lists poll, read from libvirt
|
||||
(host_domain_states); for a user who does not see hosts only their own
|
||||
VMs (a VM is its host and UUID: one UUID may be defined on two hosts)."""
|
||||
show_hosts = user.is_superuser or user.has_perm("instances.view_instances")
|
||||
own = None if show_hosts else set(
|
||||
Instance.objects.filter(userinstance__user=user).values_list("compute_id", "uuid"))
|
||||
result = {}
|
||||
states = host_domain_states(computes)
|
||||
for compute in computes:
|
||||
try:
|
||||
vms = compute.proxy.get_domain_states() if compute.status is True else None
|
||||
except libvirtError:
|
||||
vms = None
|
||||
vms = states[compute.pk]
|
||||
result[str(compute.pk)] = {"ok": False} if vms is None else {
|
||||
"ok": True, "vms": {u: s for u, s in vms.items() if own is None or (compute.pk, u) in own}}
|
||||
return result
|
||||
|
|
|
|||
|
|
@ -106,7 +106,7 @@ def index(request):
|
|||
# other users get no host names or connection errors, only whether
|
||||
# a host of their own VMs is down
|
||||
own_hosts_down = any(c.status is not True for c in computes)
|
||||
utils.read_list_rows(instances)
|
||||
utils.read_list_rows(instances, computes)
|
||||
if app_settings.VM_DRBD_STATUS == "True":
|
||||
# read here, once per host and all hosts at once (a list waits DRBD_STATUS_TIMEOUT at most):
|
||||
# the VM page alone left the list showing an old state
|
||||
|
|
|
|||
Binary file not shown.
|
|
@ -11,7 +11,7 @@ msgid ""
|
|||
msgstr ""
|
||||
"Project-Id-Version: PACKAGE VERSION\n"
|
||||
"Report-Msgid-Bugs-To: \n"
|
||||
"POT-Creation-Date: 2026-11-13 11:54+0000\n"
|
||||
"POT-Creation-Date: 2026-11-13 12:28+0000\n"
|
||||
"PO-Revision-Date: 2020-11-04 12:00+0000\n"
|
||||
"Last-Translator: catborise <muhammetalisag@gmail.com>, 2020\n"
|
||||
"Language-Team: Turkish (https://www.transifex.com/catborise/teams/110663/tr/)\n"
|
||||
|
|
@ -6122,7 +6122,7 @@ msgstr "En son satırlar, en yenisi önce."
|
|||
msgid "All of this VM's logs"
|
||||
msgstr "Bu sanal makinenin tüm logları"
|
||||
|
||||
#: instances/utils.py:94 instances/utils.py:126
|
||||
#: instances/utils.py:96 instances/utils.py:128
|
||||
#, python-format
|
||||
msgid ""
|
||||
"The disk files cannot be checked on %(host)s: it did not answer within "
|
||||
|
|
@ -6131,12 +6131,12 @@ msgstr ""
|
|||
"Disk dosyaları %(host)s üzerinde denetlenemiyor: %(seconds)s saniye içinde "
|
||||
"yanıt vermedi"
|
||||
|
||||
#: instances/utils.py:112
|
||||
#: instances/utils.py:114
|
||||
#, python-format
|
||||
msgid "The disk files cannot be checked on %(host)s: %(err)s"
|
||||
msgstr "Disk dosyaları %(host)s üzerinde denetlenemiyor: %(err)s"
|
||||
|
||||
#: instances/utils.py:176
|
||||
#: instances/utils.py:178
|
||||
msgid ""
|
||||
"The quota cannot be checked: a host of your virtual machines cannot be "
|
||||
"reached, or the size of one of their disks cannot be read. Nothing was "
|
||||
|
|
@ -6145,41 +6145,46 @@ msgstr ""
|
|||
"Kota denetlenemiyor: sanal makinelerinizin bir sunucusuna erişilemiyor ya da "
|
||||
"disklerinden birinin boyutu okunamıyor. Hiçbir şey değiştirilmedi."
|
||||
|
||||
#: instances/utils.py:183
|
||||
#: instances/utils.py:185
|
||||
msgid "The quota of virtual machines is reached"
|
||||
msgstr "Sanal makine kotası doldu"
|
||||
|
||||
#: instances/utils.py:184
|
||||
#: instances/utils.py:186
|
||||
msgid "The vCPU quota is reached"
|
||||
msgstr "vCPU kotası doldu"
|
||||
|
||||
#: instances/utils.py:185
|
||||
#: instances/utils.py:187
|
||||
msgid "The memory quota is reached"
|
||||
msgstr "Bellek kotası doldu"
|
||||
|
||||
#: instances/utils.py:186
|
||||
#: instances/utils.py:188
|
||||
msgid "The disk quota is reached"
|
||||
msgstr "Disk kotası doldu"
|
||||
|
||||
#: instances/utils.py:195
|
||||
#: instances/utils.py:197
|
||||
#, python-format
|
||||
msgid "%(quota)s (%(total)s > %(limit)s)"
|
||||
msgstr "%(quota)s (%(total)s > %(limit)s)"
|
||||
|
||||
#: instances/utils.py:287
|
||||
#: instances/utils.py:289
|
||||
msgid "None available device name"
|
||||
msgstr "Hiçbiri uygun aygıt adı değil"
|
||||
|
||||
#: instances/utils.py:460
|
||||
#: instances/utils.py:472
|
||||
#, python-format
|
||||
msgid "Connection Failed: %(host)s did not answer within %(seconds)s seconds"
|
||||
msgstr "Bağlantı başarısız: %(host)s %(seconds)s saniye içinde yanıt vermedi"
|
||||
|
||||
#: instances/utils.py:503
|
||||
#, python-format
|
||||
msgid "%(hostname)s via %(address)s"
|
||||
msgstr "%(address)s üzerinden %(hostname)s"
|
||||
|
||||
#: instances/utils.py:493
|
||||
#: instances/utils.py:536
|
||||
msgid "Invalid MAC address"
|
||||
msgstr "Geçersiz MAC adresi"
|
||||
|
||||
#: instances/utils.py:495
|
||||
#: instances/utils.py:538
|
||||
msgid "More MAC addresses than networks"
|
||||
msgstr "Ağlardan daha fazla MAC adresi"
|
||||
|
||||
|
|
@ -9240,36 +9245,36 @@ msgstr ""
|
|||
"Bağlantı başarısız: %(host)s yanıt vermiyor: %(requests)s istek hâlâ onu "
|
||||
"bekliyor"
|
||||
|
||||
#: vrtManager/connection.py:231
|
||||
#: vrtManager/connection.py:243
|
||||
#, python-format
|
||||
msgid "Connection Failed: %(host)s did not answer within %(seconds)s s"
|
||||
msgstr "Bağlantı başarısız: %(host)s %(seconds)s sn içinde yanıt vermedi"
|
||||
|
||||
#: vrtManager/connection.py:242
|
||||
#: vrtManager/connection.py:254
|
||||
msgid "the connection closed right after opening"
|
||||
msgstr "bağlantı açıldıktan hemen sonra kapandı"
|
||||
|
||||
#: vrtManager/connection.py:255
|
||||
#: vrtManager/connection.py:267
|
||||
#, python-format
|
||||
msgid "Connection Failed: %(error)s"
|
||||
msgstr "Bağlantı başarısız: %(error)s"
|
||||
|
||||
#: vrtManager/connection.py:324
|
||||
#: vrtManager/connection.py:336
|
||||
#, python-format
|
||||
msgid "Connection closed (%(reason)s)"
|
||||
msgstr "Bağlantı kapandı (%(reason)s)"
|
||||
|
||||
#: vrtManager/connection.py:476
|
||||
#: vrtManager/connection.py:488
|
||||
#, python-format
|
||||
msgid "Connection Failed: %(host)s is not connected"
|
||||
msgstr "Bağlantı başarısız: %(host)s bağlı değil"
|
||||
|
||||
#: vrtManager/connection.py:528
|
||||
#: vrtManager/connection.py:540
|
||||
#, python-format
|
||||
msgid "This host has no architecture %(arch)s"
|
||||
msgstr "Bu sunucuda %(arch)s mimarisi yok"
|
||||
|
||||
#: vrtManager/connection.py:1191
|
||||
#: vrtManager/connection.py:1203
|
||||
#, python-format
|
||||
msgid ""
|
||||
"Pool %(pool)s is not active: the files the disks of the VMs in it are made "
|
||||
|
|
|
|||
Binary file not shown.
|
|
@ -7,7 +7,7 @@ msgid ""
|
|||
msgstr ""
|
||||
"Project-Id-Version: \n"
|
||||
"Report-Msgid-Bugs-To: \n"
|
||||
"POT-Creation-Date: 2026-11-13 11:54+0000\n"
|
||||
"POT-Creation-Date: 2026-11-13 12:28+0000\n"
|
||||
"PO-Revision-Date: \n"
|
||||
"Last-Translator: \n"
|
||||
"Language-Team: \n"
|
||||
|
|
@ -5788,60 +5788,65 @@ msgstr "最近的记录,最新的在前。"
|
|||
msgid "All of this VM's logs"
|
||||
msgstr "该虚拟机的全部日志"
|
||||
|
||||
#: instances/utils.py:94 instances/utils.py:126
|
||||
#: instances/utils.py:96 instances/utils.py:128
|
||||
#, 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 秒内没有响应"
|
||||
|
||||
#: instances/utils.py:112
|
||||
#: instances/utils.py:114
|
||||
#, python-format
|
||||
msgid "The disk files cannot be checked on %(host)s: %(err)s"
|
||||
msgstr "无法在 %(host)s 上检查磁盘文件:%(err)s"
|
||||
|
||||
#: instances/utils.py:176
|
||||
#: instances/utils.py:178
|
||||
msgid ""
|
||||
"The quota cannot be checked: a host of your virtual machines cannot be "
|
||||
"reached, or the size of one of their disks cannot be read. Nothing was "
|
||||
"changed."
|
||||
msgstr "无法检查配额:您的某台虚拟机所在主机无法连接,或其某个磁盘的大小无法读取。未做任何更改。"
|
||||
|
||||
#: instances/utils.py:183
|
||||
#: instances/utils.py:185
|
||||
msgid "The quota of virtual machines is reached"
|
||||
msgstr "虚拟机配额已用尽"
|
||||
|
||||
#: instances/utils.py:184
|
||||
#: instances/utils.py:186
|
||||
msgid "The vCPU quota is reached"
|
||||
msgstr "vCPU 配额已用尽"
|
||||
|
||||
#: instances/utils.py:185
|
||||
#: instances/utils.py:187
|
||||
msgid "The memory quota is reached"
|
||||
msgstr "内存配额已用尽"
|
||||
|
||||
#: instances/utils.py:186
|
||||
#: instances/utils.py:188
|
||||
msgid "The disk quota is reached"
|
||||
msgstr "磁盘配额已用尽"
|
||||
|
||||
#: instances/utils.py:195
|
||||
#: instances/utils.py:197
|
||||
#, python-format
|
||||
msgid "%(quota)s (%(total)s > %(limit)s)"
|
||||
msgstr "%(quota)s(%(total)s > %(limit)s)"
|
||||
|
||||
#: instances/utils.py:287
|
||||
#: instances/utils.py:289
|
||||
msgid "None available device name"
|
||||
msgstr "没有可用的设备名称"
|
||||
|
||||
#: instances/utils.py:460
|
||||
#: instances/utils.py:472
|
||||
#, python-format
|
||||
msgid "Connection Failed: %(host)s did not answer within %(seconds)s seconds"
|
||||
msgstr "连接失败:%(host)s 在 %(seconds)s 秒内无响应"
|
||||
|
||||
#: instances/utils.py:503
|
||||
#, python-format
|
||||
msgid "%(hostname)s via %(address)s"
|
||||
msgstr "%(hostname)s,经由 %(address)s"
|
||||
|
||||
#: instances/utils.py:493
|
||||
#: instances/utils.py:536
|
||||
msgid "Invalid MAC address"
|
||||
msgstr "无效的 MAC 地址"
|
||||
|
||||
#: instances/utils.py:495
|
||||
#: instances/utils.py:538
|
||||
msgid "More MAC addresses than networks"
|
||||
msgstr "MAC 地址数量多于网络数量"
|
||||
|
||||
|
|
@ -8729,36 +8734,36 @@ msgid ""
|
|||
" for it"
|
||||
msgstr "连接失败:%(host)s 无响应:仍有 %(requests)s 个请求在等待它"
|
||||
|
||||
#: vrtManager/connection.py:231
|
||||
#: vrtManager/connection.py:243
|
||||
#, python-format
|
||||
msgid "Connection Failed: %(host)s did not answer within %(seconds)s s"
|
||||
msgstr "连接失败:%(host)s 在 %(seconds)s 秒内未响应"
|
||||
|
||||
#: vrtManager/connection.py:242
|
||||
#: vrtManager/connection.py:254
|
||||
msgid "the connection closed right after opening"
|
||||
msgstr "连接在打开后立即关闭"
|
||||
|
||||
#: vrtManager/connection.py:255
|
||||
#: vrtManager/connection.py:267
|
||||
#, python-format
|
||||
msgid "Connection Failed: %(error)s"
|
||||
msgstr "连接失败:%(error)s"
|
||||
|
||||
#: vrtManager/connection.py:324
|
||||
#: vrtManager/connection.py:336
|
||||
#, python-format
|
||||
msgid "Connection closed (%(reason)s)"
|
||||
msgstr "连接已关闭(%(reason)s)"
|
||||
|
||||
#: vrtManager/connection.py:476
|
||||
#: vrtManager/connection.py:488
|
||||
#, python-format
|
||||
msgid "Connection Failed: %(host)s is not connected"
|
||||
msgstr "连接失败:%(host)s 未连接"
|
||||
|
||||
#: vrtManager/connection.py:528
|
||||
#: vrtManager/connection.py:540
|
||||
#, python-format
|
||||
msgid "This host has no architecture %(arch)s"
|
||||
msgstr "此主机没有 %(arch)s 架构"
|
||||
|
||||
#: vrtManager/connection.py:1191
|
||||
#: vrtManager/connection.py:1203
|
||||
#, python-format
|
||||
msgid ""
|
||||
"Pool %(pool)s is not active: the files the disks of the VMs in it are made "
|
||||
|
|
|
|||
|
|
@ -157,6 +157,18 @@ def _count_on(connection):
|
|||
_request.held = connection
|
||||
|
||||
|
||||
@contextlib.contextmanager
|
||||
def counted_thread():
|
||||
"""A thread a web request starts to use a host counts as a request of its
|
||||
own (HOST_REQUESTS): one stuck on a host it asks holds a place there, and
|
||||
the next ones are refused, not piled up."""
|
||||
_request.active = True
|
||||
try:
|
||||
yield
|
||||
finally:
|
||||
_request_finished()
|
||||
|
||||
|
||||
@contextlib.contextmanager
|
||||
def other_hosts():
|
||||
"""For code that reads other hosts in the middle of a web request, which
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue