oximedia-distributed 0.2.1

Distributed encoding coordinator for OxiMedia
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
//! Distributed consensus module (Raft-inspired).
//!
//! Provides a simplified Raft consensus implementation for leader election
//! and distributed log replication in the distributed encoding cluster.

#![allow(dead_code)]

/// Unique identifier for a Raft node.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
pub struct NodeId(pub u64);

impl NodeId {
    /// Create a new `NodeId`.
    #[must_use]
    pub fn new(id: u64) -> Self {
        Self(id)
    }

    /// Get the inner u64 value.
    #[must_use]
    pub fn inner(self) -> u64 {
        self.0
    }
}

impl std::fmt::Display for NodeId {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "Node({})", self.0)
    }
}

/// Raft term number.
#[derive(
    Debug,
    Clone,
    Copy,
    PartialEq,
    Eq,
    PartialOrd,
    Ord,
    serde::Serialize,
    serde::Deserialize,
    Default,
)]
pub struct RaftTerm(pub u64);

impl RaftTerm {
    /// Create a new `RaftTerm`.
    #[must_use]
    pub fn new(term: u64) -> Self {
        Self(term)
    }

    /// Get the inner u64 value.
    #[must_use]
    pub fn inner(self) -> u64 {
        self.0
    }

    /// Increment the term.
    #[must_use]
    pub fn increment(self) -> Self {
        Self(self.0 + 1)
    }
}

impl std::fmt::Display for RaftTerm {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "Term({})", self.0)
    }
}

/// Role of a Raft node.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RaftRole {
    /// Follower - the default state.
    Follower,
    /// Candidate - seeking election.
    Candidate,
    /// Leader - coordinating the cluster.
    Leader,
}

impl RaftRole {
    /// Returns true if the node can vote in leader elections.
    #[must_use]
    pub fn can_vote(self) -> bool {
        matches!(self, RaftRole::Follower | RaftRole::Candidate)
    }
}

impl std::fmt::Display for RaftRole {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            RaftRole::Follower => write!(f, "Follower"),
            RaftRole::Candidate => write!(f, "Candidate"),
            RaftRole::Leader => write!(f, "Leader"),
        }
    }
}

/// An entry in the Raft log.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct LogEntry {
    /// The term when this entry was created.
    pub term: RaftTerm,
    /// Index of this entry in the log (1-based).
    pub index: u64,
    /// Serialized command (e.g., JSON-encoded operation).
    pub command: String,
}

impl LogEntry {
    /// Create a new log entry.
    pub fn new(term: RaftTerm, index: u64, command: impl Into<String>) -> Self {
        Self {
            term,
            index,
            command: command.into(),
        }
    }
}

/// The Raft replicated log.
#[derive(Debug, Default)]
pub struct RaftLog {
    /// All log entries (0-indexed internally, 1-indexed externally).
    pub entries: Vec<LogEntry>,
}

impl RaftLog {
    /// Create a new empty Raft log.
    #[must_use]
    pub fn new() -> Self {
        Self {
            entries: Vec::new(),
        }
    }

    /// Append a new entry to the log.
    pub fn append(&mut self, entry: LogEntry) {
        self.entries.push(entry);
    }

    /// Get the entry at the given 1-based index.
    #[must_use]
    pub fn entry_at(&self, idx: u64) -> Option<&LogEntry> {
        if idx == 0 {
            return None;
        }
        self.entries.get((idx - 1) as usize)
    }

    /// Get the index of the last entry (0 if log is empty).
    #[must_use]
    pub fn last_index(&self) -> u64 {
        self.entries.len() as u64
    }

    /// Get the term of the last entry (default term 0 if log is empty).
    #[must_use]
    pub fn last_term(&self) -> RaftTerm {
        self.entries.last().map(|e| e.term).unwrap_or_default()
    }
}

/// A request to vote for a candidate in a Raft election.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct VoteRequest {
    /// The candidate's current term.
    pub term: RaftTerm,
    /// The candidate's node ID.
    pub candidate_id: NodeId,
    /// Index of the candidate's last log entry.
    pub last_log_index: u64,
    /// Term of the candidate's last log entry.
    pub last_log_term: RaftTerm,
}

/// A response to a vote request.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct VoteResponse {
    /// The current term, for the candidate to update itself.
    pub term: RaftTerm,
    /// True if the vote was granted.
    pub vote_granted: bool,
}

/// A Raft consensus node.
#[derive(Debug)]
pub struct RaftNode {
    /// This node's ID.
    pub id: NodeId,
    /// Current role.
    pub role: RaftRole,
    /// Latest term this node has seen.
    pub current_term: RaftTerm,
    /// `NodeId` this node voted for in the current term.
    pub voted_for: Option<NodeId>,
    /// The replicated log.
    pub log: RaftLog,
    /// Index of highest log entry known to be committed.
    pub commit_index: u64,
    /// Index of highest log entry applied to state machine.
    pub last_applied: u64,
}

impl RaftNode {
    /// Create a new Raft node in Follower state.
    #[must_use]
    pub fn new(id: NodeId) -> Self {
        Self {
            id,
            role: RaftRole::Follower,
            current_term: RaftTerm::default(),
            voted_for: None,
            log: RaftLog::new(),
            commit_index: 0,
            last_applied: 0,
        }
    }

    /// Process an incoming vote request and return a response.
    ///
    /// Grant vote if:
    /// 1. Candidate's term >= our current term.
    /// 2. We haven't voted for anyone else this term.
    /// 3. Candidate's log is at least as up-to-date as ours.
    pub fn process_vote_request(&mut self, req: &VoteRequest) -> VoteResponse {
        // If we see a higher term, update and step down
        if req.term > self.current_term {
            self.step_down(req.term);
        }

        if req.term < self.current_term {
            return VoteResponse {
                term: self.current_term,
                vote_granted: false,
            };
        }

        // Check if we can vote for this candidate
        let already_voted_other = self.voted_for.is_some_and(|v| v != req.candidate_id);

        if already_voted_other {
            return VoteResponse {
                term: self.current_term,
                vote_granted: false,
            };
        }

        // Check log up-to-date-ness
        let our_last_term = self.log.last_term();
        let our_last_index = self.log.last_index();

        let log_ok = req.last_log_term > our_last_term
            || (req.last_log_term == our_last_term && req.last_log_index >= our_last_index);

        if log_ok {
            self.voted_for = Some(req.candidate_id);
            VoteResponse {
                term: self.current_term,
                vote_granted: true,
            }
        } else {
            VoteResponse {
                term: self.current_term,
                vote_granted: false,
            }
        }
    }

    /// Transition this node to Candidate and start a new election.
    pub fn become_candidate(&mut self) {
        self.current_term = self.current_term.increment();
        self.role = RaftRole::Candidate;
        self.voted_for = Some(self.id); // Vote for self
    }

    /// Transition this node to Leader.
    pub fn become_leader(&mut self) {
        self.role = RaftRole::Leader;
    }

    /// Step down to Follower with a new term (e.g., after seeing higher term).
    pub fn step_down(&mut self, new_term: RaftTerm) {
        self.current_term = new_term;
        self.role = RaftRole::Follower;
        self.voted_for = None;
    }
}

/// A timer that tracks election timeouts.
#[derive(Debug, Clone)]
pub struct ElectionTimer {
    /// Timeout duration in milliseconds.
    pub timeout_ms: u64,
    /// Timestamp (ms) of the last reset.
    pub last_reset_ms: u64,
}

impl ElectionTimer {
    /// Create a new election timer.
    #[must_use]
    pub fn new(timeout_ms: u64, now_ms: u64) -> Self {
        Self {
            timeout_ms,
            last_reset_ms: now_ms,
        }
    }

    /// Returns true if the timer has expired at the given time.
    #[must_use]
    pub fn is_expired(&self, now_ms: u64) -> bool {
        now_ms.saturating_sub(self.last_reset_ms) >= self.timeout_ms
    }

    /// Reset the timer to the current time.
    pub fn reset(&mut self, now_ms: u64) {
        self.last_reset_ms = now_ms;
    }
}

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

    fn make_node(id: u64) -> RaftNode {
        RaftNode::new(NodeId::new(id))
    }

    #[test]
    fn test_node_initial_state() {
        let node = make_node(1);
        assert_eq!(node.id, NodeId::new(1));
        assert_eq!(node.role, RaftRole::Follower);
        assert_eq!(node.current_term, RaftTerm::default());
        assert!(node.voted_for.is_none());
    }

    #[test]
    fn test_raft_role_can_vote() {
        assert!(RaftRole::Follower.can_vote());
        assert!(RaftRole::Candidate.can_vote());
        assert!(!RaftRole::Leader.can_vote());
    }

    #[test]
    fn test_raft_term_increment() {
        let term = RaftTerm::new(5);
        assert_eq!(term.increment().inner(), 6);
    }

    #[test]
    fn test_raft_log_append_and_entry_at() {
        let mut log = RaftLog::new();
        assert_eq!(log.last_index(), 0);
        assert_eq!(log.last_term(), RaftTerm::default());

        log.append(LogEntry::new(RaftTerm::new(1), 1, "cmd1"));
        log.append(LogEntry::new(RaftTerm::new(1), 2, "cmd2"));

        assert_eq!(log.last_index(), 2);
        assert_eq!(log.last_term(), RaftTerm::new(1));
        assert!(log.entry_at(1).is_some());
        assert_eq!(log.entry_at(1).expect("entry should exist").command, "cmd1");
        assert!(log.entry_at(0).is_none());
        assert!(log.entry_at(3).is_none());
    }

    #[test]
    fn test_become_candidate() {
        let mut node = make_node(1);
        node.become_candidate();
        assert_eq!(node.role, RaftRole::Candidate);
        assert_eq!(node.current_term, RaftTerm::new(1));
        assert_eq!(node.voted_for, Some(NodeId::new(1)));
    }

    #[test]
    fn test_become_leader() {
        let mut node = make_node(1);
        node.become_candidate();
        node.become_leader();
        assert_eq!(node.role, RaftRole::Leader);
    }

    #[test]
    fn test_step_down() {
        let mut node = make_node(1);
        node.become_candidate();
        node.step_down(RaftTerm::new(5));
        assert_eq!(node.role, RaftRole::Follower);
        assert_eq!(node.current_term, RaftTerm::new(5));
        assert!(node.voted_for.is_none());
    }

    #[test]
    fn test_vote_request_grant() {
        let mut node = make_node(2);
        let req = VoteRequest {
            term: RaftTerm::new(1),
            candidate_id: NodeId::new(1),
            last_log_index: 0,
            last_log_term: RaftTerm::default(),
        };
        let resp = node.process_vote_request(&req);
        assert!(resp.vote_granted);
    }

    #[test]
    fn test_vote_request_deny_lower_term() {
        let mut node = make_node(2);
        node.step_down(RaftTerm::new(3));
        let req = VoteRequest {
            term: RaftTerm::new(2),
            candidate_id: NodeId::new(1),
            last_log_index: 0,
            last_log_term: RaftTerm::default(),
        };
        let resp = node.process_vote_request(&req);
        assert!(!resp.vote_granted);
    }

    #[test]
    fn test_vote_request_deny_already_voted() {
        let mut node = make_node(2);
        let req1 = VoteRequest {
            term: RaftTerm::new(1),
            candidate_id: NodeId::new(1),
            last_log_index: 0,
            last_log_term: RaftTerm::default(),
        };
        let req2 = VoteRequest {
            term: RaftTerm::new(1),
            candidate_id: NodeId::new(3),
            last_log_index: 0,
            last_log_term: RaftTerm::default(),
        };
        node.process_vote_request(&req1);
        let resp2 = node.process_vote_request(&req2);
        assert!(!resp2.vote_granted);
    }

    #[test]
    fn test_vote_deny_stale_log() {
        let mut node = make_node(2);
        // Node 2 has log entries at term 2
        node.log.append(LogEntry::new(RaftTerm::new(2), 1, "x"));
        let req = VoteRequest {
            term: RaftTerm::new(3),
            candidate_id: NodeId::new(1),
            last_log_index: 0,
            last_log_term: RaftTerm::new(1), // candidate's log is older
        };
        let resp = node.process_vote_request(&req);
        assert!(!resp.vote_granted);
    }

    #[test]
    fn test_election_timer_expired() {
        let timer = ElectionTimer::new(150, 1000);
        assert!(!timer.is_expired(1100));
        assert!(timer.is_expired(1150));
        assert!(timer.is_expired(1200));
    }

    #[test]
    fn test_election_timer_reset() {
        let mut timer = ElectionTimer::new(150, 1000);
        timer.reset(1100);
        assert!(!timer.is_expired(1200)); // only 100ms elapsed after reset
        assert!(timer.is_expired(1250));
    }

    #[test]
    fn test_node_id_display() {
        let id = NodeId::new(42);
        assert_eq!(format!("{}", id), "Node(42)");
    }

    #[test]
    fn test_raft_term_ordering() {
        assert!(RaftTerm::new(5) > RaftTerm::new(3));
        assert!(RaftTerm::new(1) < RaftTerm::new(2));
        assert_eq!(RaftTerm::new(4), RaftTerm::new(4));
    }
}