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 75bc583
Show file tree
Hide file tree
Showing 13 changed files with 65 additions and 72 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
26 changes: 13 additions & 13 deletions harness/tests/integration_cases/test_raft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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)]);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand All @@ -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 {
Expand Down Expand Up @@ -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
};
Expand Down 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 Expand Up @@ -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() {
Expand Down Expand Up @@ -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![];
Expand Down
14 changes: 7 additions & 7 deletions harness/tests/integration_cases/test_raft_paper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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));
Expand All @@ -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));

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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));

Expand Down
10 changes: 5 additions & 5 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 All @@ -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);
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
10 changes: 5 additions & 5 deletions harness/tests/test_util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(
Expand All @@ -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 {
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
Loading

0 comments on commit 75bc583

Please sign in to comment.