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
//! The single durable metadata database for Stream.
//!
//! Segment files own event bytes. Everything structured that cannot be
//! recovered cheaply from a segment scan belongs here. A synced metadata
//! batch may acknowledge segment bytes only after those bytes have been
//! fsynced; callers use `Batch.commitDurable` for that boundary.
const std = @import("std");
const rocksdb = @import("rocksdb");
const metrics = @import("../runtime/metrics.zig");
const Allocator = std.mem.Allocator;
const Io = std.Io;
const log = std.log.scoped(.stream_meta);
/// Deterministic, process-wide metadata fault seam used only by the offline
/// restart oracle. Production leaves this unarmed. The operation still runs
/// through the real Store and RocksDB schema; the selected durable mutation
/// returns an I/O failure immediately before it reaches RocksDB so callers'
/// fail-loud and restart behavior can be exercised without corrupting bytes.
const Fault = struct {
prefix: ?[]const u8 = null,
ordinal: u64 = 0,
seen: std.atomic.Value(u64) = .init(0),
fn check(self: *Fault, key: []const u8) !void {
const prefix = self.prefix orelse return;
if (!std.mem.startsWith(u8, key, prefix)) return;
const seen = self.seen.fetchAdd(1, .monotonic) + 1;
if (seen != self.ordinal) return;
log.warn("store fault: injected I/O failure for prefix {s} at ordinal {d}", .{ prefix, seen });
return error.InjectedStoreFault;
}
};
var fault: Fault = .{};
var read_fault: Fault = .{};
/// Arm one matching durable metadata operation. Must be called during process
/// startup, before worker threads exist. Returns false for an empty prefix or
/// zero ordinal so an invalid oracle configuration fails closed.
pub fn armFault(prefix: []const u8, ordinal: u64) bool {
if (prefix.len == 0 or ordinal == 0) return false;
fault = .{ .prefix = prefix, .ordinal = ordinal };
return true;
}
pub fn disarmFault() void {
fault = .{};
}
/// Independent read-side fault seam. Keeping it separate from durable mutation
/// faults preserves the existing crash schedules while allowing startup code
/// to prove that a storage error is not misclassified as a missing key.
pub fn armReadFault(prefix: []const u8, ordinal: u64) bool {
if (prefix.len == 0 or ordinal == 0) return false;
read_fault = .{ .prefix = prefix, .ordinal = ordinal };
return true;
}
pub fn disarmReadFault() void {
read_fault = .{};
}
pub const subdir = "meta.rocksdb";
pub const Store = struct {
allocator: Allocator,
db: rocksdb.DB,
default_cf: rocksdb.ColumnFamilyHandle,
io: ?Io = null,
stats: ?*metrics.Stats = null,
pub fn open(allocator: Allocator, data_dir: []const u8) !Store {
return openInner(allocator, data_dir, null, null);
}
pub fn openWithStats(allocator: Allocator, io: Io, data_dir: []const u8, stats: *metrics.Stats) !Store {
return openInner(allocator, data_dir, io, stats);
}
fn openInner(allocator: Allocator, data_dir: []const u8, io: ?Io, stats: ?*metrics.Stats) !Store {
const path = try std.fs.path.join(allocator, &.{ data_dir, subdir });
defer allocator.free(path);
var err_data: ?rocksdb.Data = null;
defer if (err_data) |e| e.deinit();
const db, const families = rocksdb.DB.open(
allocator,
path,
.{
.create_if_missing = true,
.create_missing_column_families = true,
},
&.{.{ .name = "default", .options = .{ .bloom_bits_per_key = 10 } }},
false,
&err_data,
) catch |err| {
if (err_data) |e| log.err("open {s}: {s}", .{ path, e.data });
return err;
};
defer allocator.free(families);
if (families.len != 1) return error.InvalidMetadataStore;
log.info("metadata store opened at {s}", .{path});
return .{
.allocator = allocator,
.db = db,
.default_cf = families[0].handle,
.io = io,
.stats = stats,
};
}
pub fn deinit(self: *Store) void {
self.db.deinit();
}
/// One-way import for canaries created before the unified metadata DB.
/// The legacy files remain untouched as rollback material; existing DB
/// keys always win, making this safe on every restart.
pub fn migrateLegacy(self: *Store, io: std.Io, data_dir: []const u8) !void {
if (try self.getAlloc(self.allocator, "relay/cursor")) |existing| {
defer self.allocator.free(existing);
// Stream previously stored a bare signed little-endian value.
// Convert that known schema exactly once; all other malformed or
// unknown-version values remain untouched so the strict loader
// fails closed instead of laundering corruption.
if (existing.len == 8) {
const seq = std.mem.readInt(i64, existing[0..8], .little);
if (seq < 0) return error.NegativeRelayCursor;
var versioned: [9]u8 = undefined;
versioned[0] = 1;
std.mem.writeInt(u64, versioned[1..9], @intCast(seq), .little);
try self.putDurable("relay/cursor", &versioned);
log.info("migrated relay cursor to versioned encoding", .{});
}
} else {
var path_buf: [std.fs.max_path_bytes]u8 = undefined;
const path = try std.fmt.bufPrint(&path_buf, "{s}/cursor", .{data_dir});
var text_buf: [64]u8 = undefined;
if (std.Io.Dir.cwd().readFile(io, path, &text_buf)) |text| {
if (std.fmt.parseInt(i64, std.mem.trim(u8, text, " \n\t"), 10)) |seq| {
if (seq < 0) return error.NegativeRelayCursor;
var bytes: [9]u8 = undefined;
bytes[0] = 1;
std.mem.writeInt(u64, bytes[1..9], @intCast(seq), .little);
try self.putDurable("relay/cursor", &bytes);
log.info("imported legacy relay cursor {d}", .{seq});
} else |_| {}
} else |_| {}
}
if (try self.getAlloc(self.allocator, "phase")) |existing| {
self.allocator.free(existing);
} else {
var path_buf: [std.fs.max_path_bytes]u8 = undefined;
const path = try std.fmt.bufPrint(&path_buf, "{s}/phase", .{data_dir});
var text_buf: [32]u8 = undefined;
if (std.Io.Dir.cwd().readFile(io, path, &text_buf)) |text| {
const phase = std.mem.trim(u8, text, " \n\t");
if (phase.len > 0) try self.putDurable("phase", phase);
} else |_| {}
}
if (try self.getAlloc(self.allocator, "compaction/seq")) |existing| {
self.allocator.free(existing);
} else {
var path_buf: [std.fs.max_path_bytes]u8 = undefined;
const path = try std.fmt.bufPrint(&path_buf, "{s}/compaction_watermark", .{data_dir});
var bytes: [9]u8 = undefined;
if (std.Io.Dir.cwd().readFile(io, path, &bytes)) |value| {
if (value.len == bytes.len) try self.putDurable("compaction/seq", value);
} else |_| {}
}
if (try self.getAlloc(self.allocator, "merge/next_source_idx")) |existing| {
defer self.allocator.free(existing);
if (existing.len == 8) {
var versioned: [9]u8 = undefined;
versioned[0] = 1;
@memcpy(versioned[1..9], existing);
try self.putDurable("merge/next_source_idx", &versioned);
log.info("migrated merge cursor to versioned encoding", .{});
}
}
}
pub fn getAlloc(self: *Store, allocator: Allocator, key: []const u8) !?[]u8 {
const started_at = self.started();
read_fault.check(key) catch |err| {
self.observe(.get, .error_result, started_at);
return err;
};
var err_data: ?rocksdb.Data = null;
defer if (err_data) |e| e.deinit();
const value = self.db.get(self.default_cf, key, &err_data) catch |err| {
self.observe(.get, .error_result, started_at);
if (err_data) |e| log.err("get {s}: {s}", .{ key, e.data });
return err;
} orelse {
self.observe(.get, .notfound, started_at);
return null;
};
self.observe(.get, .ok, started_at);
defer value.deinit();
return try allocator.dupe(u8, value.data);
}
pub fn putDurable(self: *Store, key: []const u8, value: []const u8) !void {
const started_at = self.started();
fault.check(key) catch |err| {
self.observe(.set, .error_result, started_at);
return err;
};
var err_data: ?rocksdb.Data = null;
defer if (err_data) |e| e.deinit();
self.db.putWithOptions(self.default_cf, key, value, .{ .sync = true }, &err_data) catch |err| {
self.observe(.set, .error_result, started_at);
if (err_data) |e| log.err("durable put {s}: {s}", .{ key, e.data });
return err;
};
self.observe(.set, .ok, started_at);
}
pub fn deleteDurable(self: *Store, key: []const u8) !void {
const started_at = self.started();
fault.check(key) catch |err| {
self.observe(.delete, .error_result, started_at);
return err;
};
var err_data: ?rocksdb.Data = null;
defer if (err_data) |e| e.deinit();
self.db.deleteWithOptions(self.default_cf, key, .{ .sync = true }, &err_data) catch |err| {
self.observe(.delete, .error_result, started_at);
if (err_data) |e| log.err("durable delete {s}: {s}", .{ key, e.data });
return err;
};
self.observe(.delete, .ok, started_at);
}
pub fn iterator(self: *Store, start: ?[]const u8) rocksdb.Iterator {
return self.db.iterator(self.default_cf, .forward, start);
}
pub fn batch(self: *Store) Batch {
return .{ .store = self, .inner = rocksdb.WriteBatch.init() };
}
fn started(self: *const Store) ?Io.Timestamp {
const io = self.io orelse return null;
return Io.Timestamp.now(io, .awake);
}
fn observe(self: *Store, op: metrics.StoreOp, status: metrics.StoreStatus, started_at: ?Io.Timestamp) void {
const stats = self.stats orelse return;
const io = self.io orelse return;
const started_value = started_at orelse return;
const elapsed = started_value.durationTo(Io.Timestamp.now(io, .awake)).toMicroseconds();
stats.observeStore(op, status, @intCast(@max(elapsed, 0)));
}
};
pub const Batch = struct {
store: *Store,
inner: rocksdb.WriteBatch,
fault_key: ?[]const u8 = null,
pub fn deinit(self: *Batch) void {
self.inner.deinit();
}
pub fn put(self: *Batch, key: []const u8, value: []const u8) void {
if (fault.prefix) |prefix| {
if (self.fault_key == null and std.mem.startsWith(u8, key, prefix)) self.fault_key = key;
}
self.inner.put(self.store.default_cf, key, value);
}
pub fn delete(self: *Batch, key: []const u8) void {
if (fault.prefix) |prefix| {
if (self.fault_key == null and std.mem.startsWith(u8, key, prefix)) self.fault_key = key;
}
self.inner.delete(self.store.default_cf, key);
}
pub fn commitDurable(self: *Batch) !void {
const started_at = self.store.started();
if (self.fault_key) |key| fault.check(key) catch |err| {
self.store.observe(.batch_commit, .error_result, started_at);
return err;
};
var err_data: ?rocksdb.Data = null;
defer if (err_data) |e| e.deinit();
self.store.db.writeWithOptions(self.inner, .{ .sync = true }, &err_data) catch |err| {
self.store.observe(.batch_commit, .error_result, started_at);
if (err_data) |e| log.err("durable batch commit: {s}", .{e.data});
return err;
};
self.store.observe(.batch_commit, .ok, started_at);
}
};
test "durable point reads and atomic batches survive reopen" {
const testing = std.testing;
var tmp = testing.tmpDir(.{});
defer tmp.cleanup();
var path_buf: [std.fs.max_path_bytes]u8 = undefined;
const path_len = try tmp.dir.realPath(testing.io, &path_buf);
const path = path_buf[0..path_len];
{
var store = try Store.open(testing.allocator, path);
defer store.deinit();
var batch = store.batch();
defer batch.deinit();
batch.put("relay/cursor", "41");
batch.put("repo/did:plc:alice", "complete\x003lrev");
try batch.commitDurable();
}
{
var store = try Store.open(testing.allocator, path);
defer store.deinit();
const cursor = (try store.getAlloc(testing.allocator, "relay/cursor")).?;
defer testing.allocator.free(cursor);
try testing.expectEqualStrings("41", cursor);
const repo = (try store.getAlloc(testing.allocator, "repo/did:plc:alice")).?;
defer testing.allocator.free(repo);
try testing.expectEqualStrings("complete\x003lrev", repo);
}
}
test "real RocksDB operations populate canonical store metric outcomes" {
const testing = std.testing;
var tmp = testing.tmpDir(.{});
defer tmp.cleanup();
var path_buf: [std.fs.max_path_bytes]u8 = undefined;
const path_len = try tmp.dir.realPath(testing.io, &path_buf);
var stats: metrics.Stats = .{};
var store = try Store.openWithStats(testing.allocator, testing.io, path_buf[0..path_len], &stats);
defer store.deinit();
try store.putDurable("point", "value");
const found = (try store.getAlloc(testing.allocator, "point")).?;
testing.allocator.free(found);
try testing.expectEqual(null, try store.getAlloc(testing.allocator, "missing"));
try store.deleteDurable("point");
var writes = store.batch();
defer writes.deinit();
writes.put("batch/a", "1");
writes.put("batch/b", "2");
try writes.commitDurable();
try testing.expectEqual(1, stats.store_op_duration_count[storeMetricIndex(.get, .ok)].load(.monotonic));
try testing.expectEqual(1, stats.store_op_duration_count[storeMetricIndex(.get, .notfound)].load(.monotonic));
try testing.expectEqual(1, stats.store_op_duration_count[storeMetricIndex(.set, .ok)].load(.monotonic));
try testing.expectEqual(1, stats.store_op_duration_count[storeMetricIndex(.delete, .ok)].load(.monotonic));
try testing.expectEqual(1, stats.store_op_duration_count[storeMetricIndex(.batch_commit, .ok)].load(.monotonic));
}
test "armed durable store fault fires once at matching batch ordinal" {
const testing = std.testing;
disarmFault();
defer disarmFault();
var tmp = testing.tmpDir(.{});
defer tmp.cleanup();
var path_buf: [std.fs.max_path_bytes]u8 = undefined;
const path_len = try tmp.dir.realPath(testing.io, &path_buf);
var store = try Store.open(testing.allocator, path_buf[0..path_len]);
defer store.deinit();
try testing.expect(armFault("merge/next_source_idx", 2));
for (0..2) |i| {
var writes = store.batch();
defer writes.deinit();
writes.put("repo/did:plc:alice", "complete");
writes.put("merge/next_source_idx", "1");
if (i == 0) {
try writes.commitDurable();
} else {
try testing.expectError(error.InjectedStoreFault, writes.commitDurable());
}
}
// The rejected batch never reached RocksDB. Disarming and retrying the
// exact real batch succeeds, which is the recovery contract used by the
// process oracle.
disarmFault();
var retry = store.batch();
defer retry.deinit();
retry.put("merge/next_source_idx", "2");
try retry.commitDurable();
const cursor = (try store.getAlloc(testing.allocator, "merge/next_source_idx")).?;
defer testing.allocator.free(cursor);
try testing.expectEqualStrings("2", cursor);
}
fn storeMetricIndex(op: metrics.StoreOp, status: metrics.StoreStatus) usize {
return @backingInt(op) * @typeInfo(metrics.StoreStatus).@"enum".field_names.len + @backingInt(status);
}