Skip to content

Commit

Permalink
Change: rename IntoOptionNodes to IntoNodes
Browse files Browse the repository at this point in the history
  • Loading branch information
drmingdrmer committed Nov 2, 2022
1 parent 5928fe6 commit dbeae33
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 30 deletions.
59 changes: 33 additions & 26 deletions openraft/src/membership/membership.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,41 +13,54 @@ use crate::quorum::QuorumSet;
use crate::MessageSummary;
use crate::NodeId;

/// Convert other types into the internal data structure for node infos
pub trait IntoOptionNodes<NID, N>
/// Convert types into a map of `Node`.
pub trait IntoNodes<NID, N>
where
N: Node,
NID: NodeId,
{
fn into_option_nodes(self) -> BTreeMap<NID, N>;
fn into_nodes(self) -> BTreeMap<NID, N>;
}

impl<NID, N> IntoOptionNodes<NID, N> for ()
impl<NID, N> IntoNodes<NID, N> for ()
where
N: Node,
NID: NodeId,
{
fn into_option_nodes(self) -> BTreeMap<NID, N> {
fn into_nodes(self) -> BTreeMap<NID, N> {
btreemap! {}
}
}

impl<NID, N> IntoOptionNodes<NID, N> for BTreeSet<NID>
impl<NID, N> IntoNodes<NID, N> for BTreeSet<NID>
where
N: Node,
NID: NodeId,
{
fn into_option_nodes(self) -> BTreeMap<NID, N> {
fn into_nodes(self) -> BTreeMap<NID, N> {
self.into_iter().map(|node_id| (node_id, N::default())).collect()
}
}

impl<NID, N> IntoOptionNodes<NID, N> for BTreeMap<NID, N>
impl<NID, N> IntoNodes<NID, N> for Option<BTreeSet<NID>>
where
N: Node,
NID: NodeId,
{
fn into_option_nodes(self) -> BTreeMap<NID, N> {
fn into_nodes(self) -> BTreeMap<NID, N> {
match self {
None => BTreeMap::new(),
Some(s) => s.into_iter().map(|node_id| (node_id, N::default())).collect(),
}
}
}

impl<NID, N> IntoNodes<NID, N> for BTreeMap<NID, N>
where
N: Node,
NID: NodeId,
{
fn into_nodes(self) -> BTreeMap<NID, N> {
self
}
}
Expand Down Expand Up @@ -81,7 +94,6 @@ where
{
fn from(b: BTreeMap<NID, N>) -> Self {
let member_ids = b.keys().cloned().collect::<BTreeSet<NID>>();

Membership::with_nodes(vec![member_ids], b)
}
}
Expand Down Expand Up @@ -138,18 +150,13 @@ where
{
/// Create a new Membership of multiple configs(joint) and optionally a set of learner node ids.
///
/// A node id that is in `node_ids` but is not in `configs` is a **learner**.
pub fn new(configs: Vec<BTreeSet<NID>>, node_ids: Option<BTreeSet<NID>>) -> Self {
/// A node id that is in `nodes` but is not in `configs` is a **learner**.
///
/// An node id present in `configs` but not in `nodes` is filled with default value.
pub fn new<T>(configs: Vec<BTreeSet<NID>>, nodes: T) -> Self
where T: IntoNodes<NID, N> {
let voter_ids = configs.as_joint().ids().collect::<BTreeSet<_>>();

let nodes = match node_ids {
None => {
btreemap! {}
}
Some(x) => x.into_option_nodes(),
};

let nodes = Self::extend_nodes(nodes, &voter_ids.into_option_nodes());
let nodes = Self::extend_nodes(nodes.into_nodes(), &voter_ids.into_nodes());

Membership { configs, nodes }
}
Expand All @@ -161,10 +168,10 @@ where
/// - `BTreeSet<NodeId>` provides learner node ids whose `Node` data are `Node::default()`,
/// - `BTreeMap<NodeId, Node>` provides nodes for every node id. Node ids that are not in `configs` are learners.
///
/// Every node id in `configs` has to present in `nodes`.
/// Every node id in `configs` has to present in `nodes`. This is the only difference from [`Membership::new`].
pub(crate) fn with_nodes<T>(configs: Vec<BTreeSet<NID>>, nodes: T) -> Self
where T: IntoOptionNodes<NID, N> {
let nodes = nodes.into_option_nodes();
where T: IntoNodes<NID, N> {
let nodes = nodes.into_nodes();

if cfg!(debug_assertions) {
for voter_id in configs.as_joint().ids() {
Expand Down Expand Up @@ -297,8 +304,8 @@ where
/// }
/// ```
pub(crate) fn next_safe<T>(&self, goal: T, turn_to_learner: bool) -> Self
where T: IntoOptionNodes<NID, N> {
let goal = goal.into_option_nodes();
where T: IntoNodes<NID, N> {
let goal = goal.into_nodes();

let goal_ids = goal.keys().cloned().collect::<BTreeSet<_>>();

Expand Down
2 changes: 1 addition & 1 deletion openraft/src/membership/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ mod bench;
#[cfg(test)] mod membership_test;

pub use effective_membership::EffectiveMembership;
pub use membership::IntoOptionNodes;
pub use membership::IntoNodes;
pub use membership::Membership;
pub use membership_state::MembershipState;
pub(crate) use node_type::NodeRole;
6 changes: 3 additions & 3 deletions openraft/src/raft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use crate::error::Fatal;
use crate::error::InitializeError;
use crate::error::InstallSnapshotError;
use crate::error::VoteError;
use crate::membership::IntoOptionNodes;
use crate::membership::IntoNodes;
use crate::metrics::RaftMetrics;
use crate::metrics::Wait;
use crate::node::Node;
Expand Down Expand Up @@ -398,11 +398,11 @@ impl<C: RaftTypeConfig, N: RaftNetworkFactory<C>, S: RaftStorage<C>> Raft<C, N,
/// with different config will result in split brain condition.
#[tracing::instrument(level = "debug", skip(self))]
pub async fn initialize<T>(&self, members: T) -> Result<(), InitializeError<C::NodeId, C::Node>>
where T: IntoOptionNodes<C::NodeId, C::Node> + Debug {
where T: IntoNodes<C::NodeId, C::Node> + Debug {
let (tx, rx) = oneshot::channel();
self.call_core(
RaftMsg::Initialize {
members: members.into_option_nodes(),
members: members.into_nodes(),
tx,
},
rx,
Expand Down

0 comments on commit dbeae33

Please sign in to comment.