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

raft: port raft part 1 #53

Merged
merged 16 commits into from
Jan 27, 2016
2 changes: 2 additions & 0 deletions src/raft/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ pub enum StorageError {
Compacted,
Unavailable,
SnapshotOutOfDate,
SnapshotTemporarilyUnavailable,
}

impl StorageError {
pub fn string(&self) -> &str {
match self {
&StorageError::Compacted => "log compacted",
&StorageError::Unavailable => "log unavailable",
&StorageError::SnapshotTemporarilyUnavailable => "snapshot temporarily unavailable",
&StorageError::SnapshotOutOfDate => "snapshot out of date",
}
}
Expand Down
1 change: 1 addition & 0 deletions src/raft/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod raft_log;
mod storage;
mod raft;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should export Config for outer use.

mod progress;
mod errors;
mod log_unstable;
31 changes: 21 additions & 10 deletions src/raft/progress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,24 @@
use std::cmp;

#[derive(Debug, PartialEq)]
enum ProgressState {
pub enum ProgressState {
Probe,
Replicate,
Snapshot,
Invalid,
}

struct Progress {
matched: u64,
next_idx: u64,
impl Default for ProgressState {
fn default() -> ProgressState {
ProgressState::Invalid
}
}


#[derive(Debug, Default)]
pub struct Progress {
pub matched: u64,
pub next_idx: u64,
// When in ProgressStateProbe, leader sends at most one replication message
// per heartbeat interval. It also probes actual progress of the follower.
//
Expand All @@ -21,21 +30,21 @@ struct Progress {
//
// When in ProgressStateSnapshot, leader should have sent out snapshot
// before and stops sending any replication message.
state: ProgressState,
pub state: ProgressState,
// Paused is used in ProgressStateProbe.
// When Paused is true, raft should pause sending replication message to this peer.
paused: bool,
pub paused: bool,
// pending_snapshot is used in ProgressStateSnapshot.
// If there is a pending snapshot, the pendingSnapshot will be set to the
// index of the snapshot. If pendingSnapshot is set, the replication process of
// this Progress will be paused. raft will not resend snapshot until the pending one
// is reported to be failed.
pending_snapshot: u64,
pub pending_snapshot: u64,

// recent_active is true if the progress is recently active. Receiving any messages
// from the corresponding follower indicates the progress is active.
// RecentActive can be reset to false after an election timeout.
recent_active: bool,
pub recent_active: bool,

// Inflights is a sliding window for the inflight messages.
// When inflights is full, no more message should be sent.
Expand All @@ -44,10 +53,11 @@ struct Progress {
// into inflights in order.
// When a leader receives a reply, the previous inflights should
// be freed by calling inflights.freeTo.
ins: Inflights,
pub ins: Inflights,
}



impl Progress {
pub fn reset_state(&mut self, state: ProgressState) {
self.paused = false;
Expand Down Expand Up @@ -143,6 +153,7 @@ impl Progress {
ProgressState::Probe => self.paused,
ProgressState::Replicate => self.ins.full(),
ProgressState::Snapshot => true,
ProgressState::Invalid => panic!("invalid ProgressState"),
}
}

Expand All @@ -157,7 +168,7 @@ impl Progress {


#[derive(Debug, Default)]
struct Inflights {
pub struct Inflights {
// the starting index in the buffer
start: usize,
// number of inflights in the buffer
Expand Down
Loading