Skip to content

Commit

Permalink
Return log index from raft::Log::append()
Browse files Browse the repository at this point in the history
  • Loading branch information
erikgrinaker committed Nov 14, 2023
1 parent 3cd3924 commit 0d6795b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 10 deletions.
13 changes: 5 additions & 8 deletions src/raft/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,14 @@ impl Log {
self.store.set_metadata(&Key::TermVote.encode(), bincode::serialize(&(term, voted_for))?)
}

/// Appends a command to the log, returning the entry.
pub fn append(&mut self, term: Term, command: Option<Vec<u8>>) -> Result<Entry> {
/// Appends a command to the log, returning the log index.
pub fn append(&mut self, term: Term, command: Option<Vec<u8>>) -> Result<Index> {
let entry = Entry { index: self.last_index + 1, term, command };
debug!("Appending log entry {}: {:?}", entry.index, entry);
self.store.append(bincode::serialize(&entry)?)?;
self.last_index = entry.index;
self.last_term = entry.term;
Ok(entry)
Ok(entry.index)
}

/// Commits entries up to and including an index.
Expand Down Expand Up @@ -220,10 +220,7 @@ mod tests {
let (mut l, _) = setup()?;
assert_eq!(Ok(None), l.get(1));

assert_eq!(
Entry { index: 1, term: 3, command: Some(vec![0x01]) },
l.append(3, Some(vec![0x01]))?
);
assert_eq!(1, l.append(3, Some(vec![0x01]))?);
assert_eq!(Some(Entry { index: 1, term: 3, command: Some(vec![0x01]) }), l.get(1)?);
assert_eq!(None, l.get(2)?);

Expand All @@ -237,7 +234,7 @@ mod tests {
#[test]
fn append_none() -> Result<()> {
let (mut l, _) = setup()?;
assert_eq!(Entry { index: 1, term: 3, command: None }, l.append(3, None)?);
assert_eq!(1, l.append(3, None)?);
assert_eq!(Some(Entry { index: 1, term: 3, command: None }), l.get(1)?);
Ok(())
}
Expand Down
4 changes: 2 additions & 2 deletions src/raft/node/leader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ impl RoleNode<Leader> {

/// Appends an entry to the log and replicates it to peers.
pub fn append(&mut self, command: Option<Vec<u8>>) -> Result<u64> {
let entry = self.log.append(self.term, command)?;
let index = self.log.append(self.term, command)?;
for peer in self.peers.iter() {
self.replicate(peer)?;
}
Ok(entry.index)
Ok(index)
}

/// Commits any pending log entries.
Expand Down

0 comments on commit 0d6795b

Please sign in to comment.