Skip to content

Commit

Permalink
raft: clean up node module
Browse files Browse the repository at this point in the history
  • Loading branch information
erikgrinaker committed Jun 10, 2024
1 parent 2cfaa8d commit 5d9ad01
Show file tree
Hide file tree
Showing 10 changed files with 658 additions and 709 deletions.
3 changes: 3 additions & 0 deletions src/raft/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ impl encoding::Key<'_> for KeyPrefix {}
/// 5 | 2 | UPDATE table SET value = 'bar' WHERE id = 1
/// 6 | 2 | DELETE FROM table WHERE id = 1
///
/// Note that this is for illustration only, and the actual toyDB Raft commands
/// are not SQL statements but lower-level write operations.
///
/// A key/value store is used to store the log entries on disk, keyed by index,
/// along with a few other metadata keys (e.g. who we voted for in this term).
///
Expand Down
1,319 changes: 631 additions & 688 deletions src/raft/node.rs

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions src/raft/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ pub trait State: Send {
///
/// This is only executed on a single replica/node, so it must not result in
/// any state changes (i.e. it must not write).
///
/// TODO: consider making this &mut, since the storage engine requires
/// exclusive access anyway.
fn read(&self, command: Vec<u8>) -> Result<Vec<u8>>;
}

Expand Down
6 changes: 3 additions & 3 deletions src/raft/testscripts/node/append_initial
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,12 @@ n3@1 → n1 AppendResponse match_index=2

log
---
n1@1 last=2@1 commit=2@1
n1@1 last=2@1 commit=2@1 term=1 vote=Some(1)
n1@1 entry 1@1 None
n1@1 entry 2@1 put foo=bar
n2@1 last=2@1 commit=0@0
n2@1 last=2@1 commit=0@0 term=1 vote=Some(1)
n2@1 entry 1@1 None
n2@1 entry 2@1 put foo=bar
n3@1 last=2@1 commit=0@0
n3@1 last=2@1 commit=0@0 term=1 vote=None
n3@1 entry 1@1 None
n3@1 entry 2@1 put foo=bar
4 changes: 2 additions & 2 deletions src/raft/testscripts/node/append_probe_divergent_first
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,15 @@ n5@4 leader last=7@4 commit=7@4 apply=7 progress={1:0→8 2:0→8 3:7→8 4:7→

log 1 5
---
n1@1 last=7@1 commit=1@1
n1@1 last=7@1 commit=1@1 term=1 vote=Some(1)
n1@1 entry 1@1 None
n1@1 entry 2@1 put a=2
n1@1 entry 3@1 put a=3
n1@1 entry 4@1 put a=4
n1@1 entry 5@1 put a=5
n1@1 entry 6@1 put a=6
n1@1 entry 7@1 put a=7
n5@4 last=7@4 commit=7@4
n5@4 last=7@4 commit=7@4 term=4 vote=Some(5)
n5@4 entry 1@1 None
n5@4 entry 2@2 None
n5@4 entry 3@2 put a=1
Expand Down
4 changes: 2 additions & 2 deletions src/raft/testscripts/node/append_probe_divergent_long
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ n5@4 leader last=9@4 commit=9@4 apply=9 progress={1:0→10 2:0→10 3:9→10 4:9

log 1 5
---
n1@1 last=12@1 commit=3@1
n1@1 last=12@1 commit=3@1 term=1 vote=Some(1)
n1@1 entry 1@1 None
n1@1 entry 2@1 put a=1
n1@1 entry 3@1 put b=2
Expand All @@ -99,7 +99,7 @@ n1@1 entry 9@1 put a=7
n1@1 entry 10@1 put a=8
n1@1 entry 11@1 put a=9
n1@1 entry 12@1 put a=10
n5@4 last=9@4 commit=9@4
n5@4 last=9@4 commit=9@4 term=4 vote=Some(5)
n5@4 entry 1@1 None
n5@4 entry 2@1 put a=1
n5@4 entry 3@1 put b=2
Expand Down
4 changes: 2 additions & 2 deletions src/raft/testscripts/node/append_probe_divergent_short
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,12 @@ n5@4 leader last=9@4 commit=9@4 apply=9 progress={1:0→10 2:0→10 3:9→10 4:9

log 1 5
---
n1@1 last=4@1 commit=3@1
n1@1 last=4@1 commit=3@1 term=1 vote=Some(1)
n1@1 entry 1@1 None
n1@1 entry 2@1 put a=1
n1@1 entry 3@1 put b=2
n1@1 entry 4@1 put a=2
n5@4 last=9@4 commit=9@4
n5@4 last=9@4 commit=9@4 term=4 vote=Some(5)
n5@4 entry 1@1 None
n5@4 entry 2@1 put a=1
n5@4 entry 3@1 put b=2
Expand Down
12 changes: 6 additions & 6 deletions src/raft/testscripts/node/append_probe_divergent_single
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ n2@1 → n1 AppendResponse match_index=2

log 1 2
---
n1@1 last=2@1 commit=1@1
n1@1 last=2@1 commit=1@1 term=1 vote=Some(1)
n1@1 entry 1@1 None
n1@1 entry 2@1 put a=1
n2@1 last=2@1 commit=1@1
n2@1 last=2@1 commit=1@1 term=1 vote=Some(1)
n2@1 entry 1@1 None
n2@1 entry 2@1 put a=1

Expand Down Expand Up @@ -80,10 +80,10 @@ n2@2 → n5 AppendResponse match_index=1

log 1 2
---
n1@2 last=2@1 commit=1@1
n1@2 last=2@1 commit=1@1 term=2 vote=None
n1@2 entry 1@1 None
n1@2 entry 2@1 put a=1
n2@2 last=2@1 commit=1@1
n2@2 last=2@1 commit=1@1 term=2 vote=None
n2@2 entry 1@1 None
n2@2 entry 2@1 put a=1

Expand All @@ -102,11 +102,11 @@ n2@2 → n5 AppendResponse match_index=3

log 1 2
---
n1@2 last=3@2 commit=1@1
n1@2 last=3@2 commit=1@1 term=2 vote=None
n1@2 entry 1@1 None
n1@2 entry 2@2 None
n1@2 entry 3@2 put b=2
n2@2 last=3@2 commit=1@1
n2@2 last=3@2 commit=1@1 term=2 vote=None
n2@2 entry 1@1 None
n2@2 entry 2@2 None
n2@2 entry 3@2 put b=2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ n1@1 → n3 Append base=1@1 [2@1]
# An AppendResponse beyond leader's last log should panic.
!step 1 '{"from":2, "to":1, "term":1, "message":{"AppendResponse":{"match_index":3,"reject_index":0}}}'
---
Panic: follower matched unknown index
Panic: future match index
10 changes: 5 additions & 5 deletions src/raft/testscripts/node/election_candidate_behind_leader
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ n5@1 follower(n1) last=1@1 commit=1@1 apply=1
# List the logs on n1 n2 n3 to show the replicated but uncommitted entry.
log 1 2 3
---
n1@1 last=2@1 commit=1@1
n1@1 last=2@1 commit=1@1 term=1 vote=Some(1)
n1@1 entry 1@1 None
n1@1 entry 2@1 put foo=bar
n2@1 last=2@1 commit=1@1
n2@1 last=2@1 commit=1@1 term=1 vote=Some(1)
n2@1 entry 1@1 None
n2@1 entry 2@1 put foo=bar
n3@1 last=1@1 commit=1@1
n3@1 last=1@1 commit=1@1 term=1 vote=Some(1)
n3@1 entry 1@1 None

# Heal the partition.
Expand Down Expand Up @@ -116,10 +116,10 @@ n4@2 → n5 HeartbeatResponse match_index=2 read_seq=0
# empty log entry appended by n5 when it became leader.
log 1 2
---
n1@2 last=2@2 commit=2@2
n1@2 last=2@2 commit=2@2 term=2 vote=None
n1@2 entry 1@1 None
n1@2 entry 2@2 None
n2@2 last=2@2 commit=2@2
n2@2 last=2@2 commit=2@2 term=2 vote=None
n2@2 entry 1@1 None
n2@2 entry 2@2 None

Expand Down

0 comments on commit 5d9ad01

Please sign in to comment.