Data segregation
Placement policies keep chosen parts of a sharded cluster's data on chosen shards: a customer's rows only on shards in the EU, a regulated customer on hardware of its own. You label each shard, write policies that name a range of the key space and the labels a shard must carry to hold it, and every shard then refuses to hold a range a policy forbids it. Use this on a sharded cluster (see the gateway overview) whose data has rules about where it may live.
How it works
A sharded table's rows are placed by their shard key. Every key value hashes to one block, a number from 0 to 0xffffffffffff (2⁴⁸ blocks in all), and the cluster's shard map gives each range of blocks to one shard. All rows with the same key value are in the same block, and so always on the same shard.
A placement policy names a range of blocks, [lo, hi), and the labels a shard must carry to hold any of it. A shard enforces the policies when it would come to hold blocks:
- at start, for every range the shard map gives it: a shard whose map breaks a policy refuses to start;
- when it takes a range in a move (
SHARD TAKE RANGE, whichrdb-shardsends to the destination of every move): the move fails before any row is copied; - when a new shard map is installed (
SHARD MAP INSTALL): a map that gives it a forbidden range is refused.
Each shard checks against its own enterprise configuration file, with the labels that file gives its own shard id. A shard therefore needs the policies and its own shard_labels line in its file; the simplest setup gives every shard the same file. A shard with no shard_labels line carries no labels.
The rules
For a range of blocks on a shard:
- Every policy whose range overlaps it must have all its required labels on the shard. A range that overlaps a policy only in part still needs that policy's labels.
- An exclusive policy keeps other data off its shards. If the range overlaps no policy at all, it may not be placed on a shard that carries every label of an
exclusivepolicy. Ranges that another policy covers may still go there, when the shard carries that policy's labels too.
The refusals read:
placement policy eu_customers requires region=eu-west for blocks [0x1cbe991a14, 0x1cbe991a15); shard 0 does not carry it
shard 2 is reserved to placement policy dedicated (exclusive); blocks [0x0, 0x1cbe991a14) are not itsWrite labels and policies
In the enterprise configuration file (reference):
# shard_labels <shard id> <key=value> …
shard_labels 0 region=us-east
shard_labels 1 region=eu-west
shard_labels 2 region=eu-west tier=dedicated
# policy <name> <lo> <hi> require <key=value> … [exclusive]
policy eu_customers 0x1cbe991a14 0x1cbe991a15 require region=eu-west- Labels are
key=valuewords of your choosing. A shard carries a label when itsshard_labelsline lists exactly that word. loandhiare block numbers, decimal or0xhexadecimal; the range includesloand stops beforehi.- A policy may require several labels; the shard needs all of them.
exclusive, as the last word, makes the policy exclusive.- Write as many
policylines as you need. Policy names appear in refusals only.
The file is read at start: restart each shard after a change.
Find a key's block
rdb_route_block(type, value) gives the block a shard key value hashes to. The type is the shard key column's type: int (or any integer type name), text (or varchar) or uuid. On a shard, rdb_shard_of(type, value) gives the shard that the map it serves assigns that block to.
SELECT rdb_route_block('text', 'acme-eu') AS block,
rdb_shard_of('text', 'acme-eu') AS shard;A policy for one key value covers that one block: lo is the block and hi the block plus one.
Worked example: an EU customer on EU shards
A cluster of three shards: shard 0 in us-east, shards 1 and 2 in eu-west. Its tables are sharded by a customer TEXT column. The customer acme-eu must be stored only in the EU.
1. Find the customer's block and shard. On any shard:
SELECT rdb_route_block('text', 'acme-eu') AS block,
rdb_shard_of('text', 'acme-eu') AS shard;Suppose it returns block 123456789012 (0x1cbe991a14) on shard 0.
2. Move the customer onto an EU shard before the policy exists, so no shard holds the block against it. With rdb-shard (see Moving data for the whole procedure):
export RDB_PASSWORD='operator-password-change-me'
rdb-shard move-tenant --map cluster.map --out cluster.map.next --state acme-eu.move \
--key-type text --key acme-eu --to 1 \
--table orders:customer --table invoices:customer \
--fenced --user rasterizedThe move carves the customer's block into a range of its own and hands it to shard 1.
3. Add the labels and the policy to every shard's enterprise configuration file:
shard_labels 0 region=us-east
shard_labels 1 region=eu-west
shard_labels 2 region=eu-west
policy acme_eu 0x1cbe991a14 0x1cbe991a15 require region=eu-west4. Restart the shards one at a time, each with --enterprise-config. Each checks the ranges its map gives it as it starts. Had a shard outside the EU still held the block, it would refuse to start, naming the policy.
5. Check the policy holds. A move of the customer back to shard 0 now fails when shard 0 is asked to take the range, before any row is copied:
placement policy acme_eu requires region=eu-west for blocks [0x1cbe991a14, 0x1cbe991a15); shard 0 does not carry itA move to shard 2, also in eu-west, is allowed.
Dedicated shards with exclusive
To give a customer hardware of its own, label the shard and make the customer's policy exclusive. Here 0x2d0a9f3c1b stands for the block rdb_route_block gives the customer bigcorp:
shard_labels 2 region=eu-west tier=dedicated
policy bigcorp 0x2d0a9f3c1b 0x2d0a9f3c1c require tier=dedicated exclusiveShard 2 may then hold bigcorp's block and no range that no policy covers. Give the rest of the key space to the other shards before you turn the policy on: a shard 2 whose map still gives it other ranges refuses to start.
An exclusive policy keeps out only uncovered data. In this example shard 2 also carries region=eu-west, so a range under an eu-west policy (such as acme_eu above) may still be placed on it. Give dedicated shards labels no other policy asks for when they must hold one customer only.
Rebalancing
The balancer (rdb-shard rebalance) does not read placement policies. A move it plans onto a shard that a policy forbids is refused by that shard when the move starts, and the run fails with the policy's message. Keep the balancer away from dedicated shards with rdb-shard rebalance --exclude <shard id>, which stops it from moving ranges onto that shard.
Limits
- Policies are checked only by shards (
--role shard). On a server that is not a shard they do nothing. - Each shard checks only itself, with its own file. Keep the files consistent across the cluster; a shard whose file lacks a policy does not enforce it.
- The files are read at start. Adding, removing or changing a policy or a label needs a restart of each shard.
- A policy covers blocks, not values: plan per key value, as above, or over whole ranges of blocks.
- Policies decide where a range may be placed. They do not move data: move it first, then add the policy.