Remove a member
Take a node out of a cluster: let it leave on its own, or down it from another member and let it exit.
There are two ways out of a cluster. A node which can still say goodbye leaves: it announces its departure, and the others list it as left without detecting a failure. A node which cannot, or should not, is downed: the downing provider or cluster::down decides that it is gone. Either way the node's incarnation is out for good, and only a restarted process joins again.
The examples on this page come from a cluster of three nodes. The second node leaves, and the first node downs the third. The program plays all three nodes, as in the clustered actors tutorial.
Leave and drain outgoing messages
async fn leaving_node() -> anyhow::Result<()> {
let (addr, seeds) = node_args()?;
join_cluster(addr, seeds).await?;
await_settled().await?;
await_stdin_closed().await?;
let departure = cluster::leave().await.context("leaving the cluster")?;
anyhow::ensure!(
departure.is_acknowledged(),
"departure not acknowledged by {:?}, timed out for {:?}",
departure.unacknowledged(),
departure.timed_out()
);
Ok(())
}The second node first waits until the receptionist has settled, so every peer knows it before it leaves. Then it waits until its stdin closes, which is how the first node tells it to leave in this example. cluster::leave announces the departure to every member and tries to drain what the node still has queued for them, within EndpointConfig::leave_timeout, three seconds by default. It answers a Departure, which lists the members that acknowledged the drain and those that did not. The example treats a missing acknowledgment as an error. A node which leaves together with others can check Departure::is_complete instead, which accepts the members that left themselves.
leave stops no actor, so whatever still runs on the node is cut off from the cluster afterward. A node with actors leaves with cluster::leave_on_terminated instead, which waits for its actor system to terminate first, see Leaving gracefully.
Down a member from another node
async fn first_node() -> anyhow::Result<()> {
let addrs = free_addrs(NODES)?;
let mut leaving_node = spawn_node(LEAVING_NODE_ARG, addrs[1], &addrs)?;
let mut downed_node = spawn_node(DOWNED_NODE_ARG, addrs[2], &addrs)?;
join_cluster(addrs[0], addrs.clone()).await?;
await_all_up().await?;
drop(leaving_node.stdin.take());
await_member_state(addrs[1], MemberState::Left).await?;
println!("The second node has left");
await_exit(&mut leaving_node, "second").await?;
cluster::down(addrs[2]).context("downing the third node")?;
await_member_state(addrs[2], MemberState::Down).await?;
println!("The third node is down");
await_exit(&mut downed_node, "third").await?;
let up = cluster::members()
.context("reading the members")?
.iter()
.filter(|member| member.state() == MemberState::Up)
.count();
println!("Up members: {up}");
Ok(())
}The first node waits until all three nodes are up. It closes the stdin of the second node and waits until it lists that node as left. Then it calls cluster::down with the address of the third node. That runs the node death sequence on the first node: pending asks toward the third node fail, its watchers receive synthesized terminated signals, and its incarnation is refused from then on. The other members run the same sequence once gossip has told them of the downing. down fails if no up member advertises the address, and for this node's own address, which leave is for.
The first node waits for left and for down the same way, through the cluster state:
async fn await_member_state(addr: SocketAddr, member_state: MemberState) -> anyhow::Result<()> {
let mut states = cluster::cluster_state().context("subscribing to the cluster state")?;
let reached = states.wait_for(|state| {
state
.members()
.iter()
.any(|member| member.addr() == addr && member.state() == member_state)
});
timeout(REMOVE_TIMEOUT, reached)
.await
.with_context(|| format!("{addr} not {member_state} within the timeout"))?;
Ok(())
}Exit when downed
async fn downed_node() -> anyhow::Result<()> {
let (addr, seeds) = node_args()?;
join_cluster(addr, seeds).await?;
let downed = async {
while cluster::lifecycle().context("reading the lifecycle")? != Lifecycle::Downed {
sleep(LIFECYCLE_POLL_INTERVAL).await;
}
anyhow::Ok(())
};
timeout(REMOVE_TIMEOUT + EXIT_TIMEOUT, downed)
.await
.context("not downed within the timeout")?
}The third node waits until cluster::lifecycle reports Lifecycle::Downed and exits. A node that has left also reports Downed.
What to keep in mind
Leave when you can. A departure needs no failure detection and no downing, so the others list the node as left as soon as they receive it. With leave_on_terminated, the terminated signals of its actors normally arrive as real terminations. A node which is stopped on purpose should therefore leave, see Shut down gracefully.
Down a member only when it is gone or must go. A member which crashes or is cut off from the others needs no call while the others are a strict majority. The downing provider then downs it on its own, see Membership and downing. Once the member is listed as down, cluster::down fails for its address, as long as no restarted process has joined there. A downed member may still be running, for example on the other side of a partition. Watchers receive synthesized terminated signals for its actors, which promise only that no message from those actors is delivered on the watcher's node again, see the death watch ordering guarantee. cluster::down is also how an operator ends a broken link, by downing one of its ends, see Observe a cluster.
Run it
#[tokio::main]
async fn main() -> anyhow::Result<()> {
init_tracing();
match env::args().nth(1).as_deref() {
Some(LEAVING_NODE_ARG) => leaving_node().await,
Some(DOWNED_NODE_ARG) => downed_node().await,
_ => first_node().await,
}
}From a clone of the tellus repository:
cargo run --quiet -p tellus --features cluster-dev --example how_to_remove_a_memberThe second node has left
The third node is down
Up members: 1The second node is listed as left, the third as down, and both processes exit successfully. The first node remains as the only member which is up. On stderr, the third node logs at error level that it has been downed and has to be restarted to rejoin. In a terminal that line appears before the last line of the output, as JSON because the example configures the tracing subscriber that way.