Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

refactor(Ready): add pub getters and make fields private #120

Merged
merged 5 commits into from
Nov 2, 2018
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions examples/single_mem_node/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,20 +126,20 @@ fn on_ready(r: &mut RawNode<MemStorage>, cbs: &mut HashMap<u8, ProposeCallback>)
}
}

if !raft::is_empty_snap(&ready.snapshot) {
if !raft::is_empty_snap(ready.snapshot()) {
// This is a snapshot, we need to apply the snapshot at first.
r.mut_store()
.wl()
.apply_snapshot(ready.snapshot.clone())
.apply_snapshot(ready.snapshot().clone())
.unwrap();
}

if !ready.entries.is_empty() {
if !ready.entries().is_empty() {
// Append entries to the Raft log
r.mut_store().wl().append(&ready.entries).unwrap();
r.mut_store().wl().append(ready.entries()).unwrap();
}

if let Some(ref hs) = ready.hs {
if let Some(hs) = ready.hs() {
// Raft HardState changed, and we need to persist it.
r.mut_store().wl().set_hardstate(hs.clone());
}
Expand Down
8 changes: 4 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,11 +185,11 @@ The `Ready` state contains quite a bit of information, and you need to check and
1. Check whether `snapshot` is empty or not. If not empty, it means that the Raft node has received a Raft snapshot from the leader and we must apply the snapshot:

```rust,ignore
if !raft::is_empty_snap(&ready.snapshot) {
if !raft::is_empty_snap(ready.snapshot()) {
// This is a snapshot, we need to apply the snapshot at first.
node.mut_store()
.wl()
.apply_snapshot(ready.snapshot.clone())
.apply_snapshot(ready.snapshot().clone())
.unwrap();
}

Expand All @@ -200,15 +200,15 @@ The `Ready` state contains quite a bit of information, and you need to check and
```rust,ignore
if !ready.entries.is_empty() {
// Append entries to the Raft log
node.mut_store().wl().append(&ready.entries).unwrap();
node.mut_store().wl().append(ready.entries()).unwrap();
}

```

3. Check whether `hs` is empty or not. If not empty, it means that the `HardState` of the node has changed. For example, the node may vote for a new leader, or the commit index has been increased. We must persist the changed `HardState`:

```rust,ignore
if let Some(ref hs) = ready.hs {
if let Some(hs) = ready.hs() {
// Raft HardState changed, and we need to persist it.
node.mut_store().wl().set_hardstate(hs.clone());
}
Expand Down
72 changes: 51 additions & 21 deletions src/raw_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,28 +96,15 @@ pub fn is_empty_snap(s: &Snapshot) -> bool {
/// All fields in Ready are read-only.
#[derive(Default, Debug, PartialEq)]
pub struct Ready {
/// The current volatile state of a Node.
/// SoftState will be nil if there is no update.
/// It is not required to consume or store SoftState.
pub ss: Option<SoftState>,
ss: Option<SoftState>,

/// The current state of a Node to be saved to stable storage BEFORE
/// Messages are sent.
/// HardState will be equal to empty state if there is no update.
pub hs: Option<HardState>,
hs: Option<HardState>,

/// States can be used for node to serve linearizable read requests locally
/// when its applied index is greater than the index in ReadState.
/// Note that the read_state will be returned when raft receives MsgReadIndex.
/// The returned is only valid for the request that requested to read.
pub read_states: Vec<ReadState>,
read_states: Vec<ReadState>,

/// Entries specifies entries to be saved to stable storage BEFORE
/// Messages are sent.
pub entries: Vec<Entry>,
entries: Vec<Entry>,

/// Snapshot specifies the snapshot to be saved to stable storage.
pub snapshot: Snapshot,
snapshot: Snapshot,

/// CommittedEntries specifies entries to be committed to a
/// store/state-machine. These have previously been committed to stable
Expand All @@ -130,9 +117,7 @@ pub struct Ready {
/// when the snapshot has been received or has failed by calling ReportSnapshot.
pub messages: Vec<Message>,

/// MustSync indicates whether the HardState and Entries must be synchronously
/// written to disk or if an asynchronous write is permissible.
pub must_sync: bool,
must_sync: bool,
}

impl Ready {
Expand Down Expand Up @@ -174,6 +159,51 @@ impl Ready {
}
rd
}

/// The current volatile state of a Node.
/// SoftState will be nil if there is no update.
/// It is not required to consume or store SoftState.
#[inline]
pub fn ss(&self) -> Option<&SoftState> {
self.ss.as_ref()
}

/// The current state of a Node to be saved to stable storage BEFORE
/// Messages are sent.
/// HardState will be equal to empty state if there is no update.
#[inline]
pub fn hs(&self) -> Option<&HardState> {
self.hs.as_ref()
}

/// States can be used for node to serve linearizable read requests locally
/// when its applied index is greater than the index in ReadState.
/// Note that the read_state will be returned when raft receives MsgReadIndex.
/// The returned is only valid for the request that requested to read.
#[inline]
pub fn read_states(&self) -> &[ReadState] {
&self.read_states
}

/// Entries specifies entries to be saved to stable storage BEFORE
/// Messages are sent.
#[inline]
pub fn entries(&self) -> &[Entry] {
&self.entries
}

/// Snapshot specifies the snapshot to be saved to stable storage.
#[inline]
pub fn snapshot(&self) -> &Snapshot {
&self.snapshot
}

/// MustSync indicates whether the HardState and Entries must be synchronously
/// written to disk or if an asynchronous write is permissible.
#[inline]
pub fn must_sync(&self) -> bool {
self.must_sync
}
}

/// RawNode is a thread-unsafe Node.
Expand Down
Loading