Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Porting prost migration to raft 0.4 #204

Merged
merged 14 commits into from
Apr 23, 2019
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@ tmp
/bin

Cargo.lock
rust-toolchain
*.rs.bk
*.rs.fmt
16 changes: 14 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "raft"
version = "0.4.0"
version = "0.4.1"
authors = ["The TiKV Project Developers"]
license = "Apache-2.0"
keywords = ["raft", "distributed-systems", "ha"]
Expand All @@ -10,23 +10,35 @@ homepage = "https://github.com/pingcap/raft-rs"
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.4.2"

[features]
default = []
gen = []
failpoint = ["fail"]

[dependencies]
log = ">0.2"
protobuf = "2.0.4"
protobuf = "~2.1-2.2"
lazy_static = "1.3.0"
prost = "0.5"
prost-derive = "0.5"
bytes = "0.4.11"
quick-error = "1.2.2"
rand = "0.5.4"
fxhash = "0.2.1"
hashbrown = "0.1"
fail = { version = "0.2", optional = true }

[dev-dependencies]
env_logger = "0.5"
criterion = ">0.2.4"
lazy_static = "1.0"
regex = "1.1"

[[bench]]
name = "benches"
Expand Down
5 changes: 0 additions & 5 deletions benches/benches.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
#![allow(dead_code)] // Due to criterion we need this to avoid warnings.

extern crate criterion;
extern crate env_logger;
extern crate raft;

use criterion::Criterion;
use std::time::Duration;

Expand All @@ -12,7 +8,6 @@ mod suites;
pub const DEFAULT_RAFT_SETS: [(usize, usize); 4] = [(0, 0), (3, 1), (5, 2), (7, 3)];

fn main() {
criterion::init_logging();
let mut c = Criterion::default()
// Configure defaults before overriding with args.
.warm_up_time(Duration::from_millis(500))
Expand Down
2 changes: 1 addition & 1 deletion benches/suites/progress_set.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::DEFAULT_RAFT_SETS;
use criterion::{Bencher, Criterion};
use raft::ProgressSet;
use DEFAULT_RAFT_SETS;

pub fn bench_progress_set(c: &mut Criterion) {
bench_progress_set_new(c);
Expand Down
2 changes: 1 addition & 1 deletion benches/suites/raft.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::DEFAULT_RAFT_SETS;
use criterion::{Bencher, Criterion};
use raft::{storage::MemStorage, Config, Raft};
use DEFAULT_RAFT_SETS;

pub fn bench_raft(c: &mut Criterion) {
bench_raft_new(c);
Expand Down
84 changes: 84 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// 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.

extern crate protobuf_build;

use protobuf_build::*;
use std::fs::{read_dir, File};
use std::io::Write;

fn main() {
// This build script creates files in the `src` directory. Since that is
// outside Cargo's OUT_DIR it will cause an error when this crate is used
// as a dependency. Therefore, the user must opt-in to regenerating the
// Rust files.
if !cfg!(feature = "gen") {
println!("cargo:rerun-if-changed=build.rs");
return;
}

check_protoc_version();

let file_names: Vec<_> = read_dir("proto")
.expect("Couldn't read proto directory")
.filter_map(|e| {
let e = e.expect("Couldn't list file");
if e.file_type().expect("File broken").is_dir() {
None
} else {
Some(format!("proto/{}", e.file_name().to_string_lossy()))
}
})
.collect();

for f in &file_names {
println!("cargo:rerun-if-changed={}", f);
}

// Generate Prost files.
generate_prost_files(&file_names, "src/prost");
let mod_names = module_names_for_dir("src/prost");
generate_wrappers(
&mod_names
.iter()
.map(|m| format!("src/prost/{}.rs", m))
.collect::<Vec<_>>(),
"src/prost",
GenOpt::All,
);
generate_prost_rs(&mod_names);
}

fn generate_prost_rs(mod_names: &[String]) {
let mut text = "#![allow(dead_code)]\n\
#![allow(missing_docs)]\n\
#![allow(clippy::all)]\n\n"
.to_owned();

for mod_name in mod_names {
text.push_str("pub mod ");
text.push_str(mod_name);
text.push_str("{\n");
text.push_str("include!(\"prost/");
text.push_str(mod_name);
text.push_str(".rs\");");
text.push_str("include!(\"prost/wrapper_");
text.push_str(mod_name);
text.push_str(".rs\");");
text.push_str("}\n\n");
}

let mut lib = File::create("src/prost.rs").expect("Could not create prost.rs");
lib.write_all(text.as_bytes())
.expect("Could not write prost.rs");
}
7 changes: 4 additions & 3 deletions examples/single_mem_node/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

extern crate raft;
use raft;

use std::collections::HashMap;
use std::sync::mpsc::{self, RecvTimeoutError};
Expand All @@ -21,7 +21,7 @@ use std::time::{Duration, Instant};
use raft::prelude::*;
use raft::storage::MemStorage;

type ProposeCallback = Box<Fn() + Send>;
type ProposeCallback = Box<dyn Fn() + Send>;

enum Msg {
Propose {
Expand Down Expand Up @@ -196,7 +196,8 @@ fn send_propose(sender: mpsc::Sender<Msg>) {
cb: Box::new(move || {
s1.send(0).unwrap();
}),
}).unwrap();
})
.unwrap();

let n = r1.recv().unwrap();
assert_eq!(n, 0);
Expand Down
13 changes: 0 additions & 13 deletions generate-proto.sh

This file was deleted.

Loading