forked from tigerbeetle/tigerbeetle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage.zig
438 lines (376 loc) · 17 KB
/
storage.zig
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
const std = @import("std");
const assert = std.debug.assert;
const math = std.math;
const mem = std.mem;
const config = @import("../config.zig");
const vsr = @import("../vsr.zig");
const log = std.log.scoped(.storage);
// TODOs:
// less than a majority of replicas may have corruption
// have an option to enable/disable the following corruption types:
// bitrot
// misdirected read/write
// corrupt sector
// latent sector error
// - emulate by zeroing sector, as this is how we handle this in the real Storage implementation
// - likely that surrounding sectors also corrupt
// - likely that stuff written at the same time is also corrupt even if written to a far away sector
pub const Storage = struct {
/// Options for fault injection during fuzz testing
pub const Options = struct {
/// Seed for the storage PRNG
seed: u64,
/// Minimum number of ticks it may take to read data.
read_latency_min: u64,
/// Average number of ticks it may take to read data. Must be >= read_latency_min.
read_latency_mean: u64,
/// Minimum number of ticks it may take to write data.
write_latency_min: u64,
/// Average number of ticks it may take to write data. Must be >= write_latency_min.
write_latency_mean: u64,
/// Chance out of 100 that a read will return incorrect data, if the target memory is within
/// the faulty area of this replica.
read_fault_probability: u8,
/// Chance out of 100 that a read will return incorrect data, if the target memory is within
/// the faulty area of this replica.
write_fault_probability: u8,
};
/// See usage in Journal.write_sectors() for details.
/// TODO: allow testing in both modes.
pub const synchronicity: enum {
always_synchronous,
always_asynchronous,
} = .always_asynchronous;
pub const Read = struct {
callback: fn (read: *Storage.Read) void,
buffer: []u8,
offset: u64,
/// Tick at which this read is considered "completed" and the callback should be called.
done_at_tick: u64,
fn less_than(context: void, a: *Read, b: *Read) math.Order {
_ = context;
return math.order(a.done_at_tick, b.done_at_tick);
}
};
pub const Write = struct {
callback: fn (write: *Storage.Write) void,
buffer: []const u8,
offset: u64,
/// Tick at which this write is considered "completed" and the callback should be called.
done_at_tick: u64,
fn less_than(context: void, a: *Write, b: *Write) math.Order {
_ = context;
return math.order(a.done_at_tick, b.done_at_tick);
}
};
/// Faulty areas are always sized to message_size_max
/// If the faulty areas of all replicas are superimposed, the padding between them is always message_size_max.
/// For a single replica, the padding between faulty areas depends on the number of other replicas.
pub const FaultyAreas = struct {
first_offset: u64,
period: u64,
};
memory: []align(config.sector_size) u8,
size: u64,
/// Set bits correspond to faulty sectors. The underlying sectors of `memory` is left clean.
faults: std.DynamicBitSetUnmanaged,
options: Options,
replica_index: u8,
prng: std.rand.DefaultPrng,
// We can't allow storage faults for the same message in a majority of
// the replicas as that would make recovery impossible. Instead, we only
// allow faults in certain areas which differ between replicas.
faulty_areas: FaultyAreas,
/// Whether to enable faults (when false, this supersedes `faulty_areas`).
/// This is used to disable faults during the replica's first startup.
faulty: bool = true,
reads: std.PriorityQueue(*Storage.Read, void, Storage.Read.less_than),
writes: std.PriorityQueue(*Storage.Write, void, Storage.Write.less_than),
ticks: u64 = 0,
pub fn init(
allocator: mem.Allocator,
size: u64,
options: Storage.Options,
replica_index: u8,
faulty_areas: FaultyAreas,
) !Storage {
assert(options.write_latency_mean >= options.write_latency_min);
assert(options.read_latency_mean >= options.read_latency_min);
const memory = try allocator.allocAdvanced(u8, config.sector_size, size, .exact);
errdefer allocator.free(memory);
// TODO: random data
mem.set(u8, memory, 0);
var faults = try std.DynamicBitSetUnmanaged.initEmpty(
allocator,
@divExact(size, config.sector_size),
);
errdefer faults.deinit(allocator);
var reads = std.PriorityQueue(*Storage.Read, void, Storage.Read.less_than).init(allocator, {});
errdefer reads.deinit();
try reads.ensureTotalCapacity(config.io_depth_read);
var writes = std.PriorityQueue(*Storage.Write, void, Storage.Write.less_than).init(allocator, {});
errdefer writes.deinit();
try writes.ensureTotalCapacity(config.io_depth_write);
return Storage{
.memory = memory,
.size = size,
.faults = faults,
.options = options,
.replica_index = replica_index,
.prng = std.rand.DefaultPrng.init(options.seed),
.faulty_areas = faulty_areas,
.reads = reads,
.writes = writes,
};
}
/// Cancel any currently in progress reads/writes but leave the stored data untouched.
pub fn reset(storage: *Storage) void {
while (storage.writes.peek()) |write| {
_ = storage.writes.remove();
storage.fault_sectors(write.offset, write.buffer.len);
}
storage.reads.len = 0;
assert(storage.writes.len == 0);
}
pub fn deinit(storage: *Storage, allocator: mem.Allocator) void {
allocator.free(storage.memory);
storage.faults.deinit(allocator);
storage.reads.deinit();
storage.writes.deinit();
}
pub fn tick(storage: *Storage) void {
storage.ticks += 1;
while (storage.reads.peek()) |read| {
if (read.done_at_tick > storage.ticks) break;
_ = storage.reads.remove();
storage.read_sectors_finish(read);
}
while (storage.writes.peek()) |write| {
if (write.done_at_tick > storage.ticks) break;
_ = storage.writes.remove();
storage.write_sectors_finish(write);
}
}
pub fn read_sectors(
storage: *Storage,
callback: fn (read: *Storage.Read) void,
read: *Storage.Read,
buffer: []u8,
offset: u64,
) void {
storage.assert_bounds_and_alignment(buffer, offset);
read.* = .{
.callback = callback,
.buffer = buffer,
.offset = offset,
.done_at_tick = storage.ticks + storage.read_latency(),
};
// We ensure the capacity is sufficient for config.io_depth_read in init()
storage.reads.add(read) catch unreachable;
}
fn read_sectors_finish(storage: *Storage, read: *Storage.Read) void {
mem.copy(u8, read.buffer, storage.memory[read.offset..][0..read.buffer.len]);
if (storage.x_in_100(storage.options.read_fault_probability)) {
storage.fault_sectors(read.offset, read.buffer.len);
}
if (storage.faulty) {
// Corrupt faulty sectors.
const sector_min = @divExact(read.offset, config.sector_size);
var sector: usize = 0;
while (sector < @divExact(read.buffer.len, config.sector_size)) : (sector += 1) {
if (storage.faults.isSet(sector_min + sector)) {
const faulty_sector_offset = sector * config.sector_size;
const faulty_sector_bytes = read.buffer[faulty_sector_offset..][0..config.sector_size];
storage.prng.random().bytes(faulty_sector_bytes);
}
}
}
read.callback(read);
}
pub fn write_sectors(
storage: *Storage,
callback: fn (write: *Storage.Write) void,
write: *Storage.Write,
buffer: []const u8,
offset: u64,
) void {
storage.assert_bounds_and_alignment(buffer, offset);
// Verify that there are no concurrent overlapping writes.
var iterator = storage.writes.iterator();
while (iterator.next()) |other| {
assert(offset + buffer.len <= other.offset or
other.offset + other.buffer.len <= offset);
}
write.* = .{
.callback = callback,
.buffer = buffer,
.offset = offset,
.done_at_tick = storage.ticks + storage.write_latency(),
};
// We ensure the capacity is sufficient for config.io_depth_write in init()
storage.writes.add(write) catch unreachable;
}
fn write_sectors_finish(storage: *Storage, write: *Storage.Write) void {
mem.copy(u8, storage.memory[write.offset..][0..write.buffer.len], write.buffer);
{
const sector_min = @divExact(write.offset, config.sector_size);
const sector_max = @divExact(write.offset + write.buffer.len, config.sector_size);
var sector: usize = sector_min;
while (sector < sector_max) : (sector += 1) storage.faults.unset(sector);
}
if (storage.x_in_100(storage.options.write_fault_probability)) {
storage.fault_sectors(write.offset, write.buffer.len);
}
write.callback(write);
}
fn assert_bounds_and_alignment(storage: *const Storage, buffer: []const u8, offset: u64) void {
assert(buffer.len > 0);
assert(offset + buffer.len <= storage.size);
// Ensure that the read or write is aligned correctly for Direct I/O:
// If this is not the case, the underlying syscall will return EINVAL.
assert(@mod(@ptrToInt(buffer.ptr), config.sector_size) == 0);
assert(@mod(buffer.len, config.sector_size) == 0);
assert(@mod(offset, config.sector_size) == 0);
}
fn read_latency(storage: *Storage) u64 {
return storage.latency(storage.options.read_latency_min, storage.options.read_latency_mean);
}
fn write_latency(storage: *Storage) u64 {
return storage.latency(storage.options.write_latency_min, storage.options.write_latency_mean);
}
fn latency(storage: *Storage, min: u64, mean: u64) u64 {
return min + @floatToInt(u64, @intToFloat(f64, mean - min) * storage.prng.random().floatExp(f64));
}
/// Return true with probability x/100.
fn x_in_100(storage: *Storage, x: u8) bool {
assert(x <= 100);
return x > storage.prng.random().uintLessThan(u8, 100);
}
fn random_uint_between(storage: *Storage, comptime T: type, min: T, max: T) T {
return min + storage.prng.random().uintLessThan(T, max - min);
}
/// The return value is a slice into the provided out array.
pub fn generate_faulty_areas(
prng: std.rand.Random,
size: u64,
replica_count: u8,
out: *[config.replicas_max]FaultyAreas,
) []FaultyAreas {
comptime assert(config.message_size_max % config.sector_size == 0);
const message_size_max = config.message_size_max;
// We need to ensure there is message_size_max fault-free padding
// between faulty areas of memory so that a single message
// cannot straddle the corruptable areas of a majority of replicas.
comptime assert(config.replicas_max == 6);
switch (replica_count) {
1 => {
// If there is only one replica in the cluster, storage faults are not recoverable.
out[0] = .{ .first_offset = size, .period = 1 };
},
2 => {
// 0123456789
// 0X X X
// 1 X X X
out[0] = .{ .first_offset = 0 * message_size_max, .period = 4 * message_size_max };
out[1] = .{ .first_offset = 2 * message_size_max, .period = 4 * message_size_max };
},
3 => {
// 0123456789
// 0X X
// 1 X X
// 2 X X
out[0] = .{ .first_offset = 0 * message_size_max, .period = 6 * message_size_max };
out[1] = .{ .first_offset = 2 * message_size_max, .period = 6 * message_size_max };
out[2] = .{ .first_offset = 4 * message_size_max, .period = 6 * message_size_max };
},
4 => {
// 0123456789
// 0X X X
// 1X X X
// 2 X X X
// 3 X X X
out[0] = .{ .first_offset = 0 * message_size_max, .period = 4 * message_size_max };
out[1] = .{ .first_offset = 0 * message_size_max, .period = 4 * message_size_max };
out[2] = .{ .first_offset = 2 * message_size_max, .period = 4 * message_size_max };
out[3] = .{ .first_offset = 2 * message_size_max, .period = 4 * message_size_max };
},
5 => {
// 0123456789
// 0X X
// 1X X
// 2 X X
// 3 X X
// 4 X X
out[0] = .{ .first_offset = 0 * message_size_max, .period = 6 * message_size_max };
out[1] = .{ .first_offset = 0 * message_size_max, .period = 6 * message_size_max };
out[2] = .{ .first_offset = 2 * message_size_max, .period = 6 * message_size_max };
out[3] = .{ .first_offset = 2 * message_size_max, .period = 6 * message_size_max };
out[4] = .{ .first_offset = 4 * message_size_max, .period = 6 * message_size_max };
},
6 => {
// 0123456789
// 0X X
// 1X X
// 2 X X
// 3 X X
// 4 X X
// 5 X X
out[0] = .{ .first_offset = 0 * message_size_max, .period = 6 * message_size_max };
out[1] = .{ .first_offset = 0 * message_size_max, .period = 6 * message_size_max };
out[2] = .{ .first_offset = 2 * message_size_max, .period = 6 * message_size_max };
out[3] = .{ .first_offset = 2 * message_size_max, .period = 6 * message_size_max };
out[4] = .{ .first_offset = 4 * message_size_max, .period = 6 * message_size_max };
out[5] = .{ .first_offset = 4 * message_size_max, .period = 6 * message_size_max };
},
else => unreachable,
}
{
// Allow at most `f` faulty replicas to ensure the view change can succeed.
// TODO Allow more than `f` faulty replicas when the fault is to the right of the
// highest known replica.op (and to the left of the last checkpointed op).
const majority = @divFloor(replica_count, 2) + 1;
const quorum_replication = std.math.min(config.quorum_replication_max, majority);
const quorum_view_change = std.math.max(
replica_count - quorum_replication + 1,
majority,
);
var i: usize = quorum_view_change;
while (i < replica_count) : (i += 1) {
out[i].first_offset = size;
}
}
prng.shuffle(FaultyAreas, out[0..replica_count]);
return out[0..replica_count];
}
const SectorRange = struct {
min: usize, // inclusive sector index
max: usize, // exclusive sector index
};
/// Given an offset and size of a read/write, returns the range of any faulty sectors touched
/// by the read/write.
fn faulty_sectors(storage: *const Storage, offset: u64, size: u64) ?SectorRange {
assert(size <= config.message_size_max);
const message_size_max = config.message_size_max;
const period = storage.faulty_areas.period;
const faulty_offset = storage.faulty_areas.first_offset + (offset / period) * period;
const start = std.math.max(offset, faulty_offset);
const end = std.math.min(offset + size, faulty_offset + message_size_max);
// The read/write does not touch any faulty sectors.
if (start >= end) return null;
return SectorRange{
.min = @divExact(start, config.sector_size),
.max = @divExact(end, config.sector_size),
};
}
fn fault_sectors(storage: *Storage, offset: u64, size: u64) void {
const faulty = storage.faulty_sectors(offset, size) orelse return;
// Randomly corrupt one of the faulty sectors the operation targeted.
// TODO: inject more realistic and varied storage faults as described above.
const faulty_sector = storage.random_uint_between(usize, faulty.min, faulty.max);
log.info("corrupting sector {} by replica {}", .{
faulty_sector,
storage.replica_index,
});
storage.faults.set(faulty_sector);
}
};