Crates.io | swim-rs |
lib.rs | swim-rs |
version | 0.1.1 |
source | src |
created_at | 2024-10-07 09:51:19.502439 |
updated_at | 2024-10-07 09:58:32.10452 |
description | Rust implementation of the SWIM protocol |
homepage | https://github.com/marvinlanhenke/swim-rs |
repository | https://github.com/marvinlanhenke/swim-rs |
max_upload_size | |
id | 1399813 |
size | 174,706 |
swim-rs
is an implementation of the SWIM protocol in Rust, designed for efficient and scalable cluster membership and failure detection in distributed systems.
This library is based on the official SWIM protocol paper and includes optimizations such as:
swim-rs
implements the SWIM protocol to manage cluster membership and detect node failures in a distributed system efficiently. At its core, each node periodically selects a random peer to send a PING message, verifying its health and availability. If the selected peer fails to respond within a specified timeframe, the node escalates the check by sending a PING_REQ to multiple other members of the cluster. This two-tiered approach minimizes false positives in failure detection by confirming suspicions through multiple independent confirmations.
To optimize network usage, swim-rs employs piggybacking, where membership updates and state changes are attached to regular messages (UDP) rather than sending separate multicast messages. Additionally, round-robin member selection ensures that all nodes are probed uniformly, preventing any single node from becoming a bottleneck in the failure detection process. The protocol also incorporates a suspicion mechanism, introducing an intermediate state between alive and dead, which helps in reducing the chances of incorrectly marking healthy nodes as failed.
Furthermore, swim-rs utilizes a gossip-based dissemination strategy to propagate membership information and state transitions across the cluster. This ensures that all nodes maintain a consistent and up-to-date view of the cluster's state, enhancing scalability and resilience.
Add swim-rs
to your Cargo.toml
:
[dependencies]
swim-rs = "0.1.0"
The following example demonstrates how to create two nodes in the same cluster and run the SWIM protocol:
use std::time::Duration;
use swim_rs::{
api::{config::SwimConfig, swim::SwimCluster},
Result,
};
#[tokio::main]
async fn main() -> Result<()> {
// Creates two nodes in the same cluster
let node1 = SwimCluster::try_new("127.0.0.1:8080", SwimConfig::new()).await?;
let node2 = SwimCluster::try_new(
"127.0.0.1:8081",
SwimConfig::builder()
.with_known_peers(["127.0.0.1:8080"])
.build(),
)
.await?;
// Run the SWIM protocol in the background
node1.run().await;
node2.run().await;
// Simulate a long-running process or service
tokio::time::sleep(Duration::from_secs(12)).await;
Ok(())
}
The following example demonstrates how to subscribe to events emitted by nodes in the SWIM cluster.
use std::time::Duration;
use swim_rs::{
api::{config::SwimConfig, swim::SwimCluster},
Event, Result,
};
#[tokio::main]
async fn main() -> Result<()> {
// Creates two nodes in the same cluster
let node1 = SwimCluster::try_new("127.0.0.1:8080", SwimConfig::new()).await?;
let node2 = SwimCluster::try_new(
"127.0.0.1:8081",
SwimConfig::builder()
.with_known_peers(["127.0.0.1:8080"])
.build(),
)
.await?;
// Run the SWIM protocol in the background
node1.run().await;
node2.run().await;
// Subscribe and receive events from node1
let mut rx1 = node1.subscribe();
// Handle events accordingly
while let Ok(event) = rx1.recv().await {
match event {
Event::NodeJoined(e) => tracing::info!("[{}] handle {:#?}", node1.addr(), e),
Event::NodeSuspected(e) => tracing::info!("[{}] handle {:#?}", node1.addr(), e),
Event::NodeRecovered(e) => tracing::info!("[{}] handle {:#?}", node1.addr(), e),
Event::NodeDeceased(e) => tracing::info!("[{}] handle {:#?}", node1.addr(), e),
}
}
// Simulate a long-running process or service
tokio::time::sleep(Duration::from_secs(12)).await;
Ok(())
}
The following features are planned for future releases to enhance the functionality, security, and robustness of swim-rs:
Contributions are welcome!
Please open issues and submit pull requests for any enhancements or bug fixes.
This project ist licensed under the Apache License, Version 2.0