File size: 3,929 Bytes
06d7a7d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
from dataclasses import dataclass, asdict
from typing import Optional

@dataclass
class Event:
    time: str
    kind: str
    detail: str
    tone: str = "info"

@dataclass
class Result:
    mode: str
    scenario: str
    attempts: int
    side_effects: int
    status: str
    ledger: list
    events: list
    explanation: str

SCENARIOS = {
    "Crash after commit (response lost)": "commit_lost",
    "Worker crashes before commit": "before_commit",
    "Transient delivery failure": "transient",
}

MODES = {
    "Naive retry": "naive",
    "Idempotency key": "idempotent",
    "Transactional outbox": "outbox",
}

def simulate(mode: str, scenario: str, max_retries: int = 3) -> Result:
    mode_key = MODES.get(mode, mode)
    scenario_key = SCENARIOS.get(scenario, scenario)
    events, ledger = [], []
    attempts = side_effects = 0
    key = "order-7f2a"
    events.append(Event("T+00ms", "request", f"POST /charge · idempotency-key: {key}").__dict__)
    if mode_key == "outbox":
        events.append(Event("T+02ms", "database", "Order + outbox event committed in one transaction.", "good").__dict__)

    for attempt in range(1, max_retries + 2):
        attempts = attempt
        events.append(Event(f"T+{attempt*20:03d}ms", "worker", f"Attempt {attempt}: handling {key}.").__dict__)
        if scenario_key == "before_commit" and attempt == 1:
            events.append(Event(f"T+{attempt*20+5:03d}ms", "failure", "Worker stopped before the side effect was committed.", "bad").__dict__)
            continue
        if scenario_key == "transient" and attempt == 1:
            events.append(Event(f"T+{attempt*20+5:03d}ms", "failure", "Downstream returned a retryable 503; no effect was committed.", "bad").__dict__)
            continue

        if mode_key == "naive":
            side_effects += 1
            events.append(Event(f"T+{attempt*20+8:03d}ms", "side effect", f"Charge applied. Total applied: {side_effects}.", "good").__dict__)
        else:
            prior = next((row for row in ledger if row["key"] == key), None)
            if prior:
                events.append(Event(f"T+{attempt*20+8:03d}ms", "deduplicated", f"Key already completed; replay cached result {prior['result']}.", "good").__dict__)
            else:
                side_effects += 1
                receipt = "ch_042" if mode_key == "idempotent" else "evt_042"
                ledger.append({"key": key, "result": receipt, "status": "completed"})
                events.append(Event(f"T+{attempt*20+8:03d}ms", "side effect", f"Effect committed once; receipt {receipt}.", "good").__dict__)

        if scenario_key == "commit_lost" and attempt == 1:
            events.append(Event(f"T+{attempt*20+12:03d}ms", "failure", "Worker lost the response after commit; caller cannot tell whether it succeeded.", "bad").__dict__)
            continue
        events.append(Event(f"T+{attempt*20+15:03d}ms", "response", "200 OK · operation complete", "good").__dict__)
        break
    else:
        events.append(Event("T+final", "queue", "Retry limit reached; work remains recoverable.", "bad").__dict__)

    if scenario_key == "commit_lost":
        explanation = ("A lost response creates an ambiguous outcome. The naive retry applies the charge twice; "
                       "an idempotency ledger returns the original result, while an outbox keeps durable work queued.")
    elif scenario_key == "before_commit":
        explanation = "No effect happened before the crash. Retrying is safe here; production systems still need bounded retries and durable state."
    else:
        explanation = "A transient 503 with no committed effect is retryable. Backoff and jitter would spread load in a real worker."
    status = "Duplicate side effect" if side_effects > 1 else ("Completed once" if side_effects else "No side effect")
    return Result(mode, scenario, attempts, side_effects, status, ledger, events, explanation)