forked from tigerbeetle/tigerbeetle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
packet_simulator.zig
374 lines (311 loc) · 13.6 KB
/
packet_simulator.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
const std = @import("std");
const assert = std.debug.assert;
const math = std.math;
const log = std.log.scoped(.packet_simulator);
pub const PacketSimulatorOptions = struct {
/// Mean for the exponential distribution used to calculate forward delay.
one_way_delay_mean: u64,
one_way_delay_min: u64,
packet_loss_probability: u8,
packet_replay_probability: u8,
seed: u64,
replica_count: u8,
client_count: u8,
node_count: u8,
/// How the partitions should be generated
partition_mode: PartitionMode,
/// Probability per tick that a partition will occur
partition_probability: u8,
/// Probability per tick that a partition will resolve
unpartition_probability: u8,
/// Minimum time a partition lasts
partition_stability: u32,
/// Minimum time the cluster is fully connected until it is partitioned again
unpartition_stability: u32,
/// The maximum number of in-flight packets a path can have before packets are randomly dropped.
path_maximum_capacity: u8,
/// Mean for the exponential distribution used to calculate how long a path is clogged for.
path_clog_duration_mean: u64,
path_clog_probability: u8,
};
pub const Path = struct {
source: u8,
target: u8,
};
/// Determines how the partitions are created. Partitions
/// are two-way, i.e. if i cannot communicate with j, then
/// j cannot communicate with i.
///
/// Only replicas are partitioned. There will always be exactly two partitions.
pub const PartitionMode = enum {
/// Draws the size of the partition uniformly at random from (1, n-1).
/// Replicas are randomly assigned a partition.
uniform_size,
/// Assigns each node to a partition uniformly at random. This biases towards
/// equal-size partitions.
uniform_partition,
/// Isolates exactly one replica.
isolate_single,
/// User-defined partitioning algorithm.
custom,
};
/// A fully connected network of nodes used for testing. Simulates the fault model:
/// Packets may be dropped.
/// Packets may be delayed.
/// Packets may be replayed.
pub const PacketStatistics = enum(u8) {
dropped_due_to_partition,
dropped_due_to_congestion,
dropped,
replay,
};
pub fn PacketSimulator(comptime Packet: type) type {
return struct {
const Self = @This();
const Data = struct {
expiry: u64,
callback: fn (packet: Packet, path: Path) void,
packet: Packet,
};
/// A send and receive path between each node in the network. We use the `path` function to
/// index it.
paths: []std.PriorityQueue(Data, void, Self.order_packets),
/// We can arbitrary clog a path until a tick.
path_clogged_till: []u64,
ticks: u64 = 0,
options: PacketSimulatorOptions,
prng: std.rand.DefaultPrng,
stats: [@typeInfo(PacketStatistics).Enum.fields.len]u32 = [_]u32{0} **
@typeInfo(PacketStatistics).Enum.fields.len,
is_partitioned: bool,
partition: []bool,
replicas: []u8,
stability: u32,
pub fn init(allocator: std.mem.Allocator, options: PacketSimulatorOptions) !Self {
assert(options.one_way_delay_mean >= options.one_way_delay_min);
var self = Self{
.paths = try allocator.alloc(
std.PriorityQueue(Data, void, Self.order_packets),
@as(usize, options.node_count) * options.node_count,
),
.path_clogged_till = try allocator.alloc(
u64,
@as(usize, options.node_count) * options.node_count,
),
.options = options,
.prng = std.rand.DefaultPrng.init(options.seed),
.is_partitioned = false,
.stability = options.unpartition_stability,
.partition = try allocator.alloc(bool, @as(usize, options.replica_count)),
.replicas = try allocator.alloc(u8, @as(usize, options.replica_count)),
};
for (self.replicas) |_, i| {
self.replicas[i] = @intCast(u8, i);
}
for (self.paths) |*queue| {
queue.* = std.PriorityQueue(Data, void, Self.order_packets).init(allocator, {});
try queue.ensureTotalCapacity(options.path_maximum_capacity);
}
for (self.path_clogged_till) |*clogged_till| {
clogged_till.* = 0;
}
return self;
}
pub fn deinit(self: *Self, allocator: std.mem.Allocator) void {
for (self.paths) |*queue| {
while (queue.popOrNull()) |*data| data.packet.deinit();
queue.deinit();
}
allocator.free(self.paths);
}
fn order_packets(context: void, a: Data, b: Data) math.Order {
_ = context;
return math.order(a.expiry, b.expiry);
}
fn should_drop(self: *Self) bool {
return self.prng.random().uintAtMost(u8, 100) < self.options.packet_loss_probability;
}
fn path_index(self: *Self, path: Path) usize {
assert(path.source < self.options.node_count and path.target < self.options.node_count);
return @as(usize, path.source) * self.options.node_count + path.target;
}
pub fn path_queue(self: *Self, path: Path) *std.PriorityQueue(Data, void, Self.order_packets) {
return &self.paths[self.path_index(path)];
}
fn is_clogged(self: *Self, path: Path) bool {
return self.path_clogged_till[self.path_index(path)] > self.ticks;
}
fn should_clog(self: *Self, path: Path) bool {
_ = path;
return self.prng.random().uintAtMost(u8, 100) < self.options.path_clog_probability;
}
fn clog_for(self: *Self, path: Path, ticks: u64) void {
const clog_expiry = &self.path_clogged_till[self.path_index(path)];
clog_expiry.* = self.ticks + ticks;
log.debug("Path path.source={} path.target={} clogged for ticks={}", .{
path.source,
path.target,
ticks,
});
}
fn should_replay(self: *Self) bool {
return self.prng.random().uintAtMost(u8, 100) < self.options.packet_replay_probability;
}
fn should_partition(self: *Self) bool {
return self.prng.random().uintAtMost(u8, 100) < self.options.partition_probability;
}
fn should_unpartition(self: *Self) bool {
return self.prng.random().uintAtMost(u8, 100) < self.options.unpartition_probability;
}
/// Return a value produced using an exponential distribution with
/// the minimum and mean specified in self.options
fn one_way_delay(self: *Self) u64 {
const min = self.options.one_way_delay_min;
const mean = self.options.one_way_delay_mean;
return min + @floatToInt(u64, @intToFloat(f64, mean - min) * self.prng.random().floatExp(f64));
}
/// Partitions the network. Guaranteed to isolate at least one replica.
fn partition_network(
self: *Self,
) void {
assert(self.options.replica_count > 1);
self.is_partitioned = true;
self.stability = self.options.partition_stability;
switch (self.options.partition_mode) {
.uniform_size => {
// Exclude cases sz == 0 and sz == replica_count
const sz =
1 + self.prng.random().uintAtMost(u8, self.options.replica_count - 2);
self.prng.random().shuffle(u8, self.replicas);
for (self.replicas) |r, i| {
self.partition[r] = i < sz;
}
},
.uniform_partition => {
var only_same = true;
self.partition[0] =
self.prng.random().uintLessThan(u8, 2) == 1;
var i: usize = 1;
while (i < self.options.replica_count) : (i += 1) {
self.partition[i] =
self.prng.random().uintLessThan(u8, 2) == 1;
only_same =
only_same and (self.partition[i - 1] == self.partition[i]);
}
if (only_same) {
const n = self.prng.random().uintLessThan(u8, self.options.replica_count);
self.partition[n] = true;
}
},
.isolate_single => {
for (self.replicas) |_, i| {
self.partition[i] = false;
}
const n = self.prng.random().uintLessThan(u8, self.options.replica_count);
self.partition[n] = true;
},
// Put your own partitioning logic here.
.custom => unreachable,
}
}
fn unpartition_network(
self: *Self,
) void {
self.is_partitioned = false;
self.stability = self.options.unpartition_stability;
for (self.replicas) |_, i| {
self.partition[i] = false;
}
}
fn replicas_are_in_different_partitions(self: *Self, from: u8, to: u8) bool {
return from < self.options.replica_count and
to < self.options.replica_count and
self.partition[from] != self.partition[to];
}
pub fn tick(self: *Self) void {
self.ticks += 1;
if (self.stability > 0) {
self.stability -= 1;
} else {
if (self.is_partitioned) {
if (self.should_unpartition()) {
self.unpartition_network();
log.err("unpartitioned network: partition={d}", .{self.partition});
}
} else {
if (self.options.replica_count > 1 and self.should_partition()) {
self.partition_network();
log.err("partitioned network: partition={d}", .{self.partition});
}
}
}
var from: u8 = 0;
while (from < self.options.node_count) : (from += 1) {
var to: u8 = 0;
while (to < self.options.node_count) : (to += 1) {
const path = .{ .source = from, .target = to };
if (self.is_clogged(path)) continue;
const queue = self.path_queue(path);
while (queue.peek()) |*data| {
if (data.expiry > self.ticks) break;
_ = queue.remove();
if (self.is_partitioned and
self.replicas_are_in_different_partitions(from, to))
{
self.stats[@enumToInt(PacketStatistics.dropped_due_to_partition)] += 1;
log.err("dropped packet (different partitions): from={} to={}", .{ from, to });
data.packet.deinit(path);
continue;
}
if (self.should_drop()) {
self.stats[@enumToInt(PacketStatistics.dropped)] += 1;
log.err("dropped packet from={} to={}.", .{ from, to });
data.packet.deinit(path);
continue;
}
if (self.should_replay()) {
self.submit_packet(data.packet, data.callback, path);
log.debug("replayed packet from={} to={}", .{ from, to });
self.stats[@enumToInt(PacketStatistics.replay)] += 1;
data.callback(data.packet, path);
} else {
log.debug("delivering packet from={} to={}", .{ from, to });
data.callback(data.packet, path);
data.packet.deinit(path);
}
}
const reverse_path: Path = .{ .source = to, .target = from };
if (self.should_clog(reverse_path)) {
log.debug("reverse path clogged", .{});
const mean = @intToFloat(f64, self.options.path_clog_duration_mean);
const ticks = @floatToInt(u64, mean * self.prng.random().floatExp(f64));
self.clog_for(reverse_path, ticks);
}
}
}
}
pub fn submit_packet(
self: *Self,
packet: Packet,
callback: fn (packet: Packet, path: Path) void,
path: Path,
) void {
const queue = self.path_queue(path);
var queue_length = queue.count();
if (queue_length + 1 > queue.capacity()) {
const index = self.prng.random().uintLessThanBiased(u64, queue_length);
const data = queue.removeIndex(index);
data.packet.deinit(path);
log.err("submit_packet: {} reached capacity, dropped packet={}", .{
path,
index,
});
}
queue.add(.{
.expiry = self.ticks + self.one_way_delay(),
.packet = packet,
.callback = callback,
}) catch unreachable;
}
};
}