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
const std = @import("std");
const zat = @import("zat");
const archive_mod = @import("../storage/archive.zig");
const cursor_mod = @import("cursor.zig");
const ingest = @import("ingest.zig");
const meta_store = @import("../storage/meta_store.zig");
const repair = @import("repair.zig");
const repos = @import("backfill/repos.zig");
const segment = @import("../storage/segment.zig");
const tail_mod = @import("../serve/tail.zig");
const verify = @import("verify.zig");
const Io = std.Io;
test "repair completion durably emits tombstone then replacement offline" {
const testing = std.testing;
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);
const root = root_buf[0..root_len];
var meta = try meta_store.Store.open(testing.allocator, root);
defer meta.deinit();
var archive = try archive_mod.Archive.init(testing.allocator, io, root);
defer archive.deinit();
var tail = tail_mod.Tail.init(testing.allocator, io, 8 * 1024 * 1024);
defer tail.deinit();
tail.enableDurabilityGate(0);
var cursor = cursor_mod.Store.init(&meta);
var consumer: ingest.Consumer = .{
.allocator = testing.allocator,
.io = io,
.tail = &tail,
.upstream = "ws://offline.invalid",
.cursor_store = &cursor,
.archive = &archive,
};
defer consumer.deinit();
archive.on_durable_ctx = &consumer;
archive.on_durable = ingest.Consumer.onArchiveDurable;
const prepared_arena = try testing.allocator.create(std.heap.ArenaAllocator);
prepared_arena.* = std.heap.ArenaAllocator.init(testing.allocator);
defer {
prepared_arena.deinit();
testing.allocator.destroy(prepared_arena);
}
const alloc = prepared_arena.allocator();
const did = "did:plc:durable-repair";
const record = try zat.cbor.encodeAlloc(alloc, .{ .map = &.{
.{ .key = "$type", .value = .{ .text = "app.bsky.feed.post" } },
.{ .key = "text", .value = .{ .text = "durable offline repair" } },
} });
const record_cid = try zat.cbor.Cid.forDagCbor(alloc, record);
var tree = zat.mst.Mst.init(alloc);
try tree.put("app.bsky.feed.post/3k2abcdefghij", record_cid);
const data_cid = try tree.rootCid();
const keypair = try zat.Keypair.fromSecretKey(.p256, @splat(31));
const signed = try zat.signCommit(alloc, .{
.did = did,
.rev = "3k2abcdefghij",
.data = data_cid,
}, &keypair);
var blocks: std.ArrayList(zat.car.Block) = .empty;
try blocks.append(alloc, .{ .cid_raw = signed.cid.raw, .data = signed.bytes });
try tree.collectBlocks(&blocks);
try blocks.append(alloc, .{ .cid_raw = record_cid.raw, .data = record });
const car_bytes = try zat.car.writeAlloc(alloc, .{ .roots = &.{signed.cid}, .blocks = blocks.items });
const prepared = try repos.prepareRepo(alloc, car_bytes);
var chain: verify.ChainUpdate = .{ .rev = undefined, .data_len = @intCast(data_cid.raw.len) };
@memcpy(&chain.rev, "3k2abcdefghij");
@memcpy(chain.data[0..data_cid.raw.len], data_cid.raw);
var completion: repair.Completion = .{
.allocator = testing.allocator,
.io = io,
.did = @constCast(did),
.prepared_arena = prepared_arena,
.prepared = .{ .fetched = undefined, .repo = prepared },
.fetched_chain = chain,
.witnessed_at = 1_784_160_000_000_000,
.after_ticket = 0,
.upstream_seq = 77,
};
try ingest.Consumer.emitRepair(&consumer, &completion, @import("../runtime/observability.zig").root_context);
try testing.expectEqual(@as(u64, 2), archive.committed_seq.load(.acquire));
try testing.expectEqual(@as(?i64, 77), try cursor.load(io));
try testing.expectEqual(@as(usize, 2), tail.gauges().entries);
const retained = tail.entries.items[tail.head..];
try testing.expectEqual(@as(usize, 0), retained[0].json.len);
try testing.expect(retained[0].json_v2.len > 0);
try testing.expectEqual(@as(usize, 0), retained[1].json.len);
try testing.expect(retained[1].json_v2.len > 0);
var reloaded = verify.Verifier.init(testing.allocator, io, "http://offline.invalid", &meta);
defer reloaded.deinit();
const durable_chain = (try reloaded.chainFor(did)).?;
try testing.expectEqualStrings("3k2abcdefghij", &durable_chain.rev);
try testing.expectEqualSlices(u8, data_cid.raw, durable_chain.dataSlice());
try archive.rotate();
var name_buf: [64]u8 = undefined;
const name = archive_mod.formatSegmentName(&name_buf, 0);
const bytes = try archive.dir.readFileAlloc(io, name, testing.allocator, .limited(16 * 1024 * 1024));
defer testing.allocator.free(bytes);
var sealed = try segment.Sealed.parse(testing.allocator, bytes);
defer sealed.deinit(testing.allocator);
var block = try sealed.readBlock(testing.allocator, 0);
defer block.deinit(testing.allocator);
try testing.expectEqual(@as(usize, 2), block.events.len);
try testing.expectEqual(segment.Kind.sync, block.events[0].kind);
try testing.expectEqual(segment.Kind.create_resync, block.events[1].kind);
try testing.expectEqual(@as(u64, 1), block.events[0].seq);
try testing.expectEqual(@as(u64, 2), block.events[1].seq);
}
test "large repair keeps wire encoding scratch bounded" {
const testing = std.testing;
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);
const root = root_buf[0..root_len];
var meta = try meta_store.Store.open(testing.allocator, root);
defer meta.deinit();
var archive = try archive_mod.Archive.init(testing.allocator, io, root);
defer archive.deinit();
// Make every 1,024-row batch durable so the tiny tail can retire it. The
// cap below therefore measures repair-owned scratch, not the configured
// subscriber retention window.
archive.flush_interval_us = 0;
var tail = tail_mod.Tail.init(testing.allocator, io, 64 * 1024);
defer tail.deinit();
tail.enableDurabilityGate(0);
var cursor = cursor_mod.Store.init(&meta);
var repair_alloc = std.heap.DebugAllocator(.{ .enable_memory_limit = true }){};
defer testing.expect(repair_alloc.deinit() == .ok) catch @panic("repair allocator leak");
repair_alloc.requested_memory_limit = 1 * 1024 * 1024;
var consumer: ingest.Consumer = .{
.allocator = repair_alloc.allocator(),
.io = io,
.tail = &tail,
.upstream = "ws://offline.invalid",
.cursor_store = &cursor,
.archive = &archive,
};
defer consumer.deinit();
archive.on_durable_ctx = &consumer;
archive.on_durable = ingest.Consumer.onArchiveDurable;
const prepared_arena = try testing.allocator.create(std.heap.ArenaAllocator);
prepared_arena.* = std.heap.ArenaAllocator.init(testing.allocator);
defer {
prepared_arena.deinit();
testing.allocator.destroy(prepared_arena);
}
const alloc = prepared_arena.allocator();
const did = "did:plc:large-durable-repair";
const body: [512]u8 = @splat('x');
const record = try zat.cbor.encodeAlloc(alloc, .{ .map = &.{
.{ .key = "$type", .value = .{ .text = "app.bsky.feed.post" } },
.{ .key = "text", .value = .{ .text = &body } },
} });
const record_cid = try zat.cbor.Cid.forDagCbor(alloc, record);
var tree = zat.mst.Mst.init(alloc);
for (0..4096) |i| {
const key = try std.fmt.allocPrint(alloc, "app.bsky.feed.post/r{d}", .{i});
try tree.put(key, record_cid);
}
const data_cid = try tree.rootCid();
const keypair = try zat.Keypair.fromSecretKey(.p256, @splat(29));
const signed = try zat.signCommit(alloc, .{
.did = did,
.rev = "3k2abcdefghij",
.data = data_cid,
}, &keypair);
var blocks: std.ArrayList(zat.car.Block) = .empty;
try blocks.append(alloc, .{ .cid_raw = signed.cid.raw, .data = signed.bytes });
try tree.collectBlocks(&blocks);
try blocks.append(alloc, .{ .cid_raw = record_cid.raw, .data = record });
const car_bytes = try zat.car.writeAlloc(alloc, .{ .roots = &.{signed.cid}, .blocks = blocks.items });
const prepared = try repos.prepareRepo(alloc, car_bytes);
var chain: verify.ChainUpdate = .{ .rev = undefined, .data_len = @intCast(data_cid.raw.len) };
@memcpy(&chain.rev, "3k2abcdefghij");
@memcpy(chain.data[0..data_cid.raw.len], data_cid.raw);
var completion: repair.Completion = .{
.allocator = testing.allocator,
.io = io,
.did = @constCast(did),
.prepared_arena = prepared_arena,
.prepared = .{ .fetched = undefined, .repo = prepared },
.fetched_chain = chain,
.witnessed_at = 1_784_160_000_000_000,
.after_ticket = 0,
.upstream_seq = 4096,
};
try ingest.Consumer.emitRepair(&consumer, &completion, @import("../runtime/observability.zig").root_context);
try testing.expectEqual(@as(u64, 4097), archive.committed_seq.load(.acquire));
try testing.expectEqual(@as(?i64, 4096), try cursor.load(io));
try testing.expect(repair_alloc.total_requested_bytes < 256 * 1024);
}