From 9ab43a6a4c73dcfe9dd2970394b4066f253e77d0 Mon Sep 17 00:00:00 2001 From: Ana Hobden Date: Wed, 9 Oct 2019 22:34:33 -0700 Subject: [PATCH] support optional logger (#302) Signed-off-by: Ana Hobden --- Cargo.toml | 6 +- benches/benches.rs | 32 --- benches/suites/progress_set.rs | 6 +- benches/suites/raft.rs | 4 +- benches/suites/raw_node.rs | 2 +- harness/Cargo.toml | 5 +- harness/src/lib.rs | 33 --- harness/tests/failpoints_cases/mod.rs | 7 +- .../test_membership_changes.rs | 46 ++-- harness/tests/integration_cases/test_raft.rs | 224 +++++++++--------- .../test_raft_flow_control.rs | 9 +- .../integration_cases/test_raft_paper.rs | 50 ++-- .../tests/integration_cases/test_raft_snap.rs | 17 +- .../tests/integration_cases/test_raw_node.rs | 22 +- harness/tests/tests.rs | 2 - src/lib.rs | 4 +- src/log_unstable.rs | 12 +- src/progress/progress_set.rs | 14 +- src/raft.rs | 11 +- src/raft_log.rs | 36 +-- src/raw_node.rs | 11 +- 21 files changed, 251 insertions(+), 302 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index a4f61d5ef..a543cfb3a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,16 +16,20 @@ edition = "2018" members = ["proto", "harness"] [features] -default = ["protobuf-codec"] +default = ["protobuf-codec", "default-logger"] # Enable failpoints failpoints = ["fail/failpoints"] protobuf-codec = ["raft-proto/protobuf-codec"] prost-codec = ["raft-proto/prost-codec"] +default-logger = ["slog-stdlog", "slog-envlogger", "slog-term"] # Make sure to synchronize updates with Harness. [dependencies] protobuf = "2" slog = "2.2" +slog-stdlog = { version = "4", optional = true } +slog-envlogger = { version = "2.1.0", optional = true } +slog-term = { version = "2.4.0", optional = true } quick-error = "1.2.2" raft-proto = { path = "proto", version = "0.6.0-alpha", default-features = false } # Stick with 0.6 (rather than 0.7) for now, since most of the ecosystem has not diff --git a/benches/benches.rs b/benches/benches.rs index 61b6c977f..b21fba329 100644 --- a/benches/benches.rs +++ b/benches/benches.rs @@ -1,9 +1,6 @@ #![allow(dead_code)] // Due to criterion we need this to avoid warnings. #![cfg_attr(feature = "cargo-clippy", allow(clippy::let_and_return))] // Benches often artificially return values. Allow it. -#[macro_use] -extern crate slog; - use criterion::Criterion; use std::time::Duration; @@ -11,35 +8,6 @@ mod suites; pub const DEFAULT_RAFT_SETS: [(usize, usize); 4] = [(0, 0), (3, 1), (5, 2), (7, 3)]; -/// The default logger we fall back to when passed `None` in external facing constructors. -/// -/// Currently, this is a `log` adaptor behind a `Once` to ensure there is no clobbering. -fn default_logger() -> slog::Logger { - use slog::Drain; - use std::sync::{Mutex, Once}; - - static LOGGER_INITIALIZED: Once = Once::new(); - static mut LOGGER: Option = None; - - let logger = unsafe { - LOGGER_INITIALIZED.call_once(|| { - let decorator = slog_term::TermDecorator::new().build(); - let drain = slog_term::CompactFormat::new(decorator).build(); - let drain = slog_envlogger::new(drain); - LOGGER = Some(slog::Logger::root(Mutex::new(drain).fuse(), o!())); - }); - LOGGER.as_ref().unwrap() - }; - let case = std::thread::current() - .name() - .unwrap() - .split(':') - .last() - .unwrap() - .to_string(); - logger.new(o!("case" => case)) -} - fn main() { let mut c = Criterion::default() // Configure defaults before overriding with args. diff --git a/benches/suites/progress_set.rs b/benches/suites/progress_set.rs index 43fb75441..2beea2a2e 100644 --- a/benches/suites/progress_set.rs +++ b/benches/suites/progress_set.rs @@ -16,7 +16,7 @@ pub fn bench_progress_set(c: &mut Criterion) { } fn quick_progress_set(voters: usize, learners: usize) -> ProgressSet { - let mut set = ProgressSet::with_capacity(voters, learners, crate::default_logger()); + let mut set = ProgressSet::with_capacity(voters, learners, raft::default_logger()); (0..voters).for_each(|id| { set.insert_voter(id as u64, Progress::new(0, 10)).ok(); }); @@ -29,7 +29,7 @@ fn quick_progress_set(voters: usize, learners: usize) -> ProgressSet { pub fn bench_progress_set_new(c: &mut Criterion) { let bench = |b: &mut Bencher| { // No setup. - b.iter(|| ProgressSet::new(crate::default_logger())); + b.iter(|| ProgressSet::new(raft::default_logger())); }; c.bench_function("ProgressSet::new", bench); @@ -39,7 +39,7 @@ pub fn bench_progress_set_with_capacity(c: &mut Criterion) { let bench = |voters, learners| { move |b: &mut Bencher| { // No setup. - b.iter(|| ProgressSet::with_capacity(voters, learners, crate::default_logger())); + b.iter(|| ProgressSet::with_capacity(voters, learners, raft::default_logger())); } }; diff --git a/benches/suites/raft.rs b/benches/suites/raft.rs index cbb43efa6..ee00d59b6 100644 --- a/benches/suites/raft.rs +++ b/benches/suites/raft.rs @@ -24,7 +24,7 @@ fn quick_raft(voters: usize, learners: usize, logger: &slog::Logger) -> Raft RawNode { pub fn bench_raw_node_new(c: &mut Criterion) { let bench = |b: &mut Bencher| { - let logger = crate::default_logger(); + let logger = raft::default_logger(); b.iter(|| quick_raw_node(&logger)); }; diff --git a/harness/Cargo.toml b/harness/Cargo.toml index 06432255b..a6c77695c 100644 --- a/harness/Cargo.toml +++ b/harness/Cargo.toml @@ -12,7 +12,7 @@ categories = [] edition = "2018" [features] -default = ["protobuf-codec"] +default = ["protobuf-codec", "raft/default-logger"] protobuf-codec = ["raft/protobuf-codec"] prost-codec = ["raft/prost-codec"] @@ -21,8 +21,6 @@ prost-codec = ["raft/prost-codec"] raft = { path = "..", default-features = false } rand = "0.6.5" slog = "2.2" -slog-term = "2.4.0" -slog-envlogger = "2.1.0" [dev-dependencies] criterion = ">0.2.4" @@ -30,4 +28,3 @@ hashbrown = "0.5" lazy_static = "1.0" protobuf = "2" regex = "1.1" -slog-async = "2.3.0" diff --git a/harness/src/lib.rs b/harness/src/lib.rs index c27abbe8a..6ae13fbf4 100644 --- a/harness/src/lib.rs +++ b/harness/src/lib.rs @@ -33,40 +33,7 @@ This module contains various testing harness utilities for Raft. */ -#[macro_use] -extern crate slog; - mod interface; mod network; pub use self::{interface::Interface, network::Network}; -use slog::{Drain, Logger}; - -/// Build a logger for tests. -/// -/// Currently, this is a terminal log. It ensures it is only initialized once to prevent clobbering. -// This is `pub` so that testing and benching functions can use it. -// `#[cfg(test)]` doesn't work for benching. -pub fn testing_logger() -> Logger { - use std::sync::{Mutex, Once}; - static LOGGER_INITIALIZED: Once = Once::new(); - static mut LOGGER: Option = None; - - let logger = unsafe { - LOGGER_INITIALIZED.call_once(|| { - let decorator = slog_term::TermDecorator::new().build(); - let drain = slog_term::CompactFormat::new(decorator).build(); - let drain = slog_envlogger::new(drain); - LOGGER = Some(slog::Logger::root(Mutex::new(drain).fuse(), o!())); - }); - LOGGER.as_ref().unwrap() - }; - let case = std::thread::current() - .name() - .unwrap() - .split(':') - .last() - .unwrap() - .to_string(); - logger.new(o!("case" => case)) -} diff --git a/harness/tests/failpoints_cases/mod.rs b/harness/tests/failpoints_cases/mod.rs index 51d6092b7..116d91a1c 100644 --- a/harness/tests/failpoints_cases/mod.rs +++ b/harness/tests/failpoints_cases/mod.rs @@ -12,9 +12,8 @@ // limitations under the License. use crate::test_util::*; -use crate::testing_logger; use fail; -use raft::eraftpb::MessageType; +use raft::{eraftpb::MessageType, util::default_logger}; use std::sync::*; // test_reject_stale_term_message tests that if a server receives a request with @@ -24,7 +23,7 @@ use std::sync::*; #[test] fn test_reject_stale_term_message() { let scenario = fail::FailScenario::setup(); - let l = testing_logger(); + let l = default_logger(); let mut r = new_test_raft(1, vec![1, 2, 3], 10, 1, new_storage(), &l); fail::cfg("before_step", "panic").unwrap(); r.load_state(&hard_state(2, 1, 0)); @@ -40,7 +39,7 @@ fn test_reject_stale_term_message() { #[test] fn test_step_ignore_old_term_msg() { let scenario = fail::FailScenario::setup(); - let l = testing_logger(); + let l = default_logger(); let mut sm = new_test_raft(1, vec![1], 10, 1, new_storage(), &l); fail::cfg("before_step", "panic").unwrap(); sm.term = 2; diff --git a/harness/tests/integration_cases/test_membership_changes.rs b/harness/tests/integration_cases/test_membership_changes.rs index 19f05ada5..1e0231e53 100644 --- a/harness/tests/integration_cases/test_membership_changes.rs +++ b/harness/tests/integration_cases/test_membership_changes.rs @@ -14,12 +14,12 @@ use std::ops::{Deref, DerefMut}; -use harness::testing_logger; use harness::Network; use hashbrown::{HashMap, HashSet}; use protobuf::Message as PbMessage; use raft::{ + default_logger, eraftpb::{ ConfChange, ConfChangeType, ConfState, Entry, EntryType, Message, MessageType, Snapshot, }, @@ -38,7 +38,7 @@ mod api { // Test that the cluster can transition from a single node to a whole cluster. #[test] fn can_transition() -> Result<()> { - let l = testing_logger(); + let l = default_logger(); let mut raft = Raft::new( &Config::new(1), MemStorage::new_with_conf_state((vec![1], vec![])), @@ -54,7 +54,7 @@ mod api { // Test if the process rejects an overlapping voter and learner set. #[test] fn checks_for_overlapping_membership() -> Result<()> { - let l = testing_logger(); + let l = default_logger(); let mut raft = Raft::new( &Config::new(1), MemStorage::new_with_conf_state((vec![1], vec![])), @@ -69,7 +69,7 @@ mod api { // Test if the process rejects an voter demotion. #[test] fn checks_for_voter_demotion() -> Result<()> { - let l = testing_logger(); + let l = default_logger(); let config = Config::new(1); let store = MemStorage::new_with_conf_state((vec![1, 2, 3], vec![4])); let mut raft = Raft::new(&config, store, &l)?; @@ -81,7 +81,7 @@ mod api { // Test if the process rejects an voter demotion. #[test] fn finalize_before_begin_fails_gracefully() -> Result<()> { - let l = testing_logger(); + let l = default_logger(); let mut raft = Raft::new( &Config::new(1), MemStorage::new_with_conf_state((vec![1, 2, 3], vec![4])), @@ -102,7 +102,7 @@ mod three_peers_add_voter { /// In a steady state transition should proceed without issue. #[test] fn stable() -> Result<()> { - let l = testing_logger(); + let l = default_logger(); let leader = 1; let old_configuration = (vec![1, 2, 3], vec![]); let new_configuration = (vec![1, 2, 3, 4], vec![]); @@ -159,7 +159,7 @@ mod three_peers_add_learner { /// In a steady state transition should proceed without issue. #[test] fn stable() -> Result<()> { - let l = testing_logger(); + let l = default_logger(); let leader = 1; let old_configuration = (vec![1, 2, 3], vec![]); let new_configuration = (vec![1, 2, 3], vec![4]); @@ -216,7 +216,7 @@ mod remove_learner { /// In a steady state transition should proceed without issue. #[test] fn stable() -> Result<()> { - let l = testing_logger(); + let l = default_logger(); let leader = 1; let old_configuration = (vec![1, 2, 3], vec![4]); let new_configuration = (vec![1, 2, 3], vec![]); @@ -264,7 +264,7 @@ mod remove_voter { /// In a steady state transition should proceed without issue. #[test] fn stable() -> Result<()> { - let l = testing_logger(); + let l = default_logger(); let leader = 1; let old_configuration = (vec![1, 2, 3], vec![]); let new_configuration = (vec![1, 2], vec![]); @@ -312,7 +312,7 @@ mod remove_leader { /// In a steady state transition should proceed without issue. #[test] fn stable() -> Result<()> { - let l = testing_logger(); + let l = default_logger(); let leader = 1; let old_configuration = (vec![1, 2, 3], vec![]); let new_configuration = (vec![2, 3], vec![]); @@ -388,7 +388,7 @@ mod remove_leader { /// If the leader fails after the `Begin`, then recovers after the `Finalize`, the group should ignore it. #[test] fn leader_fails_and_recovers() -> Result<()> { - let l = testing_logger(); + let l = default_logger(); let leader = 1; let old_configuration = (vec![1, 2, 3], vec![]); let new_configuration = (vec![2, 3], vec![]); @@ -472,7 +472,7 @@ mod three_peers_replace_voter { /// In a steady state transition should proceed without issue. #[test] fn stable() -> Result<()> { - let l = testing_logger(); + let l = default_logger(); let leader = 1; let old_configuration = (vec![1, 2, 3], vec![]); let new_configuration = (vec![1, 2, 4], vec![]); @@ -524,7 +524,7 @@ mod three_peers_replace_voter { /// The leader power cycles before actually sending the messages. #[test] fn leader_power_cycles_no_compaction() -> Result<()> { - let l = testing_logger(); + let l = default_logger(); let leader = 1; let old_configuration = (vec![1, 2, 3], vec![]); let new_configuration = (vec![1, 2, 4], vec![]); @@ -606,7 +606,7 @@ mod three_peers_replace_voter { /// The leader power cycles before actually sending the messages. #[test] fn leader_power_cycles_compacted_log() -> Result<()> { - let l = testing_logger(); + let l = default_logger(); let leader = 1; let old_configuration = (vec![1, 2, 3], vec![]); let new_configuration = (vec![1, 2, 4], vec![]); @@ -695,7 +695,7 @@ mod three_peers_replace_voter { // Ensure if a peer in the old quorum fails, but the quorum is still big enough, it's ok. #[test] fn pending_delete_fails_after_begin() -> Result<()> { - let l = testing_logger(); + let l = default_logger(); let leader = 1; let old_configuration = (vec![1, 2, 3], vec![]); let new_configuration = (vec![1, 2, 4], vec![]); @@ -749,7 +749,7 @@ mod three_peers_replace_voter { // Ensure if a peer in the new quorum fails, but the quorum is still big enough, it's ok. #[test] fn pending_create_with_quorum_fails_after_begin() -> Result<()> { - let l = testing_logger(); + let l = default_logger(); let leader = 1; let old_configuration = (vec![1, 2, 3], vec![]); let new_configuration = (vec![1, 2, 4], vec![]); @@ -794,7 +794,7 @@ mod three_peers_replace_voter { // Ensure if the peer pending a deletion and the peer pending a creation both fail it's still ok (so long as both quorums hold). #[test] fn pending_create_and_destroy_both_fail() -> Result<()> { - let l = testing_logger(); + let l = default_logger(); let leader = 1; let old_configuration = (vec![1, 2, 3], vec![]); let new_configuration = (vec![1, 2, 4], vec![]); @@ -840,7 +840,7 @@ mod three_peers_replace_voter { // Ensure if the old quorum fails during the joint state progress will halt until the peer group is recovered. #[test] fn old_quorum_fails() -> Result<()> { - let l = testing_logger(); + let l = default_logger(); let leader = 1; let old_configuration = (vec![1, 2, 3], vec![]); let new_configuration = (vec![1, 2, 4], vec![]); @@ -928,7 +928,7 @@ mod three_peers_replace_voter { // Ensure if the new quorum fails during the joint state progress will halt until the peer group is recovered. #[test] fn new_quorum_fails() -> Result<()> { - let l = testing_logger(); + let l = default_logger(); let leader = 1; let old_configuration = (vec![1, 2, 3], vec![]); let new_configuration = (vec![1, 2, 4], vec![]); @@ -1021,7 +1021,7 @@ mod three_peers_to_five_with_learner { /// In a steady state transition should proceed without issue. #[test] fn stable() -> Result<()> { - let l = testing_logger(); + let l = default_logger(); let leader = 1; let old_configuration = (vec![1, 2, 3], vec![]); let new_configuration = (vec![1, 2, 3, 4, 5], vec![6]); @@ -1073,7 +1073,7 @@ mod three_peers_to_five_with_learner { /// In this, a single node (of 3) halts during the transition. #[test] fn minority_old_followers_halt_at_start() -> Result<()> { - let l = testing_logger(); + let l = default_logger(); let leader = 1; let old_configuration = (vec![1, 2, 3], vec![]); let new_configuration = (vec![1, 2, 3, 4, 5], vec![6]); @@ -1142,7 +1142,7 @@ mod intermingled_config_changes { // In this test, we make sure that if the peer group is sent a `BeginMembershipChange`, then immediately a `AddNode` entry, that the `AddNode` is rejected by the leader. #[test] fn begin_then_add_node() -> Result<()> { - let l = testing_logger(); + let l = default_logger(); let leader = 1; let old_configuration = (vec![1, 2, 3], vec![]); let new_configuration = (vec![1, 2, 3, 4], vec![]); @@ -1209,7 +1209,7 @@ mod compaction { // Ensure that if a Raft compacts its log before finalizing that there are no failures. #[test] fn begin_compact_then_finalize() -> Result<()> { - let l = testing_logger(); + let l = default_logger(); let leader = 1; let old_configuration = (vec![1, 2, 3], vec![]); let new_configuration = (vec![1, 2, 3], vec![4]); diff --git a/harness/tests/integration_cases/test_raft.rs b/harness/tests/integration_cases/test_raft.rs index 96a1239cf..b4ea589ee 100644 --- a/harness/tests/integration_cases/test_raft.rs +++ b/harness/tests/integration_cases/test_raft.rs @@ -297,7 +297,7 @@ fn test_progress_resume() { #[test] fn test_progress_leader() { - let l = testing_logger(); + let l = default_logger(); let mut raft = new_test_raft(1, vec![1, 2], 5, 1, new_storage(), &l); raft.become_candidate(); raft.become_leader(); @@ -324,7 +324,7 @@ fn test_progress_leader() { // heartbeat response. #[test] fn test_progress_resume_by_heartbeat_resp() { - let l = testing_logger(); + let l = default_logger(); let mut raft = new_test_raft(1, vec![1, 2], 5, 1, new_storage(), &l); raft.become_candidate(); raft.become_leader(); @@ -342,7 +342,7 @@ fn test_progress_resume_by_heartbeat_resp() { #[test] fn test_progress_paused() { - let l = testing_logger(); + let l = default_logger(); let mut raft = new_test_raft(1, vec![1, 2], 5, 1, new_storage(), &l); raft.become_candidate(); raft.become_leader(); @@ -362,13 +362,13 @@ fn test_progress_paused() { #[test] fn test_leader_election() { - let l = testing_logger(); + let l = default_logger(); test_leader_election_with_config(false, &l); } #[test] fn test_leader_election_pre_vote() { - let l = testing_logger(); + let l = default_logger(); test_leader_election_with_config(true, &l); } @@ -452,13 +452,13 @@ fn test_leader_election_with_config(pre_vote: bool, l: &Logger) { #[test] fn test_leader_cycle() { - let l = testing_logger(); + let l = default_logger(); test_leader_cycle_with_config(false, &l) } #[test] fn test_leader_cycle_pre_vote() { - let l = testing_logger(); + let l = default_logger(); test_leader_cycle_with_config(true, &l) } @@ -497,13 +497,13 @@ fn test_leader_cycle_with_config(pre_vote: bool, l: &Logger) { #[test] fn test_leader_election_overwrite_newer_logs() { - let l = testing_logger(); + let l = default_logger(); test_leader_election_overwrite_newer_logs_with_config(false, &l); } #[test] fn test_leader_election_overwrite_newer_logs_pre_vote() { - let l = testing_logger(); + let l = default_logger(); test_leader_election_overwrite_newer_logs_with_config(true, &l); } @@ -579,13 +579,13 @@ fn test_leader_election_overwrite_newer_logs_with_config(pre_vote: bool, l: &Log #[test] fn test_vote_from_any_state() { - let l = testing_logger(); + let l = default_logger(); test_vote_from_any_state_for_type(MessageType::MsgRequestVote, &l); } #[test] fn test_prevote_from_any_state() { - let l = testing_logger(); + let l = default_logger(); test_vote_from_any_state_for_type(MessageType::MsgRequestPreVote, &l); } @@ -688,7 +688,7 @@ fn test_vote_from_any_state_for_type(vt: MessageType, l: &Logger) { #[test] fn test_log_replicatioin() { - let l = testing_logger(); + let l = default_logger(); let mut tests = vec![ ( Network::new(vec![None, None, None], &l), @@ -740,7 +740,7 @@ fn test_log_replicatioin() { #[test] fn test_single_node_commit() { - let l = testing_logger(); + let l = default_logger(); let mut tt = Network::new(vec![None], &l); assert_eq!(tt.peers[&1].raft_log.first_index(), 2); tt.send(vec![new_message(1, 1, MessageType::MsgHup, 0)]); @@ -754,7 +754,7 @@ fn test_single_node_commit() { // filtered. #[test] fn test_cannot_commit_without_new_term_entry() { - let l = testing_logger(); + let l = default_logger(); let mut tt = Network::new(vec![None, None, None, None, None], &l); assert_eq!(tt.peers[&1].raft_log.committed, 1); tt.send(vec![new_message(1, 1, MessageType::MsgHup, 0)]); @@ -794,7 +794,7 @@ fn test_cannot_commit_without_new_term_entry() { // when leader changes, no new proposal comes in. #[test] fn test_commit_without_new_term_entry() { - let l = testing_logger(); + let l = default_logger(); let mut tt = Network::new(vec![None, None, None, None, None], &l); tt.send(vec![new_message(1, 1, MessageType::MsgHup, 0)]); @@ -821,7 +821,7 @@ fn test_commit_without_new_term_entry() { #[test] fn test_dueling_candidates() { - let l = testing_logger(); + let l = default_logger(); let a = new_test_raft(1, vec![1, 2, 3], 10, 1, new_storage(), &l); let b = new_test_raft(2, vec![1, 2, 3], 10, 1, new_storage(), &l); let c = new_test_raft(3, vec![1, 2, 3], 10, 1, new_storage(), &l); @@ -878,7 +878,7 @@ fn test_dueling_candidates() { #[test] fn test_dueling_pre_candidates() { - let l = testing_logger(); + let l = default_logger(); let a = new_test_raft_with_prevote(1, vec![1, 2, 3], 10, 1, new_storage(), true, &l); let b = new_test_raft_with_prevote(2, vec![1, 2, 3], 10, 1, new_storage(), true, &l); let c = new_test_raft_with_prevote(3, vec![1, 2, 3], 10, 1, new_storage(), true, &l); @@ -928,7 +928,7 @@ fn test_dueling_pre_candidates() { #[test] fn test_candidate_concede() { - let l = testing_logger(); + let l = default_logger(); let mut tt = Network::new(vec![None, None, None], &l); tt.isolate(1); @@ -960,7 +960,7 @@ fn test_candidate_concede() { #[test] fn test_single_node_candidate() { - let l = testing_logger(); + let l = default_logger(); let mut tt = Network::new(vec![None], &l); tt.send(vec![new_message(1, 1, MessageType::MsgHup, 0)]); @@ -969,7 +969,7 @@ fn test_single_node_candidate() { #[test] fn test_sinle_node_pre_candidate() { - let l = testing_logger(); + let l = default_logger(); let mut config = Network::default_config(); config.pre_vote = true; let mut tt = Network::new_with_config(vec![None], &config, &l); @@ -980,7 +980,7 @@ fn test_sinle_node_pre_candidate() { #[test] fn test_old_messages() { - let l = testing_logger(); + let l = default_logger(); let mut tt = Network::new(vec![None, None, None], &l); // make 0 leader @ term 3 tt.send(vec![new_message(1, 1, MessageType::MsgHup, 0)]); @@ -1006,7 +1006,7 @@ fn test_old_messages() { #[test] fn test_proposal() { - let l = testing_logger(); + let l = default_logger(); let mut tests = vec![ (Network::new(vec![None, None, None], &l), true), (Network::new(vec![None, None, NOP_STEPPER], &l), true), @@ -1051,7 +1051,7 @@ fn test_proposal() { #[test] fn test_proposal_by_proxy() { - let l = testing_logger(); + let l = default_logger(); let mut tests = vec![ Network::new(vec![None, None, None], &l), Network::new(vec![None, None, NOP_STEPPER], &l), @@ -1080,7 +1080,7 @@ fn test_proposal_by_proxy() { #[test] fn test_commit() { - let l = testing_logger(); + let l = default_logger(); let mut tests = vec![ // single (vec![2], vec![empty_entry(2, 2)], 2, 2), @@ -1126,7 +1126,7 @@ fn test_commit() { #[test] fn test_pass_election_timeout() { - let l = testing_logger(); + let l = default_logger(); let tests = vec![ (5, 0f64, false), (10, 0.1, true), @@ -1164,7 +1164,7 @@ fn test_pass_election_timeout() { // 3. If leaderCommit > commitIndex, set commitIndex = min(leaderCommit, index of last new entry). #[test] fn test_handle_msg_append() { - let l = testing_logger(); + let l = default_logger(); let nm = |term, log_term, index, commit, ents: Option>| { let mut m = Message::default(); m.set_msg_type(MessageType::MsgAppend); @@ -1235,7 +1235,7 @@ fn test_handle_msg_append() { // test_handle_heartbeat ensures that the follower commits to the commit in the message. #[test] fn test_handle_heartbeat() { - let l = testing_logger(); + let l = default_logger(); let commit = 2u64; let nw = |f, to, term, commit| { let mut m = new_message(f, to, MessageType::MsgHeartbeat, 0); @@ -1281,7 +1281,7 @@ fn test_handle_heartbeat() { // test_handle_heartbeat_resp ensures that we re-send log entries when we get a heartbeat response. #[test] fn test_handle_heartbeat_resp() { - let l = testing_logger(); + let l = default_logger(); let store = new_storage(); store .wl() @@ -1325,7 +1325,7 @@ fn test_handle_heartbeat_resp() { // related issue: https://github.com/coreos/etcd/issues/7571 #[test] fn test_raft_frees_read_only_mem() { - let l = testing_logger(); + let l = default_logger(); let mut sm = new_test_raft(1, vec![1, 2], 5, 1, new_storage(), &l); sm.become_candidate(); sm.become_leader(); @@ -1367,7 +1367,7 @@ fn test_raft_frees_read_only_mem() { // MsgAppResp. #[test] fn test_msg_append_response_wait_reset() { - let l = testing_logger(); + let l = default_logger(); let mut sm = new_test_raft(1, vec![1, 2, 3], 5, 1, new_storage(), &l); sm.become_candidate(); sm.become_leader(); @@ -1413,7 +1413,7 @@ fn test_msg_append_response_wait_reset() { #[test] fn test_recv_msg_request_vote() { - let l = testing_logger(); + let l = default_logger(); test_recv_msg_request_vote_for_type(MessageType::MsgRequestVote, &l); } @@ -1485,7 +1485,7 @@ fn test_recv_msg_request_vote_for_type(msg_type: MessageType, l: &Logger) { #[test] fn test_state_transition() { - let l = testing_logger(); + let l = default_logger(); let mut tests = vec![ ( StateRole::Follower, @@ -1598,7 +1598,7 @@ fn test_state_transition() { #[test] fn test_all_server_stepdown() { - let l = testing_logger(); + let l = default_logger(); let mut tests = vec![ // state, want_state, term, last_index, entry count. (StateRole::Follower, StateRole::Follower, 3, 1, 0), @@ -1661,13 +1661,13 @@ fn test_all_server_stepdown() { #[test] fn test_candidate_reset_term_msg_heartbeat() { - let l = testing_logger(); + let l = default_logger(); test_candidate_reset_term(MessageType::MsgHeartbeat, &l) } #[test] fn test_candidate_reset_term_msg_append() { - let l = testing_logger(); + let l = default_logger(); test_candidate_reset_term(MessageType::MsgAppend, &l) } @@ -1728,7 +1728,7 @@ fn test_candidate_reset_term(message_type: MessageType, l: &Logger) { #[test] fn test_leader_stepdown_when_quorum_active() { - let l = testing_logger(); + let l = default_logger(); let mut sm = new_test_raft(1, vec![1, 2, 3], 5, 1, new_storage(), &l); sm.check_quorum = true; sm.become_candidate(); @@ -1746,7 +1746,7 @@ fn test_leader_stepdown_when_quorum_active() { #[test] fn test_leader_stepdown_when_quorum_lost() { - let l = testing_logger(); + let l = default_logger(); let mut sm = new_test_raft(1, vec![1, 2, 3], 5, 1, new_storage(), &l); sm.check_quorum = true; @@ -1763,7 +1763,7 @@ fn test_leader_stepdown_when_quorum_lost() { #[test] fn test_leader_superseding_with_check_quorum() { - let l = testing_logger(); + let l = default_logger(); let mut a = new_test_raft(1, vec![1, 2, 3], 10, 1, new_storage(), &l); let mut b = new_test_raft(2, vec![1, 2, 3], 10, 1, new_storage(), &l); let mut c = new_test_raft(3, vec![1, 2, 3], 10, 1, new_storage(), &l); @@ -1804,7 +1804,7 @@ fn test_leader_superseding_with_check_quorum() { #[test] fn test_leader_election_with_check_quorum() { - let l = testing_logger(); + let l = default_logger(); let mut a = new_test_raft(1, vec![1, 2, 3], 10, 1, new_storage(), &l); let mut b = new_test_raft(2, vec![1, 2, 3], 10, 1, new_storage(), &l); let mut c = new_test_raft(3, vec![1, 2, 3], 10, 1, new_storage(), &l); @@ -1866,7 +1866,7 @@ fn test_leader_election_with_check_quorum() { // leader is expected to step down and adopt the candidate's term #[test] fn test_free_stuck_candidate_with_check_quorum() { - let l = testing_logger(); + let l = default_logger(); let mut a = new_test_raft(1, vec![1, 2, 3], 10, 1, new_storage(), &l); let mut b = new_test_raft(2, vec![1, 2, 3], 10, 1, new_storage(), &l); let mut c = new_test_raft(3, vec![1, 2, 3], 10, 1, new_storage(), &l); @@ -1920,7 +1920,7 @@ fn test_free_stuck_candidate_with_check_quorum() { #[test] fn test_non_promotable_voter_which_check_quorum() { - let l = testing_logger(); + let l = default_logger(); let mut a = new_test_raft(1, vec![1, 2], 10, 1, new_storage(), &l); let mut b = new_test_raft(2, vec![1], 10, 1, new_storage(), &l); @@ -1961,7 +1961,7 @@ fn test_non_promotable_voter_which_check_quorum() { /// to step down. #[test] fn test_disruptive_follower() { - let l = testing_logger(); + let l = default_logger(); let mut n1 = new_test_raft(1, vec![1, 2, 3], 10, 1, new_storage(), &l); let mut n2 = new_test_raft(2, vec![1, 2, 3], 10, 1, new_storage(), &l); let mut n3 = new_test_raft(3, vec![1, 2, 3], 10, 1, new_storage(), &l); @@ -2052,7 +2052,7 @@ fn test_disruptive_follower() { /// current leader to step down, thus less disruptions. #[test] fn test_disruptive_follower_pre_vote() { - let l = testing_logger(); + let l = default_logger(); let mut n1 = new_test_raft_with_prevote(1, vec![1, 2, 3], 10, 1, new_storage(), true, &l); let mut n2 = new_test_raft_with_prevote(2, vec![1, 2, 3], 10, 1, new_storage(), true, &l); let mut n3 = new_test_raft_with_prevote(3, vec![1, 2, 3], 10, 1, new_storage(), true, &l); @@ -2103,7 +2103,7 @@ fn test_disruptive_follower_pre_vote() { #[test] fn test_read_only_option_safe() { - let l = testing_logger(); + let l = default_logger(); let a = new_test_raft(1, vec![1, 2, 3], 10, 1, new_storage(), &l); let b = new_test_raft(2, vec![1, 2, 3], 10, 1, new_storage(), &l); let c = new_test_raft(3, vec![1, 2, 3], 10, 1, new_storage(), &l); @@ -2194,7 +2194,7 @@ fn test_read_only_option_safe() { #[test] fn test_read_only_with_learner() { - let l = testing_logger(); + let l = default_logger(); let a = new_test_learner_raft(1, vec![1], vec![2], 10, 1, new_storage(), &l); let b = new_test_learner_raft(2, vec![1], vec![2], 10, 1, new_storage(), &l); @@ -2267,7 +2267,7 @@ fn test_read_only_with_learner() { #[test] fn test_read_only_option_lease() { - let l = testing_logger(); + let l = default_logger(); let mut a = new_test_raft(1, vec![1, 2, 3], 10, 1, new_storage(), &l); let mut b = new_test_raft(2, vec![1, 2, 3], 10, 1, new_storage(), &l); let mut c = new_test_raft(3, vec![1, 2, 3], 10, 1, new_storage(), &l); @@ -2344,7 +2344,7 @@ fn test_read_only_option_lease() { #[test] fn test_read_only_option_lease_without_check_quorum() { - let l = testing_logger(); + let l = default_logger(); let mut a = new_test_raft(1, vec![1, 2, 3], 10, 1, new_storage(), &l); let mut b = new_test_raft(2, vec![1, 2, 3], 10, 1, new_storage(), &l); let mut c = new_test_raft(3, vec![1, 2, 3], 10, 1, new_storage(), &l); @@ -2376,7 +2376,7 @@ fn test_read_only_option_lease_without_check_quorum() { // when it commits at least one log entry at it term. #[test] fn test_read_only_for_new_leader() { - let l = testing_logger(); + let l = default_logger(); let heartbeat_ticks = 1; let node_configs = vec![(1, 2, 2, 1), (2, 3, 3, 3), (3, 3, 3, 3)]; let mut peers = vec![]; @@ -2454,7 +2454,7 @@ fn test_read_only_for_new_leader() { #[test] fn test_leader_append_response() { - let l = testing_logger(); + let l = default_logger(); // Initial progress: match = 0, next = 4 on followers. let mut tests = vec![ // Stale resp; no replies. @@ -2523,7 +2523,7 @@ fn test_leader_append_response() { // send a MsgApp with m.Index = 0, m.LogTerm=0 and empty entries. #[test] fn test_bcast_beat() { - let l = testing_logger(); + let l = default_logger(); let store = new_storage(); let mut sm = new_test_raft(1, vec![1, 2, 3], 10, 1, store, &l); @@ -2599,7 +2599,7 @@ fn test_bcast_beat() { // tests the output of the statemachine when receiving MsgBeat #[test] fn test_recv_msg_beat() { - let l = testing_logger(); + let l = default_logger(); let mut tests = vec![ (StateRole::Leader, 2), // candidate and follower should ignore MsgBeat @@ -2636,7 +2636,7 @@ fn test_recv_msg_beat() { #[test] fn test_leader_increase_next() { - let l = testing_logger(); + let l = default_logger(); let previous_ents = vec![empty_entry(1, 2), empty_entry(1, 3), empty_entry(1, 4)]; let mut tests = vec![ // state replicate; optimistically increase next @@ -2672,7 +2672,7 @@ fn test_leader_increase_next() { #[test] fn test_send_append_for_progress_probe() { - let l = testing_logger(); + let l = default_logger(); let mut r = new_test_raft(1, vec![1, 2], 10, 1, new_storage(), &l); r.become_candidate(); r.become_leader(); @@ -2725,7 +2725,7 @@ fn test_send_append_for_progress_probe() { #[test] fn test_send_append_for_progress_replicate() { - let l = testing_logger(); + let l = default_logger(); let mut r = new_test_raft(1, vec![1, 2], 10, 1, new_storage(), &l); r.become_candidate(); r.become_leader(); @@ -2744,7 +2744,7 @@ fn test_send_append_for_progress_replicate() { #[test] fn test_send_append_for_progress_snapshot() { - let l = testing_logger(); + let l = default_logger(); let mut r = new_test_raft(1, vec![1, 2], 10, 1, new_storage(), &l); r.become_candidate(); r.become_leader(); @@ -2760,7 +2760,7 @@ fn test_send_append_for_progress_snapshot() { #[test] fn test_recv_msg_unreachable() { - let l = testing_logger(); + let l = default_logger(); let previous_ents = vec![empty_entry(1, 1), empty_entry(1, 2), empty_entry(1, 3)]; let s = new_storage(); s.wl().append(&previous_ents).unwrap(); @@ -2783,7 +2783,7 @@ fn test_recv_msg_unreachable() { #[test] fn test_restore() { - let l = testing_logger(); + let l = default_logger(); // magic number let s = new_snapshot(11, 11, vec![1, 2, 3]); @@ -2808,7 +2808,7 @@ fn test_restore() { #[test] fn test_restore_ignore_snapshot() { - let l = testing_logger(); + let l = default_logger(); let previous_ents = vec![empty_entry(1, 1), empty_entry(1, 2), empty_entry(1, 3)]; let commit = 1u64; let mut sm = new_test_raft(1, vec![], 10, 1, new_storage(), &l); @@ -2829,7 +2829,7 @@ fn test_restore_ignore_snapshot() { #[test] fn test_provide_snap() { - let l = testing_logger(); + let l = default_logger(); // restore the state machine from a snapshot so it has a compacted log and a snapshot let s = new_snapshot(11, 11, vec![1, 2]); // magic number @@ -2853,7 +2853,7 @@ fn test_provide_snap() { #[test] fn test_ignore_providing_snapshot() { - let l = testing_logger(); + let l = default_logger(); // restore the state machine from a snapshot so it has a compacted log and a snapshot let s = new_snapshot(11, 11, vec![1, 2]); // magic number let mut sm = new_test_raft(1, vec![1], 10, 1, new_storage(), &l); @@ -2875,7 +2875,7 @@ fn test_ignore_providing_snapshot() { #[test] fn test_restore_from_snap_msg() { - let l = testing_logger(); + let l = default_logger(); let s = new_snapshot(11, 11, vec![1, 2]); // magic number let mut sm = new_test_raft(2, vec![1, 2], 10, 1, new_storage(), &l); let mut m = new_message(1, 0, MessageType::MsgSnapshot, 0); @@ -2891,7 +2891,7 @@ fn test_restore_from_snap_msg() { #[test] fn test_slow_node_restore() { - let l = testing_logger(); + let l = default_logger(); let mut nt = Network::new(vec![None, None, None], &l); nt.send(vec![new_message(1, 1, MessageType::MsgHup, 0)]); @@ -2934,7 +2934,7 @@ fn test_slow_node_restore() { // it appends the entry to log and sets pendingConf to be true. #[test] fn test_step_config() { - let l = testing_logger(); + let l = default_logger(); // a raft that cannot make progress let mut r = new_test_raft(1, vec![1, 2], 10, 1, new_storage(), &l); r.become_candidate(); @@ -2953,7 +2953,7 @@ fn test_step_config() { // the proposal to noop and keep its original state. #[test] fn test_step_ignore_config() { - let l = testing_logger(); + let l = default_logger(); // a raft that cannot make progress let mut r = new_test_raft(1, vec![1, 2], 10, 1, new_storage(), &l); r.become_candidate(); @@ -2981,7 +2981,7 @@ fn test_step_ignore_config() { // based on uncommitted entries. #[test] fn test_new_leader_pending_config() { - let l = testing_logger(); + let l = default_logger(); let mut tests = vec![(false, 1), (true, 2)]; for (i, (add_entry, wpending_index)) in tests.drain(..).enumerate() { let mut r = new_test_raft(1, vec![1, 2], 10, 1, new_storage(), &l); @@ -3005,7 +3005,7 @@ fn test_new_leader_pending_config() { // test_add_node tests that add_node could update nodes correctly. #[test] fn test_add_node() -> Result<()> { - let l = testing_logger(); + let l = default_logger(); let mut r = new_test_raft(1, vec![1], 10, 1, new_storage(), &l); r.add_node(2)?; assert_eq!( @@ -3018,7 +3018,7 @@ fn test_add_node() -> Result<()> { #[test] fn test_add_node_check_quorum() -> Result<()> { - let l = testing_logger(); + let l = default_logger(); let mut r = new_test_raft(1, vec![1], 10, 1, new_storage(), &l); r.check_quorum = true; @@ -3053,7 +3053,7 @@ fn test_add_node_check_quorum() -> Result<()> { // and removed list correctly. #[test] fn test_remove_node() -> Result<()> { - let l = testing_logger(); + let l = default_logger(); let mut r = new_test_raft(1, vec![1, 2], 10, 1, new_storage(), &l); r.remove_node(2)?; assert_eq!(r.prs().voter_ids().iter().next().unwrap(), &1); @@ -3066,7 +3066,7 @@ fn test_remove_node() -> Result<()> { #[test] fn test_remove_node_itself() -> Result<()> { - let l = testing_logger().new(o!("test" => "remove_node_itself")); + let l = default_logger().new(o!("test" => "remove_node_itself")); let mut n1 = new_test_learner_raft(1, vec![1], vec![2], 10, 1, new_storage(), &l); n1.remove_node(1)?; assert_eq!(n1.prs().learner_ids().iter().next().unwrap(), &2); @@ -3076,7 +3076,7 @@ fn test_remove_node_itself() -> Result<()> { #[test] fn test_promotable() { - let l = testing_logger(); + let l = default_logger(); let id = 1u64; let mut tests = vec![ (vec![1], true), @@ -3094,7 +3094,7 @@ fn test_promotable() { #[test] fn test_raft_nodes() { - let l = testing_logger(); + let l = default_logger(); let mut tests = vec![ (vec![1, 2, 3], vec![1, 2, 3]), (vec![3, 2, 1], vec![1, 2, 3]), @@ -3111,13 +3111,13 @@ fn test_raft_nodes() { #[test] fn test_campaign_while_leader() { - let l = testing_logger(); + let l = default_logger(); test_campaign_while_leader_with_pre_vote(false, &l); } #[test] fn test_pre_campaign_while_leader() { - let l = testing_logger(); + let l = default_logger(); test_campaign_while_leader_with_pre_vote(true, &l); } @@ -3138,7 +3138,7 @@ fn test_campaign_while_leader_with_pre_vote(pre_vote: bool, l: &Logger) { // committed when a config change reduces the quorum requirements. #[test] fn test_commit_after_remove_node() -> Result<()> { - let l = testing_logger(); + let l = default_logger(); // Create a cluster with two nodes. let s = new_storage(); let mut r = new_test_raft(1, vec![1, 2], 5, 1, s.clone(), &l); @@ -3192,7 +3192,7 @@ fn test_commit_after_remove_node() -> Result<()> { // if the transferee has the most up-to-date log entries when transfer starts. #[test] fn test_leader_transfer_to_uptodate_node() { - let l = testing_logger(); + let l = default_logger(); let mut nt = Network::new(vec![None, None, None], &l); nt.send(vec![new_message(1, 1, MessageType::MsgHup, 0)]); @@ -3216,7 +3216,7 @@ fn test_leader_transfer_to_uptodate_node() { // to the follower. #[test] fn test_leader_transfer_to_uptodate_node_from_follower() { - let l = testing_logger(); + let l = default_logger(); let mut nt = Network::new(vec![None, None, None], &l); nt.send(vec![new_message(1, 1, MessageType::MsgHup, 0)]); @@ -3237,7 +3237,7 @@ fn test_leader_transfer_to_uptodate_node_from_follower() { // even the current leader is still under its leader lease #[test] fn test_leader_transfer_with_check_quorum() { - let l = testing_logger(); + let l = default_logger(); let mut nt = Network::new(vec![None, None, None], &l); for i in 1..4 { let r = &mut nt.peers.get_mut(&i).unwrap(); @@ -3272,7 +3272,7 @@ fn test_leader_transfer_with_check_quorum() { #[test] fn test_leader_transfer_to_slow_follower() { - let l = testing_logger(); + let l = default_logger(); let mut nt = Network::new(vec![None, None, None], &l); nt.send(vec![new_message(1, 1, MessageType::MsgHup, 0)]); @@ -3290,7 +3290,7 @@ fn test_leader_transfer_to_slow_follower() { #[test] fn test_leader_transfer_after_snapshot() { - let l = testing_logger(); + let l = default_logger(); let mut nt = Network::new(vec![None, None, None], &l); nt.send(vec![new_message(1, 1, MessageType::MsgHup, 0)]); @@ -3325,7 +3325,7 @@ fn test_leader_transfer_after_snapshot() { #[test] fn test_leader_transfer_to_self() { - let l = testing_logger(); + let l = default_logger(); let mut nt = Network::new(vec![None, None, None], &l); nt.send(vec![new_message(1, 1, MessageType::MsgHup, 0)]); @@ -3336,7 +3336,7 @@ fn test_leader_transfer_to_self() { #[test] fn test_leader_transfer_to_non_existing_node() { - let l = testing_logger(); + let l = default_logger(); let mut nt = Network::new(vec![None, None, None], &l); nt.send(vec![new_message(1, 1, MessageType::MsgHup, 0)]); @@ -3347,7 +3347,7 @@ fn test_leader_transfer_to_non_existing_node() { #[test] fn test_leader_transfer_to_learner() { - let l = testing_logger(); + 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); @@ -3366,7 +3366,7 @@ fn test_leader_transfer_to_learner() { #[test] fn test_leader_transfer_timeout() { - let l = testing_logger(); + let l = default_logger(); let mut nt = Network::new(vec![None, None, None], &l); nt.send(vec![new_message(1, 1, MessageType::MsgHup, 0)]); @@ -3390,7 +3390,7 @@ fn test_leader_transfer_timeout() { #[test] fn test_leader_transfer_ignore_proposal() { - let l = testing_logger(); + let l = default_logger(); let mut nt = Network::new(vec![None, None, None], &l); nt.send(vec![new_message(1, 1, MessageType::MsgHup, 0)]); @@ -3415,7 +3415,7 @@ fn test_leader_transfer_ignore_proposal() { #[test] fn test_leader_transfer_receive_higher_term_vote() { - let l = testing_logger(); + let l = default_logger(); let mut nt = Network::new(vec![None, None, None], &l); nt.send(vec![new_message(1, 1, MessageType::MsgHup, 0)]); @@ -3437,7 +3437,7 @@ fn test_leader_transfer_receive_higher_term_vote() { #[test] fn test_leader_transfer_remove_node() -> Result<()> { - let l = testing_logger(); + let l = default_logger(); let mut nt = Network::new(vec![None, None, None], &l); nt.send(vec![new_message(1, 1, MessageType::MsgHup, 0)]); @@ -3458,7 +3458,7 @@ fn test_leader_transfer_remove_node() -> Result<()> { // back to self when last transfer is pending. #[test] fn test_leader_transfer_back() { - let l = testing_logger(); + let l = default_logger(); let mut nt = Network::new(vec![None, None, None], &l); nt.send(vec![new_message(1, 1, MessageType::MsgHup, 0)]); @@ -3477,7 +3477,7 @@ fn test_leader_transfer_back() { // when last transfer is pending. #[test] fn test_leader_transfer_second_transfer_to_another_node() { - let l = testing_logger(); + let l = default_logger(); let mut nt = Network::new(vec![None, None, None], &l); nt.send(vec![new_message(1, 1, MessageType::MsgHup, 0)]); @@ -3496,7 +3496,7 @@ fn test_leader_transfer_second_transfer_to_another_node() { // to the same node should not extend the timeout while the first one is pending. #[test] fn test_leader_transfer_second_transfer_to_same_node() { - let l = testing_logger(); + let l = default_logger(); let mut nt = Network::new(vec![None, None, None], &l); nt.send(vec![new_message(1, 1, MessageType::MsgHup, 0)]); @@ -3537,7 +3537,7 @@ fn check_leader_transfer_state(r: &Raft, state: StateRole, lead: u64 // transitioned to StateRole::Leader) #[test] fn test_transfer_non_member() { - let l = testing_logger(); + let l = default_logger(); let mut raft = new_test_raft(1, vec![2, 3, 4], 5, 1, new_storage(), &l); raft.step(new_message(2, 1, MessageType::MsgTimeoutNow, 0)) .expect(""); @@ -3556,7 +3556,7 @@ fn test_transfer_non_member() { // enabled. #[test] fn test_node_with_smaller_term_can_complete_election() { - let l = testing_logger(); + let l = default_logger(); let mut n1 = new_test_raft_with_prevote(1, vec![1, 2, 3], 10, 1, new_storage(), true, &l); let mut n2 = new_test_raft_with_prevote(2, vec![1, 2, 3], 10, 1, new_storage(), true, &l); let mut n3 = new_test_raft_with_prevote(3, vec![1, 2, 3], 10, 1, new_storage(), true, &l); @@ -3638,7 +3638,7 @@ pub fn new_test_learner_raft( // even when times out. #[test] fn test_learner_election_timeout() { - let l = testing_logger(); + let l = default_logger(); let mut n1 = new_test_learner_raft(1, vec![1], vec![2], 10, 1, new_storage(), &l); n1.become_follower(1, INVALID_ID); @@ -3659,7 +3659,7 @@ fn test_learner_election_timeout() { // it is promoted to a normal peer. #[test] fn test_learner_promotion() -> Result<()> { - let l = testing_logger(); + let l = default_logger(); let mut n1 = new_test_learner_raft(1, vec![1], vec![2], 10, 1, new_storage(), &l); n1.become_follower(1, INVALID_ID); @@ -3713,7 +3713,7 @@ fn test_learner_promotion() -> Result<()> { // TestLearnerLogReplication tests that a learner can receive entries from the leader. #[test] fn test_learner_log_replication() { - let l = testing_logger(); + let l = default_logger(); let n1 = new_test_learner_raft(1, vec![1], vec![2], 10, 1, new_storage(), &l); let n2 = new_test_learner_raft(2, vec![1], vec![2], 10, 1, new_storage(), &l); let mut network = Network::new(vec![Some(n1), Some(n2)], &l); @@ -3769,7 +3769,7 @@ fn test_learner_log_replication() { // TestRestoreWithLearner restores a snapshot which contains learners. #[test] fn test_restore_with_learner() { - let l = testing_logger(); + let l = default_logger(); let mut s = new_snapshot(11, 11, vec![1, 2]); s.mut_metadata().mut_conf_state().mut_learners().push(3); @@ -3799,7 +3799,7 @@ fn test_restore_with_learner() { // when restores snapshot. #[test] fn test_restore_invalid_learner() { - let l = testing_logger(); + let l = default_logger(); let mut s = new_snapshot(11, 11, vec![1, 2]); s.mut_metadata().mut_conf_state().mut_learners().push(3); @@ -3810,7 +3810,7 @@ fn test_restore_invalid_learner() { #[test] fn test_restore_learner() { - let l = testing_logger(); + let l = default_logger(); let mut s = new_snapshot(11, 11, vec![1, 2]); s.mut_metadata().mut_conf_state().mut_learners().push(3); @@ -3824,7 +3824,7 @@ fn test_restore_learner() { // restoring snapshot. #[test] fn test_restore_learner_promotion() { - let l = testing_logger(); + let l = default_logger(); let s = new_snapshot(11, 11, vec![1, 2, 3]); let mut sm = new_test_learner_raft(3, vec![1, 2], vec![3], 10, 1, new_storage(), &l); assert!(!sm.promotable()); @@ -3835,7 +3835,7 @@ fn test_restore_learner_promotion() { // TestLearnerReceiveSnapshot tests that a learner can receive a snapshot from leader. #[test] fn test_learner_receive_snapshot() { - let l = testing_logger(); + let l = default_logger(); let mut s = new_snapshot(11, 11, vec![1]); s.mut_metadata().mut_conf_state().mut_learners().push(2); @@ -3873,7 +3873,7 @@ fn test_learner_receive_snapshot() { // TestAddLearner tests that addLearner could update nodes correctly. #[test] fn test_add_learner() -> Result<()> { - let l = testing_logger(); + let l = default_logger(); let mut n1 = new_test_raft(1, vec![1], 10, 1, new_storage(), &l); n1.add_learner(2)?; @@ -3887,7 +3887,7 @@ fn test_add_learner() -> Result<()> { // When the action fails, ensure it doesn't mutate the raft state. #[test] fn test_add_voter_peer_promotes_self_sets_is_learner() -> Result<()> { - let l = testing_logger(); + let l = default_logger(); let mut n1 = new_test_raft(1, vec![1], 10, 1, new_storage(), &l); // Node is already voter. @@ -3906,7 +3906,7 @@ fn test_add_voter_peer_promotes_self_sets_is_learner() -> Result<()> { // and removed list correctly. #[test] fn test_remove_learner() -> Result<()> { - let l = testing_logger(); + let l = default_logger(); let mut n1 = new_test_learner_raft(1, vec![1], vec![2], 10, 1, new_storage(), &l); n1.remove_node(2)?; assert_eq!(n1.prs().voter_ids().iter().next().unwrap(), &1); @@ -3971,7 +3971,7 @@ fn new_prevote_migration_cluster(l: &Logger) -> Network { #[test] fn test_prevote_migration_can_complete_election() { - let l = testing_logger(); + let l = default_logger(); // n1 is leader with term 2 // n2 is follower with term 2 // n3 is pre-candidate with term 4, and less log @@ -4001,7 +4001,7 @@ fn test_prevote_migration_can_complete_election() { #[test] fn test_prevote_migration_with_free_stuck_pre_candidate() { - let l = testing_logger(); + let l = default_logger(); let mut nt = new_prevote_migration_cluster(&l); // n1 is leader with term 2 @@ -4031,7 +4031,7 @@ fn test_prevote_migration_with_free_stuck_pre_candidate() { #[test] fn test_learner_respond_vote() -> Result<()> { - let l = testing_logger(); + let l = default_logger(); let mut n1 = new_test_learner_raft(1, vec![1, 2], vec![3], 10, 1, new_storage(), &l); n1.become_follower(1, INVALID_ID); n1.reset_randomized_election_timeout(); @@ -4062,7 +4062,7 @@ fn test_learner_respond_vote() -> Result<()> { #[test] fn test_election_tick_range() { - let l = testing_logger(); + 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, s, &l).raft.unwrap(); @@ -4101,7 +4101,7 @@ fn test_election_tick_range() { // election in next round. #[test] fn test_prevote_with_split_vote() { - let l = testing_logger(); + let l = default_logger(); let peers = (1..=3).map(|id| { let mut raft = new_test_raft_with_prevote(id, vec![1, 2, 3], 10, 1, new_storage(), true, &l); @@ -4149,7 +4149,7 @@ fn test_prevote_with_split_vote() { // ensure that after a node become pre-candidate, it will checkQuorum correctly. #[test] fn test_prevote_with_check_quorum() { - let l = testing_logger(); + let l = default_logger(); let bootstrap = |id| { let mut cfg = new_test_config(id, 10, 1); cfg.pre_vote = true; @@ -4220,14 +4220,14 @@ 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, &testing_logger()); + let raft = Raft::new(&invalid_config, s, &default_logger()); assert!(raft.is_err()) } // tests whether MsgAppend are batched #[test] fn test_batch_msg_append() { - let l = testing_logger(); + let l = default_logger(); let storage = new_storage(); let mut raft = new_test_raft(1, vec![1, 2, 3], 10, 1, storage.clone(), &l); raft.become_candidate(); @@ -4254,7 +4254,7 @@ fn test_batch_msg_append() { /// Tests if unapplied conf change is checked before campaign. #[test] fn test_conf_change_check_before_campaign() { - let l = testing_logger(); + let l = default_logger(); let mut nt = Network::new(vec![None, None, None], &l); nt.send(vec![new_message(1, 1, MessageType::MsgHup, 0)]); assert_eq!(nt.peers[&1].state, StateRole::Leader); @@ -4314,7 +4314,7 @@ fn test_conf_change_check_before_campaign() { } fn prepare_request_snapshot() -> (Network, Snapshot) { - let l = testing_logger(); + let l = default_logger(); fn index_term_11(id: u64, ids: Vec, l: &Logger) -> Interface { let store = MemStorage::new(); diff --git a/harness/tests/integration_cases/test_raft_flow_control.rs b/harness/tests/integration_cases/test_raft_flow_control.rs index 2020df1bc..0f03d6cff 100644 --- a/harness/tests/integration_cases/test_raft_flow_control.rs +++ b/harness/tests/integration_cases/test_raft_flow_control.rs @@ -26,8 +26,7 @@ // limitations under the License. use crate::test_util::*; -use harness::testing_logger; -use raft::{eraftpb::*, Raft, Storage}; +use raft::{default_logger, eraftpb::*, Raft, Storage}; // Force progress `pr` to be in replicate state at `i`. fn progress_become_replicate(r: &mut Raft, pr: u64, i: u64) @@ -43,7 +42,7 @@ where // 2. when the window is full, no more msgApp can be sent. #[test] fn test_msg_app_flow_control_full() { - let l = testing_logger(); + let l = default_logger(); let mut r = new_test_raft(1, vec![1, 2], 5, 1, new_storage(), &l); r.become_candidate(); r.become_leader(); @@ -83,7 +82,7 @@ fn test_msg_app_flow_control_full() { // 2. out-of-dated msgAppResp has no effect on the sliding window. #[test] fn test_msg_app_flow_control_move_forward() { - let l = testing_logger(); + let l = default_logger(); let mut r = new_test_raft(1, vec![1, 2], 5, 1, new_storage(), &l); r.become_candidate(); r.become_leader(); @@ -140,7 +139,7 @@ fn test_msg_app_flow_control_move_forward() { // frees one slot if the window is full. #[test] fn test_msg_app_flow_control_recv_heartbeat() { - let l = testing_logger(); + let l = default_logger(); let mut r = new_test_raft(1, vec![1, 2], 5, 1, new_storage(), &l); r.become_candidate(); r.become_leader(); diff --git a/harness/tests/integration_cases/test_raft_paper.rs b/harness/tests/integration_cases/test_raft_paper.rs index 9ab0d0e0d..c5b5db9c1 100644 --- a/harness/tests/integration_cases/test_raft_paper.rs +++ b/harness/tests/integration_cases/test_raft_paper.rs @@ -64,19 +64,19 @@ fn accept_and_reply(m: &Message) -> Message { #[test] fn test_follower_update_term_from_message() { - let l = testing_logger(); + let l = default_logger(); test_update_term_from_message(StateRole::Follower, &l); } #[test] fn test_candidate_update_term_from_message() { - let l = testing_logger(); + let l = default_logger(); test_update_term_from_message(StateRole::Candidate, &l); } #[test] fn test_leader_update_term_from_message() { - let l = testing_logger(); + let l = default_logger(); test_update_term_from_message(StateRole::Leader, &l); } @@ -109,7 +109,7 @@ fn test_update_term_from_message(state: StateRole, l: &Logger) { // Reference: section 5.2 #[test] fn test_start_as_follower() { - let l = testing_logger(); + let l = default_logger(); let r = new_test_raft(1, vec![1, 2, 3], 10, 1, new_storage(), &l); assert_eq!(r.state, StateRole::Follower); } @@ -120,7 +120,7 @@ fn test_start_as_follower() { // Reference: section 5.2 #[test] fn test_leader_bcast_beat() { - let l = testing_logger(); + let l = default_logger(); // heartbeat interval let hi = 1; let mut r = new_test_raft(1, vec![1, 2, 3], 10, hi, new_storage(), &l); @@ -150,13 +150,13 @@ fn test_leader_bcast_beat() { #[test] fn test_follower_start_election() { - let l = testing_logger(); + let l = default_logger(); test_nonleader_start_election(StateRole::Follower, &l); } #[test] fn test_candidate_start_new_election() { - let l = testing_logger(); + let l = default_logger(); test_nonleader_start_election(StateRole::Candidate, &l); } @@ -208,7 +208,7 @@ fn test_nonleader_start_election(state: StateRole, l: &Logger) { // Reference: section 5.2 #[test] fn test_leader_election_in_one_round_rpc() { - let l = testing_logger(); + let l = default_logger(); let mut tests = vec![ // win the election when receiving votes from a majority of the servers (1, map!(), StateRole::Leader), @@ -265,7 +265,7 @@ fn test_leader_election_in_one_round_rpc() { // Reference: section 5.2 #[test] fn test_follower_vote() { - let l = testing_logger(); + let l = default_logger(); let mut tests = vec![ (INVALID_ID, 1, false), (INVALID_ID, 2, false), @@ -303,7 +303,7 @@ fn test_follower_vote() { // Reference: section 5.2 #[test] fn test_candidate_fallback() { - let l = testing_logger(); + let l = default_logger(); let new_message_ext = |f, to, term| { let mut m = new_message(f, to, MessageType::MsgAppend, 0); m.term = term; @@ -334,13 +334,13 @@ fn test_candidate_fallback() { #[test] fn test_follower_election_timeout_randomized() { - let l = testing_logger(); + let l = default_logger(); test_non_leader_election_timeout_randomized(StateRole::Follower, &l); } #[test] fn test_candidate_election_timeout_randomized() { - let l = testing_logger(); + let l = default_logger(); test_non_leader_election_timeout_randomized(StateRole::Candidate, &l); } @@ -375,13 +375,13 @@ fn test_non_leader_election_timeout_randomized(state: StateRole, l: &Logger) { #[test] fn test_follower_election_timeout_nonconflict() { - let l = testing_logger(); + let l = default_logger(); test_nonleaders_election_timeout_nonconfict(StateRole::Follower, &l); } #[test] fn test_candidates_election_timeout_nonconf() { - let l = testing_logger(); + let l = default_logger(); test_nonleaders_election_timeout_nonconfict(StateRole::Candidate, &l); } @@ -436,7 +436,7 @@ fn test_nonleaders_election_timeout_nonconfict(state: StateRole, l: &Logger) { // Reference: section 5.3 #[test] fn test_leader_start_replication() { - let l = testing_logger(); + let l = default_logger(); let s = new_storage(); let mut r = new_test_raft(1, vec![1, 2, 3], 10, 1, s.clone(), &l); r.become_candidate(); @@ -478,7 +478,7 @@ fn test_leader_start_replication() { // Reference: section 5.3 #[test] fn test_leader_commit_entry() { - let l = testing_logger(); + let l = default_logger(); let s = new_storage(); let mut r = new_test_raft(1, vec![1, 2, 3], 10, 1, s.clone(), &l); r.become_candidate(); @@ -509,7 +509,7 @@ fn test_leader_commit_entry() { // Reference: section 5.3 #[test] fn test_leader_acknowledge_commit() { - let l = testing_logger(); + let l = default_logger(); let mut tests = vec![ (1, map!(), true), (3, map!(), false), @@ -551,7 +551,7 @@ fn test_leader_acknowledge_commit() { // Reference: section 5.3 #[test] fn test_leader_commit_preceding_entries() { - let l = testing_logger(); + let l = default_logger(); let mut tests = vec![ vec![], vec![empty_entry(2, 2)], @@ -595,7 +595,7 @@ fn test_leader_commit_preceding_entries() { // Reference: section 5.3 #[test] fn test_follower_commit_entry() { - let l = testing_logger(); + let l = default_logger(); let mut tests = vec![ (vec![new_entry(1, 2, SOME_DATA)], 2), ( @@ -654,7 +654,7 @@ fn test_follower_commit_entry() { // Reference: section 5.3 #[test] fn test_follower_check_msg_append() { - let l = testing_logger(); + let l = default_logger(); let ents = vec![empty_entry(1, 2), empty_entry(2, 3)]; let mut tests = vec![ // match with committed entries @@ -711,7 +711,7 @@ fn test_follower_check_msg_append() { // Reference: section 5.3 #[test] fn test_follower_append_entries() { - let l = testing_logger(); + let l = default_logger(); let mut tests = vec![ ( 3, @@ -782,7 +782,7 @@ fn test_follower_append_entries() { // Reference: section 5.3, figure 7 #[test] fn test_leader_sync_follower_log() { - let l = testing_logger(); + let l = default_logger(); let ents = vec![ empty_entry(1, 2), empty_entry(1, 3), @@ -901,7 +901,7 @@ fn test_leader_sync_follower_log() { // Reference: section 5.4.1 #[test] fn test_vote_request() { - let l = testing_logger(); + let l = default_logger(); let mut tests = vec![ (vec![empty_entry(1, 2)], 2), (vec![empty_entry(1, 2), empty_entry(2, 3)], 3), @@ -958,7 +958,7 @@ fn test_vote_request() { // Reference: section 5.4.1 #[test] fn test_voter() { - let l = testing_logger(); + let l = default_logger(); let mut tests = vec![ // same logterm (vec![empty_entry(1, 2)], 1, 2, false), @@ -1008,7 +1008,7 @@ fn test_voter() { // Reference: section 5.4.2 #[test] fn test_leader_only_commits_log_from_current_term() { - let l = testing_logger(); + let l = default_logger(); let ents = vec![empty_entry(1, 2), empty_entry(2, 3)]; let mut tests = vec![ // do not commit log entries in previous terms diff --git a/harness/tests/integration_cases/test_raft_snap.rs b/harness/tests/integration_cases/test_raft_snap.rs index dbdedf0e9..3d1931f40 100644 --- a/harness/tests/integration_cases/test_raft_snap.rs +++ b/harness/tests/integration_cases/test_raft_snap.rs @@ -26,10 +26,9 @@ // limitations under the License. use crate::test_util::*; -use crate::testing_logger; use harness::Network; use raft::eraftpb::*; -use raft::{Error, ProgressState, INVALID_INDEX}; +use raft::{default_logger, Error, ProgressState, INVALID_INDEX}; fn testing_snap() -> Snapshot { new_snapshot(11, 11, vec![1, 2]) @@ -37,7 +36,7 @@ fn testing_snap() -> Snapshot { #[test] fn test_sending_snapshot_set_pending_snapshot() { - let l = testing_logger(); + let l = default_logger(); let mut sm = new_test_raft(1, vec![1, 2], 10, 1, new_storage(), &l); sm.restore(testing_snap()); @@ -60,7 +59,7 @@ fn test_sending_snapshot_set_pending_snapshot() { #[test] fn test_pending_snapshot_pause_replication() { - let l = testing_logger(); + let l = default_logger(); let mut sm = new_test_raft(1, vec![1, 2], 10, 1, new_storage(), &l); sm.restore(testing_snap()); @@ -76,7 +75,7 @@ fn test_pending_snapshot_pause_replication() { #[test] fn test_snapshot_failure() { - let l = testing_logger(); + let l = default_logger(); let mut sm = new_test_raft(1, vec![1, 2], 10, 1, new_storage(), &l); sm.restore(testing_snap()); @@ -97,7 +96,7 @@ fn test_snapshot_failure() { #[test] fn test_snapshot_succeed() { - let l = testing_logger(); + let l = default_logger(); let mut sm = new_test_raft(1, vec![1, 2], 10, 1, new_storage(), &l); sm.restore(testing_snap()); @@ -118,7 +117,7 @@ fn test_snapshot_succeed() { #[test] fn test_snapshot_abort() { - let l = testing_logger(); + let l = default_logger(); let mut sm = new_test_raft(1, vec![1, 2], 10, 1, new_storage(), &l); sm.restore(testing_snap()); @@ -140,7 +139,7 @@ fn test_snapshot_abort() { // Initialized storage should be at term 1 instead of 0. Otherwise the case will fail. #[test] fn test_snapshot_with_min_term() { - let l = testing_logger(); + let l = default_logger(); let do_test = |pre_vote: bool| { let n1 = new_test_raft_with_prevote(1, vec![1, 2], 10, 1, new_storage(), pre_vote, &l); let n2 = new_test_raft_with_prevote(2, vec![], 10, 1, new_storage(), pre_vote, &l); @@ -156,7 +155,7 @@ fn test_snapshot_with_min_term() { #[test] fn test_request_snapshot() { - let l = testing_logger(); + let l = default_logger(); let mut sm = new_test_raft(1, vec![1, 2], 10, 1, new_storage(), &l); sm.restore(testing_snap()); diff --git a/harness/tests/integration_cases/test_raw_node.rs b/harness/tests/integration_cases/test_raw_node.rs index b64c25110..53021453f 100644 --- a/harness/tests/integration_cases/test_raw_node.rs +++ b/harness/tests/integration_cases/test_raw_node.rs @@ -25,7 +25,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -use harness::*; +use harness::Network; use protobuf::{Message as PbMessage, ProtobufEnum as _}; use raft::eraftpb::*; use raft::storage::MemStorage; @@ -80,7 +80,7 @@ fn new_raw_node( // test_raw_node_step ensures that RawNode.Step ignore local message. #[test] fn test_raw_node_step() { - let l = testing_logger(); + let l = default_logger(); for msg_t in MessageType::values() { if vec![ // Vote messages with term 0 will cause panics. @@ -115,7 +115,7 @@ fn test_raw_node_step() { // forward to the new leader and 'send' method does not attach its term #[test] fn test_raw_node_read_index_to_old_leader() { - let l = testing_logger(); + let l = default_logger(); let r1 = new_test_raft(1, vec![1, 2, 3], 10, 1, new_storage(), &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); @@ -177,7 +177,7 @@ fn test_raw_node_read_index_to_old_leader() { // RawNode.propose_conf_change send the given proposal and ConfChange to the underlying raft. #[test] fn test_raw_node_propose_and_conf_change() { - let l = testing_logger(); + let l = default_logger(); let s = new_storage(); let mut raw_node = new_raw_node(1, vec![1], 10, 1, s.clone(), &l); raw_node.campaign().expect(""); @@ -219,7 +219,7 @@ fn test_raw_node_propose_and_conf_change() { // not affect the later propose to add new node. #[test] fn test_raw_node_propose_add_duplicate_node() { - let l = testing_logger(); + let l = default_logger(); let s = new_storage(); let mut raw_node = new_raw_node(1, vec![1], 10, 1, s.clone(), &l); raw_node.campaign().expect(""); @@ -270,7 +270,7 @@ fn test_raw_node_propose_add_duplicate_node() { #[test] fn test_raw_node_propose_add_learner_node() -> Result<()> { - let l = testing_logger(); + let l = default_logger(); let s = new_storage(); let mut raw_node = new_raw_node(1, vec![1], 10, 1, s.clone(), &l); let rd = raw_node.ready(); @@ -314,7 +314,7 @@ fn test_raw_node_propose_add_learner_node() -> Result<()> { // to the underlying raft. It also ensures that ReadState can be read out. #[test] fn test_raw_node_read_index() { - let l = testing_logger(); + let l = default_logger(); let wrequest_ctx = b"somedata".to_vec(); let wrs = vec![ReadState { index: 2u64, @@ -353,7 +353,7 @@ fn test_raw_node_read_index() { // test_raw_node_start ensures that a node can be started correctly. #[test] fn test_raw_node_start() { - let l = testing_logger(); + let l = default_logger(); let store = new_storage(); let mut raw_node = new_raw_node(1, vec![1], 10, 1, store.clone(), &l); @@ -385,7 +385,7 @@ fn test_raw_node_start() { #[test] fn test_raw_node_restart() { - let l = testing_logger(); + let l = default_logger(); let entries = vec![empty_entry(1, 1), new_entry(1, 2, Some("foo"))]; let mut raw_node = { @@ -403,7 +403,7 @@ fn test_raw_node_restart() { #[test] fn test_raw_node_restart_from_snapshot() { - let l = testing_logger(); + let l = default_logger(); let snap = new_snapshot(2, 1, vec![1, 2]); let entries = vec![new_entry(1, 3, Some("foo"))]; @@ -426,7 +426,7 @@ fn test_raw_node_restart_from_snapshot() { // when skip_bcast_commit is true. #[test] fn test_skip_bcast_commit() { - let l = testing_logger(); + let l = default_logger(); 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![])); diff --git a/harness/tests/tests.rs b/harness/tests/tests.rs index 3e88343c6..8feb67b8d 100644 --- a/harness/tests/tests.rs +++ b/harness/tests/tests.rs @@ -21,8 +21,6 @@ extern crate slog; #[macro_use] extern crate lazy_static; -pub use harness::testing_logger; - /// Get the count of macro's arguments. /// /// # Examples diff --git a/src/lib.rs b/src/lib.rs index e664ccf40..2e00ad1aa 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -574,8 +574,8 @@ pub mod prelude { /// The default logger we fall back to when passed `None` in external facing constructors. /// /// Currently, this is a `log` adaptor behind a `Once` to ensure there is no clobbering. -#[cfg(test)] -fn test_logger() -> slog::Logger { +#[cfg(any(test, feature = "default-logger"))] +pub fn default_logger() -> slog::Logger { use slog::Drain; use std::sync::{Mutex, Once}; diff --git a/src/log_unstable.rs b/src/log_unstable.rs index a5618df0a..68a7e5480 100644 --- a/src/log_unstable.rs +++ b/src/log_unstable.rs @@ -221,7 +221,7 @@ mod test { entries: entries.map_or(vec![], |entry| vec![entry]), offset, snapshot, - logger: crate::test_logger(), + logger: crate::default_logger(), }; let index = u.maybe_first_index(); match index { @@ -248,7 +248,7 @@ mod test { entries: entries.map_or(vec![], |entry| vec![entry]), offset, snapshot, - logger: crate::test_logger(), + logger: crate::default_logger(), }; let index = u.maybe_last_index(); match index { @@ -309,7 +309,7 @@ mod test { entries: entries.map_or(vec![], |entry| vec![entry]), offset, snapshot, - logger: crate::test_logger(), + logger: crate::default_logger(), }; let term = u.maybe_term(index); match term { @@ -325,7 +325,7 @@ mod test { entries: vec![new_entry(5, 1)], offset: 5, snapshot: Some(new_snapshot(4, 1)), - logger: crate::test_logger(), + logger: crate::default_logger(), }; let s = new_snapshot(6, 2); @@ -407,7 +407,7 @@ mod test { entries, offset, snapshot, - logger: crate::test_logger(), + logger: crate::default_logger(), }; u.stable_to(index, term); assert_eq!(u.offset, woffset); @@ -474,7 +474,7 @@ mod test { entries, offset, snapshot, - logger: crate::test_logger(), + logger: crate::default_logger(), }; u.truncate_and_append(&to_append); assert_eq!(u.offset, woffset); diff --git a/src/progress/progress_set.rs b/src/progress/progress_set.rs index c0234cabf..2b0fb396a 100644 --- a/src/progress/progress_set.rs +++ b/src/progress/progress_set.rs @@ -657,15 +657,15 @@ impl ProgressSet { #[cfg(test)] mod test_progress_set { use super::{Configuration, ProgressSet, Result}; + use crate::default_logger; use crate::progress::Progress; - use crate::test_logger; use hashbrown::HashSet; const CANARY: u64 = 123; #[test] fn test_insert_redundant_voter() -> Result<()> { - let mut set = ProgressSet::new(test_logger()); + let mut set = ProgressSet::new(default_logger()); let default_progress = Progress::new(0, 256); let mut canary_progress = Progress::new(0, 256); canary_progress.matched = CANARY; @@ -684,7 +684,7 @@ mod test_progress_set { #[test] fn test_insert_redundant_learner() -> Result<()> { - let mut set = ProgressSet::new(test_logger()); + let mut set = ProgressSet::new(default_logger()); let default_progress = Progress::new(0, 256); let mut canary_progress = Progress::new(0, 256); canary_progress.matched = CANARY; @@ -703,7 +703,7 @@ mod test_progress_set { #[test] fn test_insert_learner_that_is_voter() -> Result<()> { - let mut set = ProgressSet::new(test_logger()); + let mut set = ProgressSet::new(default_logger()); let default_progress = Progress::new(0, 256); let mut canary_progress = Progress::new(0, 256); canary_progress.matched = CANARY; @@ -722,7 +722,7 @@ mod test_progress_set { #[test] fn test_insert_voter_that_is_learner() -> Result<()> { - let mut set = ProgressSet::new(test_logger()); + let mut set = ProgressSet::new(default_logger()); let default_progress = Progress::new(0, 256); let mut canary_progress = Progress::new(0, 256); canary_progress.matched = CANARY; @@ -741,7 +741,7 @@ mod test_progress_set { #[test] fn test_promote_learner() -> Result<()> { - let mut set = ProgressSet::new(test_logger()); + let mut set = ProgressSet::new(default_logger()); let default_progress = Progress::new(0, 256); set.insert_voter(1, default_progress)?; let pre = set.get(1).expect("Should have been inserted").clone(); @@ -818,7 +818,7 @@ mod test_progress_set { .cloned() .collect::>(); - let mut set = ProgressSet::new(test_logger()); + let mut set = ProgressSet::new(default_logger()); let default_progress = Progress::new(0, 10); for starter in start_voters { diff --git a/src/raft.rs b/src/raft.rs index 0b7735940..90dbda930 100644 --- a/src/raft.rs +++ b/src/raft.rs @@ -235,7 +235,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(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()?; @@ -329,6 +329,15 @@ impl Raft { Ok(r) } + /// Creates a new raft for use on the node with the default logger. + /// + /// 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::new(c, store, &crate::default_logger()) + } + /// Grabs an immutable reference to the store. #[inline] pub fn get_store(&self) -> &T { diff --git a/src/raft_log.rs b/src/raft_log.rs index d45118678..220b218f4 100644 --- a/src/raft_log.rs +++ b/src/raft_log.rs @@ -511,11 +511,11 @@ impl RaftLog { mod test { use std::panic::{self, AssertUnwindSafe}; + use crate::default_logger; use crate::eraftpb; use crate::errors::{Error, StorageError}; use crate::raft_log::{self, RaftLog}; use crate::storage::MemStorage; - use crate::test_logger; use protobuf::Message as PbMessage; fn new_entry(index: u64, term: u64) -> eraftpb::Entry { @@ -536,7 +536,7 @@ mod test { #[test] fn test_find_conflict() { - let l = test_logger(); + let l = default_logger(); let previous_ents = vec![new_entry(1, 1), new_entry(2, 2), new_entry(3, 3)]; let tests = vec![ // no conflict, empty ent @@ -596,7 +596,7 @@ mod test { fn test_is_up_to_date() { let previous_ents = vec![new_entry(1, 1), new_entry(2, 2), new_entry(3, 3)]; let store = MemStorage::new(); - let mut raft_log = RaftLog::new(store, test_logger()); + let mut raft_log = RaftLog::new(store, default_logger()); raft_log.append(&previous_ents); let tests = vec![ // greater term, ignore lastIndex @@ -622,7 +622,7 @@ mod test { #[test] fn test_append() { - let l = test_logger(); + let l = default_logger(); let previous_ents = vec![new_entry(1, 1), new_entry(2, 2)]; let tests = vec![ (vec![], 2, vec![new_entry(1, 1), new_entry(2, 2)], 3), @@ -675,7 +675,7 @@ mod test { .append(&[new_entry(i as u64, i as u64)]) .expect("append failed"); } - let mut raft_log = RaftLog::new(storage, test_logger()); + let mut raft_log = RaftLog::new(storage, default_logger()); for i in unstable_index..last_index { raft_log.append(&[new_entry(i as u64 + 1, i as u64 + 1)]); } @@ -723,7 +723,7 @@ mod test { .wl() .apply_snapshot(new_snapshot(storagesnapi, 1)) .expect("apply failed."); - let mut raft_log = RaftLog::new(store, test_logger()); + let mut raft_log = RaftLog::new(store, default_logger()); raft_log.restore(new_snapshot(unstablesnapi, 1)); let tests = vec![ @@ -754,7 +754,7 @@ mod test { .wl() .apply_snapshot(new_snapshot(offset, 1)) .expect("apply failed."); - let mut raft_log = RaftLog::new(store, test_logger()); + let mut raft_log = RaftLog::new(store, default_logger()); for i in 1..num { raft_log.append(&[new_entry(offset + i, i)]); } @@ -783,7 +783,7 @@ mod test { .wl() .apply_snapshot(new_snapshot(index, term)) .expect("apply failed."); - let raft_log = RaftLog::new(store, test_logger()); + let raft_log = RaftLog::new(store, default_logger()); assert!(raft_log.all_entries().is_empty()); assert_eq!(index + 1, raft_log.first_index()); @@ -795,7 +795,7 @@ mod test { #[test] fn test_stable_to_with_snap() { - let l = test_logger(); + let l = default_logger(); let (snap_index, snap_term) = (5u64, 2u64); let tests = vec![ (snap_index + 1, snap_term, vec![], snap_index + 1), @@ -862,7 +862,7 @@ mod test { #[test] fn test_stable_to() { - let l = test_logger(); + let l = default_logger(); let tests = vec![(1, 1, 2), (2, 2, 3), (2, 1, 1), (3, 1, 1)]; for (i, &(stablei, stablet, wunstable)) in tests.iter().enumerate() { let store = MemStorage::new(); @@ -882,7 +882,7 @@ mod test { // entries correctly. #[test] fn test_unstable_ents() { - let l = test_logger(); + let l = default_logger(); let previous_ents = vec![new_entry(1, 1), new_entry(2, 2)]; let tests = vec![(3, vec![]), (1, previous_ents.clone())]; @@ -916,7 +916,7 @@ mod test { #[test] fn test_next_ents() { - let l = test_logger(); + let l = default_logger(); let ents = [new_entry(4, 1), new_entry(5, 1), new_entry(6, 1)]; let tests = vec![ (0, Some(&ents[..2])), @@ -945,7 +945,7 @@ mod test { #[test] fn test_has_next_ents() { - let l = test_logger(); + let l = default_logger(); let ents = [new_entry(4, 1), new_entry(5, 1), new_entry(6, 1)]; let tests = vec![(0, true), (3, true), (4, true), (5, false)]; @@ -984,7 +984,7 @@ mod test { .append(&[new_entry(offset + i, offset + i)]) .expect(""); } - let mut raft_log = RaftLog::new(store, test_logger()); + let mut raft_log = RaftLog::new(store, default_logger()); for i in (num / 2)..num { raft_log.append(&[new_entry(offset + i, offset + i)]); } @@ -1106,7 +1106,7 @@ mod test { /// return false #[test] fn test_log_maybe_append() { - let l = test_logger(); + let l = default_logger(); let previous_ents = vec![new_entry(1, 1), new_entry(2, 2), new_entry(3, 3)]; let (last_index, last_term, commit) = (3u64, 3u64, 1u64); @@ -1287,7 +1287,7 @@ mod test { #[test] fn test_commit_to() { - let l = test_logger(); + let l = default_logger(); let previous_ents = vec![new_entry(1, 1), new_entry(2, 2), new_entry(3, 3)]; let previous_commit = 2u64; let tests = vec![ @@ -1315,7 +1315,7 @@ mod test { // TestCompaction ensures that the number of log entries is correct after compactions. #[test] fn test_compaction() { - let l = test_logger(); + let l = default_logger(); let tests = vec![ // out of upper bound (1000, vec![1001u64], vec![0usize], true), @@ -1364,7 +1364,7 @@ mod test { .wl() .apply_snapshot(new_snapshot(offset, 0)) .expect(""); - let mut raft_log = RaftLog::new(store, test_logger()); + let mut raft_log = RaftLog::new(store, default_logger()); for i in 1u64..=num { raft_log.append(&[new_entry(i + offset, 0)]); } diff --git a/src/raw_node.rs b/src/raw_node.rs index 49ab4a34e..6b94702ac 100644 --- a/src/raw_node.rs +++ b/src/raw_node.rs @@ -219,7 +219,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 { @@ -237,6 +237,15 @@ impl RawNode { Ok(rn) } + /// Create a new RawNode given some [`Config`](../struct.Config.html) and the default logger. + /// + /// 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::new(c, store, &crate::default_logger()) + } + fn commit_ready(&mut self, rd: Ready) { if rd.ss.is_some() { self.prev_ss = rd.ss.unwrap();