Skip to content

Commit

Permalink
Change: RaftLogReaderExt::get_log_id() should not return last-purged-id
Browse files Browse the repository at this point in the history
`get_log_id()` should only return present log id,
and should not be responsible to return id of an already purged log
entry, which introduce unnecessary complexity.

Upgrade tip:

An RaftStorage implementation should have already maintained the
last-purge-log-id. Avoid getting it via `RaftLogReaderExt::get_log_id()`.
  • Loading branch information
drmingdrmer committed Jul 2, 2023
1 parent 6330a25 commit 8a81df5
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 11 deletions.
7 changes: 0 additions & 7 deletions openraft/src/storage/log_store_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use macros::add_async_trait;

use crate::defensive::check_range_matches_entries;
use crate::LogId;
use crate::LogIdOptionExt;
use crate::RaftLogId;
use crate::RaftLogReader;
use crate::RaftTypeConfig;
Expand Down Expand Up @@ -40,12 +39,6 @@ where C: RaftTypeConfig

/// Get the log id of the entry at `index`.
async fn get_log_id(&mut self, log_index: u64) -> Result<LogId<C::NodeId>, StorageError<C::NodeId>> {
let st = self.get_log_state().await?;

if Some(log_index) == st.last_purged_log_id.index() {
return Ok(st.last_purged_log_id.unwrap());
}

let entries = self.get_log_entries(log_index..=log_index).await?;

Ok(*entries[0].get_log_id())
Expand Down
11 changes: 7 additions & 4 deletions openraft/src/testing/suite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -708,15 +708,18 @@ where
let res = store.get_log_id(0).await;
assert!(res.is_err());

let res = store.get_log_id(11).await;
let res = store.get_log_id(3).await;
assert!(res.is_err());

let res = store.get_log_id(3).await?;
assert_eq!(log_id_0(1, 3), res);

let res = store.get_log_id(4).await?;
assert_eq!(log_id_0(1, 4), res);

let res = store.get_log_id(10).await?;
assert_eq!(log_id_0(1, 10), res);

let res = store.get_log_id(11).await;
assert!(res.is_err());

Ok(())
}

Expand Down

0 comments on commit 8a81df5

Please sign in to comment.