From 75bc583ddeda8058ddac377ea3ccf887b0973bce Mon Sep 17 00:00:00 2001 From: Fullstop000 Date: Wed, 17 Jun 2020 11:52:01 +0800 Subject: [PATCH] rm mut self ref in config.validate Signed-off-by: Fullstop000 --- benches/suites/raft.rs | 2 +- benches/suites/raw_node.rs | 2 +- examples/five_mem_node/main.rs | 4 +- examples/single_mem_node/main.rs | 2 +- harness/src/network.rs | 2 +- harness/tests/integration_cases/test_raft.rs | 26 ++++++------ .../integration_cases/test_raft_paper.rs | 14 +++---- .../tests/integration_cases/test_raw_node.rs | 10 ++--- harness/tests/test_util/mod.rs | 10 ++--- src/config.rs | 17 +++----- src/lib.rs | 40 +++++++++---------- src/raft.rs | 4 +- src/raw_node.rs | 4 +- 13 files changed, 65 insertions(+), 72 deletions(-) diff --git a/benches/suites/raft.rs b/benches/suites/raft.rs index e7f4c0eaf..3d3fc5463 100644 --- a/benches/suites/raft.rs +++ b/benches/suites/raft.rs @@ -13,7 +13,7 @@ fn quick_raft(voters: usize, learners: usize, logger: &slog::Logger) -> Raft RawNode { let conf_state = ConfState::from((vec![1], vec![])); let storage = MemStorage::new_with_conf_state(conf_state); let config = Config::new(id); - RawNode::new(config, storage, logger).unwrap() + RawNode::new(&config, storage, logger).unwrap() } pub fn bench_raw_node_new(c: &mut Criterion) { diff --git a/examples/five_mem_node/main.rs b/examples/five_mem_node/main.rs index 4b0ca5690..5af0264c7 100644 --- a/examples/five_mem_node/main.rs +++ b/examples/five_mem_node/main.rs @@ -183,7 +183,7 @@ impl Node { s.mut_metadata().mut_conf_state().voters = vec![1]; let storage = MemStorage::new(); storage.wl().apply_snapshot(s).unwrap(); - let raft_group = Some(RawNode::new(cfg, storage, &logger).unwrap()); + let raft_group = Some(RawNode::new(&cfg, storage, &logger).unwrap()); Node { raft_group, my_mailbox, @@ -214,7 +214,7 @@ impl Node { cfg.id = msg.to; let logger = logger.new(o!("tag" => format!("peer_{}", msg.to))); let storage = MemStorage::new(); - self.raft_group = Some(RawNode::new(cfg, storage, &logger).unwrap()); + self.raft_group = Some(RawNode::new(&cfg, storage, &logger).unwrap()); } // Step a raft message, initialize the raft if need. diff --git a/examples/single_mem_node/main.rs b/examples/single_mem_node/main.rs index c8764869a..c88beab78 100644 --- a/examples/single_mem_node/main.rs +++ b/examples/single_mem_node/main.rs @@ -64,7 +64,7 @@ fn main() { }; // Create the Raft node. - let mut r = RawNode::new(cfg, storage, &logger).unwrap(); + let mut r = RawNode::new(&cfg, storage, &logger).unwrap(); let (sender, receiver) = mpsc::channel(); diff --git a/harness/src/network.rs b/harness/src/network.rs index 6b6871b70..ffa130bce 100644 --- a/harness/src/network.rs +++ b/harness/src/network.rs @@ -95,7 +95,7 @@ impl Network { nstorage.insert(*id, store.clone()); let mut config = config.clone(); config.id = *id; - let r = Raft::new(config, store, l).unwrap().into(); + let r = Raft::new(&config, store, l).unwrap().into(); npeers.insert(*id, r); } Some(r) => { diff --git a/harness/tests/integration_cases/test_raft.rs b/harness/tests/integration_cases/test_raft.rs index fe5362724..f111cbbf0 100644 --- a/harness/tests/integration_cases/test_raft.rs +++ b/harness/tests/integration_cases/test_raft.rs @@ -539,7 +539,7 @@ fn test_progress_flow_control() { cfg.max_inflight_msgs = 3; cfg.max_size_per_msg = 2048; let s = MemStorage::new_with_conf_state((vec![1, 2], vec![])); - let mut r = new_test_raft_with_config(cfg, s, &l); + let mut r = new_test_raft_with_config(&cfg, s, &l); r.become_candidate(); r.become_leader(); @@ -1388,7 +1388,7 @@ fn test_commit() { hs.term = sm_term; store.wl().set_hardstate(hs); let cfg = new_test_config(1, 5, 1); - let mut sm = new_test_raft_with_config(cfg, store, &l); + let mut sm = new_test_raft_with_config(&cfg, store, &l); for (j, v) in matches.iter().enumerate() { let id = j as u64 + 1; @@ -1533,7 +1533,7 @@ fn test_handle_heartbeat() { .append(&[empty_entry(1, 1), empty_entry(2, 2), empty_entry(3, 3)]) .unwrap(); let cfg = new_test_config(1, 5, 1); - let mut sm = new_test_raft_with_config(cfg, store, &l); + let mut sm = new_test_raft_with_config(&cfg, store, &l); sm.become_follower(2, 2); sm.raft_log.commit_to(commit); sm.handle_heartbeat(m); @@ -2676,7 +2676,7 @@ fn test_read_only_for_new_leader() { if compact_index != 0 { storage.wl().compact(compact_index).unwrap(); } - let i = new_test_raft_with_config(cfg, storage, &l); + let i = new_test_raft_with_config(&cfg, storage, &l); peers.push(Some(i)); } let mut nt = Network::new(peers, &l); @@ -3657,11 +3657,11 @@ fn test_leader_transfer_to_learner() { let l = default_logger(); let s = MemStorage::new_with_conf_state((vec![1], vec![2])); let c = new_test_config(1, 10, 1); - let leader = new_test_raft_with_config(c, s, &l); + let leader = new_test_raft_with_config(&c, s, &l); let s = MemStorage::new_with_conf_state((vec![1], vec![2])); let c = new_test_config(2, 10, 1); - let learner = new_test_raft_with_config(c, s, &l); + let learner = new_test_raft_with_config(&c, s, &l); let mut nt = Network::new(vec![Some(leader), Some(learner)], &l); nt.send(vec![new_message(1, 1, MessageType::MsgHup, 0)]); @@ -3938,7 +3938,7 @@ pub fn new_test_learner_raft( storage.initialize_with_conf_state((peers, learners)); } let cfg = new_test_config(id, election, heartbeat); - new_test_raft_with_config(cfg, storage, logger) + new_test_raft_with_config(&cfg, storage, logger) } // TestLearnerElectionTimeout verfies that the leader should not start election @@ -4372,7 +4372,7 @@ fn test_election_tick_range() { let l = default_logger(); let mut cfg = new_test_config(1, 10, 1); let s = MemStorage::new_with_conf_state((vec![1, 2, 3], vec![])); - let mut raft = new_test_raft_with_config(cfg.clone(), s, &l).raft.unwrap(); + let mut raft = new_test_raft_with_config(&cfg, s, &l).raft.unwrap(); for _ in 0..1000 { raft.reset_randomized_election_timeout(); let randomized_timeout = raft.randomized_election_timeout(); @@ -4394,7 +4394,7 @@ fn test_election_tick_range() { cfg.validate().unwrap_err(); cfg.max_election_tick = cfg.election_tick + 1; - raft = new_test_raft_with_config(cfg.clone(), new_storage(), &l) + raft = new_test_raft_with_config(&cfg, new_storage(), &l) .raft .unwrap(); for _ in 0..100 { @@ -4462,7 +4462,7 @@ fn test_prevote_with_check_quorum() { cfg.pre_vote = true; cfg.check_quorum = true; let s = MemStorage::new_with_conf_state((vec![1, 2, 3], vec![])); - let mut i = new_test_raft_with_config(cfg, s, &l); + let mut i = new_test_raft_with_config(&cfg, s, &l); i.become_follower(1, INVALID_ID); i }; @@ -4527,7 +4527,7 @@ fn test_prevote_with_check_quorum() { fn test_new_raft_with_bad_config_errors() { let invalid_config = new_test_config(INVALID_ID, 1, 1); let s = MemStorage::new_with_conf_state((vec![1, 2], vec![])); - let raft = Raft::new(invalid_config, s, &default_logger()); + let raft = Raft::new(&invalid_config, s, &default_logger()); assert!(raft.is_err()) } @@ -4947,7 +4947,7 @@ fn test_group_commit() { hs.term = 1; store.wl().set_hardstate(hs); let cfg = new_test_config(1, 5, 1); - let mut sm = new_test_raft_with_config(cfg, store, &l); + let mut sm = new_test_raft_with_config(&cfg, store, &l); let mut groups = vec![]; for (j, (m, g)) in matches.into_iter().zip(group_ids).enumerate() { @@ -5075,7 +5075,7 @@ fn test_group_commit_consistent() { store.wl().set_hardstate(hs); let mut cfg = new_test_config(1, 5, 1); cfg.applied = applied; - let mut sm = new_test_raft_with_config(cfg, store, &l); + let mut sm = new_test_raft_with_config(&cfg, store, &l); sm.state = role; let mut groups = vec![]; diff --git a/harness/tests/integration_cases/test_raft_paper.rs b/harness/tests/integration_cases/test_raft_paper.rs index 320b818ac..4ed6cafed 100644 --- a/harness/tests/integration_cases/test_raft_paper.rs +++ b/harness/tests/integration_cases/test_raft_paper.rs @@ -549,7 +549,7 @@ fn test_leader_commit_preceding_entries() { let store = MemStorage::new_with_conf_state((vec![1, 2, 3], vec![])); store.wl().append(&tt).unwrap(); let cfg = new_test_config(1, 10, 1); - new_test_raft_with_config(cfg, store, &l) + new_test_raft_with_config(&cfg, store, &l) }; r.load_state(&hard_state(2, 0, 0)); r.become_candidate(); @@ -662,7 +662,7 @@ fn test_follower_check_msg_append() { let store = MemStorage::new_with_conf_state((vec![1, 2, 3], vec![])); store.wl().append(&ents).unwrap(); let cfg = new_test_config(1, 10, 1); - new_test_raft_with_config(cfg, store, &l) + new_test_raft_with_config(&cfg, store, &l) }; r.load_state(&hard_state(0, 1, 0)); r.become_follower(2, 2); @@ -735,7 +735,7 @@ fn test_follower_append_entries() { .append(&[empty_entry(1, 1), empty_entry(2, 2)]) .unwrap(); let cfg = new_test_config(1, 10, 1); - new_test_raft_with_config(cfg, store, &l) + new_test_raft_with_config(&cfg, store, &l) }; r.become_follower(2, 2); @@ -854,7 +854,7 @@ fn test_leader_sync_follower_log() { let store = MemStorage::new_with_conf_state((vec![1, 2, 3], vec![])); store.wl().append(&ents).unwrap(); let cfg = new_test_config(1, 10, 1); - new_test_raft_with_config(cfg, store, &l) + new_test_raft_with_config(&cfg, store, &l) }; let last_index = lead.raft_log.last_index(); lead.load_state(&hard_state(term, last_index, 0)); @@ -863,7 +863,7 @@ fn test_leader_sync_follower_log() { let store = MemStorage::new_with_conf_state((vec![1, 2, 3], vec![])); store.wl().append(&tt).unwrap(); let cfg = new_test_config(2, 10, 1); - new_test_raft_with_config(cfg, store, &l) + new_test_raft_with_config(&cfg, store, &l) }; follower.load_state(&hard_state(term - 1, 0, 0)); @@ -973,7 +973,7 @@ fn test_voter() { let s = MemStorage::new_with_conf_state((vec![1, 2], vec![])); s.wl().append(&ents).unwrap(); let cfg = new_test_config(1, 10, 1); - let mut r = new_test_raft_with_config(cfg, s, &l); + let mut r = new_test_raft_with_config(&cfg, s, &l); let mut m = new_message(2, 1, MessageType::MsgRequestVote, 0); m.term = 3; @@ -1018,7 +1018,7 @@ fn test_leader_only_commits_log_from_current_term() { let store = MemStorage::new_with_conf_state((vec![1, 2], vec![])); store.wl().append(&ents).unwrap(); let cfg = new_test_config(1, 10, 1); - new_test_raft_with_config(cfg, store, &l) + new_test_raft_with_config(&cfg, store, &l) }; r.load_state(&hard_state(2, 0, 0)); diff --git a/harness/tests/integration_cases/test_raw_node.rs b/harness/tests/integration_cases/test_raw_node.rs index 0ea78e0be..51ab61141 100644 --- a/harness/tests/integration_cases/test_raw_node.rs +++ b/harness/tests/integration_cases/test_raw_node.rs @@ -66,7 +66,7 @@ fn new_raw_node( .apply_snapshot(new_snapshot(1, 1, peers)) .unwrap(); } - RawNode::new(config, storage, logger).unwrap() + RawNode::new(&config, storage, logger).unwrap() } // test_raw_node_step ensures that RawNode.Step ignore local message. @@ -392,7 +392,7 @@ fn test_raw_node_restart_from_snapshot() { store.wl().apply_snapshot(snap).unwrap(); store.wl().append(&entries).unwrap(); store.wl().set_hardstate(hard_state(1, 3, 0)); - RawNode::new(new_test_config(1, 10, 1), store, &l).unwrap() + RawNode::new(&new_test_config(1, 10, 1), store, &l).unwrap() }; let rd = raw_node.ready(); @@ -409,7 +409,7 @@ fn test_skip_bcast_commit() { let mut config = new_test_config(1, 10, 1); config.skip_bcast_commit = true; let s = MemStorage::new_with_conf_state((vec![1, 2, 3], vec![])); - let r1 = new_test_raft_with_config(config, s, &l); + let r1 = new_test_raft_with_config(&config, s, &l); let r2 = new_test_raft(2, vec![1, 2, 3], 10, 1, new_storage(), &l); let r3 = new_test_raft(3, vec![1, 2, 3], 10, 1, new_storage(), &l); let mut nt = Network::new(vec![Some(r1), Some(r2), Some(r3)], &l); @@ -537,7 +537,7 @@ fn test_commit_pagination() { let storage = MemStorage::new_with_conf_state((vec![1], vec![])); let mut config = new_test_config(1, 10, 1); config.max_committed_size_per_ready = 2048; - let mut raw_node = RawNode::new(config, storage, &l).unwrap(); + let mut raw_node = RawNode::new(&config, storage, &l).unwrap(); raw_node.campaign().unwrap(); let rd = raw_node.ready(); let committed_len = rd.committed_entries.as_ref().unwrap().len(); @@ -617,7 +617,7 @@ fn test_commit_pagination_after_restart() { .wl() .append(&[new_entry(1, 11, Some("boom"))]) .unwrap(); - let mut raw_node = RawNode::with_default_logger(cfg, s).unwrap(); + let mut raw_node = RawNode::with_default_logger(&cfg, s).unwrap(); let mut highest_applied = 0; while highest_applied != 11 { let rd = raw_node.ready(); diff --git a/harness/tests/test_util/mod.rs b/harness/tests/test_util/mod.rs index 84d569d09..358358d8e 100644 --- a/harness/tests/test_util/mod.rs +++ b/harness/tests/test_util/mod.rs @@ -62,7 +62,7 @@ pub fn new_test_raft( if !peers.is_empty() && !storage.initial_state().unwrap().initialized() { storage.initialize_with_conf_state((peers, vec![])); } - new_test_raft_with_config(config, storage, l) + new_test_raft_with_config(&config, storage, l) } pub fn new_test_raft_with_prevote( @@ -82,7 +82,7 @@ pub fn new_test_raft_with_prevote( if !peers.is_empty() && !storage.initial_state().unwrap().initialized() { storage.initialize_with_conf_state((peers, vec![])); } - new_test_raft_with_config(config, storage, l) + new_test_raft_with_config(&config, storage, l) } pub fn new_test_raft_with_logs( @@ -102,11 +102,11 @@ pub fn new_test_raft_with_logs( storage.initialize_with_conf_state((peers, vec![])); } storage.wl().append(logs).unwrap(); - new_test_raft_with_config(config, storage, l) + new_test_raft_with_config(&config, storage, l) } -pub fn new_test_raft_with_config(config: Config, storage: MemStorage, l: &Logger) -> Interface { - Interface::new(Raft::new(config, storage, l).unwrap()) +pub fn new_test_raft_with_config(config: &Config, storage: MemStorage, l: &Logger) -> Interface { + Interface::new(Raft::new(&config, storage, l).unwrap()) } pub fn hard_state(t: u64, c: u64, v: u64) -> HardState { diff --git a/src/config.rs b/src/config.rs index eb6c4d8ef..beab90362 100644 --- a/src/config.rs +++ b/src/config.rs @@ -17,7 +17,7 @@ pub use super::read_only::{ReadOnlyOption, ReadState}; use super::{ errors::{Error, Result}, - INVALID_ID, + INVALID_ID, NO_LIMIT, }; /// Config contains the parameters to start a raft. @@ -51,9 +51,8 @@ pub struct Config { /// Note: raft::NO_LIMIT for unlimited, 0 for at most one entry per message. pub max_size_per_msg: u64, - /// MaxCommittedSizePerReady limits the size of the committed entries which - /// can be applied. - /// If not set, this is same as `max_size_per_msg` + /// max_committed_size_per_ready limits the size of the committed entries which + /// can be applied. Use `NO_LIMIT` as default to keep backword compatible. pub max_committed_size_per_ready: u64, /// Limit the max number of in-flight append messages during optimistic @@ -106,7 +105,7 @@ impl Default for Config { heartbeat_tick: HEARTBEAT_TICK, applied: 0, max_size_per_msg: 0, - max_committed_size_per_ready: 0, + max_committed_size_per_ready: NO_LIMIT, max_inflight_msgs: 256, check_quorum: false, pre_vote: false, @@ -150,7 +149,7 @@ impl Config { } /// Runs validations against the config. - pub fn validate(&mut self) -> Result<()> { + pub fn validate(&self) -> Result<()> { if self.id == INVALID_ID { return Err(Error::ConfigInvalid("invalid node id".to_owned())); } @@ -183,12 +182,6 @@ impl Config { ))); } - // default MaxCommittedSizePerReady to MaxSizePerMsg because they were - // previously the same parameter. - if self.max_committed_size_per_ready == 0 { - self.max_committed_size_per_ready = self.max_size_per_msg - } - if self.max_inflight_msgs == 0 { return Err(Error::ConfigInvalid( "max inflight messages must be greater than 0".to_owned(), diff --git a/src/lib.rs b/src/lib.rs index 5ddb8541f..653b71682 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -32,7 +32,7 @@ use raft::{ use slog::{Drain, o}; // Select some defaults, then change what we need. -let mut config = Config { +let config = Config { id: 1, ..Default::default() }; @@ -44,7 +44,7 @@ config.validate().unwrap(); // We'll use the built-in `MemStorage`, but you will likely want your own. // Finally, create our Raft node! let storage = MemStorage::new_with_conf_state((vec![1], vec![])); -let mut node = RawNode::new(config, storage, &logger).unwrap(); +let mut node = RawNode::new(&config, storage, &logger).unwrap(); // We will coax it into being the lead of a single node cluster for exploration. node.raft.become_candidate(); node.raft.become_leader(); @@ -62,7 +62,7 @@ channel `recv_timeout` to drive the Raft node at least every 100ms, calling # let config = Config { id: 1, ..Default::default() }; # let store = MemStorage::new_with_conf_state((vec![1], vec![])); # let logger = slog::Logger::root(slog_stdlog::StdLog.fuse(), o!()); -# let mut node = RawNode::new(config, store, &logger).unwrap(); +# let mut node = RawNode::new(&config, store, &logger).unwrap(); # node.raft.become_candidate(); # node.raft.become_leader(); use std::{sync::mpsc::{channel, RecvTimeoutError}, time::{Instant, Duration}}; @@ -129,7 +129,7 @@ Here is a simple example to use `propose` and `step`: # let config = Config { id: 1, ..Default::default() }; # let store = MemStorage::new_with_conf_state((vec![1], vec![])); # let logger = slog::Logger::root(slog_stdlog::StdLog.fuse(), o!()); -# let mut node = RawNode::new(config, store, &logger).unwrap(); +# let mut node = RawNode::new(&config, store, &logger).unwrap(); # node.raft.become_candidate(); # node.raft.become_leader(); # @@ -188,11 +188,11 @@ state: # use slog::{Drain, o}; # use raft::{Config, storage::MemStorage, raw_node::RawNode}; # -# let mut config = Config { id: 1, ..Default::default() }; +# let config = Config { id: 1, ..Default::default() }; # config.validate().unwrap(); # let store = MemStorage::new_with_conf_state((vec![1], vec![])); # let logger = slog::Logger::root(slog_stdlog::StdLog.fuse(), o!()); -# let mut node = RawNode::new(config, store, &logger).unwrap(); +# let mut node = RawNode::new(&config, store, &logger).unwrap(); # if !node.has_ready() { return; @@ -212,11 +212,11 @@ a Raft snapshot from the leader and we must apply the snapshot: # use slog::{Drain, o}; # use raft::{Config, storage::MemStorage, raw_node::RawNode}; # - # let mut config = Config { id: 1, ..Default::default() }; + # let config = Config { id: 1, ..Default::default() }; # config.validate().unwrap(); # let store = MemStorage::new_with_conf_state((vec![1], vec![])); # let logger = slog::Logger::root(slog_stdlog::StdLog.fuse(), o!()); - # let mut node = RawNode::new(config, store, &logger).unwrap(); + # let mut node = RawNode::new(&config, store, &logger).unwrap(); # # if !node.has_ready() { # return; @@ -240,11 +240,11 @@ entries but has not been committed yet, we must append the entries to the Raft l # use slog::{Drain, o}; # use raft::{Config, storage::MemStorage, raw_node::RawNode}; # - # let mut config = Config { id: 1, ..Default::default() }; + # let config = Config { id: 1, ..Default::default() }; # config.validate().unwrap(); # let store = MemStorage::new_with_conf_state((vec![1], vec![])); # let logger = slog::Logger::root(slog_stdlog::StdLog.fuse(), o!()); - # let mut node = RawNode::new(config, store, &logger).unwrap(); + # let mut node = RawNode::new(&config, store, &logger).unwrap(); # # if !node.has_ready() { # return; @@ -266,11 +266,11 @@ We must persist the changed `HardState`: # use slog::{Drain, o}; # use raft::{Config, storage::MemStorage, raw_node::RawNode}; # - # let mut config = Config { id: 1, ..Default::default() }; + # let config = Config { id: 1, ..Default::default() }; # config.validate().unwrap(); # let store = MemStorage::new_with_conf_state((vec![1], vec![])); # let logger = slog::Logger::root(slog_stdlog::StdLog.fuse(), o!()); - # let mut node = RawNode::new(config, store, &logger).unwrap(); + # let mut node = RawNode::new(&config, store, &logger).unwrap(); # # if !node.has_ready() { # return; @@ -292,11 +292,11 @@ messages to the leader after appending the Raft entries: # use slog::{Drain, o}; # use raft::{Config, storage::MemStorage, raw_node::RawNode, StateRole}; # - # let mut config = Config { id: 1, ..Default::default() }; + # let config = Config { id: 1, ..Default::default() }; # config.validate().unwrap(); # let store = MemStorage::new_with_conf_state((vec![1], vec![])); # let logger = slog::Logger::root(slog_stdlog::StdLog.fuse(), o!()); - # let mut node = RawNode::new(config, store, &logger).unwrap(); + # let mut node = RawNode::new(&config, store, &logger).unwrap(); # # if !node.has_ready() { # return; @@ -322,11 +322,11 @@ need to update the applied index and resume `apply` later: # use slog::{Drain, o}; # use raft::{Config, storage::MemStorage, raw_node::RawNode, eraftpb::EntryType}; # - # let mut config = Config { id: 1, ..Default::default() }; + # let config = Config { id: 1, ..Default::default() }; # config.validate().unwrap(); # let store = MemStorage::new_with_conf_state((vec![1], vec![])); # let logger = slog::Logger::root(slog_stdlog::StdLog.fuse(), o!()); - # let mut node = RawNode::new(config, store, &logger).unwrap(); + # let mut node = RawNode::new(&config, store, &logger).unwrap(); # # if !node.has_ready() { # return; @@ -366,11 +366,11 @@ need to update the applied index and resume `apply` later: # use slog::{Drain, o}; # use raft::{Config, storage::MemStorage, raw_node::RawNode, eraftpb::EntryType}; # - # let mut config = Config { id: 1, ..Default::default() }; + # let config = Config { id: 1, ..Default::default() }; # config.validate().unwrap(); # let store = MemStorage::new_with_conf_state((vec![1], vec![])); # let logger = slog::Logger::root(slog_stdlog::StdLog.fuse(), o!()); - # let mut node = RawNode::new(config, store, &logger).unwrap(); + # let mut node = RawNode::new(&config, store, &logger).unwrap(); # # if !node.has_ready() { # return; @@ -417,10 +417,10 @@ use raft::{Config, storage::MemStorage, raw_node::RawNode, eraftpb::*}; use protobuf::Message as PbMessage; use slog::{Drain, o}; -let mut config = Config { id: 1, ..Default::default() }; +let config = Config { id: 1, ..Default::default() }; let store = MemStorage::new_with_conf_state((vec![1, 2], vec![])); let logger = slog::Logger::root(slog_stdlog::StdLog.fuse(), o!()); -let mut node = RawNode::new(config, store, &logger).unwrap(); +let mut node = RawNode::new(&config, store, &logger).unwrap(); node.raft.become_candidate(); node.raft.become_leader(); diff --git a/src/raft.rs b/src/raft.rs index e33b70448..5c45d02de 100644 --- a/src/raft.rs +++ b/src/raft.rs @@ -217,7 +217,7 @@ pub fn vote_resp_msg_type(t: MessageType) -> MessageType { impl Raft { /// Creates a new raft for use on the node. #[allow(clippy::new_ret_no_self)] - pub fn new(mut c: Config, store: T, logger: &Logger) -> Result { + pub fn new(c: &Config, store: T, logger: &Logger) -> Result { c.validate()?; let logger = logger.new(o!("raft_id" => c.id)); let raft_state = store.initial_state()?; @@ -310,7 +310,7 @@ impl Raft { /// The default logger is an `slog` to `log` adapter. #[allow(clippy::new_ret_no_self)] #[cfg(feature = "default-logger")] - pub fn with_default_logger(c: Config, store: T) -> Result { + pub fn with_default_logger(c: &Config, store: T) -> Result { Self::new(c, store, &crate::default_logger()) } diff --git a/src/raw_node.rs b/src/raw_node.rs index 28684333e..dc3a65771 100644 --- a/src/raw_node.rs +++ b/src/raw_node.rs @@ -221,7 +221,7 @@ pub struct RawNode { impl RawNode { #[allow(clippy::new_ret_no_self)] /// Create a new RawNode given some [`Config`](../struct.Config.html). - pub fn new(config: Config, store: T, logger: &Logger) -> Result { + pub fn new(config: &Config, store: T, logger: &Logger) -> Result { assert_ne!(config.id, 0, "config.id must not be zero"); let r = Raft::new(config, store, logger)?; let mut rn = RawNode { @@ -244,7 +244,7 @@ impl RawNode { /// The default logger is an `slog` to `log` adapter. #[cfg(feature = "default-logger")] #[allow(clippy::new_ret_no_self)] - pub fn with_default_logger(c: Config, store: T) -> Result { + pub fn with_default_logger(c: &Config, store: T) -> Result { Self::new(c, store, &crate::default_logger()) }