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
//! Shared decoded-block cache for subscriber cold replay.
//!
//! Immutable sealed blocks are keyed by segment checksum, block index, and a
//! per-segment invalidation generation. Concurrent misses for one key decode
//! exactly once; unrelated keys decode concurrently. Handles pin an item
//! across LRU eviction, and four memo lanes let the subscribe layer share v1,
//! v2, and their dictionary-compressed wire frames across readers. Memo growth
//! is charged to the same byte budget that owns the decoded block.
const std = @import("std");
const segment = @import("segment.zig");
const Allocator = std.mem.Allocator;
const Io = std.Io;
pub const default_max_bytes: usize = 64 * 1024 * 1024;
pub const Key = struct {
segment_index: u64,
checksum: u64,
block_index: u64,
generation: u64,
};
pub const MemoLane = enum(u2) { v1, v2, zstd_v1, zstd_v2 };
const Memo = struct {
done: bool = false,
body: ?[]u8 = null,
err: ?anyerror = null,
};
const Item = struct {
key: Key,
block: segment.Block,
memos: []Memo,
memo_mu: Io.Mutex = .init,
bytes: usize,
refs: usize = 0,
resident: bool = false,
prev: ?*Item = null,
next: ?*Item = null,
fn deinit(self: *Item, allocator: Allocator) void {
for (self.memos) |memo| if (memo.body) |body| allocator.free(body);
allocator.free(self.memos);
self.block.deinit(allocator);
allocator.destroy(self);
}
};
const Inflight = struct {
done: Io.Condition = .init,
complete: bool = false,
participants: usize = 1,
item: ?*Item = null,
err: ?anyerror = null,
};
pub const Handle = struct {
cache: *Cache,
item: *Item,
pub fn events(self: *const Handle) []const segment.Event {
return self.item.block.events;
}
/// Compute one immutable memo at most once. `compute` must allocate a
/// returned body with the cache allocator; the cache owns it thereafter.
pub fn memoize(
self: *Handle,
event_index: usize,
lane: MemoLane,
ctx: anytype,
comptime compute: fn (@TypeOf(ctx), Allocator, segment.Event) anyerror!?[]u8,
) !?[]const u8 {
if (event_index >= self.item.block.events.len) return error.BadEventIndex;
const at = event_index * 4 + @backingInt(lane);
self.item.memo_mu.lockUncancelable(self.cache.io);
defer self.item.memo_mu.unlock(self.cache.io);
const memo = &self.item.memos[at];
if (!memo.done) {
memo.body = compute(ctx, self.cache.allocator, self.item.block.events[event_index]) catch |err| {
memo.err = err;
memo.done = true;
return err;
};
memo.done = true;
if (memo.body) |body| self.cache.grow(self.item, body.len);
}
if (memo.err) |err| return err;
return memo.body;
}
pub fn deinit(self: *Handle) void {
const cache = self.cache;
cache.mu.lockUncancelable(cache.io);
std.debug.assert(self.item.refs > 0);
self.item.refs -= 1;
const destroy = self.item.refs == 0 and !self.item.resident;
cache.mu.unlock(cache.io);
if (destroy) self.item.deinit(cache.allocator);
self.* = undefined;
}
};
pub const Cache = struct {
allocator: Allocator,
io: Io,
max_bytes: usize,
mu: Io.Mutex = .init,
items: std.AutoHashMapUnmanaged(Key, *Item) = .empty,
inflight: std.AutoHashMapUnmanaged(Key, *Inflight) = .empty,
generations: std.AutoHashMapUnmanaged(u64, u64) = .empty,
head: ?*Item = null,
tail: ?*Item = null,
cur_bytes: usize = 0,
decode_count: std.atomic.Value(u64) = .init(0),
pub fn init(allocator: Allocator, io: Io, max_bytes: usize) Cache {
return .{
.allocator = allocator,
.io = io,
.max_bytes = if (max_bytes == 0) default_max_bytes else max_bytes,
};
}
pub fn deinit(self: *Cache) void {
self.mu.lockUncancelable(self.io);
std.debug.assert(self.inflight.count() == 0);
var it = self.items.valueIterator();
while (it.next()) |item_ptr| {
const item = item_ptr.*;
std.debug.assert(item.refs == 0);
item.resident = false;
item.deinit(self.allocator);
}
self.items.deinit(self.allocator);
self.inflight.deinit(self.allocator);
self.generations.deinit(self.allocator);
self.head = null;
self.tail = null;
self.cur_bytes = 0;
self.mu.unlock(self.io);
self.* = undefined;
}
pub fn key(self: *Cache, segment_index: u64, checksum: u64, block_index: usize) Key {
self.mu.lockUncancelable(self.io);
defer self.mu.unlock(self.io);
return .{
.segment_index = segment_index,
.checksum = checksum,
.block_index = block_index,
.generation = self.generations.get(segment_index) orelse 0,
};
}
/// Return a pinned decoded block. The first caller for a key executes the
/// real decoder outside the cache lock; concurrent callers wait for that
/// result while other keys remain independent.
pub fn acquire(
self: *Cache,
key_value: Key,
ctx: anytype,
comptime decode: fn (@TypeOf(ctx), Allocator) anyerror!segment.Block,
) !Handle {
self.mu.lockUncancelable(self.io);
if (self.items.get(key_value)) |item| {
item.refs += 1;
self.touchLocked(item);
self.mu.unlock(self.io);
return .{ .cache = self, .item = item };
}
if (self.inflight.get(key_value)) |flight| {
flight.participants += 1;
while (!flight.complete) flight.done.waitUncancelable(self.io, &self.mu);
const item = flight.item;
const err = flight.err;
self.finishFlightLocked(flight);
self.mu.unlock(self.io);
if (err) |value| return value;
return .{ .cache = self, .item = item.? };
}
const flight = self.allocator.create(Inflight) catch |err| {
self.mu.unlock(self.io);
return err;
};
flight.* = .{};
self.inflight.put(self.allocator, key_value, flight) catch |err| {
self.allocator.destroy(flight);
self.mu.unlock(self.io);
return err;
};
self.mu.unlock(self.io);
_ = self.decode_count.fetchAdd(1, .monotonic);
var decoded = decode(ctx, self.allocator) catch |err| {
self.completeFailure(key_value, flight, err);
return err;
};
var item = self.createItem(key_value, decoded) catch |err| {
decoded.deinit(self.allocator);
self.completeFailure(key_value, flight, err);
return err;
};
self.mu.lockUncancelable(self.io);
item.refs = flight.participants;
const current_generation = self.generations.get(key_value.segment_index) orelse 0;
if (current_generation == key_value.generation) {
self.items.put(self.allocator, key_value, item) catch |err| {
self.finishCompletionLocked(key_value, flight, null, err);
self.mu.unlock(self.io);
item.refs = 0;
item.deinit(self.allocator);
return err;
};
item.resident = true;
self.linkFrontLocked(item);
self.cur_bytes += item.bytes;
self.evictLocked();
}
self.finishCompletionLocked(key_value, flight, item, null);
self.mu.unlock(self.io);
return .{ .cache = self, .item = item };
}
fn createItem(self: *Cache, key_value: Key, block: segment.Block) !*Item {
const item = try self.allocator.create(Item);
errdefer self.allocator.destroy(item);
const memos = try self.allocator.alloc(Memo, block.events.len * 4);
@memset(memos, .{});
item.* = .{
.key = key_value,
.block = block,
.memos = memos,
.bytes = blockBytes(block.events),
};
return item;
}
/// Match upstream's decoded-byte estimate: borrowed event fields plus a
/// fixed per-entry allowance for the Event/Entry/pointer structures.
fn blockBytes(events: []const segment.Event) usize {
var total: usize = 0;
for (events) |event| {
total += event.payload.len + event.did.len + event.collection.len +
event.rkey.len + event.rev.len + 128;
}
return total;
}
fn completeFailure(self: *Cache, key_value: Key, flight: *Inflight, err: anyerror) void {
self.mu.lockUncancelable(self.io);
self.finishCompletionLocked(key_value, flight, null, err);
self.mu.unlock(self.io);
}
fn finishCompletionLocked(self: *Cache, key_value: Key, flight: *Inflight, item: ?*Item, err: ?anyerror) void {
_ = self.inflight.remove(key_value);
flight.item = item;
flight.err = err;
flight.complete = true;
flight.done.broadcast(self.io);
self.finishFlightLocked(flight);
}
fn finishFlightLocked(self: *Cache, flight: *Inflight) void {
std.debug.assert(flight.participants > 0);
flight.participants -= 1;
if (flight.participants == 0) self.allocator.destroy(flight);
}
pub fn invalidateSegment(self: *Cache, segment_index: u64) !void {
self.mu.lockUncancelable(self.io);
errdefer self.mu.unlock(self.io);
const generation = self.generations.get(segment_index) orelse 0;
try self.generations.put(self.allocator, segment_index, generation +% 1);
var current = self.head;
while (current) |item| {
const next = item.next;
if (item.key.segment_index == segment_index) self.removeResidentLocked(item);
current = next;
}
self.mu.unlock(self.io);
}
pub fn bytes(self: *Cache) usize {
self.mu.lockUncancelable(self.io);
defer self.mu.unlock(self.io);
return self.cur_bytes;
}
fn grow(self: *Cache, item: *Item, delta: usize) void {
if (delta == 0) return;
self.mu.lockUncancelable(self.io);
if (item.resident) {
item.bytes += delta;
self.cur_bytes += delta;
self.evictLocked();
}
self.mu.unlock(self.io);
}
fn touchLocked(self: *Cache, item: *Item) void {
if (self.head == item) return;
self.unlinkLocked(item);
self.linkFrontLocked(item);
}
fn linkFrontLocked(self: *Cache, item: *Item) void {
item.prev = null;
item.next = self.head;
if (self.head) |head| head.prev = item else self.tail = item;
self.head = item;
}
fn unlinkLocked(self: *Cache, item: *Item) void {
if (item.prev) |prev| prev.next = item.next else self.head = item.next;
if (item.next) |next| next.prev = item.prev else self.tail = item.prev;
item.prev = null;
item.next = null;
}
fn removeResidentLocked(self: *Cache, item: *Item) void {
if (!item.resident) return;
_ = self.items.remove(item.key);
self.unlinkLocked(item);
self.cur_bytes -= item.bytes;
item.resident = false;
if (item.refs == 0) item.deinit(self.allocator);
}
fn evictLocked(self: *Cache) void {
while (self.cur_bytes > self.max_bytes) {
const item = self.tail orelse break;
self.removeResidentLocked(item);
}
}
};