github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/tests/integration_tests/ddl_reentrant/run.sh (about)

     1  #!/bin/bash
     2  
     3  set -eu
     4  
     5  CUR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
     6  source $CUR/../_utils/test_prepare
     7  WORK_DIR=$OUT_DIR/$TEST_NAME
     8  CDC_BINARY=cdc.test
     9  SINK_TYPE=$1
    10  
    11  # cdc parse and restore ddl with flags format.RestoreStringSingleQuotes|format.RestoreNameBackQuotes|format.RestoreKeyWordUppercase|format.RestoreTiDBSpecialComment
    12  ddls=("create database ddl_reentrant" false 'CREATE DATABASE `ddl_reentrant`'
    13  	"create table ddl_reentrant.t3 (a int primary key, b int) partition by range(a) (partition p0 values less than (1000), partition p1 values less than (2000))" false 'CREATE TABLE `ddl_reentrant`.`t3` (`a` INT PRIMARY KEY,`b` INT) PARTITION BY RANGE (`a`) (PARTITION `p0` VALUES LESS THAN (1000),PARTITION `p1` VALUES LESS THAN (2000))'
    14  	"create table ddl_reentrant.t4 (a int primary key, b int)" false 'CREATE TABLE `ddl_reentrant`.`t4` (`a` INT PRIMARY KEY,`b` INT)'
    15  	"alter table ddl_reentrant.t3 exchange partition p0 with table ddl_reentrant.t4" true 'ALTER TABLE `ddl_reentrant`.`t3` EXCHANGE PARTITION `p0` WITH TABLE `ddl_reentrant`.`t4`'
    16  )
    17  
    18  function complete_ddls() {
    19  	# TODO: refine the release detection after 5.0 tag of TiDB is ready
    20  	if [[ ! $tidb_build_branch =~ master ]]; then
    21  		echo "skip some DDLs in tidb v4.0.x"
    22  	else
    23  		echo "complete all DDLs in master"
    24  		# DDLs that are supportted since 5.0
    25  		# ddls+=("alter table ddl_reentrant.t2 add column c1 int, add column c2 int, add column c3 int" false 'ALTER TABLE `ddl_reentrant`.`t2` ADD COLUMN `c1` INT, ADD COLUMN `c2` INT, ADD COLUMN `c3` INT')
    26  		# ddls+=("alter table ddl_reentrant.t2 drop column c1, drop column c2, drop column c3" false 'ALTER TABLE `ddl_reentrant`.`t2` DROP COLUMN `c1`, DROP COLUMN `c2`, DROP COLUMN `c3`')
    27  		# ddls+=("rename table ddl_reentrant.t2 to ddl_reentrant.tt2, ddl_reentrant.t3 to ddl_reentrant.tt3" false 'RENAME TABLE `ddl_reentrant`.`t2` TO `ddl_reentrant`.`tt2`, `ddl_reentrant`.`t3` TO `ddl_reentrant`.`tt3`')
    28  		# ddls+=("rename table ddl_reentrant.tt2 to ddl_reentrant.t2, ddl_reentrant.tt3 to ddl_reentrant.t3" false 'RENAME TABLE `ddl_reentrant`.`tt2` TO `ddl_reentrant`.`t2`, `ddl_reentrant`.`tt3` TO `ddl_reentrant`.`t3`')
    29  	fi
    30  	# ddls+=("alter table ddl_reentrant.t2 drop primary key" false 'ALTER TABLE `ddl_reentrant`.`t2` DROP PRIMARY KEY')
    31  	# ddls+=("alter table ddl_reentrant.t2 add primary key pk(id)" false 'ALTER TABLE `ddl_reentrant`.`t2` ADD PRIMARY KEY `pk`(`id`)')
    32  	# ddls+=("drop table ddl_reentrant.t2" false 'DROP TABLE `ddl_reentrant`.`t2`')
    33  	# ddls+=("recover table ddl_reentrant.t2" false 'RECOVER TABLE `ddl_reentrant`.`t2`')
    34  	# ddls+=("drop database ddl_reentrant" false 'DROP DATABASE `ddl_reentrant`')
    35  }
    36  
    37  changefeedid=""
    38  # this test contains `recover table`, which requires super privilege, so we
    39  # can't use the normal user
    40  SINK_URI="mysql://root@127.0.0.1:3306/"
    41  
    42  function check_ts_forward() {
    43  	changefeedid=$1
    44  	rts1=$(cdc cli changefeed query --changefeed-id=${changefeedid} 2>&1 | jq '.resolved_ts')
    45  	checkpoint1=$(cdc cli changefeed query --changefeed-id=${changefeedid} 2>&1 | jq '.checkpoint_tso')
    46  	sleep 1
    47  	rts2=$(cdc cli changefeed query --changefeed-id=${changefeedid} 2>&1 | jq '.resolved_ts')
    48  	checkpoint2=$(cdc cli changefeed query --changefeed-id=${changefeedid} 2>&1 | jq '.checkpoint_tso')
    49  	if [[ "$rts1" != "null" ]] && [[ "$rts1" != "0" ]]; then
    50  		if [[ "$rts1" -ne "$rts2" ]] || [[ "$checkpoint1" -ne "$checkpoint2" ]]; then
    51  			echo "changefeed is working normally rts: ${rts1}->${rts2} checkpoint: ${checkpoint1}->${checkpoint2}"
    52  			return
    53  		fi
    54  	fi
    55  	exit 1
    56  }
    57  
    58  function check_ddl_executed() {
    59  	log_file="$1"
    60  	ddl=$(cat $2)
    61  	success="$3"
    62  	if [[ $success == "true" ]]; then
    63  		key_word="Exec DDL succeeded"
    64  	else
    65  		key_word="Execute DDL failed, but error can be ignored"
    66  	fi
    67  	log=$(grep "${key_word}" ${log_file} | tail -n 1)
    68  	if [[ $log == *"${ddl}"* ]]; then
    69  		echo $log
    70  		return
    71  	else
    72  		exit 1
    73  	fi
    74  }
    75  
    76  export -f check_ts_forward
    77  export -f check_ddl_executed
    78  
    79  tidb_build_branch=$(mysql -uroot -h${UP_TIDB_HOST} -P${UP_TIDB_PORT} -e \
    80  	"select tidb_version()\G" | grep "Git Branch" | awk -F: '{print $(NF)}' | tr -d " ")
    81  
    82  function ddl_test() {
    83  	ddl=$1
    84  	is_reentrant=$2
    85  	restored_sql=$3
    86  
    87  	echo "------------------------------------------"
    88  	echo "test ddl $ddl, is_reentrant: $is_reentrant restored_sql: $restored_sql"
    89  
    90  	run_sql $ddl ${UP_TIDB_HOST} ${UP_TIDB_PORT}
    91  	ensure 10 check_ts_forward $changefeedid
    92  
    93  	echo $restored_sql >${WORK_DIR}/ddl_temp.sql
    94  	ensure 10 check_ddl_executed "${WORK_DIR}/cdc.log" "${WORK_DIR}/ddl_temp.sql" true
    95  	ddl_finished_ts=$(grep "Execute DDL succeeded" ${WORK_DIR}/cdc.log | tail -n 1 | grep -oE '"CommitTs\\":[0-9]{18}' | awk -F: '{print $(NF)}')
    96  	cdc cli changefeed pause --changefeed-id=${changefeedid}
    97  	cdc cli changefeed resume --no-confirm --changefeed-id=${changefeedid} --overwrite-checkpoint-ts=${ddl_finished_ts}
    98  	echo "resume changefeed ${changefeedid} from ${ddl_finished_ts}"
    99  	ensure 10 check_ts_forward $changefeedid
   100  	ensure 1000000000000000 check_ddl_executed "${WORK_DIR}/cdc.log" "${WORK_DIR}/ddl_temp.sql" $is_reentrant
   101  }
   102  
   103  function run() {
   104  	# No need to test kafka and storage sink.
   105  	if [ "$SINK_TYPE" != "mysql" ]; then
   106  		return
   107  	fi
   108  
   109  	rm -rf $WORK_DIR && mkdir -p $WORK_DIR
   110  
   111  	start_tidb_cluster --workdir $WORK_DIR --tidb-config $CUR/conf/tidb_config.toml
   112  
   113  	complete_ddls
   114  	# TODO: refine the release detection after 5.0 tag of TiDB is ready
   115  	if [[ $tidb_build_branch =~ master ]]; then
   116  		# https://github.com/pingcap/tidb/pull/21533 disables multi_schema change
   117  		# feature by default, turn it on first
   118  		run_sql "set @@global.tidb_enable_exchange_partition=on" ${UP_TIDB_HOST} ${UP_TIDB_PORT}
   119  		run_sql "set global tidb_enable_change_multi_schema = on" ${UP_TIDB_HOST} ${UP_TIDB_PORT}
   120  		# This must be set before cdc server starts
   121  		run_sql "set @@global.tidb_enable_exchange_partition=on" ${DOWN_TIDB_HOST} ${DOWN_TIDB_PORT}
   122  		run_sql "set global tidb_enable_change_multi_schema = on" ${DOWN_TIDB_HOST} ${DOWN_TIDB_PORT}
   123  		# TiDB global variables cache 2 seconds at most
   124  		sleep 2
   125  	fi
   126  
   127  	cd $WORK_DIR
   128  
   129  	run_cdc_server --workdir $WORK_DIR --binary $CDC_BINARY
   130  	changefeedid=$(cdc cli changefeed create --sink-uri="$SINK_URI" 2>&1 | tail -n2 | head -n1 | awk '{print $2}')
   131  
   132  	OLDIFS=$IFS
   133  	IFS=""
   134  	idx=0
   135  	while [ $idx -lt ${#ddls[*]} ]; do
   136  		ddl=${ddls[$idx]}
   137  		idx=$((idx + 1))
   138  		idxs_reentrant=${ddls[$idx]}
   139  		idx=$((idx + 1))
   140  		restored_sql=${ddls[$idx]}
   141  		idx=$((idx + 1))
   142  		ddl_test $ddl $idxs_reentrant $restored_sql
   143  	done
   144  	IFS=$OLDIFS
   145  
   146  	cleanup_process $CDC_BINARY
   147  }
   148  
   149  trap stop_tidb_cluster EXIT
   150  run $*
   151  check_logs $WORK_DIR
   152  echo "[$(date)] <<<<<< run test case $TEST_NAME success! >>>>>>"