// packages/cluster_raft/examples/demo.vk // Self-contained verification suite & cluster simulation for `cluster_raft` and `cluster_raft/swim`. use [ raft = cluster_raft swim = cluster_raft/swim ] assert-true = fn [cond bool, label str] enum [ if not cond [ println (str/join ['[FAIL] ' label] '') os/exit 1 ] print (str/join ['[PASS] ' label] '') .ok ] replicate-one-round = fn [mut leader raft/RaftNode, followers block[raft/RaftNode]] block[raft/RaftNode] [ mut updated = [] mut i = 0 while i < (len followers) [ mut f = followers[i] ae = raft/build-append-entries leader f.id resp = raft/handle-append-entries mut f ae _ = raft/handle-append-response mut leader resp push mut updated f i = i + 1 ] updated ] // --- 1. 5-Node Raft Cluster Election in Term 1 --- all_ids = ['n1' 'n2' 'n3' 'n4' 'n5'] mut n1 = raft/new-node 'n1' all_ids mut n2 = raft/new-node 'n2' all_ids mut n3 = raft/new-node 'n3' all_ids mut n4 = raft/new-node 'n4' all_ids mut n5 = raft/new-node 'n5' all_ids rv1 = raft/tick-election mut n1 v2 = raft/handle-request-vote mut n2 rv1 v3 = raft/handle-request-vote mut n3 rv1 _ = raft/handle-vote-response mut n1 v2 role_after_quorum = raft/handle-vote-response mut n1 v3 _ = assert-true (role_after_quorum == .leader and n1.current_term == 1 and (raft/quorum-size n1) == 3) '5-node Raft cluster elects n1 as leader in term 1 upon 3/5 quorum' // --- 2. Quorum Log Replication & Deterministic KV State Machine --- _ = raft/propose-put mut n1 'region' 'ca-west-1' _ = raft/propose-put mut n1 'epoch' 10 _ = raft/propose-put mut n1 'temp_key' 'ephemeral' _ = raft/propose-del mut n1 'temp_key' // Round 1 replicates entries 1..4 to followers and advances n1.commit_index = 4; // Round 2 heartbeats leader_commit = 4 so all followers apply entries 1..4 to kv_state. mut peers_r1 = replicate-one-round mut n1 [n2 n3 n4 n5] peers_r2 = replicate-one-round mut n1 peers_r1 n2 = peers_r2[0] n3 = peers_r2[1] n4 = peers_r2[2] n5 = peers_r2[3] rep_ok_1 = n1.commit_index == 4 and (raft/kv-get n1 'epoch') == 10 and (not (map/has? n1.kv_state 'temp_key')) rep_ok_2 = n2.commit_index == 4 and (raft/kv-get n2 'region') == 'ca-west-1' and (raft/kv-get n4 'epoch') == 10 and (raft/kv-get n5 'epoch') == 10 _ = assert-true (rep_ok_1 and rep_ok_2) 'quorum replication commits and applies KV mutations across all 5 nodes' // --- 3. Network Partition: Minority Leader Cannot Commit; Majority Elects Term-2 Leader --- // Partition: {n1, n5} isolated from majority {n2, n3, n4}. // Old leader n1 proposes an entry that replicates only to n5 (2/5 < quorum 3 -> uncommitted!). _ = raft/propose-put mut n1 'split_brain' 'stale-term-1' ae_n5 = raft/build-append-entries n1 'n5' resp_n5 = raft/handle-append-entries mut n5 ae_n5 _ = raft/handle-append-response mut n1 resp_n5 _ = assert-true (n1.commit_index == 4 and (not (map/has? n1.kv_state 'split_brain'))) 'isolated minority leader (2/5) cannot advance commit_index' // Majority partition {n2, n3, n4} elects n2 in term 2 rv_t2 = raft/tick-election mut n2 v3_t2 = raft/handle-request-vote mut n3 rv_t2 v4_t2 = raft/handle-request-vote mut n4 rv_t2 _ = raft/handle-vote-response mut n2 v3_t2 _ = raft/handle-vote-response mut n2 v4_t2 _ = assert-true (n2.role == .leader and n2.current_term == 2) 'majority partition {n2, n3, n4} elects n2 as leader in term 2' // Verify ยง5.4.1 Election Restriction: a lagging candidate with older log is denied vote mut lagging = raft/new-node 'lagging' all_ids put mut lagging .current_term 2 stale_rv = raft/tick-election mut lagging // lagging is at term 3, but its last_log_term = 0 < n3.last_log_term = 1! deny_resp = raft/handle-request-vote mut n3 stale_rv _ = assert-true ((not deny_resp.vote_granted) and n3.current_term == 3) 'Raft section 5.4.1 rejects candidate with less up-to-date log' // Advance n2 to term 4 so {n2, n3, n4} continue cleanly after the term-3 probe put mut n2 .current_term 3 rv_t4 = raft/tick-election mut n2 _ = raft/handle-vote-response mut n2 (raft/handle-request-vote mut n3 rv_t4) _ = raft/handle-vote-response mut n2 (raft/handle-request-vote mut n4 rv_t4) _ = raft/propose-put mut n2 'epoch' 20 _ = raft/propose-put mut n2 'leader' 'n2' mut maj_p1 = replicate-one-round mut n2 [n3 n4] maj_p2 = replicate-one-round mut n2 maj_p1 n3 = maj_p2[0] n4 = maj_p2[1] _ = assert-true (n2.commit_index == 6 and (raft/kv-get n2 'epoch') == 20 and (raft/kv-get n3 'leader') == 'n2') 'new leader n2 commits entries 5..6 across majority quorum' // --- 4. Partition Heal: Old Leader Steps Down & Truncates Conflicting Uncommitted Tail --- // When n2 sends AppendEntries to n1 & n5, initial next_index=7 fails consistency at prev_log_index=6, // backs up via conflict_index to overwrite index 5 (term 1 -> term 4), and reconciles kv_state! mut healed = [n1 n5] mut sync_round = 0 while sync_round < 3 [ healed = replicate-one-round mut n2 healed sync_round = sync_round + 1 ] n1 = healed[0] n5 = healed[1] heal_ok_1 = n1.role == .follower and n1.current_term == 4 and n1.leader_id == 'n2' and n1.commit_index == 6 heal_ok_2 = (raft/kv-get n1 'epoch') == 20 and (raft/kv-get n1 'leader') == 'n2' and (not (map/has? n1.kv_state 'split_brain')) heal_ok_3 = (raft/kv-get n5 'epoch') == 20 and (len n1.log) == 6 and (len n5.log) == 6 _ = assert-true (heal_ok_1 and heal_ok_2 and heal_ok_3) 'healed old leader n1 steps down, truncates uncommitted split-brain tail, and reconciles KV state' // --- 5. SWIM 5-Node Gossip, Suspicion Refutation & Dead Confirmation --- mut s1 = swim/new-member-list/seed 's1' '10.0.0.1:7946' 101 mut s2 = swim/new-member-list/seed 's2' '10.0.0.2:7946' 202 mut s3 = swim/new-member-list/seed 's3' '10.0.0.3:7946' 303 mut idx = 1 while idx <= 5 [ id = str/join ['s' (mold idx)] '' addr = str/join ['10.0.0.' (mold idx) ':7946'] '' if id != 's1' [ _ = swim/join-member mut s1 id addr ] if id != 's2' [ _ = swim/join-member mut s2 id addr ] if id != 's3' [ _ = swim/join-member mut s3 id addr ] idx = idx + 1 ] probes = swim/select-probe-targets mut s1 3 _ = assert-true ((len probes) == 3 and probes[0].id != 's1' and probes[1].id != 's1') 'SWIM randomized k-peer probe target selection excludes self' // s1 suspects s3 -> gossips to s2 -> s2 gossips to s3 -> s3 refutes by bumping incarnation to 1! _ = swim/suspect-node mut s1 's3' pkt_1_to_2 = swim/drain-piggyback mut s1 8 _ = swim/apply-gossip mut s2 pkt_1_to_2 _ = assert-true ((swim/get-member s2 's3').state == .suspect) 'SWIM suspicion rumor propagates via piggybacked gossip' pkt_2_to_3 = swim/drain-piggyback mut s2 8 _ = swim/apply-gossip mut s3 pkt_2_to_3 refute_pkt = swim/drain-piggyback mut s3 8 _ = swim/apply-gossip mut s1 refute_pkt _ = swim/apply-gossip mut s2 refute_pkt m3_on_s1 = swim/get-member s1 's3' m3_on_s2 = swim/get-member s2 's3' _ = assert-true (s3.incarnation == 1 and m3_on_s1.state == .alive and m3_on_s1.incarnation == 1 and m3_on_s2.state == .alive) 'SWIM node s3 refutes false suspicion via incarnation bump (0 -> 1)' // Crash s5 and advance ticks past suspect_timeout_ticks (5) -> confirmed .dead _ = swim/suspect-node mut s1 's5' mut t = 0 mut dead_ids = [] while t < 5 [ exp = swim/advance-tick mut s1 if (len exp) > 0 [ dead_ids = exp ] t = t + 1 ] _ = assert-true ((len dead_ids) == 1 and dead_ids[0] == 's5' and (swim/get-member s1 's5').state == .dead and (len (swim/alive-members s1)) == 4) 'SWIM confirms unresponsive suspect peer s5 as .dead after timeout ticks' print 'cluster_raft: all verification checks passed'