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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
//! disk persistence matching indigo's diskpersist format
//!
//! append-only log files with relay-assigned sequence numbers.
//! Postgres metadata index for fast cursor→file lookup.
//!
//! on-disk entry format (28-byte LE header + CBOR payload):
//! [4B flags LE] [4B kind LE] [4B payload_len LE] [8B uid LE] [8B seq LE] [payload]
//!
//! file naming: evts-{startSeq} (rotated every events_per_file events)
//!
//! see: indigo cmd/relay/stream/persist/diskpersist/diskpersist.go
const std = @import("std");
const Io = std.Io;
const pg = @import("pg");
const lru = @import("util/lru.zig");
const util = @import("util/util.zig");
const getenv = util.getenv;
const Allocator = std.mem.Allocator;
const log = std.log.scoped(.relay);
// --- constants matching indigo ---
pub const header_size: usize = 28; // 4 + 4 + 4 + 8 + 8
pub const EvtKind = enum(u32) {
commit = 1,
handle = 2, // deprecated
tombstone = 3, // deprecated
identity = 4,
account = 5,
sync = 6,
};
pub const EvtFlags = struct {
pub const takedown: u32 = 1;
pub const rebased: u32 = 2;
};
const default_events_per_file: u32 = 10_000;
const default_flush_interval_ms: u64 = 100;
const default_flush_threshold: usize = 400;
/// upper bound on a single record's payload, used to reject a corrupt length
/// during recovery. firehose frames are well under this; a larger value means a
/// torn/garbage header, not a real record.
const max_record_len: u32 = 16 * 1024 * 1024;
// --- header ---
pub const EvtHeader = struct {
flags: u32,
kind: u32,
len: u32, // payload length (not including header)
uid: u64,
seq: u64,
pub fn encode(self: EvtHeader, buf: *[header_size]u8) void {
std.mem.writeInt(u32, buf[0..4], self.flags, .little);
std.mem.writeInt(u32, buf[4..8], self.kind, .little);
std.mem.writeInt(u32, buf[8..12], self.len, .little);
std.mem.writeInt(u64, buf[12..20], self.uid, .little);
std.mem.writeInt(u64, buf[20..28], self.seq, .little);
}
pub fn decode(buf: *const [header_size]u8) EvtHeader {
return .{
.flags = std.mem.readInt(u32, buf[0..4], .little),
.kind = std.mem.readInt(u32, buf[4..8], .little),
.len = std.mem.readInt(u32, buf[8..12], .little),
.uid = std.mem.readInt(u64, buf[12..20], .little),
.seq = std.mem.readInt(u64, buf[20..28], .little),
};
}
};
// --- persist job (write buffer entry) ---
const PersistJob = struct {
data: []u8, // header + payload, owned
seq: u64, // assigned seq (for broadcast ordering)
};
// --- disk persistence ---
/// bounds on a single playback call. a consumer connect with an old cursor
/// must never materialize the whole retained backlog in one shot — that is
/// an O(retention) allocation per connect, and a flapping consumer turns it
/// into an OOM (see docs/handoffs/HANDOFF-2026-08-06-consumer-flap-oom.md).
/// callers replay in bounded chunks instead, advancing `since` between calls.
pub const PlaybackLimits = struct {
max_entries: usize = std.math.maxInt(usize),
max_bytes: usize = std.math.maxInt(usize),
};
/// playback request — caller posts, pool_io worker executes
pub const PlaybackRequest = struct {
since: u64,
allocator: Allocator,
limits: PlaybackLimits = .{},
entries: std.ArrayListUnmanaged(PlaybackEntry) = .empty,
err: ?anyerror = null,
done: std.atomic.Value(bool) = .{ .raw = false },
next: std.atomic.Value(?*PlaybackRequest) = .{ .raw = null },
};
/// DB request — caller posts, pool_io worker executes.
/// callers define typed structs embedding DbRequest + @fieldParentPtr.
pub const DbRequest = struct {
callback: *const fn (*DbRequest, *DiskPersist) void,
done: std.atomic.Value(bool) = .{ .raw = false },
err: ?anyerror = null,
/// spin-wait for completion, yielding via io.sleep when available.
/// never returns until `done` is true — the request struct lives on the
/// caller's stack, so we must outlive the worker's callback execution.
/// the queue's shutdown drain sets done+err on unprocessed requests,
/// and workers set done after the callback returns, so this always terminates.
pub fn wait(self: *DbRequest, io: Io, shutdown: *std.atomic.Value(bool)) void {
_ = shutdown;
while (!self.done.load(.acquire)) {
io.sleep(Io.Duration.fromMicroseconds(100), .awake) catch {
// sleep failed (Io shutting down) — busy-wait for worker to finish
while (!self.done.load(.acquire)) {
std.atomic.spinLoopHint();
}
break;
};
}
}
};
/// MPSC FIFO ring buffer for general DB traffic.
/// multiple producers push via spinlock,
/// multiple consumers (pool_io worker threads) pop via CAS.
pub const DbRequestQueue = struct {
const CAPACITY = 4096;
items: [CAPACITY]*DbRequest = undefined,
head: std.atomic.Value(u32) = .{ .raw = 0 },
tail: std.atomic.Value(u32) = .{ .raw = 0 },
push_lock: std.atomic.Value(u32) = .{ .raw = 0 }, // producer spinlock
/// total requests handled across all worker threads. exposed as
/// relay_db_queue_handled_total — if this stops advancing while
/// the queue has depth, the workers are dead or stuck.
handled: std.atomic.Value(u64) = .{ .raw = 0 },
shutdown: *std.atomic.Value(bool),
persist: *DiskPersist,
/// current depth = tail - head. exposed as relay_db_queue_depth
/// so operators can correlate handler hangs with queue backup.
pub fn depth(self: *const DbRequestQueue) u32 {
const t = self.tail.load(.acquire);
const h = self.head.load(.acquire);
return t -% h;
}
pub fn push(self: *DbRequestQueue, req: *DbRequest) void {
// acquire producer spinlock
while (self.push_lock.cmpxchgWeak(0, 1, .acquire, .monotonic) != null) {
std.atomic.spinLoopHint();
}
defer self.push_lock.store(0, .release);
const tail = self.tail.load(.monotonic);
const head = self.head.load(.acquire);
// if full, spin until space opens (workers are fast, this shouldn't happen)
if (tail -% head >= CAPACITY) {
self.push_lock.store(0, .release);
while (self.tail.load(.monotonic) -% self.head.load(.acquire) >= CAPACITY) {
std.atomic.spinLoopHint();
}
while (self.push_lock.cmpxchgWeak(0, 1, .acquire, .monotonic) != null) {
std.atomic.spinLoopHint();
}
}
self.items[self.tail.load(.monotonic) % CAPACITY] = req;
self.tail.store(self.tail.load(.monotonic) +% 1, .release);
}
pub fn pop(self: *DbRequestQueue) ?*DbRequest {
while (true) {
const head = self.head.load(.acquire);
if (head == self.tail.load(.acquire)) return null;
const item = self.items[head % CAPACITY];
if (self.head.cmpxchgWeak(head, head +% 1, .acq_rel, .acquire) == null)
return item;
}
}
pub fn run(self: *DbRequestQueue, pool_io: Io) void {
log.info("db_queue worker: starting", .{});
var consecutive_sleep_errors: u32 = 0;
while (!self.shutdown.load(.acquire)) {
if (self.pop()) |req| {
req.callback(req, self.persist);
req.done.store(true, .release);
_ = self.handled.fetchAdd(1, .monotonic);
consecutive_sleep_errors = 0;
} else {
pool_io.sleep(Io.Duration.fromMilliseconds(5), .awake) catch |e| {
// previous behavior: silently `return`, killing the worker
// forever. one of these errors during a transient io hiccup
// would wedge requestCrawl + every other DB-bound handler.
// log + continue. spin-yield to avoid a tight error loop.
consecutive_sleep_errors += 1;
if (consecutive_sleep_errors == 1 or consecutive_sleep_errors % 1000 == 0) {
log.warn("db_queue worker: pool_io.sleep failed ({s}), continuing (consecutive={d})", .{ @errorName(e), consecutive_sleep_errors });
}
std.atomic.spinLoopHint();
};
}
}
// shutdown drain — signal error on unprocessed requests
while (self.pop()) |req| {
req.err = error.ShuttingDown;
req.done.store(true, .release);
}
log.info("db_queue worker: exiting (shutdown=true, handled_total={d})", .{self.handled.load(.acquire)});
}
};
pub const DiskPersist = struct {
allocator: Allocator,
dir_path: []const u8,
dir: Io.Dir,
db: *pg.Pool,
current_file: ?Io.File = null,
current_file_path: ?[]const u8 = null,
current_file_pos: u64 = 0,
// sequence state
cur_seq: u64 = 1,
event_counter: u64 = 0,
// config
events_per_file: u32 = default_events_per_file,
history_enabled: bool = true,
retention_seconds: ?u64 = null,
retention_hours: u64 = 72, // 3 days
max_dir_bytes: u64 = 100 * 1024 * 1024 * 1024, // 100 GB
// DID → UID cache (matches indigo's bidirectional ARC cache)
did_cache: lru.LruCache(u64),
// UID → host_id cache. host_id mutates ~5 orders of magnitude slower than
// it's read (every event hits getAccountHostId) and setAccountHostId is its
// sole writer, so a read-through cache eliminates a per-event point read.
// keyed by the uid's raw bytes; only nonzero host_ids are cached (0 means
// "not yet assigned", a transient state that resolves on the next commit).
host_id_cache: lru.LruCache(u64),
// write buffer (flushed periodically or when threshold hit)
outbuf: std.ArrayListUnmanaged(u8) = .empty,
evtbuf: std.ArrayListUnmanaged(PersistJob) = .empty,
mutex: Io.Mutex = Io.Mutex.init,
// flush task
flush_future: ?Io.Future(void) = null,
alive: std.atomic.Value(bool) = .{ .raw = true },
// highest seq that is durably on disk. advanced only by flushLocked, after the
// write. the broadcaster fiber refuses to emit a queued frame until its seq is
// <= committed_seq, so a frame never reaches consumers before it is durable —
// which means the seq recovered on restart can't trail an already-broadcast
// seq (the post-crash rewind). see docs/incident-2026-05-31-seq-rewind.md.
committed_seq: std.atomic.Value(u64) = .{ .raw = 0 },
io: Io,
/// last successful DB interaction (epoch seconds, set by Threaded workers).
/// read by metrics server to report health without direct pg.Pool access.
last_db_success: std.atomic.Value(i64) = .{ .raw = 0 },
/// MPSC queue for playback requests (caller → pool_io worker)
playback_head: std.atomic.Value(?*PlaybackRequest) = .{ .raw = null },
/// current evtbuf entry count (for metrics — non-blocking, returns 0 if lock is contended)
pub fn evtbufLen(self: *DiskPersist) usize {
if (!self.mutex.tryLock()) return 0;
defer self.mutex.unlock(self.io);
return self.evtbuf.items.len;
}
/// current DID cache entry count (for metrics)
pub fn didCacheLen(self: *DiskPersist) u32 {
return self.did_cache.count();
}
/// DID cache hashmap backing capacity (for memory attribution)
pub fn didCacheMapCap(self: *DiskPersist) u32 {
return self.did_cache.mapCapacity();
}
/// evtbuf allocated capacity in jobs (for memory attribution — non-blocking)
pub fn evtbufCap(self: *DiskPersist) usize {
if (!self.mutex.tryLock()) return 0;
defer self.mutex.unlock(self.io);
return self.evtbuf.capacity;
}
/// outbuf allocated capacity in bytes (for memory attribution — non-blocking)
pub fn outbufCap(self: *DiskPersist) usize {
if (!self.mutex.tryLock()) return 0;
defer self.mutex.unlock(self.io);
return self.outbuf.capacity;
}
pub fn init(allocator: Allocator, dir_path: []const u8, database_url: []const u8, db_pool_size: u16, io: Io) !DiskPersist {
// ensure directory exists
try Io.Dir.cwd().createDirPath(io, dir_path);
var dir = try Io.Dir.cwd().openDir(io, dir_path, .{ .iterate = true });
errdefer dir.close(io);
// connect to Postgres
const uri = std.Uri.parse(database_url) catch return error.InvalidDatabaseUrl;
const pool = try pg.Pool.initUri(io, allocator, uri, .{ .size = db_pool_size });
errdefer pool.deinit();
// create tables (matching indigo's Go relay schema)
_ = try pool.exec(
\\CREATE TABLE IF NOT EXISTS log_file_refs (
\\ id BIGSERIAL PRIMARY KEY,
\\ path TEXT NOT NULL,
\\ archived BOOLEAN NOT NULL DEFAULT false,
\\ seq_start BIGINT NOT NULL,
\\ created_at TIMESTAMPTZ NOT NULL DEFAULT now()
\\)
, .{});
_ = try pool.exec(
\\CREATE TABLE IF NOT EXISTS account (
\\ uid BIGSERIAL PRIMARY KEY,
\\ did TEXT NOT NULL UNIQUE,
\\ host_id BIGINT NOT NULL DEFAULT 0,
\\ status TEXT NOT NULL DEFAULT 'active',
\\ upstream_status TEXT NOT NULL DEFAULT 'active',
\\ created_at TIMESTAMPTZ NOT NULL DEFAULT now()
\\)
, .{});
// migration: add columns if they don't exist (for existing deployments)
_ = pool.exec("ALTER TABLE account ADD COLUMN IF NOT EXISTS host_id BIGINT NOT NULL DEFAULT 0", .{}) catch {};
_ = pool.exec("ALTER TABLE account ADD COLUMN IF NOT EXISTS upstream_status TEXT NOT NULL DEFAULT 'active'", .{}) catch {};
// index host_id so the per-host account COUNT(*) (new-account admission)
// is an index scan instead of an 8.7M-row seq scan. CONCURRENTLY avoids
// locking the live account table during the build; IF NOT EXISTS makes
// re-runs (and resuming a failed build) safe.
_ = pool.exec("CREATE INDEX CONCURRENTLY IF NOT EXISTS account_host_id_idx ON account (host_id)", .{}) catch {};
_ = try pool.exec(
\\CREATE TABLE IF NOT EXISTS account_repo (
\\ uid BIGINT PRIMARY KEY REFERENCES account(uid),
\\ rev TEXT NOT NULL,
\\ commit_data_cid TEXT NOT NULL
\\)
, .{});
_ = try pool.exec("ALTER TABLE account_repo ADD COLUMN IF NOT EXISTS commit_cid TEXT NOT NULL DEFAULT ''", .{});
_ = try pool.exec("CREATE INDEX CONCURRENTLY IF NOT EXISTS account_repo_known_commit_idx ON account_repo (uid) WHERE commit_cid <> '' AND rev <> ''", .{});
_ = try pool.exec(
\\CREATE TABLE IF NOT EXISTS domain_ban (
\\ id BIGSERIAL PRIMARY KEY,
\\ domain TEXT NOT NULL UNIQUE,
\\ created_at TIMESTAMPTZ NOT NULL DEFAULT now()
\\)
, .{});
_ = try pool.exec(
\\CREATE TABLE IF NOT EXISTS host (
\\ id BIGSERIAL PRIMARY KEY,
\\ hostname TEXT NOT NULL UNIQUE,
\\ status TEXT NOT NULL DEFAULT 'active',
\\ last_seq BIGINT NOT NULL DEFAULT 0,
\\ failed_attempts INTEGER NOT NULL DEFAULT 0,
\\ account_limit BIGINT,
\\ created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
\\ updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
\\)
, .{});
// migration: add account_limit column to existing host tables
_ = try pool.exec("ALTER TABLE host ADD COLUMN IF NOT EXISTS account_limit BIGINT", .{});
_ = try pool.exec(
\\CREATE TABLE IF NOT EXISTS backfill_progress (
\\ collection TEXT NOT NULL,
\\ source TEXT NOT NULL,
\\ cursor TEXT NOT NULL DEFAULT '',
\\ imported_count BIGINT NOT NULL DEFAULT 0,
\\ completed_at TIMESTAMPTZ,
\\ created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
\\ PRIMARY KEY (collection, source)
\\)
, .{});
// migrate: old schema had collection as sole PK — add source to composite PK
_ = pool.exec(
\\DO $$ BEGIN
\\ IF EXISTS (
\\ SELECT 1 FROM pg_constraint
\\ WHERE conname = 'backfill_progress_pkey'
\\ AND conrelid = 'backfill_progress'::regclass
\\ AND array_length(conkey, 1) = 1
\\ ) THEN
\\ ALTER TABLE backfill_progress DROP CONSTRAINT backfill_progress_pkey;
\\ ALTER TABLE backfill_progress ADD PRIMARY KEY (collection, source);
\\ END IF;
\\END $$
, .{}) catch {};
var self = DiskPersist{
.allocator = allocator,
.dir_path = try allocator.dupe(u8, dir_path),
.dir = dir,
.db = pool,
.did_cache = lru.LruCache(u64).init(allocator, 500_000, io),
.host_id_cache = lru.LruCache(u64).init(allocator, 500_000, io),
.io = io,
};
// recover from existing log files
try self.resumeLog();
// everything already on disk is committed; cur_seq points one past it.
self.committed_seq.store(self.cur_seq -| 1, .monotonic);
return self;
}
pub fn deinit(self: *DiskPersist) void {
// stop flush task (loop checks alive after each sleep interval)
self.alive.store(false, .release);
if (self.flush_future) |*f| f.cancel(self.io);
// flush remaining
self.mutex.lockUncancelable(self.io);
self.flushLocked() catch |err| log.err("final event flush failed: {s}", .{@errorName(err)});
self.mutex.unlock(self.io);
// free write buffer
for (self.evtbuf.items) |job| self.allocator.free(job.data);
self.evtbuf.deinit(self.allocator);
self.outbuf.deinit(self.allocator);
self.did_cache.deinit();
self.host_id_cache.deinit();
if (self.current_file) |f| f.close(self.io);
if (self.current_file_path) |p| self.allocator.free(p);
self.dir.close(self.io);
self.db.deinit();
self.allocator.free(self.dir_path);
}
/// start the background flush thread
pub fn start(self: *DiskPersist) !void {
self.flush_future = try self.io.concurrent(flushLoop, .{self});
}
pub const UidResult = struct {
uid: u64,
host_changed: bool = false,
is_new: bool = false,
};
/// resolve a DID to a numeric UID, checking host association.
/// does NOT set host_id — caller must verify authority first via DID doc
/// resolution, then call setAccountHostId on success.
/// returns is_new=true when no host_id is set yet (first encounter),
/// host_changed=true when host_id differs from stored value.
pub fn uidForDidFromHost(self: *DiskPersist, did: []const u8, host_id: u64) !UidResult {
const uid = try self.uidForDid(did);
if (host_id > 0) {
const current_host = self.getAccountHostId(uid) catch 0;
if (current_host == 0) {
return .{ .uid = uid, .is_new = true };
} else if (current_host != host_id) {
log.info("account {s} (uid={d}) host mismatch: current={d} new={d}", .{ did, uid, current_host, host_id });
return .{ .uid = uid, .host_changed = true };
}
}
return .{ .uid = uid };
}
/// check DB health without touching pg.Pool — safe from any Io context.
/// returns true if a Threaded worker successfully queried the DB within the last 30s.
pub fn isDbHealthy(self: *DiskPersist) bool {
const last = self.last_db_success.load(.acquire);
if (last == 0) return false;
var ts: std.c.timespec = undefined;
_ = std.c.clock_gettime(.REALTIME, &ts);
return (@as(i64, ts.sec) - last) < 30;
}
fn markDbSuccess(self: *DiskPersist) void {
var ts: std.c.timespec = undefined;
_ = std.c.clock_gettime(.REALTIME, &ts);
self.last_db_success.store(@as(i64, ts.sec), .release);
}
/// resolve a DID to a numeric UID. creates a new account row on first encounter.
/// matches indigo's Relay.DidToUid → Account.UID mapping.
pub fn uidForDid(self: *DiskPersist, did: []const u8) !u64 {
// fast path: check in-memory cache
if (self.did_cache.get(did)) |uid| return uid;
// check database
if (try self.db.rowUnsafe(
"SELECT uid FROM account WHERE did = $1",
.{did},
)) |row| {
var r = row;
defer r.deinit() catch {};
const uid: u64 = @intCast(r.get(i64, 0));
self.didCachePut(did, uid);
self.markDbSuccess();
return uid;
}
// create new account row (ignore if already exists from concurrent insert)
_ = self.db.exec(
"INSERT INTO account (did) VALUES ($1) ON CONFLICT (did) DO NOTHING",
.{did},
) catch |err| {
log.warn("failed to create account for {s}: {s}", .{ did, @errorName(err) });
return err;
};
// read back the UID (whether we just created it or it already existed)
var row = try self.db.rowUnsafe(
"SELECT uid FROM account WHERE did = $1",
.{did},
) orelse return error.AccountCreationFailed;
defer row.deinit() catch {};
const uid: u64 = @intCast(row.get(i64, 0));
self.didCachePut(did, uid);
return uid;
}
/// insert into did_cache (LRU handles eviction at capacity).
fn didCachePut(self: *DiskPersist, did: []const u8, uid: u64) void {
self.did_cache.put(did, uid) catch {};
}
/// per-DID sync state for chain tracking
pub const AccountState = struct {
rev: []const u8,
data_cid: []const u8,
};
/// get stored sync state for a user (from account_repo table)
pub fn getAccountState(self: *DiskPersist, uid: u64, allocator: Allocator) !?AccountState {
var row = (try self.db.rowUnsafe(
"SELECT rev, commit_data_cid FROM account_repo WHERE uid = $1",
.{@as(i64, @intCast(uid))},
)) orelse return null;
defer row.deinit() catch {};
const rev = row.get([]const u8, 0);
const data_cid = row.get([]const u8, 1);
// A rev-only row is still prior repo state. Older zlay versions and
// signature-only #sync handling stored an empty data CID; discarding
// those rows disabled stale-rev and missing-prevData checks entirely.
if (rev.len == 0) return null;
return .{
.rev = try allocator.dupe(u8, rev),
.data_cid = try allocator.dupe(u8, data_cid),
};
}
/// update stored sync state after a verified commit (conditional upsert into account_repo).
/// uses WHERE clause to ensure rev only moves forward, preventing race conditions
/// when concurrent workers process commits for the same DID.
/// returns true if the state was updated, false if a newer rev was already stored.
pub fn updateAccountState(self: *DiskPersist, uid: u64, rev: []const u8, data_cid: []const u8, commit_cid: []const u8) !bool {
const rows_affected = try self.db.exec(
"INSERT INTO account_repo (uid, rev, commit_data_cid, commit_cid) VALUES ($1, $2, $3, $4) ON CONFLICT (uid) DO UPDATE SET rev = EXCLUDED.rev, commit_data_cid = EXCLUDED.commit_data_cid, commit_cid = EXCLUDED.commit_cid WHERE account_repo.rev < EXCLUDED.rev",
.{ @as(i64, @intCast(uid)), rev, data_cid, commit_cid },
);
return (rows_affected orelse 0) > 0;
}
// --- account status ---
/// raw little-endian bytes of a uid, for use as an LRU key.
fn uidKey(uid: u64) [8]u8 {
return @bitCast(std.mem.nativeToLittle(u64, uid));
}
/// get the host_id for an account. returns 0 if not set.
/// read-through cache: only nonzero host_ids are cached (0 is the transient
/// pre-assignment state, which resolves via setAccountHostId on next commit).
pub fn getAccountHostId(self: *DiskPersist, uid: u64) !u64 {
const key = uidKey(uid);
if (self.host_id_cache.get(&key)) |hid| return hid;
var row = (try self.db.rowUnsafe(
"SELECT host_id FROM account WHERE uid = $1",
.{@as(i64, @intCast(uid))},
)) orelse return 0;
defer row.deinit() catch {};
const hid = row.get(i64, 0);
if (hid > 0) {
const host_id: u64 = @intCast(hid);
self.host_id_cache.put(&key, host_id) catch {};
return host_id;
}
return 0;
}
/// count accounts on a host (Threaded pool)
pub fn getHostAccountCount(self: *DiskPersist, host_id: u64) u64 {
return getHostAccountCountImpl(host_id, self.db);
}
fn getHostAccountCountImpl(host_id: u64, db: *pg.Pool) u64 {
var row = (db.rowUnsafe(
"SELECT COUNT(*) FROM account WHERE host_id = $1",
.{@as(i64, @intCast(host_id))},
) catch return 0) orelse return 0;
defer row.deinit() catch {};
const count = row.get(i64, 0);
return if (count > 0) @intCast(count) else 0;
}
/// effective account count (Threaded pool)
pub fn getEffectiveAccountCount(self: *DiskPersist, host_id: u64) u64 {
return getEffectiveAccountCountImpl(host_id, self.db);
}
/// uses admin-configured limit if set, otherwise actual COUNT(*).
/// CASE (not COALESCE) so the COUNT subquery is only evaluated when no
/// account_limit is set — SQL doesn't short-circuit a COALESCE's aggregate.
fn getEffectiveAccountCountImpl(host_id: u64, db: *pg.Pool) u64 {
var row = (db.rowUnsafe(
"SELECT CASE WHEN h.account_limit IS NOT NULL THEN h.account_limit ELSE (SELECT COUNT(*) FROM account a WHERE a.host_id = h.id) END FROM host h WHERE h.id = $1",
.{@as(i64, @intCast(host_id))},
) catch return 0) orelse return 0;
defer row.deinit() catch {};
const count = row.get(i64, 0);
return if (count > 0) @intCast(count) else 0;
}
/// set host account limit (Threaded pool)
pub fn setHostAccountLimit(self: *DiskPersist, host_id: u64, limit: ?u64) !void {
return setHostAccountLimitImpl(host_id, limit, self.db);
}
/// pass null to clear the override and revert to actual COUNT(*).
fn setHostAccountLimitImpl(host_id: u64, limit: ?u64, db: *pg.Pool) !void {
if (limit) |l| {
const clamped: i64 = if (l > @as(u64, @intCast(std.math.maxInt(i64)))) std.math.maxInt(i64) else @intCast(l);
_ = try db.exec(
"UPDATE host SET account_limit = $2, updated_at = now() WHERE id = $1",
.{ @as(i64, @intCast(host_id)), clamped },
);
} else {
_ = try db.exec(
"UPDATE host SET account_limit = NULL, updated_at = now() WHERE id = $1",
.{@as(i64, @intCast(host_id))},
);
}
}
/// set the host_id for an account (first encounter or migration)
pub fn setAccountHostId(self: *DiskPersist, uid: u64, host_id: u64) !void {
_ = try self.db.exec(
"UPDATE account SET host_id = $2 WHERE uid = $1",
.{ @as(i64, @intCast(uid)), @as(i64, @intCast(host_id)) },
);
// write-through: sole writer, so the cache stays coherent with the DB.
const key = uidKey(uid);
if (host_id > 0) {
self.host_id_cache.put(&key, host_id) catch {};
} else {
_ = self.host_id_cache.remove(&key);
}
}
/// update the upstream status for an account (from #account events).
/// Go relay: account.go UpdateAccountUpstreamStatus
pub fn updateAccountUpstreamStatus(self: *DiskPersist, uid: u64, upstream_status: []const u8) !void {
_ = try self.db.exec(
"UPDATE account SET upstream_status = $2 WHERE uid = $1",
.{ @as(i64, @intCast(uid)), upstream_status },
);
}
/// check if an account is active (both local status and upstream status).
/// Go relay: models.Account.IsActive()
pub const AccountActivity = struct {
local_ok: bool,
upstream_ok: bool,
pub fn active(self: AccountActivity) bool {
return self.local_ok and self.upstream_ok;
}
};
/// account activity split by source: local (relay takedown) vs upstream
/// (PDS-reported via #account events). callers that drop events for
/// upstream-inactive accounts use the distinction to trigger a
/// StatusChecker re-check — local takedowns must never be re-checked away.
pub fn accountActivity(self: *DiskPersist, uid: u64) !AccountActivity {
var row = (try self.db.rowUnsafe(
"SELECT status, upstream_status FROM account WHERE uid = $1",
.{@as(i64, @intCast(uid))},
)) orelse return .{ .local_ok = false, .upstream_ok = false };
defer row.deinit() catch {};
const status = row.get([]const u8, 0);
const upstream = row.get([]const u8, 1);
return .{
.local_ok = std.mem.eql(u8, status, "active"),
.upstream_ok = std.mem.eql(u8, upstream, "active"),
};
}
pub fn isAccountActive(self: *DiskPersist, uid: u64) !bool {
return (try self.accountActivity(uid)).active();
}
// --- host management ---
pub const Host = struct {
id: u64,
hostname: []const u8,
status: []const u8,
last_seq: u64,
failed_attempts: u32,
account_limit: ?u64 = null,
};
const HostResult = struct { id: u64, last_seq: u64 };
/// get or create a host row (Threaded pool)
pub fn getOrCreateHost(self: *DiskPersist, hostname: []const u8) !HostResult {
return getOrCreateHostImpl(hostname, self.db);
}
fn getOrCreateHostImpl(hostname: []const u8, db: *pg.Pool) !HostResult {
_ = db.exec(
"INSERT INTO host (hostname) VALUES ($1) ON CONFLICT (hostname) DO NOTHING",
.{hostname},
) catch |err| {
log.warn("failed to create host {s}: {s}", .{ hostname, @errorName(err) });
return err;
};
var row = try db.rowUnsafe(
"SELECT id, last_seq FROM host WHERE hostname = $1",
.{hostname},
) orelse return error.HostCreationFailed;
defer row.deinit() catch {};
return .{
.id = @intCast(row.get(i64, 0)),
.last_seq = @intCast(row.get(i64, 1)),
};
}
/// check if a host is banned or blocked by status (Threaded pool)
pub fn isHostBanned(self: *DiskPersist, hostname: []const u8) bool {
return isHostBannedImpl(hostname, self.db);
}
fn isHostBannedImpl(hostname: []const u8, db: *pg.Pool) bool {
var row = db.rowUnsafe(
"SELECT status FROM host WHERE hostname = $1",
.{hostname},
) catch return false;
if (row) |*r| {
defer r.deinit() catch {};
const status = r.get([]const u8, 0);
return std.mem.eql(u8, status, "banned") or std.mem.eql(u8, status, "blocked");
}
return false;
}
/// update cursor position for a host
pub fn updateHostSeq(self: *DiskPersist, host_id: u64, seq: u64) !void {
_ = try self.db.exec(
"UPDATE host SET last_seq = $2, updated_at = now() WHERE id = $1",
.{ @as(i64, @intCast(host_id)), @as(i64, @intCast(seq)) },
);
}
/// look up host ID by hostname (Threaded pool)
pub fn getHostIdForHostname(self: *DiskPersist, hostname: []const u8) !?u64 {
return getHostIdForHostnameImpl(hostname, self.db);
}
fn getHostIdForHostnameImpl(hostname: []const u8, db: *pg.Pool) !?u64 {
var row = (try db.rowUnsafe(
"SELECT id FROM host WHERE hostname = $1",
.{hostname},
)) orelse return null;
defer row.deinit() catch {};
return @intCast(row.get(i64, 0));
}
/// update host status (Threaded pool)
pub fn updateHostStatus(self: *DiskPersist, host_id: u64, status: []const u8) !void {
return updateHostStatusImpl(host_id, status, self.db);
}
fn updateHostStatusImpl(host_id: u64, status: []const u8, db: *pg.Pool) !void {
_ = try db.exec(
"UPDATE host SET status = $2, updated_at = now() WHERE id = $1",
.{ @as(i64, @intCast(host_id)), status },
);
}
/// list all active hosts (Threaded pool)
pub fn listActiveHosts(self: *DiskPersist, allocator: Allocator) ![]Host {
return listActiveHostsImpl(allocator, self.db);
}
fn listActiveHostsImpl(allocator: Allocator, db: *pg.Pool) ![]Host {
var hosts: std.ArrayListUnmanaged(Host) = .empty;
errdefer {
for (hosts.items) |host| {
allocator.free(host.hostname);
allocator.free(host.status);
}
hosts.deinit(allocator);
}
var result = try db.query(
"SELECT id, hostname, status, last_seq, failed_attempts, account_limit FROM host WHERE status = 'active' ORDER BY id ASC",
.{},
);
defer result.deinit();
while (result.nextUnsafe() catch null) |row| {
try hosts.append(allocator, .{
.id = @intCast(row.get(i64, 0)),
.hostname = try allocator.dupe(u8, row.get([]const u8, 1)),
.status = try allocator.dupe(u8, row.get([]const u8, 2)),
.last_seq = @intCast(row.get(i64, 3)),
.failed_attempts = @intCast(row.get(i32, 4)),
.account_limit = if (row.get(?i64, 5)) |v| @as(?u64, @intCast(v)) else null,
});
}
return try hosts.toOwnedSlice(allocator);
}
/// list all hosts (Threaded pool)
pub fn listAllHosts(self: *DiskPersist, allocator: Allocator) ![]Host {
return listAllHostsImpl(allocator, self.db);
}
fn listAllHostsImpl(allocator: Allocator, db: *pg.Pool) ![]Host {
var hosts: std.ArrayListUnmanaged(Host) = .empty;
errdefer {
for (hosts.items) |host| {
allocator.free(host.hostname);
allocator.free(host.status);
}
hosts.deinit(allocator);
}
var result = try db.query(
"SELECT id, hostname, status, last_seq, failed_attempts, account_limit FROM host ORDER BY id ASC",
.{},
);
defer result.deinit();
while (result.nextUnsafe() catch null) |row| {
try hosts.append(allocator, .{
.id = @intCast(row.get(i64, 0)),
.hostname = try allocator.dupe(u8, row.get([]const u8, 1)),
.status = try allocator.dupe(u8, row.get([]const u8, 2)),
.last_seq = @intCast(row.get(i64, 3)),
.failed_attempts = @intCast(row.get(i32, 4)),
.account_limit = if (row.get(?i64, 5)) |v| @as(?u64, @intCast(v)) else null,
});
}
return try hosts.toOwnedSlice(allocator);
}
/// increment failure count and return new value
pub fn incrementHostFailures(self: *DiskPersist, host_id: u64) !u32 {
_ = try self.db.exec(
"UPDATE host SET failed_attempts = failed_attempts + 1, updated_at = now() WHERE id = $1",
.{@as(i64, @intCast(host_id))},
);
var row = try self.db.rowUnsafe(
"SELECT failed_attempts FROM host WHERE id = $1",
.{@as(i64, @intCast(host_id))},
) orelse return 0;
defer row.deinit() catch {};
return @intCast(row.get(i32, 0));
}
/// check if a hostname (or any parent domain) is banned (Threaded pool).
pub fn isDomainBanned(self: *DiskPersist, hostname: []const u8) bool {
return self.isDomainBannedImpl(hostname, self.db);
}
/// Go relay: domain_ban.go DomainIsBanned — suffix-based check.
fn isDomainBannedImpl(_: *DiskPersist, hostname: []const u8, db: *pg.Pool) bool {
// check each suffix: "pds.host.example.com", "host.example.com", "example.com"
var offset: usize = 0;
while (offset < hostname.len) {
const suffix = hostname[offset..];
var row = db.rowUnsafe(
"SELECT 1 FROM domain_ban WHERE domain = $1",
.{suffix},
) catch return false;
if (row) |*r| {
r.deinit() catch {};
return true;
}
// advance past next dot
if (std.mem.indexOfScalarPos(u8, hostname, offset, '.')) |dot| {
offset = dot + 1;
} else break;
}
return false;
}
/// reset failure count (Threaded pool)
pub fn resetHostFailures(self: *DiskPersist, host_id: u64) !void {
return resetHostFailuresImpl(host_id, self.db);
}
fn resetHostFailuresImpl(host_id: u64, db: *pg.Pool) !void {
_ = try db.exec(
"UPDATE host SET failed_attempts = 0, updated_at = now() WHERE id = $1",
.{@as(i64, @intCast(host_id))},
);
}
/// enqueue a playback request for the pool_io worker to execute.
/// uses MPSC push (lock-free Treiber stack).
pub fn enqueuePlayback(self: *DiskPersist, req: *PlaybackRequest) void {
while (true) {
const head = self.playback_head.load(.acquire);
req.next.store(head, .monotonic);
if (self.playback_head.cmpxchgWeak(@as(?*PlaybackRequest, head), req, .release, .monotonic) == null) {
return;
}
}
}
/// pop all pending playback requests (single-consumer — pool_io worker only).
/// returns a singly-linked list via .next pointers, or null if empty.
pub fn popPlaybackBatch(self: *DiskPersist) ?*PlaybackRequest {
return self.playback_head.swap(null, .acq_rel);
}
/// persist an event. assigns a sequence number. returns the assigned seq.
/// the event is buffered and will be flushed to disk asynchronously.
pub fn persist(self: *DiskPersist, kind: EvtKind, uid: u64, payload: []const u8) !u64 {
if (!self.history_enabled) {
self.mutex.lockUncancelable(self.io);
defer self.mutex.unlock(self.io);
const seq = self.cur_seq;
self.cur_seq += 1;
self.committed_seq.store(seq, .release);
return seq;
}
// build the on-disk record: header + payload
const total_len = header_size + payload.len;
const data = try self.allocator.alloc(u8, total_len);
errdefer self.allocator.free(data);
// write header (seq filled in later under lock)
const header = EvtHeader{
.flags = 0,
.kind = @backingInt(kind),
.len = @intCast(payload.len),
.uid = uid,
.seq = 0, // placeholder
};
header.encode(data[0..header_size]);
@memcpy(data[header_size..], payload);
self.mutex.lockUncancelable(self.io);
defer self.mutex.unlock(self.io);
if (self.evtbuf.items.len >= default_flush_threshold) try self.flushLocked();
try self.evtbuf.ensureUnusedCapacity(self.allocator, 1);
try self.outbuf.ensureUnusedCapacity(self.allocator, data.len);
// assign seq
const seq = self.cur_seq;
self.cur_seq += 1;
std.mem.writeInt(u64, data[20..28], seq, .little);
self.evtbuf.appendAssumeCapacity(.{ .data = data, .seq = seq });
self.outbuf.appendSliceAssumeCapacity(data);
self.event_counter += 1;
// flush if threshold hit
if (self.evtbuf.items.len >= default_flush_threshold) {
self.flushLocked() catch |err| log.warn("event flush deferred: {s}", .{@errorName(err)});
}
return seq;
}
/// playback events with seq > since, bounded by `limits`. a truncated
/// result is detected by the caller via entries hitting the limits;
/// resume by calling again with since = last returned seq.
///
/// runs without `mutex`. holding it across the file reads blocked persist
/// and flush (so `committed_seq` froze and the broadcaster held the ring)
/// for the length of every replay chunk (2026-10-18). the reads are safe
/// without it: sealed files are immutable, the current file only grows by
/// whole records appended in seq order (a torn tail reads as a short read
/// and ends the chunk; the caller resumes from the last complete seq), and
/// a file gc removes between the ref query and the open is skipped. the
/// one writer that mutates records in place, takedown, sets the flag word
/// before zeroing the payload, so a concurrent reader either skips the
/// record or still sees the original payload.
pub fn playback(self: *DiskPersist, since: u64, allocator: Allocator, entries: *std.ArrayListUnmanaged(PlaybackEntry), limits: PlaybackLimits) !void {
if (!self.history_enabled) return error.ReplayDisabled;
const since_i: i64 = @intCast(since);
// find the log file containing `since`
var start_files: std.ArrayListUnmanaged(LogFileRef) = .empty;
defer start_files.deinit(allocator);
if (since > 0) {
// find file whose seq_start is just before `since`
if (try self.db.rowUnsafe(
"SELECT id, path, seq_start FROM log_file_refs WHERE seq_start <= $1 ORDER BY seq_start DESC LIMIT 1",
.{since_i},
)) |row| {
var r = row;
defer r.deinit() catch {};
try start_files.append(allocator, .{
.path = try allocator.dupe(u8, r.get([]const u8, 1)),
.seq_start = @intCast(r.get(i64, 2)),
});
}
}
// find all subsequent files
{
var result = try self.db.query(
"SELECT id, path, seq_start FROM log_file_refs WHERE seq_start > $1 ORDER BY seq_start ASC",
.{since_i},
);
defer result.deinit();
while (result.nextUnsafe() catch null) |r| {
try start_files.append(allocator, .{
.path = try allocator.dupe(u8, r.get([]const u8, 1)),
.seq_start = @intCast(r.get(i64, 2)),
});
}
}
defer for (start_files.items) |f| allocator.free(f.path);
// read events from each file until the limits are hit
var bytes_used: usize = 0;
for (start_files.items) |ref| {
if (entries.items.len >= limits.max_entries or bytes_used >= limits.max_bytes) break;
var file = self.dir.openFile(self.io, ref.path, .{}) catch continue;
defer file.close(self.io);
try readEventsFrom(allocator, file, self.io, since, entries, limits, &bytes_used);
}
}
/// last assigned sequence number, or null if empty
pub fn lastSeq(self: *DiskPersist) ?u64 {
if (self.cur_seq <= 1) return null;
return self.cur_seq - 1;
}
/// oldest available sequence number (Threaded pool)
pub fn firstSeq(self: *DiskPersist) ?u64 {
return firstSeqImpl(self.db);
}
fn firstSeqImpl(db: *pg.Pool) ?u64 {
if (db.rowUnsafe(
"SELECT seq_start FROM log_file_refs ORDER BY seq_start ASC LIMIT 1",
.{},
) catch null) |row| {
var r = row;
defer r.deinit() catch {};
const v = r.get(i64, 0);
return if (v > 0) @intCast(v) else null;
}
return null;
}
/// garbage-collect log files older than the retention period
pub fn gc(self: *DiskPersist) !void {
self.mutex.lockUncancelable(self.io);
defer self.mutex.unlock(self.io);
const cutoff_interval = if (self.retention_seconds) |seconds|
try std.fmt.allocPrint(self.allocator, "{d} seconds", .{seconds})
else
try std.fmt.allocPrint(self.allocator, "{d} hours", .{self.retention_hours});
defer self.allocator.free(cutoff_interval);
// find expired refs
var expired: std.ArrayListUnmanaged(GcRef) = .empty;
defer {
for (expired.items) |e| self.allocator.free(e.path);
expired.deinit(self.allocator);
}
{
var result = try self.db.query(
"SELECT id, path FROM log_file_refs WHERE created_at < now() - $1::interval",
.{cutoff_interval},
);
defer result.deinit();
while (result.nextUnsafe() catch null) |r| {
try expired.append(self.allocator, .{
.id = r.get(i64, 0),
.path = try self.allocator.dupe(u8, r.get([]const u8, 1)),
});
}
}
for (expired.items) |ref| {
// skip current file
if (self.current_file_path) |cur| {
if (std.mem.eql(u8, ref.path, cur)) continue;
}
// delete db record first (prevents playback from finding it)
_ = self.db.exec("DELETE FROM log_file_refs WHERE id = $1", .{ref.id}) catch |err| {
log.warn("gc: failed to delete db record {d}: {s}", .{ ref.id, @errorName(err) });
continue;
};
// delete file
self.dir.deleteFile(self.io, ref.path) catch |err| {
log.warn("gc: failed to delete {s}: {s}", .{ ref.path, @errorName(err) });
};
}
if (expired.items.len > 0) {
log.info("gc: removed {d} expired log files", .{expired.items.len});
}
// size-based GC: delete oldest files until total size is under max_dir_bytes
if (self.max_dir_bytes > 0) {
try self.gcBySize();
}
self.gcOrphans() catch |err| {
log.warn("gc: orphan sweep failed: {s}", .{@errorName(err)});
};
self.markDbSuccess();
}
/// delete segment files that no log_file_refs row references and that are
/// strictly older (by start seq, from the evts-{seq} name) than everything
/// still registered. gc deletes the row before the file — deliberately, so
/// playback can never resolve a missing file — which means a failed file
/// delete leaves an orphan no later time-based pass can see: the row is
/// gone, and the file leaks forever. that is how production accumulated
/// ~100G / 10 weeks of unreferenced segments (found 2026-08-27). playback
/// resolves every read through log_file_refs, so an unreferenced file
/// below the retained window is unreachable and safe to remove while live.
fn gcOrphans(self: *DiskPersist) !void {
var min_registered: ?u64 = null;
{
var result = try self.db.query("SELECT min(seq_start) FROM log_file_refs", .{});
defer result.deinit();
while (result.nextUnsafe() catch null) |r| {
if (r.get(?i64, 0)) |v| min_registered = @intCast(v);
}
}
// no registered segments at all: nothing is provably expired — a
// fresh DB over an old data dir must not eat the recovery source
const floor = min_registered orelse return;
var doomed: std.ArrayListUnmanaged([]u8) = .empty;
defer {
for (doomed.items) |p| self.allocator.free(p);
doomed.deinit(self.allocator);
}
{
var it = self.dir.iterate();
while (it.next(self.io) catch null) |entry| {
if (entry.kind != .file) continue;
if (!std.mem.startsWith(u8, entry.name, "evts-")) continue;
const seq = std.fmt.parseInt(u64, entry.name["evts-".len..], 10) catch continue;
if (seq >= floor) continue;
if (self.current_file_path) |cur| {
if (std.mem.eql(u8, entry.name, cur)) continue;
}
try doomed.append(self.allocator, try self.allocator.dupe(u8, entry.name));
}
}
var removed: usize = 0;
for (doomed.items) |path| {
self.dir.deleteFile(self.io, path) catch |err| {
log.warn("gc: failed to delete orphan {s}: {s}", .{ path, @errorName(err) });
continue;
};
removed += 1;
}
if (removed > 0) {
log.info("gc: removed {d} orphaned log files (unreferenced, below seq {d})", .{ removed, floor });
}
}
/// delete oldest event files until total directory size is under max_dir_bytes
fn gcBySize(self: *DiskPersist) !void {
// query all refs ordered by creation time (oldest first)
var refs: std.ArrayListUnmanaged(GcSizeRef) = .empty;
defer {
for (refs.items) |e| self.allocator.free(e.path);
refs.deinit(self.allocator);
}
var total_bytes: u64 = 0;
{
var result = try self.db.query(
"SELECT id, path FROM log_file_refs ORDER BY id ASC",
.{},
);
defer result.deinit();
while (result.nextUnsafe() catch null) |r| {
const path = r.get([]const u8, 1);
const stat = self.dir.statFile(self.io, path, .{}) catch continue;
total_bytes += stat.size;
try refs.append(self.allocator, .{
.id = r.get(i64, 0),
.path = try self.allocator.dupe(u8, path),
.size = stat.size,
});
}
}
if (total_bytes <= self.max_dir_bytes) return;
log.info("gc: event dir size {d} MB exceeds limit {d} MB, pruning oldest files", .{
total_bytes / (1024 * 1024),
self.max_dir_bytes / (1024 * 1024),
});
var removed: u64 = 0;
for (refs.items) |ref| {
if (total_bytes <= self.max_dir_bytes) break;
// never delete the current file
if (self.current_file_path) |cur| {
if (std.mem.eql(u8, ref.path, cur)) continue;
}
_ = self.db.exec("DELETE FROM log_file_refs WHERE id = $1", .{ref.id}) catch |err| {
log.warn("gc: failed to delete db record {d}: {s}", .{ ref.id, @errorName(err) });
continue;
};
self.dir.deleteFile(self.io, ref.path) catch |err| {
log.warn("gc: failed to delete {s}: {s}", .{ ref.path, @errorName(err) });
continue; // don't subtract size if file delete failed
};
total_bytes -= @min(total_bytes, ref.size);
removed += 1;
}
if (removed > 0) {
log.info("gc: size-based pruning removed {d} files, dir now {d} MB", .{
removed,
total_bytes / (1024 * 1024),
});
}
}
/// take down all events for a user (set flag + zero payload)
pub fn takeDownUser(self: *DiskPersist, uid: u64) !void {
self.mutex.lockUncancelable(self.io);
defer self.mutex.unlock(self.io);
// iterate all log files
var refs: std.ArrayListUnmanaged([]const u8) = .empty;
defer {
for (refs.items) |p| self.allocator.free(p);
refs.deinit(self.allocator);
}
{
var result = try self.db.query("SELECT path FROM log_file_refs ORDER BY seq_start DESC", .{});
defer result.deinit();
while (result.nextUnsafe() catch null) |r| {
try refs.append(self.allocator, try self.allocator.dupe(u8, r.get([]const u8, 0)));
}
}
for (refs.items) |path| {
var file = self.dir.openFile(self.io, path, .{ .mode = .read_write }) catch continue;
defer file.close(self.io);
mutateUserEventsInFile(file, self.io, uid) catch |err| {
log.warn("takedown: failed to process {s}: {s}", .{ path, @errorName(err) });
};
}
}
// --- internals ---
fn resumeLog(self: *DiskPersist) !void {
// find most recent log file
if (try self.db.rowUnsafe(
"SELECT id, path, seq_start FROM log_file_refs ORDER BY seq_start DESC LIMIT 1",
.{},
)) |row| {
var r = row;
defer r.deinit() catch {};
const path = r.get([]const u8, 1);
const seq_start: u64 = @intCast(r.get(i64, 2));
var file = self.dir.openFile(self.io, path, .{ .mode = .read_write }) catch {
// file missing, start fresh
try self.initLogFile();
return;
};
// scan for last seq (validates records, reports the last good offset)
const scan = scanForLastSeq(file, self.io, seq_start) catch {
file.close(self.io);
try self.initLogFile();
return;
};
// heal a torn tail (partial write from an unclean kill) so subsequent
// appends stay record-aligned and future scans don't desync.
const full_size = (try file.stat(self.io)).size;
if (scan.valid_end < full_size) {
log.warn("recovered log {s} has a torn tail: truncating {d} -> {d} bytes", .{ path, full_size, scan.valid_end });
file.setLength(self.io, scan.valid_end) catch |err| {
log.warn("failed to truncate torn log tail: {s}", .{@errorName(err)});
};
}
if (scan.last_seq) |ls| {
self.cur_seq = ls + 1;
log.info("recovered seq from disk: last_seq={d}", .{ls});
} else {
self.cur_seq = if (seq_start > 0) seq_start else 1;
}
// append at the end of the last valid record (post-truncation)
self.current_file_pos = scan.valid_end;
self.current_file = file;
self.current_file_path = try self.allocator.dupe(u8, path);
} else {
try self.initLogFile();
}
}
fn initLogFile(self: *DiskPersist) !void {
try self.createLogFile(self.cur_seq);
}
fn createLogFile(self: *DiskPersist, start_seq: u64) !void {
var name_buf: [64]u8 = undefined;
const name = std.fmt.bufPrint(&name_buf, "evts-{d}", .{start_seq}) catch unreachable;
const path = try self.allocator.dupe(u8, name);
errdefer self.allocator.free(path);
const file = try self.dir.createFile(self.io, name, .{ .exclusive = true, .read = true });
errdefer {
file.close(self.io);
self.dir.deleteFile(self.io, name) catch {};
}
// register in Postgres
_ = try self.db.exec(
"INSERT INTO log_file_refs (path, seq_start) VALUES ($1, $2)",
.{ name, @as(i64, @intCast(start_seq)) },
);
if (self.current_file) |f| f.close(self.io);
if (self.current_file_path) |p| self.allocator.free(p);
self.current_file = file;
self.current_file_pos = 0;
self.current_file_path = path;
self.event_counter = 0;
}
pub fn flush(self: *DiskPersist) !void {
self.mutex.lockUncancelable(self.io);
defer self.mutex.unlock(self.io);
try self.flushLocked();
}
fn flushLocked(self: *DiskPersist) !void {
if (self.evtbuf.items.len == 0) return;
// write buffered bytes to current file
const file = self.current_file orelse return;
try file.writePositionalAll(self.io, self.outbuf.items, self.current_file_pos);
self.current_file_pos += self.outbuf.items.len;
// clear write buffer (bytes are now durable)
self.outbuf.clearRetainingCapacity();
// these events are now durable — publish the watermark so the broadcaster
// fiber may emit them. evtbuf is append-ordered, so the last entry holds
// the highest seq just written. this is the only place committed_seq moves.
if (self.evtbuf.items.len > 0) {
self.committed_seq.store(self.evtbuf.items[self.evtbuf.items.len - 1].seq, .release);
}
// free job data
for (self.evtbuf.items) |job| {
self.allocator.free(job.data);
}
self.evtbuf.clearRetainingCapacity();
// check if we need to rotate
if (self.event_counter >= self.events_per_file) {
self.createLogFile(self.cur_seq) catch |err| {
log.err("flush: log rotation failed: {s}", .{@errorName(err)});
};
}
}
fn flushLoop(self: *DiskPersist) void {
while (self.alive.load(.acquire)) {
// sleep for flush interval (replaces timedWait — Io.Condition has no timed variant)
self.io.sleep(Io.Duration.fromMilliseconds(@intCast(default_flush_interval_ms)), .awake) catch {};
self.mutex.lockUncancelable(self.io);
self.flushLocked() catch |err| log.err("event flush failed: {s}", .{@errorName(err)});
self.mutex.unlock(self.io);
}
}
};
// --- playback types ---
pub const PlaybackEntry = struct {
seq: u64,
kind: u32,
uid: u64,
data: []const u8, // payload only (owned by caller)
};
const LogFileRef = struct {
path: []const u8,
seq_start: u64,
};
const GcRef = struct {
id: i64,
path: []const u8,
};
const GcSizeRef = struct {
id: i64,
path: []const u8,
size: u64,
};
// --- file-level operations ---
/// positional reads through a block buffer. the event log is parsed one
/// 28-byte header at a time, and reading each header with its own pread cost
/// a syscall per record (a 4,096-entry replay chunk was ~10^4 syscalls, with
/// the seek from the start of the file on top). a block refill amortizes
/// that to a syscall per `block_size` bytes.
const PositionalReader = struct {
const block_size: usize = 256 * 1024;
file: Io.File,
io: Io,
buf: []u8,
base: u64 = 0,
len: usize = 0,
fn init(allocator: Allocator, file: Io.File, io: Io) !PositionalReader {
return .{ .file = file, .io = io, .buf = try allocator.alloc(u8, block_size) };
}
fn deinit(self: *PositionalReader, allocator: Allocator) void {
allocator.free(self.buf);
}
/// read `dst.len` bytes at `pos`. returns the number of bytes read; short
/// at end of file (or at a torn tail still being written).
fn readAt(self: *PositionalReader, pos: u64, dst: []u8) !usize {
if (dst.len > self.buf.len) {
return try self.file.readPositionalAll(self.io, dst, pos);
}
if (pos < self.base or pos + dst.len > self.base + self.len) {
self.base = pos;
self.len = try self.file.readPositionalAll(self.io, self.buf, pos);
}
const off: usize = @intCast(pos - self.base);
const n = @min(dst.len, self.len - off);
@memcpy(dst[0..n], self.buf[off .. off + n]);
return n;
}
};
fn readEventsFrom(allocator: Allocator, file: Io.File, io: Io, since: u64, result: *std.ArrayListUnmanaged(PlaybackEntry), limits: PlaybackLimits, bytes_used: *usize) !void {
const file_size = (try file.stat(io)).size;
var reader = try PositionalReader.init(allocator, file, io);
defer reader.deinit(allocator);
// if since > 0, scan to the right position
var pos: u64 = 0;
if (since > 0) {
pos = seekToSeq(&reader, since, file_size) catch return;
}
// read events
while (pos + header_size <= file_size) {
if (result.items.len >= limits.max_entries or bytes_used.* >= limits.max_bytes) return;
var hdr_buf: [header_size]u8 = undefined;
const n = reader.readAt(pos, &hdr_buf) catch break;
if (n < header_size) break;
pos += header_size;
const hdr = EvtHeader.decode(&hdr_buf);
// skip taken down / rebased events
if (hdr.flags & (EvtFlags.takedown | EvtFlags.rebased) != 0) {
pos += hdr.len;
continue;
}
if (hdr.seq <= since) {
pos += hdr.len;
continue;
}
// read payload
const data = allocator.alloc(u8, hdr.len) catch break;
const read_n = reader.readAt(pos, data) catch {
allocator.free(data);
break;
};
if (read_n < hdr.len) {
allocator.free(data);
break;
}
pos += hdr.len;
result.append(allocator, .{
.seq = hdr.seq,
.kind = hdr.kind,
.uid = hdr.uid,
.data = data,
}) catch {
allocator.free(data);
break;
};
bytes_used.* += data.len;
}
}
/// scan file headers to find position of first event with seq > target
fn seekToSeq(reader: *PositionalReader, target: u64, file_size: u64) !u64 {
var pos: u64 = 0;
while (pos + header_size <= file_size) {
var hdr_buf: [header_size]u8 = undefined;
const n = try reader.readAt(pos, &hdr_buf);
if (n < header_size) break;
const hdr = EvtHeader.decode(&hdr_buf);
if (hdr.seq > target) {
return pos;
}
pos += header_size + hdr.len;
}
return pos;
}
const ScanResult = struct {
/// highest seq among fully-valid records, or null if the file has none
last_seq: ?u64,
/// byte offset just past the last fully-valid record. anything beyond is a
/// torn/corrupt tail (a partial final write after an unclean kill) and must
/// be truncated before appending, or it desyncs every future scan.
valid_end: u64,
};
/// scan a log file for the last valid sequence number. each record is validated
/// (known kind, plausible length, body fully present, AND a seq that matches the
/// dense monotonic space the writer guarantees) before being accepted; the scan
/// stops at the first record it can't trust rather than following a garbage
/// length and silently mis-recovering. this is what bounds a torn tail to the
/// torn record itself — see docs/incident-2026-05-31-seq-rewind.md.
///
/// `seq_start` is the file's declared starting seq (from its name / log_file_refs
/// row). the writer assigns seqs densely (seq = cur_seq; cur_seq += 1) and a file
/// begins at exactly seq_start, so the first record must have seq == seq_start and
/// every subsequent record seq == prev + 1. a record whose seq breaks that chain
/// is a torn/garbage tail even if its kind+len happen to look plausible — this is
/// what stops `scanForLastSeq` from accepting CBOR payload bytes as a seq and
/// relocating the whole sequencer into a junk space (the 2026-06 quintillion-seq
/// incident). pass 0 to skip the seq-chain check (genuinely unknown start).
fn scanForLastSeq(file: Io.File, io: Io, seq_start: u64) !ScanResult {
const file_size = (try file.stat(io)).size;
var last_seq: ?u64 = null;
var pos: u64 = 0;
while (pos + header_size <= file_size) {
var hdr_buf: [header_size]u8 = undefined;
const n = try file.readPositionalAll(io, &hdr_buf, pos);
if (n < header_size) break;
const hdr = EvtHeader.decode(&hdr_buf);
// a record we can't trust — unknown kind, implausible length, or a body
// that runs past EOF (partial final write) — marks the torn tail. stop;
// `pos` is the end of the last good record.
const valid_kind = hdr.kind >= @backingInt(EvtKind.commit) and hdr.kind <= @backingInt(EvtKind.sync);
if (!valid_kind or hdr.len > max_record_len or pos + header_size + hdr.len > file_size) break;
// seq must follow the dense monotonic chain: the first record == seq_start,
// each next == prev + 1. a break here is a torn/garbage tail.
if (seq_start > 0) {
const expected = if (last_seq) |ls| ls + 1 else seq_start;
if (hdr.seq != expected) break;
}
last_seq = hdr.seq;
pos += header_size + hdr.len;
}
return .{ .last_seq = last_seq, .valid_end = pos };
}
/// set takedown flag + zero payload for all events belonging to uid
fn mutateUserEventsInFile(file: Io.File, io: Io, uid: u64) !void {
const file_size = (try file.stat(io)).size;
var pos: u64 = 0;
while (pos + header_size <= file_size) {
var hdr_buf: [header_size]u8 = undefined;
const n = try file.readPositionalAll(io, &hdr_buf, pos);
if (n < header_size) break;
const hdr = EvtHeader.decode(&hdr_buf);
if (hdr.uid == uid and (hdr.flags & EvtFlags.takedown) == 0) {
// set takedown flag
const new_flags = hdr.flags | EvtFlags.takedown;
var flags_buf: [4]u8 = undefined;
std.mem.writeInt(u32, &flags_buf, new_flags, .little);
try file.writePositionalAll(io, &flags_buf, pos);
// zero the payload
const payload_start = pos + header_size;
var zeros: [4096]u8 = @splat(0);
var remaining: u64 = hdr.len;
var write_pos: u64 = payload_start;
while (remaining > 0) {
const chunk: usize = @min(remaining, zeros.len);
try file.writePositionalAll(io, zeros[0..chunk], write_pos);
write_pos += chunk;
remaining -= chunk;
}
}
pos += header_size + hdr.len;
}
}
// === tests ===
test "header encode/decode roundtrip" {
const hdr = EvtHeader{
.flags = 0,
.kind = @backingInt(EvtKind.commit),
.len = 1234,
.uid = 42,
.seq = 99999,
};
var buf: [header_size]u8 = undefined;
hdr.encode(&buf);
const decoded = EvtHeader.decode(&buf);
try std.testing.expectEqual(hdr.flags, decoded.flags);
try std.testing.expectEqual(hdr.kind, decoded.kind);
try std.testing.expectEqual(hdr.len, decoded.len);
try std.testing.expectEqual(hdr.uid, decoded.uid);
try std.testing.expectEqual(hdr.seq, decoded.seq);
}
test "header is little-endian" {
const hdr = EvtHeader{ .flags = 0, .kind = 1, .len = 256, .uid = 0, .seq = 1 };
var buf: [header_size]u8 = undefined;
hdr.encode(&buf);
// len=256 in LE is 0x00 0x01 0x00 0x00
try std.testing.expectEqual(@as(u8, 0x00), buf[8]);
try std.testing.expectEqual(@as(u8, 0x01), buf[9]);
}
test "scanForLastSeq stops at a torn tail" {
// regression for the seq-rewind incident (docs/incident-2026-05-31-seq-rewind.md):
// an interrupted flush leaves a partial final record. recovery must stop at the
// last fully-written record, not trust the torn record's length and desync.
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
var file = try tmp.dir.createFile(io, "log", .{ .truncate = true, .read = true });
defer file.close(io);
var pos: u64 = 0;
const writeRecord = struct {
fn go(f: Io.File, tio: Io, at: *u64, seq: u64, payload: []const u8) !void {
var hbuf: [header_size]u8 = undefined;
(EvtHeader{ .flags = 0, .kind = @backingInt(EvtKind.commit), .len = @intCast(payload.len), .uid = 0, .seq = seq }).encode(&hbuf);
try f.writePositionalAll(tio, &hbuf, at.*);
at.* += header_size;
try f.writePositionalAll(tio, payload, at.*);
at.* += payload.len;
}
}.go;
// two complete records
try writeRecord(file, io, &pos, 1, "aa");
try writeRecord(file, io, &pos, 2, "bbbb");
const valid_end = pos;
// torn record: header claims a payload of 999 bytes, only 2 are present
var hbuf: [header_size]u8 = undefined;
(EvtHeader{ .flags = 0, .kind = @backingInt(EvtKind.commit), .len = 999, .uid = 0, .seq = 3 }).encode(&hbuf);
try file.writePositionalAll(io, &hbuf, pos);
try file.writePositionalAll(io, "xy", pos + header_size);
const scan = try scanForLastSeq(file, io, 1);
try std.testing.expectEqual(@as(?u64, 2), scan.last_seq); // torn record 3 rejected
try std.testing.expectEqual(valid_end, scan.valid_end); // stops at end of record 2
}
test "scanForLastSeq rejects a record with a garbage seq" {
// regression for the 2026-06 quintillion-seq incident: a torn tail left bytes
// that decode to a structurally-valid header (good kind, plausible len, body
// present) but whose seq field is actually CBOR payload (0x6363a382...). the
// old scan trusted that seq and relocated the whole sequencer; the seq-chain
// check must reject it so recovery resumes in the real space.
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
var file = try tmp.dir.createFile(io, "log", .{ .truncate = true, .read = true });
defer file.close(io);
var pos: u64 = 0;
const writeRecord = struct {
fn go(f: Io.File, tio: Io, at: *u64, seq: u64, payload: []const u8) !void {
var hbuf: [header_size]u8 = undefined;
(EvtHeader{ .flags = 0, .kind = @backingInt(EvtKind.commit), .len = @intCast(payload.len), .uid = 0, .seq = seq }).encode(&hbuf);
try f.writePositionalAll(tio, &hbuf, at.*);
at.* += header_size;
try f.writePositionalAll(tio, payload, at.*);
at.* += payload.len;
}
}.go;
// file declares seq_start = 100; two good records continue the dense chain
try writeRecord(file, io, &pos, 100, "aa");
try writeRecord(file, io, &pos, 101, "bbbb");
const valid_end = pos;
// a record that is structurally valid (good kind, small len, body present)
// but whose seq is the garbage CBOR value — breaks the chain (expected 102)
try writeRecord(file, io, &pos, 0x6363a38200000000, "zz");
const scan = try scanForLastSeq(file, io, 100);
try std.testing.expectEqual(@as(?u64, 101), scan.last_seq); // garbage-seq record rejected
try std.testing.expectEqual(valid_end, scan.valid_end); // stops at end of record 101
// and a file whose very first record is already garbage recovers nothing —
// resumeLog then falls back to seq_start rather than the junk value
var f2 = try tmp.dir.createFile(io, "log2", .{ .truncate = true, .read = true });
defer f2.close(io);
var p2: u64 = 0;
try writeRecord(f2, io, &p2, 0x6363a38200000000, "aa");
const scan2 = try scanForLastSeq(f2, io, 100);
try std.testing.expectEqual(@as(?u64, null), scan2.last_seq);
try std.testing.expectEqual(@as(u64, 0), scan2.valid_end);
}
fn requireDatabaseUrl() ![]const u8 {
return getenv("DATABASE_URL") orelse return error.SkipZigTest;
}
/// per-test database isolation. the test binary's runner executes tests
/// concurrently against one server, and the relay tables (log_file_refs /
/// account / host) are global — so sharing one database lets tests see each
/// other's rows (playback(0) returns another test's events). each test gets its
/// own freshly-created database instead, dropped on teardown. unique across
/// processes (pid) and within one (counter).
var test_db_counter: std.atomic.Value(u32) = .{ .raw = 0 };
pub const TestDb = struct {
base_url: []const u8,
name: []u8,
url: []u8,
pub fn create(base_url: []const u8) !TestDb {
const pid: u64 = @intCast(std.c.getpid());
const n = test_db_counter.fetchAdd(1, .monotonic);
const name = try std.fmt.allocPrint(std.testing.allocator, "zlay_test_{d}_{d}", .{ pid, n });
errdefer std.testing.allocator.free(name);
{
const uri = std.Uri.parse(base_url) catch return error.InvalidDatabaseUrl;
const pool = try pg.Pool.initUri(std.testing.io, std.testing.allocator, uri, .{ .size = 1 });
defer pool.deinit();
var buf: [128]u8 = undefined;
// name is generated from pid/counter — no injection risk
const stmt = try std.fmt.bufPrint(&buf, "CREATE DATABASE \"{s}\"", .{name});
_ = try pool.exec(stmt, .{});
}
const slash = std.mem.lastIndexOfScalar(u8, base_url, '/') orelse return error.InvalidDatabaseUrl;
const url = try std.fmt.allocPrint(std.testing.allocator, "{s}/{s}", .{ base_url[0..slash], name });
return .{ .base_url = base_url, .name = name, .url = url };
}
pub fn destroy(self: *TestDb) void {
const uri = std.Uri.parse(self.base_url) catch return;
const pool = pg.Pool.initUri(std.testing.io, std.testing.allocator, uri, .{ .size = 1 }) catch return;
defer pool.deinit();
var buf: [128]u8 = undefined;
const stmt = std.fmt.bufPrint(&buf, "DROP DATABASE IF EXISTS \"{s}\" WITH (FORCE)", .{self.name}) catch return;
_ = pool.exec(stmt, .{}) catch {};
std.testing.allocator.free(self.name);
std.testing.allocator.free(self.url);
}
};
test "persist and playback" {
const base_url = try requireDatabaseUrl();
var tdb = try TestDb.create(base_url);
defer tdb.destroy();
const database_url = tdb.url;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
const dir_path = try tmpDirRealPath(std.testing.allocator, tmp);
defer std.testing.allocator.free(dir_path);
var dp = try DiskPersist.init(std.testing.allocator, dir_path, database_url, 5, std.testing.io);
defer dp.deinit();
// persist some events (sync flush, no background thread)
const seq1 = try dp.persist(.commit, 1, "payload-one");
const seq2 = try dp.persist(.identity, 2, "payload-two");
const seq3 = try dp.persist(.commit, 1, "payload-three");
try std.testing.expectEqual(@as(u64, 1), seq1);
try std.testing.expectEqual(@as(u64, 2), seq2);
try std.testing.expectEqual(@as(u64, 3), seq3);
// flush manually
{
dp.mutex.lockUncancelable(dp.io);
defer dp.mutex.unlock(dp.io);
try dp.flushLocked();
}
// playback from cursor=0 → all events
var entries: std.ArrayListUnmanaged(PlaybackEntry) = .empty;
defer {
for (entries.items) |e| std.testing.allocator.free(e.data);
entries.deinit(std.testing.allocator);
}
try dp.playback(0, std.testing.allocator, &entries, .{});
try std.testing.expectEqual(@as(usize, 3), entries.items.len);
try std.testing.expectEqualStrings("payload-one", entries.items[0].data);
try std.testing.expectEqual(@as(u64, 1), entries.items[0].seq);
try std.testing.expectEqualStrings("payload-two", entries.items[1].data);
try std.testing.expectEqualStrings("payload-three", entries.items[2].data);
}
test "worker failure preserves upstream cursor and successful retry reaches disk" {
const cbor = @import("zat").cbor;
const broadcaster = @import("broadcaster.zig");
const validator_mod = @import("validator.zig");
const worker = @import("frame_worker.zig");
const ingest_ack = @import("ingest_ack.zig");
var tdb = try TestDb.create(try requireDatabaseUrl());
defer tdb.destroy();
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
const dir_path = try tmpDirRealPath(std.testing.allocator, tmp);
defer std.testing.allocator.free(dir_path);
var dp = try DiskPersist.init(std.testing.allocator, dir_path, tdb.url, 2, std.testing.io);
defer dp.deinit();
var shutdown = std.atomic.Value(bool){ .raw = false };
var bc = broadcaster.Broadcaster.init(std.testing.allocator, std.testing.io, &shutdown);
defer bc.deinit();
var validator = validator_mod.Validator.init(std.testing.allocator, &bc.stats, std.testing.io);
defer validator.deinit();
var progress: ingest_ack.Progress = .{};
var cursor: ?u64 = 10;
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
defer arena.deinit();
const header = try cbor.encodeAlloc(arena.allocator(), .{ .map = &.{
.{ .key = "op", .value = .{ .unsigned = 1 } },
.{ .key = "t", .value = .{ .text = "#identity" } },
} });
const payload = try cbor.encodeAlloc(arena.allocator(), .{ .map = &.{
.{ .key = "did", .value = .{ .text = "did:plc:cursor-test" } },
.{ .key = "seq", .value = .{ .unsigned = 11 } },
} });
for (0..2) |attempt| {
var failing = std.testing.FailingAllocator.init(std.testing.allocator, .{ .fail_index = 0 });
dp.allocator = if (attempt == 0) failing.allocator() else std.testing.allocator;
defer dp.allocator = std.testing.allocator;
var work: worker.FrameWork = .{
.data = try std.mem.concat(std.testing.allocator, u8, &.{ header, payload }),
.hostname = try std.testing.allocator.dupe(u8, "example.com"),
.host_id = 1,
.allocator = std.testing.allocator,
.io = std.testing.io,
.bc = &bc,
.validator = &validator,
.persist = &dp,
.collection_index = null,
.resyncer = null,
.receipt = try progress.reserve(std.testing.io, 11, &cursor),
};
bc.stats.pool_queued_bytes.store(work.data.len, .monotonic);
worker.processFrame(&work);
if (attempt == 0) {
try std.testing.expectError(error.FrameProcessingFailed, progress.drain(&cursor, true));
try std.testing.expectEqual(@as(?u64, 10), cursor);
try std.testing.expectEqual(@as(u64, 1), dp.cur_seq);
progress.reset();
} else {
try progress.drain(&cursor, true);
try std.testing.expectEqual(@as(?u64, 11), cursor);
try std.testing.expectEqual(@as(u64, 1), dp.committed_seq.load(.acquire));
const scan = try scanForLastSeq(dp.current_file.?, std.testing.io, 1);
try std.testing.expectEqual(@as(?u64, 1), scan.last_seq);
}
}
}
test "persist allocation failures preserve sequence and buffered records" {
const Case = struct {
fn run(allocator: Allocator) !void {
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
const file = try tmp.dir.createFile(std.testing.io, "events", .{ .read = true });
defer file.close(std.testing.io);
var dp: DiskPersist = .{
.allocator = allocator,
.dir_path = "",
.dir = tmp.dir,
.db = undefined,
.current_file = file,
.did_cache = undefined,
.host_id_cache = undefined,
.io = std.testing.io,
};
defer {
for (dp.evtbuf.items) |job| allocator.free(job.data);
dp.evtbuf.deinit(allocator);
dp.outbuf.deinit(allocator);
}
const seq = dp.persist(.commit, 1, "payload") catch |err| {
try std.testing.expectEqual(@as(u64, 1), dp.cur_seq);
try std.testing.expectEqual(@as(usize, 0), dp.evtbuf.items.len);
try std.testing.expectEqual(@as(usize, 0), dp.outbuf.items.len);
return err;
};
try std.testing.expectEqual(@as(u64, 1), seq);
try dp.flushLocked();
const scan = try scanForLastSeq(file, std.testing.io, 1);
try std.testing.expectEqual(@as(?u64, 1), scan.last_seq);
}
};
try std.testing.checkAllAllocationFailures(std.testing.allocator, Case.run, .{});
}
test "failed threshold write retains ownership and bounds pending events until retry" {
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
const file = try tmp.dir.createFile(std.testing.io, "events", .{ .read = true });
defer file.close(std.testing.io);
const read_only = try tmp.dir.openFile(std.testing.io, "events", .{});
defer read_only.close(std.testing.io);
var dp: DiskPersist = .{
.allocator = std.testing.allocator,
.dir_path = "",
.dir = tmp.dir,
.db = undefined,
.current_file = read_only,
.did_cache = undefined,
.host_id_cache = undefined,
.io = std.testing.io,
};
defer {
for (dp.evtbuf.items) |job| std.testing.allocator.free(job.data);
dp.evtbuf.deinit(std.testing.allocator);
dp.outbuf.deinit(std.testing.allocator);
}
for (0..default_flush_threshold) |_| _ = try dp.persist(.commit, 1, "payload");
try std.testing.expectEqual(@as(u64, 0), dp.committed_seq.load(.acquire));
try std.testing.expectError(error.NotOpenForWriting, dp.persist(.commit, 1, "next"));
try std.testing.expectEqual(default_flush_threshold, dp.evtbuf.items.len);
try std.testing.expectEqual(default_flush_threshold + 1, dp.cur_seq);
dp.current_file = file;
try dp.flush();
const scan = try scanForLastSeq(file, std.testing.io, 1);
try std.testing.expectEqual(@as(?u64, default_flush_threshold), scan.last_seq);
try std.testing.expectEqual(@as(usize, 0), dp.evtbuf.items.len);
}
test "failed rotation preserves the active file and retries after database recovery" {
var tdb = try TestDb.create(try requireDatabaseUrl());
defer tdb.destroy();
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
const dir_path = try tmpDirRealPath(std.testing.allocator, tmp);
defer std.testing.allocator.free(dir_path);
var dp = try DiskPersist.init(std.testing.allocator, dir_path, tdb.url, 2, std.testing.io);
defer dp.deinit();
_ = try dp.persist(.commit, 1, "before");
try dp.flushLocked();
const old_pos = dp.current_file_pos;
{
var failing = std.testing.FailingAllocator.init(std.testing.allocator, .{ .fail_index = 0 });
dp.allocator = failing.allocator();
defer dp.allocator = std.testing.allocator;
try std.testing.expectError(error.OutOfMemory, dp.createLogFile(2));
}
try std.testing.expectEqualStrings("evts-1", dp.current_file_path.?);
try tmp.dir.createDir(std.testing.io, "evts-2", .default_dir);
if (dp.createLogFile(2)) |_| return error.ExpectedRotationFailure else |_| {}
try std.testing.expectEqualStrings("evts-1", dp.current_file_path.?);
try std.testing.expectEqual(old_pos, dp.current_file_pos);
try tmp.dir.deleteDir(std.testing.io, "evts-2");
_ = try dp.db.exec("ALTER TABLE log_file_refs ADD CONSTRAINT reject_rotation CHECK (seq_start < 2)", .{});
if (dp.createLogFile(2)) |_| return error.ExpectedRotationFailure else |_| {}
try std.testing.expectEqualStrings("evts-1", dp.current_file_path.?);
try std.testing.expectError(error.FileNotFound, tmp.dir.openFile(std.testing.io, "evts-2", .{}));
_ = try dp.persist(.commit, 1, "after");
try dp.flushLocked();
const scan = try scanForLastSeq(dp.current_file.?, std.testing.io, 1);
try std.testing.expectEqual(@as(?u64, 2), scan.last_seq);
_ = try dp.db.exec("ALTER TABLE log_file_refs DROP CONSTRAINT reject_rotation", .{});
try dp.createLogFile(3);
_ = try dp.persist(.commit, 1, "rotated");
try dp.flushLocked();
var entries: std.ArrayListUnmanaged(PlaybackEntry) = .empty;
defer {
for (entries.items) |entry| std.testing.allocator.free(entry.data);
entries.deinit(std.testing.allocator);
}
try dp.playback(0, std.testing.allocator, &entries, .{});
try std.testing.expectEqual(@as(usize, 3), entries.items.len);
}
test "gc deletes expired segments and their refs, keeps the current file" {
// written 2026-08-27, when production surfaced 10 weeks of never-pruned
// segments (132G/150G): gc() had no test at all. this exercises the whole
// path against real postgres — the $1::interval bind, the row decode
// (whose `nextUnsafe() catch null` would silently turn a query error into
// "nothing expired"), the ref-before-file delete order, and the
// current-file skip.
const base_url = try requireDatabaseUrl();
var tdb = try TestDb.create(base_url);
defer tdb.destroy();
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
const dir_path = try tmpDirRealPath(std.testing.allocator, tmp);
defer std.testing.allocator.free(dir_path);
var dp = try DiskPersist.init(std.testing.allocator, dir_path, tdb.url, 5, std.testing.io);
defer dp.deinit();
dp.events_per_file = 2; // force rotation quickly
dp.retention_hours = 1;
// 5 events at 2/file, flushing each (rotation happens at flush end once
// the counter passes events_per_file) → at least 3 segments registered
for (0..5) |i| {
var payload_buf: [16]u8 = undefined;
const payload = std.fmt.bufPrint(&payload_buf, "payload-{d}", .{i}) catch unreachable;
_ = try dp.persist(.commit, 1, payload);
dp.mutex.lockUncancelable(dp.io);
defer dp.mutex.unlock(dp.io);
try dp.flushLocked();
}
var ref_count: i64 = 0;
{
var result = try dp.db.query("SELECT count(*) FROM log_file_refs", .{});
defer result.deinit();
while (result.nextUnsafe() catch null) |r| ref_count = r.get(i64, 0);
}
try std.testing.expect(ref_count >= 3);
// nothing is expired yet: gc must be a no-op
try dp.gc();
var after_noop: i64 = 0;
{
var result = try dp.db.query("SELECT count(*) FROM log_file_refs", .{});
defer result.deinit();
while (result.nextUnsafe() catch null) |r| after_noop = r.get(i64, 0);
}
try std.testing.expectEqual(ref_count, after_noop);
// age every ref past retention, then gc: everything but the current
// file (and its ref... the ref of the skipped file survives) must go
dp.retention_seconds = 60;
_ = try dp.db.exec("UPDATE log_file_refs SET created_at = now() - interval '2 minutes'", .{});
try dp.gc();
var survivors: std.ArrayListUnmanaged([]u8) = .empty;
defer {
for (survivors.items) |s| std.testing.allocator.free(s);
survivors.deinit(std.testing.allocator);
}
{
var result = try dp.db.query("SELECT path FROM log_file_refs ORDER BY seq_start", .{});
defer result.deinit();
while (result.nextUnsafe() catch null) |r| {
try survivors.append(std.testing.allocator, try std.testing.allocator.dupe(u8, r.get([]const u8, 0)));
}
}
// only the current file's ref remains
try std.testing.expectEqual(@as(usize, 1), survivors.items.len);
try std.testing.expectEqualStrings(dp.current_file_path.?, survivors.items[0]);
// expired files are gone from disk; the current file remains
var dir = try std.Io.Dir.cwd().openDir(std.testing.io, dir_path, .{ .iterate = true });
defer dir.close(std.testing.io);
var files_on_disk: usize = 0;
var it = dir.iterate();
while (try it.next(std.testing.io)) |entry| {
if (entry.kind != .file) continue;
if (!std.mem.startsWith(u8, entry.name, "evts-")) continue;
files_on_disk += 1;
try std.testing.expectEqualStrings(dp.current_file_path.?, entry.name);
}
try std.testing.expectEqual(@as(usize, 1), files_on_disk);
// orphan sweep: an unreferenced file below the retained window (the
// production 100G leak: row deleted, file delete failed) is removed;
// an unreferenced file at/above the retained floor is left alone
{
const old_orphan = try dir.createFile(std.testing.io, "evts-1", .{});
old_orphan.close(std.testing.io);
const new_unregistered = try dir.createFile(std.testing.io, "evts-999999", .{});
new_unregistered.close(std.testing.io);
}
try dp.gc();
try std.testing.expectError(error.FileNotFound, dir.openFile(std.testing.io, "evts-1", .{}));
const kept = try dir.openFile(std.testing.io, "evts-999999", .{});
kept.close(std.testing.io);
dir.deleteFile(std.testing.io, "evts-999999") catch {};
}
test "committed_seq advances only on durable flush" {
// regression for the seq-rewind incident (docs/incident-2026-05-31-seq-rewind.md):
// the broadcaster fiber only emits a frame once its seq <= committed_seq, and
// committed_seq advances ONLY here, after the durable write. so a frame can
// never reach consumers before it is on disk, and the seq recovered on restart
// can't trail an already-broadcast seq.
const base_url = try requireDatabaseUrl();
var tdb = try TestDb.create(base_url);
defer tdb.destroy();
const database_url = tdb.url;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
const dir_path = try tmpDirRealPath(std.testing.allocator, tmp);
defer std.testing.allocator.free(dir_path);
var dp = try DiskPersist.init(std.testing.allocator, dir_path, database_url, 5, std.testing.io);
defer dp.deinit();
// fresh log: nothing durable yet
try std.testing.expectEqual(@as(u64, 0), dp.committed_seq.load(.acquire));
// persist below the flush threshold — assigned seqs but NOT yet committed,
// so the broadcaster would hold all three back.
_ = try dp.persist(.commit, 1, "a");
_ = try dp.persist(.commit, 2, "b");
_ = try dp.persist(.commit, 3, "c");
try std.testing.expectEqual(@as(u64, 0), dp.committed_seq.load(.acquire));
// flush → committed_seq jumps to the highest durable seq, releasing all three
{
dp.mutex.lockUncancelable(dp.io);
defer dp.mutex.unlock(dp.io);
try dp.flushLocked();
}
try std.testing.expectEqual(@as(u64, 3), dp.committed_seq.load(.acquire));
}
test "playback with cursor" {
const base_url = try requireDatabaseUrl();
var tdb = try TestDb.create(base_url);
defer tdb.destroy();
const database_url = tdb.url;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
const dir_path = try tmpDirRealPath(std.testing.allocator, tmp);
defer std.testing.allocator.free(dir_path);
var dp = try DiskPersist.init(std.testing.allocator, dir_path, database_url, 5, std.testing.io);
defer dp.deinit();
_ = try dp.persist(.commit, 1, "a");
_ = try dp.persist(.commit, 2, "b");
_ = try dp.persist(.commit, 3, "c");
{
dp.mutex.lockUncancelable(dp.io);
defer dp.mutex.unlock(dp.io);
try dp.flushLocked();
}
// playback from cursor=2 → only seq 3
var entries: std.ArrayListUnmanaged(PlaybackEntry) = .empty;
defer {
for (entries.items) |e| std.testing.allocator.free(e.data);
entries.deinit(std.testing.allocator);
}
try dp.playback(2, std.testing.allocator, &entries, .{});
try std.testing.expectEqual(@as(usize, 1), entries.items.len);
try std.testing.expectEqual(@as(u64, 3), entries.items[0].seq);
try std.testing.expectEqualStrings("c", entries.items[0].data);
}
test "playback respects entry and byte limits, resumable by cursor" {
// regression for the 2026-08-06 consumer-flap OOM: an old cursor must
// not materialize the whole backlog in one playback call.
const base_url = try requireDatabaseUrl();
var tdb = try TestDb.create(base_url);
defer tdb.destroy();
const database_url = tdb.url;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
const dir_path = try tmpDirRealPath(std.testing.allocator, tmp);
defer std.testing.allocator.free(dir_path);
var dp = try DiskPersist.init(std.testing.allocator, dir_path, database_url, 5, std.testing.io);
defer dp.deinit();
for (0..10) |_| _ = try dp.persist(.commit, 1, "0123456789");
{
dp.mutex.lockUncancelable(dp.io);
defer dp.mutex.unlock(dp.io);
try dp.flushLocked();
}
// max_entries caps the chunk; resuming from the last seq gets the rest
var chunk: std.ArrayListUnmanaged(PlaybackEntry) = .empty;
defer {
for (chunk.items) |e| std.testing.allocator.free(e.data);
chunk.deinit(std.testing.allocator);
}
try dp.playback(0, std.testing.allocator, &chunk, .{ .max_entries = 4 });
try std.testing.expectEqual(@as(usize, 4), chunk.items.len);
try std.testing.expectEqual(@as(u64, 4), chunk.items[3].seq);
var rest: std.ArrayListUnmanaged(PlaybackEntry) = .empty;
defer {
for (rest.items) |e| std.testing.allocator.free(e.data);
rest.deinit(std.testing.allocator);
}
try dp.playback(chunk.items[3].seq, std.testing.allocator, &rest, .{ .max_entries = 100 });
try std.testing.expectEqual(@as(usize, 6), rest.items.len);
try std.testing.expectEqual(@as(u64, 5), rest.items[0].seq);
try std.testing.expectEqual(@as(u64, 10), rest.items[5].seq);
// max_bytes caps the chunk: 10-byte payloads, 25-byte budget → the
// check runs before each read, so the 3rd entry crosses the budget
// and the 4th is never read
var byte_chunk: std.ArrayListUnmanaged(PlaybackEntry) = .empty;
defer {
for (byte_chunk.items) |e| std.testing.allocator.free(e.data);
byte_chunk.deinit(std.testing.allocator);
}
try dp.playback(0, std.testing.allocator, &byte_chunk, .{ .max_bytes = 25 });
try std.testing.expectEqual(@as(usize, 3), byte_chunk.items.len);
}
test "seq recovery after reinit" {
const base_url = try requireDatabaseUrl();
var tdb = try TestDb.create(base_url);
defer tdb.destroy();
const database_url = tdb.url;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
const dir_path = try tmpDirRealPath(std.testing.allocator, tmp);
defer std.testing.allocator.free(dir_path);
// write some events
{
var dp = try DiskPersist.init(std.testing.allocator, dir_path, database_url, 5, std.testing.io);
defer dp.deinit();
_ = try dp.persist(.commit, 1, "x");
_ = try dp.persist(.commit, 2, "y");
_ = try dp.persist(.account, 3, "z");
dp.mutex.lockUncancelable(dp.io);
defer dp.mutex.unlock(dp.io);
try dp.flushLocked();
}
// reinit — should recover seq
{
var dp = try DiskPersist.init(std.testing.allocator, dir_path, database_url, 5, std.testing.io);
defer dp.deinit();
try std.testing.expectEqual(@as(u64, 3), dp.lastSeq().?);
const seq4 = try dp.persist(.commit, 1, "w");
try std.testing.expectEqual(@as(u64, 4), seq4);
}
}
test "takedown zeros payload" {
const base_url = try requireDatabaseUrl();
var tdb = try TestDb.create(base_url);
defer tdb.destroy();
const database_url = tdb.url;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
const dir_path = try tmpDirRealPath(std.testing.allocator, tmp);
defer std.testing.allocator.free(dir_path);
var dp = try DiskPersist.init(std.testing.allocator, dir_path, database_url, 5, std.testing.io);
defer dp.deinit();
_ = try dp.persist(.commit, 42, "secret-data");
_ = try dp.persist(.commit, 99, "other-data");
{
dp.mutex.lockUncancelable(dp.io);
defer dp.mutex.unlock(dp.io);
try dp.flushLocked();
}
// take down user 42
try dp.takeDownUser(42);
// playback should skip user 42's events
var entries: std.ArrayListUnmanaged(PlaybackEntry) = .empty;
defer {
for (entries.items) |e| std.testing.allocator.free(e.data);
entries.deinit(std.testing.allocator);
}
try dp.playback(0, std.testing.allocator, &entries, .{});
try std.testing.expectEqual(@as(usize, 1), entries.items.len);
try std.testing.expectEqualStrings("other-data", entries.items[0].data);
}
test "uidForDid assigns and caches UIDs" {
const base_url = try requireDatabaseUrl();
var tdb = try TestDb.create(base_url);
defer tdb.destroy();
const database_url = tdb.url;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
const dir_path = try tmpDirRealPath(std.testing.allocator, tmp);
defer std.testing.allocator.free(dir_path);
var dp = try DiskPersist.init(std.testing.allocator, dir_path, database_url, 5, std.testing.io);
defer dp.deinit();
// first call creates the account
const uid1 = try dp.uidForDid("did:plc:alice");
try std.testing.expect(uid1 > 0);
// same DID returns same UID (from cache)
const uid1_again = try dp.uidForDid("did:plc:alice");
try std.testing.expectEqual(uid1, uid1_again);
// different DID gets a different UID
const uid2 = try dp.uidForDid("did:plc:bob");
try std.testing.expect(uid2 > 0);
try std.testing.expect(uid1 != uid2);
}
test "accountActivity distinguishes local takedown from stale upstream status" {
const base_url = try requireDatabaseUrl();
var tdb = try TestDb.create(base_url);
defer tdb.destroy();
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
const dir_path = try tmpDirRealPath(std.testing.allocator, tmp);
defer std.testing.allocator.free(dir_path);
var dp = try DiskPersist.init(std.testing.allocator, dir_path, tdb.url, 5, std.testing.io);
defer dp.deinit();
const uid = try dp.uidForDid("did:plc:migrant");
// fresh account: fully active
var activity = try dp.accountActivity(uid);
try std.testing.expect(activity.local_ok and activity.upstream_ok);
try std.testing.expect(activity.active());
// the migration-stale shape: locally active, upstream deactivated.
// this is the only combination that may trigger a StatusChecker re-check.
try dp.updateAccountUpstreamStatus(uid, "deactivated");
activity = try dp.accountActivity(uid);
try std.testing.expect(activity.local_ok);
try std.testing.expect(!activity.upstream_ok);
try std.testing.expect(!activity.active());
try std.testing.expect(!try dp.isAccountActive(uid));
// recovery: the checker writes the PDS's answer back
try dp.updateAccountUpstreamStatus(uid, "active");
activity = try dp.accountActivity(uid);
try std.testing.expect(activity.active());
try std.testing.expect(try dp.isAccountActive(uid));
// local takedown: local_ok false — must never look like the re-check shape
_ = try dp.db.exec("UPDATE account SET status = 'takendown' WHERE uid = $1", .{@as(i64, @intCast(uid))});
activity = try dp.accountActivity(uid);
try std.testing.expect(!activity.local_ok);
try std.testing.expect(activity.upstream_ok);
try std.testing.expect(!activity.active());
// unknown uid: inactive on both axes
activity = try dp.accountActivity(999_999);
try std.testing.expect(!activity.local_ok and !activity.upstream_ok);
}
test "getAccountState retains rev-only legacy state" {
const base_url = try requireDatabaseUrl();
var tdb = try TestDb.create(base_url);
defer tdb.destroy();
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
const dir_path = try tmpDirRealPath(std.testing.allocator, tmp);
defer std.testing.allocator.free(dir_path);
var dp = try DiskPersist.init(std.testing.allocator, dir_path, tdb.url, 5, std.testing.io);
defer dp.deinit();
const uid = try dp.uidForDid("did:plc:revonly");
try std.testing.expect(try dp.updateAccountState(uid, "3k2abcdefghij", "", ""));
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
defer arena.deinit();
const state = (try dp.getAccountState(uid, arena.allocator())).?;
try std.testing.expectEqualStrings("3k2abcdefghij", state.rev);
try std.testing.expectEqualStrings("", state.data_cid);
}
test "host_id cache stays coherent with setAccountHostId" {
const base_url = try requireDatabaseUrl();
var tdb = try TestDb.create(base_url);
defer tdb.destroy();
const database_url = tdb.url;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
const dir_path = try tmpDirRealPath(std.testing.allocator, tmp);
defer std.testing.allocator.free(dir_path);
var dp = try DiskPersist.init(std.testing.allocator, dir_path, database_url, 5, std.testing.io);
defer dp.deinit();
const uid = try dp.uidForDid("did:plc:hostcache");
// unset host_id reads as 0 and is not cached (transient pre-assignment state)
try std.testing.expectEqual(@as(u64, 0), try dp.getAccountHostId(uid));
// write-through: set populates the cache, read returns it
try dp.setAccountHostId(uid, 42);
try std.testing.expectEqual(@as(u64, 42), try dp.getAccountHostId(uid));
// a host change updates the cache in place — no stale read
try dp.setAccountHostId(uid, 99);
try std.testing.expectEqual(@as(u64, 99), try dp.getAccountHostId(uid));
// clearing to 0 evicts the entry; read falls through to the DB (0)
try dp.setAccountHostId(uid, 0);
try std.testing.expectEqual(@as(u64, 0), try dp.getAccountHostId(uid));
// value survives a cache miss: clear the cache, read repopulates from DB
try dp.setAccountHostId(uid, 7);
_ = dp.host_id_cache.remove(&DiskPersist.uidKey(uid));
try std.testing.expectEqual(@as(u64, 7), try dp.getAccountHostId(uid));
}
test "uidForDid survives reinit" {
const base_url = try requireDatabaseUrl();
var tdb = try TestDb.create(base_url);
defer tdb.destroy();
const database_url = tdb.url;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
const dir_path = try tmpDirRealPath(std.testing.allocator, tmp);
defer std.testing.allocator.free(dir_path);
var uid1: u64 = undefined;
{
var dp = try DiskPersist.init(std.testing.allocator, dir_path, database_url, 5, std.testing.io);
defer dp.deinit();
uid1 = try dp.uidForDid("did:plc:carol");
}
// reinit — UID should be the same from database
{
var dp = try DiskPersist.init(std.testing.allocator, dir_path, database_url, 5, std.testing.io);
defer dp.deinit();
const uid1_again = try dp.uidForDid("did:plc:carol");
try std.testing.expectEqual(uid1, uid1_again);
}
}
test "takedown with real UIDs" {
const base_url = try requireDatabaseUrl();
var tdb = try TestDb.create(base_url);
defer tdb.destroy();
const database_url = tdb.url;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
const dir_path = try tmpDirRealPath(std.testing.allocator, tmp);
defer std.testing.allocator.free(dir_path);
var dp = try DiskPersist.init(std.testing.allocator, dir_path, database_url, 5, std.testing.io);
defer dp.deinit();
const alice_uid = try dp.uidForDid("did:plc:alice");
const bob_uid = try dp.uidForDid("did:plc:bob");
_ = try dp.persist(.commit, alice_uid, "alice-post");
_ = try dp.persist(.commit, bob_uid, "bob-post");
_ = try dp.persist(.commit, alice_uid, "alice-post-2");
{
dp.mutex.lockUncancelable(dp.io);
defer dp.mutex.unlock(dp.io);
try dp.flushLocked();
}
// take down alice
try dp.takeDownUser(alice_uid);
// playback should only have bob's event
var entries: std.ArrayListUnmanaged(PlaybackEntry) = .empty;
defer {
for (entries.items) |e| std.testing.allocator.free(e.data);
entries.deinit(std.testing.allocator);
}
try dp.playback(0, std.testing.allocator, &entries, .{});
try std.testing.expectEqual(@as(usize, 1), entries.items.len);
try std.testing.expectEqualStrings("bob-post", entries.items[0].data);
try std.testing.expectEqual(bob_uid, entries.items[0].uid);
}
fn tmpDirRealPath(allocator: Allocator, tmp: std.testing.TmpDir) ![]const u8 {
var buf: [Io.Dir.max_path_bytes]u8 = undefined;
const n = try tmp.dir.realPath(std.testing.io, &buf);
return try allocator.dupe(u8, buf[0..n]);
}
test "playback does not take the persist mutex" {
// regression: 2026-10-18 — playback held DiskPersist.mutex for every
// replay chunk, which blocked persist and flush and froze committed_seq.
const base_url = try requireDatabaseUrl();
var tdb = try TestDb.create(base_url);
defer tdb.destroy();
const database_url = tdb.url;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
const dir_path = try tmpDirRealPath(std.testing.allocator, tmp);
defer std.testing.allocator.free(dir_path);
var dp = try DiskPersist.init(std.testing.allocator, dir_path, database_url, 5, std.testing.io);
defer dp.deinit();
for (0..20) |_| _ = try dp.persist(.commit, 1, "payload");
try dp.flush();
// hold the mutex from this thread; playback on another thread must finish anyway
dp.mutex.lockUncancelable(dp.io);
const Reader = struct {
fn run(d: *DiskPersist, out: *std.ArrayListUnmanaged(PlaybackEntry), ok: *std.atomic.Value(bool)) void {
d.playback(5, std.testing.allocator, out, .{}) catch return;
ok.store(true, .release);
}
};
var entries: std.ArrayListUnmanaged(PlaybackEntry) = .empty;
defer {
for (entries.items) |e| std.testing.allocator.free(e.data);
entries.deinit(std.testing.allocator);
}
var ok = std.atomic.Value(bool){ .raw = false };
const t = try std.Thread.spawn(.{}, Reader.run, .{ &dp, &entries, &ok });
t.join();
dp.mutex.unlock(dp.io);
try std.testing.expect(ok.load(.acquire));
try std.testing.expectEqual(@as(usize, 15), entries.items.len);
try std.testing.expectEqual(@as(u64, 6), entries.items[0].seq);
try std.testing.expectEqual(@as(u64, 20), entries.items[14].seq);
}
test "buffered playback reads records that straddle block boundaries" {
const base_url = try requireDatabaseUrl();
var tdb = try TestDb.create(base_url);
defer tdb.destroy();
const database_url = tdb.url;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
const dir_path = try tmpDirRealPath(std.testing.allocator, tmp);
defer std.testing.allocator.free(dir_path);
var dp = try DiskPersist.init(std.testing.allocator, dir_path, database_url, 5, std.testing.io);
defer dp.deinit();
// 700 records of 1,000 bytes: ~720 KB, crossing the 256 KiB block
// boundary several times, with one payload larger than a whole block
const payload = try std.testing.allocator.alloc(u8, 1000);
defer std.testing.allocator.free(payload);
for (payload, 0..) |*b, i| b.* = @intCast(i % 251);
const big = try std.testing.allocator.alloc(u8, PositionalReader.block_size + 17);
defer std.testing.allocator.free(big);
@memset(big, 0xAB);
const count: usize = 700;
for (0..count) |i| {
if (i == 350) {
_ = try dp.persist(.commit, 2, big);
} else {
payload[0] = @intCast(i % 256);
_ = try dp.persist(.commit, 1, payload);
}
}
try dp.flush();
var entries: std.ArrayListUnmanaged(PlaybackEntry) = .empty;
defer {
for (entries.items) |e| std.testing.allocator.free(e.data);
entries.deinit(std.testing.allocator);
}
try dp.playback(100, std.testing.allocator, &entries, .{});
try std.testing.expectEqual(count - 100, entries.items.len);
for (entries.items, 0..) |e, j| {
const i = j + 100;
try std.testing.expectEqual(@as(u64, i + 1), e.seq);
if (i == 350) {
try std.testing.expectEqual(big.len, e.data.len);
try std.testing.expectEqual(@as(u8, 0xAB), e.data[e.data.len - 1]);
} else {
try std.testing.expectEqual(@as(usize, 1000), e.data.len);
try std.testing.expectEqual(@as(u8, @intCast(i % 256)), e.data[0]);
try std.testing.expectEqual(payload[999], e.data[999]);
}
}
}
test "account commit migration preserves roots and updates both CIDs atomically" {
const base_url = try requireDatabaseUrl();
var tdb = try TestDb.create(base_url);
defer tdb.destroy();
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
const dir_path = try tmpDirRealPath(std.testing.allocator, tmp);
defer std.testing.allocator.free(dir_path);
{
var old = try DiskPersist.init(std.testing.allocator, dir_path, tdb.url, 5, std.testing.io);
defer old.deinit();
_ = try old.db.exec("ALTER TABLE account_repo DROP COLUMN commit_cid CASCADE", .{});
const uid = try old.uidForDid("did:plc:migration");
_ = try old.db.exec("INSERT INTO account_repo (uid, rev, commit_data_cid) VALUES ($1, $2, $3)", .{ @as(i64, @intCast(uid)), "3my46tkfps52u", "old-mst-root" });
}
var dp = try DiskPersist.init(std.testing.allocator, dir_path, tdb.url, 5, std.testing.io);
defer dp.deinit();
const uid = try dp.uidForDid("did:plc:migration");
{
var row = (try dp.db.rowUnsafe("SELECT rev, commit_data_cid, commit_cid FROM account_repo WHERE uid = $1", .{@as(i64, @intCast(uid))})).?;
defer row.deinit() catch {};
try std.testing.expectEqualStrings("3my46tkfps52u", row.get([]const u8, 0));
try std.testing.expectEqualStrings("old-mst-root", row.get([]const u8, 1));
try std.testing.expectEqualStrings("", row.get([]const u8, 2));
}
try std.testing.expect(try dp.updateAccountState(uid, "3my46tkfps62u", "new-mst-root", "new-commit"));
try std.testing.expect(!try dp.updateAccountState(uid, "3my46tkfps52u", "stale-root", "stale-commit"));
try std.testing.expect(!try dp.updateAccountState(uid, "3my46tkfps62u", "conflicting-root", "conflicting-commit"));
{
var row = (try dp.db.rowUnsafe("SELECT rev, commit_data_cid, commit_cid FROM account_repo WHERE uid = $1", .{@as(i64, @intCast(uid))})).?;
defer row.deinit() catch {};
try std.testing.expectEqualStrings("3my46tkfps62u", row.get([]const u8, 0));
try std.testing.expectEqualStrings("new-mst-root", row.get([]const u8, 1));
try std.testing.expectEqualStrings("new-commit", row.get([]const u8, 2));
}
}
test "live-only sequences do not buffer history or touch replay storage" {
var dp: DiskPersist = .{
.allocator = std.testing.failing_allocator,
.dir_path = "",
.dir = undefined,
.db = undefined,
.did_cache = undefined,
.host_id_cache = undefined,
.history_enabled = false,
.io = std.testing.io,
};
try std.testing.expectEqual(@as(u64, 1), try dp.persist(.commit, 1, "first"));
try std.testing.expectEqual(@as(u64, 2), try dp.persist(.commit, 1, "second"));
try dp.flush();
try std.testing.expectEqual(@as(u64, 2), dp.committed_seq.load(.acquire));
try std.testing.expectEqual(@as(usize, 0), dp.evtbuf.items.len);
try std.testing.expectEqual(@as(usize, 0), dp.outbuf.items.len);
var entries: std.ArrayListUnmanaged(PlaybackEntry) = .empty;
try std.testing.expectError(error.ReplayDisabled, dp.playback(0, std.testing.allocator, &entries, .{}));
}