lol_core/storage/
persistency.rs1use super::RaftStorage;
2use super::{Ballot, Entry};
3use crate::{Clock, Command, Id};
4use anyhow::Result;
5
6#[derive(serde::Serialize, serde::Deserialize)]
7struct EntryB {
8 prev_clock: (u64, u64),
9 this_clock: (u64, u64),
10 command: bytes::Bytes,
11}
12#[derive(serde::Serialize, serde::Deserialize)]
13struct BallotB {
14 term: u64,
15 voted_for: Option<Id>,
16}
17#[derive(serde::Serialize, serde::Deserialize)]
18struct SnapshotIndexB(u64);
19
20impl From<Vec<u8>> for Entry {
21 fn from(x: Vec<u8>) -> Self {
22 let x: EntryB = bincode::deserialize(&x).unwrap();
23 Entry {
24 prev_clock: Clock {
25 term: x.prev_clock.0,
26 index: x.prev_clock.1,
27 },
28 this_clock: Clock {
29 term: x.this_clock.0,
30 index: x.this_clock.1,
31 },
32 command: x.command.into(),
33 }
34 }
35}
36impl Into<Vec<u8>> for Entry {
37 fn into(self) -> Vec<u8> {
38 let x = EntryB {
39 prev_clock: (self.prev_clock.term, self.prev_clock.index),
40 this_clock: (self.this_clock.term, self.this_clock.index),
41 command: self.command,
42 };
43 bincode::serialize(&x).unwrap()
44 }
45}
46
47impl From<Vec<u8>> for Ballot {
48 fn from(x: Vec<u8>) -> Self {
49 let x: BallotB = bincode::deserialize(&x).unwrap();
50 Ballot {
51 cur_term: x.term,
52 voted_for: x.voted_for,
53 }
54 }
55}
56impl Into<Vec<u8>> for Ballot {
57 fn into(self) -> Vec<u8> {
58 let x = BallotB {
59 term: self.cur_term,
60 voted_for: self.voted_for,
61 };
62 bincode::serialize(&x).unwrap()
63 }
64}
65
66pub(crate) async fn test_pre_close(s: impl RaftStorage) -> Result<()> {
67 use std::collections::HashSet;
68
69 let e = Entry {
70 prev_clock: Clock { term: 0, index: 0 },
71 this_clock: Clock { term: 0, index: 0 },
72 command: Command::serialize(&Command::Noop),
73 };
74 let sn = Entry {
75 prev_clock: Clock { term: 0, index: 0 },
76 this_clock: Clock { term: 0, index: 0 },
77 command: Command::serialize(&Command::Snapshot {
78 membership: HashSet::new(),
79 }),
80 };
81 s.insert_entry(1, sn.clone()).await?;
82 s.insert_entry(2, e.clone()).await?;
83 s.insert_entry(3, e.clone()).await?;
84 s.insert_entry(4, e.clone()).await?;
85 s.insert_entry(3, sn.clone()).await?;
86 s.save_ballot(Ballot {
87 cur_term: 1,
88 voted_for: None,
89 })
90 .await?;
91 Ok(())
92}
93
94pub(crate) async fn test_post_close(s: impl RaftStorage) -> Result<()> {
95 assert_eq!(
96 s.load_ballot().await?,
97 Ballot {
98 cur_term: 1,
99 voted_for: None,
100 }
101 );
102 assert_eq!(super::find_last_snapshot_index(&s).await?, Some(3));
103 assert_eq!(s.get_last_index().await?, 4);
104 Ok(())
105}