Skip to content

Commit

Permalink
Refactor: move storage related types into types/v070. Dir "types" inc…
Browse files Browse the repository at this point in the history
…ludes all data-compatibility related types
  • Loading branch information
drmingdrmer committed Jul 4, 2022
1 parent 080ce1d commit 84b32de
Show file tree
Hide file tree
Showing 33 changed files with 900 additions and 777 deletions.
34 changes: 10 additions & 24 deletions openraft/src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,33 +51,19 @@ use crate::raft::RaftRespTx;
use crate::raft_types::LogIdOptionExt;
use crate::replication::ReplicaEvent;
use crate::replication::ReplicationStream;
use crate::storage::HardState;
use crate::AppData;
use crate::AppDataResponse;
use crate::LogId;
use crate::Membership;
use crate::types::v070::AppData;
use crate::types::v070::AppDataResponse;
use crate::types::v070::EffectiveMembership;
use crate::types::v070::HardState;
use crate::types::v070::LogId;
use crate::types::v070::Membership;
use crate::types::v070::NodeId;
use crate::types::v070::RaftNetwork;
use crate::types::v070::RaftStorage;
use crate::types::v070::StorageError;
use crate::MessageSummary;
use crate::NodeId;
use crate::RaftNetwork;
use crate::RaftStorage;
use crate::StorageError;
use crate::Update;

/// The currently active membership config.
///
/// It includes:
/// - the id of the log that sets this membership config,
/// - and the config.
///
/// An active config is just the last seen config in raft spec.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct EffectiveMembership {
/// The id of the log that applies this membership config
pub log_id: LogId,

pub membership: Membership,
}

impl EffectiveMembership {
pub fn new_initial(node_id: u64) -> Self {
EffectiveMembership {
Expand Down
16 changes: 8 additions & 8 deletions openraft/src/core/replication.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ use crate::raft::RaftRespTx;
use crate::replication::RaftEvent;
use crate::replication::ReplicaEvent;
use crate::replication::ReplicationStream;
use crate::storage::Snapshot;
use crate::summary::MessageSummary;
use crate::AppData;
use crate::AppDataResponse;
use crate::LogId;
use crate::NodeId;
use crate::RaftNetwork;
use crate::RaftStorage;
use crate::types::v070::AppData;
use crate::types::v070::AppDataResponse;
use crate::types::v070::LogId;
use crate::types::v070::NodeId;
use crate::types::v070::RaftNetwork;
use crate::types::v070::RaftStorage;
use crate::types::v070::Snapshot;
use crate::types::v070::StorageError;
use crate::ReplicationMetrics;
use crate::StorageError;

impl<'a, D: AppData, R: AppDataResponse, N: RaftNetwork<D>, S: RaftStorage<D, R>> LeaderState<'a, D, R, N, S> {
/// Spawn a new replication stream returning its replication state handle.
Expand Down
2 changes: 1 addition & 1 deletion openraft/src/defensive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ use async_trait::async_trait;

use crate::raft::Entry;
use crate::raft_types::LogIdOptionExt;
use crate::storage::HardState;
use crate::AppData;
use crate::AppDataResponse;
use crate::DefensiveError;
use crate::ErrorSubject;
use crate::HardState;
use crate::LogId;
use crate::RaftStorage;
use crate::StorageError;
Expand Down
2 changes: 1 addition & 1 deletion openraft/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ use std::time::Duration;
use serde::Deserialize;
use serde::Serialize;

use crate::raft_types::SnapshotSegmentId;
use crate::LogId;
use crate::NodeId;
use crate::SnapshotSegmentId;
use crate::StorageError;

/// Fatal is unrecoverable and shuts down raft at once.
Expand Down
74 changes: 28 additions & 46 deletions openraft/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,68 +18,50 @@ pub mod network;
pub mod raft;
pub mod storage;
pub mod testing;
pub mod types;

#[cfg(test)]
mod metrics_wait_test;

pub use async_trait;
use serde::de::DeserializeOwned;
use serde::Serialize;

pub use crate::config::Config;
pub use crate::config::ConfigError;
pub use crate::config::SnapshotPolicy;
pub use crate::core::EffectiveMembership;
pub use crate::core::State;
pub use crate::defensive::DefensiveCheck;
pub use crate::membership::Membership;
pub use crate::metrics::RaftMetrics;
pub use crate::network::RaftNetwork;
pub use crate::raft::Raft;
pub use crate::raft_types::LogId;
pub use crate::raft_types::LogIdOptionExt;
pub use crate::raft_types::SnapshotId;
pub use crate::raft_types::SnapshotSegmentId;
pub use crate::raft_types::StateMachineChanges;
pub use crate::raft_types::Update;
pub use crate::replication::ReplicationMetrics;
pub use crate::storage::RaftStorage;
pub use crate::storage::RaftStorageDebug;
pub use crate::storage::SnapshotMeta;
pub use crate::storage_error::DefensiveError;
pub use crate::storage_error::ErrorSubject;
pub use crate::storage_error::ErrorVerb;
pub use crate::storage_error::StorageError;
pub use crate::storage_error::StorageIOError;
pub use crate::storage_error::Violation;
pub use crate::store_ext::StoreExt;
pub use crate::store_wrapper::Wrapper;
pub use crate::summary::MessageSummary;

/// A Raft node's ID.
pub type NodeId = u64;

/// A trait defining application specific data.
///
/// The intention of this trait is that applications which are using this crate will be able to
/// use their own concrete data types throughout their application without having to serialize and
/// deserialize their data as it goes through Raft. Instead, applications can present their data
/// models as-is to Raft, Raft will present it to the application's `RaftStorage` impl when ready,
/// and the application may then deal with the data directly in the storage engine without having
/// to do a preliminary deserialization.
pub trait AppData: Clone + Send + Sync + Serialize + DeserializeOwned + 'static {}

/// A trait defining application specific response data.
///
/// The intention of this trait is that applications which are using this crate will be able to
/// use their own concrete data types for returning response data from the storage layer when an
/// entry is applied to the state machine as part of a client request (this is not used during
/// replication). This allows applications to seamlessly return application specific data from
/// their storage layer, up through Raft, and back into their application for returning
/// data to clients.
///
/// This type must encapsulate both success and error responses, as application specific logic
/// related to the success or failure of a client request — application specific validation logic,
/// enforcing of data constraints, and anything of that nature — are expressly out of the realm of
/// the Raft consensus protocol.
pub trait AppDataResponse: Clone + Send + Sync + Serialize + DeserializeOwned + 'static {}
pub use crate::types::v070::AppData;
pub use crate::types::v070::AppDataResponse;
pub use crate::types::v070::AppendEntriesRequest;
pub use crate::types::v070::AppendEntriesResponse;
pub use crate::types::v070::DefensiveError;
pub use crate::types::v070::EffectiveMembership;
pub use crate::types::v070::Entry;
pub use crate::types::v070::EntryPayload;
pub use crate::types::v070::ErrorSubject;
pub use crate::types::v070::ErrorVerb;
pub use crate::types::v070::HardState;
pub use crate::types::v070::InitialState;
pub use crate::types::v070::LogId;
pub use crate::types::v070::LogState;
pub use crate::types::v070::Membership;
pub use crate::types::v070::NodeId;
pub use crate::types::v070::RaftNetwork;
pub use crate::types::v070::RaftStorage;
pub use crate::types::v070::RaftStorageDebug;
pub use crate::types::v070::Snapshot;
pub use crate::types::v070::SnapshotId;
pub use crate::types::v070::SnapshotMeta;
pub use crate::types::v070::SnapshotSegmentId;
pub use crate::types::v070::StateMachineChanges;
pub use crate::types::v070::StorageError;
pub use crate::types::v070::StorageIOError;
pub use crate::types::v070::Violation;
16 changes: 1 addition & 15 deletions openraft/src/membership/membership.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,12 @@ use std::collections::BTreeMap;
use std::collections::BTreeSet;

use maplit::btreeset;
use serde::Deserialize;
use serde::Serialize;

use crate::membership::quorum;
use crate::Membership;
use crate::MessageSummary;
use crate::NodeId;

/// The membership configuration of the cluster.
///
/// It could be a joint of one, two or more configs, i.e., a quorum is a node set that is superset of a majority of
/// every config.
#[derive(Clone, Default, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Membership {
/// Multi configs.
configs: Vec<BTreeSet<NodeId>>,

/// Cache of all node ids.
all_nodes: BTreeSet<NodeId>,
}

impl MessageSummary for Membership {
fn summary(&self) -> String {
let mut res = vec!["[".to_string()];
Expand Down
2 changes: 0 additions & 2 deletions openraft/src/membership/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,3 @@ mod membership;
mod membership_test;

pub mod quorum;

pub use membership::Membership;
2 changes: 1 addition & 1 deletion openraft/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ use tokio::sync::watch;
use tokio::time::Duration;
use tokio::time::Instant;

use crate::core::EffectiveMembership;
use crate::core::State;
use crate::error::Fatal;
use crate::raft_types::LogIdOptionExt;
use crate::EffectiveMembership;
use crate::LogId;
use crate::Membership;
use crate::MessageSummary;
Expand Down
2 changes: 1 addition & 1 deletion openraft/src/metrics_wait_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ use maplit::btreeset;
use tokio::sync::watch;
use tokio::time::sleep;

use crate::core::EffectiveMembership;
use crate::metrics::Wait;
use crate::metrics::WaitError;
use crate::raft_types::LogIdOptionExt;
use crate::types::v070::EffectiveMembership;
use crate::LogId;
use crate::Membership;
use crate::RaftMetrics;
Expand Down
34 changes: 0 additions & 34 deletions openraft/src/network.rs
Original file line number Diff line number Diff line change
@@ -1,35 +1 @@
//! The Raft network interface.

use anyhow::Result;
use async_trait::async_trait;

use crate::raft::AppendEntriesRequest;
use crate::raft::AppendEntriesResponse;
use crate::raft::InstallSnapshotRequest;
use crate::raft::InstallSnapshotResponse;
use crate::raft::VoteRequest;
use crate::raft::VoteResponse;
use crate::AppData;
use crate::NodeId;

/// A trait defining the interface for a Raft network between cluster members.
///
/// See the [network chapter of the guide](https://datafuselabs.github.io/openraft/network.html)
/// for details and discussion on this trait and how to implement it.
#[async_trait]
pub trait RaftNetwork<D>: Send + Sync + 'static
where D: AppData
{
/// Send an AppendEntries RPC to the target Raft node (§5).
async fn send_append_entries(&self, target: NodeId, rpc: AppendEntriesRequest<D>) -> Result<AppendEntriesResponse>;

/// Send an InstallSnapshot RPC to the target Raft node (§7).
async fn send_install_snapshot(
&self,
target: NodeId,
rpc: InstallSnapshotRequest,
) -> Result<InstallSnapshotResponse>;

/// Send a RequestVote RPC to the target Raft node (§5).
async fn send_vote(&self, target: NodeId, rpc: VoteRequest) -> Result<VoteResponse>;
}
Loading

0 comments on commit 84b32de

Please sign in to comment.