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
//! crashpoint — named crash-injection seams at durable commit boundaries
//!
//! the oracle rig (tests/oracle.py, docs/upstream-harness.md) runs the
//! real binary with `--crashpoint=<name>`, lets it abort at that seam,
//! restarts it clean, and asserts the lifecycle converges to a healthy
//! steady state. seams live at every point where upstream's crash
//! matrix distinguishes an outcome. production runs never arm one, and
//! hit() is a single enum compare when unarmed.
//!
//! abort() (not exit) so no defers, flushes, or atexit handlers run —
//! the closest in-process approximation of SIGKILL.
const std = @import("std");
const builtin = @import("builtin");
const log = std.log.scoped(.stream);
pub const Point = enum {
// Canonical upstream Jetstream V2 lifecycle boundaries.
after_repo_complete,
after_merge_dst_flush_before_source_commit,
after_merge_dst_seal_before_discovery,
after_merge_discovery_before_cleanup,
after_merge_cleanup_complete,
after_bootstrap_live_close_before_seal,
after_steady_phase_before_steady_run,
after_compaction_rewrite_before_watermark,
after_compaction_chunk_watermark,
after_segment_rewrite_temp_written,
after_segment_rewrite_temp_synced,
after_segment_rewrite_renamed,
after_segment_rewrite_dir_synced,
after_segment_patch_temp_written,
after_segment_patch_temp_synced,
after_segment_patch_renamed,
after_segment_patch_dir_synced,
/// Between the archive fsync that made a repository's rows durable and the
/// metadata commit that records the repository as complete. This is the
/// durability-ordering seam the whole design rests on: data first, then the
/// bookkeeping that acknowledges it. A cut here must lose nothing — the rows
/// are on disk and the repository simply completes again on restart.
after_archive_durable_before_completion_commit,
// Additional local boundaries retained by the broader restart oracle.
mid_backfill_download,
after_backfill_before_phase_merging,
after_phase_merging_before_live_stop,
after_live_close_before_seal,
after_merge_drain_before_pending,
after_pending_pass,
after_seal_before_discovery,
after_discovery_before_cleanup,
after_cleanup_remove_before_sync,
after_cleanup_before_phase_write,
};
const Action = enum { abort_process, await_power_cut };
var armed: ?Point = null;
var action: Action = .abort_process;
var target_ordinal: u64 = 1;
var occurrence: std.atomic.Value(u64) = .init(0);
pub fn pointName(point: Point) []const u8 {
return switch (point) {
.after_repo_complete => "after-repo-complete",
.after_archive_durable_before_completion_commit => "after-archive-durable-before-completion-commit",
.after_merge_dst_flush_before_source_commit => "after-merge-dst-flush-before-source-commit",
.after_merge_dst_seal_before_discovery => "after-merge-dst-seal-before-discovery",
.after_merge_discovery_before_cleanup => "after-merge-discovery-before-cleanup",
.after_merge_cleanup_complete => "after-merge-cleanup-complete",
.after_bootstrap_live_close_before_seal => "after-bootstrap-live-close-before-seal",
.after_steady_phase_before_steady_run => "after-steady-phase-before-steady-run",
.after_compaction_rewrite_before_watermark => "after-compaction-rewrite-before-watermark",
.after_compaction_chunk_watermark => "after-compaction-chunk-watermark",
.after_segment_rewrite_temp_written => "after-segment-rewrite-temp-written",
.after_segment_rewrite_temp_synced => "after-segment-rewrite-temp-synced",
.after_segment_rewrite_renamed => "after-segment-rewrite-renamed",
.after_segment_rewrite_dir_synced => "after-segment-rewrite-dir-synced",
.after_segment_patch_temp_written => "after-segment-patch-temp-written",
.after_segment_patch_temp_synced => "after-segment-patch-temp-synced",
.after_segment_patch_renamed => "after-segment-patch-renamed",
.after_segment_patch_dir_synced => "after-segment-patch-dir-synced",
else => @tagName(point),
};
}
fn parse(name: []const u8) ?Point {
inline for (comptime std.enums.values(Point)) |point| {
if (std.mem.eql(u8, name, pointName(point)) or std.mem.eql(u8, name, @tagName(point))) return point;
}
return null;
}
/// arm from the CLI (debug flag); returns false for an unknown name
pub fn arm(name: []const u8) bool {
return armAt(name, 1);
}
/// Arm a process-abort seam at an explicit 1-based occurrence. This is the
/// deterministic predicate-kill coordinate used by the upstream restart
/// oracle: `(crashpoint, ordinal)` rather than wall-clock timing.
pub fn armAt(name: []const u8, ordinal: u64) bool {
if (ordinal == 0) return false;
armed = parse(name) orelse return false;
action = .abort_process;
target_ordinal = ordinal;
occurrence.store(0, .release);
return true;
}
/// Arm a lifecycle boundary for the strict block-device oracle. The real
/// process stops at the requested occurrence without running defers; the
/// external harness then removes power from its backing device and SIGKILLs
/// the process. This action is intentionally Linux-only.
pub fn armPowerCut(name: []const u8, ordinal: u64) bool {
if (ordinal == 0) return false;
armed = parse(name) orelse return false;
action = .await_power_cut;
target_ordinal = ordinal;
occurrence.store(0, .release);
return true;
}
/// true when this seam is armed — for seams that must make some state
/// durable before aborting (e.g. the not_started row a mid-download
/// crash leaves behind)
pub fn wouldHit(p: Point) bool {
return armed == p;
}
/// crash here if this seam is armed
pub fn hit(p: Point) void {
const a = armed orelse return;
if (a != p) return;
const ordinal = occurrence.fetchAdd(1, .acq_rel) + 1;
if (ordinal != target_ordinal) return;
switch (action) {
.abort_process => {
log.warn("crashpoint {s}: aborting", .{pointName(p)});
std.process.abort();
},
.await_power_cut => {
log.warn("power cutpoint {s} ordinal {d}: ready", .{ pointName(p), ordinal });
if (comptime builtin.os.tag == .linux) {
while (true) _ = std.os.linux.pause();
} else {
std.process.abort();
}
},
}
}