Skip to content

Commit

Permalink
raft: rename Progress.last to match_index
Browse files Browse the repository at this point in the history
  • Loading branch information
erikgrinaker committed Jun 4, 2024
1 parent 3619a3b commit bcf719a
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 39 deletions.
4 changes: 2 additions & 2 deletions src/bin/toysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ The following commands are also available:
let status = self.client.status()?;
let mut node_logs = status
.raft
.last_index
.match_index
.iter()
.map(|(id, index)| format!("{}:{}", id, index))
.collect::<Vec<_>>();
Expand All @@ -134,7 +134,7 @@ Storage: {keys} keys, {logical_size} MB logical, {nodes}x {disk_size} MB disk,
server = status.server,
leader = status.raft.leader,
term = status.raft.term,
nodes = status.raft.last_index.len(),
nodes = status.raft.match_index.len(),
committed = status.raft.commit_index,
applied = status.raft.apply_index,
raft_storage = status.raft.storage.name,
Expand Down
6 changes: 2 additions & 4 deletions src/raft/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,9 @@ pub struct Status {
pub leader: NodeID,
/// The current Raft term.
pub term: Term,
/// The last log indexes of all nodes. Use a BTreeMap for deterministic
/// The match indexes of all nodes. Use a BTreeMap for deterministic
/// debug output.
///
/// TODO: rename to match.
pub last_index: BTreeMap<NodeID, Index>,
pub match_index: BTreeMap<NodeID, Index>,
/// The current commit index.
pub commit_index: Index,
/// The current applied index.
Expand Down
52 changes: 27 additions & 25 deletions src/raft/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -681,16 +681,13 @@ impl RawNode<Follower> {
}
}

/// Peer replication progress.
/// Follower replication progress.
#[derive(Clone, Debug, PartialEq)]
struct Progress {
/// The next index to replicate to the peer.
next: Index,
/// The last index known to be replicated to the peer.
///
/// TODO: rename to match. It needs to track the position where the
/// follower's log matches the leader's, not its last position.
last: Index,
/// The next index to replicate to the follower.
next_index: Index,
/// The last index where the follower's log matches the leader.
match_index: Index,
/// The last read sequence number confirmed by the peer.
read_seq: ReadSequence,
}
Expand Down Expand Up @@ -720,7 +717,7 @@ struct Read {
// A leader serves requests and replicates the log to followers.
#[derive(Clone, Debug, PartialEq)]
pub struct Leader {
/// Peer replication progress.
/// Follower replication progress.
progress: HashMap<NodeID, Progress>,
/// Keeps track of pending write requests, keyed by log index. These are
/// added when the write is proposed and appended to the leader's log, and
Expand Down Expand Up @@ -749,9 +746,11 @@ pub struct Leader {
impl Leader {
/// Creates a new leader role.
fn new(peers: HashSet<NodeID>, last_index: Index) -> Self {
let next = last_index + 1;
let progress =
peers.into_iter().map(|p| (p, Progress { next, last: 0, read_seq: 0 })).collect();
let next_index = last_index + 1;
let progress = peers
.into_iter()
.map(|p| (p, Progress { next_index, match_index: 0, read_seq: 0 }))
.collect();
Self {
progress,
writes: HashMap::new(),
Expand Down Expand Up @@ -859,11 +858,12 @@ impl RawNode<Leader> {
Message::AppendResponse { reject_index: 0, last_index, last_term } => {
let (li, lt) = self.log.get_last_index();
assert!(last_index <= li && last_term <= lt, "follower appended future entries");
debug_assert!(self.log.has(last_index, last_term)?, "follower has unknown tail");

let progress = self.role.progress.get_mut(&msg.from).expect("unknown node");
if last_index > progress.last {
progress.last = last_index;
progress.next = last_index + 1;
if last_index > progress.match_index {
progress.match_index = last_index;
progress.next_index = last_index + 1;
self.maybe_commit_and_apply()?;
}
}
Expand All @@ -880,8 +880,8 @@ impl RawNode<Leader> {
// If the next index was rejected, try the previous one.
// Otherwise, the rejection is stale and can be ignored. If the
// follower's log is shorter, skip straight to its last index.
if progress.next == reject_index + 1 {
progress.next = std::cmp::min(progress.next - 1, last_index + 1);
if progress.next_index == reject_index + 1 {
progress.next_index = std::cmp::min(progress.next_index - 1, last_index + 1);
self.send_log(msg.from)?;
}
}
Expand Down Expand Up @@ -918,11 +918,11 @@ impl RawNode<Leader> {
let status = Status {
leader: self.id,
term: self.term,
last_index: self
match_index: self
.role
.progress
.iter()
.map(|(id, p)| (*id, p.last))
.map(|(id, p)| (*id, p.match_index))
.chain(std::iter::once((self.id, self.log.get_last_index().0)))
.sorted()
.collect(),
Expand Down Expand Up @@ -993,7 +993,7 @@ impl RawNode<Leader> {
self.role
.progress
.values()
.map(|p| p.last)
.map(|p| p.match_index)
.chain(std::iter::once(self.log.get_last_index().0))
.collect(),
);
Expand Down Expand Up @@ -1073,10 +1073,12 @@ impl RawNode<Leader> {
/// Sends pending log entries to a peer.
fn send_log(&mut self, peer: NodeID) -> Result<()> {
let (base_index, base_term) = match self.role.progress.get(&peer) {
Some(Progress { next, .. }) if *next > 1 => match self.log.get(next - 1)? {
Some(entry) => (entry.index, entry.term),
None => panic!("missing base entry {}", next - 1),
},
Some(Progress { next_index, .. }) if *next_index > 1 => {
match self.log.get(next_index - 1)? {
Some(entry) => (entry.index, entry.term),
None => panic!("missing base entry {}", next_index - 1),
}
}
Some(_) => (0, 0),
None => panic!("unknown peer {}", peer),
};
Expand Down Expand Up @@ -1643,7 +1645,7 @@ mod tests {
.progress
.iter()
.sorted_by_key(|(id, _)| *id)
.map(|(id, pr)| format!("{id}:{}→{}", pr.last, pr.next))
.map(|(id, pr)| format!("{id}:{}→{}", pr.match_index, pr.next_index))
.join(" ")
))
}
Expand Down
10 changes: 5 additions & 5 deletions src/raft/testscripts/node/request_status
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ status request=true 1
stabilize
---
c1@1 → n1 ClientRequest id=0x02 status
n1@1 → c1 ClientResponse id=0x02 status Status { leader: 1, term: 1, last_index: {1: 2, 2: 2, 3: 1}, commit_index: 2, apply_index: 2, storage: Status { name: "memory", keys: 4, size: 41, total_disk_size: 0, live_disk_size: 0, garbage_disk_size: 0 } }
n1@1 → c1 ClientResponse id=0x02 status Status { leader: 1, term: 1, match_index: {1: 2, 2: 2, 3: 1}, commit_index: 2, apply_index: 2, storage: Status { name: "memory", keys: 4, size: 41, total_disk_size: 0, live_disk_size: 0, garbage_disk_size: 0 } }
c1@1 status ⇒ Status {
leader: 1,
term: 1,
last_index: {
match_index: {
1: 2,
2: 2,
3: 1,
Expand All @@ -52,12 +52,12 @@ stabilize
---
c2@1 → n2 ClientRequest id=0x03 status
n2@1 → n1 ClientRequest id=0x03 status
n1@1 → n2 ClientResponse id=0x03 status Status { leader: 1, term: 1, last_index: {1: 2, 2: 2, 3: 1}, commit_index: 2, apply_index: 2, storage: Status { name: "memory", keys: 4, size: 41, total_disk_size: 0, live_disk_size: 0, garbage_disk_size: 0 } }
n2@1 → c2 ClientResponse id=0x03 status Status { leader: 1, term: 1, last_index: {1: 2, 2: 2, 3: 1}, commit_index: 2, apply_index: 2, storage: Status { name: "memory", keys: 4, size: 41, total_disk_size: 0, live_disk_size: 0, garbage_disk_size: 0 } }
n1@1 → n2 ClientResponse id=0x03 status Status { leader: 1, term: 1, match_index: {1: 2, 2: 2, 3: 1}, commit_index: 2, apply_index: 2, storage: Status { name: "memory", keys: 4, size: 41, total_disk_size: 0, live_disk_size: 0, garbage_disk_size: 0 } }
n2@1 → c2 ClientResponse id=0x03 status Status { leader: 1, term: 1, match_index: {1: 2, 2: 2, 3: 1}, commit_index: 2, apply_index: 2, storage: Status { name: "memory", keys: 4, size: 41, total_disk_size: 0, live_disk_size: 0, garbage_disk_size: 0 } }
c2@1 status ⇒ Status {
leader: 1,
term: 1,
last_index: {
match_index: {
1: 2,
2: 2,
3: 1,
Expand Down
4 changes: 2 additions & 2 deletions src/raft/testscripts/node/request_status_single
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ status request=true 1
stabilize
---
c1@1 → n1 ClientRequest id=0x02 status
n1@1 → c1 ClientResponse id=0x02 status Status { leader: 1, term: 1, last_index: {1: 2}, commit_index: 2, apply_index: 2, storage: Status { name: "memory", keys: 4, size: 41, total_disk_size: 0, live_disk_size: 0, garbage_disk_size: 0 } }
n1@1 → c1 ClientResponse id=0x02 status Status { leader: 1, term: 1, match_index: {1: 2}, commit_index: 2, apply_index: 2, storage: Status { name: "memory", keys: 4, size: 41, total_disk_size: 0, live_disk_size: 0, garbage_disk_size: 0 } }
c1@1 status ⇒ Status {
leader: 1,
term: 1,
last_index: {
match_index: {
1: 2,
},
commit_index: 2,
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ fn status() -> Result<()> {
raft: raft::Status {
leader: 1,
term: 1,
last_index: [(1, 27)].into(),
match_index: [(1, 27)].into(),
commit_index: 27,
apply_index: 27,
storage: storage::engine::Status {
Expand Down

0 comments on commit bcf719a

Please sign in to comment.