Skip to content

Commit

Permalink
support optional logger (#302)
Browse files Browse the repository at this point in the history
Signed-off-by: Ana Hobden <[email protected]>
  • Loading branch information
Hoverbear authored and hicqu committed Oct 10, 2019
1 parent cae7627 commit 9ab43a6
Show file tree
Hide file tree
Showing 21 changed files with 251 additions and 302 deletions.
6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 0 additions & 32 deletions benches/benches.rs
Original file line number Diff line number Diff line change
@@ -1,45 +1,13 @@
#![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;

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<slog::Logger> = 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.
Expand Down
6 changes: 3 additions & 3 deletions benches/suites/progress_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand All @@ -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);
Expand All @@ -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()));
}
};

Expand Down
4 changes: 2 additions & 2 deletions benches/suites/raft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ fn quick_raft(voters: usize, learners: usize, logger: &slog::Logger) -> Raft<Mem
pub fn bench_raft_new(c: &mut Criterion) {
DEFAULT_RAFT_SETS.iter().for_each(|(voters, learners)| {
c.bench_function(&format!("Raft::new ({}, {})", voters, learners), move |b| {
let logger = crate::default_logger();
let logger = raft::default_logger();
b.iter(|| quick_raft(*voters, *learners, &logger))
});
});
Expand All @@ -46,7 +46,7 @@ pub fn bench_raft_campaign(c: &mut Criterion) {
c.bench_function(
&format!("Raft::campaign ({}, {}, {})", voters, learners, msg),
move |b| {
let logger = crate::default_logger();
let logger = raft::default_logger();
b.iter(|| {
let mut raft = quick_raft(*voters, *learners, &logger);
raft.campaign(msg.as_bytes());
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 @@ -14,7 +14,7 @@ fn quick_raw_node(logger: &slog::Logger) -> RawNode<MemStorage> {

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));
};

Expand Down
5 changes: 1 addition & 4 deletions harness/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]

Expand All @@ -21,13 +21,10 @@ 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"
hashbrown = "0.5"
lazy_static = "1.0"
protobuf = "2"
regex = "1.1"
slog-async = "2.3.0"
33 changes: 0 additions & 33 deletions harness/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Logger> = 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))
}
7 changes: 3 additions & 4 deletions harness/tests/failpoints_cases/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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));
Expand All @@ -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;
Expand Down
Loading

0 comments on commit 9ab43a6

Please sign in to comment.