Skip to content

Commit

Permalink
Change: rename InitialState to RaftState
Browse files Browse the repository at this point in the history
RaftState is not only used when starting up.
It is used as long as server runs.
  • Loading branch information
drmingdrmer committed Apr 17, 2022
1 parent 30b485b commit ca8a09c
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 42 deletions.
6 changes: 3 additions & 3 deletions openraft/src/engine/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::error::InitializeError;
use crate::error::NotAMembershipEntry;
use crate::error::NotAllowed;
use crate::error::NotInMembers;
use crate::storage::InitialState;
use crate::raft_state::RaftState;
use crate::EffectiveMembership;
use crate::LogId;
use crate::LogIdOptionExt;
Expand Down Expand Up @@ -40,7 +40,7 @@ pub(crate) struct Engine<NID: NodeId> {
pub(crate) single_node_cluster: BTreeSet<NID>,

/// The state of this raft node.
pub(crate) state: InitialState<NID>,
pub(crate) state: RaftState<NID>,

/// Tracks what kind of metrics changed
pub(crate) metrics_flags: MetricsChangeFlags,
Expand Down Expand Up @@ -109,7 +109,7 @@ pub(crate) enum Command<NID: NodeId> {
}

impl<NID: NodeId> Engine<NID> {
pub(crate) fn new(id: NID, init_state: &InitialState<NID>) -> Self {
pub(crate) fn new(id: NID, init_state: &RaftState<NID>) -> Self {
Self {
id,
single_node_cluster: btreeset! {id},
Expand Down
2 changes: 2 additions & 0 deletions openraft/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub mod error;
pub mod metrics;
pub mod network;
pub mod raft;
mod raft_state;
mod runtime;
pub mod storage;
pub mod testing;
Expand Down Expand Up @@ -58,6 +59,7 @@ pub use crate::node::NodeId;
pub use crate::raft::ChangeMembers;
pub use crate::raft::Raft;
pub use crate::raft::RaftTypeConfig;
pub use crate::raft_state::RaftState;
pub use crate::raft_types::LogId;
pub use crate::raft_types::LogIdOptionExt;
pub(crate) use crate::raft_types::MetricsChangeFlags;
Expand Down
42 changes: 42 additions & 0 deletions openraft/src/raft_state.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use std::sync::Arc;

use crate::EffectiveMembership;
use crate::LogId;
use crate::NodeId;
use crate::ServerState;
use crate::Vote;

/// A struct used to represent the raft state which a Raft node needs.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct RaftState<NID: NodeId> {
/// The vote state of this node.
pub vote: Vote<NID>,

/// The greatest log id that has been purged after being applied to state machine.
/// The range of log entries that exist in storage is `(last_purged_log_id, last_log_id]`,
/// left open and right close.
///
/// `last_purged_log_id == last_log_id` means there is no log entry in the storage.
pub last_purged_log_id: Option<LogId<NID>>,

/// The id of the last log entry.
pub last_log_id: Option<LogId<NID>>,

/// The LogId of the last log applied to the state machine.
pub last_applied: Option<LogId<NID>>,

/// The latest cluster membership configuration found, in log or in state machine.
pub effective_membership: Arc<EffectiveMembership<NID>>,

// -- volatile fields: they are not persisted.
/// The log id of the last known committed entry.
///
/// - Committed means: a log that is replicated to a quorum of the cluster and it is of the term of the leader.
///
/// - A quorum could be a uniform quorum or joint quorum.
///
/// - `committed` in raft is volatile and will not be persisted.
pub committed: Option<LogId<NID>>,

pub server_state: ServerState,
}
40 changes: 3 additions & 37 deletions openraft/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use tokio::io::AsyncWrite;

use crate::core::EffectiveMembership;
use crate::defensive::check_range_matches_entries;
use crate::raft_state::RaftState;
use crate::raft_types::SnapshotId;
use crate::raft_types::StateMachineChanges;
use crate::Entry;
Expand All @@ -21,7 +22,6 @@ use crate::LogId;
use crate::LogIdOptionExt;
use crate::NodeId;
use crate::RaftTypeConfig;
use crate::ServerState;
use crate::StorageError;
use crate::Vote;

Expand Down Expand Up @@ -50,40 +50,6 @@ where
pub snapshot: Box<S>,
}

/// A struct used to represent the initial state which a Raft node needs when first starting.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct InitialState<NID: NodeId> {
/// The vote state of this node.
pub vote: Vote<NID>,

/// The greatest log id that has been purged after being applied to state machine.
/// The range of log entries that exist in storage is `(last_purged_log_id, last_log_id]`,
/// left open and right close.
///
/// `last_purged_log_id == last_log_id` means there is no log entry in the storage.
pub last_purged_log_id: Option<LogId<NID>>,

/// The id of the last log entry.
pub last_log_id: Option<LogId<NID>>,

/// The log id of the last known committed entry.
///
/// - Committed means: a log that is replicated to a quorum of the cluster and it is of the term of the leader.
///
/// - A quorum could be a uniform quorum or joint quorum.
///
/// - `committed` in raft is volatile and will not be persisted.
pub committed: Option<LogId<NID>>,

/// The LogId of the last log applied to the state machine.
pub last_applied: Option<LogId<NID>>,

/// The latest cluster membership configuration found, in log or in state machine.
pub effective_membership: Arc<EffectiveMembership<NID>>,

pub server_state: ServerState,
}

/// The state about logs.
///
/// Invariance: last_purged_log_id <= last_applied <= last_log_id
Expand Down Expand Up @@ -261,7 +227,7 @@ where C: RaftTypeConfig
///
/// When the Raft node is first started, it will call this interface to fetch the last known state from stable
/// storage.
async fn get_initial_state(&mut self) -> Result<InitialState<C::NodeId>, StorageError<C::NodeId>> {
async fn get_initial_state(&mut self) -> Result<RaftState<C::NodeId>, StorageError<C::NodeId>> {
let vote = self.read_vote().await?;
let st = self.get_log_state().await?;
let mut last_purged_log_id = st.last_purged_log_id;
Expand All @@ -276,7 +242,7 @@ where C: RaftTypeConfig
last_purged_log_id = last_applied;
}

Ok(InitialState {
Ok(RaftState {
last_log_id,
last_purged_log_id,
last_applied,
Expand Down
4 changes: 2 additions & 2 deletions openraft/src/testing/suite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::option::Option::None;

use maplit::btreeset;

use crate::storage::InitialState;
use crate::raft_state::RaftState;
use crate::storage::LogState;
use crate::testing::DefensiveStoreBuilder;
use crate::testing::StoreBuilder;
Expand Down Expand Up @@ -267,7 +267,7 @@ where
let mut store = builder.build().await;

let initial = store.get_initial_state().await?;
assert_eq!(InitialState::default(), initial, "uninitialized state");
assert_eq!(RaftState::default(), initial, "uninitialized state");
Ok(())
}

Expand Down

0 comments on commit ca8a09c

Please sign in to comment.