Skip to content

Commit

Permalink
Steps to move a sharded lookup keyspace
Browse files Browse the repository at this point in the history
Signed-off-by: Brendan Dougherty <[email protected]>
  • Loading branch information
brendar committed Oct 24, 2023
1 parent ebde310 commit 95e506c
Show file tree
Hide file tree
Showing 11 changed files with 199 additions and 29 deletions.
4 changes: 1 addition & 3 deletions examples/local/101_initial_cluster.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

3 changes: 3 additions & 0 deletions examples/local/201_newkeyspace_tablets.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion examples/local/401_teardown.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 5 additions & 12 deletions examples/local/create_commerce_schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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);
4 changes: 0 additions & 4 deletions examples/local/create_commerce_seq.sql

This file was deleted.

14 changes: 12 additions & 2 deletions examples/local/create_customer_sharded.sql
Original file line number Diff line number Diff line change
@@ -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;
6 changes: 6 additions & 0 deletions examples/local/create_lookups_sharded.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
create table corder_keyspace_idx(
order_id bigint,
keyspace_id varbinary(10),
primary key(order_id)
) ENGINE=InnoDB;

140 changes: 140 additions & 0 deletions examples/local/movetables-testing.md
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
```
7 changes: 0 additions & 7 deletions examples/local/vschema_commerce_initial.json

This file was deleted.

13 changes: 13 additions & 0 deletions examples/local/vschema_customer_sharded.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -23,6 +32,10 @@
{
"column": "customer_id",
"name": "hash"
},
{
"column": "order_id",
"name": "corder_keyspace_idx"
}
],
"auto_increment": {
Expand Down
18 changes: 18 additions & 0 deletions examples/local/vschema_lookups_sharded.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"sharded": true,
"vindexes": {
"hash": {
"type": "hash"
}
},
"tables": {
"corder_keyspace_idx": {
"column_vindexes": [
{
"column": "order_id",
"name": "hash"
}
]
}
}
}

0 comments on commit 95e506c

Please sign in to comment.