tellus
How-to guides

Form without the lowest seed

Let bootstrap pass over a lowest seed which never starts, at the price of possibly forming a second cluster.

cluster::bootstrap forms a new cluster only when a formation provider decides so. The default, Majority, lets only the node at the lowest address of the seeds form, and only once a strict majority of the seeds is known to be no cluster yet. While the lowest address does not answer, nothing forms. That is safe, but a discovery which keeps resolving a node that is gone, such as a fixed list of seeds, then never lets a cold start finish. Each waiting node logs not forming a cluster once at warn level and names the address of the node that is gone as the address which may form. MajorityWithFallback passes over a lowest address which has been unavailable for a while. Its price is that a lowest address which is only unreachable, not gone, can form a second cluster.

The examples on this page come from three seeds, of which the lowest never starts. The program plays the other two nodes, as in the clustered actors tutorial.

Choose the formation provider

tellus/examples/how_to/form_without_the_lowest_seed.rs
async fn join_cluster(addr: SocketAddr, seeds: Vec<SocketAddr>) -> anyhow::Result<()> {
    let transport = QuicTransport::dev(addr).context("dev QUIC transport")?;
    cluster::start_endpoint(EndpointConfig::new(addr), transport).context("remoting endpoint")?;
    let config = BootstrapConfig {
        join_attempt_timeout: JOIN_ATTEMPT_TIMEOUT,
        formation_provider: Arc::new(|| Box::new(MajorityWithFallback::new(FALLBACK_AFTER))),
        ..BootstrapConfig::new()
    };
    timeout(
        BOOTSTRAP_TIMEOUT,
        cluster::bootstrap(FixedSeeds::new(seeds)?, config),
    )
    .await
    .context("no cluster within the timeout")?
    .context("bootstrapping the cluster")
}

BootstrapConfig::formation_provider creates the provider. MajorityWithFallback::new(after) keeps the strict majority. The address which may form, though, is the lowest one of the seeds minus every address which has answered nothing but unavailable for at least after. The example sets after to two seconds. An address's timer starts with the first round of join attempts in which it is unavailable. Any other answer, or a round which did not attempt it, resets the timer.

join_attempt_timeout bounds how long a round waits for each attempt, five seconds by default. An attempt which outlasts it counts as unavailable. Nothing answers at the missing seed, so its attempts always time out, and the example lowers the timeout to one second to finish sooner.

With the serde feature, BootstrapConfig is deserializable, and the provider is chosen by name:

formation_provider:
  majority_with_fallback:
    after: 60s

after has no default.

Form and join

tellus/examples/how_to/form_without_the_lowest_seed.rs
async fn first_node() -> anyhow::Result<()> {
    let mut seeds = free_addrs(SEEDS)?;
    seeds.sort();
    let mut second_node = spawn_second_node(seeds[2], &seeds)?;
    join_cluster(seeds[1], seeds.clone()).await?;

    let mut states = cluster::cluster_state().context("subscribing to the cluster state")?;
    let left = states.wait_for(|state| {
        state
            .members()
            .iter()
            .any(|member| member.addr() == seeds[2] && member.state() == MemberState::Left)
    });
    timeout(BOOTSTRAP_TIMEOUT + LEAVE_TIMEOUT, left)
        .await
        .context("the second node did not leave within the timeout")?;
    let status = timeout(EXIT_TIMEOUT, second_node.wait())
        .await
        .context("the second node did not exit within the timeout")?
        .context("awaiting the second node's exit")?;
    anyhow::ensure!(status.success(), "the second node failed: {status}");

    let members = cluster::members().context("reading the members")?;
    for (index, seed) in seeds.iter().enumerate() {
        let state = members
            .iter()
            .find(|member| member.addr() == *seed)
            .map_or_else(
                || "never a member".to_string(),
                |member| member.state().to_string(),
            );
        println!("Seed {}: {state}", index + 1);
    }
    Ok(())
}

The first node runs at the second seed and starts the second node at the third. Nothing ever runs at the first seed. Once it has been unavailable for two seconds, the second seed is the lowest address left. Two of the three seeds are known to be no cluster, a strict majority, so the first node forms the cluster once its view has settled. In the example that is about five seconds after it started. The second node joins it:

tellus/examples/how_to/form_without_the_lowest_seed.rs
async fn second_node() -> anyhow::Result<()> {
    let addr = env::args()
        .nth(2)
        .context("node address argument")?
        .parse()
        .context("node address")?;
    let seeds = env::args()
        .skip(3)
        .map(|seed| seed.parse().context("seed address"))
        .collect::<anyhow::Result<_>>()?;
    join_cluster(addr, seeds).await?;

    let mut states = cluster::cluster_state().context("subscribing to the cluster state")?;
    timeout(
        BOOTSTRAP_TIMEOUT,
        states.wait_for(|state| state.up().count() == SEEDS - 1),
    )
    .await
    .context("not both nodes up within the timeout")?;
    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(())
}

It waits until both nodes are up and then leaves. The first node waits until it lists the second node as left and prints the state of every seed.

What to keep in mind

The price is uniqueness. A lowest seed which is running but unreachable from the others can form a cluster of its own beside theirs. An unreachable lowest seed which is a member of a cluster already is passed over just the same, since a seed which does not answer looks like one which is gone. The two clusters never merge. Each lists the other in ClusterState::foreign_clusters once the probe reaches the other cluster, and an operator has to resolve the split, see Membership and downing.

A discovery which drops nodes that are gone needs no fallback. Under Majority, the next lowest address inherits the role once discovery no longer resolves the lowest one, as long as the smaller set still holds min_nodes addresses. Only a discovery which keeps resolving nodes that are gone, such as a fixed list of seeds, needs MajorityWithFallback.

after sets how long an unavailable seed can block the fallback. The timer starts when a round of join attempts first reports the seed unavailable. A longer after waits longer for a lowest seed which is only slow to start. A shorter one passes over a lowest seed sooner, including one which is merely unreachable. Bootstrap can still join an existing cluster sooner, and forming a new cluster still needs a settled view and a strict majority.

The majority is taken over every seed. That includes the passed-over ones. With two seeds, one node alone is never a strict majority, so the fallback forms nothing there.

Run it

tellus/examples/how_to/form_without_the_lowest_seed.rs
#[tokio::main]
async fn main() -> anyhow::Result<()> {
    init_tracing();

    match env::args().nth(1).as_deref() {
        Some(SECOND_NODE_ARG) => second_node().await,
        _ => first_node().await,
    }
}

From a clone of the tellus repository:

cargo run --quiet -p tellus --features cluster-dev --example how_to_form_without_the_lowest_seed
Seed 1: never a member
Seed 2: up
Seed 3: left

The first seed never became a member. The second one formed the cluster without it and is still up, and the third one joined and left again. With the default Majority, the two nodes would wait for the first seed until the bootstrap timeout ended them.