Skip to content

Commit

Permalink
[Fix] Failing Examples (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
dharanad authored Aug 11, 2024
1 parent 83a2e52 commit 85ec6c2
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 23 deletions.
9 changes: 5 additions & 4 deletions examples/simple_run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand Down
24 changes: 9 additions & 15 deletions examples/simulate_add_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand All @@ -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(),
Expand All @@ -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());
}));

Expand All @@ -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();

Expand Down
9 changes: 5 additions & 4 deletions examples/simulate_node_failure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand Down

0 comments on commit 85ec6c2

Please sign in to comment.