Skip to content

Commit

Permalink
raft: fix goldenscript append conflict handling
Browse files Browse the repository at this point in the history
  • Loading branch information
erikgrinaker committed Jun 7, 2024
1 parent 5d09b8e commit bf32044
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/raft/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1793,8 +1793,9 @@ mod tests {
let mut node = self.nodes.remove(&id).ok_or(format!("unknown node {id}"))?;

let old_noderole = Self::format_node_role(&node);
let old_commit_index = Self::borrow_log(&node).get_commit_index().0;
let old_last_index = Self::borrow_log(&node).get_last_index().0;
let log = Self::borrow_log_mut(&mut node);
let old_commit_index = log.get_commit_index().0;
let entries = log.scan(..)?.collect::<crate::error::Result<Vec<_>>>()?;

// Apply the transition.
node = f(node)?;
Expand All @@ -1804,10 +1805,11 @@ mod tests {

let log = Self::borrow_log_mut(&mut node);
let (commit_index, commit_term) = log.get_commit_index();
// TODO: this doesn't handle replaced indexes. It needs to fetch the
// index/term of all entries and compare them.
let appended: Vec<_> =
log.scan(old_last_index + 1..)?.collect::<crate::error::Result<_>>()?;
let mut appended = log.scan(..)?.collect::<crate::error::Result<Vec<_>>>()?;
match appended.iter().zip(entries.iter()).position(|(a, e)| a.term != e.term) {
Some(i) => appended = appended[i..].to_vec(),
None => appended = appended[entries.len()..].to_vec(),
}

self.nodes.insert(id, node);

Expand Down
12 changes: 12 additions & 0 deletions src/raft/testscripts/node/append_probe_divergent_first
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,20 @@ deliver 1 2
n5@4 → n1 Append base=1@1 [2@2 3@2 4@3 5@3 6@4 7@4 8@4]
n5@4 → n2 Append base=1@1 [2@2 3@2 4@3 5@3 6@4 7@4 8@4]
n5@4 leader last=8@4 commit=7@4 apply=7 progress={1:1→9 2:1→9 3:7→9 4:7→9}
n1@4 append 2@2 None
n1@4 append 3@2 put a=1
n1@4 append 4@3 None
n1@4 append 5@3 put b=2
n1@4 append 6@4 None
n1@4 append 7@4 put c=3
n1@4 append 8@4 put d=4
n1@4 → n5 AppendResponse match_index=8
n2@4 append 2@2 None
n2@4 append 3@2 put a=1
n2@4 append 4@3 None
n2@4 append 5@3 put b=2
n2@4 append 6@4 None
n2@4 append 7@4 put c=3
n2@4 append 8@4 put d=4
n2@4 → n5 AppendResponse match_index=8

Expand Down
14 changes: 14 additions & 0 deletions src/raft/testscripts/node/append_probe_divergent_long
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,21 @@ deliver 1 2
n5@4 → n1 Append base=3@1 [4@2 5@2 6@3 7@3 8@4 9@4 10@4]
n5@4 → n2 Append base=3@1 [4@2 5@2 6@3 7@3 8@4 9@4 10@4]
n5@4 leader last=10@4 commit=9@4 apply=9 progress={1:3→11 2:3→11 3:9→11 4:9→11}
n1@4 append 4@2 None
n1@4 append 5@2 put c=3
n1@4 append 6@3 None
n1@4 append 7@3 put d=4
n1@4 append 8@4 None
n1@4 append 9@4 put e=5
n1@4 append 10@4 put f=6
n1@4 → n5 AppendResponse match_index=10
n2@4 append 4@2 None
n2@4 append 5@2 put c=3
n2@4 append 6@3 None
n2@4 append 7@3 put d=4
n2@4 append 8@4 None
n2@4 append 9@4 put e=5
n2@4 append 10@4 put f=6
n2@4 → n5 AppendResponse match_index=10

deliver 5
Expand Down
2 changes: 2 additions & 0 deletions src/raft/testscripts/node/append_probe_divergent_short
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,15 @@ deliver 1 2
---
n5@4 → n1 Append base=3@1 [4@2 5@2 6@3 7@3 8@4 9@4 10@4]
n5@4 → n2 Append base=3@1 [4@2 5@2 6@3 7@3 8@4 9@4 10@4]
n1@4 append 4@2 None
n1@4 append 5@2 put c=3
n1@4 append 6@3 None
n1@4 append 7@3 put d=4
n1@4 append 8@4 None
n1@4 append 9@4 put e=5
n1@4 append 10@4 put f=6
n1@4 → n5 AppendResponse match_index=10
n2@4 append 4@2 None
n2@4 append 5@2 put c=3
n2@4 append 6@3 None
n2@4 append 7@3 put d=4
Expand Down
2 changes: 2 additions & 0 deletions src/raft/testscripts/node/append_probe_divergent_single
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,10 @@ deliver 1 2
---
n5@2 → n1 Append base=1@1 [2@2 3@2]
n5@2 → n2 Append base=1@1 [2@2 3@2]
n1@2 append 2@2 None
n1@2 append 3@2 put b=2
n1@2 → n5 AppendResponse match_index=3
n2@2 append 2@2 None
n2@2 append 3@2 put b=2
n2@2 → n5 AppendResponse match_index=3

Expand Down
2 changes: 2 additions & 0 deletions src/raft/testscripts/node/election_candidate_behind_leader
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,11 @@ n5@2 → n2 Heartbeat last_index=2 commit_index=1 read_seq=0
n5@2 → n3 Heartbeat last_index=2 commit_index=1 read_seq=0
n5@2 → n4 Heartbeat last_index=2 commit_index=1 read_seq=0
n1@2 follower() ⇨ n1@2 follower(n5)
n1@2 append 2@2 None
n1@2 → n5 AppendResponse match_index=2
n1@2 → n5 HeartbeatResponse match_index=2 read_seq=0
n2@2 follower() ⇨ n2@2 follower(n5)
n2@2 append 2@2 None
n2@2 → n5 AppendResponse match_index=2
n2@2 → n5 HeartbeatResponse match_index=2 read_seq=0
n3@2 follower() ⇨ n3@2 follower(n5)
Expand Down
7 changes: 7 additions & 0 deletions src/raft/testscripts/node/heartbeat_probe_divergent
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,13 @@ n3@3 → n1 Append base=3@1 [4@2 5@2 6@2 7@3 8@3 9@3 10@3]

deliver 1
---
n1@3 append 4@2 None
n1@3 append 5@2 put c=3
n1@3 append 6@2 put d=4
n1@3 append 7@3 None
n1@3 append 8@3 put e=5
n1@3 append 9@3 put f=6
n1@3 append 10@3 put g=7
n1@3 → n3 AppendResponse match_index=10

deliver 3
Expand Down

0 comments on commit bf32044

Please sign in to comment.