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
//! archive — disk-backed jss segments with rotation, sealing, and recovery
//!
//! owns <data-dir>/segments: assigns instance-local seqs (starting at 1;
//! 0 is the "before beginning" sentinel), appends events to an active
//! segment, flushes blocks to disk with fsync, seals + rotates at a byte
//! budget, and on startup recovers by truncating any torn tail and sealing
//! the leftover active file (upstream's recovery-scan lesson:
//! docs/lessons-from-zlay.md #3 — validate on recovery, truncate torn
//! tails, never trust an unvalidated record).
//!
//! durability watermark: committed_seq is the last local seq whose block
//! is fsynced. anything above it exists only in memory. sealed headers and
//! block indexes are held by the same refreshable resident manifest as
//! upstream; files remain the source of truth for frame bytes.
const std = @import("std");
const segment = @import("segment.zig");
const writer_mod = @import("segment_writer.zig");
const metrics = @import("../runtime/metrics.zig");
const observability = @import("../runtime/observability.zig");
const manifest_mod = @import("manifest.zig");
const segment_io = @import("segment_io.zig");
const Io = std.Io;
const Allocator = std.mem.Allocator;
const log = std.log.scoped(.stream);
pub const default_max_segment_bytes: usize = 256 * 1024 * 1024;
const AsyncFlushJob = struct {
id: u64 = 0,
prepared: *writer_mod.PreparedBlock,
frame: ?[]u8 = null,
failure: ?anyerror = null,
compressed: Io.Event = .unset,
};
const AsyncFlushQueue = struct {
buffer: []*AsyncFlushJob,
head: usize = 0,
len: usize = 0,
closed: bool = false,
mu: Io.Mutex = .init,
not_empty: Io.Condition = .init,
not_full: Io.Condition = .init,
fn put(self: *AsyncFlushQueue, io: Io, job: *AsyncFlushJob) void {
self.mu.lockUncancelable(io);
defer self.mu.unlock(io);
while (self.len == self.buffer.len and !self.closed)
self.not_full.waitUncancelable(io, &self.mu);
if (self.closed) unreachable;
self.buffer[(self.head + self.len) % self.buffer.len] = job;
self.len += 1;
self.not_empty.signal(io);
}
fn pop(self: *AsyncFlushQueue, io: Io) !*AsyncFlushJob {
self.mu.lockUncancelable(io);
defer self.mu.unlock(io);
while (self.len == 0 and !self.closed)
try self.not_empty.wait(io, &self.mu);
if (self.len == 0) return error.Closed;
const job = self.buffer[self.head];
self.head = (self.head + 1) % self.buffer.len;
self.len -= 1;
self.not_full.signal(io);
return job;
}
fn close(self: *AsyncFlushQueue, io: Io) void {
self.mu.lockUncancelable(io);
defer self.mu.unlock(io);
self.closed = true;
self.not_empty.broadcast(io);
self.not_full.broadcast(io);
}
};
const AsyncFlush = struct {
allocator: Allocator,
io: Io,
queue: AsyncFlushQueue,
group: Io.Group = .init,
next_job_id: u64 = 0,
next_commit_id: u64 = 0,
commit_mu: Io.Mutex = .init,
commit_ready: Io.Condition = .init,
admission_mu: Io.Mutex = .init,
outstanding_mu: Io.Mutex = .init,
outstanding_ready: Io.Condition = .init,
outstanding: usize = 0,
workers_active: std.atomic.Value(usize) = .init(0),
workers_peak: std.atomic.Value(usize) = .init(0),
fn init(allocator: Allocator, io: Io, worker_count: usize) !AsyncFlush {
std.debug.assert(worker_count > 0);
return .{
.allocator = allocator,
.io = io,
.queue = .{ .buffer = try allocator.alloc(*AsyncFlushJob, worker_count) },
};
}
fn start(self: *AsyncFlush, worker_count: usize) !void {
errdefer {
self.queue.close(self.io);
self.group.cancel(self.io);
}
for (0..worker_count) |_| try self.group.concurrent(self.io, workerLoop, .{self});
}
fn deinit(self: *AsyncFlush) void {
self.queue.close(self.io);
self.group.await(self.io) catch {};
self.allocator.free(self.queue.buffer);
}
fn assign(self: *AsyncFlush, job: *AsyncFlushJob) void {
job.id = self.next_job_id;
self.next_job_id += 1;
self.outstanding_mu.lockUncancelable(self.io);
self.outstanding += 1;
self.outstanding_mu.unlock(self.io);
}
fn submit(self: *AsyncFlush, job: *AsyncFlushJob) void {
self.queue.put(self.io, job);
}
fn workerLoop(self: *AsyncFlush) void {
while (self.queue.pop(self.io)) |job| {
const active = self.workers_active.fetchAdd(1, .acq_rel) + 1;
var peak = self.workers_peak.load(.acquire);
while (active > peak) {
peak = self.workers_peak.cmpxchgWeak(peak, active, .acq_rel, .acquire) orelse break;
}
job.frame = writer_mod.compressPrepared(self.allocator, job.prepared) catch |err| blk: {
job.failure = err;
break :blk null;
};
_ = self.workers_active.fetchSub(1, .acq_rel);
job.compressed.set(self.io);
} else |_| {}
}
fn waitTurn(self: *AsyncFlush, id: u64) void {
self.commit_mu.lockUncancelable(self.io);
while (self.next_commit_id != id)
self.commit_ready.waitUncancelable(self.io, &self.commit_mu);
}
fn finishTurn(self: *AsyncFlush) void {
self.next_commit_id += 1;
self.commit_ready.broadcast(self.io);
self.commit_mu.unlock(self.io);
self.outstanding_mu.lockUncancelable(self.io);
self.outstanding -= 1;
self.outstanding_ready.broadcast(self.io);
self.outstanding_mu.unlock(self.io);
}
fn drain(self: *AsyncFlush) void {
self.outstanding_mu.lockUncancelable(self.io);
defer self.outstanding_mu.unlock(self.io);
while (self.outstanding != 0)
self.outstanding_ready.waitUncancelable(self.io, &self.outstanding_mu);
}
};
pub const Archive = struct {
allocator: Allocator,
io: Io,
dir: Io.Dir,
manifest: manifest_mod.Manifest,
writer: writer_mod.ActiveWriter,
file: Io.File,
/// bytes of writer.buf already written to disk
file_written: usize = 0,
/// Number of compressed blocks included in the last successful fsync +
/// metadata-hook boundary. Metrics advance from this exact watermark.
synced_block_count: u32 = 0,
seg_index: u64 = 0,
next_seq: u64 = 1,
max_segment_bytes: usize = default_max_segment_bytes,
max_events_per_block: usize = writer_mod.default_max_events_per_block,
/// last local seq whose block is durable (fsynced)
committed_seq: std.atomic.Value(u64) = .init(0),
/// wall clock µs of the last disk sync; partial blocks flush after
/// flush_interval_us so quiet periods still become durable (upstream:
/// blocks flush at 4096 events OR 30s)
last_sync_us: i64 = 0,
flush_interval_us: i64 = 30 * std.time.us_per_s,
/// serializes the writer against the steady compactor's force-rotate;
/// uncontended in the single-writer steady state
mu: Io.Mutex = .init,
/// Serializes every in-place sealed-segment rewrite. Delete compaction and
/// timestamp import both take this lock around their mutating pass so two
/// tmp+fsync+rename transactions can never overwrite one another's result.
/// It is deliberately separate from `mu`: live appends continue while a
/// cold segment pass is running.
rewrite_mu: Io.Mutex = .init,
/// upstream's OnAppend hook: runs under `mu` with the stamped event,
/// so a force-rotate returns only after every sealed row was observed
/// (the live-tombstone ordering guarantee)
on_append_ctx: ?*anyopaque = null,
on_append: ?*const fn (*anyopaque, segment.Event) anyerror!void = null,
/// Upstream's timestamp Stamper: called under `mu`, before sequence
/// assignment and buffering. It mutates the caller's row so archive bytes
/// and immediate subscriber delivery use the identical display timestamp.
stamper_ctx: ?*anyopaque = null,
stamper: ?*const fn (*anyopaque, *segment.Event) anyerror!void = null,
/// Called after segment bytes are fsynced but before committed_seq is
/// published. The hook must synchronously commit metadata describing
/// those bytes; failure keeps the durability watermark behind them.
on_durable_ctx: ?*anyopaque = null,
on_durable: ?*const fn (*anyopaque, u64, u64) anyerror!void = null,
stats: ?*metrics.Stats = null,
/// Bootstrap-only ordered compression pipeline. Steady-state writes stay
/// synchronous, matching upstream's BackfillAsyncFlushWorkers scope.
async_flush: ?AsyncFlush = null,
/// A failed append/flush may leave a partial in-memory transaction. Never
/// attempt a graceful flush afterward; recovery trusts only complete
/// frames already on disk and truncates a torn suffix.
poisoned: bool = false,
pub const ColdSnapshot = struct {
allocator: Allocator,
summaries: []manifest_mod.Summary,
active_index: u64,
active_length: usize,
active_start: usize,
pub fn deinit(self: *ColdSnapshot) void {
self.allocator.free(self.summaries);
self.* = undefined;
}
};
/// Atomically pair the sealed manifest with the active segment's durable
/// byte boundary. A later rotation is harmless: the old active filename
/// becomes that same index's sealed filename, while it was not yet present
/// in `summaries` and the captured prefix remains byte-stable.
pub fn coldSnapshot(self: *Archive, allocator: Allocator, from_seq: u64) !ColdSnapshot {
self.mu.lockUncancelable(self.io);
defer self.mu.unlock(self.io);
const summaries = try self.manifest.all(allocator);
errdefer allocator.free(summaries);
// Prepared async blocks have no file offsets yet; committed blocks
// may still exceed the metadata-accepted fsync boundary.
const blocks = self.writer.builder.infos.items[0..self.writer.block_count];
var lo: usize = 0;
var hi: usize = blocks.len;
while (lo < hi) {
const mid = lo + (hi - lo) / 2;
if (blocks[mid].max_seq < from_seq) lo = mid + 1 else hi = mid;
}
const start = if (lo < blocks.len) @min(blocks[lo].offset, self.file_written) else self.file_written;
return .{
.allocator = allocator,
.summaries = summaries,
.active_index = self.seg_index,
.active_length = self.file_written,
.active_start = @intCast(start),
};
}
pub fn init(allocator: Allocator, io: Io, data_dir: []const u8) !Archive {
return initWithStats(allocator, io, data_dir, null);
}
pub fn initWithStats(allocator: Allocator, io: Io, data_dir: []const u8, stats: ?*metrics.Stats) !Archive {
var buf: [256]u8 = undefined;
const seg_path = try std.fmt.bufPrint(&buf, "{s}/segments", .{data_dir});
var dir = try Io.Dir.cwd().createDirPathOpen(io, seg_path, .{ .open_options = .{ .iterate = true } });
errdefer dir.close(io);
var a: Archive = .{
.allocator = allocator,
.io = io,
.dir = dir,
.manifest = undefined,
.writer = undefined,
.file = undefined,
.stats = stats,
};
const resumed_active = try a.recover();
errdefer if (resumed_active) {
a.writer.deinit();
a.file.close(io);
};
// Recovery only advances next_seq from bytes that are already sealed
// and fsynced. Publish that durable floor immediately; otherwise the
// watermark (and /status) falsely reports zero until the first new
// block happens to flush after a restart.
a.committed_seq.store(a.next_seq - 1, .release);
a.manifest = try manifest_mod.Manifest.init(allocator, io, dir, stats);
errdefer a.manifest.deinit();
if (!resumed_active) try a.openNextSegment();
return a;
}
pub fn deinit(self: *Archive) void {
if (self.async_flush) |*pipeline| {
pipeline.admission_mu.lockUncancelable(self.io);
pipeline.drain();
pipeline.deinit();
pipeline.admission_mu.unlock(self.io);
self.async_flush = null;
}
self.writer.deinit();
self.file.close(self.io);
self.manifest.deinit();
self.dir.close(self.io);
}
/// Enable upstream's bootstrap-only async compression path. A zero worker
/// count is the explicit synchronous mode.
pub fn enableAsyncFlush(self: *Archive, worker_count: usize) !void {
if (worker_count == 0) return;
if (self.async_flush != null) return error.AsyncFlushAlreadyEnabled;
self.async_flush = try AsyncFlush.init(self.allocator, self.io, worker_count);
errdefer {
self.async_flush.?.deinit();
self.async_flush = null;
}
try self.async_flush.?.start(worker_count);
}
/// Drain and remove the bootstrap pipeline before merge/steady state.
pub fn disableAsyncFlush(self: *Archive) void {
if (self.async_flush) |*pipeline| {
pipeline.admission_mu.lockUncancelable(self.io);
pipeline.drain();
pipeline.deinit();
pipeline.admission_mu.unlock(self.io);
self.async_flush = null;
}
}
/// Scan the directory, recover the durable seq floor, and resume the
/// highest active segment in place. Returns true when `writer` and `file`
/// were initialized from that active segment.
fn recover(self: *Archive) !bool {
var max_index: ?u64 = null;
var it = self.dir.iterate();
while (try it.next(self.io)) |entry| {
const idx = parseSegmentIndex(entry.name) orelse continue;
if (max_index == null or idx > max_index.?) max_index = idx;
}
const candidate = max_index orelse return false;
var name_buf: [64]u8 = undefined;
const name = formatSegmentName(&name_buf, candidate);
const bytes = try self.dir.readFileAlloc(self.io, name, self.allocator, .limited(1 << 31));
defer self.allocator.free(bytes);
if (bytes.len < segment.header_size) return error.Truncated;
if (segment.Header.decode(bytes)) |_| {
const max_seq = try self.sealedMaxSeq(candidate, bytes);
self.next_seq = @max(self.next_seq, max_seq + 1);
self.seg_index = candidate + 1;
return false;
} else |err| switch (err) {
error.ActiveSegment => {},
else => return err,
}
var valid_end: usize = segment.header_size;
var max_seq: u64 = 0;
var event_count: u64 = 0;
var block_count: u32 = 0;
var torn = false;
var walk: writer_mod.ActiveIterator = .{ .bytes = bytes };
while (true) {
var block = (walk.next(self.allocator) catch |err| switch (err) {
error.Truncated => {
torn = true;
break;
},
else => return err,
}) orelse break;
defer block.deinit(self.allocator);
if (block.events.len == 0) break;
valid_end = walk.offset;
event_count += block.events.len;
block_count += 1;
for (block.events) |event| max_seq = @max(max_seq, event.seq);
}
if (max_seq == 0 and candidate > 0)
max_seq = try self.sealedFloorBefore(candidate);
var writer = try writer_mod.ActiveWriter.init(self.allocator);
errdefer writer.deinit();
writer.buf.clearRetainingCapacity();
try writer.buf.appendSlice(self.allocator, bytes[0..valid_end]);
writer.event_count = event_count;
writer.block_count = block_count;
try writer.rebuildIndex();
var file = try self.dir.openFile(self.io, name, .{ .mode = .read_write });
errdefer file.close(self.io);
if (torn or valid_end != bytes.len) {
try file.setLength(self.io, valid_end);
try segment_io.sync(&file, self.io);
}
self.writer = writer;
self.file = file;
self.file_written = valid_end;
self.synced_block_count = block_count;
self.seg_index = candidate;
self.next_seq = @max(self.next_seq, max_seq + 1);
if (self.stats) |stats|
stats.ingest_active_segment_bytes.store(valid_end - segment.header_size, .monotonic);
log.info("recovery: resumed active segment {s} at {d} valid bytes", .{ name, valid_end });
return true;
}
fn sealedMaxSeq(self: *Archive, candidate: u64, candidate_bytes: []const u8) !u64 {
var sealed = try segment.Sealed.parse(self.allocator, candidate_bytes);
defer sealed.deinit(self.allocator);
if (sealed.header.max_seq != 0) return sealed.header.max_seq;
if (candidate == 0) return 0;
return self.sealedFloorBefore(candidate);
}
fn sealedFloorBefore(self: *Archive, before: u64) !u64 {
var candidate = before;
while (candidate > 0) {
candidate -= 1;
var name_buf: [64]u8 = undefined;
const name = formatSegmentName(&name_buf, candidate);
const bytes = try self.dir.readFileAlloc(self.io, name, self.allocator, .limited(1 << 31));
defer self.allocator.free(bytes);
var sealed = try segment.Sealed.parse(self.allocator, bytes);
defer sealed.deinit(self.allocator);
if (sealed.header.max_seq != 0) return sealed.header.max_seq;
}
return 0;
}
fn openNextSegment(self: *Archive) !void {
self.writer = try writer_mod.ActiveWriter.init(self.allocator);
errdefer self.writer.deinit();
// the writer owns block granularity (it also sizes per-block blooms
// from this at seal); we sync to disk whenever the buffer grows
self.writer.max_events_per_block = self.max_events_per_block;
var name_buf: [64]u8 = undefined;
const name = formatSegmentName(&name_buf, self.seg_index);
self.file = try self.dir.createFile(self.io, name, .{ .truncate = true });
var file_open = true;
errdefer {
if (file_open) self.file.close(self.io);
self.dir.deleteFile(self.io, name) catch {};
}
try segment_io.writeStreamingAll(&self.file, self.io, self.writer.bytes());
try segment_io.sync(&self.file, self.io);
// The header is not durable if a power cut can still forget the new
// directory entry. Upstream syncs the parent after writer creation.
try segment_io.syncDir(self.dir, self.io);
file_open = false;
self.file_written = self.writer.bytes().len;
self.synced_block_count = 0;
if (self.stats) |stats| stats.ingest_active_segment_bytes.store(0, .monotonic);
}
/// append one event; assigns and returns its local seq. blocks flush to
/// disk at the block threshold; rotation seals at the byte budget.
pub fn append(self: *Archive, ev: segment.Event, now_us: i64) !u64 {
return self.appendTraced(ev, now_us, observability.root_context);
}
pub fn appendTraced(
self: *Archive,
ev: segment.Event,
now_us: i64,
trace_context: observability.Context,
) !u64 {
var rows = [_]segment.Event{ev};
return (try self.appendBatchTraced(&rows, now_us, trace_context)).?;
}
/// Append every row derived from one upstream event under one writer
/// lock. This is the transaction boundary relied on by frame seq
/// assignment (`first_seq + op_index`) and verifier checkpoints.
pub fn appendBatch(self: *Archive, events: []segment.Event, now_us: i64) !?u64 {
return self.appendBatchTraced(events, now_us, observability.root_context);
}
pub fn appendBatchTraced(
self: *Archive,
events: []segment.Event,
now_us: i64,
trace_context: observability.Context,
) !?u64 {
return self.appendBatchBeforeDurableTraced(events, now_us, null, null, trace_context);
}
/// Append rows and run `before_durable(first,last)` under the writer lock
/// before this append is allowed to trigger an fsync. The ingest writer
/// uses this to register cursor/verifier checkpoints against assigned
/// sequence numbers, closing the rows-before-metadata crash window.
pub fn appendBatchBeforeDurable(
self: *Archive,
events: []segment.Event,
now_us: i64,
hook_ctx: ?*anyopaque,
before_durable: ?*const fn (*anyopaque, u64, u64) anyerror!void,
) !?u64 {
return self.appendBatchBeforeDurableTraced(
events,
now_us,
hook_ctx,
before_durable,
observability.root_context,
);
}
pub fn appendBatchBeforeDurableTraced(
self: *Archive,
events: []segment.Event,
now_us: i64,
hook_ctx: ?*anyopaque,
before_durable: ?*const fn (*anyopaque, u64, u64) anyerror!void,
trace_context: observability.Context,
) !?u64 {
if (events.len == 0) return null;
if (self.async_flush != null)
return @as(?u64, try self.appendBatchAsync(events, now_us, hook_ctx, before_durable, trace_context));
return @as(?u64, try self.appendBatchSync(events, now_us, hook_ctx, before_durable, trace_context));
}
fn appendBatchSync(
self: *Archive,
events: []segment.Event,
now_us: i64,
hook_ctx: ?*anyopaque,
before_durable: ?*const fn (*anyopaque, u64, u64) anyerror!void,
trace_context: observability.Context,
) !u64 {
var flush_trace: ?observability.Span = null;
defer if (flush_trace) |*trace| trace.deinit();
const result = self.appendBatchSyncLocked(events, now_us, hook_ctx, before_durable, trace_context, &flush_trace);
if (flush_trace) |*trace| return trace.recordError(result);
return result;
}
fn appendBatchSyncLocked(
self: *Archive,
events: []segment.Event,
now_us: i64,
hook_ctx: ?*anyopaque,
before_durable: ?*const fn (*anyopaque, u64, u64) anyerror!void,
trace_context: observability.Context,
flush_trace: *?observability.Span,
) !u64 {
self.mu.lockUncancelable(self.io);
defer self.mu.unlock(self.io);
errdefer self.poisoned = true;
if (self.poisoned) return error.ArchivePoisoned;
const first_seq = self.next_seq;
for (events) |*ev| {
_ = try self.appendStampedLocked(ev);
}
if (before_durable) |hook| try hook(hook_ctx.?, first_seq, self.next_seq - 1);
if (self.writer.pending.items.len > 0 and now_us - self.last_sync_us >= self.flush_interval_us) {
flush_trace.* = observability.Span.start(
self.allocator,
"ingest",
"flushAndRotateLocked",
trace_context,
&.{},
);
try self.writer.flush(); // partial block: durability over block size
}
if (self.writer.bytes().len > self.file_written) {
if (flush_trace.* == null)
flush_trace.* = observability.Span.start(
self.allocator,
"ingest",
"flushAndRotateLocked",
trace_context,
&.{},
);
try self.syncToDisk(null);
self.last_sync_us = now_us;
if (self.writer.bytes().len >= self.max_segment_bytes)
try self.rotateLockedTraced(flush_trace.*.?.context());
}
if (flush_trace.*) |*trace| trace.succeed();
return first_seq;
}
fn appendBatchAsync(
self: *Archive,
events: []segment.Event,
now_us: i64,
hook_ctx: ?*anyopaque,
before_durable: ?*const fn (*anyopaque, u64, u64) anyerror!void,
trace_context: observability.Context,
) !u64 {
const pipeline = &self.async_flush.?;
pipeline.admission_mu.lockUncancelable(self.io);
var admission_locked = true;
defer if (admission_locked) pipeline.admission_mu.unlock(self.io);
var prepared: std.ArrayList(writer_mod.PreparedBlock) = .empty;
defer {
for (prepared.items) |*block| block.deinit(self.allocator);
prepared.deinit(self.allocator);
}
self.mu.lockUncancelable(self.io);
var writer_locked = true;
defer if (writer_locked) self.mu.unlock(self.io);
errdefer self.poisoned = true;
if (self.poisoned) return error.ArchivePoisoned;
const first_seq = self.next_seq;
for (events) |*ev| {
if (try self.appendStampedPreparedLocked(ev)) |block|
try prepared.append(self.allocator, block);
}
if (before_durable) |hook| try hook(hook_ctx.?, first_seq, self.next_seq - 1);
if (self.writer.pending.items.len > 0 and now_us - self.last_sync_us >= self.flush_interval_us) {
if (try self.writer.prepareFlush()) |block| try prepared.append(self.allocator, block);
}
const jobs = try self.allocator.alloc(AsyncFlushJob, prepared.items.len);
defer self.allocator.free(jobs);
for (jobs, prepared.items) |*job, *block| {
job.* = .{ .prepared = block };
pipeline.assign(job);
}
self.mu.unlock(self.io);
writer_locked = false;
for (jobs) |*job| pipeline.submit(job);
pipeline.admission_mu.unlock(self.io);
admission_locked = false;
try self.finishAsyncJobs(jobs, trace_context);
if (jobs.len > 0) {
self.mu.lockUncancelable(self.io);
self.last_sync_us = now_us;
self.mu.unlock(self.io);
}
try self.rotateIfFullAsync(trace_context);
return first_seq;
}
fn finishAsyncJobs(self: *Archive, jobs: []AsyncFlushJob, trace_context: observability.Context) !void {
const pipeline = &self.async_flush.?;
var first_error: ?anyerror = null;
for (jobs) |*job| {
job.compressed.waitUncancelable(self.io);
pipeline.waitTurn(job.id);
var trace = observability.Span.start(
self.allocator,
"ingest",
"commitAsyncFlush",
trace_context,
&.{},
);
defer trace.deinit();
self.mu.lockUncancelable(self.io);
if (job.failure) |err| {
trace.fail(err);
self.poisoned = true;
if (first_error == null) first_error = err;
} else if (self.poisoned) {
trace.fail(error.ArchivePoisoned);
if (first_error == null) first_error = error.ArchivePoisoned;
} else {
self.writer.commitPrepared(job.frame.?) catch |err| {
trace.fail(err);
self.poisoned = true;
if (first_error == null) first_error = err;
};
if (!self.poisoned) self.syncToDisk(job.prepared.max_seq) catch |err| {
trace.fail(err);
self.poisoned = true;
if (first_error == null) first_error = err;
};
}
self.mu.unlock(self.io);
if (job.frame) |frame| self.allocator.free(frame);
pipeline.finishTurn();
trace.succeed();
}
if (first_error) |err| return err;
}
fn rotateIfFullAsync(self: *Archive, trace_context: observability.Context) !void {
const pipeline = &self.async_flush.?;
self.mu.lockUncancelable(self.io);
const full = self.writer.bytes().len >= self.max_segment_bytes;
self.mu.unlock(self.io);
if (!full) return;
var trace = observability.Span.start(self.allocator, "ingest", "rotateIfFull", trace_context, &.{});
defer trace.deinit();
defer trace.succeed();
try trace.recordError(self.rotateDrained(pipeline, trace.context()));
}
fn rotateDrained(self: *Archive, pipeline: *AsyncFlush, trace_context: observability.Context) !void {
pipeline.admission_mu.lockUncancelable(self.io);
defer pipeline.admission_mu.unlock(self.io);
pipeline.drain();
self.mu.lockUncancelable(self.io);
defer self.mu.unlock(self.io);
errdefer self.poisoned = true;
if (self.poisoned) return error.ArchivePoisoned;
if (self.writer.bytes().len >= self.max_segment_bytes) try self.rotateLockedTraced(trace_context);
}
fn appendStampedLocked(self: *Archive, event: *segment.Event) !u64 {
if (self.stamper) |stamp| stamp(self.stamper_ctx.?, event) catch |err| {
if (self.stats) |stats| _ = stats.ingest_append_errors_total.fetchAdd(1, .monotonic);
return err;
};
const seq = self.next_seq;
event.seq = seq;
self.writer.append(event.*) catch |err| {
if (self.stats) |stats| _ = stats.ingest_append_errors_total.fetchAdd(1, .monotonic);
return err;
};
self.next_seq += 1;
if (self.stats) |stats| _ = stats.ingest_events_appended_total.fetchAdd(1, .monotonic);
if (self.on_append) |hook| try hook(self.on_append_ctx.?, event.*);
return seq;
}
fn appendStampedPreparedLocked(self: *Archive, event: *segment.Event) !?writer_mod.PreparedBlock {
if (self.stamper) |stamp| stamp(self.stamper_ctx.?, event) catch |err| {
if (self.stats) |stats| _ = stats.ingest_append_errors_total.fetchAdd(1, .monotonic);
return err;
};
const seq = self.next_seq;
event.seq = seq;
const prepared = self.writer.appendPrepared(event.*) catch |err| {
if (self.stats) |stats| _ = stats.ingest_append_errors_total.fetchAdd(1, .monotonic);
return err;
};
self.next_seq += 1;
if (self.stats) |stats| _ = stats.ingest_events_appended_total.fetchAdd(1, .monotonic);
if (self.on_append) |hook| try hook(self.on_append_ctx.?, event.*);
return prepared;
}
pub fn lastSeq(self: *Archive) u64 {
self.mu.lockUncancelable(self.io);
defer self.mu.unlock(self.io);
return self.next_seq - 1;
}
/// Heal resident metadata after an atomic segment rewrite. Returns the
/// number of missing or checksum-mismatched entries refreshed from disk.
pub fn reconcileManifest(self: *Archive) !u64 {
var reconciled: u64 = 0;
var it = self.dir.iterate();
while (try it.next(self.io)) |entry| {
const idx = parseSegmentIndex(entry.name) orelse continue;
var file = try self.dir.openFile(self.io, entry.name, .{});
defer file.close(self.io);
const header = segment.readHeaderFile(self.io, &file) catch |err| switch (err) {
error.ActiveSegment => continue,
else => return err,
};
if (self.manifest.segmentByIndex(idx)) |resident| {
if (resident.header.checksum == header.checksum) continue;
}
try self.manifest.refresh(self.dir, idx, true);
reconciled += 1;
}
return reconciled;
}
pub fn lockRewrites(self: *Archive) void {
self.rewrite_mu.lockUncancelable(self.io);
}
pub fn unlockRewrites(self: *Archive) void {
self.rewrite_mu.unlock(self.io);
}
/// compress + write any pending block, fsync, advance the watermark.
pub fn flushBlock(self: *Archive) !void {
return self.flushBlockTraced(observability.root_context);
}
pub fn flushBlockTraced(self: *Archive, trace_context: observability.Context) !void {
if (self.async_flush != null) return self.flushBlockAsync(trace_context);
var trace = observability.Span.start(self.allocator, "ingest", "flushAndRotateLocked", trace_context, &.{});
defer trace.deinit();
defer trace.succeed();
try trace.recordError(self.flushBlockSync());
}
fn flushBlockSync(self: *Archive) !void {
self.mu.lockUncancelable(self.io);
defer self.mu.unlock(self.io);
errdefer self.poisoned = true;
try self.flushBlockLocked();
}
fn flushBlockAsync(self: *Archive, trace_context: observability.Context) !void {
const pipeline = &self.async_flush.?;
pipeline.admission_mu.lockUncancelable(self.io);
var admission_locked = true;
defer if (admission_locked) pipeline.admission_mu.unlock(self.io);
self.mu.lockUncancelable(self.io);
var writer_locked = true;
defer if (writer_locked) self.mu.unlock(self.io);
errdefer self.poisoned = true;
if (self.poisoned) return error.ArchivePoisoned;
var prepared = (try self.writer.prepareFlush()) orelse return;
defer prepared.deinit(self.allocator);
var job: AsyncFlushJob = .{ .prepared = &prepared };
pipeline.assign(&job);
self.mu.unlock(self.io);
writer_locked = false;
pipeline.submit(&job);
pipeline.admission_mu.unlock(self.io);
admission_locked = false;
try self.finishAsyncJobs((&job)[0..1], trace_context);
}
fn flushBlockLocked(self: *Archive) !void {
try self.writer.flush();
if (self.writer.bytes().len > self.file_written) try self.syncToDisk(null);
}
fn syncToDisk(self: *Archive, async_durable_seq: ?u64) !void {
const all = self.writer.bytes();
try segment_io.writePositionalAll(&self.file, self.io, all[self.file_written..], self.file_written);
try segment_io.sync(&self.file, self.io);
if (self.stats) |stats| {
const flushed: u64 = self.writer.block_count - self.synced_block_count;
if (flushed > 0) _ = stats.ingest_blocks_flushed_total.fetchAdd(flushed, .monotonic);
stats.ingest_active_segment_bytes.store(all.len - segment.header_size, .monotonic);
}
self.synced_block_count = self.writer.block_count;
// everything up to (not including) pending is now durable
const durable_seq = async_durable_seq orelse self.next_seq - 1 - self.writer.pending.items.len;
if (self.on_durable) |hook| try hook(self.on_durable_ctx.?, durable_seq, durable_seq + 1);
// Advance only after metadata accepts the same boundary. If the hook
// fails, the next flush safely rewrites and re-syncs this suffix.
self.file_written = all.len;
self.committed_seq.store(durable_seq, .release);
}
/// seal the active segment (footer + finalized header) and open the
/// next. safe from another thread (the compactor's force-rotate).
pub fn rotate(self: *Archive) !void {
if (self.async_flush) |*pipeline| {
pipeline.admission_mu.lockUncancelable(self.io);
defer pipeline.admission_mu.unlock(self.io);
pipeline.drain();
}
self.mu.lockUncancelable(self.io);
defer self.mu.unlock(self.io);
errdefer self.poisoned = true;
try self.rotateLocked();
}
pub fn rotateTraced(self: *Archive, trace_context: observability.Context) !void {
if (self.async_flush) |*pipeline| {
pipeline.admission_mu.lockUncancelable(self.io);
defer pipeline.admission_mu.unlock(self.io);
pipeline.drain();
}
self.mu.lockUncancelable(self.io);
defer self.mu.unlock(self.io);
errdefer self.poisoned = true;
try self.rotateLockedTraced(trace_context);
}
fn rotateLocked(self: *Archive) !void {
const seal_start = Io.Timestamp.now(self.io, .awake);
try self.flushBlockLocked();
if (self.writer.block_count == 0) return; // nothing to seal
try self.writer.seal();
const all = self.writer.bytes();
try segment_io.writePositionalAll(&self.file, self.io, all[self.file_written..], self.file_written);
try segment_io.sync(&self.file, self.io);
try segment_io.writePositionalAll(&self.file, self.io, all[0..segment.header_size], 0);
try segment_io.sync(&self.file, self.io);
try self.manifest.refresh(self.dir, self.seg_index, false);
self.file.close(self.io);
self.writer.deinit();
if (self.stats) |stats| {
const elapsed_us = seal_start.durationTo(Io.Timestamp.now(self.io, .awake)).toMicroseconds();
stats.observeSegmentSeal(@intCast(@max(0, elapsed_us)));
_ = stats.ingest_segments_rotated_total.fetchAdd(1, .monotonic);
}
self.seg_index += 1;
try self.openNextSegment();
}
fn rotateLockedTraced(self: *Archive, trace_context: observability.Context) !void {
var trace = observability.Span.start(self.allocator, "ingest", "rotateLocked", trace_context, &.{});
defer trace.deinit();
defer trace.succeed();
try trace.recordError(self.rotateLocked());
trace.setAttribute(.{ .key = "active_idx", .value = .{ .int = @intCast(self.seg_index) } });
}
/// terminal cutover (bootstrap-live source at merge): seal the active
/// segment so the merge drain can read it; an empty active (header
/// only, no blocks) is deleted instead. the archive is unusable after.
pub fn sealAndClose(self: *Archive) !void {
self.mu.lockUncancelable(self.io);
defer self.mu.unlock(self.io);
errdefer self.poisoned = true;
// Cross the ordinary fsync + metadata hook before sealing. Bypassing
// syncToDisk here would make the final bootstrap-live block durable
// without its relay cursor and promoted verifier state.
const seal_start = Io.Timestamp.now(self.io, .awake);
try self.flushBlockLocked();
if (self.writer.block_count == 0) {
var name_buf: [64]u8 = undefined;
const name = formatSegmentName(&name_buf, self.seg_index);
self.file.close(self.io);
self.dir.deleteFile(self.io, name) catch {};
} else {
try self.writer.seal();
const all = self.writer.bytes();
try segment_io.writePositionalAll(&self.file, self.io, all[self.file_written..], self.file_written);
try segment_io.sync(&self.file, self.io);
try segment_io.writePositionalAll(&self.file, self.io, all[0..segment.header_size], 0);
try segment_io.sync(&self.file, self.io);
try self.manifest.refresh(self.dir, self.seg_index, false);
self.file.close(self.io);
if (self.stats) |stats| {
const elapsed_us = seal_start.durationTo(Io.Timestamp.now(self.io, .awake)).toMicroseconds();
stats.observeSegmentSeal(@intCast(@max(0, elapsed_us)));
}
}
self.writer.deinit();
self.manifest.deinit();
self.dir.close(self.io);
}
/// graceful shutdown: flush what's pending (the segment stays active —
/// upstream's steady-state shutdown doesn't seal either; recovery
/// handles it next start).
pub fn close(self: *Archive) void {
if (self.poisoned) {
log.err("archive is poisoned; refusing shutdown flush (recovery will truncate to the durable prefix)", .{});
return;
}
self.flushBlock() catch |err| log.warn("archive close flush failed: {s}", .{@errorName(err)});
}
};
pub const formatSegmentName = manifest_mod.formatSegmentName;
pub const parseSegmentIndex = manifest_mod.parseSegmentIndex;
// === tests ===
const testing = std.testing;
test "async flush pipeline performs four real zstd compressions concurrently" {
var threaded: Io.Threaded = .init(testing.allocator, .{});
defer threaded.deinit();
const io = threaded.io();
const worker_count = 4;
const block_bytes = 8 * 1024 * 1024;
var pipeline = try AsyncFlush.init(testing.allocator, io, worker_count);
defer pipeline.deinit();
try pipeline.start(worker_count);
var prepared: [worker_count]writer_mod.PreparedBlock = undefined;
defer for (&prepared) |*block| block.deinit(testing.allocator);
var jobs: [worker_count]AsyncFlushJob = undefined;
var prng = std.Random.DefaultPrng.init(0xfeed_face_dead_beef);
for (&prepared, 0..) |*block, i| {
const bytes = try testing.allocator.alloc(u8, block_bytes);
prng.random().bytes(bytes);
block.* = .{ .bytes = bytes, .max_seq = i + 1 };
}
for (&jobs, &prepared) |*job, *block| {
job.* = .{ .prepared = block };
pipeline.assign(job);
pipeline.submit(job);
}
for (&jobs) |*job| job.compressed.waitUncancelable(io);
try testing.expectEqual(@as(usize, worker_count), pipeline.workers_peak.load(.acquire));
for (&jobs) |*job| {
pipeline.waitTurn(job.id);
testing.allocator.free(job.frame.?);
pipeline.finishTurn();
}
}
test "async flush commits deterministic JSS blocks and durability in preparation order" {
var threaded: Io.Threaded = .init(testing.allocator, .{});
defer threaded.deinit();
const io = threaded.io();
var sync_tmp = testing.tmpDir(.{});
defer sync_tmp.cleanup();
var async_tmp = testing.tmpDir(.{});
defer async_tmp.cleanup();
var sync_path_buf: [Io.Dir.max_path_bytes]u8 = undefined;
var async_path_buf: [Io.Dir.max_path_bytes]u8 = undefined;
const sync_path_len = try sync_tmp.dir.realPath(io, &sync_path_buf);
const async_path_len = try async_tmp.dir.realPath(io, &async_path_buf);
var synchronous = try Archive.init(testing.allocator, io, sync_path_buf[0..sync_path_len]);
defer synchronous.deinit();
synchronous.writer.max_events_per_block = 1;
synchronous.max_events_per_block = 1;
var sync_rows: [8]segment.Event = undefined;
for (&sync_rows, 0..) |*event, i| event.* = testEvent(i + 1);
_ = try synchronous.appendBatch(&sync_rows, 1);
var asynchronous = try Archive.init(testing.allocator, io, async_path_buf[0..async_path_len]);
defer asynchronous.deinit();
const DurableReceipt = struct {
values: [8]u64 = undefined,
len: usize = 0,
fn record(ctx: *anyopaque, durable_seq: u64, _: u64) anyerror!void {
const self: *@This() = @ptrCast(@alignCast(ctx));
if (self.len == self.values.len) return error.TooManyDurableBoundaries;
self.values[self.len] = durable_seq;
self.len += 1;
}
};
var receipt: DurableReceipt = .{};
asynchronous.on_durable_ctx = &receipt;
asynchronous.on_durable = DurableReceipt.record;
asynchronous.writer.max_events_per_block = 1;
asynchronous.max_events_per_block = 1;
try asynchronous.enableAsyncFlush(4);
var async_rows: [8]segment.Event = undefined;
for (&async_rows, 0..) |*event, i| event.* = testEvent(i + 1);
_ = try asynchronous.appendBatch(&async_rows, 1);
try testing.expectEqualSlices(u8, synchronous.writer.bytes(), asynchronous.writer.bytes());
try testing.expectEqual(@as(u64, 8), asynchronous.committed_seq.load(.acquire));
try testing.expectEqual(@as(u32, 8), asynchronous.writer.block_count);
try testing.expectEqual(@as(usize, 8), receipt.len);
for (receipt.values, 1..) |durable_seq, expected|
try testing.expectEqual(@as(u64, expected), durable_seq);
asynchronous.disableAsyncFlush();
}
fn testEvent(seq_hint: u64) segment.Event {
return .{
.seq = 0, // archive assigns
.witnessed_at = @intCast(1_700_000_000_000_000 + seq_hint),
.indexed_at = 0,
.kind = .create,
.did = "did:plc:archivetestaccount",
.collection = "app.bsky.feed.post",
.rkey = "3l3qo2vuowo2b",
.rev = "3l3qo2vutsw2b",
.payload = "\xa1\x64\x74\x65\x78\x74\x62\x68\x69",
};
}
test "segment name round-trip" {
var buf: [64]u8 = undefined;
try testing.expectEqualStrings("seg_0000000000.jss", formatSegmentName(&buf, 0));
try testing.expectEqualStrings("seg_0000000001.jss", formatSegmentName(&buf, 1));
try testing.expectEqual(@as(?u64, 0), parseSegmentIndex("seg_0000000000.jss"));
try testing.expectEqual(@as(?u64, 35), parseSegmentIndex(formatSegmentName(&buf, 35)));
try testing.expectEqual(@as(?u64, null), parseSegmentIndex("seg_x.jss"));
}
test "active segment initialization faults remove the incomplete file" {
segment_io.disarm();
defer segment_io.disarm();
const cases = [_]struct { spec: []const u8, expected: anyerror }{
.{ .spec = "write:1:enospc", .expected = error.NoSpaceLeft },
.{ .spec = "sync:1:eio", .expected = error.InputOutput },
.{ .spec = "sync:2:eio", .expected = error.InputOutput },
};
for (cases) |case| {
var tmp = testing.tmpDir(.{});
defer tmp.cleanup();
var path_buf: [Io.Dir.max_path_bytes]u8 = undefined;
const path_len = try tmp.dir.realPath(testing.io, &path_buf);
try testing.expect(segment_io.armSpec(case.spec));
try testing.expectError(case.expected, Archive.init(testing.allocator, testing.io, path_buf[0..path_len]));
segment_io.disarm();
var segments = try tmp.dir.openDir(testing.io, "segments", .{ .iterate = true });
defer segments.close(testing.io);
var it = segments.iterate();
try testing.expectEqual(@as(?Io.Dir.Entry, null), try it.next(testing.io));
var recovered = try Archive.init(testing.allocator, testing.io, path_buf[0..path_len]);
recovered.deinit();
}
}
test "archive: append, rotate, recover across restart" {
var threaded: Io.Threaded = .init(testing.allocator, .{});
defer threaded.deinit();
const io = threaded.io();
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
// Archive.init uses cwd-relative paths; build one under the tmp dir
var path_buf: [std.Io.Dir.max_path_bytes]u8 = undefined;
const n = try tmp.dir.realPath(io, &path_buf);
const tmp_path = path_buf[0..n];
var data_dir_buf: [std.Io.Dir.max_path_bytes]u8 = undefined;
const data_dir = try std.fmt.bufPrint(&data_dir_buf, "{s}/arch", .{tmp_path});
var seal_stats: metrics.Stats = .{};
{
var a = try Archive.init(testing.allocator, io, data_dir);
defer a.deinit();
a.stats = &seal_stats;
a.max_events_per_block = 10;
a.writer.max_events_per_block = 10;
a.max_segment_bytes = 2000; // force rotation
var i: u64 = 0;
while (i < 100) : (i += 1) {
const seq = try a.append(testEvent(i), @intCast(i));
try testing.expectEqual(i + 1, seq);
}
try testing.expect(a.seg_index > 0); // rotated at least once
try testing.expect(a.committed_seq.load(.acquire) == 100);
try testing.expectEqual(
seal_stats.ingest_segments_rotated_total.load(.monotonic),
seal_stats.segment_seal_duration_count.load(.monotonic),
);
a.close();
// simulate crash: no seal of the current active segment
}
{
var a = try Archive.init(testing.allocator, io, data_dir);
defer a.deinit();
// Recovery resumes the leftover active segment; seqs continue with no
// reuse and the segment index does not churn merely because of restart.
const seq = try a.append(testEvent(999), 999);
try testing.expectEqual(@as(u64, 101), seq);
a.close();
}
{
var a = try Archive.init(testing.allocator, io, data_dir);
defer a.deinit();
// The next restart resumes the same segment with seq 101 durable.
try testing.expectEqual(@as(u64, 102), a.next_seq);
a.close();
}
{
var a = try Archive.init(testing.allocator, io, data_dir);
defer a.deinit();
// Repeated recovery must retain the active segment's high-water mark.
try testing.expectEqual(@as(u64, 102), a.next_seq);
const seq = try a.append(testEvent(1000), 1000);
try testing.expectEqual(@as(u64, 102), seq);
}
// Every sealed file parses, and the one active file contains the durable
// suffix that was resumed across all three restarts.
var dir = try Io.Dir.cwd().openDir(io, data_dir, .{});
defer dir.close(io);
var seg_dir = try dir.openDir(io, "segments", .{ .iterate = true });
defer seg_dir.close(io);
var it = seg_dir.iterate();
var sealed_count: usize = 0;
var active_count: usize = 0;
var total_events: u64 = 0;
while (try it.next(io)) |entry| {
const bytes = try seg_dir.readFileAlloc(io, entry.name, testing.allocator, .limited(1 << 20));
defer testing.allocator.free(bytes);
var sealed = segment.Sealed.parse(testing.allocator, bytes) catch |err| switch (err) {
error.ActiveSegment => {
active_count += 1;
var walk: writer_mod.ActiveIterator = .{ .bytes = bytes };
while (try walk.next(testing.allocator)) |block_value| {
var block = block_value;
defer block.deinit(testing.allocator);
total_events += block.events.len;
}
continue;
},
else => return err,
};
defer sealed.deinit(testing.allocator);
sealed_count += 1;
total_events += sealed.header.event_count;
}
try testing.expect(sealed_count >= 1);
try testing.expectEqual(@as(usize, 1), active_count);
try testing.expectEqual(@as(u64, 101), total_events); // event 102 is unflushed-active
}
test "archive recovery fails closed on a complete corrupt active frame" {
var threaded: Io.Threaded = .init(testing.allocator, .{});
defer threaded.deinit();
const io = threaded.io();
var tmp = testing.tmpDir(.{});
defer tmp.cleanup();
var root_buf: [Io.Dir.max_path_bytes]u8 = undefined;
const root_len = try tmp.dir.realPath(io, &root_buf);
var data_dir_buf: [Io.Dir.max_path_bytes]u8 = undefined;
const data_dir = try std.fmt.bufPrint(&data_dir_buf, "{s}/archive", .{root_buf[0..root_len]});
{
var archive = try Archive.init(testing.allocator, io, data_dir);
defer archive.deinit();
archive.max_events_per_block = 1;
archive.writer.max_events_per_block = 1;
_ = try archive.append(testEvent(1), 1);
archive.close();
}
var segment_path_buf: [Io.Dir.max_path_bytes]u8 = undefined;
const segment_path = try std.fmt.bufPrint(
&segment_path_buf,
"{s}/segments/seg_0000000000.jss",
.{data_dir},
);
var file = try Io.Dir.cwd().openFile(io, segment_path, .{ .mode = .read_write });
const stat = try file.stat(io);
const before = try testing.allocator.alloc(u8, @intCast(stat.size));
defer testing.allocator.free(before);
try testing.expectEqual(before.len, try file.readPositionalAll(io, before, 0));
// Preserve the complete length-prefixed frame while corrupting its zstd
// magic. This is not a torn tail and must never be truncated or sealed.
const frame_start = segment.header_size + 8;
before[frame_start] ^= 0xff;
try file.writePositionalAll(io, before[frame_start..][0..1], frame_start);
try file.sync(io);
file.close(io);
try testing.expectError(
error.DecompressFailed,
Archive.init(testing.allocator, io, data_dir),
);
const after = try Io.Dir.cwd().readFileAlloc(
io,
segment_path,
testing.allocator,
.limited(1 << 20),
);
defer testing.allocator.free(after);
try testing.expectEqualSlices(u8, before, after);
}
test "archive recovery truncates only a torn suffix and resumes the same segment" {
var threaded: Io.Threaded = .init(testing.allocator, .{});
defer threaded.deinit();
const io = threaded.io();
var tmp = testing.tmpDir(.{});
defer tmp.cleanup();
var root_buf: [Io.Dir.max_path_bytes]u8 = undefined;
const root_len = try tmp.dir.realPath(io, &root_buf);
var data_dir_buf: [Io.Dir.max_path_bytes]u8 = undefined;
const data_dir = try std.fmt.bufPrint(&data_dir_buf, "{s}/archive", .{root_buf[0..root_len]});
{
var archive = try Archive.init(testing.allocator, io, data_dir);
defer archive.deinit();
archive.max_events_per_block = 1;
archive.writer.max_events_per_block = 1;
_ = try archive.append(testEvent(1), 1);
archive.close();
}
var segment_path_buf: [Io.Dir.max_path_bytes]u8 = undefined;
const segment_path = try std.fmt.bufPrint(
&segment_path_buf,
"{s}/segments/seg_0000000000.jss",
.{data_dir},
);
var file = try Io.Dir.cwd().openFile(io, segment_path, .{ .mode = .read_write });
const durable_len = (try file.stat(io)).size;
var torn: [12]u8 = undefined;
std.mem.writeInt(u64, torn[0..8], 1 << 20, .little);
@memset(torn[8..], 0xff);
try file.writePositionalAll(io, &torn, durable_len);
try file.sync(io);
file.close(io);
{
var recovered = try Archive.init(testing.allocator, io, data_dir);
defer recovered.deinit();
try testing.expectEqual(@as(u64, 0), recovered.seg_index);
try testing.expectEqual(@as(usize, @intCast(durable_len)), recovered.file_written);
try testing.expectEqual(durable_len, (try recovered.file.stat(io)).size);
const seq = try recovered.append(testEvent(2), 2);
try testing.expectEqual(@as(u64, 2), seq);
recovered.close();
}
var reopened = try Archive.init(testing.allocator, io, data_dir);
defer reopened.deinit();
try testing.expectEqual(@as(u64, 3), reopened.next_seq);
}
test "archive recovery checksum-verifies the sealed high-water floor" {
var threaded: Io.Threaded = .init(testing.allocator, .{});
defer threaded.deinit();
const io = threaded.io();
var tmp = testing.tmpDir(.{});
defer tmp.cleanup();
var root_buf: [Io.Dir.max_path_bytes]u8 = undefined;
const root_len = try tmp.dir.realPath(io, &root_buf);
var data_dir_buf: [Io.Dir.max_path_bytes]u8 = undefined;
const data_dir = try std.fmt.bufPrint(&data_dir_buf, "{s}/archive", .{root_buf[0..root_len]});
{
var archive = try Archive.init(testing.allocator, io, data_dir);
defer archive.deinit();
archive.max_events_per_block = 1;
archive.writer.max_events_per_block = 1;
_ = try archive.append(testEvent(1), 1);
try archive.rotate();
}
var sealed_path_buf: [Io.Dir.max_path_bytes]u8 = undefined;
const sealed_path = try std.fmt.bufPrint(
&sealed_path_buf,
"{s}/segments/seg_0000000000.jss",
.{data_dir},
);
var sealed_file = try Io.Dir.cwd().openFile(io, sealed_path, .{ .mode = .read_write });
var corrupt_max_seq: [8]u8 = undefined;
std.mem.writeInt(u64, &corrupt_max_seq, 999_999, .little);
try sealed_file.writePositionalAll(io, &corrupt_max_seq, 34);
try sealed_file.sync(io);
sealed_file.close(io);
try testing.expectError(
error.BadChecksum,
Archive.init(testing.allocator, io, data_dir),
);
}
test "archive: metadata failure does not publish or strand durability" {
var threaded: Io.Threaded = .init(testing.allocator, .{});
defer threaded.deinit();
const io = threaded.io();
var tmp = testing.tmpDir(.{});
defer tmp.cleanup();
var path_buf: [Io.Dir.max_path_bytes]u8 = undefined;
const n = try tmp.dir.realPath(io, &path_buf);
var data_dir_buf: [Io.Dir.max_path_bytes]u8 = undefined;
const data_dir = try std.fmt.bufPrint(&data_dir_buf, "{s}/durable-hook", .{path_buf[0..n]});
var archive = try Archive.init(testing.allocator, io, data_dir);
defer archive.deinit();
var stats: metrics.Stats = .{};
archive.stats = &stats;
archive.max_events_per_block = 1;
archive.writer.max_events_per_block = 1;
const Hook = struct {
fail: bool = true,
calls: usize = 0,
fn run(ctx: *anyopaque, _: u64, _: u64) !void {
const self: *@This() = @ptrCast(@alignCast(ctx));
self.calls += 1;
if (self.fail) return error.InjectedMetadataFailure;
}
};
var hook: Hook = .{};
archive.on_durable_ctx = &hook;
archive.on_durable = Hook.run;
try testing.expectError(error.InjectedMetadataFailure, archive.append(testEvent(1), 1));
try testing.expectEqual(@as(u64, 0), archive.committed_seq.load(.acquire));
try testing.expectEqual(@as(u64, 1), stats.ingest_blocks_flushed_total.load(.monotonic));
try testing.expectEqual(@as(u64, 0), stats.ingest_append_errors_total.load(.monotonic));
try testing.expect(stats.ingest_active_segment_bytes.load(.monotonic) > 0);
var rejected = try archive.coldSnapshot(testing.allocator, 1);
defer rejected.deinit();
try testing.expectEqual(@as(usize, segment.header_size), rejected.active_start);
try testing.expectEqual(rejected.active_start, rejected.active_length);
hook.fail = false;
try archive.flushBlock();
var accepted = try archive.coldSnapshot(testing.allocator, 1);
defer accepted.deinit();
try testing.expectEqual(@as(usize, segment.header_size), accepted.active_start);
try testing.expect(accepted.active_length > accepted.active_start);
try testing.expectEqual(@as(u64, 1), archive.committed_seq.load(.acquire));
// The retry re-establishes the metadata boundary but does not recount an
// already-fsynced block.
try testing.expectEqual(@as(u64, 1), stats.ingest_blocks_flushed_total.load(.monotonic));
try testing.expectEqual(@as(usize, 2), hook.calls);
}
test "archive: timestamp lookup failure poisons transaction before sequence assignment" {
var threaded: Io.Threaded = .init(testing.allocator, .{});
defer threaded.deinit();
const io = threaded.io();
var tmp = testing.tmpDir(.{});
defer tmp.cleanup();
var path_buf: [Io.Dir.max_path_bytes]u8 = undefined;
const path_len = try tmp.dir.realPath(io, &path_buf);
var archive = try Archive.init(testing.allocator, io, path_buf[0..path_len]);
defer archive.deinit();
var stats: metrics.Stats = .{};
archive.stats = &stats;
const Stamper = struct {
fn run(_: *anyopaque, _: *segment.Event) !void {
return error.InjectedTimestampLookupFailure;
}
};
var context: u8 = 0;
archive.stamper_ctx = &context;
archive.stamper = Stamper.run;
var rows = [_]segment.Event{testEvent(1)};
try testing.expectError(error.InjectedTimestampLookupFailure, archive.appendBatch(&rows, 1));
try testing.expectEqual(@as(u64, 1), archive.next_seq);
try testing.expectEqual(@as(u64, 0), archive.committed_seq.load(.acquire));
try testing.expectEqual(@as(usize, 0), archive.writer.pending.items.len);
try testing.expectEqual(@as(u64, 1), stats.ingest_append_errors_total.load(.monotonic));
try testing.expectError(error.ArchivePoisoned, archive.append(testEvent(2), 2));
}
test "archive: terminal seal publishes final partial block through metadata hook" {
var threaded: Io.Threaded = .init(testing.allocator, .{});
defer threaded.deinit();
const io = threaded.io();
var tmp = testing.tmpDir(.{});
defer tmp.cleanup();
var path_buf: [Io.Dir.max_path_bytes]u8 = undefined;
const path_len = try tmp.dir.realPath(io, &path_buf);
var archive = try Archive.init(testing.allocator, io, path_buf[0..path_len]);
archive.flush_interval_us = std.math.maxInt(i64);
const Hook = struct {
called: u64 = 0,
durable: u64 = 0,
fn run(ctx: *anyopaque, durable: u64, _: u64) !void {
const self: *@This() = @ptrCast(@alignCast(ctx));
self.called += 1;
self.durable = durable;
}
};
var hook: Hook = .{};
archive.on_durable_ctx = &hook;
archive.on_durable = Hook.run;
_ = try archive.append(testEvent(1), 1);
try testing.expectEqual(@as(u64, 0), hook.called);
try archive.sealAndClose();
try testing.expectEqual(@as(u64, 1), hook.called);
try testing.expectEqual(@as(u64, 1), hook.durable);
}