Skip to content

Commit

Permalink
Add with_default_logger
Browse files Browse the repository at this point in the history
Signed-off-by: Ana Hobden <operator@hoverbear.org>
  • Loading branch information
Hoverbear committed Oct 9, 2019
1 parent c7ae9a9 commit cc35e2a
Show file tree
Hide file tree
Showing 13 changed files with 31 additions and 57 deletions.
29 changes: 0 additions & 29 deletions benches/benches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,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<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/tests/failpoints_cases/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@

use crate::test_util::*;
use fail;
use raft::{
eraftpb::MessageType,
util::default_logger,
};
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 Down
2 changes: 1 addition & 1 deletion harness/tests/integration_cases/test_membership_changes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ use hashbrown::{HashMap, HashSet};

use protobuf::Message as PbMessage;
use raft::{
default_logger,
eraftpb::{
ConfChange, ConfChangeType, ConfState, Entry, EntryType, Message, MessageType, Snapshot,
},
storage::MemStorage,
Config, Configuration, Raft, Result, INVALID_ID,
default_logger,
};

use crate::test_util::new_message;
Expand Down
7 changes: 1 addition & 6 deletions harness/tests/integration_cases/test_raft_flow_control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,7 @@
// limitations under the License.

use crate::test_util::*;
use raft::{
eraftpb::*,
Raft,
Storage,
default_logger,
};
use raft::{default_logger, eraftpb::*, Raft, Storage};

// Force progress `pr` to be in replicate state at `i`.
fn progress_become_replicate<T>(r: &mut Raft<T>, pr: u64, i: u64)
Expand Down
7 changes: 1 addition & 6 deletions harness/tests/integration_cases/test_raft_snap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,7 @@
use crate::test_util::*;
use harness::Network;
use raft::eraftpb::*;
use raft::{
Error,
ProgressState,
INVALID_INDEX,
default_logger,
};
use raft::{default_logger, Error, ProgressState, INVALID_INDEX};

fn testing_snap() -> Snapshot {
new_snapshot(11, 11, vec![1, 2])
Expand Down
2 changes: 1 addition & 1 deletion src/progress/progress_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -667,8 +667,8 @@ impl ProgressSet {
#[cfg(test)]
mod test_progress_set {
use super::{Configuration, ProgressSet, Result};
use crate::progress::Progress;
use crate::default_logger;
use crate::progress::Progress;
use hashbrown::HashSet;

const CANARY: u64 = 123;
Expand Down
10 changes: 9 additions & 1 deletion src/raft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ pub fn vote_resp_msg_type(t: MessageType) -> MessageType {
impl<T: Storage> Raft<T> {
/// Creates a new raft for use on the node.
#[allow(clippy::new_ret_no_self)]
pub fn new(c: &Config, store: T, logger: &Logger) -> Result<Raft<T>> {
pub fn new(c: &Config, store: T, logger: &Logger) -> Result<Self> {
c.validate()?;
let logger = logger.new(o!("raft_id" => c.id));
let raft_state = store.initial_state()?;
Expand Down Expand Up @@ -329,6 +329,14 @@ impl<T: Storage> Raft<T> {
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)]
pub fn with_default_logger(c: &Config, store: T) -> Result<Self> {
Self::new(c, store, &crate::default_logger())
}

/// Grabs an immutable reference to the store.
#[inline]
pub fn get_store(&self) -> &T {
Expand Down
2 changes: 1 addition & 1 deletion src/raft_log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -511,11 +511,11 @@ impl<T: Storage> RaftLog<T> {
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::default_logger;
use protobuf::Message as PbMessage;

fn new_entry(index: u64, term: u64) -> eraftpb::Entry {
Expand Down
10 changes: 9 additions & 1 deletion src/raw_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ pub struct RawNode<T: Storage> {
impl<T: Storage> RawNode<T> {
#[allow(clippy::new_ret_no_self)]
/// Create a new RawNode given some [`Config`](../struct.Config.html).
pub fn new(config: &Config, store: T, logger: &Logger) -> Result<RawNode<T>> {
pub fn new(config: &Config, store: T, logger: &Logger) -> Result<Self> {
assert_ne!(config.id, 0, "config.id must not be zero");
let r = Raft::new(config, store, logger)?;
let mut rn = RawNode {
Expand All @@ -237,6 +237,14 @@ impl<T: Storage> RawNode<T> {
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.
#[allow(clippy::new_ret_no_self)]
pub fn with_default_logger(c: &Config, store: T) -> Result<Self> {
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();
Expand Down
2 changes: 1 addition & 1 deletion src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,4 @@ pub(crate) fn format_kv_list(kv_list: &OwnedKVList) -> String {
)
.unwrap();
formatter.buffer
}
}

0 comments on commit cc35e2a

Please sign in to comment.