Skip to content

Instantly share code, notes, and snippets.

@dasl-
Created January 18, 2019 21:46
Show Gist options
  • Save dasl-/c46c31f604c82a0167ac06908aff54ea to your computer and use it in GitHub Desktop.
Save dasl-/c46c31f604c82a0167ac06908aff54ea to your computer and use it in GitHub Desktop.
#!/bin/bash
# This script initializes zookeeper, mysql, vttablet, and vtgate
# There are 3 mysql instances that are setup: an unsharded shops_index table that will back a lookup vindex
# for the sharded shop_data table. The shop_data table is sharded across two mysql shards.
cd $VTROOT/src/vitess.io/vitess/examples/local
./zk-up.sh
./vtctld-up.sh
# Set some useful variables
# Set topology environment parameters.
ZK_SERVER="localhost:21811,localhost:21812,localhost:21813"
TOPOLOGY_FLAGS="-topo_implementation zk2 -topo_global_server_address ${ZK_SERVER} -topo_global_root /vitess/global"
# Bring up tablets, an unsharded index keyspace with 2 replicas
# And a sharded keyspace with 2 shards, each with 2 replicas
# Designate a master for each keyspace:shard
#
# If you get errors like:
# E0114 21:43:31.676030 9980 main.go:63] Remote error: rpc error: code = Unknown desc = zk: could not connect to a server
# try upping the resouces that docker can use and then restart docker:
# https://i.imgur.com/9thANH5.png
#
# sleep between these commands to prevent errors like:
# E0116 20:16:58.119028 3483 main.go:63] Remote error: rpc error: code = Unknown desc = node doesn't exist: /vitess/global/keyspaces/index_test/shards/0/locks/
KEYSPACE=index_test TABLETS_UIDS='0 1' ./vttablet-up.sh
sleep 2
./lvtctl.sh InitShardMaster -force index_test/0 test-100
KEYSPACE=shard_test TABLETS_UIDS='0 1' SHARD='-80' UID_BASE=200 ./vttablet-up.sh
sleep 2
./lvtctl.sh InitShardMaster -force shard_test/-80 test-200
KEYSPACE=shard_test TABLETS_UIDS='0 1' SHARD='80-' UID_BASE=300 ./vttablet-up.sh
sleep 2
./lvtctl.sh InitShardMaster -force shard_test/80- test-300
# Put some tables in the databases
echo 'CREATE TABLE `shops_index` (
`shop_id` bigint(20) unsigned NOT NULL,
`shop_shard` int(11) NOT NULL,
PRIMARY KEY (`shop_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=COMPRESSED KEY_BLOCK_SIZE=8;' > /tmp/index.sql
./lvtctl.sh ApplySchema -sql-file /tmp/index.sql index_test
echo 'CREATE TABLE `shop_data` (
`shop_id` bigint(20) unsigned NOT NULL,
`name` varchar(255) CHARACTER SET utf8 NOT NULL,
PRIMARY KEY (`shop_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=COMPRESSED KEY_BLOCK_SIZE=8;' > /tmp/shard.sql
./lvtctl.sh ApplySchema -sql-file /tmp/shard.sql shard_test
# create the vschemas
echo '{
"sharded": false,
"tables": {
"shops_index": {}
}
}' > /tmp/index_vschema.json
./lvtctl.sh ApplyVSchema -vschema_file /tmp/index_vschema.json index_test
echo '{
"sharded": true,
"vindexes": {
"shop_id_shop_shard_idx": {
"type": "lookup_hash_unique",
"params": {
"from": "shop_id",
"table": "shops_index",
"to": "shop_shard"
}
}
},
"tables": {
"shop_data": {
"column_vindexes": [
{
"column": "shop_id",
"name": "shop_id_shop_shard_idx"
}
]
}
}
}' > /tmp/shard_vschema.json
./lvtctl.sh ApplyVSchema -vschema_file /tmp/shard_vschema.json shard_test
./vtgate-up.sh
for i in $(seq 1 10); do
echo -n .
vtgate_pid=$(cat $VTDATAROOT/tmp/vtgate.pid)
[ $? -ne 0 ] || break
sleep 1
done
echo $vtgate_pid
# vtexplain queries. vtexplain requires combined vschema and sql schema files.
echo '{"index_test": ' "$(cat /tmp/index_vschema.json)" ', "shard_test":' "$(cat /tmp/shard_vschema.json)" '}' > /tmp/combined_vschema.json
cat /tmp/index.sql /tmp/shard.sql > /tmp/combined.sql
vtexplain -shards 1 -vschema-file /tmp/combined_vschema.json -schema-file /tmp/combined.sql -replication-mode "ROW" -output-mode text -sql "INSERT INTO shops_index (shop_id, shop_shard) VALUES(123, 100)"
vtexplain -shards 2 -vschema-file /tmp/combined_vschema.json -schema-file /tmp/combined.sql -replication-mode "ROW" -output-mode text -sql "INSERT INTO shop_data (shop_id, name) VALUES(123, 'my_shop')"
# insert data
vtctl -enable_queries ${TOPOLOGY_FLAGS} VtGateExecute -server localhost:15991 "INSERT INTO shops_index (shop_id, shop_shard) VALUES(1, 1)"
vtctl -enable_queries ${TOPOLOGY_FLAGS} VtGateExecute -server localhost:15991 "INSERT INTO shops_index (shop_id, shop_shard) VALUES(2, 2)"
vtctl -enable_queries ${TOPOLOGY_FLAGS} VtGateExecute -server localhost:15991 "INSERT INTO shops_index (shop_id, shop_shard) VALUES(3, 3)"
vtctl -enable_queries ${TOPOLOGY_FLAGS} VtGateExecute -server localhost:15991 "INSERT INTO shops_index (shop_id, shop_shard) VALUES(4, 4)"
vtctl -enable_queries ${TOPOLOGY_FLAGS} VtGateExecute -server localhost:15991 "INSERT INTO shops_index (shop_id, shop_shard) VALUES(5, 5)"
vtctl -enable_queries ${TOPOLOGY_FLAGS} VtGateExecute -server localhost:15991 "INSERT INTO shops_index (shop_id, shop_shard) VALUES(6, 6)"
vtctl -enable_queries ${TOPOLOGY_FLAGS} VtGateExecute -server localhost:15991 "INSERT INTO shops_index (shop_id, shop_shard) VALUES(7, 7)"
vtctl -enable_queries ${TOPOLOGY_FLAGS} VtGateExecute -server localhost:15991 "INSERT INTO shops_index (shop_id, shop_shard) VALUES(8, 8)"
vtctl -enable_queries ${TOPOLOGY_FLAGS} VtGateExecute -server localhost:15991 "INSERT INTO shop_data (shop_id, name) VALUES(1, 'my_shop')"
vtctl -enable_queries ${TOPOLOGY_FLAGS} VtGateExecute -server localhost:15991 "INSERT INTO shop_data (shop_id, name) VALUES(2, 'my_shop')"
vtctl -enable_queries ${TOPOLOGY_FLAGS} VtGateExecute -server localhost:15991 "INSERT INTO shop_data (shop_id, name) VALUES(3, 'my_shop')"
vtctl -enable_queries ${TOPOLOGY_FLAGS} VtGateExecute -server localhost:15991 "INSERT INTO shop_data (shop_id, name) VALUES(4, 'my_shop')"
vtctl -enable_queries ${TOPOLOGY_FLAGS} VtGateExecute -server localhost:15991 "INSERT INTO shop_data (shop_id, name) VALUES(5, 'my_shop')"
vtctl -enable_queries ${TOPOLOGY_FLAGS} VtGateExecute -server localhost:15991 "INSERT INTO shop_data (shop_id, name) VALUES(6, 'my_shop')"
vtctl -enable_queries ${TOPOLOGY_FLAGS} VtGateExecute -server localhost:15991 "INSERT INTO shop_data (shop_id, name) VALUES(7, 'my_shop')"
vtctl -enable_queries ${TOPOLOGY_FLAGS} VtGateExecute -server localhost:15991 "INSERT INTO shop_data (shop_id, name) VALUES(8, 'my_shop')"
echo "shops_index data:"
vtctl -enable_queries ${TOPOLOGY_FLAGS} VtGateExecute -server localhost:15991 "select * from shops_index"
echo "shop_data data:"
vtctl -enable_queries ${TOPOLOGY_FLAGS} VtGateExecute -server localhost:15991 "select * from shop_data"
echo "shop_data shard -80 data:"
vtctl -enable_queries ${TOPOLOGY_FLAGS} VtGateExecute -server localhost:15991 -target shard_test:-80 "select * from shop_data"
echo "shop_data shard 80- data:"
vtctl -enable_queries ${TOPOLOGY_FLAGS} VtGateExecute -server localhost:15991 -target shard_test:80- "select * from shop_data"
dlv attach $vtgate_pid --listen=:40000 --headless --api-version=2 --log
# dlv attach $vtgate_pid
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment