Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests: add random region merge test (#1738) #1746

Merged
7 changes: 7 additions & 0 deletions tests/_utils/start_tidb_cluster
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ set -e

OUT_DIR=
tidb_config=
pd_config=
retry_times=3
multiple_upstream_pd="false"

Expand All @@ -21,6 +22,10 @@ while [[ ${1} ]]; do
tidb_config=${2}
shift
;;
--pd-config)
pd_config=${2}
shift
;;
--retry)
retry_times=${2}
shift
Expand Down Expand Up @@ -51,6 +56,8 @@ do

if [[ "$tidb_config" != "" ]]; then
start_tidb_cluster_impl --workdir ${OUT_DIR} --multiple-upstream-pd ${multiple_upstream_pd} --tidb-config ${tidb_config}
elif [[ "$pd_config" != "" ]]; then
start_tidb_cluster_impl --workdir ${OUT_DIR} --multiple-upstream-pd ${multiple_upstream_pd} --pd-config ${pd_config}
else
start_tidb_cluster_impl --workdir ${OUT_DIR} --multiple-upstream-pd ${multiple_upstream_pd}
fi
Expand Down
13 changes: 12 additions & 1 deletion tests/_utils/start_tidb_cluster_impl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ set -e

OUT_DIR=
tidb_config=
pd_config=
multiple_upstream_pd=

while [[ ${1} ]]; do
Expand All @@ -20,6 +21,10 @@ while [[ ${1} ]]; do
tidb_config=${2}
shift
;;
--pd-config)
pd_config=${2}
shift
;;
--multiple-upstream-pd)
multiple_upstream_pd=${2}
shift
Expand All @@ -42,12 +47,18 @@ stop_tidb_cluster

cd $OUT_DIR && echo "start tidb cluster in $OUT_DIR"

cat - >"$OUT_DIR/pd-config.toml" <<EOF

# pd server config file
if [[ "$pd_config" != "" ]]; then
cat $pd_config > $OUT_DIR/pd-config.toml
else
cat - >"$OUT_DIR/pd-config.toml" <<EOF
[replication]
# Set it to 1 to make sure we have enough replicas to run placement-rules.
max-replicas = 1
enable-placement-rules = true
EOF
fi

echo "Starting Upstream PD..."
if [[ "$multiple_upstream_pd" == "true" ]]; then
Expand Down
27 changes: 27 additions & 0 deletions tests/region_merge/conf/diff_config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# diff Configuration.

log-level = "info"
chunk-size = 10
check-thread-count = 4
sample-percent = 100
use-rowid = false
use-checksum = true
fix-sql-file = "fix.sql"

# tables need to check.
[[check-tables]]
schema = "region_merge"
tables = ["~.*"]

[[source-db]]
host = "127.0.0.1"
port = 4000
user = "root"
password = ""
instance-id = "source-1"

[target-db]
host = "127.0.0.1"
port = 3306
user = "root"
password = ""
18 changes: 18 additions & 0 deletions tests/region_merge/conf/pd_config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[replication]
# Set it to 1 to make sure we have enough replicas to run placement-rules.
max-replicas = 1
enable-placement-rules = true

[schedule]
# decrease the split-merge-interval to trigger fast region merge
split-merge-interval = "3s"
merge-schedule-limit = 32

[[schedule.schedulers]]
type = "random-merge"

[[schedule.schedulers]]
type = "balance-region"

[[schedule.schedulers]]
type = "balance-leader"
67 changes: 67 additions & 0 deletions tests/region_merge/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#!/bin/bash

set -e

CUR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
source $CUR/../_utils/test_prepare
WORK_DIR=$OUT_DIR/$TEST_NAME
CDC_BINARY=cdc.test
SINK_TYPE=$1

function split_and_random_merge() {
pd_addr=$1
scale=$2
echo "split_and_random_merge scale: $scale"
run_sql "SPLIT TABLE region_merge.t1 BETWEEN (-9223372036854775808) AND (9223372036854775807) REGIONS $scale;" ${UP_TIDB_HOST} ${UP_TIDB_PORT} || true
run_sql "SELECT count(distinct region_id) from information_schema.tikv_region_status where db_name = 'region_merge' and table_name = 't1';" \
&& cat $OUT_DIR/sql_res.region_merge.txt
run_sql "insert into region_merge.t1 values (-9223372036854775808),(0),(1),(9223372036854775807);" ${UP_TIDB_HOST} ${UP_TIDB_PORT}
run_sql "delete from region_merge.t1;" ${UP_TIDB_HOST} ${UP_TIDB_PORT}
# sleep 5s to wait some region merge
sleep 5
}

large_scale=(100 200 400 800 1600 3200 6400 12800 25600 51200)
small_scale=(100 200 400)
# in CI, we use the small data set
test_scale=( "${small_scale[@]}" )

function run() {
rm -rf $WORK_DIR && mkdir -p $WORK_DIR

start_tidb_cluster --workdir $WORK_DIR --multiple-upstream-pd true --pd-config $CUR/conf/pd_config.toml

cd $WORK_DIR

pd_addr="http://$UP_PD_HOST_1:$UP_PD_PORT_1"
run_cdc_server --workdir $WORK_DIR --binary $CDC_BINARY
TOPIC_NAME="ticdc-region-merge-test-$RANDOM"
case $SINK_TYPE in
kafka) SINK_URI="kafka://127.0.0.1:9092/$TOPIC_NAME?partition-num=4&kafka-version=${KAFKA_VERSION}";;
*) SINK_URI="mysql://root@127.0.0.1:3306/";;
esac
run_cdc_cli changefeed create --sink-uri="$SINK_URI"
if [ "$SINK_TYPE" == "kafka" ]; then
run_kafka_consumer $WORK_DIR "kafka://127.0.0.1:9092/$TOPIC_NAME?partition-num=4&version=${KAFKA_VERSION}"
fi

# set max_execution_time to 15s, because split region could block even region has been split.
run_sql "SET @@global.MAX_EXECUTION_TIME = 15000;" ${UP_TIDB_HOST} ${UP_TIDB_PORT}
run_sql "CREATE DATABASE region_merge;" ${UP_TIDB_HOST} ${UP_TIDB_PORT}
run_sql "CREATE TABLE region_merge.t1 (id bigint primary key);" ${UP_TIDB_HOST} ${UP_TIDB_PORT}

for scale in "${test_scale[@]}"; do
split_and_random_merge $pd_addr $scale
done

run_sql "insert into region_merge.t1 values (-9223372036854775808),(0),(1),(9223372036854775807);" ${UP_TIDB_HOST} ${UP_TIDB_PORT}
check_table_exists region_merge.t1 ${DOWN_TIDB_HOST} ${DOWN_TIDB_PORT}
check_sync_diff $WORK_DIR $CUR/conf/diff_config.toml

cleanup_process $CDC_BINARY
}

trap stop_tidb_cluster EXIT
run $*
check_logs $WORK_DIR
echo "[$(date)] <<<<<< run test case $TEST_NAME success! >>>>>>"