Replication and clustering

The fence

The epoch and lease a managed node keeps so that only the legitimate primary accepts writes, the role commands a control account can give, and the state a node reports.

A node must refuse writes when it is not the legitimate primary, and only the node itself can do that in bounded time when it cannot reach anybody. If a controller alone said "you are primary", a partition between the controller and a node would leave two primaries, because the old one still believes it. So the database keeps a fence: a few hundred lines and no election logic (D51, step C1). Everything else about electing a primary is the watcher's job.

Managed nodes#

control = true
user = ctl:PASSWORD:control

control = true makes the server a node a watcher manages: read-only at start, no election inside it. It needs an account with the control role, which can use only the control interface and do nothing with data. control cannot be combined with replica-of. A node with groups above 1 needs control too.

The fence rules#

RuleDetail
EpochA number that only grows, persisted in control.state in the data directory. A role command with a lower epoch is refused. Becoming primary needs a higher epoch than the node has seen.
LeaseA node is writable only while it holds the role primary at the current epoch and the lease it was given has not run out on its own monotonic clock. The watcher renews the lease (same epoch, same role) at a third of its length or faster; a node that is not renewed stops writing by itself.
Dirty flag and data epochA node that was primary may hold an unconfirmed tail of log; it is marked dirty (on disk before the first write is accepted) and reports rank 0 until it follows a newer primary that accepted its copy. The data epoch is the epoch of the primary whose history the node's log continues; positions compare only within one data epoch, as Raft compares the last log term first.
Strict syncThe node uses strict synchronous replication: an unconfirmed write is an error.

The control interface#

All of it needs a login as a control account (POST /v1/auth/login).

CallMeaning
POST /v1/control/roleBody: epoch, role, and for primary a leaseMs of 100 to 120000, for follower a leader (the leader's client host:port), or idle. With table groups also "group": g.
GET /v1/control/stateEpoch, role, leader, writable, leaseRemainingMs, dirty, rank (0 while dirty, else the data position), offset, inSync. With groups, a groups array. busy is reported while a command is in progress. Also the load for resource control: foreground latency histogram, stalls, level-0 files, compaction bytes.
POST /v1/control/throttleSet limits on compaction and the replication sender (0 means none). Used by the watcher's resource control.

A follower uses replica-user or replica-token as its credential for reading the leader.

Why a lease and not only an epoch#

A watcher that is cut off from the quorum cannot be told "you are no longer primary". The lease turns that into a bounded wait: the node stops writing by itself, at most one lease after its last renewal. The new primary is assigned only after the old lease is certain to have expired, which is why a failover takes seconds, not milliseconds (Failover with a watcher quorum). Clock drift between nodes only has to be small against the lease; clocks need no synchronising, but they must be monotonic and run at about the same speed.

Tests#

The control tests and the fence's unit tests check: a stale epoch is refused, writes stop when the lease expires, a role change takes effect at once, state reports are exact, two nodes with two groups, restart, and fewer groups than stored are refused.

See also#