✓
Passing This code compiles and runs correctly.
Code
// The World in Koru — entry 9. A SECOND WITNESS, and a deliberate boundary test.
// Doorway: the quorum commit rule, as four different systems spell it.
//
// hashicorp/raft commitment.go:98 quorumMatchIndex := matched[(len(matched)-1)/2]
// ZooKeeper QuorumMaj.java:140 return (ackSet.size() > half);
// TigerBeetle replica.zig:2318 assert(count == threshold);
// celld — has no quorum at all; its coordinator is a bucket.
//
// WHY THIS ENTRY EXISTS. Entry 8 ported three invariants from celld and drew two
// conclusions from them. Both carried the same weakness, written into the
// fragments at the time: one system, read closely, that ADVERTISED its pure
// decision core. So this entry goes looking for a second witness on purpose,
// and picks the invariant all three other systems independently nominated as
// the one a linear type should NOT be able to hold.
//
// WHAT LINEARITY REACHES. An ack is a resource: minted once by `peer.matched`,
// consumed once by `tally.count`. Counting the same ack twice is refused — 850.
// That is not a small thing; double-counting an agreement is the classic quorum
// bug, and TigerBeetle spends a dedicated flag on it
// (`assert(!prepare.ok_quorum_received)`, replica.zig:2319).
//
// WHAT IT DOES NOT REACH, and this is the point of 851: linearity counts
// TOKENS, not IDENTITIES. Two acks minted for the same peer are two distinct
// resources, each legitimately consumed once, and they form a quorum of three
// out of two real voters. The compiler cannot see it. The peer id is passed
// right into `tally.count` and changes nothing, because an identity is data and
// linearity is about tokens.
//
// So the boundary lands exactly where entry 8 said it did, now from a different
// family of systems: TYPES HOLD PROVENANCE AND AT-MOST-ONCE; RUNTIME HOLDS THE
// COMPARISONS AND THE COUNTS. `> half` is arithmetic. `> commit` is arithmetic.
// Neither is expressible as an obligation, and neither should be faked as one.
import std/io
import std/store
// Five voters, so a majority is three. `half` is ZooKeeper's precomputed
// `half`; `counted` and `best` are the tally; `commit` is Raft's commitIndex.
std/store:new(raft, capacity: 1) { counted: 0[i64], best: 0[i64], commit: 0[i64], half: 2[i64] }
// One peer's durable agreement at an index — Raft's matchIndex, ZAB's ack.
// Minted by the replication path; it is the only source of <ack!>.
pub tor peer.matched { id: i64, index: i64 } -> i64<ack!>
peer.matched -> index
// Counting CONSUMES the ack. `from` is carried only so the trace can show it —
// note that the identity is right there in the call and changes nothing.
pub tor tally.count { ack: i64<!ack>, from: i64 }
tally.count = std/store:stored { raft.counted: raft.counted + 1, raft.best: ack }
|> std/io:print.ln(" counted ack from peer {{ from:d }} at index {{ ack:d }}")
// ZooKeeper: `return (ackSet.size() > half);` — arithmetic, so it stays runtime.
pub tor quorum.check {}
| reached i64
| short
quorum.check = if(raft.counted > raft.half)
| then => reached raft.best
| else => short
// Raft: the commit index only advances. Also arithmetic.
pub tor commit.advance { upto: i64 }
| advanced
| stale
commit.advance = if(upto > raft.commit)
| then |> std/store:stored { raft.commit: upto } => advanced
| else => stale
peer.matched(id: 1, index: 7): a1 |> tally.count(ack: a1, from: 1)
peer.matched(id: 2, index: 7): a2 |> tally.count(ack: a2, from: 2)
peer.matched(id: 3, index: 7): a3 |> tally.count(ack: a3, from: 3)
quorum.check()
| reached idx |> commit.advance(upto: idx)
| advanced |> std/io:print.ln("commit -> {{ raft.commit:d }} on {{ raft.counted:d }} acks of 5")
| stale |> std/io:print.ln("stale")
| short |> std/io:print.ln("no quorum on {{ raft.counted:d }} acks")
Actual
counted ack from peer 1 at index 7
counted ack from peer 2 at index 7
counted ack from peer 3 at index 7
commit -> 7 on 3 acks of 5
Expected output
✓ Zig✓ C# counted ack from peer 1 at index 7
counted ack from peer 2 at index 7
counted ack from peer 3 at index 7
commit -> 7 on 3 acks of 5
Emitted Zig source
// Access compiler flags via the per-user compiler_env module
const CompilerEnv = @import("compiler_env").CompilerEnv;
pub const panic = if (@import("builtin").mode == .Debug)
@import("std").debug.FullPanic(@import("std").debug.defaultPanic)
else
@import("std").debug.simple_panic;
const __koru_bare = struct {
extern fn posix_memalign(memptr: *?*anyopaque, alignment: usize, size: usize) c_int;
extern fn free(ptr: ?*anyopaque) void;
fn bareAlloc(_: *anyopaque, len: usize, alignment: @import("std").mem.Alignment, _: usize) ?[*]u8 {
var p: ?*anyopaque = null;
const a = @max(alignment.toByteUnits(), @sizeOf(usize));
if (posix_memalign(&p, a, len) != 0) return null;
return @ptrCast(p);
}
fn bareResize(_: *anyopaque, _: []u8, _: @import("std").mem.Alignment, _: usize, _: usize) bool { return false; }
fn bareRemap(_: *anyopaque, _: []u8, _: @import("std").mem.Alignment, _: usize, _: usize) ?[*]u8 { return null; }
fn bareFree(_: *anyopaque, memory: []u8, _: @import("std").mem.Alignment, _: usize) void { free(@ptrCast(memory.ptr)); }
const vtable = @import("std").mem.Allocator.VTable{ .alloc = bareAlloc, .resize = bareResize, .remap = bareRemap, .free = bareFree };
const allocator = @import("std").mem.Allocator{ .ptr = undefined, .vtable = &vtable };
};
const __koru_backing = if (@import("builtin").link_libc) @import("std").heap.c_allocator else if (@import("builtin").os.tag == .freestanding) __koru_bare.allocator else @import("std").heap.page_allocator;
var __koru_leak_count: @import("std").atomic.Value(usize) = .init(0);
fn __koru_alloc(ctx: *anyopaque, len: usize, alignment: @import("std").mem.Alignment, ret_addr: usize) ?[*]u8 {
_ = ctx;
const r = __koru_backing.rawAlloc(len, alignment, ret_addr);
if (comptime @import("builtin").mode == .Debug) {
if (r != null) _ = __koru_leak_count.fetchAdd(1, .monotonic);
}
return r;
}
fn __koru_resize(ctx: *anyopaque, memory: []u8, alignment: @import("std").mem.Alignment, new_len: usize, ret_addr: usize) bool {
_ = ctx;
return __koru_backing.rawResize(memory, alignment, new_len, ret_addr);
}
fn __koru_remap(ctx: *anyopaque, memory: []u8, alignment: @import("std").mem.Alignment, new_len: usize, ret_addr: usize) ?[*]u8 {
_ = ctx;
return __koru_backing.rawRemap(memory, alignment, new_len, ret_addr);
}
fn __koru_free(ctx: *anyopaque, memory: []u8, alignment: @import("std").mem.Alignment, ret_addr: usize) void {
_ = ctx;
__koru_backing.rawFree(memory, alignment, ret_addr);
if (comptime @import("builtin").mode == .Debug) {
_ = __koru_leak_count.fetchSub(1, .monotonic);
}
}
const __koru_vtable = @import("std").mem.Allocator.VTable{ .alloc = __koru_alloc, .resize = __koru_resize, .remap = __koru_remap, .free = __koru_free };
pub fn koru_allocator() @import("std").mem.Allocator {
return .{ .ptr = undefined, .vtable = &__koru_vtable };
}
pub inline fn __koru_intcast(comptime T: type, x: anytype) T {
if (comptime (@import("builtin").mode == .Debug or @import("builtin").mode == .ReleaseSafe))
return @as(T, @intCast(x));
const dst = @typeInfo(T);
const src = @typeInfo(@TypeOf(x));
if (comptime (dst == .int and src == .int and dst.int.bits == src.int.bits and dst.int.signedness != src.int.signedness))
return @as(T, @bitCast(x));
return @as(T, @intCast(x));
}
pub fn koru_leak_check() void {
if (comptime @import("builtin").mode != .Debug) return;
if (__koru_leak_count.load(.acquire) == 0) return;
if (comptime @import("builtin").target.os.tag == .freestanding) {
if (comptime @import("builtin").cpu.arch == .wasm32 or @import("builtin").cpu.arch == .wasm64) {
@panic("KORU LEAK CHECK FAILED: the produced program leaked");
} else {
const __klc = struct { extern var stdout: ?*anyopaque; extern fn fputs(__s: [*:0]const u8, __st: ?*anyopaque) c_int; };
var __lb: [128]u8 = undefined;
const __lm = "KORU LEAK CHECK FAILED: allocations still outstanding at end of run: ";
@memcpy(__lb[0..__lm.len], __lm);
var __ln: usize = __lm.len;
var __lv = __koru_leak_count.load(.acquire);
var __ld: [20]u8 = undefined;
var __lk: usize = 0;
while (__lv > 0) : (__lk += 1) { __ld[__lk] = @intCast('0' + __lv % 10); __lv /= 10; }
for (0..__lk) |__li| { __lb[__ln] = __ld[__lk - 1 - __li]; __ln += 1; }
__lb[__ln] = '\n'; __ln += 1; __lb[__ln] = 0;
_ = __klc.fputs(@as([*:0]const u8, @ptrCast(&__lb)), __klc.stdout);
@trap();
}
} else {
@import("std").debug.print("KORU LEAK CHECK FAILED: the produced program leaked (trace above)\n", .{});
@import("std").process.exit(1);
}
}
pub const main_module = struct {
// std/store: 'raft' created (cell + write-subflow at module scope); fields counted: i64, best: i64, commit: i64, half: i64
pub const peer_matched_event = struct {
pub const Input = struct {
id: i64,
index: i64,
};
pub const Output = i64;
pub inline fn handler(__koru_event_input: @This().Input) @This().Output {
return __koru_handler_impl(__koru_event_input.id, __koru_event_input.index);
}
fn __koru_handler_impl(__koru_p_0: i64, __koru_p_1: i64) @This().Output {
const __koru_event_input: @This().Input = .{ .id = __koru_p_0, .index = __koru_p_1 };
const id = __koru_event_input.id;
const index = __koru_event_input.index;
_ = &id;
_ = &index;
return index;
}
};
pub const tally_count_event = struct {
pub const Input = struct {
ack: i64,
from: i64,
};
pub const Output = void;
pub fn handler(__koru_event_input: @This().Input) @This().Output {
// >>> SUBFLOW: tests/regression/830_THE_WORLD/849_raft_quorum_commit/input.k:49
const ack = __koru_event_input.ack;
const from = __koru_event_input.from;
_ = &ack;
_ = &from;
_ = &__koru_event_input;
const result = main_module.__store_envwrite_raft_event.handler(.{ .field = @as(i64, @bitCast(@as(u64, 1) << 0)), .value = __koru_store_raft.counted + 1 });
_ = &result;
const result_0 = main_module.__store_envwrite_raft_event.handler(.{ .field = @as(i64, @bitCast(@as(u64, 1) << 1)), .value = ack });
_ = &result_0;
const result_1 = main_module.__store_announce_raft_event.handler(.{ .field = 0 });
_ = &result_1;
const result_2 = main_module.__store_announce_raft_event.handler(.{ .field = 1 });
_ = &result_2;
(struct { fn __kout(__fd: i32, __b: []const u8) void { if (@import("builtin").os.tag == .freestanding) { const __kc = struct { extern var stdout: ?*anyopaque; extern var stderr: ?*anyopaque; extern fn fputs(__s: [*:0]const u8, __st: ?*anyopaque) c_int; }; var __kt: [4096]u8 = undefined; for (0..(__b.len + __kt.len - 2) / (__kt.len - 1)) |__ki| { const __kn = @min(__kt.len - 1, __b.len - __ki * (__kt.len - 1)); @memcpy(__kt[0..__kn], __b[__ki * (__kt.len - 1)..][0..__kn]); __kt[__kn] = 0; @import("std").mem.doNotOptimizeAway(__kc.fputs(@as([*:0]const u8, @ptrCast(&__kt)), if (__fd == 2) __kc.stderr else __kc.stdout)); } } else { @import("std").mem.doNotOptimizeAway(@import("std").posix.write(__fd, __b) catch @as(usize, 0)); } } fn __kw(comptime __f: []const u8, __a: anytype) void { var __kb: [65536]u8 = undefined; const __ks = @import("std").fmt.bufPrint(&__kb, __f, __a) catch __kb[0..0]; __kout(1, __ks); } }).__kw(" counted ack from peer {d} at index {d}\n", .{from, ack});
}
};
pub const quorum_check_event = struct {
pub const Input = struct {
};
pub const Output = union(enum(u8)) {
reached: i64,
short: struct {
},
};
pub fn handler(__koru_event_input: @This().Input) @This().Output {
// >>> SUBFLOW: tests/regression/830_THE_WORLD/849_raft_quorum_commit/input.k:56
_ = &__koru_event_input;
if (__koru_store_raft.counted > __koru_store_raft.half) {
{ return .{ .reached = __koru_store_raft.best };
}
} else {
{ return .{ .short = .{ } };
}
}
}
};
pub const commit_advance_event = struct {
pub const Input = struct {
upto: i64,
};
pub const Output = union(enum(u8)) {
advanced: struct {
},
stale: struct {
},
};
pub inline fn handler(__koru_event_input: @This().Input) @This().Output {
return __koru_handler_impl(__koru_event_input.upto);
}
fn __koru_handler_impl(__koru_p_0: i64) @This().Output {
const __koru_event_input: @This().Input = .{ .upto = __koru_p_0 };
// >>> SUBFLOW: tests/regression/830_THE_WORLD/849_raft_quorum_commit/input.k:64
const upto = __koru_event_input.upto;
_ = &upto;
_ = &__koru_event_input;
if (upto > __koru_store_raft.commit) {
{ const result_c0_0 = main_module.__store_write_raft_event.handler(.{ .field = 2, .value = upto });
_ = &result_c0_0;
return .{ .advanced = .{ } };
}
} else {
{ return .{ .stale = .{ } };
}
}
}
};
// >>> FLOW: tests/regression/830_THE_WORLD/849_raft_quorum_commit/input.k:68 ~input:peer.matched()
pub fn flow0() void {
const a1: i64 = blk: {
break :blk 7;
};
_ = main_module.tally_count_event.handler(.{ .ack = a1, .from = 1 });
}
// >>> FLOW: tests/regression/830_THE_WORLD/849_raft_quorum_commit/input.k:69 ~input:peer.matched()
pub fn flow1() void {
const a2: i64 = blk: {
break :blk 7;
};
_ = main_module.tally_count_event.handler(.{ .ack = a2, .from = 2 });
}
// >>> FLOW: tests/regression/830_THE_WORLD/849_raft_quorum_commit/input.k:70 ~input:peer.matched()
pub fn flow2() void {
const a3: i64 = blk: {
break :blk 7;
};
_ = main_module.tally_count_event.handler(.{ .ack = a3, .from = 3 });
}
// >>> FLOW: tests/regression/830_THE_WORLD/849_raft_quorum_commit/input.k:72 ~input:quorum.check()
pub fn flow3() void {
const result_0 = main_module.quorum_check_event.handler(.{ });
switch (result_0) {
// >>> BRANCH: tests/regression/830_THE_WORLD/849_raft_quorum_commit/input.k:73 | reached idx |>
.reached => |idx| {
const result_1 = main_module.commit_advance_event.handler(.{ .upto = idx });
_ = &result_1;
switch (result_1) {
// >>> BRANCH: tests/regression/830_THE_WORLD/849_raft_quorum_commit/input.k:75 | stale |>
.stale => {
(struct { fn __kout(__fd: i32, __b: []const u8) void { if (@import("builtin").os.tag == .freestanding) { const __kc = struct { extern var stdout: ?*anyopaque; extern var stderr: ?*anyopaque; extern fn fputs(__s: [*:0]const u8, __st: ?*anyopaque) c_int; }; var __kt: [4096]u8 = undefined; for (0..(__b.len + __kt.len - 2) / (__kt.len - 1)) |__ki| { const __kn = @min(__kt.len - 1, __b.len - __ki * (__kt.len - 1)); @memcpy(__kt[0..__kn], __b[__ki * (__kt.len - 1)..][0..__kn]); __kt[__kn] = 0; @import("std").mem.doNotOptimizeAway(__kc.fputs(@as([*:0]const u8, @ptrCast(&__kt)), if (__fd == 2) __kc.stderr else __kc.stdout)); } } else { @import("std").mem.doNotOptimizeAway(@import("std").posix.write(__fd, __b) catch @as(usize, 0)); } } fn __kw(comptime __f: []const u8, __a: anytype) void { var __kb: [65536]u8 = undefined; const __ks = @import("std").fmt.bufPrint(&__kb, __f, __a) catch __kb[0..0]; __kout(1, __ks); } }).__kw("stale\n", .{});
},
// >>> BRANCH: tests/regression/830_THE_WORLD/849_raft_quorum_commit/input.k:74 | advanced |>
.advanced => {
(struct { fn __kout(__fd: i32, __b: []const u8) void { if (@import("builtin").os.tag == .freestanding) { const __kc = struct { extern var stdout: ?*anyopaque; extern var stderr: ?*anyopaque; extern fn fputs(__s: [*:0]const u8, __st: ?*anyopaque) c_int; }; var __kt: [4096]u8 = undefined; for (0..(__b.len + __kt.len - 2) / (__kt.len - 1)) |__ki| { const __kn = @min(__kt.len - 1, __b.len - __ki * (__kt.len - 1)); @memcpy(__kt[0..__kn], __b[__ki * (__kt.len - 1)..][0..__kn]); __kt[__kn] = 0; @import("std").mem.doNotOptimizeAway(__kc.fputs(@as([*:0]const u8, @ptrCast(&__kt)), if (__fd == 2) __kc.stderr else __kc.stdout)); } } else { @import("std").mem.doNotOptimizeAway(@import("std").posix.write(__fd, __b) catch @as(usize, 0)); } } fn __kw(comptime __f: []const u8, __a: anytype) void { var __kb: [65536]u8 = undefined; const __ks = @import("std").fmt.bufPrint(&__kb, __f, __a) catch __kb[0..0]; __kout(1, __ks); } }).__kw("commit -> {d} on {d} acks of 5\n", .{__koru_store_raft.commit, __koru_store_raft.counted});
},
}
},
// >>> BRANCH: tests/regression/830_THE_WORLD/849_raft_quorum_commit/input.k:76 | short |>
.short => {
(struct { fn __kout(__fd: i32, __b: []const u8) void { if (@import("builtin").os.tag == .freestanding) { const __kc = struct { extern var stdout: ?*anyopaque; extern var stderr: ?*anyopaque; extern fn fputs(__s: [*:0]const u8, __st: ?*anyopaque) c_int; }; var __kt: [4096]u8 = undefined; for (0..(__b.len + __kt.len - 2) / (__kt.len - 1)) |__ki| { const __kn = @min(__kt.len - 1, __b.len - __ki * (__kt.len - 1)); @memcpy(__kt[0..__kn], __b[__ki * (__kt.len - 1)..][0..__kn]); __kt[__kn] = 0; @import("std").mem.doNotOptimizeAway(__kc.fputs(@as([*:0]const u8, @ptrCast(&__kt)), if (__fd == 2) __kc.stderr else __kc.stdout)); } } else { @import("std").mem.doNotOptimizeAway(@import("std").posix.write(__fd, __b) catch @as(usize, 0)); } } fn __kw(comptime __f: []const u8, __a: anytype) void { var __kb: [65536]u8 = undefined; const __ks = @import("std").fmt.bufPrint(&__kb, __f, __a) catch __kb[0..0]; __kout(1, __ks); } }).__kw("no quorum on {d} acks\n", .{__koru_store_raft.counted});
},
}
}
pub fn koru_start_flow() void {
const result_0 = koru_koru.start_event.handler(.{ });
const result_0_done = result_0.done;
_ = &result_0_done;
}
pub fn koru_end_flow() void {
const result_0 = koru_koru.end_event.handler(.{ });
const result_0_done = result_0.done;
_ = &result_0_done;
}
const __KoruStoreT_raft = blk: {
const __info = @typeInfo(@TypeOf(.{ .counted = @as(i64, 0), .best = @as(i64, 0), .commit = @as(i64, 0), .half = @as(i64, 2) }));
var __fields: [__info.@"struct".fields.len]@import("std").builtin.Type.StructField = undefined;
for (__info.@"struct".fields, 0..) |__f, __i| {
__fields[__i] = .{ .name = __f.name, .type = __f.type, .default_value_ptr = null, .is_comptime = false, .alignment = __f.alignment };
}
break :blk @Type(.{ .@"struct" = .{ .layout = .auto, .fields = &__fields, .decls = &.{}, .is_tuple = false } });
};
var __koru_store_raft: __KoruStoreT_raft = .{ .counted = @as(i64, 0), .best = @as(i64, 0), .commit = @as(i64, 0), .half = @as(i64, 2) };
pub const __store_apply_raft_event = struct {
pub const Input = struct {
field: i64,
value: i64,
};
pub const Output = union(enum(u8)) {
counted: i64,
best: i64,
commit: i64,
half: i64,
};
pub fn handler(__koru_event_input: @This().Input) @This().Output {
// >>> PROC: __store_apply_raft [tests/regression/830_THE_WORLD/849_raft_quorum_commit/input.k:39]
const field = __koru_event_input.field;
const value = __koru_event_input.value;
_ = &field;
_ = &value;
_ = &__koru_event_input;
return switch (field) {
0 => blk: { __koru_store_raft.counted = value; break :blk .{ .counted = value }; },
1 => blk: { __koru_store_raft.best = value; break :blk .{ .best = value }; },
2 => blk: { __koru_store_raft.commit = value; break :blk .{ .commit = value }; },
3 => blk: { __koru_store_raft.half = value; break :blk .{ .half = value }; },
else => unreachable,
};
}
};
pub const __store_write_raft_event = struct {
pub const Input = struct {
field: i64,
value: i64,
};
pub const Output = void;
pub fn handler(__koru_event_input: @This().Input) @This().Output {
// >>> SUBFLOW: tests/regression/830_THE_WORLD/849_raft_quorum_commit/input.k:39
const field = __koru_event_input.field;
const value = __koru_event_input.value;
_ = &field;
_ = &value;
_ = &__koru_event_input;
const result = main_module.__store_apply_raft_event.handler(.{ .field = field, .value = value });
return switch (result) {
.counted => {},
.half => {},
.commit => {},
.best => {},
};
}
};
pub const __store_announce_raft_event = struct {
pub const Input = struct {
field: i64,
};
pub const Output = void;
pub inline fn handler(__koru_event_input: @This().Input) @This().Output {
return __koru_handler_impl(__koru_event_input.field);
}
fn __koru_handler_impl(__koru_p_0: i64) @This().Output {
const __koru_event_input: @This().Input = .{ .field = __koru_p_0 };
// >>> PROC: __store_announce_raft [tests/regression/830_THE_WORLD/849_raft_quorum_commit/input.k:39]
const field = __koru_event_input.field;
_ = &field;
_ = &__koru_event_input;
return;
}
};
pub const __store_envwrite_raft_event = struct {
pub const Input = struct {
field: i64,
value: i64,
};
pub const Output = void;
pub fn handler(__koru_event_input: @This().Input) @This().Output {
// >>> PROC: __store_envwrite_raft [tests/regression/830_THE_WORLD/849_raft_quorum_commit/input.k:39]
const field = __koru_event_input.field;
const value = __koru_event_input.value;
_ = &field;
_ = &value;
_ = &__koru_event_input;
const __koru_mask = @as(u64, @bitCast(field));
if (__koru_mask & (@as(u64, 1) << 0) != 0) __koru_store_raft.counted = value;
if (__koru_mask & (@as(u64, 1) << 1) != 0) __koru_store_raft.best = value;
if (__koru_mask & (@as(u64, 1) << 2) != 0) __koru_store_raft.commit = value;
if (__koru_mask & (@as(u64, 1) << 3) != 0) __koru_store_raft.half = value;
return;
}
};
};
pub const koru_koru = struct {
pub const start_event = struct {
pub const Input = struct {
};
pub const Output = union(enum(u8)) {
done: struct {
},
};
pub fn handler(__koru_event_input: @This().Input) @This().Output {
_ = &__koru_event_input;
return .{ .done = .{} };
}
};
pub const end_event = struct {
pub const Input = struct {
};
pub const Output = union(enum(u8)) {
done: struct {
},
};
pub fn handler(__koru_event_input: @This().Input) @This().Output {
_ = &__koru_event_input;
return .{ .done = .{} };
... [truncated - 20KB total]Emitted C# source
static class main_module {
public static void __koru_stdout_write(dynamic s) => global::System.Console.Out.Write(s);
public static void __koru_stderr_write(dynamic s) => global::System.Console.Error.Write(s);
// The mutable handle carrier — `*String`-style resources are
// `new __KoruBox { data = … }` because C# anonymous types are
// read-only: `s.data = …` (std/string append/clear, handle
// mutation generally) needs a settable member.
public class __KoruBox { public dynamic data; }
// Textification for `{{ … }}` operands: C# bool ToStrings as
// `True` where Koru prints `true`, and the operand's static type
// is unknown at this boundary — a `(x) is bool` inline test would
// be a compile error on statically-typed operands instead. Generic
// on purpose: `dynamic` boxed every value-type operand — measured
// ~1s/10M elements on 012_threat_scanner — while T specializes to
// the operand's own ToString() with no box.
public static string __koru_str<T>(T v) => v is bool b ? (b ? "true" : "false") : v?.ToString();
// std/store: 'raft' created (cell + write-subflow at module scope); fields counted: i64, best: i64, commit: i64, half: i64
static class __koru_store_raft {
public static long counted = 0;
public static long best = 0;
public static long commit = 0;
public static long half = 2;
public static long __koru_brand = -1;
}
public static class peer_matched_event {
public struct Input {
public long id;
public long index;
}
public static long handler(Input __koru_input) {
var id = __koru_input.id;
var index = __koru_input.index;
return (long)(index);
}
}
public static class tally_count_event {
public struct Input {
public long ack;
public long from;
}
public static dynamic handler(Input __koru_input) {
var ack = __koru_input.ack;
var from = __koru_input.from;
{
var __koru_p_field = (long)((long)((long)((ulong)((ulong)(1) << 0))));
var __koru_p_value = (long)(__koru_store_raft.counted + 1);
var __koru_mask = (ulong)__koru_p_field;
if ((__koru_mask & (1UL << 0)) != 0) __koru_store_raft.counted = __koru_p_value;
if ((__koru_mask & (1UL << 1)) != 0) __koru_store_raft.best = __koru_p_value;
if ((__koru_mask & (1UL << 2)) != 0) __koru_store_raft.commit = __koru_p_value;
if ((__koru_mask & (1UL << 3)) != 0) __koru_store_raft.half = __koru_p_value;
}
{
var __koru_p_field = (long)((long)((long)((ulong)((ulong)(1) << 1))));
var __koru_p_value = (long)(ack);
var __koru_mask = (ulong)__koru_p_field;
if ((__koru_mask & (1UL << 0)) != 0) __koru_store_raft.counted = __koru_p_value;
if ((__koru_mask & (1UL << 1)) != 0) __koru_store_raft.best = __koru_p_value;
if ((__koru_mask & (1UL << 2)) != 0) __koru_store_raft.commit = __koru_p_value;
if ((__koru_mask & (1UL << 3)) != 0) __koru_store_raft.half = __koru_p_value;
}
{
var __koru_p_field = (long)(0);
}
{
var __koru_p_field = (long)(1);
}
__koru_stdout_write(" counted ack from peer " + __koru_str(from) + " at index " + __koru_str(ack) + "\n");
return default;
}
}
public static class quorum_check_event {
public struct Input {
}
public struct Output {
public string tag;
public long reached;
public dynamic @short;
}
public static Output handler(Input __koru_input) {
if (__koru_store_raft.counted > __koru_store_raft.half) {
{ return new Output { tag = "reached", reached = __koru_store_raft.best };
}
} else {
{ return new Output { tag = "short" };
}
}
return default;
}
}
public static class commit_advance_event {
public struct Input {
public long upto;
}
public struct Output {
public string tag;
public dynamic advanced;
public dynamic stale;
}
public static Output handler(Input __koru_input) {
var upto = __koru_input.upto;
if (upto > __koru_store_raft.commit) {
{ main_module.__store_write_raft_event.handler(new __store_write_raft_event.Input { field = (long)(2), value = (long)(upto)});
return new Output { tag = "advanced" };
}
} else {
{ return new Output { tag = "stale" };
}
}
return default;
}
}
public static class __store_apply_raft_event {
public struct Input {
public long field;
public long value;
}
public struct Output {
public string tag;
public long counted;
public long best;
public long commit;
public long half;
}
public static Output handler(Input __koru_input) {
var field = __koru_input.field;
var value = __koru_input.value;
switch (field) {
case 0: { __koru_store_raft.counted = value; return new Output { tag = "counted", counted = value }; }
case 1: { __koru_store_raft.best = value; return new Output { tag = "best", best = value }; }
case 2: { __koru_store_raft.commit = value; return new Output { tag = "commit", commit = value }; }
case 3: { __koru_store_raft.half = value; return new Output { tag = "half", half = value }; }
}
throw new global::System.Exception("__store_apply_raft: field index " + field + " is not a column of store 'raft'");
return default;
}
}
public static class __store_write_raft_event {
public struct Input {
public long field;
public long value;
}
public static dynamic handler(Input __koru_input) {
var field = __koru_input.field;
var value = __koru_input.value;
var result_0 = main_module.__store_apply_raft_event.handler(new __store_apply_raft_event.Input { field = (long)(field), value = (long)(value)});
if (result_0.tag == "counted") {
var _auto_6 = result_0.counted;
}
if (result_0.tag == "best") {
var _auto_7 = result_0.best;
}
if (result_0.tag == "commit") {
var _auto_8 = result_0.commit;
}
if (result_0.tag == "half") {
var _auto_9 = result_0.half;
}
return default;
}
}
public static class __store_announce_raft_event {
public struct Input {
public long field;
}
public static dynamic handler(Input __koru_input) {
var field = __koru_input.field;
return default;
}
}
public static class __store_envwrite_raft_event {
public struct Input {
public long field;
public long value;
}
public static dynamic handler(Input __koru_input) {
var field = __koru_input.field;
var value = __koru_input.value;
var __koru_mask = (ulong)field;
if ((__koru_mask & (1UL << 0)) != 0) __koru_store_raft.counted = value;
if ((__koru_mask & (1UL << 1)) != 0) __koru_store_raft.best = value;
if ((__koru_mask & (1UL << 2)) != 0) __koru_store_raft.commit = value;
if ((__koru_mask & (1UL << 3)) != 0) __koru_store_raft.half = value;
return default;
}
}
public static class std_control_if_event {
public struct Input {
public dynamic expr;
}
public struct Output {
public string tag;
public dynamic then;
public dynamic @else;
}
public static Output handler(Input __koru_input) {
var expr = __koru_input.expr;
throw new global::System.Exception("if: |template| proc is inlined at call sites and must never be called");
}
}
public static class koru_start_event {
public struct Input {
}
public struct Output {
public string tag;
public dynamic done;
}
public static Output handler(Input __koru_input) => new Output { tag = "done" };
}
public static class koru_end_event {
public struct Input {
}
public struct Output {
public string tag;
public dynamic done;
}
public static Output handler(Input __koru_input) => new Output { tag = "done" };
}
public static void flow0() {
var a1 = main_module.peer_matched_event.handler(new peer_matched_event.Input { id = (long)(1), index = (long)(7)});
main_module.tally_count_event.handler(new tally_count_event.Input { ack = (long)(a1), from = (long)(1)});
}
public static void flow1() {
var a2 = main_module.peer_matched_event.handler(new peer_matched_event.Input { id = (long)(2), index = (long)(7)});
main_module.tally_count_event.handler(new tally_count_event.Input { ack = (long)(a2), from = (long)(2)});
}
public static void flow2() {
var a3 = main_module.peer_matched_event.handler(new peer_matched_event.Input { id = (long)(3), index = (long)(7)});
main_module.tally_count_event.handler(new tally_count_event.Input { ack = (long)(a3), from = (long)(3)});
}
public static void flow3() {
var result_1 = main_module.quorum_check_event.handler(new quorum_check_event.Input { });
if (result_1.tag == "reached") {
var idx = result_1.reached;
var result_2 = main_module.commit_advance_event.handler(new commit_advance_event.Input { upto = (long)(idx)});
if (result_2.tag == "advanced") {
__koru_stdout_write("commit -> " + __koru_str(__koru_store_raft.commit) + " on " + __koru_str(__koru_store_raft.counted) + " acks of 5" + "\n");
}
if (result_2.tag == "stale") {
__koru_stdout_write("stale" + "\n");
}
}
if (result_1.tag == "short") {
__koru_stdout_write("no quorum on " + __koru_str(__koru_store_raft.counted) + " acks" + "\n");
}
}
public static void flow4() {
main_module.koru_start_event.handler(new koru_start_event.Input { });
}
public static void flow5() {
main_module.koru_end_event.handler(new koru_end_event.Input { });
}
}
static class Program {
static void Main() {
main_module.flow4();
main_module.flow0();
main_module.flow1();
main_module.flow2();
main_module.flow3();
main_module.flow5();
}
}
Flows
flow ~new click a branch to expand · @labels scroll to their anchor
new (raft, capacity: 1, source: counted: 0[i64], best: 0[i64], commit: 0[i64], half: 2[i64])
subflow ~tally.count click a branch to expand · @labels scroll to their anchor
stored (source: raft.counted: raft.counted + 1, raft.best: ack)
subflow ~quorum.check click a branch to expand · @labels scroll to their anchor
if (raft.counted > raft.half)
subflow ~commit.advance click a branch to expand · @labels scroll to their anchor
if (upto > raft.commit)
flow ~peer.matched click a branch to expand · @labels scroll to their anchor
peer.matched (id: 1, index: 7)
flow ~peer.matched click a branch to expand · @labels scroll to their anchor
peer.matched (id: 2, index: 7)
flow ~peer.matched click a branch to expand · @labels scroll to their anchor
peer.matched (id: 3, index: 7)
flow ~quorum.check click a branch to expand · @labels scroll to their anchor
quorum.check
Test Configuration
MUST_RUN LANGUAGES: zig cs