forked from mtusi2026/mtusi2026lab7
Ограничить повторные уведомления и проверять восстановление в заданный срок
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -0,0 +1,26 @@
|
|||||||
|
{
|
||||||
|
"date": "2026-11-22T19:06:20.779410+00:00",
|
||||||
|
"runtime_source": "b9b6ece535757ea2138a638b56fc9e58c0cd7c2b",
|
||||||
|
"scope": "Read-only follow-up on retained failed PKI study, not completed HTTP/S3 preservation verification",
|
||||||
|
"accepted_uploads": 149,
|
||||||
|
"observed": {
|
||||||
|
"states": {
|
||||||
|
"queued": 15,
|
||||||
|
"ready": 134
|
||||||
|
},
|
||||||
|
"outbox_rows": 2439,
|
||||||
|
"variant_rows": 536,
|
||||||
|
"publications": 134,
|
||||||
|
"events": {
|
||||||
|
"effect_committed": 134,
|
||||||
|
"worker_resource_limits": 151,
|
||||||
|
"published": 2438,
|
||||||
|
"duplicate_decision": 889,
|
||||||
|
"child_finished": 158,
|
||||||
|
"claimed": 167,
|
||||||
|
"retry": 33
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"all_accepted_ready_in_database": false,
|
||||||
|
"original_bytes_not_checked_here": true
|
||||||
|
}
|
||||||
@@ -31,6 +31,36 @@ def images():
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def observe_get(client, path, *, deadline, observations, kind, **kwargs):
|
||||||
|
"""Observe recovery without mistaking a transient read failure for loss.
|
||||||
|
|
||||||
|
Every error remains counted. Authentication/ACL/contract failures are fatal,
|
||||||
|
and an unavailable object never becomes a successful observation by retrying.
|
||||||
|
The caller still has to prove every acknowledgement and ready attachment.
|
||||||
|
"""
|
||||||
|
counts = observations.setdefault(kind, {})
|
||||||
|
while (remaining := deadline - time.monotonic()) > 0:
|
||||||
|
try:
|
||||||
|
response = client.get(path, timeout=min(8, remaining), **kwargs)
|
||||||
|
except httpx.TransportError as error:
|
||||||
|
key = "transport:" + type(error).__name__
|
||||||
|
else:
|
||||||
|
key = str(response.status_code)
|
||||||
|
counts[key] = counts.get(key, 0) + 1
|
||||||
|
if response.status_code == 200:
|
||||||
|
return response
|
||||||
|
if response.status_code not in {429, 502, 503, 504}:
|
||||||
|
raise AssertionError(
|
||||||
|
f"Unexpected {kind} observation status {response.status_code}"
|
||||||
|
)
|
||||||
|
# No tokens, response bodies or private request headers in evidence.
|
||||||
|
time.sleep(min(0.25, max(0, deadline - time.monotonic())))
|
||||||
|
continue
|
||||||
|
counts[key] = counts.get(key, 0) + 1
|
||||||
|
time.sleep(min(0.25, max(0, deadline - time.monotonic())))
|
||||||
|
raise TimeoutError(f"Recovery observation deadline exceeded: {kind}")
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
parser = argparse.ArgumentParser(description=__doc__)
|
parser = argparse.ArgumentParser(description=__doc__)
|
||||||
parser.add_argument("--output", default="evidence/pki-research-first.json")
|
parser.add_argument("--output", default="evidence/pki-research-first.json")
|
||||||
@@ -302,16 +332,21 @@ def main():
|
|||||||
# counted separately and never presented as acknowledged writes.
|
# counted separately and never presented as acknowledged writes.
|
||||||
messages = {row[5] for row in load.requests if row[2] == 201}
|
messages = {row[5] for row in load.requests if row[2] == 201}
|
||||||
found, cursor = set(), None
|
found, cursor = set(), None
|
||||||
|
observations = report["preservation_observations"] = {}
|
||||||
|
message_deadline = time.monotonic() + 180
|
||||||
while True:
|
while True:
|
||||||
r = client.get(
|
r = observe_get(
|
||||||
|
client,
|
||||||
f"/api/v1/chats/{chat}/messages",
|
f"/api/v1/chats/{chat}/messages",
|
||||||
|
deadline=message_deadline,
|
||||||
|
observations=observations,
|
||||||
|
kind="message_pages",
|
||||||
headers=headers,
|
headers=headers,
|
||||||
params={
|
params={
|
||||||
"limit": 100,
|
"limit": 100,
|
||||||
**({"cursor": cursor} if cursor else {}),
|
**({"cursor": cursor} if cursor else {}),
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
assert r.status_code == 200
|
|
||||||
page = r.json()
|
page = r.json()
|
||||||
found.update(item["id"] for item in page["items"])
|
found.update(item["id"] for item in page["items"])
|
||||||
cursor = page["next_cursor"]
|
cursor = page["next_cursor"]
|
||||||
@@ -333,10 +368,14 @@ def main():
|
|||||||
failed = set()
|
failed = set()
|
||||||
while pending and time.monotonic() < deadline:
|
while pending and time.monotonic() < deadline:
|
||||||
for identity in list(pending):
|
for identity in list(pending):
|
||||||
r = client.get(
|
r = observe_get(
|
||||||
"/api/v1/attachments/" + identity, headers=headers
|
client,
|
||||||
|
"/api/v1/attachments/" + identity,
|
||||||
|
deadline=deadline,
|
||||||
|
observations=observations,
|
||||||
|
kind="attachment_metadata",
|
||||||
|
headers=headers,
|
||||||
)
|
)
|
||||||
assert r.status_code == 200
|
|
||||||
state = r.json()["status"]
|
state = r.json()["status"]
|
||||||
if state == "ready":
|
if state == "ready":
|
||||||
pending.remove(identity)
|
pending.remove(identity)
|
||||||
@@ -365,15 +404,22 @@ def main():
|
|||||||
"PKI research recovery after controlled seal",
|
"PKI research recovery after controlled seal",
|
||||||
)
|
)
|
||||||
failed.clear()
|
failed.clear()
|
||||||
|
report["drain"]["remaining"] = len(pending)
|
||||||
|
save()
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
report["drain"].update(
|
report["drain"].update(
|
||||||
elapsed_seconds=round(time.monotonic() - drain_started, 3),
|
elapsed_seconds=round(time.monotonic() - drain_started, 3),
|
||||||
remaining=len(pending),
|
remaining=len(pending),
|
||||||
)
|
)
|
||||||
assert not pending, "Accepted upload not ready after recovery"
|
assert not pending, "Accepted upload not ready after recovery"
|
||||||
|
content_deadline = time.monotonic() + 120
|
||||||
for row in accepted[:: max(1, len(accepted) // 5)]:
|
for row in accepted[:: max(1, len(accepted) // 5)]:
|
||||||
r = client.get(
|
r = observe_get(
|
||||||
|
client,
|
||||||
"/api/v1/attachments/" + row[3] + "/content",
|
"/api/v1/attachments/" + row[3] + "/content",
|
||||||
|
deadline=content_deadline,
|
||||||
|
observations=observations,
|
||||||
|
kind="sampled_originals",
|
||||||
headers=headers,
|
headers=headers,
|
||||||
)
|
)
|
||||||
assert r.status_code == 200 and r.content == load.raw
|
assert r.status_code == 200 and r.content == load.raw
|
||||||
|
|||||||
+8
-2
@@ -210,9 +210,15 @@ def watchdog(db):
|
|||||||
).fetchall()
|
).fetchall()
|
||||||
for row in rows:
|
for row in rows:
|
||||||
retry(conn, row, "lease_expired")
|
retry(conn, row, "lease_expired")
|
||||||
# Recreate a wakeup if its stream entry was lost: never recreate logical jobs.
|
# This is a safety recheck for a lost wakeup, not a delivery retry.
|
||||||
|
# A confirmed durable stream can legitimately have a backlog while the
|
||||||
|
# worker is recovering. Republishing every 15s amplified that backlog:
|
||||||
|
# 149 accepted uploads produced 2204 outbox rows in the PKI experiment.
|
||||||
|
# Failed attempts and expired leases still enqueue their retry at once.
|
||||||
|
recheck = integer("JOB_WAKEUP_RECHECK_SECONDS", 120, 30, 300)
|
||||||
rows = conn.execute(
|
rows = conn.execute(
|
||||||
"SELECT j.* FROM jobs j WHERE j.state='queued' AND j.next_at<=clock_timestamp() AND NOT EXISTS(SELECT 1 FROM outbox o WHERE o.job_id=j.id AND (o.published_at IS NULL OR o.published_at>clock_timestamp()-interval '15 seconds')) FOR UPDATE OF j SKIP LOCKED LIMIT 16"
|
"SELECT j.* FROM jobs j WHERE j.state='queued' AND j.next_at<=clock_timestamp() AND NOT EXISTS(SELECT 1 FROM outbox o WHERE o.job_id=j.id AND (o.published_at IS NULL OR o.published_at>clock_timestamp()-(%s*interval '1 second'))) ORDER BY j.next_at,j.id FOR UPDATE OF j SKIP LOCKED LIMIT 16",
|
||||||
|
(recheck,),
|
||||||
).fetchall()
|
).fetchall()
|
||||||
for row in rows:
|
for row in rows:
|
||||||
enqueue(conn, row["id"])
|
enqueue(conn, row["id"])
|
||||||
|
|||||||
@@ -0,0 +1,90 @@
|
|||||||
|
import json
|
||||||
|
import time
|
||||||
|
|
||||||
|
import httpx
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from scripts.research_security import observe_get
|
||||||
|
|
||||||
|
|
||||||
|
def test_transient_reads_remain_observed_until_an_actual_200(monkeypatch):
|
||||||
|
observations = {}
|
||||||
|
attempts = 0
|
||||||
|
|
||||||
|
def handle(request):
|
||||||
|
nonlocal attempts
|
||||||
|
attempts += 1
|
||||||
|
if attempts == 1:
|
||||||
|
return httpx.Response(503, request=request)
|
||||||
|
if attempts == 2:
|
||||||
|
raise httpx.ReadTimeout(
|
||||||
|
"synthetic private diagnostic", request=request
|
||||||
|
)
|
||||||
|
return httpx.Response(200, json={"status": "ready"}, request=request)
|
||||||
|
|
||||||
|
monkeypatch.setattr("scripts.research_security.time.sleep", lambda _: None)
|
||||||
|
with httpx.Client(transport=httpx.MockTransport(handle)) as client:
|
||||||
|
result = observe_get(
|
||||||
|
client,
|
||||||
|
"https://synthetic/attachment",
|
||||||
|
deadline=time.monotonic() + 3,
|
||||||
|
observations=observations,
|
||||||
|
kind="metadata",
|
||||||
|
headers={"Authorization": "Bearer synthetic-private-token"},
|
||||||
|
)
|
||||||
|
assert result.json()["status"] == "ready"
|
||||||
|
assert attempts == 3
|
||||||
|
assert observations == {
|
||||||
|
"metadata": {"503": 1, "transport:ReadTimeout": 1, "200": 1}
|
||||||
|
}
|
||||||
|
assert "private" not in json.dumps(observations)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("status", [401, 403, 404, 500])
|
||||||
|
def test_auth_acl_missing_objects_and_unexpected_errors_are_fatal(status):
|
||||||
|
observations = {}
|
||||||
|
attempts = 0
|
||||||
|
|
||||||
|
def handle(request):
|
||||||
|
nonlocal attempts
|
||||||
|
attempts += 1
|
||||||
|
return httpx.Response(status, request=request)
|
||||||
|
|
||||||
|
with httpx.Client(transport=httpx.MockTransport(handle)) as client:
|
||||||
|
with pytest.raises(AssertionError, match=str(status)):
|
||||||
|
observe_get(
|
||||||
|
client,
|
||||||
|
"https://synthetic/attachment",
|
||||||
|
deadline=time.monotonic() + 3,
|
||||||
|
observations=observations,
|
||||||
|
kind="metadata",
|
||||||
|
)
|
||||||
|
assert attempts == 1
|
||||||
|
assert observations == {"metadata": {str(status): 1}}
|
||||||
|
|
||||||
|
|
||||||
|
def test_retries_cannot_turn_permanent_unavailability_into_success(
|
||||||
|
monkeypatch,
|
||||||
|
):
|
||||||
|
clock = [0.0]
|
||||||
|
monkeypatch.setattr(
|
||||||
|
"scripts.research_security.time.monotonic", lambda: clock[0]
|
||||||
|
)
|
||||||
|
monkeypatch.setattr(
|
||||||
|
"scripts.research_security.time.sleep",
|
||||||
|
lambda seconds: clock.__setitem__(0, clock[0] + seconds),
|
||||||
|
)
|
||||||
|
observations = {}
|
||||||
|
with httpx.Client(
|
||||||
|
transport=httpx.MockTransport(lambda r: httpx.Response(503, request=r))
|
||||||
|
) as client:
|
||||||
|
with pytest.raises(TimeoutError, match="deadline exceeded"):
|
||||||
|
observe_get(
|
||||||
|
client,
|
||||||
|
"https://synthetic/attachment",
|
||||||
|
deadline=1,
|
||||||
|
observations=observations,
|
||||||
|
kind="metadata",
|
||||||
|
)
|
||||||
|
assert clock[0] == 1
|
||||||
|
assert observations == {"metadata": {"503": 4}}
|
||||||
Reference in New Issue
Block a user