Mirexa

Wiki Cluster

Failure detection, leases and failover

How Mirexa notices a dead node, why a cut-off primary stops serving before it can be replaced, and how its partitions move to in-sync copies automatically.

When a node stops answering, three mechanisms work in sequence. The failure detector notices its silence and fences it: the cluster stops believing it. The primary lease guarantees the node itself has stopped serving as a primary before that happens, even if it is merely cut off rather than dead. Then failover hands each of its partitions to an in-sync copy. All three are automatic and are timed off one cluster-wide value, the liveness timeout (T, cluster.liveness_timeout, default 30 s, agreed once and immutable).

Timing Value At the default (T = 30 s)
Heartbeat interval T / 3 10 s
Fence sweep interval T / 3 10 s
A silent node is fenced after between T − one heartbeat interval and T + one sweep 20 – 40 s
Primary lease ¾ T from the heartbeat's send time 22.5 s
Failover delay after the fence is first seen T 30 s
In-sync staleness bound 2T + 2 s 62 s

A failover therefore takes about two liveness timeouts end to end — one to fence, one to wait out any lease — by construction. At the default that is about a minute; it is a trade made so that a partition is never failed over while its old primary could still be serving it.

Fencing

Every node heartbeats the control-plane leader every T/3. The leader keeps the last time it heard from each node in memory — a heartbeat is evidence, not a decision, and never becomes a log entry. A leader-local sweep, every T/3, proposes a fence for any node silent longer than T. A fence moves ONLINE to OFFLINE and LEAVING to OFFLINE_LEAVING (a drain in progress survives the fence).[1]

  • Incarnations. Each registration is stamped with the log index of the entry that created it. A fence names the incarnation the detector saw and is refused if the node has re-registered since, so a node that restarted inside the window between "silence observed" and "fence applied" is not marked dead the moment after it proved it was alive.
  • A new leader starts every clock at its election, so an election never fences the whole cluster for silence the new leader simply did not observe.
  • A fence does nothing to the node and reassigns nothing. It stops the cluster routing to the node: other nodes now refuse requests for its partitions with PartitionUnavailable (0x0009) instead of redirecting clients to an address the cluster has stopped believing.
  • Coming back. A fenced node's heartbeats are answered accepted=false; it re-registers with a new incarnation and is ONLINE again, keeping any partition that has not yet been failed over.

The primary lease

Fencing alone cannot stop a primary that is alive but cut off from the control plane: it never hears about the fence, and writes carrying the old epoch still match its own. A Mirexa primary therefore serves only while it holds a lease:[2]

  • Each acknowledged heartbeat grants a lease of ¾ T, counted from the moment the heartbeat was sent. The leader measures silence from when it received the heartbeat — later — and fences no earlier than T after that, so the lease ends at least ¼ T before the earliest possible fence. That margin absorbs clock-rate drift between machines and a write admitted just before expiry finishing just after it.
  • A grant takes effect only once the node's own control-plane state has applied the position the acknowledging leader named, so a node restarted on an old log cannot serve on a stale directory.
  • One missed heartbeat does not lapse a lease (they go every T/3).
  • Without a lease in effect, the node refuses every partition it is primary of — reads and writes — with PartitionUnavailable, logs a warning once when the lease lapses and once when it returns, and exports mirexa_cluster_primary_lease_remaining_seconds.
  • A copy's stale reads are lease-gated too: a node the control plane cannot vouch for answers nothing from its copies.

There is no autonomous mode. If a node loses the control plane — or the whole control plane loses quorum — its primaries stop serving within their lease. This is deliberate: a node that cannot be acknowledged cannot rule out having been replaced. The data path itself does not run through consensus, but serving as a primary requires the control plane's acknowledgement.

Measured 2026-10-08 with T = 3 s, three runs, in-process on one development laptop (TestLease_APrimaryCutOffFromTheControlPlaneStopsBeforeItCanBeFenced): a primary cut off from the control plane refused requests 1.71–1.77 s after the cut and was fenced 3.45–3.49 s after it. The shipped binary is checked too: with the whole control plane killed, a live primary refuses within its lease (TestServerBinary_APrimaryStopsServingWhenItsControlPlaneIsGone).

Not claimed: a process paused (by a GC, a VM stall, a stopped container) for longer than the ¼ T margin after checking its lease could still write after the lease ended — the caveat of every lease. And a lease prevents split brain; it does not prevent the loss of writes acknowledged after a copy's last pull.

Automatic failover

A leader-local loop, sweeping every T/3, plans and proposes failovers. It is on by default and has no off switch: it moves no data (the copy already holds it) and acts only on partitions nobody is serving.[3]

  1. When. A fenced node's partitions move only T after this leader first saw the fence at that incarnation. The last lease the node could hold came from a heartbeat sent before that sighting, so it has ended by then with ¼ T to spare. The clock is leader-local; a new leader starts it again, which only ever waits longer.
  2. Where. Each partition goes to a failover candidate: an in-sync copy on a node the cluster believes. Among candidates: ONLINE before LEAVING; then the node primary of the fewest partitions, counting this plan's own moves, so a dead node's partitions spread over the survivors; then the lowest id.
  3. Stranded partitions. A partition with no candidate is not moved — promoting a copy that was not caught up would discard everything it missed. It is logged once and stays unavailable until its node returns.
  4. The entry. One FAIL_OVER entry covers all the node's movable partitions, all or nothing. It names the incarnation it was planned at and is refused if the node is no longer fenced at that incarnation — a node that came back meanwhile either kept its partitions or lost them, never both. Each partition's new epoch is the entry's log index.
  5. Every other copy becomes divergent. After a failover, each surviving copy pulled from the dead primary independently and may hold artifacts the new primary never received. The old primary and every other copy are recorded as divergent: not replicas, never read, never handed the partition again until their data is set aside. The partition reads UNDER_REPLICATED until they return.
  6. Promotion. The new primary replays the replica log it kept as a copy — the writes the old primary acknowledged that never reached a chunk — and refuses the partition with a retryable Overloaded (250 ms hint) until it has. Only then does it serve.

Measured 2026-10-08 in the shipped binary with T = 3 s, three runs, one development laptop (TestServerBinary_ADeadPrimaryIsFailedOverToItsCopy): fenced 2.9 s after a SIGKILL, failed over 3.97–4.97 s after the fence, every acknowledged candle the copy held served, and the old primary — restarted on its old data directory — back ONLINE and redirecting the partition to its successor.

What a failover can lose

Writes the dead primary acknowledged after its copy's last pull. A copy is a candidate only while in sync (level within 2T + 2 s), which bounds the loss; in practice it measured at milliseconds' worth of writes. See Replication for the numbers.

When the old node returns

A node that comes back after its partitions failed over holds data from an abandoned history. Its replica supervisor — acting only once the node holds a lease, i.e. once its view of the directory has caught up — handles it in two steps:[4]

  1. Quarantine. For each partition the directory lists it as divergent for, the node stops the partition's streams, records a floor so WAL recovery never replays the abandoned records, and moves the whole partition directory to quarantine/partition_<id>-epoch_<e>/ in one durable rename. Nothing is deleted; a quarantine is evidence.
  2. Re-admission. Only after the quarantine has landed does the node propose READMIT_DIVERGENT about itself, becoming a SYNCING copy of the new primary, and the ordinary catch-up re-seeds it from nothing to IN_SYNC. A draining node does not ask back.

A node's files for a partition in which it no longer holds any role are likewise moved aside as partition_<id>-epoch_<e>-released. See Reconciliation.

Partition health

What a partition can still do is projected from the directory and the registry on every read, never stored:[5]

State Meaning Gauge
serving Primary and every named copy on nodes the cluster believes —
under_replicated Primary serving; at least one copy fenced or divergent mirexa_cluster_partitions_under_replicated
primary_down Primary fenced; an in-sync copy survives on a believed node — failover can restore it mirexa_cluster_partitions_primary_down
unavailable Primary fenced and no caught-up copy survives — back only with its node mirexa_cluster_partitions_unavailable

The gauges read NaN, not zero, when a node cannot read its control-plane state. ListPartitionHealth (a linearizable read, with an impaired_only filter) lists each impaired partition with its failover candidates.

References

Source

Tests

Further reading

External

  • Cary G. Gray and David R. Cheriton, "Leases: An Efficient Fault-Tolerant Mechanism for Distributed File Cache Consistency", SOSP 1989 — https://dl.acm.org/doi/10.1145/74850.74870
  • Martin Kleppmann, "How to do distributed locking" (fencing tokens and process pauses) — https://martin.kleppmann.com/2016/02/08/how-to-do-distributed-locking.html
  • KIP-631, "The Quorum-based Kafka Controller" (broker fencing by the controller) — https://cwiki.apache.org/confluence/display/KAFKA/KIP-631%3A+The+Quorum-based+Kafka+Controller

  1. internal/control/fence.go; State.Fence and NodeStatus in internal/cluster/state.go; heartbeat interval in internal/control/join.go. ↩︎

  2. internal/cluster/lease.go; partitionResolver.leaseInEffect and copyRead in cmd/server/resolver.go. ↩︎

  3. internal/control/failover.go; State.PlanFailover and State.FailOver in internal/cluster/failover.go. ↩︎

  4. The replica supervisor in cmd/server/replica.go; Layout.QuarantinePartition in internal/storage/layout.go. ↩︎

  5. internal/cluster/health.go; registerPartitionHealth in cmd/server/resolver.go. ↩︎