|
@@ -1,9 +1,15 @@
|
|
|
|
|
+use std::collections::HashMap;
|
|
|
|
|
+use std::sync::Arc;
|
|
|
|
|
+use std::time::Instant;
|
|
|
|
|
+
|
|
|
|
|
+pub use anyhow::Result;
|
|
|
use parking_lot::Mutex;
|
|
use parking_lot::Mutex;
|
|
|
use rand::{thread_rng, Rng};
|
|
use rand::{thread_rng, Rng};
|
|
|
|
|
+use tokio::time::Duration;
|
|
|
|
|
+
|
|
|
use ruaft::rpcs::register_server;
|
|
use ruaft::rpcs::register_server;
|
|
|
|
|
+use ruaft::utils::DropGuard;
|
|
|
use ruaft::{Raft, RpcClient};
|
|
use ruaft::{Raft, RpcClient};
|
|
|
-use std::collections::HashMap;
|
|
|
|
|
-use std::sync::Arc;
|
|
|
|
|
|
|
|
|
|
struct ConfigState {
|
|
struct ConfigState {
|
|
|
rafts: Vec<Option<Raft>>,
|
|
rafts: Vec<Option<Raft>>,
|
|
@@ -23,11 +29,6 @@ pub struct Config {
|
|
|
log: Arc<Mutex<LogState>>,
|
|
log: Arc<Mutex<LogState>>,
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-pub use anyhow::Result;
|
|
|
|
|
-use ruaft::utils::DropGuard;
|
|
|
|
|
-use std::time::Instant;
|
|
|
|
|
-use tokio::time::Duration;
|
|
|
|
|
-
|
|
|
|
|
impl Config {
|
|
impl Config {
|
|
|
fn server_name(i: usize) -> String {
|
|
fn server_name(i: usize) -> String {
|
|
|
format!("ruaft-server-{}", i)
|
|
format!("ruaft-server-{}", i)
|
|
@@ -226,7 +227,7 @@ impl Config {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- pub fn start1(&mut self, index: usize) -> std::io::Result<()> {
|
|
|
|
|
|
|
+ pub fn start1(&mut self, index: usize) -> Result<()> {
|
|
|
if self.state.lock().rafts[index].is_some() {
|
|
if self.state.lock().rafts[index].is_some() {
|
|
|
self.crash1(index);
|
|
self.crash1(index);
|
|
|
}
|
|
}
|
|
@@ -249,7 +250,22 @@ impl Config {
|
|
|
self.state.lock().rafts[index].replace(raft.clone());
|
|
self.state.lock().rafts[index].replace(raft.clone());
|
|
|
|
|
|
|
|
let raft = Arc::new(raft);
|
|
let raft = Arc::new(raft);
|
|
|
- register_server(raft, Self::server_name(index), self.network.as_ref())
|
|
|
|
|
|
|
+ register_server(raft, Self::server_name(index), self.network.as_ref())?;
|
|
|
|
|
+ Ok(())
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ pub fn leader_start(
|
|
|
|
|
+ &self,
|
|
|
|
|
+ leader: usize,
|
|
|
|
|
+ cmd: i32,
|
|
|
|
|
+ ) -> Option<(usize, usize)> {
|
|
|
|
|
+ self.state.lock().rafts[leader]
|
|
|
|
|
+ .as_ref()
|
|
|
|
|
+ .map(|raft| {
|
|
|
|
|
+ raft.start(ruaft::Command(cmd))
|
|
|
|
|
+ .map(|(term, index)| (term.0, index))
|
|
|
|
|
+ })
|
|
|
|
|
+ .unwrap()
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
pub fn end(&self) {}
|
|
pub fn end(&self) {}
|