From ca9dc9b991c810154cfba89007da1e7a12c72656 Mon Sep 17 00:00:00 2001 From: Jay Date: Tue, 15 May 2018 00:28:45 +0800 Subject: [PATCH] *: update raft (#3069) --- Cargo.lock | 61 ++++++++++++++++++++--------- Cargo.toml | 3 +- src/raftstore/store/mod.rs | 7 ++++ src/raftstore/store/peer.rs | 21 +++++++--- src/raftstore/store/peer_storage.rs | 9 +++++ src/server/debug.rs | 2 +- 6 files changed, 76 insertions(+), 27 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 070a3084820..ded32bf7cc6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -48,7 +48,7 @@ version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "backtrace-sys 0.1.15 (registry+https://github.com/rust-lang/crates.io-index)", - "cfg-if 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "cfg-if 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", "dbghelp-sys 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)", @@ -125,7 +125,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "cfg-if" -version = "0.1.0" +version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] @@ -223,7 +223,7 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "arrayvec 0.4.7 (registry+https://github.com/rust-lang/crates.io-index)", - "cfg-if 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "cfg-if 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", "crossbeam-utils 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", "memoffset 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -235,7 +235,7 @@ name = "crossbeam-utils" version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "cfg-if 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "cfg-if 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -315,6 +315,14 @@ dependencies = [ "num_cpus 1.6.2 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "fxhash" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "byteorder 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "gcc" version = "0.3.53" @@ -459,12 +467,12 @@ dependencies = [ [[package]] name = "kvproto" version = "0.0.1" -source = "git+https://github.com/pingcap/kvproto.git#05b1d9675489f16f81b7c950ed039cf5fdc93055" +source = "git+https://github.com/pingcap/kvproto.git?branch=release-2.0#d8452b05c2b086005e523f29cd432f391cb4716e" dependencies = [ "futures 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)", "grpcio 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", "protobuf 1.4.1 (registry+https://github.com/rust-lang/crates.io-index)", - "raft 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "raft 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -537,6 +545,14 @@ name = "log" version = "0.3.6" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "log" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "cfg-if 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "lz4-sys" version = "1.8.0" @@ -655,7 +671,7 @@ name = "net2" version = "0.2.23" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "cfg-if 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "cfg-if 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)", "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", @@ -677,7 +693,7 @@ version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "bitflags 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)", - "cfg-if 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "cfg-if 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)", "void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -786,7 +802,7 @@ name = "prometheus" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "cfg-if 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "cfg-if 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", "fnv 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)", "hyper 0.9.18 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", @@ -822,6 +838,11 @@ name = "quick-error" version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "quick-error" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "quine-mc_cluskey" version = "0.2.2" @@ -834,14 +855,13 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "raft" -version = "0.1.0" +version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "flat_map 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)", - "fnv 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)", - "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", + "fxhash 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", "protobuf 1.4.1 (registry+https://github.com/rust-lang/crates.io-index)", - "quick-error 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "quick-error 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -1156,7 +1176,7 @@ dependencies = [ "grpcio 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", "indexmap 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", "jemallocator 0.1.0 (git+https://github.com/busyjay/jemallocator.git?branch=dev)", - "kvproto 0.0.1 (git+https://github.com/pingcap/kvproto.git)", + "kvproto 0.0.1 (git+https://github.com/pingcap/kvproto.git?branch=release-2.0)", "lazy_static 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1166,7 +1186,7 @@ dependencies = [ "prometheus 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "protobuf 1.4.1 (registry+https://github.com/rust-lang/crates.io-index)", "quick-error 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", - "raft 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "raft 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)", "regex 0.1.70 (registry+https://github.com/rust-lang/crates.io-index)", "rocksdb 0.3.0 (git+https://github.com/pingcap/rust-rocksdb.git)", @@ -1445,7 +1465,7 @@ dependencies = [ "checksum bytes 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "46112a0060ae15e3a3f9a445428a53e082b91215b744fa27a1948842f4a64b96" "checksum bzip2-sys 0.1.6 (git+https://github.com/alexcrichton/bzip2-rs.git)" = "" "checksum cc 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a9b13a57efd6b30ecd6598ebdb302cca617930b5470647570468a65d12ef9719" -"checksum cfg-if 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "de1e760d7b6535af4241fca8bd8adf68e2e7edacc6b29f5d399050c5e48cf88c" +"checksum cfg-if 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "405216fd8fe65f718daa7102ea808a946b6ce40c742998fbfd3463645552de18" "checksum chrono 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7c20ebe0b2b08b0aeddba49c609fe7957ba2e33449882cb186a180bc60682fa9" "checksum clap 2.23.3 (registry+https://github.com/rust-lang/crates.io-index)" = "f57e9b63057a545ad2ecd773ea61e49422ed1b1d63d74d5da5ecaee55b3396cd" "checksum clippy 0.0.192 (registry+https://github.com/rust-lang/crates.io-index)" = "6d5b9c448ab03cade9cf3c15b80ebb282c996eade63a4e0eadb06dd3ca08168e" @@ -1467,6 +1487,7 @@ dependencies = [ "checksum fuchsia-zircon-sys 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7" "checksum futures 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)" = "118b49cac82e04121117cbd3121ede3147e885627d82c4546b87c702debb90c1" "checksum futures-cpupool 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "ab90cde24b3319636588d0c35fe03b1333857621051837ed769faefb4c2162e4" +"checksum fxhash 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "c31b6d751ae2c7f11320402d34e41349dd1016f8d5d45e48c4312bc8625af50c" "checksum gcc 0.3.53 (registry+https://github.com/rust-lang/crates.io-index)" = "e8310f7e9c890398b0e80e301c4f474e9918d2b27fca8f48486ca775fa9ffc5a" "checksum getopts 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)" = "d9047cfbd08a437050b363d35ef160452c5fe8ea5187ae0a624708c91581d685" "checksum glob 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "8be18de09a56b60ed0edf84bc9df007e30040691af7acd1c41874faac5895bfb" @@ -1484,7 +1505,7 @@ dependencies = [ "checksum jemalloc-sys 0.1.0 (git+https://github.com/busyjay/jemallocator.git?branch=dev)" = "" "checksum jemallocator 0.1.0 (git+https://github.com/busyjay/jemallocator.git?branch=dev)" = "" "checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d" -"checksum kvproto 0.0.1 (git+https://github.com/pingcap/kvproto.git)" = "" +"checksum kvproto 0.0.1 (git+https://github.com/pingcap/kvproto.git?branch=release-2.0)" = "" "checksum language-tags 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "a91d884b6667cd606bb5a69aa0c99ba811a115fc68915e7056ec08a46e93199a" "checksum lazy_static 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)" = "cf186d1a8aa5f5bee5fd662bc9c1b949e0259e1bcc379d1f006847b0080c7417" "checksum lazy_static 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "3b37545ab726dd833ec6420aaba8231c5b320814b9029ad585555d2a03e94fbf" @@ -1495,6 +1516,7 @@ dependencies = [ "checksum librocksdb_sys 0.1.0 (git+https://github.com/pingcap/rust-rocksdb.git)" = "" "checksum libz-sys 1.0.18 (git+https://github.com/busyjay/libz-sys.git?branch=static-link)" = "" "checksum log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "ab83497bf8bf4ed2a74259c1c802351fcd67a65baa86394b6ba73c36f4838054" +"checksum log 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "89f010e843f2b1a31dbd316b3b8d443758bc634bed37aabade59c686d644e0a2" "checksum lz4-sys 1.8.0 (git+https://github.com/busyjay/lz4-rs.git?branch=adjust-build)" = "" "checksum matches 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "15305656809ce5a4805b1ff2946892810992197ce1270ff79baded852187942e" "checksum memchr 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)" = "d8b629fb514376c675b98c1421e80b151d3817ac42d7c667717d282761418d20" @@ -1528,9 +1550,10 @@ dependencies = [ "checksum protobuf 1.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "568a15e4d572d9a5e63ae3a55f84328c984842887db179b40b4cc6a608bac6a4" "checksum pulldown-cmark 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "d6fdf85cda6cadfae5428a54661d431330b312bc767ddbc57adbedc24da66e32" "checksum quick-error 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7ac990ab4e038dd8481a5e3fd00641067fcfc674ad663f3222752ed5284e05d4" +"checksum quick-error 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "eda5fe9b71976e62bc81b781206aaa076401769b2143379d3eb2118388babac4" "checksum quine-mc_cluskey 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "a6683b0e23d80813b1a535841f0048c1537d3f86d63c999e8373b39a9b0eb74a" "checksum quote 0.3.12 (registry+https://github.com/rust-lang/crates.io-index)" = "e7b44fd83db28b83c1c58187159934906e5e955c812e211df413b76b03c909a5" -"checksum raft 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f3730c9e245b3e1115be47304ae5359b4be9e8f03166549fef7a732097568138" +"checksum raft 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "833b9bacf973f6b77ff1be6bd5024a25eae15504bec8cb7efe4caa784adc859b" "checksum rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)" = "2791d88c6defac799c3f20d74f094ca33b9332612d9aef9078519c82e4fe04a5" "checksum rand 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "eba5f8cb59cc50ed56be8880a5c7b496bfd9bd26394e176bc67884094145c2c5" "checksum redox_syscall 0.1.27 (registry+https://github.com/rust-lang/crates.io-index)" = "80dcf663dc552529b9bfc7bdb30ea12e5fa5d3545137d850a91ad410053f68e9" diff --git a/Cargo.toml b/Cargo.toml index 41ab1ec770c..fe1a46ffcae 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -73,7 +73,7 @@ bitflags = "1.0.1" fail = "0.2" uuid = { version = "0.4", features = [ "serde", "v4" ] } grpcio = { version = "0.2", features = [ "secure" ] } -raft = "0.1" +raft = "0.2" crossbeam-channel = { version = "0.1", features = [ "nightly" ] } [target.'cfg(unix)'.dependencies] @@ -87,6 +87,7 @@ git = "https://github.com/pingcap/rust-rocksdb.git" [dependencies.kvproto] git = "https://github.com/pingcap/kvproto.git" +branch = "release-2.0" [dependencies.tipb] git = "https://github.com/pingcap/tipb.git" diff --git a/src/raftstore/store/mod.rs b/src/raftstore/store/mod.rs index e3f28d30cf7..e8b8cd733dd 100644 --- a/src/raftstore/store/mod.rs +++ b/src/raftstore/store/mod.rs @@ -50,3 +50,10 @@ pub use self::transport::Transport; // Only used in tests #[cfg(test)] pub use self::worker::{SplitCheckRunner, SplitCheckTask}; + +bitflags! { + // TODO: maybe declare it as protobuf struct is better. + struct ProposalContext: u8 { + const SYNC_LOG = 0b00000001; + } +} diff --git a/src/raftstore/store/peer.rs b/src/raftstore/store/peer.rs index 22acf9a95b8..83f20da0a6f 100644 --- a/src/raftstore/store/peer.rs +++ b/src/raftstore/store/peer.rs @@ -35,11 +35,11 @@ use raftstore::coprocessor::CoprocessorHost; use raftstore::store::worker::apply::ExecResult; use raftstore::store::worker::{Apply, ApplyRes, ApplyTask}; use raftstore::store::worker::{apply, Proposal, RegionProposal}; -use raftstore::store::{Callback, Config, ReadResponse, RegionSnapshot}; +use raftstore::store::{Callback, Config, ProposalContext, ReadResponse, RegionSnapshot}; use raftstore::{Error, Result}; use util::MustConsumeVec; -use util::collections::{FlatMap, FlatMapValues as Values, HashSet}; +use util::collections::{FlatMap, HashSet}; use util::time::{duration_to_sec, monotonic_raw_now}; use util::worker::{FutureWorker, Scheduler}; @@ -324,7 +324,7 @@ impl Peer { ..Default::default() }; - let raft_group = RawNode::new(&raft_cfg, ps, &[])?; + let raft_group = RawNode::new(&raft_cfg, ps, vec![])?; let mut peer = Peer { kv_engine: store.kv_engine(), @@ -1229,7 +1229,10 @@ impl Peer { /// 2. it's a follower, and it does not lag behind the leader a lot. /// If a snapshot is involved between it and the Raft leader, it's not healthy since /// it cannot works as a node in the quorum to receive replicating logs from leader. - fn count_healthy_node(&self, progress: Values) -> usize { + fn count_healthy_node<'a, I>(&self, progress: I) -> usize + where + I: Iterator, + { let mut healthy = 0; for pr in progress { if pr.matched >= self.get_store().truncated_index() { @@ -1519,7 +1522,12 @@ impl Peer { let sync_log = get_sync_log_from_request(&req); let propose_index = self.next_proposal_index(); - self.raft_group.propose(data, sync_log)?; + let context = if !sync_log { + vec![] + } else { + vec![ProposalContext::SYNC_LOG.bits()] + }; + self.raft_group.propose(context, data)?; if self.next_proposal_index() == propose_index { // The message is dropped silently, this usually due to leader absence // or transferring leader. Both cases can be considered as NotLeader error. @@ -1598,7 +1606,8 @@ impl Peer { ); let propose_index = self.next_proposal_index(); - self.raft_group.propose_conf_change(cc)?; + self.raft_group + .propose_conf_change(vec![ProposalContext::SYNC_LOG.bits()], cc)?; if self.next_proposal_index() == propose_index { // The message is dropped silently, this usually due to leader absence // or transferring leader. Both cases can be considered as NotLeader error. diff --git a/src/raftstore/store/peer_storage.rs b/src/raftstore/store/peer_storage.rs index 65b16e4ca31..72a5d260026 100644 --- a/src/raftstore/store/peer_storage.rs +++ b/src/raftstore/store/peer_storage.rs @@ -34,6 +34,7 @@ use kvproto::raft_serverpb::{MergeState, PeerState, RaftApplyState, RaftLocalSta RaftSnapshotData, RegionLocalState}; use raft::eraftpb::{ConfState, Entry, HardState, Snapshot}; use raft::{self, Error as RaftError, RaftState, Ready, Storage, StorageError}; +use raftstore::store::ProposalContext; use raftstore::store::util::conf_state_from_region; use raftstore::{Error, Result}; use storage::CF_RAFT; @@ -763,6 +764,14 @@ impl PeerStorage { for entry in entries { if entry.get_sync_log() { ready_ctx.sync_log = true; + } else { + let ctx = entry.get_context(); + if !ctx.is_empty() { + let ctx = ProposalContext::from_bits_truncate(ctx[0]); + if ctx.contains(ProposalContext::SYNC_LOG) { + ready_ctx.sync_log = true; + } + } } ready_ctx.raft_wb.put_msg( &keys::raft_log_key(self.get_region_id(), entry.get_index()), diff --git a/src/server/debug.rs b/src/server/debug.rs index afb2ec3c031..f80b3a905a3 100644 --- a/src/server/debug.rs +++ b/src/server/debug.rs @@ -360,7 +360,7 @@ impl Debugger { ..Default::default() }; - box_try!(RawNode::new(&raft_cfg, peer_storage, &[])); + box_try!(RawNode::new(&raft_cfg, peer_storage, vec![])); Ok(()) };