Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions examples/region_sharding/101_initial_cluster.sh
Original file line number Diff line number Diff line change
@@ -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
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Q: we are keeping all shards in the same cell. Will it make the example better to create multiple cells and put each shard in a different cell?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. I think it will be important to showcase multi-cell.

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
59 changes: 59 additions & 0 deletions examples/region_sharding/201_teardown.sh
Original file line number Diff line number Diff line change
@@ -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
22 changes: 22 additions & 0 deletions examples/region_sharding/README.md
Original file line number Diff line number Diff line change
@@ -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
```
10 changes: 10 additions & 0 deletions examples/region_sharding/countries.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"United States": 1,
"Canada": 2,
"France": 64,
"Germany": 65,
"China": 128,
"Japan": 129,
"India": 192,
"Indonesia": 193
}
12 changes: 12 additions & 0 deletions examples/region_sharding/create_main_schema.sql
Original file line number Diff line number Diff line change
@@ -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)
);
82 changes: 82 additions & 0 deletions examples/region_sharding/env.sh
Original file line number Diff line number Diff line change
@@ -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

16 changes: 16 additions & 0 deletions examples/region_sharding/insert_customers.sql
Original file line number Diff line number Diff line change
@@ -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');
46 changes: 46 additions & 0 deletions examples/region_sharding/main_vschema.json
Original file line number Diff line number Diff line change
@@ -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"
}
]
}
}
}
22 changes: 22 additions & 0 deletions examples/region_sharding/scripts/etcd-down.sh
Original file line number Diff line number Diff line change
@@ -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`
51 changes: 51 additions & 0 deletions examples/region_sharding/scripts/etcd-up.sh
Original file line number Diff line number Diff line change
@@ -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..."


Loading