mirror of
https://github.com/retspen/webvirtcloud
synced 2026-12-03 17:36:05 +00:00
fix(computes): the Computes page reads its hosts side by side, within one wait
The page connected to its hosts one after another: each host that did not answer added its connect timeout, so a few such hosts made the page take half a minute, and a host that hung while answering held it until the worker was killed. The hosts are now read on their own threads, and a host not read within ten seconds is shown as not answering.
This commit is contained in:
parent
eb3bc69754
commit
e264104e85
6 changed files with 114 additions and 15 deletions
45
computes/tests/test_list_hosts.py
Normal file
45
computes/tests/test_list_hosts.py
Normal file
|
|
@ -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"])
|
||||
|
|
@ -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"]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue