Mirexa

Wiki Cluster

The control plane

The Raft-replicated state every Mirexa node shares — what it holds, how it changes, the cluster settings, the leader-local loops and heartbeats.

The control plane is the small, strongly consistent part of a Mirexa cluster: the facts every node must agree on to route a request — which nodes exist, which node serves each partition, and the handful of settings whose disagreement would produce wrong answers nobody could detect. It is replicated with Raft, one consensus group for the whole cluster. It holds no candles, no ticks and no stream catalogue; a change to it is a decision (a registration, a placement, a fence), and decisions are rare enough to afford a quorum round each.

Raft, as used here

Mirexa embeds hashicorp/raft v1.8.0. It is the only consensus library in the build and only internal/raft imports it.[1]

  • Storage. The Raft log and stable store live in BoltDB at <storage.data_dir>/raft/raft-log.bolt, with file snapshots beside it. There is deliberately no separate data-directory knob, so the log cannot be separated from the data it describes.
  • Membership. cluster.initial_members non-empty means bootstrap a new group from that list; empty means join an existing one through cluster.seeds. It is consulted only when the node has no Raft state on disk — a restarted founder reads its configuration from its own log. Passing a member list to a node meant to join forms a second, separate cluster.
  • Voters and non-voters. Every joining node is admitted as a non-voter; the leader promotes caught-up non-voters toward cluster.voters (3). See Joining, draining and decommissioning nodes.
  • A refusal is a value. When the cluster agrees on an entry and the state machine declines it (registering a decommissioned node, failing over a primary that came back), that is reported as a refusal, distinct from "the proposal did not happen".
  • Ambiguous proposals. A proposal that times out or is cancelled may still commit later. Only errors the library raises before the entry reaches the log are treated as safe; every other error is raft.ErrAmbiguous, to be reconciled by reading the state back, never by a blind retry.[2]
  • A startup failure is fatal. A node configured to cluster that cannot start Raft does not fall back to running standalone.

Timing knobs

Key Default Meaning
cluster.heartbeat_timeout 1s How long a follower waits without hearing a leader before standing for election (follower patience, not a send rate)
cluster.election_timeout 1s How long a candidate waits before another election round
cluster.leader_lease_timeout 500ms How long a leader may act without reaching a quorum; must not exceed heartbeat_timeout (checked when the node starts)
cluster.commit_timeout 50ms Idle heartbeat cadence bounding a follower's commit-index staleness
cluster.snapshot_threshold 8192 Applied entries since the last snapshot that trigger a new one
cluster.snapshot_interval 2m How often the threshold is checked
cluster.trailing_logs 10240 Entries kept behind a snapshot for a lagging follower
cluster.snapshot_retention 2 Snapshots kept on disk
cluster.max_append_entries 64 Entries per AppendEntries RPC

These are hashicorp/raft's own defaults, carried over unchanged. They govern the consensus group only; whether a data node is alive is governed separately by cluster.liveness_timeout (below).

What is replicated

All replicated state is one cluster.State, mutated only by the state machine applying a command. The commands are:[3]

Op Command Effect
1 REGISTER_NODE Admit or re-admit a node; always assigns a fresh incarnation
2 DRAIN_NODE Record an operator's intent that a node leave
3 FENCE_NODE Stop believing a node, conditional on its incarnation
4 DEREGISTER_NODE Remove a node record (refused while the directory names it)
5 ASSIGN_PARTITION Set a partition's primary and copies; moves the epoch if the primary changes
6 SET_IN_SYNC Replace a partition's in-sync set (operator use)
7 SET_CLUSTER_SETTINGS Compare-and-set the cluster settings
8 PLACE_PARTITIONS Place many unassigned partitions in one all-or-nothing entry
9 / 10 ADD_SECONDARY / REMOVE_SECONDARY Add a copy (enters SYNCING); remove a copy from membership and the in-sync set together
11 / 12 / 13 START_MIGRATION / ABORT_MIGRATION / COMPLETE_MIGRATION Record, stop or close a primary move
14 START_REPLICA_MOVE Record a move of one secondary copy
15 DECOMMISSION_NODE Mark a fully drained node gone
16 / 17 JOIN_IN_SYNC / LEAVE_IN_SYNC A copy adds or removes itself from the in-sync set
18 FAIL_OVER Hand a fenced primary's partitions to in-sync copies
19 READMIT_DIVERGENT Return a quarantined divergent copy as a syncing secondary
20 REVOKE_KEY Stop trusting a token-issuer key cluster-wide
21 START_SWAP Record a swap of primary and in-sync copy

Every command carries exactly the fields its family uses; a command with an unexpected field is refused rather than partly applied, so an older build never silently drops an instruction a newer build meant. A test reads internal/cluster's source and fails if any error sentinel there is neither classified as a refusal nor listed as a non-refusal.

Snapshot format

The state is snapshotted in a canonical binary encoding (magic MXCP, format version 12). Records must be in ascending id order; a snapshot that is not canonical is refused, so two replicas holding the same state always produce the same bytes. Older versions are refused, not upgraded: an older header read with absent fields as zeros would silently restore a cluster that had forgotten its partition count, its in-flight migrations, its divergent copies or its revoked keys.[4] In practice this means a cluster's raft/ directory does not survive an upgrade across a snapshot format change; see Operating a cluster.

Cluster settings

Four values must be the same on every node, because a disagreement would produce a wrong answer that neither node could detect. They are replicated as cluster.Settings:[5]

Setting Seeded from Range Changeable Why it must agree
Partition count partition.count (4096) power of two, ≤ 2^20 No Routing is hash mod count; two counts send one stream to two partitions
Compression codec compression.algorithm non-empty Yes, new chunks only Copies are compared by bytes; different codecs make identical data look divergent
Liveness timeout cluster.liveness_timeout (30s) 100ms – 1h No A primary's lease is sized from the timeout the granting leader announced, and the next leader fences on its own
Replication factor cluster.replication_factor (1) 1 – 7 No Two leaders with different factors would add copies the other never would

How a node arrives at its settings at startup:

  1. With consensus off, the YAML is used as is.
  2. Before seeding, the node checks its own values against the rules above and refuses to start naming the rule a bad value breaks.
  3. If the cluster has already agreed on settings, they win. A differing YAML value is logged as a warning and ignored.
  4. If nothing is agreed yet, the node proposes its YAML values at version 0. Several founders racing is safe: exactly one proposal commits, the rest are refused and adopt what won.
  5. If nothing arrives within cluster.settings_timeout (15s), the node starts on its own YAML with a loud warning — unless cluster.strict_settings is on, in which case it refuses to start.

This all runs before the storage layout and the dispatcher are built, because the partition count and codec are baked into them. A running node does not adopt a changed codec until it restarts; there is no hot reload of cluster settings.

Updates are compare-and-set. UpdateClusterConfig names the version it replaces and must carry every field (the immutable ones unchanged). Two operators updating at once cannot both be told they succeeded: the loser gets a version conflict and reads the state back.

Heartbeats are not log entries

Every registered node sends the leader a heartbeat every third of the liveness timeout (10s at the default 30s), over the control API. A heartbeat is evidence, kept in the leader's memory; only the transition it justifies — a fence — becomes a log entry. Putting one entry per node per interval through Raft would be the very firehose the data plane is kept out of consensus to avoid.[6]

  • The leader confirms it still leads with a quorum round that appends nothing (VerifyLeader), then checks the heartbeat's incarnation against its local state.
  • A heartbeat from a stale incarnation, or from a node the registry has fenced, is answered accepted=false (not an error), and the node re-registers. A transport failure is not a reason to re-register.
  • An accepted heartbeat grants the node its primary lease — see Failure detection, leases and failover.
  • The heartbeat carries the node's load report (bytes and records per partition, and a live-write counter) for the rebalancer, and its own Raft commit index as evidence for voter promotion. A malformed load report is refused and logged; the heartbeat still counts.
  • A new leader has no heartbeat history, so it starts every node's clock at its own election. Treating "never heard from" as "expired" would fence the whole cluster on every election.

Leader-local loops

Eight loops run on every node and do nothing unless the node is the current leader. Running them everywhere avoids a second mechanism for starting and stopping them on leadership changes. Each loop reads local applied state, decides, and proposes ordinary commands; anything it remembers between runs (fence clocks, cooldowns, load reports) is in memory and discarded with leadership.[7]

Loop Cadence Decides Page
Fence ⅓ of the liveness timeout Nodes silent past the timeout become OFFLINE / OFFLINE_LEAVING Failover
Failover ⅓ of the liveness timeout A fenced primary's partitions go to in-sync copies, one timeout after the fence is first seen Failover
Placement 5s Every unassigned partition gets a primary Routing
Migration cluster.migration_interval (1s) Advances each migration intent one step, paced by max_concurrent_migrations Migrations
Rebalance cluster.rebalance_interval (1m) Computes the balance plan; executes it only with rebalance_auto Migrations
Promotion 1s Promotes caught-up non-voters; removes decommissioned voters Membership
Drain 1s Moves a draining node's partitions off it; decommissions it when empty Membership
Replication 2s Adds copies to partitions below the replication factor Replication

Reads of the control-plane state

  • Linearizable reads (Lookup) append a Raft barrier entry and then read local state. They cost a log entry each and are used where staleness would mislead an operator — ListPartitions, ListPartitionHealth, migration progress.
  • Local reads (LocalLookup) read the node's own applied state with no round trip. Routing uses them on every request, and GetClusterStatus uses them so that it still answers when the cluster has no quorum — the moment an operator most needs it.
  • Position. The state records the log index of the last command it applied (barriers and no-ops never reach it). A lease grant names a position, and a node acts on it only once its own state has applied that far.

Seeds and contact points

cluster.seeds lists the control addresses of other members. They are contact points, not leader addresses: any member answers a write it cannot take with a NotLeader detail naming the leader's control address, and the caller follows it. A follower redirects; it never forwards.

Seeds are needed only for the first contact. Once a node is a member, registration, heartbeats and proposals try the members its own applied registry knows — the leader first, then every serving member — before falling back to the seeds, so a node whose seeds have all left (after a rolling restart, say) still reaches the cluster. A founding node with no seeds uses its own control address as its seed and registers through the same call every joiner uses.[8]

The control API and its security

The control plane is served by the gRPC control listener (control.enabled, control.addr, default 127.0.0.1:9090). The ClusterService is registered only when clustering is on. Its RPCs are listed in the Control-plane gRPC API reference; operational use is on Operating a cluster.

  • Loopback unless authenticated. The control listener refuses to start on a non-loopback address unless control.admin_auth is on, clustered or not. A hostname is refused rather than resolved, because resolution can change under a running node. Exposure and authentication are one decision.[9]
  • Admin tokens. With control.admin_auth: true, every call on the listener — every service, not only ClusterService — needs an Ed25519-signed token carrying the ADMIN_CLUSTER permission, sent as authorization: Bearer <token> metadata and verified offline against security.auth.trusted_keys, by the data plane's own admission rule: signature first (the other fields are attacker-supplied until it verifies), then the revoked keys, expiry, the one-hour validity window and security.max_permissions. An invalid token is UNAUTHENTICATED; a valid token without the permission is PERMISSION_DENIED. Turning admin auth on with no trusted keys is a startup failure.[10]
  • A node's own credential. A node with admin auth on heartbeats with the token in cluster.admin_token_file, re-read before every request, so rotating the file reaches a running node. Tokens live at most an hour, so something outside the process must rotate it.
  • Revoked issuer keys are replicated state (REVOKE_KEY). Each node hands its applied revoked set to the data plane every 250 ms; from then on that key's handshakes and renewals are refused, its sessions are refused at their next frame and its subscriptions are cut. See Authentication and tokens.
  • The Raft transport and the replication listener are separate ports. Raft RPCs are not exposed on the control listener; its service set is pinned by a test.

References

Source

Tests

Further reading

External

  • Diego Ongaro and John Ousterhout, "In Search of an Understandable Consensus Algorithm (Extended Version)" — https://raft.github.io/raft.pdf
  • HashiCorp, hashicorp/raft — https://github.com/hashicorp/raft
  • HashiCorp Consul, "Autopilot" (leader-driven voter promotion on the same library) — https://developer.hashicorp.com/consul/docs/agent/config/config-files#autopilot
  • KIP-631, "The Quorum-based Kafka Controller" (broker heartbeats to a controller, only state changes in the metadata log) — https://cwiki.apache.org/confluence/display/KAFKA/KIP-631%3A+The+Quorum-based+Kafka+Controller

  1. internal/raft package; TestClientPackage_DependencyFootprint (client/client_test.go) forbids the public SDK from importing internal/cluster, internal/raft or internal/control. ↩︎

  2. ErrAmbiguous and its classification in internal/raft/propose.go. ↩︎

  3. Op constants in internal/cluster/command.go. ↩︎

  4. snapshotVersion and snapshotMagic in internal/cluster/codec.go. ↩︎

  5. internal/cluster/settings.go; resolveClusterSettings in cmd/server/settings.go. ↩︎

  6. ClusterService.Heartbeat in internal/control/cluster_service.go; the interval (timeout / 3) in internal/control/join.go. ↩︎

  7. startClusterReconcilers in cmd/server/resolver.go; default intervals in internal/control/{fence,placement,promotion,drain,replication,migration}.go. ↩︎

  8. clusterContacts in cmd/server/join.go. ↩︎

  9. requireLoopback in internal/control/server.go. ↩︎

  10. internal/control/admin_auth.go; ControlConfig.AdminAuth in internal/config/config.go. ↩︎