From 85ec6c2c7e99fb919f9702b3a5a558bcf6e498bb Mon Sep 17 00:00:00 2001 From: Dharan Aditya Date: Sun, 11 Aug 2024 15:43:39 +0530 Subject: [PATCH] [Fix] Failing Examples (#31) --- examples/simple_run.rs | 9 +++++---- examples/simulate_add_node.rs | 24 +++++++++--------------- examples/simulate_node_failure.rs | 9 +++++---- 3 files changed, 19 insertions(+), 23 deletions(-) diff --git a/examples/simple_run.rs b/examples/simple_run.rs index cf6904e..5e25e5e 100644 --- a/examples/simple_run.rs +++ b/examples/simple_run.rs @@ -28,13 +28,14 @@ async fn main() { NodeMeta::from((4, SocketAddr::from_str("127.0.0.1:5004").unwrap())), NodeMeta::from((5, SocketAddr::from_str("127.0.0.1:5005").unwrap())), ]; - let cluster_config = ClusterConfig::new(peers); + let cluster_config = ClusterConfig::new(peers.clone()); // Create server configs - let configs: Vec<_> = cluster_nodes + let configs: Vec<_> = peers + .clone() .iter() - .map(|&id| ServerConfig { + .map(|n| ServerConfig { election_timeout: Duration::from_millis(200), - address: SocketAddr::from_str(format!("127.0.0.1:{}", 5000 + id).as_str()).unwrap(), + address: n.address, default_leader: Some(1u32), leadership_preferences: HashMap::new(), storage_location: Some("logs/".to_string()), diff --git a/examples/simulate_add_node.rs b/examples/simulate_add_node.rs index 01e92f6..eb036d0 100644 --- a/examples/simulate_add_node.rs +++ b/examples/simulate_add_node.rs @@ -25,13 +25,14 @@ async fn main() { NodeMeta::from((4, SocketAddr::from_str("127.0.0.1:5004").unwrap())), NodeMeta::from((5, SocketAddr::from_str("127.0.0.1:5005").unwrap())), ]; - let mut cluster_config = ClusterConfig::new(peers); + let mut cluster_config = ClusterConfig::new(peers.clone()); // Create server configs - let configs: Vec<_> = cluster_nodes + let configs: Vec<_> = peers + .clone() .iter() - .map(|&id| ServerConfig { + .map(|n| ServerConfig { election_timeout: Duration::from_millis(1000), - address: SocketAddr::from_str(format!("127.0.0.1:{}", 5000 + id).as_str()).unwrap(), + address: n.address, default_leader: Some(1 as u32), leadership_preferences: HashMap::new(), storage_location: Some("logs/".to_string()), @@ -54,15 +55,8 @@ async fn main() { // The following defines the basic configuration of the new node tokio::time::sleep(Duration::from_secs(10)).await; let new_node_id = 6; - let new_node_port = 5006; - let new_node_address = - SocketAddr::from_str(format!("127.0.0.1:{}", new_node_port).as_str()).unwrap(); - cluster_nodes.push(new_node_id); - // id_to_address_mapping.insert(new_node_id, new_node_address.to_string()); - cluster_config.add_server(NodeMeta::from(( - new_node_id, - new_node_address.clone().into(), - ))); + let new_node_address = SocketAddr::from_str(format!("127.0.0.1:{}", 5006).as_str()).unwrap(); + let new_node_conf = ServerConfig { election_timeout: Duration::from_millis(1000), address: new_node_address.clone().into(), @@ -74,7 +68,7 @@ async fn main() { // Launching a new node handles.push(thread::spawn(move || { let rt = Runtime::new().unwrap(); - let mut server = Server::new(6, new_node_conf, cluster_config); + let mut server = Server::new(new_node_id, new_node_conf, cluster_config); rt.block_on(server.start()); })); @@ -99,7 +93,7 @@ async fn add_node_request(new_node_id: u32, addr: SocketAddr) { new_node_id.to_be_bytes().to_vec(), 0u32.to_be_bytes().to_vec(), 10u32.to_be_bytes().to_vec(), - addr.ip().to_string().as_bytes().to_vec(), + addr.to_string().as_bytes().to_vec(), ] .concat(); diff --git a/examples/simulate_node_failure.rs b/examples/simulate_node_failure.rs index 6bb117e..98feb24 100644 --- a/examples/simulate_node_failure.rs +++ b/examples/simulate_node_failure.rs @@ -24,14 +24,15 @@ async fn main() { NodeMeta::from((4, SocketAddr::from_str("127.0.0.1:5004").unwrap())), NodeMeta::from((5, SocketAddr::from_str("127.0.0.1:5005").unwrap())), ]; - let cluster_config = ClusterConfig::new(peers); + let cluster_config = ClusterConfig::new(peers.clone()); // Create server configs - let configs: Vec<_> = cluster_nodes + let configs: Vec<_> = peers + .clone() .iter() - .map(|&id| ServerConfig { + .map(|n| ServerConfig { election_timeout: Duration::from_millis(200), - address: SocketAddr::from_str(format!("127.0.0.1:{}", 5000 + id).as_str()).unwrap(), + address: n.address, default_leader: Some(1), leadership_preferences: HashMap::new(), storage_location: Some("logs/".to_string()),