GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — golive: journal the measured handover delay; load-proof gap timing te… · scuttletoby/t3code@f49745f

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • fork-kit/golive
    • README.md
    • lib/golive
      • handoff.py
    • tests
      • gap.test.py
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎fork-kit/golive/README.md‎

Copy file name to clipboardExpand all lines: fork-kit/golive/README.md
+5-2Lines changed: 5 additions & 2 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -329,8 +329,11 @@ start: the new launcher then idled and verify waited for minutes.
329329
and the kit must not take that probe for the launcher (it would free the home in the middle of `t3 update`). A
330330
rule of "held on every poll for 100 ms" could not tell frequent probes from the launcher when load stretched the
331331
polls (#546). For an older build the kit lets go just before the start, as before (its server would meet the
332-
kit's lock and idle). The verify step journals how the lock was let go (`handover`: `launcher`, `timeout`,
333-
`start failed`, `before the start ...`) and any marker renewal that failed (`renewFailed`).
332+
kit's lock and idle). The kit lets go at its first look (every 20 ms) after the launcher's claim, well inside
333+
the 250 ms its server waits for the owner lock (#534). The verify step journals how the lock was let go
334+
(`handover`: `launcher`, `timeout`, `start failed`, `before the start ...`), for `launcher` the measured delay
335+
(`handoverMs`: at most this many ms from the claim to the let-go; with `handoverLate` when it reached 250 ms, so
336+
a server that idled on a very busy host is named), and any marker renewal that failed (`renewFailed`).
334337
- **Backstop, a foreign server.** An SSH client older than 0.1.10 reads neither, and a server started by hand
335338
ignores both. When the runtime record names a live process that the boot service did not start and that itself
336339
holds `userdata/server-owner.sqlite` (a stale record whose pid was reused holds nothing and is never named), for
Collapse file

‎fork-kit/golive/lib/golive/handoff.py‎

Copy file name to clipboardExpand all lines: fork-kit/golive/lib/golive/handoff.py
+24-4Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
# Seconds between two looks for the launcher. It holds the serving lock from #claimServing on, and its server then
5959
# waits 250 ms for the owner lock (serverOwnership.ts busy_timeout): the kit lets go well inside that.
6060
HANDOVER_POLL = 0.02
61+
SERVER_LOCK_WAIT_MS = 250 # serverOwnership.ts busy_timeout (contract.test.py): a later handover may leave it idle
6162
FOREIGN_GRACE = 10.0 # seconds a server not started by the boot service must stay before verify stops on it
6263
_now_ms = lambda: int(time.time() * 1000) # noqa: E731 (Date.now(), as upstream's reader compares)
6364

@@ -147,6 +148,9 @@ def __init__(self, home):
147148
# how the last starting() let go of the owner lock: 'launcher' (it held its serving lock), 'timeout' (none did
148149
# within HANDOVER_WAIT), 'start failed', 'before the start' (a build that takes no serving lock); None: none
149150
self.handover = None
151+
# the last 'launcher' handover: at most this many ms between the launcher's claim of the serving lock and the
152+
# kit letting go of the owner lock (from the start of the kit's last look that did not find it serving)
153+
self.handover_ms = None
150154
self._stop = threading.Event()
151155
self._thread = None
152156
self._mutex = threading.Lock()
@@ -184,8 +188,10 @@ def let_go(self):
184188
def starting(self, fn, version, service):
185189
"""Run fn (it starts T3 `version`: a service start, or `t3 update`), and let go of the owner lock only once
186190
the new launcher holds its serving lock (or HANDOVER_WAIT after fn returned, or fn failed). service: reads
187-
the boot service (adapter.service). Returns fn's result. Afterwards the kit holds no lock on the home."""
188-
self.handover = None
191+
the boot service (adapter.service). Returns fn's result. Afterwards the kit holds no lock on the home.
192+
A handover to the launcher measures its delay (handover_ms, an upper bound: the claim came after the start of
193+
the last look that did not find the launcher serving, and the lock was let go before the clock is read)."""
194+
self.handover, self.handover_ms = None, None
189195
if self.lock is not None and not takes_serving_lock(version):
190196
self.let_go()
191197
self.handover = 'before the start (a build that takes no serving lock)'
@@ -196,16 +202,21 @@ def starting(self, fn, version, service):
196202
serving = os.path.join(self.home, 'runtime', SERVING_LOCK)
197203

198204
def watch(): # HANDOVER_WAIT counts from fn's return (`t3 update` installs first, then restarts T3)
205+
looked = time.monotonic() # the start of the last look that did not find the launcher serving
199206
try:
200207
while not done.is_set():
208+
began = time.monotonic()
201209
if lock_holders(serving): # someone holds it: the launcher, or an SSH launch's probe
202210
try:
203211
svc = service()
204212
except Exception: # noqa: BLE001 (systemctl failed for a moment: look again)
205213
svc = {}
206214
if launcher_serving(self.home, svc):
207215
outcome['how'] = 'launcher'
216+
self.let_go()
217+
outcome['ms'] = (time.monotonic() - looked) * 1000
208218
return
219+
looked = began
209220
if returned and time.monotonic() >= returned[0] + HANDOVER_WAIT:
210221
outcome['how'] = 'timeout'
211222
return
@@ -225,6 +236,8 @@ def watch(): # HANDOVER_WAIT counts from fn's return (`t3 update` installs firs
225236
finally:
226237
watcher.join()
227238
self.handover = outcome.get('how', 'start failed')
239+
if 'ms' in outcome:
240+
self.handover_ms = round(outcome['ms'])
228241

229242
def close(self):
230243
"""Stop renewing, let go of any lock. The marker stays to run out by itself (within 60 s)."""
@@ -238,14 +251,21 @@ def close(self):
238251

239252
def served(self):
240253
"""The new server is verified: stop renewing and remove the marker if it is still the kit's. Returns the
241-
facts for the verify step: 'marker' (what was done to it), 'handover' (how the owner lock was let go), and
242-
'renewFailed' when a renewal failed (the marker may have run out mid-stop; the owner lock still held).
254+
facts for the verify step: 'marker' (what was done to it), 'handover' (how the owner lock was let go),
255+
'handoverMs' (handover_ms; with 'handoverLate' when it reached the server's 250 ms wait), and 'renewFailed'
256+
when a renewal failed (the marker may have run out mid-stop; the owner lock still held).
243257
A newer marker renamed in between the read and the unlink would still be removed: a window of microseconds,
244258
the same as upstream's own DesktopBackendPool.releaseUpdateMarker."""
245259
self.close()
246260
facts = {'marker': self._remove()}
247261
if self.handover is not None:
248262
facts['handover'] = self.handover
263+
if self.handover_ms is not None: # the journal keeps how close the handover came to the server's wait
264+
facts['handoverMs'] = self.handover_ms
265+
if self.handover_ms >= SERVER_LOCK_WAIT_MS:
266+
facts['handoverLate'] = (f'the kit let go up to {self.handover_ms} ms after the launcher took its '
267+
f'serving lock; its server waits {SERVER_LOCK_WAIT_MS} ms for the owner lock '
268+
'and may have idled (the home is then free until its launcher tries again)')
249269
if self.failed:
250270
facts['renewFailed'] = f'{self.failed} time(s), last: {self.error}'
251271
return facts
Collapse file

‎fork-kit/golive/tests/gap.test.py‎

Copy file name to clipboardExpand all lines: fork-kit/golive/tests/gap.test.py
+88-7Lines changed: 88 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,23 @@ def home_owned(home, hold=0.0):
125125
PROBING_LIMIT = 300 # seconds: a probing phase that has not ended by then fails its test instead of hanging it
126126

127127

128+
def kit_looks_for_the_launcher(test):
129+
"""[(monotonic time a look began, whether it found the launcher)]: each of the kit's handover looks that found
130+
the serving lock held (handoff.launcher_serving), recorded as it ran."""
131+
from golive import handoff
132+
looks, real = [], handoff.launcher_serving
133+
134+
def spy(home, svc):
135+
began = time.monotonic()
136+
found = real(home, svc)
137+
looks.append((began, found))
138+
return found
139+
patcher = mock.patch.object(handoff, 'launcher_serving', spy)
140+
patcher.start()
141+
test.addCleanup(patcher.stop)
142+
return looks
143+
144+
128145
def kit_meets_probes(test):
129146
"""An Event set once the kit's handover poll has found the serving lock held by another process than this
130147
one (an SSH probe: this process is the fake launcher), so a probing phase surely tested what it is about."""
@@ -681,7 +698,8 @@ def test_a_probe_loop_on_the_serving_lock_is_not_the_launcher(self):
681698
g = self.h.Gap(self.m.home)
682699
g.adopt(coldcopy.hold_owner_lock(userdata, 0))
683700
met = kit_meets_probes(self)
684-
stop, probing, probes, free = threading.Event(), threading.Event(), [], []
701+
looks = kit_looks_for_the_launcher(self)
702+
stop, probing, probes, free, claimed = threading.Event(), threading.Event(), [], [], []
685703

686704
def ssh_probes():
687705
try:
@@ -701,21 +719,84 @@ def start(): # `t3 update`: it installs while launches probe, then its launcher
701719
t.join()
702720
launcher = sqlite3.connect(serving, isolation_level=None, timeout=1)
703721
launcher.execute('BEGIN EXCLUSIVE')
704-
claimed = time.monotonic()
705-
for _ in range(300):
706-
if not held(owner):
707-
break
722+
claimed.append(time.monotonic())
723+
end = claimed[0] + PROBING_LIMIT
724+
while held(owner) and time.monotonic() < end:
708725
time.sleep(0.005)
709-
free.append(time.monotonic() - claimed)
726+
free.append(not held(owner))
710727
launcher.close()
711728
t = threading.Thread(target=ssh_probes, daemon=True)
712729
t.start()
713730
g.starting(start, NEW, self.service)
714731
self.assertEqual(len(free), 1, 'the kit let go of the owner lock while only SSH probes held the serving lock')
732+
self.assertTrue(free[0], 'the kit kept the owner lock after the launcher claimed the serving lock')
715733
self.assertGreaterEqual(len(probes), PROBES)
716734
self.assertTrue(met.is_set(), 'the kit never met an SSH probe of the serving lock')
717-
self.assertLess(free[0], 0.25) # within its server's 250 ms wait for the owner lock
735+
# #534: let go at the kit's first look after the claim, counted in looks, not in wall-clock time (a slow
736+
# runner stretches both alike): every look that began after the claim found the launcher, and there was one
737+
self.assertEqual([found for at, found in looks if at >= claimed[0]], [True])
718738
self.assertEqual(g.handover, 'launcher')
739+
self.assertIsInstance(g.handover_ms, int)
740+
741+
def test_the_handover_delay_is_measured_and_journalled(self):
742+
"""#534: the verify step journals how long the kit kept the owner lock after the launcher's claim (an upper
743+
bound), and names a handover that reached the 250 ms its server waits for the owner lock."""
744+
userdata = self.m.path('home/userdata')
745+
os.makedirs(userdata)
746+
serving = self.m.path('home/runtime/service-serving.lock')
747+
owner = os.path.join(userdata, 'server-owner.sqlite')
748+
claims = []
749+
750+
def launcher(): # claims the serving lock and keeps it until the kit let go of the owner lock
751+
c = sqlite3.connect(serving, isolation_level=None, check_same_thread=False)
752+
c.execute('BEGIN EXCLUSIVE')
753+
claims.append(c)
754+
# (seconds a look that found the launcher takes, the server's wait in ms): the lateness is fixed by the wait,
755+
# never by how fast this runner is: 0.3 s is surely past 250 ms, and no runner reaches a wait of 10**9 ms
756+
for slow, wait, late in ((0.0, 10**9, False), (0.3, self.h.SERVER_LOCK_WAIT_MS, True)):
757+
real = self.h.launcher_serving
758+
759+
def seen(home, svc, slow=slow, real=real): # a look stretched by load, after the claim
760+
found = real(home, svc)
761+
if found:
762+
time.sleep(slow)
763+
return found
764+
g = self.h.open_gap(self.m.home)
765+
g.adopt(coldcopy.hold_owner_lock(userdata, 0))
766+
with mock.patch.object(self.h, 'launcher_serving', seen), \
767+
mock.patch.object(self.h, 'SERVER_LOCK_WAIT_MS', wait):
768+
g.starting(launcher, NEW, self.service)
769+
self.assertFalse(held(owner))
770+
facts = self.h.served(self.m.home)
771+
self.assertEqual(facts['handover'], 'launcher')
772+
self.assertIsInstance(facts['handoverMs'], int)
773+
self.assertGreaterEqual(facts['handoverMs'], slow * 1000) # an upper bound: it covers the slow look
774+
self.assertEqual('handoverLate' in facts, late, facts)
775+
if late:
776+
self.assertIn(f'up to {facts["handoverMs"]} ms after the launcher took its serving lock',
777+
facts['handoverLate'])
778+
claims.pop().close()
779+
g = self.h.open_gap(self.m.home) # no launcher: nothing to measure
780+
g.adopt(coldcopy.hold_owner_lock(userdata, 0))
781+
with mock.patch.object(self.h, 'HANDOVER_WAIT', 0.05):
782+
g.starting(lambda: None, NEW, self.service)
783+
self.assertIsNone(g.handover_ms) # a run again: the last handover's delay is not carried over
784+
facts = self.h.served(self.m.home)
785+
self.assertEqual(facts['handover'], 'timeout')
786+
self.assertNotIn('handoverMs', facts)
787+
g = self.h.open_gap(self.m.home) # a start that failed half-way: nothing to measure
788+
g.adopt(coldcopy.hold_owner_lock(userdata, 0))
789+
g.handover_ms = 7 # left by an earlier start of this gap
790+
791+
def fails():
792+
raise OSError('the start failed')
793+
with self.assertRaises(OSError):
794+
g.starting(fails, NEW, self.service)
795+
self.assertFalse(held(owner))
796+
facts = self.h.served(self.m.home)
797+
self.assertEqual(facts['handover'], 'start failed')
798+
self.assertNotIn('handoverMs', facts)
799+
self.assertEqual(self.h.served(self.m.home), {'marker': None}) # no gap open: nothing measured
719800

720801
def test_only_the_service_s_main_process_holding_the_serving_lock_is_the_launcher(self):
721802
"""However long another process holds the serving lock, the kit keeps the owner lock; it lets go at once

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.