Skip to content

Commit b390356

Browse files
committed
fix: first_known_log_id() should returns the min one in log or in state machine
1 parent c3139d3 commit b390356

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-12
lines changed

memstore/src/lib.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -305,11 +305,12 @@ impl RaftStorage<ClientRequest, ClientResponse> for MemStore {
305305

306306
async fn first_known_log_id(&self) -> Result<LogId, StorageError> {
307307
let first = self.first_id_in_log().await?;
308+
let (last_applied, _) = self.last_applied_state().await?;
309+
308310
if let Some(x) = first {
309-
return Ok(x);
311+
return Ok(std::cmp::min(x, last_applied));
310312
}
311313

312-
let (last_applied, _) = self.last_applied_state().await?;
313314
Ok(last_applied)
314315
}
315316

memstore/src/test.rs

+18-10
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ where
466466
let log_id = store.first_known_log_id().await?;
467467
assert_eq!(LogId::new(0, 0), log_id, "store initialized with a log at 0");
468468

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

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

491-
tracing::info!("--- return applied_log_id only when there is no log at all");
492-
{
493490
store
494491
.apply_to_state_machine(&[&Entry {
495492
log_id: LogId { term: 1, index: 1 },
496493
payload: EntryPayload::Blank,
497494
}])
498495
.await?;
496+
let log_id = store.first_known_log_id().await?;
497+
assert_eq!(LogId::new(1, 1), log_id);
499498

500-
// NOTE: it assumes non applied logs always exist.
499+
store
500+
.apply_to_state_machine(&[&Entry {
501+
log_id: LogId { term: 1, index: 2 },
502+
payload: EntryPayload::Blank,
503+
}])
504+
.await?;
501505
let log_id = store.first_known_log_id().await?;
502-
assert_eq!(LogId { term: 1, index: 2 }, log_id);
506+
assert_eq!(LogId::new(1, 2), log_id);
503507

504-
// When there is no logs, return applied_log_id
505-
store.delete_logs_from(0..3).await?;
508+
store
509+
.apply_to_state_machine(&[&Entry {
510+
log_id: LogId { term: 1, index: 3 },
511+
payload: EntryPayload::Blank,
512+
}])
513+
.await?;
506514
let log_id = store.first_known_log_id().await?;
507-
assert_eq!(LogId { term: 1, index: 1 }, log_id);
515+
assert_eq!(LogId::new(1, 2), log_id, "least id is in log");
508516
}
509517

510518
Ok(())

0 commit comments

Comments
 (0)