Skip to content

Commit

Permalink
rm mut self ref in config.validate
Browse files Browse the repository at this point in the history
Signed-off-by: Fullstop000 <fullstop1005@gmail.com>
  • Loading branch information
Fullstop000 committed Jun 17, 2020
1 parent 3c696b1 commit c0a0766
Show file tree
Hide file tree
Showing 12 changed files with 41 additions and 48 deletions.
2 changes: 1 addition & 1 deletion benches/suites/raft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fn quick_raft(voters: usize, learners: usize, logger: &slog::Logger) -> Raft<Mem
let id = 1;
let storage = MemStorage::default();
let config = Config::new(id);
let mut raft = Raft::new(config, storage, logger).unwrap();
let mut raft = Raft::new(&config, storage, logger).unwrap();
(0..voters).for_each(|id| {
raft.add_node(id as u64).unwrap();
});
Expand Down
2 changes: 1 addition & 1 deletion benches/suites/raw_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fn quick_raw_node(logger: &slog::Logger) -> RawNode<MemStorage> {
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) {
Expand Down
4 changes: 2 additions & 2 deletions examples/five_mem_node/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion examples/single_mem_node/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
2 changes: 1 addition & 1 deletion harness/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
2 changes: 1 addition & 1 deletion harness/tests/integration_cases/test_raft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}

Expand Down
8 changes: 4 additions & 4 deletions harness/tests/integration_cases/test_raw_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion harness/tests/test_util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ pub fn new_test_raft_with_logs(
}

pub fn new_test_raft_with_config(config: Config, storage: MemStorage, l: &Logger) -> Interface {
Interface::new(Raft::new(config, storage, l).unwrap())
Interface::new(Raft::new(&config, storage, l).unwrap())
}

pub fn hard_state(t: u64, c: u64, v: u64) -> HardState {
Expand Down
17 changes: 5 additions & 12 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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()));
}
Expand Down Expand Up @@ -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(),
Expand Down
40 changes: 20 additions & 20 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
};
Expand All @@ -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();
Expand All @@ -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}};
Expand Down Expand Up @@ -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();
#
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions src/raft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ pub fn vote_resp_msg_type(t: MessageType) -> MessageType {
impl<T: Storage> Raft<T> {
/// 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<Self> {
pub fn new(c: &Config, store: T, logger: &Logger) -> Result<Self> {
c.validate()?;
let logger = logger.new(o!("raft_id" => c.id));
let raft_state = store.initial_state()?;
Expand Down Expand Up @@ -310,7 +310,7 @@ impl<T: Storage> Raft<T> {
/// 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<Self> {
pub fn with_default_logger(c: &Config, store: T) -> Result<Self> {
Self::new(c, store, &crate::default_logger())
}

Expand Down
4 changes: 2 additions & 2 deletions src/raw_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ pub struct RawNode<T: Storage> {
impl<T: Storage> RawNode<T> {
#[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<Self> {
pub fn new(config: &Config, store: T, logger: &Logger) -> Result<Self> {
assert_ne!(config.id, 0, "config.id must not be zero");
let r = Raft::new(config, store, logger)?;
let mut rn = RawNode {
Expand All @@ -244,7 +244,7 @@ impl<T: Storage> RawNode<T> {
/// 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<Self> {
pub fn with_default_logger(c: &Config, store: T) -> Result<Self> {
Self::new(c, store, &crate::default_logger())
}

Expand Down

0 comments on commit c0a0766

Please sign in to comment.