diff --git a/examples/region_sharding/101_initial_cluster.sh b/examples/region_sharding/101_initial_cluster.sh new file mode 100755 index 00000000000..46ed082886c --- /dev/null +++ b/examples/region_sharding/101_initial_cluster.sh @@ -0,0 +1,57 @@ +#!/bin/bash + +# Copyright 2019 The Vitess Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# this script brings up zookeeper and all the vitess components +# required for a single shard deployment. + +source ./env.sh + +# start topo server +if [ "${TOPO}" = "zk2" ]; then + CELL=zone1 ./scripts/zk-up.sh +elif [ "${TOPO}" = "k8s" ]; then + CELL=zone1 ./scripts/k3s-up.sh +else + CELL=zone1 ./scripts/etcd-up.sh +fi + +# start vtctld +CELL=zone1 ./scripts/vtctld-up.sh + +# start vttablets for main keyspace. we start only one tablet each (master) +CELL=zone1 TABLET_UID=100 ./scripts/mysqlctl-up.sh +SHARD=-40 CELL=zone1 KEYSPACE=main TABLET_UID=100 ./scripts/vttablet-up.sh +CELL=zone1 TABLET_UID=200 ./scripts/mysqlctl-up.sh +SHARD=40-80 CELL=zone1 KEYSPACE=main TABLET_UID=200 ./scripts/vttablet-up.sh +CELL=zone1 TABLET_UID=300 ./scripts/mysqlctl-up.sh +SHARD=80-c0 CELL=zone1 KEYSPACE=main TABLET_UID=300 ./scripts/vttablet-up.sh +CELL=zone1 TABLET_UID=400 ./scripts/mysqlctl-up.sh +SHARD=c0- CELL=zone1 KEYSPACE=main TABLET_UID=400 ./scripts/vttablet-up.sh + +# set master +vtctlclient InitShardMaster -force main/-40 zone1-100 +vtctlclient InitShardMaster -force main/40-80 zone1-200 +vtctlclient InitShardMaster -force main/80-c0 zone1-300 +vtctlclient InitShardMaster -force main/c0- zone1-400 + +# create the schema +vtctlclient ApplySchema -sql-file create_main_schema.sql main + +# create the vschema +vtctlclient ApplyVSchema -vschema_file main_vschema.json main + +# start vtgate +CELL=zone1 ./scripts/vtgate-up.sh diff --git a/examples/region_sharding/201_teardown.sh b/examples/region_sharding/201_teardown.sh new file mode 100755 index 00000000000..4f8e869ffeb --- /dev/null +++ b/examples/region_sharding/201_teardown.sh @@ -0,0 +1,59 @@ +#!/bin/bash + +# Copyright 2019 The Vitess Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# We should not assume that any of the steps have been executed. +# This makes it possible for a user to cleanup at any point. + +source ./env.sh + +./scripts/vtgate-down.sh + +for tablet in 100 200 300 400; do + if vtctlclient -server localhost:15999 GetTablet zone1-$tablet >/dev/null 2>&1; then + echo "Shutting down tablet zone1-$tablet" + CELL=zone1 TABLET_UID=$tablet ./scripts/vttablet-down.sh + echo "Shutting down mysql zone1-$tablet" + CELL=zone1 TABLET_UID=$tablet ./scripts/mysqlctl-down.sh + fi +done + +./scripts/vtctld-down.sh + +if [ "${TOPO}" = "zk2" ]; then + CELL=zone1 ./scripts/zk-down.sh +elif [ "${TOPO}" = "k8s" ]; then + CELL=zone1 ./scripts/k3s-down.sh +else + CELL=zone1 ./scripts/etcd-down.sh +fi + +# pedantic check: grep for any remaining processes + +if [ ! -z "$VTDATAROOT" ]; then + + if pgrep -f -l "$VTDATAROOT" >/dev/null; then + echo "ERROR: Stale processes detected! It is recommended to manuallly kill them:" + pgrep -f -l "$VTDATAROOT" + else + echo "All good! It looks like every process has shut down" + fi + + # shellcheck disable=SC2086 + rm -r ${VTDATAROOT:?}/* + +fi + +disown -a diff --git a/examples/region_sharding/README.md b/examples/region_sharding/README.md new file mode 100644 index 00000000000..6392463553e --- /dev/null +++ b/examples/region_sharding/README.md @@ -0,0 +1,22 @@ +# Instructions + +Detailed instructions for running this example can be found at https://vitess.io. +This document contains the summary of the commands to be run. + + +``` +# Edit main_vschema.json and set region_map to full path of countries.json file +# Example: + "region_map": "/home/user/vitess/examples/region_sharding/countries.json", + + +# Bring up initial cluster and main keyspace +./101_initial_cluster.sh + +# Insert and verify data +mysql < insert_customers.sql +mysql --table < show_data.sql + +# Down cluster +./201_teardown.sh +``` diff --git a/examples/region_sharding/countries.json b/examples/region_sharding/countries.json new file mode 100644 index 00000000000..ed9dde2d29b --- /dev/null +++ b/examples/region_sharding/countries.json @@ -0,0 +1,10 @@ +{ + "United States": 1, + "Canada": 2, + "France": 64, + "Germany": 65, + "China": 128, + "Japan": 129, + "India": 192, + "Indonesia": 193 +} diff --git a/examples/region_sharding/create_main_schema.sql b/examples/region_sharding/create_main_schema.sql new file mode 100644 index 00000000000..2e41cb35e54 --- /dev/null +++ b/examples/region_sharding/create_main_schema.sql @@ -0,0 +1,12 @@ +CREATE TABLE customer ( + id int NOT NULL, + fullname varbinary(256), + nationalid varbinary(256), + country varbinary(256), + primary key(id) + ); +CREATE TABLE customer_lookup ( + id int NOT NULL, + keyspace_id varbinary(256), + primary key(id) + ); diff --git a/examples/region_sharding/env.sh b/examples/region_sharding/env.sh new file mode 100644 index 00000000000..c0b982a3dbe --- /dev/null +++ b/examples/region_sharding/env.sh @@ -0,0 +1,82 @@ +#!/bin/bash + +# Copyright 2019 The Vitess Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +hostname=$(hostname -f) +vtctld_web_port=15000 +export VTDATAROOT="${VTDATAROOT:-${PWD}/vtdataroot}" + +function fail() { + echo "ERROR: $1" + exit 1 +} + +if [[ $EUID -eq 0 ]]; then + fail "This script refuses to be run as root. Please switch to a regular user." +fi + +# mysqld might be in /usr/sbin which will not be in the default PATH +PATH="/usr/sbin:$PATH" +for binary in mysqld etcd etcdctl curl vtctlclient vttablet vtgate vtctld mysqlctl; do + command -v "$binary" > /dev/null || fail "${binary} is not installed in PATH. See https://vitess.io/docs/get-started/local/ for install instructions." +done; + +if [ "${TOPO}" = "zk2" ]; then + # Each ZooKeeper server needs a list of all servers in the quorum. + # Since we're running them all locally, we need to give them unique ports. + # In a real deployment, these should be on different machines, and their + # respective hostnames should be given. + zkcfg=(\ + "1@$hostname:28881:38881:21811" \ + "2@$hostname:28882:38882:21812" \ + "3@$hostname:28883:38883:21813" \ + ) + printf -v zkcfg ",%s" "${zkcfg[@]}" + zkcfg=${zkcfg:1} + + zkids='1 2 3' + + # Set topology environment parameters. + ZK_SERVER="localhost:21811,localhost:21812,localhost:21813" + # shellcheck disable=SC2034 + TOPOLOGY_FLAGS="-topo_implementation zk2 -topo_global_server_address ${ZK_SERVER} -topo_global_root /vitess/global" + + mkdir -p "${VTDATAROOT}/tmp" +elif [ "${TOPO}" = "k8s" ]; then + # Set topology environment parameters. + K8S_ADDR="localhost" + K8S_PORT="8443" + K8S_KUBECONFIG=$VTDATAROOT/tmp/k8s.kubeconfig + # shellcheck disable=SC2034 + TOPOLOGY_FLAGS="-topo_implementation k8s -topo_k8s_kubeconfig ${K8S_KUBECONFIG} -topo_global_server_address ${K8S_ADDR}:${K8S_PORT} -topo_global_root /vitess/global" +else + ETCD_SERVER="localhost:2379" + TOPOLOGY_FLAGS="-topo_implementation etcd2 -topo_global_server_address $ETCD_SERVER -topo_global_root /vitess/global" + + mkdir -p "${VTDATAROOT}/etcd" +fi + +mkdir -p "${VTDATAROOT}/tmp" + +# Set aliases to simplify instructions. +# In your own environment you may prefer to use config files, +# such as ~/.my.cnf + +alias mysql="command mysql -h 127.0.0.1 -P 15306" +alias vtctlclient="command vtctlclient -server localhost:15999 -log_dir ${VTDATAROOT}/tmp -alsologtostderr" + +# Make sure aliases are expanded in non-interactive shell +shopt -s expand_aliases + diff --git a/examples/region_sharding/insert_customers.sql b/examples/region_sharding/insert_customers.sql new file mode 100644 index 00000000000..a055cf89e51 --- /dev/null +++ b/examples/region_sharding/insert_customers.sql @@ -0,0 +1,16 @@ +insert into customer(id, fullname, nationalid, country) values (1, 'Philip Roth', '123-456-789', 'United States'); +insert into customer(id, fullname, nationalid, country) values (2, 'Gary Shteyngart', '234-567-891', 'United States'); +insert into customer(id, fullname, nationalid, country) values (3, 'Margaret Atwood', '345-678-912', 'Canada'); +insert into customer(id, fullname, nationalid, country) values (4, 'Alice Munro', '456-789-123', 'Canada'); +insert into customer(id, fullname, nationalid, country) values (5, 'Albert Camus', '912-345-678', 'France'); +insert into customer(id, fullname, nationalid, country) values (6, 'Colette', '102-345-678', 'France'); +insert into customer(id, fullname, nationalid, country) values (7, 'Hermann Hesse', '304-567-891', 'Germany'); +insert into customer(id, fullname, nationalid, country) values (8, 'Cornelia Funke', '203-456-789', 'Germany'); +insert into customer(id, fullname, nationalid, country) values (9, 'Cixin Liu', '789-123-456', 'China'); +insert into customer(id, fullname, nationalid, country) values (10, 'Jian Ma', '891-234-567', 'China'); +insert into customer(id, fullname, nationalid, country) values (11, 'Haruki Murakami', '405-678-912', 'Japan'); +insert into customer(id, fullname, nationalid, country) values (12, 'Banana Yoshimoto', '506-789-123', 'Japan'); +insert into customer(id, fullname, nationalid, country) values (13, 'Arundhati Roy', '567-891-234', 'India'); +insert into customer(id, fullname, nationalid, country) values (14, 'Shashi Tharoor', '678-912-345', 'India'); +insert into customer(id, fullname, nationalid, country) values (15, 'Andrea Hirata', '607-891-234', 'Indonesia'); +insert into customer(id, fullname, nationalid, country) values (16, 'Ayu Utami', '708-912-345', 'Indonesia'); diff --git a/examples/region_sharding/main_vschema.json b/examples/region_sharding/main_vschema.json new file mode 100644 index 00000000000..48a8f93dce4 --- /dev/null +++ b/examples/region_sharding/main_vschema.json @@ -0,0 +1,46 @@ +{ + "sharded": true, + "vindexes": { + "region_vdx": { + "type": "region_json", + "params": { + "region_map": "/home/user/my-vitess/examples/region_sharding/countries.json", + "region_bytes": "1" + } + }, + "customer_region_lookup": { + "type": "consistent_lookup_unique", + "params": { + "table": "customer_lookup", + "from": "id", + "to": "keyspace_id" + }, + "owner": "customer" + }, + "identity": { + "type": "binary" + } + }, + "tables": { + "customer_lookup": { + "column_vindexes": [ + { + "column": "keyspace_id", + "name": "identity" + } + ] + }, + "customer": { + "column_vindexes": [ + { + "columns": ["id", "country"], + "name": "region_vdx" + }, + { + "column": "id", + "name": "customer_region_lookup" + } + ] + } + } +} diff --git a/examples/region_sharding/scripts/etcd-down.sh b/examples/region_sharding/scripts/etcd-down.sh new file mode 100755 index 00000000000..018af7432a3 --- /dev/null +++ b/examples/region_sharding/scripts/etcd-down.sh @@ -0,0 +1,22 @@ +#!/bin/bash + +# Copyright 2019 The Vitess Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# This is an example script that stops the etcd servers started by etcd-up.sh. + +source ./env.sh + +echo "Stopping etcd..." +kill -9 `cat $VTDATAROOT/tmp/etcd.pid` diff --git a/examples/region_sharding/scripts/etcd-up.sh b/examples/region_sharding/scripts/etcd-up.sh new file mode 100755 index 00000000000..d8c0a869a7e --- /dev/null +++ b/examples/region_sharding/scripts/etcd-up.sh @@ -0,0 +1,51 @@ +#!/bin/bash + +# Copyright 2019 The Vitess Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# This is an example script that creates a quorum of ZooKeeper servers. + +source ./env.sh + +cell=${CELL:-'test'} +export ETCDCTL_API=2 + +# Check that etcd is not already running +curl "http://${ETCD_SERVER}" > /dev/null 2>&1 && fail "etcd is already running. Exiting." + +etcd --enable-v2=true --data-dir "${VTDATAROOT}/etcd/" --listen-client-urls "http://${ETCD_SERVER}" --advertise-client-urls "http://${ETCD_SERVER}" > "${VTDATAROOT}"/tmp/etcd.out 2>&1 & +PID=$! +echo $PID > "${VTDATAROOT}/tmp/etcd.pid" +sleep 5 + +echo "add /vitess/global" +etcdctl --endpoints "http://${ETCD_SERVER}" mkdir /vitess/global & + +echo "add /vitess/$cell" +etcdctl --endpoints "http://${ETCD_SERVER}" mkdir /vitess/$cell & + +# And also add the CellInfo description for the cell. +# If the node already exists, it's fine, means we used existing data. +echo "add $cell CellInfo" +set +e +# shellcheck disable=SC2086 +vtctl $TOPOLOGY_FLAGS AddCellInfo \ + -root /vitess/$cell \ + -server_address "${ETCD_SERVER}" \ + $cell +set -e + +echo "etcd start done..." + + diff --git a/examples/region_sharding/scripts/k3s-down.sh b/examples/region_sharding/scripts/k3s-down.sh new file mode 100755 index 00000000000..590dc604e3e --- /dev/null +++ b/examples/region_sharding/scripts/k3s-down.sh @@ -0,0 +1,30 @@ +#!/bin/bash + +# Copyright 2019 The Vitess Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# This is an example script that stops the k3s server started by k3s-up.sh. + +set -e + +# shellcheck source=./env.sh +# shellcheck disable=SC1091 +source ./env.sh + +# Stop K3s server. +echo "Stopping k3s server..." + +pid=`cat $VTDATAROOT/tmp/k3s.pid` +echo "Stopping k3s..." +kill -9 $pid diff --git a/examples/region_sharding/scripts/k3s-up.sh b/examples/region_sharding/scripts/k3s-up.sh new file mode 100755 index 00000000000..eb8530f15d8 --- /dev/null +++ b/examples/region_sharding/scripts/k3s-up.sh @@ -0,0 +1,60 @@ +#!/bin/bash + +# Copyright 2019 The Vitess Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# This is an example script that creates a Kubernetes api for topo use by running k3s + +set -e +cell=${CELL:-'test'} + +script_root=$(dirname "${BASH_SOURCE[0]}") + +# shellcheck source=./env.sh +# shellcheck disable=SC1091 +source ./env.sh + +case $(uname) in + Linux) ;; + *) echo "WARNING: unsupported platform. K3s only supports running on Linux, the k8s topology is available for local examples."; exit 1;; +esac + +case $(uname -m) in + aarch64) ;; + x86_64) ;; + *) echo "ERROR: unsupported architecture, the k8s topology is not available for local examples."; exit 1;; +esac + +k3s server --disable-agent --data-dir "${VTDATAROOT}/k3s/" --https-listen-port "${K8S_PORT}" --write-kubeconfig "${K8S_KUBECONFIG}" > "${VTDATAROOT}"/tmp/k3s.out 2>&1 & +PID=$! +echo $PID > "${VTDATAROOT}/tmp/k3s.pid" +disown -a +echo "Waiting for k3s server to start" +sleep 15 + +# Use k3s built-in kubectl with custom config +KUBECTL="k3s kubectl --kubeconfig=${K8S_KUBECONFIG}" + +# Create the CRD for vitesstopologynodes +$KUBECTL create -f ../../go/vt/topo/k8stopo/VitessTopoNodes-crd.yaml + +# Add the CellInfo description for the cell +set +e +echo "add $cell CellInfo" +vtctl $TOPOLOGY_FLAGS AddCellInfo \ + -root /vitess/$cell \ + $cell +set -e + +echo "k3s start done..." diff --git a/examples/region_sharding/scripts/mysqlctl-down.sh b/examples/region_sharding/scripts/mysqlctl-down.sh new file mode 100755 index 00000000000..9e5491d1d4c --- /dev/null +++ b/examples/region_sharding/scripts/mysqlctl-down.sh @@ -0,0 +1,23 @@ +#!/bin/bash + +# Copyright 2019 The Vitess Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# This is an example script that stops the mysqld and vttablet instances +# created by vttablet-up.sh + +source ./env.sh + +mysqlctl -tablet_uid $TABLET_UID shutdown + diff --git a/examples/region_sharding/scripts/mysqlctl-up.sh b/examples/region_sharding/scripts/mysqlctl-up.sh new file mode 100755 index 00000000000..e4c9f58f819 --- /dev/null +++ b/examples/region_sharding/scripts/mysqlctl-up.sh @@ -0,0 +1,42 @@ +#!/bin/bash + +# Copyright 2019 The Vitess Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# This is an example script that creates a single shard vttablet deployment. + +source ./env.sh + +cell=${CELL:-'test'} +uid=$TABLET_UID +mysql_port=$[17000 + $uid] +printf -v alias '%s-%010d' $cell $uid +printf -v tablet_dir 'vt_%010d' $uid + +mkdir -p $VTDATAROOT/backups + +echo "Starting MySQL for tablet $alias..." +action="init" + +if [ -d $VTDATAROOT/$tablet_dir ]; then + echo "Resuming from existing vttablet dir:" + echo " $VTDATAROOT/$tablet_dir" + action='start' +fi + +mysqlctl \ + -log_dir $VTDATAROOT/tmp \ + -tablet_uid $uid \ + -mysql_port $mysql_port \ + $action diff --git a/examples/region_sharding/scripts/vtctld-down.sh b/examples/region_sharding/scripts/vtctld-down.sh new file mode 100755 index 00000000000..d96fa3b927f --- /dev/null +++ b/examples/region_sharding/scripts/vtctld-down.sh @@ -0,0 +1,22 @@ +#!/bin/bash + +# Copyright 2019 The Vitess Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# This is an example script that stops vtctld. + +source ./env.sh + +echo "Stopping vtctld..." +kill -9 `cat $VTDATAROOT/tmp/vtctld.pid` diff --git a/examples/region_sharding/scripts/vtctld-up.sh b/examples/region_sharding/scripts/vtctld-up.sh new file mode 100755 index 00000000000..662a234ae48 --- /dev/null +++ b/examples/region_sharding/scripts/vtctld-up.sh @@ -0,0 +1,38 @@ +#!/bin/bash + +# Copyright 2019 The Vitess Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# This is an example script that starts vtctld. + +source ./env.sh + +cell=${CELL:-'test'} +grpc_port=15999 + +echo "Starting vtctld..." +# shellcheck disable=SC2086 +vtctld \ + $TOPOLOGY_FLAGS \ + -cell $cell \ + -workflow_manager_init \ + -workflow_manager_use_election \ + -service_map 'grpc-vtctl' \ + -backup_storage_implementation file \ + -file_backup_storage_root $VTDATAROOT/backups \ + -log_dir $VTDATAROOT/tmp \ + -port $vtctld_web_port \ + -grpc_port $grpc_port \ + -pid_file $VTDATAROOT/tmp/vtctld.pid \ + > $VTDATAROOT/tmp/vtctld.out 2>&1 & diff --git a/examples/region_sharding/scripts/vtgate-down.sh b/examples/region_sharding/scripts/vtgate-down.sh new file mode 100755 index 00000000000..9da0a7179df --- /dev/null +++ b/examples/region_sharding/scripts/vtgate-down.sh @@ -0,0 +1,23 @@ +#!/bin/bash + +# Copyright 2019 The Vitess Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# This is an example script that stops the instance started by vtgate-up.sh. + +source ./env.sh + +# Stop vtgate. +echo "Stopping vtgate..." +kill `cat $VTDATAROOT/tmp/vtgate.pid` diff --git a/examples/region_sharding/scripts/vtgate-up.sh b/examples/region_sharding/scripts/vtgate-up.sh new file mode 100755 index 00000000000..e3fe2d17f82 --- /dev/null +++ b/examples/region_sharding/scripts/vtgate-up.sh @@ -0,0 +1,58 @@ +#!/bin/bash + +# Copyright 2019 The Vitess Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# This is an example script that starts a single vtgate. + +source ./env.sh + +cell=${CELL:-'test'} +web_port=15001 +grpc_port=15991 +mysql_server_port=15306 +mysql_server_socket_path="/tmp/mysql.sock" + +# Start vtgate. +# shellcheck disable=SC2086 +vtgate \ + $TOPOLOGY_FLAGS \ + -log_dir $VTDATAROOT/tmp \ + -log_queries_to_file $VTDATAROOT/tmp/vtgate_querylog.txt \ + -port $web_port \ + -grpc_port $grpc_port \ + -mysql_server_port $mysql_server_port \ + -mysql_server_socket_path $mysql_server_socket_path \ + -cell $cell \ + -cells_to_watch $cell \ + -tablet_types_to_wait MASTER,REPLICA \ + -gateway_implementation discoverygateway \ + -service_map 'grpc-vtgateservice' \ + -pid_file $VTDATAROOT/tmp/vtgate.pid \ + -mysql_auth_server_impl none \ + > $VTDATAROOT/tmp/vtgate.out 2>&1 & + +# Block waiting for vtgate to be listening +# Not the same as healthy + +echo "Waiting for vtgate to be up..." +while true; do + curl -I "http://$hostname:$web_port/debug/status" >/dev/null 2>&1 && break + sleep 0.1 +done; +echo "vtgate is up!" + +echo "Access vtgate at http://$hostname:$web_port/debug/status" + +disown -a diff --git a/examples/region_sharding/scripts/vttablet-down.sh b/examples/region_sharding/scripts/vttablet-down.sh new file mode 100755 index 00000000000..47b881b9793 --- /dev/null +++ b/examples/region_sharding/scripts/vttablet-down.sh @@ -0,0 +1,30 @@ +#!/bin/bash + +# Copyright 2019 The Vitess Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# This is an example script that stops the mysqld and vttablet instances +# created by vttablet-up.sh + +source ./env.sh + +printf -v tablet_dir 'vt_%010d' $TABLET_UID +pid=`cat $VTDATAROOT/$tablet_dir/vttablet.pid` + +kill $pid + +# Wait for vttablet to die. +while ps -p $pid > /dev/null; do sleep 1; done + + diff --git a/examples/region_sharding/scripts/vttablet-up.sh b/examples/region_sharding/scripts/vttablet-up.sh new file mode 100755 index 00000000000..ad99b998ee0 --- /dev/null +++ b/examples/region_sharding/scripts/vttablet-up.sh @@ -0,0 +1,68 @@ +#!/bin/bash + +# Copyright 2019 The Vitess Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +source ./env.sh + +cell=${CELL:-'test'} +keyspace=${KEYSPACE:-'test_keyspace'} +shard=${SHARD:-'0'} +uid=$TABLET_UID +mysql_port=$[17000 + $uid] +port=$[15000 + $uid] +grpc_port=$[16000 + $uid] +printf -v alias '%s-%010d' $cell $uid +printf -v tablet_dir 'vt_%010d' $uid +tablet_hostname='' +printf -v tablet_logfile 'vttablet_%010d_querylog.txt' $uid + +tablet_type=replica +if [[ "${uid: -1}" -gt 1 ]]; then + tablet_type=rdonly +fi + +echo "Starting vttablet for $alias..." +# shellcheck disable=SC2086 +vttablet \ + $TOPOLOGY_FLAGS \ + -log_dir $VTDATAROOT/tmp \ + -log_queries_to_file $VTDATAROOT/tmp/$tablet_logfile \ + -tablet-path $alias \ + -tablet_hostname "$tablet_hostname" \ + -init_keyspace $keyspace \ + -init_shard $shard \ + -init_tablet_type $tablet_type \ + -health_check_interval 5s \ + -enable_replication_reporter \ + -backup_storage_implementation file \ + -file_backup_storage_root $VTDATAROOT/backups \ + -restore_from_backup \ + -port $port \ + -grpc_port $grpc_port \ + -service_map 'grpc-queryservice,grpc-tabletmanager,grpc-updatestream' \ + -pid_file $VTDATAROOT/$tablet_dir/vttablet.pid \ + -vtctld_addr http://$hostname:$vtctld_web_port/ \ + > $VTDATAROOT/$tablet_dir/vttablet.out 2>&1 & + +# Block waiting for the tablet to be listening +# Not the same as healthy + +for i in $(seq 0 300); do + curl -I "http://$hostname:$port/debug/status" >/dev/null 2>&1 && break + sleep 0.1 +done + +# check one last time +curl -I "http://$hostname:$port/debug/status" || fail "tablet could not be started!" diff --git a/examples/region_sharding/scripts/zk-down.sh b/examples/region_sharding/scripts/zk-down.sh new file mode 100755 index 00000000000..18dd7933bc9 --- /dev/null +++ b/examples/region_sharding/scripts/zk-down.sh @@ -0,0 +1,26 @@ +#!/bin/bash + +# Copyright 2019 The Vitess Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# This is an example script that stops the ZooKeeper servers started by zk-up.sh. + +source ./env.sh + +# Stop ZooKeeper servers. +echo "Stopping zk servers..." +for zkid in $zkids; do + zkctl -zk.myid $zkid -zk.cfg $zkcfg -log_dir $VTDATAROOT/tmp shutdown +done + diff --git a/examples/region_sharding/scripts/zk-up.sh b/examples/region_sharding/scripts/zk-up.sh new file mode 100755 index 00000000000..6671d1063a6 --- /dev/null +++ b/examples/region_sharding/scripts/zk-up.sh @@ -0,0 +1,62 @@ +#!/bin/bash + +# Copyright 2019 The Vitess Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# This is an example script that creates a quorum of ZooKeeper servers. + +source ./env.sh + +cell=${CELL:-'test'} + +# Start ZooKeeper servers. +# The "zkctl init" command won't return until the server is able to contact its +# peers, so we need to start them all in the background and then wait for them. +echo "Starting zk servers..." +for zkid in $zkids; do + action='init' + printf -v zkdir 'zk_%03d' $zkid + if [ -f $VTDATAROOT/$zkdir/myid ]; then + echo "Resuming from existing ZK data dir:" + echo " $VTDATAROOT/$zkdir" + action='start' + fi + zkctl -zk.myid $zkid -zk.cfg $zkcfg -log_dir $VTDATAROOT/tmp $action \ + > $VTDATAROOT/tmp/zkctl_$zkid.out 2>&1 & + pids[$zkid]=$! +done + +# Wait for all the zkctl commands to return. +echo "Waiting for zk servers to be ready..." + +for zkid in $zkids; do + if ! wait ${pids[$zkid]}; then + echo "ZK server number $zkid failed to start. See log:" + echo " $VTDATAROOT/tmp/zkctl_$zkid.out" + fi +done + +echo "Started zk servers." + +# Add the CellInfo description for the $CELL cell. +# If the node already exists, it's fine, means we used existing data. +set +e +# shellcheck disable=SC2086 +vtctl $TOPOLOGY_FLAGS AddCellInfo \ + -root /vitess/$cell \ + -server_address $ZK_SERVER \ + $cell +set -e + +echo "Configured zk servers." diff --git a/examples/region_sharding/show_data.sql b/examples/region_sharding/show_data.sql new file mode 100644 index 00000000000..5928a6c6070 --- /dev/null +++ b/examples/region_sharding/show_data.sql @@ -0,0 +1,15 @@ +use main/-40; +select * from customer; +select id, hex(keyspace_id) from customer_lookup; +use main/40-80; +select * from customer; +select id, hex(keyspace_id) from customer_lookup; +use main/80-c0; +select * from customer; +select id, hex(keyspace_id) from customer_lookup; +use main/c0-; +select * from customer; +select id, hex(keyspace_id) from customer_lookup; +use main; +select count(*) from customer; +select count(*) from customer_lookup; diff --git a/examples/region_sharding/topo-etcd2.sh b/examples/region_sharding/topo-etcd2.sh new file mode 100644 index 00000000000..c61543a806c --- /dev/null +++ b/examples/region_sharding/topo-etcd2.sh @@ -0,0 +1,21 @@ +#!/bin/bash + +# Copyright 2019 The Vitess Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# This is an example script that creates a single shard vttablet deployment. + +export TOPO='etcd2' + + diff --git a/examples/region_sharding/topo-k8s.sh b/examples/region_sharding/topo-k8s.sh new file mode 100644 index 00000000000..92e14ba4d69 --- /dev/null +++ b/examples/region_sharding/topo-k8s.sh @@ -0,0 +1,21 @@ +#!/bin/bash + +# Copyright 2019 The Vitess Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# This is an example script that creates a single shard vttablet deployment. + +export TOPO='k8s' + + diff --git a/examples/region_sharding/topo-zk2.sh b/examples/region_sharding/topo-zk2.sh new file mode 100644 index 00000000000..29380949d8f --- /dev/null +++ b/examples/region_sharding/topo-zk2.sh @@ -0,0 +1,21 @@ +#!/bin/bash + +# Copyright 2019 The Vitess Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# This is an example script that creates a single shard vttablet deployment. + +export TOPO='zk2' + +