Skip to content

Commit

Permalink
Move protos to a separate crate (#247)
Browse files Browse the repository at this point in the history
* Move protos to a separate crate

Makes Raft a workspace project

Signed-off-by: Nick Cameron <nrc@ncameron.org>

* Satisfy Rustfmt

Signed-off-by: Nick Cameron <nrc@ncameron.org>
  • Loading branch information
nrc authored Jun 19, 2019
1 parent 4fd768d commit 7ba7760
Show file tree
Hide file tree
Showing 11 changed files with 83 additions and 59 deletions.
7 changes: 3 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,12 @@ documentation = "https://docs.rs/raft"
description = "The rust language implementation of Raft algorithm."
categories = ["algorithms", "database-implementations"]
edition = "2018"
build = "build.rs"

[build-dependencies]
protobuf-build = "0.6"
[workspace]
members = ["proto"]

[features]
default = []
gen = []
# Enable failpoints
failpoint = ["fail"]

Expand All @@ -32,6 +30,7 @@ prost-derive = "0.5"
bytes = "0.4.11"
slog = "2.2"
quick-error = "1.2.2"
raft-proto = { path = "proto" }
rand = "0.6.4"
hashbrown = "0.3"
fail = { version = "0.2", optional = true }
Expand Down
1 change: 0 additions & 1 deletion harness/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ mod network;
pub use self::{interface::Interface, network::Network};
use slog::{Drain, Logger};


/// Build a logger for tests.
///
/// Currently, this is a terminal log. It ensures it is only initialized once to prevent clobbering.
Expand Down
27 changes: 27 additions & 0 deletions proto/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[package]
name = "raft-proto"
version = "0.1.0"
authors = ["The TiKV Project Developers"]
edition = "2018"
license = "Apache-2.0"
keywords = ["raft", "distributed-systems", "ha"]
repository = "https://github.com/pingcap/raft-rs"
homepage = "https://github.com/pingcap/raft-rs"
documentation = "https://docs.rs/raft-proto"
description = "Protocol definitions for the rust language implementation of the Raft algorithm."
categories = ["algorithms", "database-implementations"]
build = "build.rs"

[features]
default = []
gen = []

[build-dependencies]
protobuf-build = "0.6"

[dependencies]
bytes = "0.4.11"
lazy_static = "1.3.0"
prost = "0.5"
prost-derive = "0.5"
protobuf = "2"
15 changes: 2 additions & 13 deletions build.rs → proto/build.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,4 @@
// Copyright 2019 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
// Copyright 2019 TiKV Project Authors. Licensed under Apache-2.0.

use protobuf_build::*;
use std::fs::{read_dir, File};
Expand Down Expand Up @@ -44,7 +33,7 @@ fn main() {
println!("cargo:rerun-if-changed={}", f);
}

// Generate Prost files.
// Generate Prost output.
generate_prost_files(&file_names, "src/prost");
let mod_names = module_names_for_dir("src/prost");
generate_wrappers(
Expand Down
File renamed without changes.
48 changes: 48 additions & 0 deletions proto/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright 2019 TiKV Project Authors. Licensed under Apache-2.0.

pub use crate::prost::eraftpb;

mod prost;

pub mod prelude {
pub use crate::eraftpb::{
ConfChange, ConfChangeType, ConfState, Entry, EntryType, HardState, Message, MessageType,
Snapshot, SnapshotMetadata,
};
}

pub mod util {
use crate::eraftpb::{ConfChange, ConfChangeType, ConfState};

// Bring some consistency to things. The protobuf has `nodes` and it's not really a term that's used anymore.
impl ConfState {
/// Get the voters. This is identical to `nodes`.
#[inline]
pub fn get_voters(&self) -> &[u64] {
&self.nodes
}
}

impl<Iter1, Iter2> From<(Iter1, Iter2)> for ConfState
where
Iter1: IntoIterator<Item = u64>,
Iter2: IntoIterator<Item = u64>,
{
fn from((voters, learners): (Iter1, Iter2)) -> Self {
let mut conf_state = ConfState::default();
conf_state.mut_nodes().extend(voters.into_iter());
conf_state.mut_learners().extend(learners.into_iter());
conf_state
}
}

impl From<(u64, ConfState)> for ConfChange {
fn from((start_index, state): (u64, ConfState)) -> Self {
let mut change = ConfChange::default();
change.set_change_type(ConfChangeType::BeginMembershipChange);
change.set_configuration(state);
change.set_start_index(start_index);
change
}
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
10 changes: 2 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,10 +382,6 @@ extern crate quick_error;
extern crate getset;

mod config;
mod prost;
/// This module supplies the needed message types. However, it is autogenerated and thus cannot be
/// documented by field.
pub use crate::prost::eraftpb;
mod errors;
mod log_unstable;
mod progress;
Expand All @@ -412,6 +408,7 @@ pub use self::raw_node::{is_empty_snap, Peer, RawNode, Ready, SnapshotStatus};
pub use self::read_only::{ReadOnlyOption, ReadState};
pub use self::status::Status;
pub use self::storage::{RaftState, Storage};
pub use raft_proto::eraftpb;
use slog::{Drain, Logger};

pub mod prelude {
Expand All @@ -427,10 +424,7 @@ pub mod prelude {
//!
//! The prelude may grow over time as additional items see ubiquitous use.

pub use crate::eraftpb::{
ConfChange, ConfChangeType, ConfState, Entry, EntryType, HardState, Message, MessageType,
Snapshot, SnapshotMetadata,
};
pub use raft_proto::prelude::*;

pub use crate::config::Config;
pub use crate::raft::Raft;
Expand Down
34 changes: 1 addition & 33 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

use std::u64;

use crate::eraftpb::{ConfChange, ConfChangeType, ConfState, Entry, Message};
use crate::eraftpb::{Entry, Message};
use prost::Message as ProstMsg;

/// A number to represent that there is no limit.
Expand Down Expand Up @@ -79,38 +79,6 @@ pub fn limit_size<T: ProstMsg + Clone>(entries: &mut Vec<T>, max: Option<u64>) {
entries.truncate(limit);
}

// Bring some consistency to things. The protobuf has `nodes` and it's not really a term that's used anymore.
impl ConfState {
/// Get the voters. This is identical to `nodes`.
#[inline]
pub fn get_voters(&self) -> &[u64] {
&self.nodes
}
}

impl<Iter1, Iter2> From<(Iter1, Iter2)> for ConfState
where
Iter1: IntoIterator<Item = u64>,
Iter2: IntoIterator<Item = u64>,
{
fn from((voters, learners): (Iter1, Iter2)) -> Self {
let mut conf_state = ConfState::default();
conf_state.mut_nodes().extend(voters.into_iter());
conf_state.mut_learners().extend(learners.into_iter());
conf_state
}
}

impl From<(u64, ConfState)> for ConfChange {
fn from((start_index, state): (u64, ConfState)) -> Self {
let mut change = ConfChange::default();
change.set_change_type(ConfChangeType::BeginMembershipChange);
change.set_configuration(state);
change.set_start_index(start_index);
change
}
}

/// Check whether the entry is continuous to the message.
/// i.e msg's next entry index should be equal to the first entries's index
pub fn is_continuous_ents(msg: &Message, ents: &[Entry]) -> bool {
Expand Down

0 comments on commit 7ba7760

Please sign in to comment.