From 95e506c6f4247f9e90390761f09cd0c558001848 Mon Sep 17 00:00:00 2001 From: Brendan Dougherty Date: Tue, 24 Oct 2023 21:57:59 +0000 Subject: [PATCH] Steps to move a sharded lookup keyspace Signed-off-by: Brendan Dougherty --- examples/local/101_initial_cluster.sh | 4 +- examples/local/201_newkeyspace_tablets.sh | 3 + examples/local/401_teardown.sh | 2 +- examples/local/create_commerce_schema.sql | 17 +-- examples/local/create_commerce_seq.sql | 4 - examples/local/create_customer_sharded.sql | 14 +- examples/local/create_lookups_sharded.sql | 6 + examples/local/movetables-testing.md | 140 +++++++++++++++++++ examples/local/vschema_commerce_initial.json | 7 - examples/local/vschema_customer_sharded.json | 13 ++ examples/local/vschema_lookups_sharded.json | 18 +++ 11 files changed, 199 insertions(+), 29 deletions(-) delete mode 100644 examples/local/create_commerce_seq.sql create mode 100644 examples/local/create_lookups_sharded.sql create mode 100644 examples/local/movetables-testing.md delete mode 100644 examples/local/vschema_commerce_initial.json create mode 100644 examples/local/vschema_lookups_sharded.json diff --git a/examples/local/101_initial_cluster.sh b/examples/local/101_initial_cluster.sh index a14fa142e4b..be26379913e 100755 --- a/examples/local/101_initial_cluster.sh +++ b/examples/local/101_initial_cluster.sh @@ -63,11 +63,9 @@ vtctldclient GetTablets --keyspace commerce --shard 0 | grep "primary" || (echo vtctldclient ApplySchema --sql-file create_commerce_schema.sql commerce # create the vschema -vtctldclient ApplyVSchema --vschema-file vschema_commerce_initial.json commerce +vtctldclient ApplyVSchema --vschema-file vschema_commerce_seq.json commerce # start vtgate CELL=zone1 ./scripts/vtgate-up.sh -# start vtadmin -./scripts/vtadmin-up.sh diff --git a/examples/local/201_newkeyspace_tablets.sh b/examples/local/201_newkeyspace_tablets.sh index 6a77605fe29..53b57da3311 100755 --- a/examples/local/201_newkeyspace_tablets.sh +++ b/examples/local/201_newkeyspace_tablets.sh @@ -32,6 +32,9 @@ for i in ${BASETABLETNUM}00 ${BASETABLETNUM}01 ${BASETABLETNUM}02; do SHARD=${SHARD} CELL=zone1 KEYSPACE=${KEYSPACE} TABLET_UID=$i ./scripts/vttablet-up.sh done +# set the correct durability policy for the keyspace +vtctldclient --server localhost:15999 SetKeyspaceDurabilityPolicy --durability-policy=semi_sync ${KEYSPACE} + # Wait for all the tablets to be up and registered in the topology server for _ in $(seq 0 200); do vtctldclient GetTablets --keyspace "${KEYSPACE}" --shard "${SHARD}" | wc -l | grep -q "3" && break diff --git a/examples/local/401_teardown.sh b/examples/local/401_teardown.sh index ae78d262486..5e607285df0 100755 --- a/examples/local/401_teardown.sh +++ b/examples/local/401_teardown.sh @@ -25,7 +25,7 @@ source ./env.sh ./scripts/vtgate-down.sh -for tablet in 100 200 300 400; do +for tablet in 100 200 300 400 500 600 700; do if vtctlclient --action_timeout 1s --server localhost:15999 GetTablet zone1-$tablet >/dev/null 2>&1; then # The zero tablet is up. Try to shutdown 0-2 tablet + mysqlctl for i in 0 1 2; do diff --git a/examples/local/create_commerce_schema.sql b/examples/local/create_commerce_schema.sql index e62e7d2e8bf..735b4796161 100644 --- a/examples/local/create_commerce_schema.sql +++ b/examples/local/create_commerce_schema.sql @@ -4,15 +4,8 @@ create table product( price bigint, primary key(sku) ) ENGINE=InnoDB; -create table customer( - customer_id bigint not null auto_increment, - email varbinary(128), - primary key(customer_id) -) ENGINE=InnoDB; -create table corder( - order_id bigint not null auto_increment, - customer_id bigint, - sku varbinary(128), - price bigint, - primary key(order_id) -) ENGINE=InnoDB; + +create table customer_seq(id int, next_id bigint, cache bigint, primary key(id)) comment 'vitess_sequence'; +insert into customer_seq(id, next_id, cache) values(0, 1000, 100); +create table order_seq(id int, next_id bigint, cache bigint, primary key(id)) comment 'vitess_sequence'; +insert into order_seq(id, next_id, cache) values(0, 1000, 100); diff --git a/examples/local/create_commerce_seq.sql b/examples/local/create_commerce_seq.sql deleted file mode 100644 index b4e66c771b6..00000000000 --- a/examples/local/create_commerce_seq.sql +++ /dev/null @@ -1,4 +0,0 @@ -create table customer_seq(id int, next_id bigint, cache bigint, primary key(id)) comment 'vitess_sequence'; -insert into customer_seq(id, next_id, cache) values(0, 1000, 100); -create table order_seq(id int, next_id bigint, cache bigint, primary key(id)) comment 'vitess_sequence'; -insert into order_seq(id, next_id, cache) values(0, 1000, 100); diff --git a/examples/local/create_customer_sharded.sql b/examples/local/create_customer_sharded.sql index 9d3931c7c94..ed03138ec55 100644 --- a/examples/local/create_customer_sharded.sql +++ b/examples/local/create_customer_sharded.sql @@ -1,2 +1,12 @@ -alter table customer change customer_id customer_id bigint not null; -alter table corder change order_id order_id bigint not null; +create table customer( + customer_id bigint not null, + email varbinary(128), + primary key(customer_id) +) ENGINE=InnoDB; +create table corder( + order_id bigint not null, + customer_id bigint, + sku varbinary(128), + price bigint, + primary key(order_id) +) ENGINE=InnoDB; diff --git a/examples/local/create_lookups_sharded.sql b/examples/local/create_lookups_sharded.sql new file mode 100644 index 00000000000..66dcf684a04 --- /dev/null +++ b/examples/local/create_lookups_sharded.sql @@ -0,0 +1,6 @@ +create table corder_keyspace_idx( + order_id bigint, + keyspace_id varbinary(10), + primary key(order_id) +) ENGINE=InnoDB; + diff --git a/examples/local/movetables-testing.md b/examples/local/movetables-testing.md new file mode 100644 index 00000000000..1ed4e49eae2 --- /dev/null +++ b/examples/local/movetables-testing.md @@ -0,0 +1,140 @@ +```bash +source env.sh + +# Bring up initial cluster and unsharded commerce keyspace +./101_initial_cluster.sh + +# Bring up sharded customer keyspace shard -80 +./201_newkeyspace_tablets.sh customer 2 -80 + +# Bring up sharded customer keyspace shard 80- +./201_newkeyspace_tablets.sh customer 3 80- + +# Bring up sharded lookups keyspace shard -80 +./201_newkeyspace_tablets.sh lookups 4 -80 + +# Bring up sharded lookups keyspace shard 80- +./201_newkeyspace_tablets.sh lookups 5 80- + +# Apply vschemas: (commerce vschema was already applied in 101_initial_cluster) +vtctldclient ApplyVSchema --vschema-file vschema_customer_sharded.json customer +vtctldclient ApplyVSchema --vschema-file vschema_lookups_sharded.json lookups + +# Apply schemas (commerce schema was already applied in 101_initial_cluster) +vtctldclient ApplySchema --sql-file create_customer_sharded.sql customer +vtctldclient ApplySchema --sql-file create_lookups_sharded.sql lookups + +# Insert data +mysql < ../common/insert_commerce_data.sql + +# Confirm lookups are being used +mysql --comments --execute "EXPLAIN format=vtexplain SELECT * FROM corder WHERE order_id = 1000" ++------+----------+-------+--------------------------------------------------------------------------------+ +| # | keyspace | shard | query | ++------+----------+-------+--------------------------------------------------------------------------------+ +| 0 | lookups | -80 | select order_id, keyspace_id from corder_keyspace_idx where order_id in (1000) | +| 1 | customer | -80 | select order_id, customer_id, sku, price from corder where order_id = 1000 | ++------+----------+-------+--------------------------------------------------------------------------------+ +2 rows in set (0.00 sec) + +# Bring up sharded lookups2 keyspace shard -80 +./201_newkeyspace_tablets.sh lookups2 6 -80 + +# Bring up sharded lookups2 keyspace shard 80- +./201_newkeyspace_tablets.sh lookups2 7 80- + +# Apply original lookups vschema to lookups2 +vtctldclient ApplyVSchema --vschema-file vschema_lookups_sharded.json lookups2 + +# Start moving lookups to lookups2 +vtctlclient MoveTables -- --source lookups --tables='corder_keyspace_idx' Create lookups2.lookups_to_lookups2 + +# Ensure copy completed and vreplication is running +vtctlclient MoveTables -- progress lookups2.lookups_to_lookups2 + +# Start vdiff +vtctlclient VDiff -- --v2 lookups2.lookups_to_lookups2 create + +# Confirm success +vtctlclient VDiff -- --v2 lookups2.lookups_to_lookups2 show last + +# Switch traffic +vtctlclient MoveTables -- SwitchTraffic lookups2.lookups_to_lookups2 + +# Confirm queries are using the new lookups2 keyspace +mysql --comments --execute "EXPLAIN format=vtexplain SELECT * FROM corder WHERE order_id = 1000" ++------+----------+-------+--------------------------------------------------------------------------------+ +| # | keyspace | shard | query | ++------+----------+-------+--------------------------------------------------------------------------------+ +| 0 | lookups2 | -80 | select order_id, keyspace_id from corder_keyspace_idx where order_id in (1000) | +| 1 | customer | -80 | select order_id, customer_id, sku, price from corder where order_id = 1000 | ++------+----------+-------+--------------------------------------------------------------------------------+ +2 rows in set (0.00 sec) + +# Confirm inserts will go to the new lookups2 keyspace +mysql --comments --execute "EXPLAIN /*vt+ EXECUTE_DML_QUERIES */ format=vtexplain insert into corder(customer_id, sku, price) values(5, 'SKU-1002', 30)" ++------+----------+-------+----------------------------------------------------------------------------------------+ +| # | keyspace | shard | query | ++------+----------+-------+----------------------------------------------------------------------------------------+ +| 0 | lookups2 | -80 | begin | +| 0 | lookups2 | -80 | insert into corder_keyspace_idx(order_id, keyspace_id) values (1009, 'p�<� + �z') | +| 1 | customer | -80 | begin | +| 1 | customer | -80 | insert into corder(customer_id, sku, price, order_id) values (5, 'SKU-1002', 30, 1009) | ++------+----------+-------+----------------------------------------------------------------------------------------+ + +# Update the customer vschema to use `lookups2` +vtctlclient ApplyVSchema -- --vschema "$(cat vschema_customer_sharded.json | sed s/lookups\.corder_keyspace_idx/lookups2\.corder_keyspace_idx/)" customer + +# Complete the movetables +vtctlclient MoveTables -- Complete lookups2.lookups_to_lookups2 + +# Confirm you can insert and query records +mysql --comments + +mysql> EXPLAIN format=vtexplain SELECT * FROM corder WHERE order_id = (SELECT MAX(order_id) FROM corder); ++------+----------+-------+--------------------------------------------------------------------------------+ +| # | keyspace | shard | query | ++------+----------+-------+--------------------------------------------------------------------------------+ +| 0 | customer | -80 | select max(order_id) from corder | +| 1 | customer | 80- | select max(order_id) from corder | +| 2 | lookups2 | 80- | select order_id, keyspace_id from corder_keyspace_idx where order_id in (1101) | +| 3 | customer | -80 | select order_id, customer_id, sku, price from corder where order_id = 1101 | ++------+----------+-------+--------------------------------------------------------------------------------+ +4 rows in set (0.00 sec) + +mysql> EXPLAIN /*vt+ EXECUTE_DML_QUERIES */ format=vtexplain insert into corder(customer_id, sku, price) values(5, 'SKU-1002', 30); ++------+----------+-------+----------------------------------------------------------------------------------------+ +| # | keyspace | shard | query | ++------+----------+-------+----------------------------------------------------------------------------------------+ +| 0 | lookups2 | -80 | begin | +| 0 | lookups2 | -80 | insert into corder_keyspace_idx(order_id, keyspace_id) values (1102, 'p�<� + �z') | +| 1 | customer | -80 | begin | +| 1 | customer | -80 | insert into corder(customer_id, sku, price, order_id) values (5, 'SKU-1002', 30, 1102) | ++------+----------+-------+----------------------------------------------------------------------------------------+ +4 rows in set (0.01 sec) + +mysql> SELECT * FROM corder WHERE order_id = (SELECT MAX(order_id) FROM corder); ++----------+-------------+--------------------+-------+ +| order_id | customer_id | sku | price | ++----------+-------------+--------------------+-------+ +| 1102 | 5 | 0x534B552D31303032 | 30 | ++----------+-------------+--------------------+-------+ +1 row in set (0.00 sec) + +# Confirm table was removed from original lookups keyspace +mysql> show tables from lookups; +Empty set (0.00 sec) + +# Confirm table removed from original lookups vschema +vtctlclient GetVschema lookups +{ + "sharded": true, + "vindexes": { + "hash": { + "type": "hash" + } + } +} +``` diff --git a/examples/local/vschema_commerce_initial.json b/examples/local/vschema_commerce_initial.json deleted file mode 100644 index 0d1bae44428..00000000000 --- a/examples/local/vschema_commerce_initial.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "tables": { - "product": {}, - "customer": {}, - "corder": {} - } -} diff --git a/examples/local/vschema_customer_sharded.json b/examples/local/vschema_customer_sharded.json index 3109e2a2f3c..9dd7ce9306a 100644 --- a/examples/local/vschema_customer_sharded.json +++ b/examples/local/vschema_customer_sharded.json @@ -3,6 +3,15 @@ "vindexes": { "hash": { "type": "hash" + }, + "corder_keyspace_idx": { + "type": "consistent_lookup_unique", + "params": { + "table": "lookups.corder_keyspace_idx", + "from": "order_id", + "to": "keyspace_id" + }, + "owner": "corder" } }, "tables": { @@ -23,6 +32,10 @@ { "column": "customer_id", "name": "hash" + }, + { + "column": "order_id", + "name": "corder_keyspace_idx" } ], "auto_increment": { diff --git a/examples/local/vschema_lookups_sharded.json b/examples/local/vschema_lookups_sharded.json new file mode 100644 index 00000000000..0d30dfca91d --- /dev/null +++ b/examples/local/vschema_lookups_sharded.json @@ -0,0 +1,18 @@ +{ + "sharded": true, + "vindexes": { + "hash": { + "type": "hash" + } + }, + "tables": { + "corder_keyspace_idx": { + "column_vindexes": [ + { + "column": "order_id", + "name": "hash" + } + ] + } + } +}