Skip to content

Commit 601d1f9

Browse files
committed
add toxiproxy option for localtests (#1591)
1 parent 2f0f85b commit 601d1f9

File tree

4 files changed

+86
-14
lines changed

4 files changed

+86
-14
lines changed

doc/local-tests.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,11 @@ TEST_MYSQL_IMAGE="mysql-server:8.0.16" ./script/docker-gh-ost-replica-tests up
3434
# cleanup containers
3535
./script/docker-gh-ost-replica-tests down
3636
```
37+
38+
Pass the `-t` flag to run the tests with a toxiproxy between gh-ost and the MySQL replica. This simulates network conditions where MySQL connections are closed unexpectedly.
39+
40+
```shell
41+
# run tests with toxiproxy
42+
./script/docker-gh-ost-replica-tests up -t
43+
./script/docker-gh-ost-replica-tests run -t
44+
```

go/cmd/gh-ost/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ func main() {
143143
flag.BoolVar(&migrationContext.IncludeTriggers, "include-triggers", false, "When true, the triggers (if exist) will be created on the new table")
144144
flag.StringVar(&migrationContext.TriggerSuffix, "trigger-suffix", "", "Add a suffix to the trigger name (i.e '_v2'). Requires '--include-triggers'")
145145
flag.BoolVar(&migrationContext.RemoveTriggerSuffix, "remove-trigger-suffix-if-exists", false, "Remove given suffix from name of trigger. Requires '--include-triggers' and '--trigger-suffix'")
146+
flag.BoolVar(&migrationContext.SkipPortValidation, "skip-port-validation", false, "Skip port validation for MySQL connections")
146147

147148
maxLoad := flag.String("max-load", "", "Comma delimited status-name=threshold. e.g: 'Threads_running=100,Threads_connected=500'. When status exceeds threshold, app throttles writes")
148149
criticalLoad := flag.String("critical-load", "", "Comma delimited status-name=threshold, same format as --max-load. When status exceeds threshold, app panics and quits")

localtests/test.sh

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ test_logfile=/tmp/gh-ost-test.log
1212
default_ghost_binary=/tmp/gh-ost-test
1313
ghost_binary=""
1414
docker=false
15+
toxiproxy=false
1516
storage_engine=innodb
1617
exec_command_file=/tmp/gh-ost-test.bash
1718
ghost_structure_output_file=/tmp/gh-ost-test.ghost.structure.sql
@@ -29,14 +30,17 @@ original_sql_mode=
2930
sysbench_pid=
3031

3132
OPTIND=1
32-
while getopts "b:s:d" OPTION; do
33+
while getopts "b:s:dt" OPTION; do
3334
case $OPTION in
3435
b)
3536
ghost_binary="$OPTARG"
3637
;;
3738
s)
3839
storage_engine="$OPTARG"
3940
;;
41+
t)
42+
toxiproxy=true
43+
;;
4044
d)
4145
docker=true
4246
;;
@@ -75,6 +79,20 @@ verify_master_and_replica() {
7579
read replica_host replica_port <<<$(gh-ost-test-mysql-replica -e "select @@hostname, @@port" -ss)
7680
[ "$replica_host" == "$(hostname)" ] && replica_host="127.0.0.1"
7781
echo "# replica verified at $replica_host:$replica_port"
82+
83+
if [ "$docker" = true ]; then
84+
master_host="0.0.0.0"
85+
master_port="3307"
86+
echo "# using docker master at $master_host:$master_port"
87+
replica_host="0.0.0.0"
88+
if [ "$toxiproxy" = true ]; then
89+
replica_port="23308"
90+
echo "# using toxiproxy replica at $replica_host:$replica_port"
91+
else
92+
replica_port="3308"
93+
echo "# using docker replica at $replica_host:$replica_port"
94+
fi
95+
fi
7896
}
7997

8098
exec_cmd() {
@@ -154,13 +172,6 @@ test_single() {
154172
local test_name
155173
test_name="$1"
156174

157-
if [ "$docker" = true ]; then
158-
master_host="0.0.0.0"
159-
master_port="3307"
160-
replica_host="0.0.0.0"
161-
replica_port="3308"
162-
fi
163-
164175
if [ -f $tests_path/$test_name/ignore_versions ]; then
165176
ignore_versions=$(cat $tests_path/$test_name/ignore_versions)
166177
mysql_version=$(gh-ost-test-mysql-master -s -s -e "select @@version")
@@ -199,6 +210,9 @@ test_single() {
199210
if [ -f $tests_path/$test_name/extra_args ]; then
200211
extra_args=$(cat $tests_path/$test_name/extra_args)
201212
fi
213+
if [ "$toxiproxy" = true ]; then
214+
extra_args+=" --skip-port-validation"
215+
fi
202216
orig_columns="*"
203217
ghost_columns="*"
204218
order_by=""

script/docker-gh-ost-replica-tests

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,15 @@
55
# Set the environment var TEST_MYSQL_IMAGE to change the docker image.
66
#
77
# Usage:
8-
# docker-gh-ost-replica-tests up start the containers
9-
# docker-gh-ost-replica-tests down remove the containers
10-
# docker-gh-ost-replica-tests run run replica tests on the containers
8+
# docker-gh-ost-replica-tests up [-t] start the containers
9+
# docker-gh-ost-replica-tests down remove the containers
10+
# docker-gh-ost-replica-tests run [-t] run replica tests on the containers
11+
#
12+
# Flags:
13+
# -t use a toxiproxy for replica connection to simulate dropped connections
1114

1215
set -e
16+
toxiproxy=false
1317

1418
GH_OST_ROOT=$(git rev-parse --show-toplevel)
1519
if [[ ":$PATH:" != *":$GH_OST_ROOT:"* ]]; then
@@ -47,6 +51,22 @@ mysql-replica() {
4751
fi
4852
}
4953

54+
create_toxiproxy() {
55+
curl --fail -X POST http://localhost:8474/proxies \
56+
-H "Content-Type: application/json" \
57+
-d '{"name": "mysql_proxy",
58+
"listen": "0.0.0.0:23308",
59+
"upstream": "host.docker.internal:3308"}'
60+
echo
61+
62+
curl --fail -X POST http://localhost:8474/proxies/mysql_proxy/toxics \
63+
-H "Content-Type: application/json" \
64+
-d '{"name": "limit_data_downstream",
65+
"type": "limit_data",
66+
"attributes": {"bytes": 300000}}'
67+
echo
68+
}
69+
5070
setup() {
5171
[ -z "$TEST_MYSQL_IMAGE" ] && TEST_MYSQL_IMAGE="mysql:8.0.41"
5272

@@ -59,6 +79,22 @@ setup() {
5979
MYSQL_SHA2_RSA_KEYS_FLAG="--caching-sha2-password-auto-generate-rsa-keys=ON"
6080
fi
6181
(TEST_MYSQL_IMAGE="$TEST_MYSQL_IMAGE" MYSQL_SHA2_RSA_KEYS_FLAG="$MYSQL_SHA2_RSA_KEYS_FLAG" envsubst <"$compose_file") >"$compose_file.tmp"
82+
83+
if [ "$toxiproxy" = true ]; then
84+
echo "Starting toxiproxy container..."
85+
cat <<EOF >>"$compose_file.tmp"
86+
mysql-toxiproxy:
87+
image: "ghcr.io/shopify/toxiproxy:latest"
88+
container_name: mysql-toxiproxy
89+
ports:
90+
- '8474:8474'
91+
- '23308:23308'
92+
expose:
93+
- '23308'
94+
- '8474'
95+
EOF
96+
fi
97+
6298
docker compose -f "$compose_file.tmp" up -d --wait
6399

64100
echo "Waiting for MySQL..."
@@ -80,23 +116,36 @@ setup() {
80116
mysql-replica -e "start slave;"
81117
fi
82118
echo "OK"
119+
120+
if [ "$toxiproxy" = true ]; then
121+
echo "Creating toxiproxy..."
122+
create_toxiproxy
123+
echo "OK"
124+
fi
83125
}
84126

85127
teardown() {
86128
echo "Stopping containers..."
87129
docker stop mysql-replica
88130
docker stop mysql-primary
131+
docker stop mysql-toxiproxy 2>/dev/null || true
89132
echo "Removing containers..."
90133
docker rm mysql-replica
91134
docker rm mysql-primary
135+
docker rm mysql-toxiproxy 2>/dev/null || true
92136
}
93137

94138
main() {
95-
if [[ "$1" == "up" ]]; then
139+
local cmd="$1"
140+
local tflag=
141+
if [[ "$2" == "-t" ]]; then
142+
toxiproxy=true
143+
fi
144+
if [[ "$cmd" == "up" ]]; then
96145
setup
97-
elif [[ "$1" == "down" ]]; then
146+
elif [[ "$cmd" == "down" ]]; then
98147
teardown
99-
elif [[ "$1" == "run" ]]; then
148+
elif [[ "$cmd" == "run" ]]; then
100149
shift 1
101150
"$GH_OST_ROOT/localtests/test.sh" -d "$@"
102151
fi

0 commit comments

Comments
 (0)