Skip to content

Commit

Permalink
Chore: replace usage of ProgressEntry::matching with `ProgressEntry…
Browse files Browse the repository at this point in the history
…::matching()`
  • Loading branch information
drmingdrmer committed Jan 10, 2025
1 parent d7698b8 commit bcc3af2
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 16 deletions.
4 changes: 2 additions & 2 deletions openraft/src/core/raft_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ where

let rpc = AppendEntriesRequest {
vote: my_vote.clone(),
prev_log_id: progress.matching.clone(),
prev_log_id: progress.matching().cloned(),
entries: vec![],
leader_commit: self.engine.state.committed().cloned(),
};
Expand Down Expand Up @@ -842,7 +842,7 @@ where
session_id,
self.config.clone(),
self.engine.state.committed().cloned(),
progress_entry.matching,
progress_entry.matching().cloned(),
network,
snapshot_network,
self.log_store.get_log_reader().await,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ fn test_leader_append_membership_for_leader() -> anyhow::Result<()> {
);

assert!(
eng.leader.as_ref().unwrap().progress.get(&4).matching.is_none(),
eng.leader.as_ref().unwrap().progress.get(&4).matching().is_none(),
"exists, but it is a None"
);

Expand Down
4 changes: 2 additions & 2 deletions openraft/src/engine/handler/replication_handler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,11 +403,11 @@ where C: RaftTypeConfig
// The leader may not be in membership anymore
if let Some(prog_entry) = self.leader.progress.get_mut(&id) {
tracing::debug!(
self_matching = display(prog_entry.matching.display()),
self_matching = display(prog_entry.matching().display()),
"update progress"
);

if prog_entry.matching >= upto {
if prog_entry.matching() >= upto.as_ref() {
return;
}
// TODO: It should be self.state.last_log_id() but None is ok.
Expand Down
12 changes: 8 additions & 4 deletions openraft/src/progress/entry/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ where C: RaftTypeConfig
Updater::new(engine_config, self)
}

pub(crate) fn matching(&self) -> Option<&LogIdOf<C>> {
self.matching.as_ref()
}

/// Return if a range of log id `..=log_id` is inflight sending.
///
/// `prev_log_id` is never inflight.
Expand Down Expand Up @@ -138,7 +142,7 @@ where C: RaftTypeConfig

// Replicate by logs.
// Run a binary search to find the matching log id, if matching log id is not determined.
let mut start = Self::calc_mid(self.matching.next_index(), self.searching_end);
let mut start = Self::calc_mid(self.matching().next_index(), self.searching_end);
if start < purge_upto_next {
start = purge_upto_next;
}
Expand All @@ -163,7 +167,7 @@ where C: RaftTypeConfig
/// The returned range is left close and right close.
#[allow(dead_code)]
pub(crate) fn sending_start(&self) -> (u64, u64) {
let mid = Self::calc_mid(self.matching.next_index(), self.searching_end);
let mid = Self::calc_mid(self.matching().next_index(), self.searching_end);
(mid, self.searching_end)
}

Expand All @@ -190,7 +194,7 @@ where C: RaftTypeConfig
write!(
f,
"{{[{}, {}), inflight:{}}}",
self.matching.display(),
self.matching().display(),
self.searching_end,
self.inflight
)
Expand All @@ -201,7 +205,7 @@ impl<C> Validate for ProgressEntry<C>
where C: RaftTypeConfig
{
fn validate(&self) -> Result<(), Box<dyn Error>> {
validit::less_equal!(self.matching.next_index(), self.searching_end);
validit::less_equal!(self.matching().next_index(), self.searching_end);

self.inflight.validate()?;

Expand Down
4 changes: 2 additions & 2 deletions openraft/src/progress/entry/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ fn test_update_matching() -> anyhow::Result<()> {
pe.inflight = inflight_logs(5, 10);
pe.new_updater(&engine_config).update_matching(Some(log_id(6)));
assert_eq!(inflight_logs(6, 10), pe.inflight);
assert_eq!(Some(log_id(6)), pe.matching);
assert_eq!(Some(&log_id(6)), pe.matching());
assert_eq!(20, pe.searching_end);

pe.new_updater(&engine_config).update_matching(Some(log_id(10)));
assert_eq!(Inflight::None, pe.inflight);
assert_eq!(Some(log_id(10)), pe.matching);
assert_eq!(Some(&log_id(10)), pe.matching());
assert_eq!(20, pe.searching_end);
}

Expand Down
10 changes: 5 additions & 5 deletions openraft/src/progress/entry/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,25 +52,25 @@ where C: RaftTypeConfig
let allow_reset = self.entry.allow_log_reversion || self.engine_config.allow_log_reversion;

if allow_reset {
if conflict < self.entry.matching.next_index() {
if conflict < self.entry.matching().next_index() {
tracing::warn!(
"conflict {} < last matching {}: \
follower log is reverted; \
with 'allow_log_reversion' enabled, this is allowed.",
conflict,
self.entry.matching.display(),
self.entry.matching().display(),
);

self.entry.matching = None;
self.entry.allow_log_reversion = false;
}
} else {
debug_assert!(
conflict >= self.entry.matching.next_index(),
conflict >= self.entry.matching().next_index(),
"follower log reversion is not allowed \
without `allow_log_reversion` enabled; \
matching: {}; conflict: {}",
self.entry.matching.display(),
self.entry.matching().display(),
conflict
);
}
Expand All @@ -88,7 +88,7 @@ where C: RaftTypeConfig
debug_assert!(matching >= self.entry.matching);
self.entry.matching = matching;

let matching_next = self.entry.matching.next_index();
let matching_next = self.entry.matching().next_index();
self.entry.searching_end = std::cmp::max(self.entry.searching_end, matching_next);
}
}

0 comments on commit bcc3af2

Please sign in to comment.