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

add a simple single memory node example #44

Merged
merged 11 commits into from
Apr 2, 2018
Merged

Conversation

siddontang
Copy link
Contributor

Add a very simple example to let the user know how to use the Raft library basically.

PTAL @BusyJay @Hoverbear

use std::thread;
use std::collections::HashMap;

use raft::*;
Copy link
Member

Choose a reason for hiding this comment

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

Maybe we need to provide a prelude for those frequently used structs.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, we can add an issue with help-wanted.


enum Msg {
Propose { id: u8, cb: ProposeCallback },
#[allow(dead_code)] Raft(Message),
Copy link
Member

Choose a reason for hiding this comment

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

Why dead code?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We don't handle Raft message here, but I also want to show how to drive the Raft with step function.

fn main() {
// Create a storage for Raft, here we just use a simple memory storage.
// You need to build your own persistent storage in your production.
// Please check the Storage traint in src/storage.rs to see how to implement one.
Copy link
Member

Choose a reason for hiding this comment

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

s/traint/trait/

// Heartbeat tick is for how long the leader needs to send
// a heartbeat to keepalive.
heartbeat_tick: 3,
// The max size for one message, mostly, 1 MB is enough.
Copy link
Member

Choose a reason for hiding this comment

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

Not consistent with the actual configuration.

Err(RecvTimeoutError::Disconnected) => return,
}

if t.elapsed() >= d {
Copy link
Member

Choose a reason for hiding this comment

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

d should be updated if elapsed is less than d.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

here I don't need an accurate check, and the elapsed is based on the created time, so the elapsed time will be increased next time, seem no need to update d.

Copy link
Member

Choose a reason for hiding this comment

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

For example, if a message arrive right before timeout, then next tick is called after d * 2 instead of d.

@BusyJay
Copy link
Member

BusyJay commented Mar 28, 2018

@queenypingcap PTAL

@siddontang
Copy link
Contributor Author

PTAL @BusyJay

@QueenyJin
Copy link

@lilin90 PTAL.

#[allow(dead_code)] Raft(Message),
}

// A simple example about how to use the raft library in Rust.
Copy link
Member

Choose a reason for hiding this comment

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

raft -> Raft


// A simple example about how to use the raft library in Rust.
fn main() {
// Create a storage for Raft, here we just use a simple memory storage.
Copy link
Member

Choose a reason for hiding this comment

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

-> Create a storage for Raft, and here we just use a simple memory storage.
OR
-> Create a storage for Raft. Here we just use a simple memory storage.

id: 1,
// The Raft node list.
// Mostly, the peers need to be saved in the storage
// and we can get them from Storage::initial_state function, so here
Copy link
Member

Choose a reason for hiding this comment

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

from -> from the

// The Raft node list.
// Mostly, the peers need to be saved in the storage
// and we can get them from Storage::initial_state function, so here
// you need to set empty.
Copy link
Member

Choose a reason for hiding this comment

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

set empty -> set it empty

// it doesn't receive any message from the leader.
election_tick: 10,
// Heartbeat tick is for how long the leader needs to send
// a heartbeat to keepalive.
Copy link
Member

Choose a reason for hiding this comment

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

keepalive -> keep alive

heartbeat_tick: 3,
// The max size limits the max size of each append message, mostly, 1 MB is enough.
max_size_per_msg: 1024 * 1024 * 1024,
// Max inflight msgs that the leader sends message to follower without
Copy link
Member

Choose a reason for hiding this comment

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

message -> messages

let mut t = Instant::now();
let mut timeout = Duration::from_millis(100);

// Use a HashMap to holde the propose callbacks.
Copy link
Member

Choose a reason for hiding this comment

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

holde -> hold
propose -> proposed? (because "propose" is a verb)

}

if let Some(ref hs) = ready.hs {
// Raft HardState changed, we need to persist it.
Copy link
Member

Choose a reason for hiding this comment

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

-> Raft HardState changed. we need to persist it.
OR
-> Raft HardState changed, and we need to persist it.

// the leader after appending Raft entries.
let msgs = ready.messages.drain(..);
for _msg in msgs {
// Send message to other peers.
Copy link
Member

Choose a reason for hiding this comment

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

message -> messages


println!("propose a request");

// Send a command to the Raft, wait the Raft applying it
Copy link
Member

Choose a reason for hiding this comment

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

wait the Raft applying it -> wait for the Raft to apply it

@siddontang
Copy link
Contributor Author

PTAL @lilin90 @BusyJay

Copy link
Member

@lilin90 lilin90 left a comment

Choose a reason for hiding this comment

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

LGTM except line 59. Please update.

// Heartbeat tick is for how long the leader needs to send
// a heartbeat to keep alive.
heartbeat_tick: 3,
// The max size limits the max size of each append message, mostly, 1 MB is enough.
Copy link
Member

Choose a reason for hiding this comment

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

(grammar)
-> The max size limits the max size of each appended message. Mostly, 1 MB is enough.

@lilin90
Copy link
Member

lilin90 commented Apr 2, 2018

LGTM

@siddontang
Copy link
Contributor Author

PTAL @BusyJay

Copy link
Member

@BusyJay BusyJay left a comment

Choose a reason for hiding this comment

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

rest LGTM

cb: ProposeCallback,
},
#[allow(dead_code)]
// Here we don't use Raft Message, so use dead_code to
Copy link
Member

Choose a reason for hiding this comment

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

Please put the comment above attributes.

@siddontang siddontang merged commit 447076a into master Apr 2, 2018
@siddontang siddontang deleted the siddontang/example branch April 2, 2018 13:53
@Hoverbear Hoverbear added this to the 0.2.0 milestone Jul 5, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants