Skip to content

Commit

Permalink
raft: add tag for panic messages
Browse files Browse the repository at this point in the history
  • Loading branch information
hhkbp2 committed Sep 19, 2016
1 parent 12384d9 commit e38392f
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 48 deletions.
16 changes: 13 additions & 3 deletions src/raft/log_unstable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,18 @@ pub struct Unstable {
// all entries that have not yet been written to storage.
pub entries: Vec<Entry>,
pub offset: u64,

pub tag: String,
}


impl Unstable {
pub fn new(offset: u64) -> Unstable {
pub fn new(offset: u64, tag: String) -> Unstable {
Unstable {
offset: offset,
snapshot: None,
entries: vec![],
tag: tag,
}
}
// maybe_first_index returns the index of the first possible entry in entries
Expand Down Expand Up @@ -152,11 +155,12 @@ impl Unstable {

pub fn must_check_outofbounds(&self, lo: u64, hi: u64) {
if lo > hi {
panic!("invalid unstable.slice {} > {}", lo, hi)
panic!("{} invalid unstable.slice {} > {}", self.tag, lo, hi)
}
let upper = self.offset + self.entries.len() as u64;
if lo < self.offset || hi > upper {
panic!("unstable.slice[{}, {}] out of bound[{}, {}]",
panic!("{} unstable.slice[{}, {}] out of bound[{}, {}]",
self.tag,
lo,
hi,
self.offset,
Expand Down Expand Up @@ -205,6 +209,7 @@ mod test {
entries: entries.map_or(vec![], |entry| vec![entry]),
offset: offset,
snapshot: snapshot,
..Default::default()
};
let index = u.maybe_first_index();
match index {
Expand All @@ -231,6 +236,7 @@ mod test {
entries: entries.map_or(vec![], |entry| vec![entry]),
offset: offset,
snapshot: snapshot,
..Default::default()
};
let index = u.maybe_last_index();
match index {
Expand Down Expand Up @@ -262,6 +268,7 @@ mod test {
entries: entries.map_or(vec![], |entry| vec![entry]),
offset: offset,
snapshot: snapshot,
..Default::default()
};
let term = u.maybe_term(index);
match term {
Expand All @@ -278,6 +285,7 @@ mod test {
entries: vec![new_entry(5, 1)],
offset: 5,
snapshot: Some(new_snapshot(4, 1)),
..Default::default()
};

let s = new_snapshot(6, 2);
Expand Down Expand Up @@ -319,6 +327,7 @@ mod test {
entries: entries,
offset: offset,
snapshot: snapshot,
..Default::default()
};
u.stable_to(index, term);
assert_eq!(u.offset, woffset);
Expand Down Expand Up @@ -360,6 +369,7 @@ mod test {
entries: entries,
offset: offset,
snapshot: snapshot,
..Default::default()
};
u.truncate_and_append(&to_append);
assert_eq!(u.offset, woffset);
Expand Down
11 changes: 6 additions & 5 deletions src/raft/raft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,14 +217,15 @@ impl<T: Storage> Raft<T> {
pub fn new(c: &Config, store: T) -> Raft<T> {
c.validate().expect("configuration is invalid");
let rs = store.initial_state().expect("");
let raft_log = RaftLog::new(store);
let raft_log = RaftLog::new(store, c.tag.clone());
let mut peers: &[u64] = &c.peers;
if !rs.conf_state.get_nodes().is_empty() {
if !peers.is_empty() {
// TODO: the peers argument is always nil except in
// tests; the argument should be removed and these tests should be
// updated to specify their nodes through a snap
panic!("cannot specify both new(peers) and ConfState.Nodes")
panic!("{} cannot specify both new(peers) and ConfState.Nodes",
c.tag)
}
peers = rs.conf_state.get_nodes();
}
Expand Down Expand Up @@ -363,11 +364,11 @@ impl<T: Storage> Raft<T> {
to);
return false;
}
panic!("unexpected error: {:?}", e);
panic!("{} unexpected error: {:?}", self.tag, e);
}
let snapshot = snapshot_r.unwrap();
if snapshot.get_metadata().get_index() == 0 {
panic!("need non-empty snapshot");
panic!("{} need non-empty snapshot", self.tag);
}
let (sindex, sterm) = (snapshot.get_metadata().get_index(),
snapshot.get_metadata().get_term());
Expand Down Expand Up @@ -608,7 +609,7 @@ impl<T: Storage> Raft<T> {
.expect("unexpected error getting uncommitted entries");
let nconf = self.num_pending_conf(&ents);
if nconf > 1 {
panic!("unexpected double uncommitted config entry");
panic!("{} unexpected double uncommitted config entry", self.tag);
}

if nconf == 1 {
Expand Down
Loading

0 comments on commit e38392f

Please sign in to comment.