|
1 | 1 | """ |
2 | | -Prints each SDK route's attempts from attempts.jsonl with the gap since the previous attempt, and |
3 | | -the gap the SDK should have waited. Usage: report.py <attempts.jsonl> [route regex] |
| 2 | +Prints each SDK operation's attempts from attempts.jsonl with the gap since the previous attempt, |
| 3 | +and the gap the SDK should have waited. Usage: report.py <attempts.jsonl> [route regex] |
4 | 4 |
|
5 | | -Expected gaps model OperationRepo's per-operation backoff: |
6 | | - - After the Nth retryable failure an operation waits N * 15s (opRepoDefaultFailRetryBackoff), |
7 | | - capped at 1h. There is no jitter and HttpClient does not re-send on its own. |
| 5 | +Expected gaps model OperationRepo's backoff: |
| 6 | + - After the Nth retryable failure a request waits 5s * 2^(N-1), capped at 300s, times a jitter |
| 7 | + of 0.5 to 1. HttpClient does not re-send on its own. |
8 | 8 | - Retry-After is the minimum wait, capped at 1h. An unparsable value, or a 429 without one, |
9 | | - counts as 60s. |
10 | | - - The queue may wait up to opRepoExecutionInterval (5s) more if something else was enqueued |
11 | | - during the backoff, so a few seconds of slack is allowed. |
12 | | -A foreground or network return ends the backoff early (but not Retry-After), so short gaps right |
13 | | -after those are fine. |
| 9 | + counts as 60s. HttpClient also holds every SDK request until it passes, which is checked |
| 10 | + separately at the end. |
| 11 | + - A timed retry skips the queue's batching wait, but an enqueue near the end of a backoff can add |
| 12 | + up to opRepoExecutionInterval (5s), so that much slack is allowed. |
| 13 | +A foreground, or a network return while in the foreground, ends the backoff early (but not |
| 14 | +Retry-After), so short gaps right after those are fine. |
14 | 15 | """ |
15 | 16 |
|
16 | 17 | import json |
| 18 | +import math |
17 | 19 | import re |
18 | 20 | import sys |
19 | 21 | from collections import OrderedDict |
20 | 22 |
|
21 | | -BACKOFF_STEP = 15 |
22 | | -MAX_DELAY = 3600 |
| 23 | +BASE_BACKOFF = 5 |
| 24 | +MAX_BACKOFF = 300 |
| 25 | +MIN_JITTER = 0.5 |
| 26 | +MAX_RETRY_AFTER = 3600 |
23 | 27 | RETRY_AFTER_PARSE_FAIL = 60 |
24 | | -QUEUE_SLACK = 6.0 |
| 28 | +QUEUE_SLACK = 5.0 |
25 | 29 | CLOCK_SLACK = 1.0 |
26 | 30 | NOT_RETRIED = (400, 401, 402, 403, 404, 409, 410) |
27 | 31 | EXECUTOR_ROUTE = re.compile(r"^apps/[^/]+/(users(/by/.*)?|subscriptions/.+|custom_events)$") |
28 | 32 | IGNORED_ROUTE = re.compile(r"/iams$") |
29 | | -ID_SEGMENT = re.compile(r"(by/[^/]+/|subscriptions/|identity/)[^/]+") |
| 33 | +# Routes without a record id in the path. Their attempts are told apart by body instead. |
| 34 | +NO_ID_ROUTE = re.compile(r"(/users|/subscriptions|/custom_events)$") |
30 | 35 |
|
31 | 36 |
|
32 | | -def route_key(entry): |
33 | | - path = ID_SEGMENT.sub(r"\1:id", entry["path"]) |
34 | | - return f"{entry['method']} {path}" |
| 37 | +def operation_key(entry): |
| 38 | + key = f"{entry['method']} {entry['path']}" |
| 39 | + if entry["method"] == "POST" and NO_ID_ROUTE.search(entry["path"]): |
| 40 | + key += f" body={entry.get('body', '')}" |
| 41 | + return key |
35 | 42 |
|
36 | 43 |
|
37 | | -def backoff_window(failures, status, retry_after): |
38 | | - low = min(failures * BACKOFF_STEP, MAX_DELAY) |
39 | | - if retry_after is None and status == 429: |
40 | | - retry_after = RETRY_AFTER_PARSE_FAIL |
41 | | - has_floor = retry_after is not None |
42 | | - if has_floor: |
43 | | - try: |
44 | | - floor = min(max(float(retry_after), 0), MAX_DELAY) |
45 | | - except ValueError: |
46 | | - floor = RETRY_AFTER_PARSE_FAIL |
47 | | - low = max(low, floor) |
48 | | - return low, low + QUEUE_SLACK, has_floor |
| 44 | +def retry_after_seconds(status, header): |
| 45 | + """Mirrors HttpClient.retryAfterFromResponse.""" |
| 46 | + if header is None: |
| 47 | + return RETRY_AFTER_PARSE_FAIL if status == 429 else None |
| 48 | + try: |
| 49 | + value = float(str(header).strip()) |
| 50 | + except ValueError: |
| 51 | + return RETRY_AFTER_PARSE_FAIL |
| 52 | + if not math.isfinite(value) or value < 0: |
| 53 | + return RETRY_AFTER_PARSE_FAIL |
| 54 | + return math.ceil(min(value, MAX_RETRY_AFTER)) |
| 55 | + |
| 56 | + |
| 57 | +def backoff_window(failures, retry_after): |
| 58 | + backoff = min(BASE_BACKOFF * 2 ** (failures - 1), MAX_BACKOFF) |
| 59 | + low, high = backoff * MIN_JITTER, backoff |
| 60 | + if retry_after is not None: |
| 61 | + low, high = max(low, retry_after), max(high, retry_after) |
| 62 | + return low, high + QUEUE_SLACK |
| 63 | + |
| 64 | + |
| 65 | +def print_operation(key, entries): |
| 66 | + print(f"\n{key}") |
| 67 | + print(f" {'#':>3} {'time':>8} {'status':>6} {'body':>8} {'gap':>8} {'expected':>16} note") |
| 68 | + failures = 0 |
| 69 | + expected = None |
| 70 | + start = entries[0]["t"] |
| 71 | + for i, entry in enumerate(entries): |
| 72 | + gap = entry["t"] - entries[i - 1]["t"] if i else None |
| 73 | + note = "" |
| 74 | + if expected and gap is not None: |
| 75 | + low, high, kind = expected |
| 76 | + if gap < low - CLOCK_SLACK: |
| 77 | + note = f"EARLY ({kind})" |
| 78 | + if not kind.endswith("Retry-After"): |
| 79 | + note += "; fine if right after a foreground or network return" |
| 80 | + elif gap > high + CLOCK_SLACK: |
| 81 | + note = f"LATE ({kind})" |
| 82 | + else: |
| 83 | + note = kind |
| 84 | + elif gap is not None: |
| 85 | + note = "new attempt" |
| 86 | + expected_text = f"{expected[0]:.1f}-{expected[1]:.1f}s" if expected else "" |
| 87 | + gap_text = f"{gap:.1f}s" if gap is not None else "" |
| 88 | + mark = "*" if entry["injected"] else " " |
| 89 | + print(f" {i + 1:>3} {entry['t'] - start:>7.1f}s {entry['status']:>5}{mark} {entry.get('body', ''):>8} {gap_text:>8} {expected_text:>16} {note}") |
| 90 | + |
| 91 | + status = entry["status"] |
| 92 | + if 200 <= status < 300 or status in NOT_RETRIED: |
| 93 | + failures = 0 |
| 94 | + expected = None |
| 95 | + else: |
| 96 | + failures += 1 |
| 97 | + retry_after = retry_after_seconds(status, entry.get("retry_after")) |
| 98 | + low, high = backoff_window(failures, retry_after) |
| 99 | + expected = (low, high, f"backoff #{failures}" + (", Retry-After" if retry_after is not None else "")) |
| 100 | + |
| 101 | + |
| 102 | +def print_gate_violations(entries): |
| 103 | + """HttpClient holds every SDK request, on any route, until a Retry-After passes.""" |
| 104 | + windows = [] |
| 105 | + for entry in entries: |
| 106 | + retry_after = retry_after_seconds(entry["status"], entry.get("retry_after")) |
| 107 | + if retry_after: |
| 108 | + windows.append((entry["t"], entry["t"] + retry_after)) |
| 109 | + violations = [ |
| 110 | + entry |
| 111 | + for entry in entries |
| 112 | + if any(start < entry["t"] < end - CLOCK_SLACK for start, end in windows) |
| 113 | + ] |
| 114 | + if not windows: |
| 115 | + return |
| 116 | + if not violations: |
| 117 | + print("\nNo SDK request was sent during a Retry-After.") |
| 118 | + return |
| 119 | + print("\nSENT DURING A RETRY-AFTER (HttpClient should have held these):") |
| 120 | + for entry in violations: |
| 121 | + print(f" {entry['method']} {entry['path']} status={entry['status']} body={entry.get('body', '')}") |
49 | 122 |
|
50 | 123 |
|
51 | 124 | def main(): |
52 | 125 | path = sys.argv[1] |
53 | 126 | route_filter = re.compile(sys.argv[2]) if len(sys.argv) > 2 else None |
54 | | - groups = OrderedDict() |
| 127 | + entries = [] |
55 | 128 | with open(path) as f: |
56 | 129 | for line in f: |
57 | 130 | if line.strip(): |
58 | | - entry = json.loads(line) |
59 | | - groups.setdefault(route_key(entry), []).append(entry) |
| 131 | + entries.append(json.loads(line)) |
60 | 132 |
|
61 | | - if not groups: |
| 133 | + if not entries: |
62 | 134 | print("No SDK requests recorded yet.") |
63 | 135 | return |
64 | 136 |
|
65 | | - for key, entries in groups.items(): |
66 | | - if route_filter and not route_filter.search(key): |
67 | | - continue |
68 | | - if not EXECUTOR_ROUTE.search(entries[0]["path"]) or IGNORED_ROUTE.search(entries[0]["path"]): |
69 | | - continue |
70 | | - print(f"\n{key}") |
71 | | - print(f" {'#':>3} {'time':>8} {'status':>6} {'body':>8} {'gap':>8} {'expected':>16} note") |
72 | | - failures = 0 |
73 | | - expected = None |
74 | | - start = entries[0]["t"] |
75 | | - for i, entry in enumerate(entries): |
76 | | - gap = entry["t"] - entries[i - 1]["t"] if i else None |
77 | | - note = "" |
78 | | - if expected and gap is not None: |
79 | | - low, high, kind = expected |
80 | | - if gap < low - CLOCK_SLACK: |
81 | | - note = f"EARLY ({kind})" |
82 | | - if not kind.endswith("Retry-After"): |
83 | | - note += "; fine if right after a foreground or network return" |
84 | | - elif gap > high + CLOCK_SLACK: |
85 | | - note = f"LATE ({kind})" |
86 | | - else: |
87 | | - note = kind |
88 | | - elif gap is not None: |
89 | | - note = "new attempt" |
90 | | - expected_text = f"{expected[0]:.1f}-{expected[1]:.1f}s" if expected else "" |
91 | | - gap_text = f"{gap:.1f}s" if gap is not None else "" |
92 | | - mark = "*" if entry["injected"] else " " |
93 | | - print(f" {i + 1:>3} {entry['t'] - start:>7.1f}s {entry['status']:>5}{mark} {entry.get('body', ''):>8} {gap_text:>8} {expected_text:>16} {note}") |
94 | | - |
95 | | - status = entry["status"] |
96 | | - if 200 <= status < 300 or status in NOT_RETRIED: |
97 | | - failures = 0 |
98 | | - expected = None |
99 | | - else: |
100 | | - failures += 1 |
101 | | - low, high, has_floor = backoff_window(failures, status, entry.get("retry_after")) |
102 | | - expected = (low, high, f"backoff #{failures}" + (", Retry-After" if has_floor else "")) |
| 137 | + operations = OrderedDict() |
| 138 | + for entry in entries: |
| 139 | + if EXECUTOR_ROUTE.search(entry["path"]) and not IGNORED_ROUTE.search(entry["path"]): |
| 140 | + operations.setdefault(operation_key(entry), []).append(entry) |
| 141 | + |
| 142 | + for key, op_entries in operations.items(): |
| 143 | + if not route_filter or route_filter.search(key): |
| 144 | + print_operation(key, op_entries) |
| 145 | + print_gate_violations(entries) |
103 | 146 | print("\n* = failure injected by the proxy") |
104 | 147 |
|
105 | 148 |
|
|
0 commit comments