Retry guarded restoration while accepted intake drains

This commit is contained in:
Codex committed 2026-11-19 05:45:24 +00:00
1 parent 9c4ad839ac
commit 5ca0dca01f
3 files changed
+75 -4

No files matched your search

@@ -14,6 +14,7 @@ from fixtures import bundle, original_values, restore_originals, selection_files
from owned_process import cancellation_scope, run_session
from read_transport import recover_reader
from target_lease import child_context, target_lease
from target_observation import IntakeNotQuiescentError
from target_safety import InactiveChangingConsumerError, read_intent
@@ -46,6 +47,24 @@ def require_running(cancellation: dict) -> None:
raise InterruptedError('fixture session was interrupted')
def restore_when_quiescent() -> None:
# An interrupted publisher can leave already accepted traffic to commit.
# Start a fresh protected observation while the owned lease is held.
# An attempt started before the deadline keeps its own proof deadline.
deadline = time.monotonic() + 120
while True:
try:
restore_originals()
return
except IntakeNotQuiescentError:
remaining = deadline - time.monotonic()
if remaining <= 0:
raise
time.sleep(min(1, remaining))
if time.monotonic() >= deadline:
raise
def restore(evidence: Path, token: str) -> None:
# Possible cancellation is latched by the outer scope during recovery.
# The canonical controller and its fresh readiness probes finish normally.
@@ -66,7 +85,7 @@ def restore(evidence: Path, token: str) -> None:
if not on_originals and selection.get('token') != token:
raise ValueError('selected fixture changed during this session')
try:
restore_originals()
restore_when_quiescent()
except InactiveChangingConsumerError:
if on_originals:
raise
@@ -76,7 +95,7 @@ def restore(evidence: Path, token: str) -> None:
if status:
raise RuntimeError(f'canonical fixture restart before restoration failed (exit {status})')
resumed_at = utc_now()
restore_originals()
restore_when_quiescent()
if run_controller('scripts/dev-all.sh', ['start'], recovery):
raise RuntimeError('canonical original-store start failed')
if selection_values() != original_values():
@@ -19,6 +19,10 @@ QUEUES=('meshcore_mqtt_ingest_active','meshcore_mqtt_ingest_queue_depth',
SECONDS=45
class IntakeNotQuiescentError(ValueError):
"""A live owner still reports pending intake work."""
def stable_identities(snapshot):
return {name:{key:row.get(key) for key in ('container_id','started_at','pid','image_id','running','refs')}
for name,row in snapshot['consumers'].items() if row['refs']}
@@ -28,8 +32,13 @@ class IntakeProof:
def __init__(self,started,required):
self.started=started;self.required=set(required);self.first={};self.last={};self.core_times=set();self.reads=0
def add(self,observations):
def add(self,observations,*,allow_initial_pending=False):
if set(observations)!=self.required:raise ValueError('all live intake owners must report; missing is not zero')
if allow_initial_pending and self.reads == 0 and all(
type(value.get('quiescent')) is bool for value in observations.values()):
pending = next((name for name, value in observations.items() if value['quiescent'] is False), None)
if pending is not None:
raise IntakeNotQuiescentError(pending + ' intake is not quiescent')
for name,value in observations.items():
if value.get('quiescent') is not True:raise ValueError(name+' intake is not quiescent')
old=self.first.setdefault(name,value)
@@ -120,7 +129,7 @@ def observe(snapshot_reader,*,apps=read_apps,broker=broker_observation,clock=tim
required={app for app,name in APPS.items() if initial['consumers'].get(name,{}).get('running') is True}
tracker=IntakeProof(started_wall,required)
# Fail missing/unsupported app state before creating even a clean observer client.
tracker.add(apps(initial,deadline))
tracker.add(apps(initial,deadline),allow_initial_pending=True)
with ThreadPoolExecutor(max_workers=1) as worker:
pending=worker.submit(broker,deadline)
while True:
@@ -13,6 +13,7 @@ from unittest.mock import call, patch
from fixture_session import restore_lease
from owned_process import cancellation_scope, run_session
from target_lease import child_context, target_lease
from target_observation import IntakeNotQuiescentError, IntakeProof
from target_safety import InactiveChangingConsumerError
import fixture_session
import read_transport
@@ -123,6 +124,48 @@ with target_lease(path=Path(sys.argv[1])):
receipt = json.loads((evidence / 'resume-fixture.json').read_text())
self.assertEqual(receipt['fixture'], token)
def test_restore_retries_through_recent_intake_then_requires_fresh_proof(self):
pending = IntakeNotQuiescentError('ukmesh intake is not quiescent')
with patch.object(fixture_session, 'restore_originals', side_effect=[pending, None]) as restore, \
patch.object(fixture_session.time, 'monotonic', side_effect=[0, 1, 2]), \
patch.object(fixture_session.time, 'sleep') as sleep:
fixture_session.restore_when_quiescent()
self.assertEqual(restore.call_args_list, [call(), call()])
sleep.assert_called_once_with(1)
def test_restore_keeps_deadline_and_refuses_unrelated_failures(self):
for failure in (IntakeNotQuiescentError('pending'), ValueError('ownership changed')):
with self.subTest(failure=failure), \
patch.object(fixture_session, 'restore_originals', side_effect=failure) as restore, \
patch.object(fixture_session.time, 'monotonic', side_effect=[0, 120]), \
patch.object(fixture_session.time, 'sleep') as sleep:
with self.assertRaises(type(failure)) as caught:
fixture_session.restore_when_quiescent()
self.assertIs(caught.exception, failure)
restore.assert_called_once_with()
sleep.assert_not_called()
def test_restore_avoids_a_new_attempt_at_the_deadline(self):
with patch.object(fixture_session, 'restore_originals', side_effect=IntakeNotQuiescentError) as restore, \
patch.object(fixture_session.time, 'monotonic', side_effect=[0, 119.5, 120]), \
patch.object(fixture_session.time, 'sleep') as sleep:
with self.assertRaises(IntakeNotQuiescentError):
fixture_session.restore_when_quiescent()
restore.assert_called_once_with()
sleep.assert_called_once_with(.5)
def test_busy_intake_is_recoverable_only_before_proof(self):
proof = IntakeProof(0, {'ukmesh'})
with self.assertRaises(IntakeNotQuiescentError):
proof.add({'ukmesh': {'quiescent': False}}, allow_initial_pending=True)
with self.assertRaises(ValueError) as malformed:
proof.add({'ukmesh': {}}, allow_initial_pending=True)
self.assertIs(type(malformed.exception), ValueError)
proof.add({'ukmesh': {'quiescent': True, 'probe_last_success': 1}}, allow_initial_pending=True)
with self.assertRaises(ValueError) as busy:
proof.add({'ukmesh': {'quiescent': False}}, allow_initial_pending=True)
self.assertIs(type(busy.exception), ValueError)
def test_reader_recovery_uses_run_labels_and_immutable_container_id(self):
with tempfile.TemporaryDirectory() as folder:
run = Path(folder)