Skip to content
Snippets Groups Projects
Verified Commit ef459775 authored by Yaroslav Dynnikov's avatar Yaroslav Dynnikov
Browse files

Write some docstrings

parent e4855d7a
No related branches found
No related tags found
1 merge request!71Fair raft join
//! Compatibility layer between Tarantool and `raft-rs`.
mod error; mod error;
mod network; mod network;
pub mod node; pub mod node;
...@@ -15,9 +17,14 @@ use protobuf::ProtobufEnum as _; ...@@ -15,9 +17,14 @@ use protobuf::ProtobufEnum as _;
pub use network::ConnectionPool; pub use network::ConnectionPool;
pub use storage::Storage; pub use storage::Storage;
/////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////
/// LogicalClock /// Timestamps for raft entries.
///
/// Logical clock provides a cheap and easy way for generating globally unique identifiers.
///
/// - `count` is a simple in-memory counter. It's cheap to increment because it's volatile.
/// - `gen` should be persisted upon LogicalClock initialization to ensure the uniqueness.
/// - `id` corresponds to `raft_id` of the instance (that is already unique across nodes).
#[derive(Clone, Debug, Default, Serialize, Deserialize, Hash, PartialEq, Eq)] #[derive(Clone, Debug, Default, Serialize, Deserialize, Hash, PartialEq, Eq)]
pub struct LogicalClock { pub struct LogicalClock {
id: u64, id: u64,
...@@ -30,27 +37,27 @@ impl LogicalClock { ...@@ -30,27 +37,27 @@ impl LogicalClock {
Self { id, gen, count: 0 } Self { id, gen, count: 0 }
} }
pub fn inc(&mut self) -> Self { pub fn inc(&mut self) {
self.count += 1; self.count += 1;
self.clone()
} }
} }
/////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////
/// Op /// The operation on the raft state machine.
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)] #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "snake_case")] #[serde(rename_all = "snake_case")]
#[serde(tag = "kind")] #[serde(tag = "kind")]
pub enum Op { pub enum Op {
/// No operation.
Nop, Nop,
/// Print the message in tarantool log.
Info { msg: String }, Info { msg: String },
/// Evaluate the code on every instance in cluster.
EvalLua { code: String }, EvalLua { code: String },
} }
/////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////
/// Peer /// Serializable struct representing the member of the raft group.
#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq)] #[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq)]
pub struct Peer { pub struct Peer {
pub raft_id: u64, pub raft_id: u64,
...@@ -60,25 +67,41 @@ pub struct Peer { ...@@ -60,25 +67,41 @@ pub struct Peer {
// pub replicaset_id: String, // pub replicaset_id: String,
// pub instance_uuid: String, // pub instance_uuid: String,
// pub replicaset_uuid: String, // pub replicaset_uuid: String,
pub commit_index: u64, // 0 means it's not committed yet /// `0` means it's not committed yet.
pub commit_index: u64,
} }
impl AsTuple for Peer {} impl AsTuple for Peer {}
impl Peer {} impl Peer {}
/////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////
/// Entry /// Serializable representation of `raft::prelude::Entry`.
///
/// See correspondig definition in `raft-rs`:
/// - <https://github.com/tikv/raft-rs/blob/v0.6.0/proto/proto/eraftpb.proto#L23>
///
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)] #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
pub struct Entry { pub struct Entry {
/// ```
/// enum EntryType {
/// EntryNormal = 0;
/// EntryConfChange = 1;
/// EntryConfChangeV2 = 2;
/// }
/// ```
pub entry_type: i32, pub entry_type: i32,
pub index: u64, pub index: u64,
pub term: u64, pub term: u64,
/// Corresponding `entry.data`. Solely managed by `raft-rs`.
#[serde(with = "serde_bytes")] #[serde(with = "serde_bytes")]
pub data: Vec<u8>, // base64 pub data: Vec<u8>,
/// Corresponding `entry.payload`. Managed by the Picodata.
pub context: Option<EntryContext>, pub context: Option<EntryContext>,
} }
/// Raft entry payload specific to the Picodata.
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)] #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
#[serde(untagged)] #[serde(untagged)]
pub enum EntryContext { pub enum EntryContext {
...@@ -86,12 +109,14 @@ pub enum EntryContext { ...@@ -86,12 +109,14 @@ pub enum EntryContext {
ConfChange(EntryContextConfChange), ConfChange(EntryContextConfChange),
} }
/// [`EntryContext`] of a normal entry.
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)] #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
pub struct EntryContextNormal { pub struct EntryContextNormal {
pub lc: LogicalClock, pub lc: LogicalClock,
pub op: Op, pub op: Op,
} }
/// [`EntryContext`] of a conf change entry, either `EntryConfChange` or `EntryConfChangeV2`
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)] #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
pub struct EntryContextConfChange { pub struct EntryContextConfChange {
pub lc: LogicalClock, pub lc: LogicalClock,
...@@ -103,6 +128,8 @@ impl ContextCoercion for EntryContextNormal {} ...@@ -103,6 +128,8 @@ impl ContextCoercion for EntryContextNormal {}
impl ContextCoercion for EntryContextConfChange {} impl ContextCoercion for EntryContextConfChange {}
impl Entry { impl Entry {
/// Returns the logical clock value (if any)
/// from both `EntryNormal` and `EntryConfChange`.
fn lc(&self) -> Option<&LogicalClock> { fn lc(&self) -> Option<&LogicalClock> {
match &self.context { match &self.context {
None => None, None => None,
...@@ -111,6 +138,7 @@ impl Entry { ...@@ -111,6 +138,7 @@ impl Entry {
} }
} }
/// Returns the contained `Op` if it's an `EntryNormal`.
fn op(&self) -> Option<&Op> { fn op(&self) -> Option<&Op> {
match &self.context { match &self.context {
Some(EntryContext::Normal(v)) => Some(&v.op), Some(EntryContext::Normal(v)) => Some(&v.op),
...@@ -118,10 +146,11 @@ impl Entry { ...@@ -118,10 +146,11 @@ impl Entry {
None => None, None => None,
} }
} }
/// Returns the iterator over contained `Vec<Peer>` if it's an `EntryConfChange`.
fn iter_peers(&self) -> std::slice::Iter<'_, Peer> { fn iter_peers(&self) -> std::slice::Iter<'_, Peer> {
match &self.context { match &self.context {
Some(EntryContext::ConfChange(v)) => v.peers.iter(), Some(EntryContext::ConfChange(v)) => v.peers.iter(),
// Some(EntryContext::Normal(v)) => &[].iter(),
_ => (&[]).iter(), _ => (&[]).iter(),
} }
} }
...@@ -191,8 +220,10 @@ impl TryFrom<self::Entry> for raft::Entry { ...@@ -191,8 +220,10 @@ impl TryFrom<self::Entry> for raft::Entry {
} }
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
/// Message /// A wrapper for `raft::prelude::Message` already serialized with a protobuf.
///
/// This struct is used for passing `raft::prelude::Message`
/// over Tarantool binary protocol (`net_box`).
#[derive(Clone, Deserialize, Serialize)] #[derive(Clone, Deserialize, Serialize)]
struct MessagePb(#[serde(with = "serde_bytes")] Vec<u8>); struct MessagePb(#[serde(with = "serde_bytes")] Vec<u8>);
impl AsTuple for MessagePb {} impl AsTuple for MessagePb {}
...@@ -220,8 +251,7 @@ impl TryFrom<self::MessagePb> for raft::Message { ...@@ -220,8 +251,7 @@ impl TryFrom<self::MessagePb> for raft::Message {
} }
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
/// Context coercion /// This trait allows converting `EntryContext` to / from `Vec<u8>`.
pub trait ContextCoercion: Serialize + DeserializeOwned { pub trait ContextCoercion: Serialize + DeserializeOwned {
fn read_from_bytes(bytes: &[u8]) -> Result<Option<Self>, error::CoercionError> { fn read_from_bytes(bytes: &[u8]) -> Result<Option<Self>, error::CoercionError> {
match bytes { match bytes {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment