forked from tigerbeetle/tigerbeetle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vopr.zig
1273 lines (1112 loc) · 54.1 KB
/
vopr.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
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
const std = @import("std");
const stdx = @import("./stdx.zig");
const builtin = @import("builtin");
const assert = std.debug.assert;
const maybe = stdx.maybe;
const mem = std.mem;
const constants = @import("constants.zig");
const flags = @import("./flags.zig");
const schema = @import("lsm/schema.zig");
const vsr = @import("vsr.zig");
const Header = vsr.Header;
const vsr_vopr_options = @import("vsr_vopr_options");
const state_machine = vsr_vopr_options.state_machine;
const StateMachineType = switch (state_machine) {
.accounting => @import("state_machine.zig").StateMachineType,
.testing => @import("testing/state_machine.zig").StateMachineType,
};
const Client = @import("testing/cluster.zig").Client;
const Cluster = @import("testing/cluster.zig").ClusterType(StateMachineType);
const Release = @import("testing/cluster.zig").Release;
const StateMachine = Cluster.StateMachine;
const Failure = @import("testing/cluster.zig").Failure;
const PartitionMode = @import("testing/packet_simulator.zig").PartitionMode;
const PartitionSymmetry = @import("testing/packet_simulator.zig").PartitionSymmetry;
const Core = @import("testing/cluster/network.zig").Network.Core;
const ReplySequence = @import("testing/reply_sequence.zig").ReplySequence;
const IdPermutation = @import("testing/id.zig").IdPermutation;
const Message = @import("message_pool.zig").MessagePool.Message;
const releases = [_]Release{
.{
.release = vsr.Release.from(.{ .major = 0, .minor = 0, .patch = 1 }),
.release_client_min = vsr.Release.from(.{ .major = 0, .minor = 0, .patch = 1 }),
},
.{
.release = vsr.Release.from(.{ .major = 0, .minor = 0, .patch = 2 }),
.release_client_min = vsr.Release.from(.{ .major = 0, .minor = 0, .patch = 1 }),
},
.{
.release = vsr.Release.from(.{ .major = 0, .minor = 0, .patch = 3 }),
.release_client_min = vsr.Release.from(.{ .major = 0, .minor = 0, .patch = 1 }),
},
};
pub const output = std.log.scoped(.cluster);
const log = std.log.scoped(.simulator);
pub const std_options = .{
// The -Dsimulator-log=<full|short> build option selects two logging modes.
// In "short" mode, only state transitions are printed (see `Cluster.log_replica`).
// "full" mode is the usual logging according to the level.
.log_level = if (vsr_vopr_options.log == .short) .info else .debug,
.logFn = log_override,
// Uncomment if you need per-scope control over the log levels.
// pub const log_scope_levels: []const std.log.ScopeLevel = &.{
// .{ .scope = .cluster, .level = .info },
// .{ .scope = .replica, .level = .debug },
// };
};
pub const tigerbeetle_config = @import("config.zig").configs.test_min;
const cluster_id = 0;
const CLIArgs = struct {
// "lite" mode runs a small cluster and only looks for crashes.
lite: bool = false,
ticks_max_requests: u32 = 40_000_000,
ticks_max_convergence: u32 = 10_000_000,
positional: struct {
seed: ?[]const u8 = null,
},
};
pub fn main() !void {
// This must be initialized at runtime as stderr is not comptime known on e.g. Windows.
log_buffer.unbuffered_writer = std.io.getStdErr().writer();
// TODO Use std.testing.allocator when all deinit() leaks are fixed.
const allocator = std.heap.page_allocator;
var args = try std.process.argsWithAllocator(allocator);
defer args.deinit();
const cli_args = flags.parse(&args, CLIArgs);
const seed_random = std.crypto.random.int(u64);
const seed = seed_from_arg: {
const seed_argument = cli_args.positional.seed orelse break :seed_from_arg seed_random;
break :seed_from_arg vsr.testing.parse_seed(seed_argument);
};
if (builtin.mode == .ReleaseFast or builtin.mode == .ReleaseSmall) {
// We do not support ReleaseFast or ReleaseSmall because they disable assertions.
@panic("the simulator must be run with -OReleaseSafe");
}
if (seed == seed_random) {
if (builtin.mode != .ReleaseSafe) {
// If no seed is provided, than Debug is too slow and ReleaseSafe is much faster.
@panic("no seed provided: the simulator must be run with -OReleaseSafe");
}
if (vsr_vopr_options.log != .short) {
output.warn("no seed provided: full debug logs are enabled, this will be slow", .{});
}
}
var prng = std.rand.DefaultPrng.init(seed);
const random = prng.random();
const replica_count =
if (cli_args.lite) 3 else 1 + random.uintLessThan(u8, constants.replicas_max);
const standby_count =
if (cli_args.lite) 0 else random.uintAtMost(u8, constants.standbys_max);
const node_count = replica_count + standby_count;
// -1 since otherwise it is possible that all clients will evict each other.
// (Due to retried register messages from the first set of evicted clients.
// See the "Cluster: eviction: session_too_low" replica test for a related scenario.)
const client_count = @max(1, random.uintAtMost(u8, constants.clients_max * 2 - 1));
const batch_size_limit_min = comptime batch_size_limit_min: {
var event_size_max: u32 = @sizeOf(vsr.RegisterRequest);
for (std.enums.values(StateMachine.Operation)) |operation| {
const event_size = @sizeOf(StateMachine.EventType(operation));
event_size_max = @max(event_size_max, event_size);
}
break :batch_size_limit_min event_size_max;
};
const batch_size_limit: u32 = if (random.boolean())
constants.message_body_size_max
else
batch_size_limit_min +
random.uintAtMost(u32, constants.message_body_size_max - batch_size_limit_min);
const MiB = 1024 * 1024;
const storage_size_limit = vsr.sector_floor(
200 * MiB - random.uintLessThan(u64, 20 * MiB),
);
const cluster_options = Cluster.Options{
.cluster_id = cluster_id,
.replica_count = replica_count,
.standby_count = standby_count,
.client_count = client_count,
.storage_size_limit = storage_size_limit,
.seed = random.int(u64),
.releases = &releases,
.client_release = releases[0].release,
.network = .{
.node_count = node_count,
.client_count = client_count,
.seed = random.int(u64),
.one_way_delay_mean = 3 + random.uintLessThan(u16, 10),
.one_way_delay_min = random.uintLessThan(u16, 3),
.packet_loss_probability = random.uintLessThan(u8, 30),
.path_maximum_capacity = 2 + random.uintLessThan(u8, 19),
.path_clog_duration_mean = random.uintLessThan(u16, 500),
.path_clog_probability = random.uintLessThan(u8, 2),
.packet_replay_probability = random.uintLessThan(u8, 50),
.partition_mode = random_partition_mode(random),
.partition_symmetry = random_partition_symmetry(random),
.partition_probability = random.uintLessThan(u8, 3),
.unpartition_probability = 1 + random.uintLessThan(u8, 10),
.partition_stability = 100 + random.uintLessThan(u32, 100),
.unpartition_stability = random.uintLessThan(u32, 20),
},
.storage = .{
.seed = random.int(u64),
.read_latency_min = random.uintLessThan(u16, 3),
.read_latency_mean = 3 + random.uintLessThan(u16, 10),
.write_latency_min = random.uintLessThan(u16, 3),
.write_latency_mean = 3 + random.uintLessThan(u16, 100),
.read_fault_probability = random.uintLessThan(u8, 10),
.write_fault_probability = random.uintLessThan(u8, 10),
.crash_fault_probability = 80 + random.uintLessThan(u8, 21),
},
.storage_fault_atlas = .{
.faulty_superblock = true,
.faulty_wal_headers = replica_count > 1,
.faulty_wal_prepares = replica_count > 1,
.faulty_client_replies = replica_count > 1,
// >2 instead of >1 because in R=2, a lagging replica may sync to the leading replica,
// but then the leading replica may have the only copy of a block in the cluster.
.faulty_grid = replica_count > 2,
},
.state_machine = switch (state_machine) {
.testing => .{
.batch_size_limit = batch_size_limit,
.lsm_forest_node_count = 4096,
},
.accounting => .{
.batch_size_limit = batch_size_limit,
.lsm_forest_compaction_block_count = random.uintAtMost(u32, 256) +
StateMachine.Forest.Options.compaction_block_count_min,
.lsm_forest_node_count = 4096,
.cache_entries_accounts = 256,
.cache_entries_transfers = 256,
.cache_entries_posted = 256,
.cache_entries_account_balances = 256,
},
},
.on_cluster_reply = Simulator.on_cluster_reply,
.on_client_reply = Simulator.on_client_reply,
};
const workload_options = StateMachine.Workload.Options.generate(random, .{
.batch_size_limit = batch_size_limit,
.client_count = client_count,
// TODO(DJ) Once Workload no longer needs in_flight_max, make stalled_queue_capacity
// private. Also maybe make it dynamic (computed from the client_count instead of
// clients_max).
.in_flight_max = ReplySequence.stalled_queue_capacity,
});
const simulator_options = Simulator.Options{
.cluster = cluster_options,
.workload = workload_options,
// TODO Swarm testing: Test long+few crashes and short+many crashes separately.
.replica_crash_probability = 0.00002,
.replica_crash_stability = random.uintLessThan(u32, 1_000),
.replica_restart_probability = 0.0002,
.replica_restart_stability = random.uintLessThan(u32, 1_000),
.replica_release_advance_probability = 0.0001,
.replica_release_catchup_probability = 0.001,
.requests_max = constants.journal_slot_count * 3,
.request_probability = 1 + random.uintLessThan(u8, 99),
.request_idle_on_probability = random.uintLessThan(u8, 20),
.request_idle_off_probability = 10 + random.uintLessThan(u8, 10),
};
output.info(
\\
\\ SEED={}
\\
\\ replicas={}
\\ standbys={}
\\ clients={}
\\ request_probability={}%
\\ idle_on_probability={}%
\\ idle_off_probability={}%
\\ one_way_delay_mean={} ticks
\\ one_way_delay_min={} ticks
\\ packet_loss_probability={}%
\\ path_maximum_capacity={} messages
\\ path_clog_duration_mean={} ticks
\\ path_clog_probability={}%
\\ packet_replay_probability={}%
\\ partition_mode={}
\\ partition_symmetry={}
\\ partition_probability={}%
\\ unpartition_probability={}%
\\ partition_stability={} ticks
\\ unpartition_stability={} ticks
\\ read_latency_min={}
\\ read_latency_mean={}
\\ write_latency_min={}
\\ write_latency_mean={}
\\ read_fault_probability={}%
\\ write_fault_probability={}%
\\ crash_probability={d}%
\\ crash_stability={} ticks
\\ restart_probability={d}%
\\ restart_stability={} ticks
, .{
seed,
cluster_options.replica_count,
cluster_options.standby_count,
cluster_options.client_count,
simulator_options.request_probability,
simulator_options.request_idle_on_probability,
simulator_options.request_idle_off_probability,
cluster_options.network.one_way_delay_mean,
cluster_options.network.one_way_delay_min,
cluster_options.network.packet_loss_probability,
cluster_options.network.path_maximum_capacity,
cluster_options.network.path_clog_duration_mean,
cluster_options.network.path_clog_probability,
cluster_options.network.packet_replay_probability,
cluster_options.network.partition_mode,
cluster_options.network.partition_symmetry,
cluster_options.network.partition_probability,
cluster_options.network.unpartition_probability,
cluster_options.network.partition_stability,
cluster_options.network.unpartition_stability,
cluster_options.storage.read_latency_min,
cluster_options.storage.read_latency_mean,
cluster_options.storage.write_latency_min,
cluster_options.storage.write_latency_mean,
cluster_options.storage.read_fault_probability,
cluster_options.storage.write_fault_probability,
simulator_options.replica_crash_probability,
simulator_options.replica_crash_stability,
simulator_options.replica_restart_probability,
simulator_options.replica_restart_stability,
});
var simulator = try Simulator.init(allocator, random, simulator_options);
defer simulator.deinit(allocator);
for (0..simulator.cluster.clients.len) |client_index| {
simulator.cluster.register(client_index);
}
// Safety: replicas crash and restart; at any given point in time arbitrarily many replicas may
// be crashed, but each replica restarts eventually. The cluster must process all requests
// without split-brain.
var tick_total: u64 = 0;
var tick: u64 = 0;
while (tick < cli_args.ticks_max_requests) : (tick += 1) {
const requests_replied_old = simulator.requests_replied;
simulator.tick();
tick_total += 1;
if (simulator.requests_replied > requests_replied_old) {
tick = 0;
}
const requests_done = simulator.requests_replied == simulator.options.requests_max;
const upgrades_done =
for (simulator.cluster.replicas, simulator.cluster.replica_health) |*replica, health|
{
if (health == .down) continue;
const release_latest = releases[simulator.replica_releases_limit - 1].release;
if (replica.release.value == release_latest.value) {
break true;
}
} else false;
if (requests_done and upgrades_done) break;
} else {
output.info(
"no liveness, final cluster state (requests_max={} requests_replied={}):",
.{ simulator.options.requests_max, simulator.requests_replied },
);
simulator.cluster.log_cluster();
if (cli_args.lite) return;
output.err("you can reproduce this failure with seed={}", .{seed});
fatal(.liveness, "unable to complete requests_committed_max before ticks_max", .{});
}
if (cli_args.lite) return;
simulator.transition_to_liveness_mode();
// Liveness: a core set of replicas is up and fully connected. The rest of replicas might be
// crashed or partitioned permanently. The core should converge to the same state.
tick = 0;
while (tick < cli_args.ticks_max_convergence) : (tick += 1) {
simulator.tick();
tick_total += 1;
if (simulator.pending() == null) {
break;
}
}
if (simulator.pending()) |reason| {
if (simulator.core_missing_primary()) {
stdx.unimplemented("repair requires reachable primary");
} else if (simulator.core_missing_quorum()) {
output.warn("no liveness, core replicas cannot view-change", .{});
} else if (simulator.core_missing_prepare()) |header| {
output.warn("no liveness, op={} is not available in core", .{header.op});
} else if (try simulator.core_missing_blocks(allocator)) |blocks| {
output.warn("no liveness, {} blocks are not available in core", .{blocks});
} else {
output.info("no liveness, final cluster state (core={b}):", .{simulator.core.mask});
simulator.cluster.log_cluster();
output.err("you can reproduce this failure with seed={}", .{seed});
fatal(.liveness, "no state convergence: {s}", .{reason});
}
} else {
const commits = simulator.cluster.state_checker.commits.items;
const last_checksum = commits[commits.len - 1].header.checksum;
for (simulator.cluster.aofs, 0..) |*aof, replica_index| {
if (simulator.core.isSet(replica_index)) {
try aof.validate(last_checksum);
} else {
try aof.validate(null);
}
}
}
output.info("\n PASSED ({} ticks)", .{tick_total});
}
pub const Simulator = struct {
pub const Options = struct {
cluster: Cluster.Options,
workload: StateMachine.Workload.Options,
/// Probability per tick that a crash will occur.
replica_crash_probability: f64,
/// Minimum duration of a crash.
replica_crash_stability: u32,
/// Probability per tick that a crashed replica will recovery.
replica_restart_probability: f64,
/// Minimum time a replica is up until it is crashed again.
replica_restart_stability: u32,
/// Probability per tick that a healthy replica will be crash-upgraded.
/// This probability is set to 0 during liveness mode.
replica_release_advance_probability: f64,
/// Probability that a crashed with an outdated version will be upgraded as it restarts.
/// This helps ensure that when the cluster upgrades, that replicas without the newest
/// version don't take too long to receive that new version.
/// This probability is set to 0 during liveness mode.
replica_release_catchup_probability: f64,
/// The total number of requests to send. Does not count `register` messages.
requests_max: usize,
request_probability: u8, // percent
request_idle_on_probability: u8, // percent
request_idle_off_probability: u8, // percent
};
random: std.rand.Random,
options: Options,
cluster: *Cluster,
workload: StateMachine.Workload,
// The number of releases in each replica's "binary".
replica_releases: []usize,
/// The maximum number of releases available in any replica's "binary".
/// (i.e. the maximum of any `replica_releases`.)
replica_releases_limit: usize = 1,
/// Protect a replica from fast successive crash/restarts.
replica_stability: []usize,
reply_sequence: ReplySequence,
reply_op_next: u64 = 1, // Skip the root op.
/// Fully-connected subgraph of replicas for liveness checking.
core: Core = Core.initEmpty(),
/// Total number of requests sent, including those that have not been delivered.
/// Does not include `register` messages.
requests_sent: usize = 0,
/// Total number of replies received by non-evicted clients.
/// Does not include `register` messages.
requests_replied: usize = 0,
requests_idle: bool = false,
pub fn init(
allocator: std.mem.Allocator,
random: std.rand.Random,
options: Options,
) !Simulator {
assert(options.replica_crash_probability < 100.0);
assert(options.replica_crash_probability >= 0.0);
assert(options.replica_restart_probability < 100.0);
assert(options.replica_restart_probability >= 0.0);
assert(options.requests_max > 0);
assert(options.request_probability > 0);
assert(options.request_probability <= 100);
assert(options.request_idle_on_probability <= 100);
assert(options.request_idle_off_probability > 0);
assert(options.request_idle_off_probability <= 100);
var cluster = try Cluster.init(allocator, options.cluster);
errdefer cluster.deinit();
var workload = try StateMachine.Workload.init(allocator, random, options.workload);
errdefer workload.deinit(allocator);
const replica_releases = try allocator.alloc(
usize,
options.cluster.replica_count + options.cluster.standby_count,
);
errdefer allocator.free(replica_releases);
@memset(replica_releases, 1);
const replica_stability = try allocator.alloc(
usize,
options.cluster.replica_count + options.cluster.standby_count,
);
errdefer allocator.free(replica_stability);
@memset(replica_stability, 0);
var reply_sequence = try ReplySequence.init(allocator);
errdefer reply_sequence.deinit(allocator);
return Simulator{
.random = random,
.options = options,
.cluster = cluster,
.workload = workload,
.replica_releases = replica_releases,
.replica_stability = replica_stability,
.reply_sequence = reply_sequence,
};
}
pub fn deinit(simulator: *Simulator, allocator: std.mem.Allocator) void {
allocator.free(simulator.replica_releases);
allocator.free(simulator.replica_stability);
simulator.reply_sequence.deinit(allocator);
simulator.workload.deinit(allocator);
simulator.cluster.deinit();
}
pub fn pending(simulator: *const Simulator) ?[]const u8 {
assert(simulator.core.count() > 0);
assert(simulator.requests_sent - simulator.requests_cancelled() ==
simulator.options.requests_max);
assert(simulator.reply_sequence.empty());
for (
simulator.cluster.clients,
simulator.cluster.client_eviction_reasons,
) |*client, reason| {
if (reason == null) {
if (client.request_inflight) |request| {
// Registration isn't counted by requests_sent, so an operation=register may
// still be in-flight. Any other requests should already be complete before
// done() is called.
assert(request.message.header.operation == .register);
return "pending register request";
}
}
}
// Even though there are no client requests in progress, the cluster may be upgrading.
const release_max = simulator.core_release_max();
for (simulator.cluster.replicas) |*replica| {
if (simulator.core.isSet(replica.replica)) {
// (If down, the replica is waiting to be upgraded.)
maybe(simulator.cluster.replica_health[replica.replica] == .down);
if (replica.release.value != release_max.value) return "pending upgrade";
}
}
for (simulator.cluster.replicas) |*replica| {
if (simulator.core.isSet(replica.replica)) {
if (!simulator.cluster.state_checker.replica_convergence(replica.replica)) {
return "pending replica convergence";
}
}
}
simulator.cluster.state_checker.assert_cluster_convergence();
// Check whether the replica is still repairing prepares/tables/replies.
const commit_max: u64 = simulator.cluster.state_checker.commits.items.len - 1;
for (simulator.cluster.replicas) |*replica| {
if (simulator.core.isSet(replica.replica)) {
for (replica.op_checkpoint() + 1..commit_max + 1) |op| {
const header = simulator.cluster.state_checker.header_with_op(op);
if (!replica.journal.has_clean(&header)) return "pending journal";
}
// It's okay for a replica to miss some prepares older than the current checkpoint.
maybe(replica.journal.faulty.count > 0);
if (!replica.sync_content_done()) return "pending sync content";
}
}
// Expect that all core replicas have arrived at an identical (non-divergent) checkpoint.
var checkpoint_id: ?u128 = null;
for (simulator.cluster.replicas) |*replica| {
if (simulator.core.isSet(replica.replica)) {
const replica_checkpoint_id = replica.superblock.working.checkpoint_id();
if (checkpoint_id) |id| {
assert(checkpoint_id == id);
} else {
checkpoint_id = replica_checkpoint_id;
}
}
}
assert(checkpoint_id != null);
return null;
}
pub fn tick(simulator: *Simulator) void {
// TODO(Zig): Remove (see on_cluster_reply()).
simulator.cluster.context = simulator;
simulator.cluster.tick();
simulator.tick_requests();
simulator.tick_crash();
}
/// Executes the following:
/// * Pick a quorum of replicas to be fully available (the core)
/// * Restart any core replicas that are down at the moment
/// * Heal all network partitions between core replicas
/// * Disable storage faults on the core replicas
/// * For all failures involving non-core replicas, make those failures permanent.
///
/// See https://tigerbeetle.com/blog/2023-07-06-simulation-testing-for-liveness for broader
/// context.
pub fn transition_to_liveness_mode(simulator: *Simulator) void {
simulator.core = random_core(
simulator.random,
simulator.options.cluster.replica_count,
simulator.options.cluster.standby_count,
);
log.debug("transition_to_liveness_mode: core={b}", .{simulator.core.mask});
var it = simulator.core.iterator(.{});
while (it.next()) |replica_index| {
const fault = false;
if (simulator.cluster.replica_health[replica_index] == .down) {
simulator.restart_replica(@intCast(replica_index), fault);
}
simulator.cluster.storages[replica_index].transition_to_liveness_mode();
}
simulator.cluster.network.transition_to_liveness_mode(simulator.core);
simulator.options.replica_crash_probability = 0;
simulator.options.replica_restart_probability = 0;
simulator.options.replica_release_advance_probability = 0;
simulator.options.replica_release_catchup_probability = 0;
}
// If a primary ends up being outside of a core, and is only partially connected to the core,
// the core might fail to converge, as parts of the repair protocol rely on primary-sent
// `.start_view_change` messages. Until we fix this issue, we special-case this scenario in
// VOPR and don't treat it as a liveness failure.
//
// TODO: make sure that .recovering_head replicas can transition to normal even without direct
// connection to the primary
pub fn core_missing_primary(simulator: *const Simulator) bool {
assert(simulator.core.count() > 0);
for (simulator.cluster.replicas) |*replica| {
if (simulator.cluster.replica_health[replica.replica] == .up and
replica.status == .normal and replica.primary() and
!simulator.core.isSet(replica.replica))
{
// `replica` considers itself a primary, check that at least part of the core thinks
// so as well.
var it = simulator.core.iterator(.{});
while (it.next()) |replica_core_index| {
if (simulator.cluster.replicas[replica_core_index].view == replica.view) {
return true;
}
}
}
}
return false;
}
/// The core contains at least a view-change quorum of replicas. But if one or more of those
/// replicas are in status=recovering_head (due to corruption or state sync), then that may be
/// insufficient.
/// TODO: State sync can trigger recovering_head without any crashes, and we should be able to
/// recover in that case.
/// (See https://github.com/tigerbeetle/tigerbeetle/pull/933#discussion_r1245440623)
pub fn core_missing_quorum(simulator: *const Simulator) bool {
assert(simulator.core.count() > 0);
var core_replicas: usize = 0;
var core_recovering_head: usize = 0;
for (simulator.cluster.replicas) |*replica| {
if (simulator.core.isSet(replica.replica) and !replica.standby()) {
core_replicas += 1;
core_recovering_head += @intFromBool(replica.status == .recovering_head);
}
}
if (core_recovering_head == 0) return false;
const quorums = vsr.quorums(simulator.options.cluster.replica_count);
return quorums.view_change > core_replicas - core_recovering_head;
}
// Returns a header for a prepare which can't be repaired by the core due to storage faults.
//
// If a replica cannot make progress on committing, then it may be stuck while repairing either
// missing headers *or* prepares (see `repair` in replica.zig). This function checks for both.
//
// When generating a FaultAtlas, we don't try to protect core from excessive errors. Instead,
// if the core gets stuck, we verify that this is indeed due to storage faults.
pub fn core_missing_prepare(simulator: *const Simulator) ?vsr.Header.Prepare {
assert(simulator.core.count() > 0);
// Don't check for missing uncommitted ops (since the StateChecker does not record them).
// There may be uncommitted ops due to pulses/upgrades sent during liveness mode.
const cluster_commit_max: u64 = simulator.cluster.state_checker.commits.items.len - 1;
var cluster_op_checkpoint: u64 = 0;
for (simulator.cluster.replicas) |*replica| {
if (simulator.core.isSet(replica.replica) and !replica.standby()) {
cluster_op_checkpoint = @max(
cluster_op_checkpoint,
replica.superblock.working.vsr_state.checkpoint.header.op,
);
}
}
var missing_header_op: ?u64 = null;
var missing_prepare_op: ?u64 = null;
for (simulator.cluster.replicas) |replica| {
if (simulator.core.isSet(replica.replica) and !replica.standby()) {
assert(simulator.cluster.replica_health[replica.replica] == .up);
// Lagging replicas do not initiate WAL repair during view change.
if (replica.status == .view_change and
replica.op_checkpoint() < cluster_op_checkpoint) continue;
const commit_max = @min(replica.op, cluster_commit_max);
const op_repair_min = replica.op_repair_min();
// Check if replica's commit pipeline was stuck due to missing headers.
// Find latest missing header as we repair headers from high -> low ops.
if (replica.journal.find_latest_headers_break_between(
op_repair_min,
commit_max,
)) |range| {
if (missing_header_op == null or missing_header_op.? < range.op_max) {
missing_header_op = range.op_max;
}
} else {
// Check if replica's commit pipeline was stuck due to missing prepares.
// Find earliest missing prepare as we repair prepares from low -> high ops.
for (op_repair_min..commit_max + 1) |op| {
const header = simulator.cluster.state_checker.header_with_op(op);
if (!replica.journal.has_clean(&header)) {
if (missing_prepare_op == null or missing_prepare_op.? > op) {
missing_prepare_op = op;
}
}
}
}
}
}
if (missing_header_op == null and missing_prepare_op == null) return null;
const missing_op = if (missing_header_op) |op| op else missing_prepare_op.?;
const missing_header = simulator.cluster.state_checker.header_with_op(missing_op);
for (simulator.cluster.replicas) |replica| {
if (simulator.core.isSet(replica.replica) and !replica.standby()) {
if (replica.journal.has_clean(&missing_header)) {
// Prepare *was* found on an active core replica, so the header isn't
// actually missing.
return null;
}
}
}
return missing_header;
}
/// Check whether the cluster is stuck because the entire core is missing the same block[s].
pub fn core_missing_blocks(
simulator: *const Simulator,
allocator: std.mem.Allocator,
) error{OutOfMemory}!?usize {
assert(simulator.core.count() > 0);
var blocks_missing = std.ArrayList(struct {
replica: u8,
address: u64,
checksum: u128,
}).init(allocator);
defer blocks_missing.deinit();
// Find all blocks that any replica in the core is missing.
for (simulator.cluster.replicas) |replica| {
if (!simulator.core.isSet(replica.replica)) continue;
const storage = &simulator.cluster.storages[replica.replica];
var fault_iterator = replica.grid.read_global_queue.peek();
while (fault_iterator) |faulty_read| : (fault_iterator = faulty_read.next) {
try blocks_missing.append(.{
.replica = replica.replica,
.address = faulty_read.address,
.checksum = faulty_read.checksum,
});
log.debug("{}: core_missing_blocks: " ++
"missing address={} checksum={} corrupt={} (remote read)", .{
replica.replica,
faulty_read.address,
faulty_read.checksum,
storage.area_faulty(.{ .grid = .{ .address = faulty_read.address } }),
});
}
var repair_iterator = replica.grid.blocks_missing.faulty_blocks.iterator();
while (repair_iterator.next()) |fault| {
try blocks_missing.append(.{
.replica = replica.replica,
.address = fault.key_ptr.*,
.checksum = fault.value_ptr.checksum,
});
log.debug("{}: core_missing_blocks: " ++
"missing address={} checksum={} corrupt={} (GridBlocksMissing)", .{
replica.replica,
fault.key_ptr.*,
fault.value_ptr.checksum,
storage.area_faulty(.{ .grid = .{ .address = fault.key_ptr.* } }),
});
}
}
// Check whether every replica in the core is missing the blocks.
// (If any core replica has the block, then that is a bug, since it should have repaired.)
for (blocks_missing.items) |block_missing| {
for (simulator.cluster.replicas) |replica| {
const storage = &simulator.cluster.storages[replica.replica];
// A replica might actually have the block that it is requesting, but not know.
// This can occur after state sync: if we compact and create a table, but then skip
// over that table via state sync, we will try to sync the table anyway.
if (replica.replica == block_missing.replica) continue;
if (!simulator.core.isSet(replica.replica)) continue;
if (replica.standby()) continue;
if (storage.area_faulty(.{
.grid = .{ .address = block_missing.address },
})) continue;
const block = storage.grid_block(block_missing.address) orelse continue;
const block_header = schema.header_from_block(block);
if (block_header.checksum == block_missing.checksum) {
log.err("{}: core_missing_blocks: found address={} checksum={}", .{
replica.replica,
block_missing.address,
block_missing.checksum,
});
@panic("block found in core");
}
}
}
if (blocks_missing.items.len == 0) {
return null;
} else {
return blocks_missing.items.len;
}
}
fn core_release_max(simulator: *const Simulator) vsr.Release {
assert(simulator.core.count() > 0);
var release_max: vsr.Release = vsr.Release.zero;
for (simulator.cluster.replicas) |*replica| {
if (simulator.core.isSet(replica.replica)) {
release_max = release_max.max(replica.release);
if (replica.upgrade_release) |release| {
release_max = release_max.max(release);
}
}
}
assert(release_max.value > 0);
return release_max;
}
fn on_cluster_reply(
cluster: *Cluster,
reply_client: ?usize,
prepare: *const Message.Prepare,
reply: *const Message.Reply,
) void {
assert((reply_client == null) == (prepare.header.client == 0));
const simulator: *Simulator = @ptrCast(@alignCast(cluster.context.?));
if (reply.header.op < simulator.reply_op_next) return;
if (simulator.reply_sequence.contains(reply)) return;
simulator.reply_sequence.insert(reply_client, prepare, reply);
while (!simulator.reply_sequence.empty()) {
const op = simulator.reply_op_next;
const prepare_header = simulator.cluster.state_checker.commits.items[op].header;
assert(prepare_header.op == op);
if (simulator.reply_sequence.peek(op)) |commit| {
defer simulator.reply_sequence.next();
simulator.reply_op_next += 1;
assert(commit.reply.references == 1);
assert(commit.reply.header.op == op);
assert(commit.reply.header.command == .reply);
assert(commit.reply.header.request == commit.prepare.header.request);
assert(commit.reply.header.operation == commit.prepare.header.operation);
assert(commit.prepare.references == 1);
assert(commit.prepare.header.checksum == prepare_header.checksum);
assert(commit.prepare.header.command == .prepare);
log.debug("consume_stalled_replies: op={} operation={} client={} request={}", .{
commit.reply.header.op,
commit.reply.header.operation,
commit.prepare.header.client,
commit.prepare.header.request,
});
if (prepare_header.operation == .pulse) {
simulator.workload.on_pulse(
prepare_header.operation.cast(StateMachine),
prepare_header.timestamp,
);
}
if (!commit.prepare.header.operation.vsr_reserved()) {
simulator.workload.on_reply(
commit.client_index.?,
commit.reply.header.operation.cast(StateMachine),
commit.reply.header.timestamp,
commit.prepare.body_used(),
commit.reply.body_used(),
);
}
}
}
}
fn on_client_reply(
cluster: *Cluster,
reply_client: usize,
request: *const Message.Request,
reply: *const Message.Reply,
) void {
_ = reply;
const simulator: *Simulator = @ptrCast(@alignCast(cluster.context.?));
assert(simulator.cluster.client_eviction_reasons[reply_client] == null);
if (!request.header.operation.vsr_reserved()) {
simulator.requests_replied += 1;
}
}
/// Maybe send a request from one of the cluster's clients.
fn tick_requests(simulator: *Simulator) void {
if (simulator.requests_idle) {
if (chance(simulator.random, simulator.options.request_idle_off_probability)) {
simulator.requests_idle = false;
}
} else {
if (chance(simulator.random, simulator.options.request_idle_on_probability)) {
simulator.requests_idle = true;
}
}
if (simulator.requests_idle) return;
if (simulator.requests_sent - simulator.requests_cancelled() ==
simulator.options.requests_max) return;
if (!chance(simulator.random, simulator.options.request_probability)) return;
const client_index = index: {
const client_count = simulator.options.cluster.client_count;
const client_index_base =
simulator.random.uintLessThan(usize, client_count);
for (0..client_count) |offset| {
const client_index = (client_index_base + offset) % client_count;
if (simulator.cluster.client_eviction_reasons[client_index] == null) {
break :index client_index;
}
} else {
for (0..client_count) |index| {
assert(simulator.cluster.client_eviction_reasons[index] != null);
assert(simulator.cluster.client_eviction_reasons[index] == .no_session or
simulator.cluster.client_eviction_reasons[index] == .session_too_low);
}
stdx.unimplemented("client replacement; all clients were evicted");
}
};
var client = &simulator.cluster.clients[client_index];
// Messages aren't added to the ReplySequence until a reply arrives.
// Before sending a new message, make sure there will definitely be room for it.
var reserved: usize = 0;
for (simulator.cluster.clients) |*c| {
// Count the number of clients that are still waiting for a `register` to complete,
// since they may start one at any time.
reserved += @intFromBool(c.session == 0);
// Count the number of non-register requests queued.
reserved += @intFromBool(c.request_inflight != null);
}
// +1 for the potential request — is there room in the sequencer's queue?
if (reserved + 1 > simulator.reply_sequence.free()) return;
// Make sure that the client is ready to send a new request.
if (client.request_inflight != null) return;
const request_message = client.get_message();
errdefer client.release_message(request_message);
const request_metadata = simulator.workload.build_request(
client_index,
request_message.buffer[@sizeOf(vsr.Header)..constants.message_size_max],
);
assert(request_metadata.size <= constants.message_size_max - @sizeOf(vsr.Header));
simulator.cluster.request(
client_index,
request_metadata.operation,
request_message,
request_metadata.size,