Skip to content

Commit 8782c2b

Browse files
committed
raft: minor cleanups
1 parent 0ce1fb3 commit 8782c2b

File tree

3 files changed

+190
-156
lines changed

3 files changed

+190
-156
lines changed

src/raft/log.rs

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::ops::Bound;
1+
use std::ops::{Bound, RangeBounds};
22

33
use serde::{Deserialize, Serialize};
44

@@ -162,14 +162,14 @@ impl Log {
162162
}
163163

164164
/// Returns the current term (0 if none) and vote.
165-
pub fn get_term(&self) -> (Term, Option<NodeID>) {
165+
pub fn get_term_vote(&self) -> (Term, Option<NodeID>) {
166166
(self.term, self.vote)
167167
}
168168

169169
/// Stores the current term and cast vote (if any). Enforces that the term
170170
/// does not regress, and that we only vote for one node in a term. append()
171171
/// will use this term, and splice() can't write entries beyond it.
172-
pub fn set_term(&mut self, term: Term, vote: Option<NodeID>) -> Result<()> {
172+
pub fn set_term_vote(&mut self, term: Term, vote: Option<NodeID>) -> Result<()> {
173173
assert!(term > 0, "can't set term 0");
174174
assert!(term >= self.term, "term regression {} → {}", self.term, term);
175175
assert!(term > self.term || self.vote.is_none() || vote == self.vote, "can't change vote");
@@ -240,7 +240,7 @@ impl Log {
240240
}
241241

242242
/// Returns an iterator over log entries in the given index range.
243-
pub fn scan(&mut self, range: impl std::ops::RangeBounds<Index>) -> Iterator {
243+
pub fn scan(&mut self, range: impl RangeBounds<Index>) -> Iterator {
244244
let from = match range.start_bound() {
245245
Bound::Excluded(&index) => Bound::Excluded(Key::Entry(index).encode()),
246246
Bound::Included(&index) => Bound::Included(Key::Entry(index).encode()),
@@ -266,7 +266,7 @@ impl Log {
266266
self.scan(applied_index + 1..=self.commit_index)
267267
}
268268

269-
/// Splices a set of entries into the log and flushes it to disk. New
269+
/// Splices a set of entries into the log and flushes it to disk. New
270270
/// indexes will be appended. Overlapping indexes with the same term must be
271271
/// equal and will be ignored. Overlapping indexes with different terms will
272272
/// truncate the existing log at the first conflict and then splice the new
@@ -281,15 +281,15 @@ impl Log {
281281
};
282282

283283
// Check that the entries are well-formed.
284-
if first.index == 0 || first.term == 0 {
285-
panic!("spliced entry has index or term 0");
286-
}
287-
if !entries.windows(2).all(|w| w[0].index + 1 == w[1].index) {
288-
panic!("spliced entries are not contiguous");
289-
}
290-
if !entries.windows(2).all(|w| w[0].term <= w[1].term) {
291-
panic!("spliced entries have term regression");
292-
}
284+
assert!(first.index > 0 && first.term > 0, "spliced entry has index or term 0",);
285+
assert!(
286+
entries.windows(2).all(|w| w[0].index + 1 == w[1].index),
287+
"spliced entries are not contiguous"
288+
);
289+
assert!(
290+
entries.windows(2).all(|w| w[0].term <= w[1].term),
291+
"spliced entries have term regression",
292+
);
293293

294294
// Check that the entries connect to the existing log (if any), and that the
295295
// term doesn't regress.
@@ -372,7 +372,6 @@ impl std::iter::Iterator for Iterator<'_> {
372372
mod tests {
373373
use std::error::Error;
374374
use std::fmt::Write as _;
375-
use std::ops::RangeBounds;
376375
use std::result::Result;
377376

378377
use crossbeam::channel::Receiver;
@@ -503,7 +502,7 @@ mod tests {
503502
// get_term
504503
"get_term" => {
505504
command.consume_args().reject_rest()?;
506-
let (term, vote) = self.log.get_term();
505+
let (term, vote) = self.log.get_term_vote();
507506
let vote = vote.map(|v| v.to_string()).unwrap_or("None".to_string());
508507
writeln!(output, "term={term} vote={vote}")?;
509508
}
@@ -566,7 +565,7 @@ mod tests {
566565
let term = args.next_pos().ok_or("term not given")?.parse()?;
567566
let vote = args.next_pos().map(|a| a.parse()).transpose()?;
568567
args.reject_rest()?;
569-
self.log.set_term(term, vote)?;
568+
self.log.set_term_vote(term, vote)?;
570569
}
571570

572571
// splice [INDEX@TERM=COMMAND...]
@@ -593,7 +592,7 @@ mod tests {
593592
let mut args = command.consume_args();
594593
let engine = args.lookup_parse("engine")?.unwrap_or(false);
595594
args.reject_rest()?;
596-
let (term, vote) = self.log.get_term();
595+
let (term, vote) = self.log.get_term_vote();
597596
let (last_index, last_term) = self.log.get_last_index();
598597
let (commit_index, commit_term) = self.log.get_commit_index();
599598
let vote = vote.map(|id| id.to_string()).unwrap_or("None".to_string());

0 commit comments

Comments
 (0)