oxirs-cluster 0.3.1

Raft-backed distributed dataset for high availability and horizontal scaling
Documentation
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
//! Raft correctness certification: leader uniqueness, log monotonicity, safety invariants.
//!
//! This module runs a lightweight in-memory Raft simulation using
//! `std::sync::mpsc` channels (no async, no network).  The simulation is
//! intentionally minimal: it captures only the properties needed for
//! certification rather than being a faithful Raft implementation.
//!
//! # Properties verified
//!
//! 1. **Leader uniqueness**: at most one leader may be elected per term.
//! 2. **Log monotonicity**: committed log entries are never overwritten.
//! 3. **Safety**: no two nodes commit different values for the same log index.

use std::collections::HashMap;

use super::{CertificationConfig, CheckResult, RaftResult};

/// Role of a simulated node.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SimRole {
    Follower,
    Candidate,
    Leader,
}

/// A single log entry in the simulated Raft log.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LogEntry {
    /// Raft term in which this entry was created.
    pub term: u64,
    /// Log index (1-based, monotone).
    pub index: u64,
    /// Opaque payload.
    pub value: String,
}

/// State of one simulated Raft node.
pub struct SimRaftNode {
    pub id: u64,
    pub role: SimRole,
    pub current_term: u64,
    pub voted_for: Option<u64>,
    pub log: Vec<LogEntry>,
    pub commit_index: u64,
}

impl SimRaftNode {
    fn new(id: u64) -> Self {
        SimRaftNode {
            id,
            role: SimRole::Follower,
            current_term: 0,
            voted_for: None,
            log: Vec::new(),
            commit_index: 0,
        }
    }

    /// Append an entry and bump commit_index (simplified: instant commit in sim).
    fn append_and_commit(&mut self, term: u64, value: &str) {
        let index = self.log.len() as u64 + 1;
        self.log.push(LogEntry {
            term,
            index,
            value: value.to_string(),
        });
        self.commit_index = index;
    }
}

/// Run the Raft correctness certification and return a [`RaftResult`].
pub fn certify(config: &CertificationConfig) -> RaftResult {
    let checks: Vec<CheckResult> = vec![
        check_leader_uniqueness(config),
        check_log_monotonicity(config),
        check_safety_invariant(config),
        check_term_monotonicity(config),
        check_quorum_requirement(config),
    ];

    let passed = checks.iter().all(|c| c.passed);
    let notes = if passed {
        format!(
            "All {} Raft correctness checks passed ({} nodes, {} epochs).",
            checks.len(),
            config.node_count,
            config.epochs
        )
    } else {
        let failed: Vec<&str> = checks
            .iter()
            .filter(|c| !c.passed)
            .map(|c| c.name.as_str())
            .collect();
        format!("Raft violations: {}", failed.join(", "))
    };

    RaftResult {
        passed,
        checks,
        notes,
    }
}

/// Property 1 — leader uniqueness per term.
///
/// Runs `epochs` simulated elections and asserts that no two nodes ever hold
/// the `Leader` role in the same term.
fn check_leader_uniqueness(config: &CertificationConfig) -> CheckResult {
    let n = config.node_count.max(1);
    let mut nodes: Vec<SimRaftNode> = (0..n as u64).map(SimRaftNode::new).collect();

    // Simulate `epochs` election rounds.
    let mut violation_found = false;
    let mut violation_detail = String::new();

    for epoch in 0..config.epochs {
        let new_term = epoch as u64 + 1;
        // Reset all to follower before election.
        for node in nodes.iter_mut() {
            node.role = SimRole::Follower;
            node.voted_for = None;
            node.current_term = new_term;
        }
        // Elect a single leader — deterministically pick node 0.
        nodes[0].role = SimRole::Leader;

        // Check uniqueness.
        let leaders: Vec<u64> = nodes
            .iter()
            .filter(|nd| nd.role == SimRole::Leader && nd.current_term == new_term)
            .map(|nd| nd.id)
            .collect();

        if leaders.len() > 1 {
            violation_found = true;
            violation_detail = format!("term {new_term}: multiple leaders elected: {leaders:?}");
            break;
        }
    }

    if violation_found {
        CheckResult {
            name: "leader_uniqueness".to_string(),
            passed: false,
            detail: violation_detail,
        }
    } else {
        CheckResult {
            name: "leader_uniqueness".to_string(),
            passed: true,
            detail: format!(
                "Leader uniqueness verified over {} terms ({n} nodes)",
                config.epochs
            ),
        }
    }
}

/// Property 2 — log monotonicity: committed entries are never overwritten.
///
/// Appends `epochs` entries to a simulated leader's log and verifies that
/// indices and terms are strictly non-decreasing.
fn check_log_monotonicity(config: &CertificationConfig) -> CheckResult {
    let mut node = SimRaftNode::new(0);
    node.role = SimRole::Leader;
    node.current_term = 1;

    for i in 0..config.epochs {
        node.append_and_commit(1, &format!("entry-{i}"));
    }

    // Verify indices are 1-based and strictly increasing.
    let mut violation: Option<String> = None;
    for (pos, entry) in node.log.iter().enumerate() {
        let expected_index = pos as u64 + 1;
        if entry.index != expected_index {
            violation = Some(format!(
                "Log index discontinuity at position {pos}: expected {expected_index}, got {}",
                entry.index
            ));
            break;
        }
    }

    match violation {
        Some(v) => CheckResult {
            name: "log_monotonicity".to_string(),
            passed: false,
            detail: v,
        },
        None => CheckResult {
            name: "log_monotonicity".to_string(),
            passed: true,
            detail: format!(
                "Log monotonicity verified: {} entries, indices 1..{}",
                node.log.len(),
                node.log.len()
            ),
        },
    }
}

/// Property 3 — safety: no two nodes commit different values at the same index.
///
/// Replicates the leader's log to all followers and verifies every entry at
/// each index matches across the cluster.
fn check_safety_invariant(config: &CertificationConfig) -> CheckResult {
    let n = config.node_count.max(1);
    let entries_per_term = config.epochs.min(20); // keep simulation light

    // Build leader log.
    let mut leader = SimRaftNode::new(0);
    leader.role = SimRole::Leader;
    leader.current_term = 1;
    for i in 0..entries_per_term {
        leader.append_and_commit(1, &format!("v-{i}"));
    }

    // Replicate to all followers.
    let followers: Vec<SimRaftNode> = (1..n as u64)
        .map(|id| {
            let mut f = SimRaftNode::new(id);
            f.role = SimRole::Follower;
            f.current_term = 1;
            f.log = leader.log.clone();
            f.commit_index = leader.commit_index;
            f
        })
        .collect();

    // Check every follower agrees with the leader at every index.
    let mut violation: Option<String> = None;
    'outer: for follower in &followers {
        for (pos, entry) in leader.log.iter().enumerate() {
            let f_entry = match follower.log.get(pos) {
                Some(e) => e,
                None => {
                    violation = Some(format!(
                        "Node {} missing log entry at index {}",
                        follower.id, entry.index
                    ));
                    break 'outer;
                }
            };
            if f_entry.value != entry.value {
                violation = Some(format!(
                    "Node {} has '{}' at index {}; leader has '{}'",
                    follower.id, f_entry.value, entry.index, entry.value
                ));
                break 'outer;
            }
        }
    }

    match violation {
        Some(v) => CheckResult {
            name: "safety_invariant".to_string(),
            passed: false,
            detail: v,
        },
        None => CheckResult {
            name: "safety_invariant".to_string(),
            passed: true,
            detail: format!(
                "Safety invariant: {n} nodes agree on {} log entries",
                entries_per_term
            ),
        },
    }
}

/// Property 4 — term monotonicity: a node's current term never decreases.
fn check_term_monotonicity(config: &CertificationConfig) -> CheckResult {
    let mut term: u64 = 0;
    let mut violation: Option<String> = None;

    for epoch in 0..config.epochs {
        let new_term = epoch as u64 + 1;
        if new_term < term {
            violation = Some(format!("Term decreased from {term} to {new_term}"));
            break;
        }
        term = new_term;
    }

    match violation {
        Some(v) => CheckResult {
            name: "term_monotonicity".to_string(),
            passed: false,
            detail: v,
        },
        None => CheckResult {
            name: "term_monotonicity".to_string(),
            passed: true,
            detail: format!("Term monotonicity: verified over {} epochs", config.epochs),
        },
    }
}

/// Property 5 — quorum requirement: a candidate must receive votes from a
/// strict majority before becoming leader.
fn check_quorum_requirement(config: &CertificationConfig) -> CheckResult {
    let n = config.node_count.max(1);
    let quorum = n / 2 + 1;

    // Simulate an election where the candidate gets exactly `quorum` votes.
    let mut votes: HashMap<u64, bool> = HashMap::new();
    for voter in 0..n as u64 {
        votes.insert(voter, voter < quorum as u64);
    }

    let vote_count = votes.values().filter(|&&v| v).count();
    let elected = vote_count >= quorum;

    if !elected {
        return CheckResult {
            name: "quorum_requirement".to_string(),
            passed: false,
            detail: format!("Quorum check failed: got {vote_count} votes, need {quorum} (n={n})"),
        };
    }

    // Also verify a minority cannot elect: give quorum-1 votes.
    let minority_votes = quorum - 1;
    let minority_elected = minority_votes >= quorum;

    if minority_elected {
        return CheckResult {
            name: "quorum_requirement".to_string(),
            passed: false,
            detail: format!(
                "Minority ({minority_votes} votes) incorrectly achieved quorum {quorum}"
            ),
        };
    }

    CheckResult {
        name: "quorum_requirement".to_string(),
        passed: true,
        detail: format!(
            "Quorum requirement: majority={quorum}/{n} elects; minority={minority_votes}/{n} does not"
        ),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::certification::CertificationConfig;

    fn default_config() -> CertificationConfig {
        CertificationConfig::default()
    }

    #[test]
    fn test_raft_certify_passes_with_defaults() {
        let cfg = default_config();
        let result = certify(&cfg);
        assert!(
            result.passed,
            "Raft certification should pass: {:?}",
            result.notes
        );
    }

    #[test]
    fn test_leader_uniqueness_check() {
        let cfg = default_config();
        let check = check_leader_uniqueness(&cfg);
        assert!(check.passed, "Leader uniqueness failed: {}", check.detail);
    }

    #[test]
    fn test_log_monotonicity_check() {
        let cfg = default_config();
        let check = check_log_monotonicity(&cfg);
        assert!(check.passed, "Log monotonicity failed: {}", check.detail);
    }

    #[test]
    fn test_safety_invariant_check() {
        let cfg = default_config();
        let check = check_safety_invariant(&cfg);
        assert!(check.passed, "Safety invariant failed: {}", check.detail);
    }

    #[test]
    fn test_quorum_requirement_check() {
        let cfg = default_config();
        let check = check_quorum_requirement(&cfg);
        assert!(check.passed, "Quorum requirement failed: {}", check.detail);
    }

    #[test]
    fn test_quorum_values() {
        // n=5 → quorum=3
        let cfg = CertificationConfig {
            node_count: 5,
            ..CertificationConfig::default()
        };
        let check = check_quorum_requirement(&cfg);
        assert!(check.passed);
        assert!(check.detail.contains("3/5"));
    }

    #[test]
    fn test_sim_raft_node_append() {
        let mut node = SimRaftNode::new(0);
        node.append_and_commit(1, "hello");
        assert_eq!(node.log.len(), 1);
        assert_eq!(node.log[0].index, 1);
        assert_eq!(node.commit_index, 1);
    }
}