Placement rules, copies and spreading data over servers
How to keep each table on some of the servers instead of all of them with node labels, placement rules (copies, spread, sync, read), how a table or a partition lands in a group, the safe order for turning it on, what the watcher does and never does, and what is not built.
Without placement every replica holds a full copy of the server. With placement a rule says, per table, how many copies exist, in which places, and how many must confirm a write, so a server holds only some of the data. The watcher quorum computes and keeps the placement map; the servers follow it. Nothing here changes a server that has no rules: it behaves as before.
Placement builds on table groups, the watcher quorum and synchronous replication. Read those first.
The pieces#
| Piece | What it is |
|---|---|
| Node label | A free key=value pair a node declares with the server setting labels, for example zone=eu-1,region=eu. The product gives no label a meaning except capacity (below); rules use the others. At most 16 labels; keys and values use A-Za-z0-9._-. |
| Group | The unit that has one primary and its copies. A server started with groups = N has groups 0 to N-1. Group 0 holds the catalog and every table made before placement; every node holds it. Groups 1 and up are the ones rules place. |
| Rule | One line of text that gives a set of tables a policy. The first rule that matches wins; a table that matches no rule gets one copy. |
| Placement map | Kept in the watcher quorum's log. Which nodes hold each group, which is primary, which copy is being built. It holds no data. |
Rules#
One rule per line. A line that starts with # and blank lines are ignored.
rule * copies=3 spread=zone sync=1
rule table=main.users copies=2| Selector | Meaning |
|---|---|
* | every table |
table=NAME | one table (a partitioned table is placed as one table) |
Rules by key prefix, by field value or by key range exist in the grammar but are refused today with 422 needs_partition_key: they need a table that is partitioned by that key (not built).
| Setting | Meaning | Default |
|---|---|---|
copies=N | Holders of a group, including the primary. 1 to 9. | 1 |
spread=KEY | The copies must sit on nodes whose label KEY differs (for example spread=zone). | none |
within=K:V|V,... | A holder must carry one of the listed values of label K (data residency). | none |
not_in=K:V|V,... | A holder must not carry a listed value. | none |
sync=N | Copies that must confirm before a write is acknowledged, 0 to copies-1. See synchronous replication. | 0 |
min=M | The smallest number of copies the group should run with; sync+1 to copies. Reported on the status page (belowMin) and used to order repairs. Writes are not refused below min yet. A rule with min is not readable by watchers and servers of builds before it, so upgrade them first. | copies |
read=primary|any|bounded_stale(MS) | Which copies may answer reads. primary means the primary only. | primary |
prefer=K=V,... | Labels favoured when choosing a copy to read. | none |
The watcher warns when a rule is valid but risky. With sync >= 1 and copies < sync + 2, losing one follower stops writes to the group until it is repaired: copies=3 sync=1 keeps writing with one node down, copies=2 sync=1 does not.
Setting the rules#
Rules are set on a watcher's status port, which listens on its own machine only, with the cluster secret as the bearer token. Read the version first; the write is a compare-and-set, so two operators cannot overwrite each other.
curl -s http://127.0.0.1:STATUS_PORT/rules
curl -X PUT -H "Authorization: Bearer CLUSTER_SECRET" --data-binary @rules.txt "http://127.0.0.1:STATUS_PORT/rules?expected=VERSION"expected is 0 for the first rules. Answers: 200 with the new version and any warnings; 400 with line and column when the text does not parse (nothing is stored); 409 version_conflict; 503 not_leader with the leader's id (ask that watcher). Upgrade every watcher before the first rules are set: an older watcher does not understand them.
Where a table goes#
- A table is placed once, when it is created: the group is recorded in a
groupfile in the table's folder. Nothing moves it afterwards (moving a table is not built). - The group is chosen among groups
1and up whose policy class equals the rule's, taking the one with the fewest tables. Group 0 is never chosen for a new table. - A partitioned table spreads its partitions over those groups one after another, so no node holds all of it: the way to segment a large table over several servers is to create it partitioned (new data, or the old rows imported into it). Existing tables stay in group 0 and on every node.
What the watcher does, and never does#
The leader watcher compares the map with the rules every second and:
- adds a missing copy first. The group goes to state
copying, the chosen node follows the primary, and it joins the copies only after it reports itself caught up with the primary's data epoch; - replaces a dead holder only after the node has been unreachable for
placement_down_after_ms(default 60 s), so a restart moves nothing, and only in the same step in which a caught-up replacement joins; - builds one copy at a time per group, at most
placement_max_jobs(default 2; see the table below) in the cluster andplacement_max_jobs_per_node(default 1) on one node; - chooses a primary only among holders, requires the winner to hold at least as much as every other answering holder, and waits rather than choose a node that lacks data.
It never deletes data. A node that holds a group the plan no longer wants is reported (surplus in the status) and keeps its files; removing it is the operator's act (POST /placement/release?group=G&node=N on the watcher's status port). When the primary itself is not wanted, hand the group over first (POST /v1/control/handover?group=G&to=NODE, see handover and rebalancing), then release.
Uneven placement is allowed. The planner picks the allowed node with the least load, which is the partitions it holds divided by its capacity label (a whole number, default 1). A node with capacity=4 takes about four times what a node with capacity=1 takes.
Watcher settings#
In the watcher's configuration file or as --key=value:
| Key | Meaning | Default |
|---|---|---|
placement_down_after_ms | How long a node must be unreachable before its copies are replaced. 100 to 86400000. | 60000 |
placement_max_jobs | Copy jobs in the whole cluster at one time. Not set: 2, and while a node is down, the live nodes less one (at least 2, at most 4). | not set |
placement_max_jobs_per_node | Copy jobs filling one node at one time. | 1 |
Looking at it#
GET /placement/status on any watcher (the leader's answer is the complete one):
{"rulesVersion":2,"mapVersion":9,"complete":true,"groups":[{"group":1,"class":"copies=3 sync=1 spread=zone","state":"copying","holders":[2,3,4],"primary":2,"copy":{"target":5,"caughtUp":false},"reason":"copying to node 5: waiting for it to hold everything the primary has"}]}state is active, copying or unmet (for example need 3 distinct zone values, have 2; the copies in place stay). reason says why a group is not at its plan yet.
The leader's answer also has maxJobs (copy jobs allowed at once now) and repairOrder: the groups in the order their missing copies are
started, {"group","live","copies","min","belowMin"}. Groups that can no longer confirm their sync writes come first, then those with
the fewest live copies. GET /v1/control/tables on a node says which tables it holds.
Turning it on: the safe order#
- Back up first, and compare every table (count and a hash of the rows) before and after. A check of one table is not enough.
- Tables made while
groups = 1have no group record. A server started with more groups gives such a table the group of a hash of its names, which can be a group this node does not hold: the table would seem to vanish on that node. Record group 0 in thegroupfile of every such table folder beforegroupsis raised (a script for it,tools/multisite/stamp_groups.py, is in the source repository; it is not part of the installed package; the server must be stopped). - Do not set
shardsin the cluster configuration of data made with the server default: a table without ashardsfile uses the default, and a smallershardshides rows that are still on disk. - Start the watchers of nodes that hold no data before their servers, and the server that holds the data first. Empty nodes that elect themselves primary of a group before the node with the data is up make that node follow an empty primary.
- Upgrade every watcher, then set the rules, then create the partitioned tables.
Reads#
With read=any or bounded_stale, a client library that holds the map sends a read to the least loaded copy that is allowed; a copy is eligible only if it has reached the position of the caller's own last write (read your writes) or is within the allowed lag. The front-ends (MySQL, PostgreSQL, SQL Server, Bolt) do not hold a map and rely on the server forwarding. See consistency.
Remarks#
- Plain TCP between nodes and watchers; use a trusted network.
- One decree at a time across all groups.
- Server settings used by placement:
groups,labels,control,control-peers,lag-beacon-ms,read-wait-ms,gather-*; watcher settings:placement_down_after_ms,placement_max_jobs,placement_max_jobs_per_node. See server options and monitoring with the watcher.
Not supported yet#
- Moving a table to another group, adding groups to a running cluster, and a node that returns getting its groups back by itself (a returned node does not regain its groups; an operator releases a surplus copy and the planner restores the count).
- Rules by key prefix, field value or range (they need a partition key).
- The health table shared by all watchers is built (see Node health shared by the watchers): a node found dead by one watcher and confirmed by another is known dead to all, and gets no primary role or handover until it is confirmed up; servers no longer name it to clients. Changing configuration on a running cluster with a per-node table of which nodes applied it is built for the watchers' settings and the servers' live settings (see Changing the configuration of a running cluster); settings that need a restart are only shown. Copies of a dead node's groups start as soon as the health table says
down(about 5 seconds after the failure in the tests, whateverplacement_down_after_msis), groups most at risk first, and, whenplacement_max_jobsis not set, with up to the number of live nodes less one copy jobs at once (at least 2, at most 4) while a node is down. Not built: refusing writes to a group below itsmin. - Cross-partition transactions.
- A comparison with other systems, or any claim about throughput gain, is not made here: it has been shown only on loopback and on a small test cluster.