Skip to content

Commit 0b719d6

Browse files
committed
refactor: LeaderState::apply_entry_to_state_machine use log_id to replace index
1 parent eff11d6 commit 0b719d6

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

async-raft/src/core/client.rs

+11-6
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ impl<'a, D: AppData, R: AppDataResponse, N: RaftNetwork<D>, S: RaftStorage<D, R>
311311
ClientOrInternalResponseTx::Client(tx) => {
312312
match &req.entry.payload {
313313
EntryPayload::Normal(inner) => {
314-
match self.apply_entry_to_state_machine(&req.entry.log_id.index, &inner.data).await {
314+
match self.apply_entry_to_state_machine(&req.entry.log_id, &inner.data).await {
315315
Ok(data) => {
316316
let _ = tx.send(Ok(ClientWriteResponse {
317317
index: req.entry.log_id.index,
@@ -346,23 +346,28 @@ impl<'a, D: AppData, R: AppDataResponse, N: RaftNetwork<D>, S: RaftStorage<D, R>
346346

347347
/// Apply the given log entry to the state machine.
348348
#[tracing::instrument(level = "trace", skip(self, entry))]
349-
pub(super) async fn apply_entry_to_state_machine(&mut self, index: &u64, entry: &D) -> RaftResult<R> {
349+
pub(super) async fn apply_entry_to_state_machine(&mut self, log_id: &LogId, entry: &D) -> RaftResult<R> {
350350
// First, we just ensure that we apply any outstanding up to, but not including, the index
351351
// of the given entry. We need to be able to return the data response from applying this
352352
// entry to the state machine.
353353
//
354354
// Note that this would only ever happen if a node had unapplied logs from before becoming leader.
355+
356+
let index = log_id.index;
357+
355358
let expected_next_index = self.core.last_applied + 1;
356-
if index != &expected_next_index {
359+
if index != expected_next_index {
357360
let entries = self
358361
.core
359362
.storage
360-
.get_log_entries(expected_next_index, *index)
363+
.get_log_entries(expected_next_index, index)
361364
.await
362365
.map_err(|err| self.core.map_fatal_storage_error(err))?;
366+
363367
if let Some(entry) = entries.last() {
364368
self.core.last_applied = entry.log_id.index;
365369
}
370+
366371
let data_entries: Vec<_> = entries
367372
.iter()
368373
.filter_map(|entry| match &entry.payload {
@@ -388,7 +393,7 @@ impl<'a, D: AppData, R: AppDataResponse, N: RaftNetwork<D>, S: RaftStorage<D, R>
388393
}
389394
}
390395
// Apply this entry to the state machine and return its data response.
391-
let res = self.core.storage.apply_entry_to_state_machine(index, entry).await.map_err(|err| {
396+
let res = self.core.storage.apply_entry_to_state_machine(&index, entry).await.map_err(|err| {
392397
if err.downcast_ref::<S::ShutdownError>().is_some() {
393398
// If this is an instance of the storage impl's shutdown error, then trigger shutdown.
394399
self.core.map_fatal_storage_error(err)
@@ -397,7 +402,7 @@ impl<'a, D: AppData, R: AppDataResponse, N: RaftNetwork<D>, S: RaftStorage<D, R>
397402
RaftError::RaftStorage(err)
398403
}
399404
});
400-
self.core.last_applied = *index;
405+
self.core.last_applied = index;
401406
self.leader_report_metrics();
402407
res
403408
}

0 commit comments

Comments
 (0)