-
Notifications
You must be signed in to change notification settings - Fork 406
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix a bug about snapshot and adjust some test cases #213
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -61,21 +61,39 @@ pub struct Network { | |||||
} | ||||||
|
||||||
impl Network { | ||||||
/// Initializes a network from peers. | ||||||
/// Get a base config. Calling `Network::new` will initialize peers with this config. | ||||||
pub fn base_config() -> Config { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
Config { | ||||||
election_tick: 10, | ||||||
heartbeat_tick: 1, | ||||||
max_size_per_msg: NO_LIMIT, | ||||||
max_inflight_msgs: 256, | ||||||
..Default::default() | ||||||
} | ||||||
} | ||||||
|
||||||
/// Initializes a network from `peers`. | ||||||
/// | ||||||
/// Nodes will recieve their ID based on their index in the vector, starting with 1. | ||||||
/// | ||||||
/// A `None` node will be replaced with a new Raft node. | ||||||
/// A `None` node will be replaced with a new Raft node, and its configuration will | ||||||
/// be `peers`. | ||||||
pub fn new(peers: Vec<Option<Interface>>) -> Network { | ||||||
Network::new_with_config(peers, false) | ||||||
let config = Network::base_config(); | ||||||
Network::new_with_config(peers, &config) | ||||||
} | ||||||
|
||||||
/// Explicitly set the pre_vote option on newly created rafts. | ||||||
/// | ||||||
/// **TODO:** Make this accept any config. | ||||||
pub fn new_with_config(mut peers: Vec<Option<Interface>>, pre_vote: bool) -> Network { | ||||||
let size = peers.len(); | ||||||
let peer_addrs: Vec<u64> = (1..=size as u64).collect(); | ||||||
/// Initialize a network from `peers` with explicitly specified `config`. | ||||||
pub fn new_with_config(mut peers: Vec<Option<Interface>>, config: &Config) -> Network { | ||||||
let peer_addrs: Vec<u64> = (1..=peers.len() as u64).collect(); | ||||||
for (i, id) in peer_addrs.iter().enumerate() { | ||||||
if let Some(ref peer) = peers[i] { | ||||||
if peer.raft.as_ref().map_or(false, |r| r.id != *id) { | ||||||
panic!("peer {} in peers has a wrong position", peer.id); | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
let mut nstorage = HashMap::new(); | ||||||
let mut npeers = HashMap::new(); | ||||||
for (p, id) in peers.drain(..).zip(peer_addrs.clone()) { | ||||||
|
@@ -84,23 +102,13 @@ impl Network { | |||||
let conf_state = ConfState::from((peer_addrs.clone(), vec![])); | ||||||
let store = MemStorage::new_with_conf_state(conf_state); | ||||||
nstorage.insert(id, store.clone()); | ||||||
let config = Config { | ||||||
id, | ||||||
election_tick: 10, | ||||||
heartbeat_tick: 1, | ||||||
max_size_per_msg: NO_LIMIT, | ||||||
max_inflight_msgs: 256, | ||||||
pre_vote, | ||||||
tag: format!("{}", id), | ||||||
..Default::default() | ||||||
}; | ||||||
let mut config = config.clone(); | ||||||
config.id = id; | ||||||
config.tag = format!("{}", id); | ||||||
let r = Raft::new(&config, store).unwrap().into(); | ||||||
npeers.insert(id, r); | ||||||
} | ||||||
Some(mut p) => { | ||||||
p.initial(id, &peer_addrs); | ||||||
npeers.insert(id, p); | ||||||
} | ||||||
Some(r) => drop(npeers.insert(id, r)), | ||||||
} | ||||||
} | ||||||
Network { | ||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -199,7 +199,12 @@ impl<T: Storage> RaftLog<T> { | |
|
||
/// Answers the question: Does this index belong to this term? | ||
pub fn match_term(&self, idx: u64, term: u64) -> bool { | ||
self.term(idx).map(|t| t == term).unwrap_or(false) | ||
match self.term(idx) { | ||
// For uninitialized storage, should return false. | ||
Ok(0) => term == 0 && self.last_index() > 0, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think it need to add some logics that exceeds etcd-raft. |
||
Ok(t) => t == term, | ||
_ => false, | ||
} | ||
} | ||
|
||
/// Returns None if the entries cannot be appended. Otherwise, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, which reason? That the program will be blocked forever?