From 3b4f4e186bca5404744d2940571974500d52734d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E7=82=8E=E6=B3=BC?= Date: Tue, 14 Mar 2023 14:02:19 +0800 Subject: [PATCH] Change: move log id related traits to mod `openraft::log_id` Move trait `RaftLogId`, `LogIdOptionExt` and `LogIndexOptionExt` from `openraft::raft_types` to mod `openraft::log_id` --- openraft/src/core/raft_core.rs | 4 +- openraft/src/core/replication_state.rs | 2 +- openraft/src/defensive.rs | 2 +- openraft/src/engine/log_id_list.rs | 2 +- openraft/src/entry/entry_ref.rs | 2 +- openraft/src/entry/mod.rs | 2 +- openraft/src/entry/traits.rs | 2 +- openraft/src/leader/leader.rs | 2 +- openraft/src/lib.rs | 3 +- openraft/src/log_id/log_id_option_ext.rs | 33 ++++++++ openraft/src/log_id/log_index_option_ext.rs | 33 ++++++++ openraft/src/log_id/mod.rs | 11 ++- openraft/src/log_id/raft_log_id.rs | 22 ++++++ openraft/src/log_id/traits.rs | 0 .../src/membership/effective_membership.rs | 2 +- openraft/src/metrics/wait_test.rs | 2 +- openraft/src/raft_state/mod.rs | 2 +- openraft/src/raft_types.rs | 79 ------------------- openraft/src/replication/mod.rs | 2 +- openraft/src/runtime/mod.rs | 2 +- 20 files changed, 112 insertions(+), 97 deletions(-) create mode 100644 openraft/src/log_id/log_id_option_ext.rs create mode 100644 openraft/src/log_id/log_index_option_ext.rs create mode 100644 openraft/src/log_id/raft_log_id.rs delete mode 100644 openraft/src/log_id/traits.rs diff --git a/openraft/src/core/raft_core.rs b/openraft/src/core/raft_core.rs index 85dc0fded..0816aa652 100644 --- a/openraft/src/core/raft_core.rs +++ b/openraft/src/core/raft_core.rs @@ -44,6 +44,8 @@ use crate::error::InitializeError; use crate::error::QuorumNotEnough; use crate::error::RPCError; use crate::error::Timeout; +use crate::log_id::LogIdOptionExt; +use crate::log_id::RaftLogId; use crate::metrics::RaftMetrics; use crate::metrics::ReplicationMetrics; use crate::metrics::UpdateMatchedLogId; @@ -63,8 +65,6 @@ use crate::raft::VoteRequest; use crate::raft::VoteResponse; use crate::raft::VoteTx; use crate::raft_state::LogStateReader; -use crate::raft_types::LogIdOptionExt; -use crate::raft_types::RaftLogId; use crate::replication::Replicate; use crate::replication::ReplicationCore; use crate::replication::ReplicationHandle; diff --git a/openraft/src/core/replication_state.rs b/openraft/src/core/replication_state.rs index 357f79d49..7430cd3c0 100644 --- a/openraft/src/core/replication_state.rs +++ b/openraft/src/core/replication_state.rs @@ -1,4 +1,4 @@ -use crate::raft_types::LogIndexOptionExt; +use crate::log_id::LogIndexOptionExt; /// Calculate the distance between the matched log index on a replication target and local last log /// index diff --git a/openraft/src/defensive.rs b/openraft/src/defensive.rs index 1d351e9a2..c34c78de8 100644 --- a/openraft/src/defensive.rs +++ b/openraft/src/defensive.rs @@ -4,7 +4,7 @@ use std::ops::RangeBounds; use async_trait::async_trait; -use crate::raft_types::LogIdOptionExt; +use crate::log_id::LogIdOptionExt; use crate::DefensiveError; use crate::Entry; use crate::ErrorSubject; diff --git a/openraft/src/engine/log_id_list.rs b/openraft/src/engine/log_id_list.rs index 2dd7cdbe6..e095b9bbb 100644 --- a/openraft/src/engine/log_id_list.rs +++ b/openraft/src/engine/log_id_list.rs @@ -1,4 +1,4 @@ -use crate::raft_types::RaftLogId; +use crate::log_id::RaftLogId; use crate::storage::StorageHelper; use crate::LogId; use crate::LogIdOptionExt; diff --git a/openraft/src/entry/entry_ref.rs b/openraft/src/entry/entry_ref.rs index f2db28f8c..9446394ee 100644 --- a/openraft/src/entry/entry_ref.rs +++ b/openraft/src/entry/entry_ref.rs @@ -2,7 +2,7 @@ use std::fmt::Debug; use crate::entry::traits::RaftPayload; use crate::entry::RaftEntry; -use crate::raft_types::RaftLogId; +use crate::log_id::RaftLogId; use crate::EntryPayload; use crate::LogId; use crate::Membership; diff --git a/openraft/src/entry/mod.rs b/openraft/src/entry/mod.rs index 27d631863..39a9af288 100644 --- a/openraft/src/entry/mod.rs +++ b/openraft/src/entry/mod.rs @@ -1,6 +1,6 @@ use std::fmt::Debug; -use crate::raft_types::RaftLogId; +use crate::log_id::RaftLogId; use crate::LogId; use crate::Membership; use crate::MessageSummary; diff --git a/openraft/src/entry/traits.rs b/openraft/src/entry/traits.rs index c561b03a4..fa492684f 100644 --- a/openraft/src/entry/traits.rs +++ b/openraft/src/entry/traits.rs @@ -1,4 +1,4 @@ -use crate::raft_types::RaftLogId; +use crate::log_id::RaftLogId; use crate::Membership; use crate::Node; use crate::NodeId; diff --git a/openraft/src/leader/leader.rs b/openraft/src/leader/leader.rs index 5b4975b37..a420fa397 100644 --- a/openraft/src/leader/leader.rs +++ b/openraft/src/leader/leader.rs @@ -1,10 +1,10 @@ use std::collections::BTreeSet; +use crate::log_id::LogIndexOptionExt; use crate::progress::entry::ProgressEntry; use crate::progress::Progress; use crate::progress::VecProgress; use crate::quorum::QuorumSet; -use crate::raft_types::LogIndexOptionExt; use crate::LogId; use crate::NodeId; use crate::Vote; diff --git a/openraft/src/lib.rs b/openraft/src/lib.rs index 81caa2e85..1dbea7a69 100644 --- a/openraft/src/lib.rs +++ b/openraft/src/lib.rs @@ -89,6 +89,8 @@ pub use crate::defensive::DefensiveCheck; pub use crate::defensive::DefensiveCheckBase; pub use crate::entry::Entry; pub use crate::entry::EntryPayload; +pub use crate::log_id::LogIdOptionExt; +pub use crate::log_id::LogIndexOptionExt; pub use crate::membership::EffectiveMembership; pub use crate::membership::Membership; pub use crate::membership::StoredMembership; @@ -104,7 +106,6 @@ pub use crate::raft::Raft; pub use crate::raft::RaftTypeConfig; pub use crate::raft_state::MembershipState; pub use crate::raft_state::RaftState; -pub use crate::raft_types::LogIdOptionExt; pub(crate) use crate::raft_types::MetricsChangeFlags; pub use crate::raft_types::SnapshotId; pub use crate::raft_types::SnapshotSegmentId; diff --git a/openraft/src/log_id/log_id_option_ext.rs b/openraft/src/log_id/log_id_option_ext.rs new file mode 100644 index 000000000..06ab478cc --- /dev/null +++ b/openraft/src/log_id/log_id_option_ext.rs @@ -0,0 +1,33 @@ +use crate::LogId; +use crate::NodeId; + +pub trait LogIdOptionExt { + fn index(&self) -> Option; + fn next_index(&self) -> u64; +} + +impl LogIdOptionExt for Option> { + fn index(&self) -> Option { + self.map(|x| x.index) + } + + fn next_index(&self) -> u64 { + match self { + None => 0, + Some(log_id) => log_id.index + 1, + } + } +} + +impl LogIdOptionExt for Option<&LogId> { + fn index(&self) -> Option { + self.map(|x| x.index) + } + + fn next_index(&self) -> u64 { + match self { + None => 0, + Some(log_id) => log_id.index + 1, + } + } +} diff --git a/openraft/src/log_id/log_index_option_ext.rs b/openraft/src/log_id/log_index_option_ext.rs new file mode 100644 index 000000000..faeddd07d --- /dev/null +++ b/openraft/src/log_id/log_index_option_ext.rs @@ -0,0 +1,33 @@ +pub trait LogIndexOptionExt { + fn next_index(&self) -> u64; + fn prev_index(&self) -> Self; + fn add(&self, v: u64) -> Self; +} + +impl LogIndexOptionExt for Option { + fn next_index(&self) -> u64 { + match self { + None => 0, + Some(v) => v + 1, + } + } + + fn prev_index(&self) -> Self { + match self { + None => { + panic!("None has no previous value"); + } + Some(v) => { + if *v == 0 { + None + } else { + Some(*v - 1) + } + } + } + } + + fn add(&self, v: u64) -> Self { + Some(self.next_index() + v).prev_index() + } +} diff --git a/openraft/src/log_id/mod.rs b/openraft/src/log_id/mod.rs index 09abb42a4..9b950a2f9 100644 --- a/openraft/src/log_id/mod.rs +++ b/openraft/src/log_id/mod.rs @@ -1,13 +1,18 @@ +mod log_id_option_ext; +mod log_index_option_ext; +mod raft_log_id; + use std::fmt::Display; use std::fmt::Formatter; -use crate::raft_types::RaftLogId; +pub use log_id_option_ext::LogIdOptionExt; +pub use log_index_option_ext::LogIndexOptionExt; +pub use raft_log_id::RaftLogId; + use crate::CommittedLeaderId; use crate::MessageSummary; use crate::NodeId; -mod traits; - /// The identity of a raft log. /// A term, node_id and an index identifies an log globally. #[derive(Debug, Default, Copy, Clone, PartialOrd, Ord, PartialEq, Eq)] diff --git a/openraft/src/log_id/raft_log_id.rs b/openraft/src/log_id/raft_log_id.rs new file mode 100644 index 000000000..2b7839c75 --- /dev/null +++ b/openraft/src/log_id/raft_log_id.rs @@ -0,0 +1,22 @@ +use crate::CommittedLeaderId; +use crate::LogId; +use crate::NodeId; + +/// Defines API to operate an object that contains a log-id, such as a log entry or a log id. +pub trait RaftLogId { + /// Returns a reference to the leader id that proposed this log id. + /// + /// When a `LeaderId` is committed, some of its data can be discarded. + /// For example, a leader id in standard raft is `(term, node_id)`, but a log id does not have + /// to store the `node_id`, because in standard raft there is at most one leader that can be + /// established. + fn leader_id(&self) -> &CommittedLeaderId { + self.get_log_id().committed_leader_id() + } + + /// Return a reference to the log-id it stores. + fn get_log_id(&self) -> &LogId; + + /// Update the log id it contains. + fn set_log_id(&mut self, log_id: &LogId); +} diff --git a/openraft/src/log_id/traits.rs b/openraft/src/log_id/traits.rs deleted file mode 100644 index e69de29bb..000000000 diff --git a/openraft/src/membership/effective_membership.rs b/openraft/src/membership/effective_membership.rs index 8dc35d67d..3a7c53ef7 100644 --- a/openraft/src/membership/effective_membership.rs +++ b/openraft/src/membership/effective_membership.rs @@ -3,10 +3,10 @@ use std::fmt::Debug; use std::sync::Arc; use crate::entry::RaftEntry; +use crate::log_id::RaftLogId; use crate::node::Node; use crate::quorum::Joint; use crate::quorum::QuorumSet; -use crate::raft_types::RaftLogId; use crate::LogId; use crate::Membership; use crate::MessageSummary; diff --git a/openraft/src/metrics/wait_test.rs b/openraft/src/metrics/wait_test.rs index 23cb6c37a..6ed7bffa2 100644 --- a/openraft/src/metrics/wait_test.rs +++ b/openraft/src/metrics/wait_test.rs @@ -7,9 +7,9 @@ use tokio::sync::watch; use tokio::time::sleep; use crate::core::ServerState; +use crate::log_id::LogIdOptionExt; use crate::metrics::Wait; use crate::metrics::WaitError; -use crate::raft_types::LogIdOptionExt; use crate::vote::CommittedLeaderId; use crate::LogId; use crate::Membership; diff --git a/openraft/src/raft_state/mod.rs b/openraft/src/raft_state/mod.rs index d68431260..b65376766 100644 --- a/openraft/src/raft_state/mod.rs +++ b/openraft/src/raft_state/mod.rs @@ -8,8 +8,8 @@ use crate::entry::RaftEntry; use crate::equal; use crate::error::ForwardToLeader; use crate::less_equal; +use crate::log_id::RaftLogId; use crate::node::Node; -use crate::raft_types::RaftLogId; use crate::utime::UTime; use crate::validate::Validate; use crate::LogId; diff --git a/openraft/src/raft_types.rs b/openraft/src/raft_types.rs index cf44bb134..e6a9098d7 100644 --- a/openraft/src/raft_types.rs +++ b/openraft/src/raft_types.rs @@ -1,85 +1,6 @@ use std::fmt::Display; use std::fmt::Formatter; -use crate::vote::CommittedLeaderId; -use crate::LogId; -use crate::NodeId; - -pub trait RaftLogId { - fn leader_id(&self) -> &CommittedLeaderId { - self.get_log_id().committed_leader_id() - } - - fn get_log_id(&self) -> &LogId; - - fn set_log_id(&mut self, log_id: &LogId); -} - -pub trait LogIdOptionExt { - fn index(&self) -> Option; - fn next_index(&self) -> u64; -} - -impl LogIdOptionExt for Option> { - fn index(&self) -> Option { - self.map(|x| x.index) - } - - fn next_index(&self) -> u64 { - match self { - None => 0, - Some(log_id) => log_id.index + 1, - } - } -} - -impl LogIdOptionExt for Option<&LogId> { - fn index(&self) -> Option { - self.map(|x| x.index) - } - - fn next_index(&self) -> u64 { - match self { - None => 0, - Some(log_id) => log_id.index + 1, - } - } -} - -pub trait LogIndexOptionExt { - fn next_index(&self) -> u64; - fn prev_index(&self) -> Self; - fn add(&self, v: u64) -> Self; -} - -impl LogIndexOptionExt for Option { - fn next_index(&self) -> u64 { - match self { - None => 0, - Some(v) => v + 1, - } - } - - fn prev_index(&self) -> Self { - match self { - None => { - panic!("None has no previous value"); - } - Some(v) => { - if *v == 0 { - None - } else { - Some(*v - 1) - } - } - } - } - - fn add(&self, v: u64) -> Self { - Some(self.next_index() + v).prev_index() - } -} - // Everytime a snapshot is created, it is assigned with a globally unique id. pub type SnapshotId = String; diff --git a/openraft/src/replication/mod.rs b/openraft/src/replication/mod.rs index 79a604882..f57895c9e 100644 --- a/openraft/src/replication/mod.rs +++ b/openraft/src/replication/mod.rs @@ -25,12 +25,12 @@ use crate::error::HigherVote; use crate::error::RPCError; use crate::error::ReplicationError; use crate::error::Timeout; +use crate::log_id::LogIdOptionExt; use crate::log_id_range::LogIdRange; use crate::raft::AppendEntriesRequest; use crate::raft::AppendEntriesResponse; use crate::raft::InstallSnapshotRequest; use crate::raft::RaftMsg; -use crate::raft_types::LogIdOptionExt; use crate::storage::RaftLogReader; use crate::storage::Snapshot; use crate::ErrorSubject; diff --git a/openraft/src/runtime/mod.rs b/openraft/src/runtime/mod.rs index f43254481..715df215a 100644 --- a/openraft/src/runtime/mod.rs +++ b/openraft/src/runtime/mod.rs @@ -1,5 +1,5 @@ use crate::engine::Command; -use crate::raft_types::RaftLogId; +use crate::log_id::RaftLogId; use crate::Entry; use crate::RaftTypeConfig; use crate::StorageError;