Skip to content

Commit

Permalink
Change: Consolidate RaftState type parameters into C
Browse files Browse the repository at this point in the history
The `RaftState` type, along with related metric types, previously used
separate type parameters (`NID`, `N`, `I`) which have now been replaced
with a single generic parameter `C` bound by `RaftTypeConfig`. This
modification simplifies the type signatures and usage.

Upgrade tip:

To adapt to this change, update the usage of `RaftState` type parameters
from separate `NID`, `N`, `I` to the single generic `C` constrained by
`RaftTypeConfig`:

```rust
RaftState<NID, N, I> --> RaftState<C>
```
  • Loading branch information
drmingdrmer committed Mar 20, 2024
1 parent b3c1f46 commit 01fcb35
Show file tree
Hide file tree
Showing 19 changed files with 102 additions and 135 deletions.
7 changes: 2 additions & 5 deletions openraft/src/engine/engine_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ where C: RaftTypeConfig
pub(crate) config: EngineConfig<C::NodeId>,

/// The state of this raft node.
pub(crate) state: Valid<RaftState<C::NodeId, C::Node, InstantOf<C>>>,
pub(crate) state: Valid<RaftState<C>>,

// TODO: add a Voting state as a container.
/// Whether a greater log id is seen during election.
Expand All @@ -86,10 +86,7 @@ where C: RaftTypeConfig
impl<C> Engine<C>
where C: RaftTypeConfig
{
pub(crate) fn new(
init_state: RaftState<C::NodeId, C::Node, InstantOf<C>>,
config: EngineConfig<C::NodeId>,
) -> Self {
pub(crate) fn new(init_state: RaftState<C>, config: EngineConfig<C::NodeId>) -> Self {
Self {
config,
state: Valid::new(init_state),
Expand Down
3 changes: 1 addition & 2 deletions openraft/src/engine/handler/following_handler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use crate::engine::EngineOutput;
use crate::entry::RaftPayload;
use crate::error::RejectAppendEntries;
use crate::raft_state::LogStateReader;
use crate::type_config::alias::InstantOf;
use crate::EffectiveMembership;
use crate::LogId;
use crate::LogIdOptionExt;
Expand All @@ -37,7 +36,7 @@ pub(crate) struct FollowingHandler<'x, C>
where C: RaftTypeConfig
{
pub(crate) config: &'x mut EngineConfig<C::NodeId>,
pub(crate) state: &'x mut RaftState<C::NodeId, C::Node, InstantOf<C>>,
pub(crate) state: &'x mut RaftState<C>,
pub(crate) output: &'x mut EngineOutput<C>,
}

Expand Down
2 changes: 1 addition & 1 deletion openraft/src/engine/handler/leader_handler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ where C: RaftTypeConfig
{
pub(crate) config: &'x mut EngineConfig<C::NodeId>,
pub(crate) leader: &'x mut Leading<C::NodeId, LeaderQuorumSet<C::NodeId>, InstantOf<C>>,
pub(crate) state: &'x mut RaftState<C::NodeId, C::Node, InstantOf<C>>,
pub(crate) state: &'x mut RaftState<C>,
pub(crate) output: &'x mut EngineOutput<C>,
}

Expand Down
3 changes: 1 addition & 2 deletions openraft/src/engine/handler/log_handler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use crate::engine::EngineConfig;
use crate::engine::EngineOutput;
use crate::raft_state::LogStateReader;
use crate::summary::MessageSummary;
use crate::type_config::alias::InstantOf;
use crate::LogId;
use crate::LogIdOptionExt;
use crate::RaftState;
Expand All @@ -17,7 +16,7 @@ pub(crate) struct LogHandler<'x, C>
where C: RaftTypeConfig
{
pub(crate) config: &'x mut EngineConfig<C::NodeId>,
pub(crate) state: &'x mut RaftState<C::NodeId, C::Node, InstantOf<C>>,
pub(crate) state: &'x mut RaftState<C>,
pub(crate) output: &'x mut EngineOutput<C>,
}

Expand Down
2 changes: 1 addition & 1 deletion openraft/src/engine/handler/replication_handler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ where C: RaftTypeConfig
{
pub(crate) config: &'x mut EngineConfig<C::NodeId>,
pub(crate) leader: &'x mut Leading<C::NodeId, LeaderQuorumSet<C::NodeId>, InstantOf<C>>,
pub(crate) state: &'x mut RaftState<C::NodeId, C::Node, InstantOf<C>>,
pub(crate) state: &'x mut RaftState<C>,
pub(crate) output: &'x mut EngineOutput<C>,
}

Expand Down
3 changes: 1 addition & 2 deletions openraft/src/engine/handler/server_state_handler/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::engine::Command;
use crate::engine::EngineConfig;
use crate::engine::EngineOutput;
use crate::type_config::alias::InstantOf;
use crate::RaftState;
use crate::RaftTypeConfig;
use crate::ServerState;
Expand All @@ -13,7 +12,7 @@ pub(crate) struct ServerStateHandler<'st, C>
where C: RaftTypeConfig
{
pub(crate) config: &'st EngineConfig<C::NodeId>,
pub(crate) state: &'st mut RaftState<C::NodeId, C::Node, InstantOf<C>>,
pub(crate) state: &'st mut RaftState<C>,
pub(crate) output: &'st mut EngineOutput<C>,
}

Expand Down
3 changes: 1 addition & 2 deletions openraft/src/engine/handler/snapshot_handler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use crate::engine::Command;
use crate::engine::EngineOutput;
use crate::raft_state::LogStateReader;
use crate::summary::MessageSummary;
use crate::type_config::alias::InstantOf;
use crate::RaftState;
use crate::RaftTypeConfig;
use crate::SnapshotMeta;
Expand All @@ -17,7 +16,7 @@ use crate::SnapshotMeta;
pub(crate) struct SnapshotHandler<'st, 'out, C>
where C: RaftTypeConfig
{
pub(crate) state: &'st mut RaftState<C::NodeId, C::Node, InstantOf<C>>,
pub(crate) state: &'st mut RaftState<C>,
pub(crate) output: &'out mut EngineOutput<C>,
}

Expand Down
4 changes: 2 additions & 2 deletions openraft/src/engine/handler/vote_handler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub(crate) struct VoteHandler<'st, C>
where C: RaftTypeConfig
{
pub(crate) config: &'st EngineConfig<C::NodeId>,
pub(crate) state: &'st mut RaftState<C::NodeId, C::Node, InstantOf<C>>,
pub(crate) state: &'st mut RaftState<C>,
pub(crate) output: &'st mut EngineOutput<C>,
pub(crate) internal_server_state: &'st mut InternalServerState<C::NodeId, InstantOf<C>>,
}
Expand All @@ -56,7 +56,7 @@ where C: RaftTypeConfig
T: Debug + Eq + OptionalSend,
E: Debug + Eq + OptionalSend,
Respond<C>: From<ValueSender<C, Result<T, E>>>,
F: Fn(&RaftState<C::NodeId, C::Node, InstantOf<C>>, RejectVoteRequest<C::NodeId>) -> Result<T, E>,
F: Fn(&RaftState<C>, RejectVoteRequest<C::NodeId>) -> Result<T, E>,
{
let vote_res = self.update_vote(vote);

Expand Down
16 changes: 11 additions & 5 deletions openraft/src/engine/testing.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
use std::io::Cursor;

use crate::Node;
use crate::RaftTypeConfig;
use crate::TokioRuntime;

/// Trivial Raft type config for Engine related unit test.
/// Trivial Raft type config for Engine related unit tests,
/// with an optional custom node type `N` for Node type.
#[derive(Debug, Clone, Copy, Default, Eq, PartialEq, Ord, PartialOrd)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub(crate) struct UTConfig {}
impl RaftTypeConfig for UTConfig {
pub(crate) struct UTConfig<N = ()> {
_p: std::marker::PhantomData<N>,
}
impl<N> RaftTypeConfig for UTConfig<N>
where N: Node + Copy + Ord
{
type D = ();
type R = ();
type NodeId = u64;
type Node = ();
type Entry = crate::Entry<UTConfig>;
type Node = N;
type Entry = crate::Entry<Self>;
type SnapshotData = Cursor<Vec<u8>>;
type AsyncRuntime = TokioRuntime;
}
10 changes: 2 additions & 8 deletions openraft/src/raft/external_request.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
//! Defines API for application to send request to access Raft core.
use crate::type_config::alias::InstantOf;
use crate::type_config::alias::NodeIdOf;
use crate::type_config::alias::NodeOf;
use crate::OptionalSend;
use crate::RaftState;
use crate::RaftTypeConfig;

pub trait BoxCoreFnInternal<C>: FnOnce(&RaftState<NodeIdOf<C>, NodeOf<C>, InstantOf<C>>) + OptionalSend
pub trait BoxCoreFnInternal<C>: FnOnce(&RaftState<C>) + OptionalSend
where C: RaftTypeConfig
{
}

impl<C: RaftTypeConfig, T: FnOnce(&RaftState<NodeIdOf<C>, NodeOf<C>, InstantOf<C>>) + OptionalSend> BoxCoreFnInternal<C>
for T
{
}
impl<C: RaftTypeConfig, T: FnOnce(&RaftState<C>) + OptionalSend> BoxCoreFnInternal<C> for T {}

/// Boxed trait object for external request function run in `RaftCore` task.
pub(crate) type BoxCoreFn<C> = Box<dyn BoxCoreFnInternal<C> + 'static>;
5 changes: 2 additions & 3 deletions openraft/src/raft/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ use crate::raft::trigger::Trigger;
use crate::storage::RaftLogStorage;
use crate::storage::RaftStateMachine;
use crate::type_config::alias::AsyncRuntimeOf;
use crate::type_config::alias::InstantOf;
use crate::type_config::alias::JoinErrorOf;
use crate::type_config::alias::SnapshotDataOf;
use crate::AsyncRuntime;
Expand Down Expand Up @@ -811,7 +810,7 @@ where C: RaftTypeConfig
/// ```
pub async fn with_raft_state<F, V>(&self, func: F) -> Result<V, Fatal<C::NodeId>>
where
F: FnOnce(&RaftState<C::NodeId, C::Node, InstantOf<C>>) -> V + OptionalSend + 'static,
F: FnOnce(&RaftState<C>) -> V + OptionalSend + 'static,
V: OptionalSend + 'static,
{
let (tx, rx) = C::AsyncRuntime::oneshot();
Expand Down Expand Up @@ -848,7 +847,7 @@ where C: RaftTypeConfig
/// If the API channel is already closed (Raft is in shutdown), then the request functor is
/// destroyed right away and not called at all.
pub fn external_request<F>(&self, req: F)
where F: FnOnce(&RaftState<C::NodeId, C::Node, InstantOf<C>>) + OptionalSend + 'static {
where F: FnOnce(&RaftState<C>) + OptionalSend + 'static {
let req: BoxCoreFn<C> = Box::new(req);
let _ignore_error = self.inner.tx_api.send(RaftMsg::ExternalCoreRequest { req });
}
Expand Down
Loading

0 comments on commit 01fcb35

Please sign in to comment.