zlayer-consensus 0.11.22

Shared Raft consensus library built on openraft 0.9 for ZLayer and Zatabase
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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
//! In-memory storage implementation using the openraft v2 storage API.
//!
//! This store keeps all Raft log entries, votes, and state machine snapshots
//! in memory using `BTreeMap` and `RwLock`. It is generic over the
//! `RaftTypeConfig` so any application can plug in its own request/response types.
//!
//! **Not suitable for production** -- data is lost on process restart.
//! Use [`RedbStore`](super::redb_store) for durable storage.

use std::collections::BTreeMap;
use std::fmt::Debug;
use std::io::Cursor;
use std::ops::RangeBounds;
use std::sync::Arc;

use openraft::log_id::RaftLogId;
use openraft::storage::{LogFlushed, LogState, RaftLogStorage, RaftStateMachine, Snapshot};
use openraft::{
    Entry, EntryPayload, LogId, OptionalSend, RaftLogReader, RaftSnapshotBuilder, RaftTypeConfig,
    SnapshotMeta, StorageError, StoredMembership, Vote,
};
use tokio::sync::RwLock;
use tracing::debug;

use crate::types::NodeId;

// ---------------------------------------------------------------------------
// Log reader (clone-able handle used by replication tasks)
// ---------------------------------------------------------------------------

/// A cloneable log reader for the in-memory store.
pub struct MemLogReader<C: RaftTypeConfig<NodeId = NodeId>> {
    log: Arc<RwLock<LogData<C>>>,
}

impl<C: RaftTypeConfig<NodeId = NodeId>> Clone for MemLogReader<C> {
    fn clone(&self) -> Self {
        Self {
            log: Arc::clone(&self.log),
        }
    }
}

impl<C> RaftLogReader<C> for MemLogReader<C>
where
    C: RaftTypeConfig<NodeId = NodeId>,
    C::Entry: Clone,
{
    async fn try_get_log_entries<RB: RangeBounds<u64> + Clone + Debug + OptionalSend>(
        &mut self,
        range: RB,
    ) -> Result<Vec<C::Entry>, StorageError<NodeId>> {
        let log = self.log.read().await;
        let entries = log.entries.range(range).map(|(_, e)| e.clone()).collect();
        Ok(entries)
    }
}

// ---------------------------------------------------------------------------
// Internal data structs
// ---------------------------------------------------------------------------

/// Internal log data protected by a `RwLock`.
struct LogData<C: RaftTypeConfig<NodeId = NodeId>> {
    last_purged_log_id: Option<LogId<NodeId>>,
    entries: BTreeMap<u64, C::Entry>,
    vote: Option<Vote<NodeId>>,
    committed: Option<LogId<NodeId>>,
}

impl<C: RaftTypeConfig<NodeId = NodeId>> Default for LogData<C> {
    fn default() -> Self {
        Self {
            last_purged_log_id: None,
            entries: BTreeMap::new(),
            vote: None,
            committed: None,
        }
    }
}

/// Internal state-machine data protected by a `RwLock`.
///
/// The generic parameter `S` is the application's state type. It must be
/// `Default + Clone + serde::Serialize + serde::de::DeserializeOwned`.
pub struct SmData<C: RaftTypeConfig<NodeId = NodeId>, S> {
    /// Last applied log id.
    pub last_applied_log: Option<LogId<NodeId>>,
    /// Last membership config.
    pub last_membership: StoredMembership<NodeId, C::Node>,
    /// Application-specific state.
    pub state: S,
    /// Current snapshot bytes (serialized `S`), if any.
    pub current_snapshot: Option<StoredSnapshot<C>>,
}

impl<C: RaftTypeConfig<NodeId = NodeId>, S: Default> Default for SmData<C, S> {
    fn default() -> Self {
        Self {
            last_applied_log: None,
            last_membership: StoredMembership::default(),
            state: S::default(),
            current_snapshot: None,
        }
    }
}

/// A serialized snapshot stored in memory.
pub struct StoredSnapshot<C: RaftTypeConfig> {
    pub meta: SnapshotMeta<C::NodeId, C::Node>,
    pub data: Vec<u8>,
}

impl<C: RaftTypeConfig> Clone for StoredSnapshot<C> {
    fn clone(&self) -> Self {
        Self {
            meta: self.meta.clone(),
            data: self.data.clone(),
        }
    }
}

// ---------------------------------------------------------------------------
// MemLogStore -- implements RaftLogStorage
// ---------------------------------------------------------------------------

/// In-memory Raft log store (v2 API).
///
/// Implements `RaftLogReader` and `RaftLogStorage`.
pub struct MemLogStore<C: RaftTypeConfig<NodeId = NodeId>> {
    log: Arc<RwLock<LogData<C>>>,
}

impl<C: RaftTypeConfig<NodeId = NodeId>> MemLogStore<C> {
    /// Create a new, empty log store.
    #[must_use]
    pub fn new() -> Self {
        Self {
            log: Arc::new(RwLock::new(LogData::default())),
        }
    }
}

impl<C: RaftTypeConfig<NodeId = NodeId>> Default for MemLogStore<C> {
    fn default() -> Self {
        Self::new()
    }
}

impl<C: RaftTypeConfig<NodeId = NodeId>> Clone for MemLogStore<C> {
    fn clone(&self) -> Self {
        Self {
            log: Arc::clone(&self.log),
        }
    }
}

impl<C> RaftLogReader<C> for MemLogStore<C>
where
    C: RaftTypeConfig<NodeId = NodeId>,
    C::Entry: Clone,
{
    async fn try_get_log_entries<RB: RangeBounds<u64> + Clone + Debug + OptionalSend>(
        &mut self,
        range: RB,
    ) -> Result<Vec<C::Entry>, StorageError<NodeId>> {
        let log = self.log.read().await;
        let entries = log.entries.range(range).map(|(_, e)| e.clone()).collect();
        Ok(entries)
    }
}

impl<C> RaftLogStorage<C> for MemLogStore<C>
where
    C: RaftTypeConfig<NodeId = NodeId>,
    C::Entry: Clone,
{
    type LogReader = MemLogReader<C>;

    async fn get_log_state(&mut self) -> Result<LogState<C>, StorageError<NodeId>> {
        let log = self.log.read().await;
        let last = log
            .entries
            .iter()
            .next_back()
            .map(|(_, ent)| *ent.get_log_id());

        Ok(LogState {
            last_purged_log_id: log.last_purged_log_id,
            last_log_id: last,
        })
    }

    async fn get_log_reader(&mut self) -> Self::LogReader {
        MemLogReader {
            log: Arc::clone(&self.log),
        }
    }

    async fn save_vote(&mut self, vote: &Vote<NodeId>) -> Result<(), StorageError<NodeId>> {
        let mut log = self.log.write().await;
        log.vote = Some(*vote);
        Ok(())
    }

    async fn read_vote(&mut self) -> Result<Option<Vote<NodeId>>, StorageError<NodeId>> {
        let log = self.log.read().await;
        Ok(log.vote)
    }

    async fn save_committed(
        &mut self,
        committed: Option<LogId<NodeId>>,
    ) -> Result<(), StorageError<NodeId>> {
        let mut log = self.log.write().await;
        log.committed = committed;
        Ok(())
    }

    async fn read_committed(&mut self) -> Result<Option<LogId<NodeId>>, StorageError<NodeId>> {
        let log = self.log.read().await;
        Ok(log.committed)
    }

    async fn append<I>(
        &mut self,
        entries: I,
        callback: LogFlushed<C>,
    ) -> Result<(), StorageError<NodeId>>
    where
        I: IntoIterator<Item = C::Entry> + OptionalSend,
        I::IntoIter: OptionalSend,
    {
        let mut log = self.log.write().await;
        for entry in entries {
            let idx = entry.get_log_id().index;
            log.entries.insert(idx, entry);
        }
        // In-memory store: data is immediately "flushed".
        callback.log_io_completed(Ok(()));
        Ok(())
    }

    async fn truncate(&mut self, log_id: LogId<NodeId>) -> Result<(), StorageError<NodeId>> {
        let mut log = self.log.write().await;
        let keys: Vec<u64> = log.entries.range(log_id.index..).map(|(k, _)| *k).collect();
        for key in keys {
            log.entries.remove(&key);
        }
        Ok(())
    }

    async fn purge(&mut self, log_id: LogId<NodeId>) -> Result<(), StorageError<NodeId>> {
        let mut log = self.log.write().await;
        let keys: Vec<u64> = log
            .entries
            .range(..=log_id.index)
            .map(|(k, _)| *k)
            .collect();
        for key in keys {
            log.entries.remove(&key);
        }
        log.last_purged_log_id = Some(log_id);
        Ok(())
    }
}

// ---------------------------------------------------------------------------
// MemStateMachine -- implements RaftStateMachine
// ---------------------------------------------------------------------------

/// In-memory Raft state machine (v2 API).
///
/// Generic over:
/// - `C`: openraft `RaftTypeConfig`
/// - `S`: application state type
/// - `F`: a function `fn(&mut S, &C::D) -> C::R` that applies a log entry to the state.
///
/// The `apply_fn` approach keeps the state machine generic: the application provides
/// a closure that knows how to mutate `S` given a log entry payload of type `C::D`.
pub struct MemStateMachine<C, S, F>
where
    C: RaftTypeConfig<NodeId = NodeId>,
    S: Default + Clone + serde::Serialize + serde::de::DeserializeOwned + Send + Sync + 'static,
    F: Fn(&mut S, &C::D) -> C::R + Send + Sync + 'static,
{
    sm: Arc<RwLock<SmData<C, S>>>,
    apply_fn: Arc<F>,
}

impl<C, S, F> MemStateMachine<C, S, F>
where
    C: RaftTypeConfig<NodeId = NodeId>,
    S: Default + Clone + serde::Serialize + serde::de::DeserializeOwned + Send + Sync + 'static,
    F: Fn(&mut S, &C::D) -> C::R + Send + Sync + 'static,
{
    /// Create a new state machine with the given apply function.
    pub fn new(apply_fn: F) -> Self {
        Self {
            sm: Arc::new(RwLock::new(SmData::default())),
            apply_fn: Arc::new(apply_fn),
        }
    }

    /// Get a read handle to the inner state machine data (for reading application state).
    #[must_use]
    pub fn data(&self) -> Arc<RwLock<SmData<C, S>>> {
        Arc::clone(&self.sm)
    }
}

impl<C, S, F> Clone for MemStateMachine<C, S, F>
where
    C: RaftTypeConfig<NodeId = NodeId>,
    S: Default + Clone + serde::Serialize + serde::de::DeserializeOwned + Send + Sync + 'static,
    F: Fn(&mut S, &C::D) -> C::R + Send + Sync + 'static,
{
    fn clone(&self) -> Self {
        Self {
            sm: Arc::clone(&self.sm),
            apply_fn: Arc::clone(&self.apply_fn),
        }
    }
}

// -- Snapshot builder -------------------------------------------------------

/// Snapshot builder for the in-memory state machine.
pub struct MemSnapshotBuilder<C, S, F>
where
    C: RaftTypeConfig<NodeId = NodeId>,
    S: Default + Clone + serde::Serialize + serde::de::DeserializeOwned + Send + Sync + 'static,
    F: Fn(&mut S, &C::D) -> C::R + Send + Sync + 'static,
{
    sm: Arc<RwLock<SmData<C, S>>>,
    _phantom: std::marker::PhantomData<F>,
}

impl<C, S, F> RaftSnapshotBuilder<C> for MemSnapshotBuilder<C, S, F>
where
    C: RaftTypeConfig<NodeId = NodeId, SnapshotData = Cursor<Vec<u8>>>,
    S: Default + Clone + serde::Serialize + serde::de::DeserializeOwned + Send + Sync + 'static,
    F: Fn(&mut S, &C::D) -> C::R + Send + Sync + 'static,
{
    async fn build_snapshot(&mut self) -> Result<Snapshot<C>, StorageError<NodeId>> {
        let sm = self.sm.read().await;

        let data = postcard2::to_vec(&sm.state).map_err(|e| {
            StorageError::from_io_error(
                openraft::ErrorSubject::StateMachine,
                openraft::ErrorVerb::Read,
                std::io::Error::other(e),
            )
        })?;

        let snapshot_id = if let Some(ref last) = sm.last_applied_log {
            format!("{}-{}", last.leader_id, last.index)
        } else {
            "0-0".to_string()
        };

        let meta = SnapshotMeta {
            last_log_id: sm.last_applied_log,
            last_membership: sm.last_membership.clone(),
            snapshot_id,
        };

        debug!(
            last_log_id = ?meta.last_log_id,
            "Built in-memory snapshot"
        );

        Ok(Snapshot {
            meta,
            snapshot: Box::new(Cursor::new(data)),
        })
    }
}

// -- RaftStateMachine impl --------------------------------------------------

impl<C, S, F> RaftStateMachine<C> for MemStateMachine<C, S, F>
where
    C: RaftTypeConfig<NodeId = NodeId, SnapshotData = Cursor<Vec<u8>>, Entry = Entry<C>>,
    C::D: Clone + serde::Serialize + serde::de::DeserializeOwned,
    C::R: Default,
    S: Default + Clone + serde::Serialize + serde::de::DeserializeOwned + Send + Sync + 'static,
    F: Fn(&mut S, &C::D) -> C::R + Send + Sync + 'static,
{
    type SnapshotBuilder = MemSnapshotBuilder<C, S, F>;

    async fn applied_state(
        &mut self,
    ) -> Result<(Option<LogId<NodeId>>, StoredMembership<NodeId, C::Node>), StorageError<NodeId>>
    {
        let sm = self.sm.read().await;
        Ok((sm.last_applied_log, sm.last_membership.clone()))
    }

    async fn apply<I>(&mut self, entries: I) -> Result<Vec<C::R>, StorageError<NodeId>>
    where
        I: IntoIterator<Item = C::Entry> + OptionalSend,
        I::IntoIter: OptionalSend,
    {
        let mut sm = self.sm.write().await;
        let mut responses = Vec::new();

        for entry in entries {
            sm.last_applied_log = Some(entry.log_id);

            match entry.payload {
                EntryPayload::Normal(ref data) => {
                    let resp = (self.apply_fn)(&mut sm.state, data);
                    responses.push(resp);
                }
                EntryPayload::Membership(ref mem) => {
                    sm.last_membership = StoredMembership::new(Some(entry.log_id), mem.clone());
                    responses.push(C::R::default());
                }
                EntryPayload::Blank => {
                    responses.push(C::R::default());
                }
            }
        }

        Ok(responses)
    }

    async fn get_snapshot_builder(&mut self) -> Self::SnapshotBuilder {
        MemSnapshotBuilder {
            sm: Arc::clone(&self.sm),
            _phantom: std::marker::PhantomData,
        }
    }

    async fn begin_receiving_snapshot(
        &mut self,
    ) -> Result<Box<Cursor<Vec<u8>>>, StorageError<NodeId>> {
        Ok(Box::new(Cursor::new(Vec::new())))
    }

    async fn install_snapshot(
        &mut self,
        meta: &SnapshotMeta<NodeId, C::Node>,
        snapshot: Box<Cursor<Vec<u8>>>,
    ) -> Result<(), StorageError<NodeId>> {
        let data = snapshot.into_inner();
        let state: S = postcard2::from_bytes(&data).map_err(|e| {
            StorageError::from_io_error(
                openraft::ErrorSubject::Snapshot(None),
                openraft::ErrorVerb::Read,
                std::io::Error::other(e),
            )
        })?;

        let mut sm = self.sm.write().await;
        sm.last_applied_log = meta.last_log_id;
        sm.last_membership = meta.last_membership.clone();
        sm.state = state.clone();

        // Store the snapshot
        let snapshot_data = postcard2::to_vec(&state).map_err(|e| {
            StorageError::from_io_error(
                openraft::ErrorSubject::Snapshot(None),
                openraft::ErrorVerb::Write,
                std::io::Error::other(e),
            )
        })?;
        sm.current_snapshot = Some(StoredSnapshot {
            meta: meta.clone(),
            data: snapshot_data,
        });

        debug!(
            last_log_id = ?meta.last_log_id,
            "Installed snapshot into in-memory state machine"
        );

        Ok(())
    }

    async fn get_current_snapshot(&mut self) -> Result<Option<Snapshot<C>>, StorageError<NodeId>> {
        let sm = self.sm.read().await;
        match &sm.current_snapshot {
            Some(stored) => Ok(Some(Snapshot {
                meta: stored.meta.clone(),
                snapshot: Box::new(Cursor::new(stored.data.clone())),
            })),
            None => Ok(None),
        }
    }
}

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

    // Minimal type config for testing
    openraft::declare_raft_types!(
        pub TestTypeConfig:
            D = String,
            R = String,
    );

    #[tokio::test]
    async fn test_log_store_empty_state() {
        let mut store = MemLogStore::<TestTypeConfig>::new();
        let state = RaftLogStorage::get_log_state(&mut store).await.unwrap();
        assert!(state.last_purged_log_id.is_none());
        assert!(state.last_log_id.is_none());
    }

    #[tokio::test]
    async fn test_vote_round_trip() {
        let mut store = MemLogStore::<TestTypeConfig>::new();

        let vote = RaftLogStorage::read_vote(&mut store).await.unwrap();
        assert!(vote.is_none());

        let new_vote = Vote::new(1, 1);
        RaftLogStorage::save_vote(&mut store, &new_vote)
            .await
            .unwrap();

        let vote = RaftLogStorage::read_vote(&mut store).await.unwrap();
        assert_eq!(vote, Some(new_vote));
    }

    #[tokio::test]
    async fn test_state_machine_new() {
        let sm = MemStateMachine::<TestTypeConfig, Vec<String>, _>::new(
            |state: &mut Vec<String>, data: &String| {
                state.push(data.clone());
                format!("applied: {data}")
            },
        );

        let data = sm.data();
        {
            let d = data.read().await;
            assert!(d.state.is_empty());
        }
    }
}