Skip to content
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

Store heartbeat messages in a independent vector #395

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 17 additions & 2 deletions src/raft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ pub struct Raft<T: Storage> {
/// The list of messages.
pub msgs: Vec<Message>,

/// The list of heartbeat messages.
pub heartbeats: Vec<Message>,

/// The leader id
pub leader_id: u64,

Expand Down Expand Up @@ -246,6 +249,7 @@ impl<T: Storage> Raft<T> {
election_timeout: c.election_tick,
votes: Default::default(),
msgs: Default::default(),
heartbeats: Default::default(),
leader_id: Default::default(),
lead_transferee: None,
term: Default::default(),
Expand Down Expand Up @@ -710,10 +714,12 @@ impl<T: Storage> Raft<T> {
m.set_msg_type(MessageType::MsgHeartbeat);
let commit = cmp::min(pr.matched, self.raft_log.committed);
m.commit = commit;
m.from = self.id;
m.term = self.term;
if let Some(context) = ctx {
m.context = context;
}
self.send(m);
self.heartbeats.push(m);
}

/// Sends RPC, with entries to all peers that are not up-to-date
Expand Down Expand Up @@ -2005,7 +2011,16 @@ impl<T: Storage> Raft<T> {
to_send.to = m.from;
to_send.context = m.take_context();
to_send.commit = self.raft_log.committed;
self.send(to_send);
if self.raft_log.committed > self.raft_log.store.last_index().unwrap()
{
// If there is some entries that has committed in memory but not persisted, the message
// shall not be sent until all entries before committed_index have been persisted.
self.send(to_send);
} else {
to_send.term = self.term;
to_send.from = self.id;
self.heartbeats.push(to_send);
}
}

fn handle_snapshot(&mut self, mut m: Message) {
Expand Down