From 0d6795b37870c124ac73de44c76bbeeaf51999f7 Mon Sep 17 00:00:00 2001 From: Erik Grinaker Date: Tue, 14 Nov 2023 20:08:52 +0100 Subject: [PATCH] Return log index from `raft::Log::append()` --- src/raft/log.rs | 13 +++++-------- src/raft/node/leader.rs | 4 ++-- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/src/raft/log.rs b/src/raft/log.rs index d467a4f35..ab136aa97 100644 --- a/src/raft/log.rs +++ b/src/raft/log.rs @@ -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>) -> Result { + /// Appends a command to the log, returning the log index. + pub fn append(&mut self, term: Term, command: Option>) -> Result { 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. @@ -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)?); @@ -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(()) } diff --git a/src/raft/node/leader.rs b/src/raft/node/leader.rs index df118601a..19dafc64e 100644 --- a/src/raft/node/leader.rs +++ b/src/raft/node/leader.rs @@ -44,11 +44,11 @@ impl RoleNode { /// Appends an entry to the log and replicates it to peers. pub fn append(&mut self, command: Option>) -> Result { - 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.