Skip to content
Snippets Groups Projects
Verified Commit 90038923 authored by Yaroslav Dynnikov's avatar Yaroslav Dynnikov
Browse files

feature: pass raft_id as an env var

Run it:

```
PICODATA_RAFT_ID=2 cargo run run
```

This is a pre-requisite for further two-node configuration development.
This is a temporary hack until fair joining is implemented.

Close https://gitlab.com/picodata/picodata/picodata/-/issues/16
parent 5abf278b
No related branches found
No related tags found
1 merge request!27feature: pass raft_id as an env var
Pipeline #3475 passed
......@@ -121,8 +121,41 @@ fn main_run() {
);
traft::Storage::init_schema();
let raft_id: u64 = {
// The id already stored in tarantool snashot
let snap_id: Option<u64> = traft::Storage::id();
// The id passed in env vars (or command line args)
let args_id: Option<u64> = std::env::var("PICODATA_RAFT_ID")
.and_then(|v| {
v.parse()
.map_err(|e| panic!("Bad PICODATA_RAFT_ID value \"{}\": {}", v, e))
})
.ok();
match snap_id {
None => {
let id: u64 = args_id.unwrap_or(1);
traft::Storage::persist_id(id);
id
}
Some(snap_id) => match args_id {
Some(args_id) if args_id != snap_id => {
panic!(
"Already initialized with a different PICODATA_RAFT_ID:
snapshot: {s}
from args: {a}",
s = snap_id,
a = args_id
)
}
_ => snap_id,
},
}
};
let raft_cfg = raft::Config {
id: 1,
id: raft_id,
applied: traft::Storage::applied().unwrap_or_default(),
..Default::default()
};
......
......@@ -86,6 +86,10 @@ impl Storage {
.unwrap()
}
pub fn id() -> Option<u64> {
Storage::raft_state("id")
}
pub fn term() -> Option<u64> {
Storage::raft_state("term")
}
......@@ -118,6 +122,15 @@ impl Storage {
Storage::persist_raft_state("vote", vote)
}
pub fn persist_id(id: u64) {
// We use `insert` instead of `replace` here
// because `id` can never be changed.
Space::find(SPACE_RAFT_STATE)
.unwrap()
.insert(&("id", id))
.unwrap();
}
pub fn entries(low: u64, high: u64) -> Vec<raft::Entry> {
let mut ret: Vec<raft::Entry> = vec![];
let space = Space::find(SPACE_RAFT_LOG).unwrap();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment