-
Notifications
You must be signed in to change notification settings - Fork 401
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
Cherry pick all 0.4.x #263
Merged
Merged
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
813fc4e
Check pending conf change before campaign (#225)
BusyJay f112f30
Add more convenient lite-weight interfaces (#227)
BusyJay c4d0710
*: bump to 0.4.2 (#228)
BusyJay 0f59e7f
Bump to v0.4.3 (#231)
overvenus e6d98a4
Request snapshot (#243)
overvenus d3bf118
fix tests
hicqu 22a016c
cargo fmt
hicqu bd43634
address comments.
hicqu a574621
Merge branch 'master' into cherry-pick-all-0.4.x
BusyJay 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
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
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 |
---|---|---|
|
@@ -112,6 +112,10 @@ pub struct Raft<T: Storage> { | |
/// The maximum length (in bytes) of all the entries. | ||
pub max_msg_size: u64, | ||
|
||
/// The peer is requesting snapshot, it is the index that the follower | ||
/// needs it to be included in a snapshot. | ||
pub pending_request_snapshot: u64, | ||
|
||
prs: Option<ProgressSet>, | ||
|
||
/// The current role of this node. | ||
|
@@ -253,6 +257,7 @@ impl<T: Storage> Raft<T> { | |
max_inflight: c.max_inflight_msgs, | ||
max_msg_size: c.max_size_per_msg, | ||
prs: Some(ProgressSet::with_capacity(peers.len(), learners.len())), | ||
pending_request_snapshot: INVALID_INDEX, | ||
state: StateRole::Follower, | ||
is_learner: false, | ||
check_quorum: c.check_quorum, | ||
|
@@ -524,7 +529,7 @@ impl<T: Storage> Raft<T> { | |
} | ||
|
||
m.set_msg_type(MessageType::MsgSnapshot); | ||
let snapshot_r = self.raft_log.snapshot(); | ||
let snapshot_r = self.raft_log.snapshot(pr.pending_request_snapshot); | ||
if let Err(e) = snapshot_r { | ||
if e == Error::Store(StorageError::SnapshotTemporarilyUnavailable) { | ||
debug!( | ||
|
@@ -619,34 +624,28 @@ impl<T: Storage> Raft<T> { | |
); | ||
return; | ||
} | ||
let term = self.raft_log.term(pr.next_idx - 1); | ||
let ents = self.raft_log.entries(pr.next_idx, self.max_msg_size); | ||
let mut m = Message::default(); | ||
m.to = to; | ||
if term.is_err() || ents.is_err() { | ||
// send snapshot if we failed to get term or entries | ||
trace!( | ||
self.logger, | ||
"Skipping sending to {to}", | ||
to = to; | ||
"index" => pr.next_idx, | ||
"tag" => &self.tag, | ||
"term" => ?term, | ||
"ents" => ?ents, | ||
); | ||
if pr.pending_request_snapshot != INVALID_INDEX { | ||
// Check pending request snapshot first to avoid unnecessary loading entries. | ||
if !self.prepare_send_snapshot(&mut m, pr, to) { | ||
return; | ||
} | ||
} else { | ||
let mut ents = ents.unwrap(); | ||
if self.batch_append { | ||
let batched = self.try_batching(to, pr, &mut ents); | ||
if batched { | ||
let term = self.raft_log.term(pr.next_idx - 1); | ||
let ents = self.raft_log.entries(pr.next_idx, self.max_msg_size); | ||
if term.is_err() || ents.is_err() { | ||
// send snapshot if we failed to get term or entries. | ||
if !self.prepare_send_snapshot(&mut m, pr, to) { | ||
return; | ||
} | ||
} else { | ||
let mut ents = ents.unwrap(); | ||
if self.batch_append && self.try_batching(to, pr, &mut ents) { | ||
return; | ||
} | ||
self.prepare_send_entries(&mut m, pr, term.unwrap(), ents); | ||
} | ||
let term = term.unwrap(); | ||
self.prepare_send_entries(&mut m, pr, term, ents); | ||
} | ||
self.send(m); | ||
} | ||
|
@@ -768,6 +767,7 @@ impl<T: Storage> Raft<T> { | |
|
||
self.pending_conf_index = 0; | ||
self.read_only = ReadOnly::new(self.read_only.option); | ||
self.pending_request_snapshot = INVALID_INDEX; | ||
|
||
let last_index = self.raft_log.last_index(); | ||
let self_id = self.id; | ||
|
@@ -857,9 +857,11 @@ impl<T: Storage> Raft<T> { | |
|
||
/// Converts this node to a follower. | ||
pub fn become_follower(&mut self, term: u64, leader_id: u64) { | ||
let pending_request_snapshot = self.pending_request_snapshot; | ||
self.reset(term); | ||
self.leader_id = leader_id; | ||
self.state = StateRole::Follower; | ||
self.pending_request_snapshot = pending_request_snapshot; | ||
info!( | ||
self.logger, | ||
"became follower at term {term}", | ||
|
@@ -1459,7 +1461,7 @@ impl<T: Storage> Raft<T> { | |
"tag" => &self.tag, | ||
); | ||
|
||
if pr.maybe_decr_to(m.index, m.reject_hint) { | ||
if pr.maybe_decr_to(m.index, m.reject_hint, m.request_snapshot) { | ||
debug!( | ||
self.logger, | ||
"decreased progress of {}", | ||
|
@@ -1531,7 +1533,8 @@ impl<T: Storage> Raft<T> { | |
if pr.state == ProgressState::Replicate && pr.ins.full() { | ||
pr.ins.free_first_one(); | ||
} | ||
if pr.matched < self.raft_log.last_index() { | ||
// Does it request snapshot? | ||
if pr.matched < self.raft_log.last_index() || pr.pending_request_snapshot != INVALID_INDEX { | ||
*send_append = true; | ||
} | ||
|
||
|
@@ -1657,6 +1660,7 @@ impl<T: Storage> Raft<T> { | |
// out the next msgAppend. | ||
// If snapshot failure, wait for a heartbeat interval before next try | ||
pr.pause(); | ||
pr.pending_request_snapshot = INVALID_INDEX; | ||
} | ||
|
||
/// Check message's progress to decide which action should be taken. | ||
|
@@ -2062,16 +2066,55 @@ impl<T: Storage> Raft<T> { | |
Ok(()) | ||
} | ||
|
||
/// Request a snapshot from a leader. | ||
pub fn request_snapshot(&mut self, request_index: u64) -> Result<()> { | ||
if self.state == StateRole::Leader { | ||
info!( | ||
self.logger, | ||
"can not request snapshot on leader; dropping request snapshot"; | ||
"tag" => &self.tag, | ||
); | ||
} else if self.leader_id == INVALID_ID { | ||
info!( | ||
self.logger, | ||
"drop request snapshot because of no leader"; | ||
"tag" => &self.tag, "term" => self.term, | ||
); | ||
} else if self.get_snap().is_some() { | ||
info!( | ||
self.logger, | ||
"there is a pending snapshot; dropping request snapshot"; | ||
"tag" => &self.tag, | ||
); | ||
} else if self.pending_request_snapshot != INVALID_INDEX { | ||
info!( | ||
self.logger, | ||
"there is a pending snapshot; dropping request snapshot"; | ||
"tag" => &self.tag, | ||
); | ||
} else { | ||
self.pending_request_snapshot = request_index; | ||
self.send_request_snapshot(); | ||
return Ok(()); | ||
} | ||
Err(Error::RequestSnapshotDropped) | ||
} | ||
|
||
// TODO: revoke pub when there is a better way to test. | ||
/// For a given message, append the entries to the log. | ||
pub fn handle_append_entries(&mut self, m: &Message) { | ||
if self.pending_request_snapshot != INVALID_INDEX { | ||
self.send_request_snapshot(); | ||
return; | ||
} | ||
if m.index < self.raft_log.committed { | ||
debug!( | ||
self.logger, | ||
"got message with lower index than committed."; | ||
"tag" => &self.tag, | ||
); | ||
let mut to_send = Message::default(); | ||
to_send.to = m.from; | ||
to_send.set_msg_type(MessageType::MsgAppendResponse); | ||
to_send.to = m.from; | ||
to_send.index = self.raft_log.committed; | ||
|
@@ -2115,7 +2158,12 @@ impl<T: Storage> Raft<T> { | |
/// For a message, commit and send out heartbeat. | ||
pub fn handle_heartbeat(&mut self, mut m: Message) { | ||
self.raft_log.commit_to(m.commit); | ||
if self.pending_request_snapshot != INVALID_INDEX { | ||
self.send_request_snapshot(); | ||
return; | ||
} | ||
let mut to_send = Message::default(); | ||
to_send.to = m.from; | ||
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. Same as above. |
||
to_send.set_msg_type(MessageType::MsgHeartbeatResponse); | ||
to_send.to = m.from; | ||
to_send.context = m.take_context(); | ||
|
@@ -2160,7 +2208,10 @@ impl<T: Storage> Raft<T> { | |
|
||
fn restore_raft(&mut self, snap: &Snapshot) -> Option<bool> { | ||
let meta = snap.get_metadata(); | ||
if self.raft_log.match_term(meta.index, meta.term) { | ||
// Do not fast-forward commit if we are requesting snapshot. | ||
if self.pending_request_snapshot == INVALID_INDEX | ||
&& self.raft_log.match_term(meta.index, meta.term) | ||
{ | ||
info!( | ||
self.logger, | ||
"[commit: {commit}, lastindex: {last_index}, lastterm: {last_term}] fast-forwarded commit to \ | ||
|
@@ -2220,6 +2271,7 @@ impl<T: Storage> Raft<T> { | |
conf_change.start_index = meta.pending_membership_change_index; | ||
self.pending_membership_change = Some(conf_change); | ||
} | ||
self.pending_request_snapshot = INVALID_INDEX; | ||
None | ||
} | ||
|
||
|
@@ -2503,4 +2555,15 @@ impl<T: Storage> Raft<T> { | |
pub fn is_in_membership_change(&self) -> bool { | ||
self.prs().is_in_membership_change() | ||
} | ||
|
||
fn send_request_snapshot(&mut self) { | ||
let mut m = Message::default(); | ||
m.set_msg_type(MessageType::MsgAppendResponse); | ||
m.index = self.raft_log.committed; | ||
m.reject = true; | ||
m.reject_hint = self.raft_log.last_index(); | ||
m.to = self.leader_id; | ||
m.request_snapshot = self.pending_request_snapshot; | ||
self.send(m); | ||
} | ||
} |
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
Oops, something went wrong.
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.
Dup with line 2119.