Skip to content

Commit

Permalink
change: InitialState: last_log_{term,index} into last_log: LogId
Browse files Browse the repository at this point in the history
  • Loading branch information
drmingdrmer committed Jul 9, 2021
1 parent d0819f1 commit 9e4fb64
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 14 deletions.
3 changes: 1 addition & 2 deletions async-raft/src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,7 @@ impl<D: AppData, R: AppDataResponse, N: RaftNetwork<D>, S: RaftStorage<D, R>> Ra
async fn main(mut self) -> RaftResult<()> {
tracing::trace!("raft node is initializing");
let state = self.storage.get_initial_state().await.map_err(|err| self.map_fatal_storage_error(err))?;
self.last_log.index = state.last_log_index;
self.last_log.term = state.last_log_term;
self.last_log = state.last_log;
self.current_term = state.hard_state.current_term;
self.voted_for = state.hard_state.voted_for;
self.membership = state.membership;
Expand Down
10 changes: 4 additions & 6 deletions async-raft/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,9 @@ pub struct HardState {
/// A struct used to represent the initial state which a Raft node needs when first starting.
#[derive(Clone, Debug)]
pub struct InitialState {
/// The index of the last entry.
pub last_log_index: u64,
/// The term of the last log entry.
pub last_log_term: u64,
/// The last entry.
pub last_log: LogId,

/// The index of the last log applied to the state machine.
pub last_applied_log: u64,
/// The saved hard state of the node.
Expand All @@ -69,8 +68,7 @@ impl InitialState {
/// The ID of the Raft node.
pub fn new_initial(id: NodeId) -> Self {
Self {
last_log_index: 0,
last_log_term: 0,
last_log: LogId { term: 0, index: 0 },
last_applied_log: 0,
hard_state: HardState {
current_term: 0,
Expand Down
7 changes: 5 additions & 2 deletions memstore/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use async_raft::storage::HardState;
use async_raft::storage::InitialState;
use async_raft::AppData;
use async_raft::AppDataResponse;
use async_raft::LogId;
use async_raft::NodeId;
use async_raft::RaftStorage;
use async_raft::SnapshotId;
Expand Down Expand Up @@ -193,8 +194,10 @@ impl RaftStorage<ClientRequest, ClientResponse> for MemStore {
};
let last_applied_log = sm.last_applied_log;
Ok(InitialState {
last_log_index,
last_log_term,
last_log: LogId {
term: last_log_term,
index: last_log_index,
},
last_applied_log,
hard_state: inner.clone(),
membership,
Expand Down
8 changes: 4 additions & 4 deletions memstore/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ async fn test_get_initial_state_default() -> Result<()> {

let initial = store.get_initial_state().await?;

assert_eq!(initial.last_log_index, 0, "unexpected default value for last log index");
assert_eq!(initial.last_log_term, 0, "unexpected default value for last log term");
assert_eq!(initial.last_log.index, 0, "unexpected default value for last log index");
assert_eq!(initial.last_log.term, 0, "unexpected default value for last log term");
assert_eq!(initial.last_applied_log, 0, "unexpected value for last applied log");
assert_eq!(
initial.hard_state, expected_hs,
Expand Down Expand Up @@ -101,8 +101,8 @@ async fn test_get_initial_state_with_previous_state() -> Result<()> {

let initial = store.get_initial_state().await?;

assert_eq!(initial.last_log_index, 1, "unexpected default value for last log index");
assert_eq!(initial.last_log_term, 1, "unexpected default value for last log term");
assert_eq!(initial.last_log.index, 1, "unexpected default value for last log index");
assert_eq!(initial.last_log.term, 1, "unexpected default value for last log term");
assert_eq!(initial.last_applied_log, 1, "unexpected value for last applied log");
assert_eq!(initial.hard_state, hs, "unexpected value for default hard state");
Ok(())
Expand Down

0 comments on commit 9e4fb64

Please sign in to comment.