Replication and clustering

Table groups and partitioned tables

How tables are assigned to groups with one primary each, how requests are forwarded, how partitioned tables spread over groups, and the exact limits of this design.

This is as far as "master-master" goes today (decision D50, layers 1 and 2): every node leads some tables, and a request may enter at any node.

Table groups (layer 1)#

groups = 3                    # on every node (raftdb.conf), needs control = true
cluster_groups = 3            # on every watcher (watch.conf); the same number

Each node keeps one fence per group (control.state, version 3). The group of a table is a fixed function of its name, group_of(db, table, N); the catalog (creating and dropping databases and tables) and everything created by DDL belong to group 0, which is also the catalog authority. Each group has its own primary, epoch, lease, dirty flag, data epoch and strict-sync majority, and its own replication manager on each follower, which follows the group's leader for the group's tables only.

The watcher quorum keeps one primary per group. With equal data (a new cluster, an empty group) a group goes to its preferred node: groups are dealt out over the nodes in the order of their ids, so three nodes and three groups give one primary each. Data always wins over the preference. A group waits for its preferred node to report, up to the time a lease takes to run out. GET /v1/control/state lists a node's groups; the watcher's status JSON has a groups array of {group, epoch, primary}.

Forwarding#

A node serves its own groups' tables locally and forwards every request for a table whose primary is another node:

  • Binary protocol: the request frames are passed on unchanged (only the request id is renumbered), so the leader encodes documents with its own field registry. The entry node logs in upstream with the client's own credential and never writes. Replies come back with the original id. Forwarding is pipelined.
  • HTTP and UDP: they do not forward. HTTP answers 409 not_leader with the primary's address; UDP refuses with the address.
  • Reads are never served from a follower in this mode: every request for a remotely led table is forwarded, so reads are as fresh as the primary and a connection reads its own writes.
  • DDL runs on the group-0 leader only. A table cannot be moved to another group.
  • The in-process client that raftdb-mysql uses with embed (Server::local) is not available with table groups, because a request may have to go to another node. Whether the SQL front-end in two-process mode works against a node in group mode has not been tested; do not assume it.

What was tested#

Test with three server processes and three watcher processes, three groups: every group has exactly one primary on three different nodes; a table of group 1 is created on node 0 and appears everywhere; 30 writes through node 0 (which does not lead group 1) are forwarded and acknowledged and read back through node 2; node 1's server dies and group 1 gets a new primary while groups 0 and 2 keep theirs; no acknowledged row is lost. Real processes on a 2-core Linux host: group 1 had a new primary 8.3 s after a kill -9 (lease 3 s), its writer saw 161 errors and an 8.2 s pause, while the writers of the other two groups saw no error and pauses of at most 0.09 s; 0 acknowledged rows were lost.

Throughput: what the numbers do and do not show#

The goal of layer 1 said "aggregate write rate with the leaders spread is higher than with one leader". On one machine with six writers the spread layout was 2% lower (2 cores), 9% higher (4 cores, fenced) and 16% higher (12 cores), and equal with twelve writers. That is not the scaling the layer is for and must not be reported as met: every process shared the machine's cores and one disk. A test that can show N nodes giving about N times the writes needs three nodes on three machines; none was available. The claim for layer 1 is correct routing, a failover that costs only the groups the dead node led, and no loss of acknowledged writes; the throughput claim is not demonstrated.

Partitioned tables (layer 2)#

A partitioned table is a logical table made of P physical tables T~p0 to T~p(P-1) (P from 1 to 256), spread over the groups: partition k goes to group (hash(db, T) + k) mod groups. Rows go to a partition by partition_of(key, P). Create one over HTTP with partitions in the create body.

OperationState
Create, list (one collapsed entry), drop (all or none)Built
Put, get, delete by key (routed to the partition's group)Built
Queries, counts, indexes, statistics over all partitionsBuilt: the filter is compiled per table and scans several tables as one
Index and checkpointDone for the partitions this node leads; the others are listed with their leaders
Import and renameBuilt with special handling (a rename moves partitions between groups, so it needs a node that leads the groups involved; otherwise 409 not_leader)
Use db T over the binary protocolNOT_SUPPORTED
Online split and merge; true partitioning (a node holds only some partitions); cross-partition transactionsNot built

The layer's test passed on test server A (L2d): see D50 layer 2.

Limits to remember#

  • One decree at a time across all groups: a node that led several groups and died gets them revoked and reassigned one after another.
  • The number of groups of a running cluster cannot be changed (the nodes refuse fewer than stored; more can be added and the watchers restarted together).
  • cluster_groups must match the nodes' groups; a node that reports fewer is never assigned the missing groups.
  • Failover time is the same as for one group.

See also#