Skip to content

Commit

Permalink
fix: first_known_log_id() should returns the min one in log or in sta…
Browse files Browse the repository at this point in the history
…te machine
  • Loading branch information
drmingdrmer committed Dec 29, 2021
1 parent c3139d3 commit b390356
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
5 changes: 3 additions & 2 deletions memstore/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,11 +305,12 @@ impl RaftStorage<ClientRequest, ClientResponse> for MemStore {

async fn first_known_log_id(&self) -> Result<LogId, StorageError> {
let first = self.first_id_in_log().await?;
let (last_applied, _) = self.last_applied_state().await?;

if let Some(x) = first {
return Ok(x);
return Ok(std::cmp::min(x, last_applied));
}

let (last_applied, _) = self.last_applied_state().await?;
Ok(last_applied)
}

Expand Down
28 changes: 18 additions & 10 deletions memstore/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ where
let log_id = store.first_known_log_id().await?;
assert_eq!(LogId::new(0, 0), log_id, "store initialized with a log at 0");

tracing::info!("--- only logs");
tracing::info!("--- returns the min id");
{
store
.append_to_log(&[
Expand All @@ -485,26 +485,34 @@ where

// NOTE: it assumes non applied logs always exist.
let log_id = store.first_known_log_id().await?;
assert_eq!(LogId::new(1, 2), log_id);
}
assert_eq!(LogId::new(0, 0), log_id, "last_applied is 0-0");

tracing::info!("--- return applied_log_id only when there is no log at all");
{
store
.apply_to_state_machine(&[&Entry {
log_id: LogId { term: 1, index: 1 },
payload: EntryPayload::Blank,
}])
.await?;
let log_id = store.first_known_log_id().await?;
assert_eq!(LogId::new(1, 1), log_id);

// NOTE: it assumes non applied logs always exist.
store
.apply_to_state_machine(&[&Entry {
log_id: LogId { term: 1, index: 2 },
payload: EntryPayload::Blank,
}])
.await?;
let log_id = store.first_known_log_id().await?;
assert_eq!(LogId { term: 1, index: 2 }, log_id);
assert_eq!(LogId::new(1, 2), log_id);

// When there is no logs, return applied_log_id
store.delete_logs_from(0..3).await?;
store
.apply_to_state_machine(&[&Entry {
log_id: LogId { term: 1, index: 3 },
payload: EntryPayload::Blank,
}])
.await?;
let log_id = store.first_known_log_id().await?;
assert_eq!(LogId { term: 1, index: 1 }, log_id);
assert_eq!(LogId::new(1, 2), log_id, "least id is in log");
}

Ok(())
Expand Down

0 comments on commit b390356

Please sign in to comment.