Skip to content

Commit

Permalink
*: allow index start from 0 (#353)
Browse files Browse the repository at this point in the history
This PR removes the modifications of index during initialization, which will
allow raft's log index goes from 0 again. This is important as many existing
test cases rely on it, especially those related to flow control. Those cases
pass on master in an accidental way, checking what are exact broken will be
too much work. Besides upstream patches from etcd also rely on the same
assumption, only forbid growing from 0 in raft-rs will make it hard to port
patches.

Most of the changes are basically revert of index changes introduced from #196
and #214.

Signed-off-by: Jay Lee <[email protected]>
  • Loading branch information
BusyJay authored Mar 26, 2020
1 parent 040b8b9 commit e2a1841
Show file tree
Hide file tree
Showing 10 changed files with 349 additions and 333 deletions.
2 changes: 1 addition & 1 deletion benches/suites/raw_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ fn test_ready_raft_node(logger: &slog::Logger) -> RawNode<MemStorage> {
let mut e = Entry::default();
e.data = vec![0; 32 * 1024];
e.context = vec![];
e.index = i + 1;
e.index = i;
e.term = 1;
entries.push(e);
}
Expand Down
12 changes: 9 additions & 3 deletions examples/five_mem_node/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use std::time::{Duration, Instant};
use std::{str, thread};

use protobuf::Message as PbMessage;
use raft::eraftpb::ConfState;
use raft::storage::MemStorage;
use raft::{prelude::*, StateRole};
use regex::Regex;
Expand Down Expand Up @@ -175,8 +174,15 @@ impl Node {
let mut cfg = example_config();
cfg.id = id;
let logger = logger.new(o!("tag" => format!("peer_{}", id)));

let storage = MemStorage::new_with_conf_state(ConfState::from((vec![id], vec![])));
let mut s = Snapshot::default();
// Because we don't use the same configuration to initialize every node, so we use
// a non-zero index to force new followers catch up logs by snapshot first, which will
// bring all nodes to the same initial state.
s.mut_metadata().index = 1;
s.mut_metadata().term = 1;
s.mut_metadata().mut_conf_state().voters = vec![1];
let storage = MemStorage::new();
storage.wl().apply_snapshot(s).unwrap();
let raft_group = Some(RawNode::new(&cfg, storage, &logger).unwrap());
Node {
raft_group,
Expand Down
2 changes: 1 addition & 1 deletion harness/tests/failpoints_cases/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ fn test_reject_stale_term_message() {
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));
r.load_state(&hard_state(2, 0, 0));

let mut m = new_message(0, 0, MessageType::MsgAppend, 0);
m.term = r.term - 1;
Expand Down
Loading

0 comments on commit e2a1841

Please sign in to comment.