-
Notifications
You must be signed in to change notification settings - Fork 409
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
Conversation
examples/single_mem_node/main.rs
Outdated
use std::thread; | ||
use std::collections::HashMap; | ||
|
||
use raft::*; |
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.
Maybe we need to provide a prelude for those frequently used structs.
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.
Yes, we can add an issue with help-wanted.
examples/single_mem_node/main.rs
Outdated
|
||
enum Msg { | ||
Propose { id: u8, cb: ProposeCallback }, | ||
#[allow(dead_code)] Raft(Message), |
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.
Why dead code?
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.
We don't handle Raft message here, but I also want to show how to drive the Raft with step
function.
examples/single_mem_node/main.rs
Outdated
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. |
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.
s/traint/trait/
examples/single_mem_node/main.rs
Outdated
// 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. |
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.
Not consistent with the actual configuration.
examples/single_mem_node/main.rs
Outdated
Err(RecvTimeoutError::Disconnected) => return, | ||
} | ||
|
||
if t.elapsed() >= d { |
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.
d
should be updated if elapsed
is less than d.
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.
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.
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.
For example, if a message arrive right before timeout, then next tick is called after d * 2
instead of d.
@queenypingcap PTAL |
PTAL @BusyJay |
@lilin90 PTAL. |
examples/single_mem_node/main.rs
Outdated
#[allow(dead_code)] Raft(Message), | ||
} | ||
|
||
// A simple example about how to use the raft library in Rust. |
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.
raft -> Raft
examples/single_mem_node/main.rs
Outdated
|
||
// 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. |
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.
-> 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.
examples/single_mem_node/main.rs
Outdated
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 |
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.
from -> from the
examples/single_mem_node/main.rs
Outdated
// 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. |
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.
set empty -> set it empty
examples/single_mem_node/main.rs
Outdated
// 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. |
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.
keepalive -> keep alive
examples/single_mem_node/main.rs
Outdated
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 |
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.
message -> messages
examples/single_mem_node/main.rs
Outdated
let mut t = Instant::now(); | ||
let mut timeout = Duration::from_millis(100); | ||
|
||
// Use a HashMap to holde the propose callbacks. |
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.
holde -> hold
propose -> proposed? (because "propose" is a verb)
examples/single_mem_node/main.rs
Outdated
} | ||
|
||
if let Some(ref hs) = ready.hs { | ||
// Raft HardState changed, we need to persist it. |
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.
-> Raft HardState changed. we need to persist it.
OR
-> Raft HardState changed, and we need to persist it.
examples/single_mem_node/main.rs
Outdated
// the leader after appending Raft entries. | ||
let msgs = ready.messages.drain(..); | ||
for _msg in msgs { | ||
// Send message to other peers. |
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.
message -> messages
examples/single_mem_node/main.rs
Outdated
|
||
println!("propose a request"); | ||
|
||
// Send a command to the Raft, wait the Raft applying it |
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.
wait the Raft applying it -> wait for the Raft to apply it
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.
LGTM except line 59. Please update.
examples/single_mem_node/main.rs
Outdated
// 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. |
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.
(grammar)
-> The max size limits the max size of each appended message. Mostly, 1 MB is enough.
LGTM |
PTAL @BusyJay |
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.
rest LGTM
examples/single_mem_node/main.rs
Outdated
cb: ProposeCallback, | ||
}, | ||
#[allow(dead_code)] | ||
// Here we don't use Raft Message, so use dead_code to |
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.
Please put the comment above attributes.
Add a very simple example to let the user know how to use the Raft library basically.
PTAL @BusyJay @Hoverbear