github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/dm/tests/start_task/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  
     8  WORK_DIR=$TEST_DIR/$TEST_NAME
     9  
    10  function prepare_data() {
    11  	run_sql 'DROP DATABASE if exists start_task;' $MYSQL_PORT1 $MYSQL_PASSWORD1
    12  	run_sql 'CREATE DATABASE start_task;' $MYSQL_PORT1 $MYSQL_PASSWORD1
    13  	run_sql "CREATE TABLE start_task.t$1(i TINYINT, j INT UNIQUE KEY);" $MYSQL_PORT1 $MYSQL_PASSWORD1
    14  	for j in $(seq 100); do
    15  		run_sql "INSERT INTO start_task.t$1 VALUES ($j,${j}000$j),($j,${j}001$j);" $MYSQL_PORT1 $MYSQL_PASSWORD1
    16  	done
    17  }
    18  
    19  function init_tracker_test() {
    20  	run_sql 'DROP DATABASE if exists start_task;' $MYSQL_PORT1 $MYSQL_PASSWORD1
    21  	run_sql 'CREATE DATABASE start_task;' $MYSQL_PORT1 $MYSQL_PASSWORD1
    22  	for j in $(seq 100); do
    23  		run_sql "CREATE TABLE start_task.t$j(i TINYINT, j INT UNIQUE KEY);" $MYSQL_PORT1 $MYSQL_PASSWORD1
    24  		run_sql "INSERT INTO start_task.t$j VALUES (1,10001),(1,10011);" $MYSQL_PORT1 $MYSQL_PASSWORD1
    25  	done
    26  
    27  	run_dm_master $WORK_DIR/master $MASTER_PORT $cur/conf/dm-master.toml
    28  	check_rpc_alive $cur/../bin/check_master_online 127.0.0.1:$MASTER_PORT
    29  	run_dm_worker $WORK_DIR/worker1 $WORKER1_PORT $cur/conf/dm-worker1.toml
    30  	check_rpc_alive $cur/../bin/check_worker_online 127.0.0.1:$WORKER1_PORT
    31  	# operate mysql config to worker
    32  	cp $cur/conf/source1.yaml $WORK_DIR/source1.yaml
    33  	sed -i "/relay-binlog-name/i\relay-dir: $WORK_DIR/worker1/relay_log" $WORK_DIR/source1.yaml
    34  	dmctl_operate_source create $WORK_DIR/source1.yaml $SOURCE_ID1
    35  
    36  	dmctl_start_task_standalone "$cur/conf/dm-task.yaml" "--remove-meta"
    37  
    38  	check_sync_diff $WORK_DIR $cur/conf/diff_config.toml
    39  
    40  	# only table 1-50 flush checkpoint
    41  	for j in $(seq 50); do
    42  		run_sql "INSERT INTO start_task.t$j VALUES (2,20002),(2,20022);" $MYSQL_PORT1 $MYSQL_PASSWORD1
    43  	done
    44  
    45  	check_sync_diff $WORK_DIR $cur/conf/diff_config.toml
    46  
    47  	run_dm_ctl $WORK_DIR "127.0.0.1:$MASTER_PORT" \
    48  		"stop-task test" \
    49  		"\"result\": true" 2
    50  	dmctl_start_task_standalone "$cur/conf/dm-task.yaml"
    51  
    52  	for j in $(seq 100); do
    53  		run_sql "INSERT INTO start_task.t$j VALUES (3,30003),(3,30033);" $MYSQL_PORT1 $MYSQL_PASSWORD1
    54  		run_sql "INSERT INTO start_task.t$j VALUES (4,40004),(4,40044);" $MYSQL_PORT1 $MYSQL_PASSWORD1
    55  	done
    56  	check_sync_diff $WORK_DIR $cur/conf/diff_config.toml 20
    57  
    58  	# now syncer will save all table structure from dump files at Init, so all tables
    59  	# should be loaded into schema tracker.
    60  	check_log_contains $WORK_DIR/worker1/log/dm-worker.log 'init table info.*t50' 1
    61  	check_log_contains $WORK_DIR/worker1/log/dm-worker.log 'init table info.*t51' 1
    62  
    63  	cleanup_process
    64  	cleanup_data start_task
    65  }
    66  
    67  function restore_timezone() {
    68  	run_sql_source1 "set global time_zone = SYSTEM"
    69  	run_sql_tidb "set global time_zone = SYSTEM"
    70  }
    71  
    72  function start_task_by_time() {
    73  	run_sql_source1 "set global time_zone = '+02:00'"
    74  	run_sql_source1 "SELECT cast(TIMEDIFF(NOW(6), UTC_TIMESTAMP(6)) as time) time"
    75  	check_contains "time: 02:00:00"
    76  	run_sql_tidb "set global time_zone = '+06:00'"
    77  	run_sql_tidb "SELECT cast(TIMEDIFF(NOW(6), UTC_TIMESTAMP(6)) as time) time"
    78  	check_contains "time: 06:00:00"
    79  	trap restore_timezone EXIT
    80  
    81  	export GO_FAILPOINTS="github.com/pingcap/tiflow/dm/syncer/SafeModeInitPhaseSeconds=return(\"10ms\")"
    82  	run_dm_master $WORK_DIR/master $MASTER_PORT $cur/conf/dm-master.toml
    83  	check_rpc_alive $cur/../bin/check_master_online 127.0.0.1:$MASTER_PORT
    84  	run_dm_worker $WORK_DIR/worker1 $WORKER1_PORT $cur/conf/dm-worker1.toml
    85  	check_rpc_alive $cur/../bin/check_worker_online 127.0.0.1:$WORKER1_PORT
    86  	dmctl_operate_source create $cur/conf/source1.yaml $SOURCE_ID1
    87  
    88  	run_sql_source1 'DROP DATABASE if exists start_task;'
    89  	run_sql_source1 'CREATE DATABASE start_task;'
    90  	run_sql_source1 'CREATE TABLE start_task.t1 (c INT PRIMARY KEY);'
    91  
    92  	sleep 2
    93  	start_time=$(TZ='UTC-2' date '+%Y-%m-%d %T') # TZ=UTC-2 means +02:00
    94  	sleep 2
    95  
    96  	run_sql_source1 'CREATE TABLE start_task.t2 (c INT PRIMARY KEY);'
    97  	run_sql_source1 'INSERT INTO start_task.t2 VALUES (1), (2);INSERT INTO start_task.t2 VALUES (3), (4);'
    98  
    99  	cp $cur/conf/dm-task.yaml $WORK_DIR/dm-task.yaml
   100  	sed -i "s/task-mode: all/task-mode: incremental/g" $WORK_DIR/dm-task.yaml
   101  
   102  	# test with relay
   103  
   104  	run_sql_tidb 'DROP DATABASE if exists start_task;CREATE DATABASE start_task;'
   105  	run_dm_ctl $WORK_DIR "127.0.0.1:$MASTER_PORT" \
   106  		"start-task $WORK_DIR/dm-task.yaml --start-time '$start_time'" \
   107  		"\"result\": true" 2
   108  
   109  	run_sql_tidb_with_retry "show tables in start_task;" "t2"
   110  	run_sql_tidb_with_retry "SELECT count(1) FROM information_schema.tables WHERE table_schema = 'start_task';" "count(1): 1"
   111  
   112  	run_dm_ctl $WORK_DIR "127.0.0.1:$MASTER_PORT" \
   113  		"stop-task test" \
   114  		"\"result\": true" 2
   115  
   116  	# test without relay and safe mode
   117  
   118  	run_dm_ctl $WORK_DIR "127.0.0.1:$MASTER_PORT" \
   119  		"stop-relay -s $SOURCE_ID1" \
   120  		"\"result\": true" 1
   121  
   122  	run_dm_ctl $WORK_DIR "127.0.0.1:$MASTER_PORT" \
   123  		"query-status -s $SOURCE_ID1" \
   124  		"\"relayStatus\": null" 1
   125  
   126  	run_dm_worker $WORK_DIR/worker2 $WORKER2_PORT $cur/conf/dm-worker2.toml
   127  	check_rpc_alive $cur/../bin/check_worker_online 127.0.0.1:$WORKER2_PORT
   128  
   129  	run_dm_ctl $WORK_DIR "127.0.0.1:$MASTER_PORT" \
   130  		"transfer-source $SOURCE_ID1 worker2" \
   131  		"\"result\": true" 1
   132  
   133  	run_sql_tidb 'DROP DATABASE if exists start_task;CREATE DATABASE start_task;'
   134  	run_dm_ctl $WORK_DIR "127.0.0.1:$MASTER_PORT" \
   135  		"start-task $WORK_DIR/dm-task.yaml --start-time '$start_time'" \
   136  		"\"result\": true" 2
   137  
   138  	run_sql_tidb_with_retry "show tables in start_task;" "t2"
   139  	run_sql_tidb_with_retry "SELECT count(1) FROM information_schema.tables WHERE table_schema = 'start_task';" "count(1): 1"
   140  
   141  	# no duplicate entry error
   142  	check_log_contain_with_retry "enable safe-mode for safe mode exit point, will exit at" $WORK_DIR/worker2/log/dm-worker.log
   143  	run_dm_ctl $WORK_DIR "127.0.0.1:$MASTER_PORT" \
   144  		"query-status test" \
   145  		"\"result\": true" 2
   146  
   147  	run_dm_ctl $WORK_DIR "127.0.0.1:$MASTER_PORT" \
   148  		"stop-task test" \
   149  		"\"result\": true" 2
   150  
   151  	# test too early
   152  
   153  	run_sql_tidb 'DROP DATABASE if exists start_task;CREATE DATABASE start_task;'
   154  	run_dm_ctl $WORK_DIR "127.0.0.1:$MASTER_PORT" \
   155  		"start-task $WORK_DIR/dm-task.yaml --start-time '1995-03-07 01:02:03'" \
   156  		"\"result\": true" 2
   157  
   158  	run_sql_tidb_with_retry "show tables in start_task;" "t1"
   159  	run_sql_tidb_with_retry "SELECT count(1) FROM information_schema.tables WHERE table_schema = 'start_task';" "count(1): 2"
   160  
   161  	run_dm_ctl $WORK_DIR "127.0.0.1:$MASTER_PORT" \
   162  		"stop-task test" \
   163  		"\"result\": true" 2
   164  
   165  	# test too late
   166  
   167  	run_sql_tidb 'DROP DATABASE if exists start_task;CREATE DATABASE start_task;'
   168  	run_dm_ctl $WORK_DIR "127.0.0.1:$MASTER_PORT" \
   169  		"start-task $WORK_DIR/dm-task.yaml --start-time '2037-12-12 01:02:03'"
   170  
   171  	run_dm_ctl_with_retry $WORK_DIR "127.0.0.1:$MASTER_PORT" \
   172  		"query-status test" \
   173  		"\"stage\": \"Paused\"" 1 \
   174  		"no binlog location matches it" 1
   175  
   176  	run_dm_ctl $WORK_DIR "127.0.0.1:$MASTER_PORT" \
   177  		"stop-task test" \
   178  		"\"result\": true" 2
   179  
   180  	export GO_FAILPOINTS=''
   181  	cleanup_process
   182  	cleanup_data start_task
   183  }
   184  
   185  function run() {
   186  	start_task_by_time
   187  	init_tracker_test
   188  	failpoints=(
   189  		# 1152 is ErrAbortingConnection
   190  		"github.com/pingcap/tiflow/dm/pkg/conn/FetchTargetDoTablesFailed=return(1152)"
   191  		"github.com/pingcap/tiflow/dm/pkg/conn/FetchAllDoTablesFailed=return(1152)"
   192  	)
   193  
   194  	for ((i = 0; i < ${#failpoints[@]}; i++)); do
   195  		WORK_DIR=$TEST_DIR/$TEST_NAME/$i
   196  
   197  		echo "failpoint=${failpoints[i]}"
   198  		export GO_FAILPOINTS=${failpoints[i]}
   199  
   200  		# clear downstream env
   201  		run_sql 'DROP DATABASE if exists dm_meta;' $TIDB_PORT $TIDB_PASSWORD
   202  		run_sql 'DROP DATABASE if exists start_task;' $TIDB_PORT $TIDB_PASSWORD
   203  		prepare_data $i
   204  
   205  		run_dm_master $WORK_DIR/master $MASTER_PORT $cur/conf/dm-master.toml
   206  		check_rpc_alive $cur/../bin/check_master_online 127.0.0.1:$MASTER_PORT
   207  		run_dm_worker $WORK_DIR/worker1 $WORKER1_PORT $cur/conf/dm-worker1.toml
   208  		check_rpc_alive $cur/../bin/check_worker_online 127.0.0.1:$WORKER1_PORT
   209  		# operate mysql config to worker
   210  		cp $cur/conf/source1.yaml $WORK_DIR/source1.yaml
   211  		sed -i "/relay-binlog-name/i\relay-dir: $WORK_DIR/worker1/relay_log" $WORK_DIR/source1.yaml
   212  		dmctl_operate_source create $WORK_DIR/source1.yaml $SOURCE_ID1
   213  
   214  		echo "check un-accessible DM-worker exists"
   215  		run_dm_ctl $WORK_DIR "127.0.0.1:$MASTER_PORT" \
   216  			"query-status -s 127.0.0.1:8888" \
   217  			"sources \[127.0.0.1:8888\] haven't been added" 1
   218  
   219  		echo "start task and will failed"
   220  		task_conf="$cur/conf/dm-task.yaml"
   221  		run_dm_ctl $WORK_DIR "127.0.0.1:$MASTER_PORT" \
   222  			"start-task $task_conf" \
   223  			"\"result\": false" 1 \
   224  			"ERROR" 1
   225  
   226  		echo "reset go failpoints, and need restart dm-worker, then start task again"
   227  		kill_dm_worker
   228  		kill_dm_master
   229  
   230  		export GO_FAILPOINTS=''
   231  		run_dm_master $WORK_DIR/master $MASTER_PORT $cur/conf/dm-master.toml
   232  		check_rpc_alive $cur/../bin/check_master_online 127.0.0.1:$MASTER_PORT
   233  		run_dm_worker $WORK_DIR/worker1 $WORKER1_PORT $cur/conf/dm-worker1.toml
   234  		check_rpc_alive $cur/../bin/check_worker_online 127.0.0.1:$WORKER1_PORT
   235  		sleep 5
   236  
   237  		dmctl_start_task_standalone $task_conf
   238  
   239  		check_sync_diff $WORK_DIR $cur/conf/diff_config.toml
   240  
   241  		cleanup_process
   242  	done
   243  
   244  	test_COMMIT_in_QueryEvent
   245  }
   246  
   247  function prepare_data_MyISAM() {
   248  	run_sql 'DROP DATABASE if exists start_task;' $TIDB_PORT $TIDB_PASSWORD
   249  	run_sql 'DROP DATABASE if exists start_task;' $MYSQL_PORT1 $MYSQL_PASSWORD1
   250  	run_sql 'CREATE DATABASE start_task;' $MYSQL_PORT1 $MYSQL_PASSWORD1
   251  	run_sql "CREATE TABLE start_task.t1(i TINYINT, j INT UNIQUE KEY) engine=MyISAM;" $MYSQL_PORT1 $MYSQL_PASSWORD1
   252  	for j in $(seq 10); do
   253  		run_sql "INSERT INTO start_task.t1 VALUES ($j,${j}000$j);" $MYSQL_PORT1 $MYSQL_PASSWORD1
   254  	done
   255  }
   256  
   257  function test_COMMIT_in_QueryEvent() {
   258  	echo "[$(date)] <<<<<< start test_COMMIT_in_QueryEvent >>>>>>"
   259  	cleanup_process
   260  	cleanup_data start_task
   261  	prepare_data_MyISAM
   262  
   263  	cp $cur/conf/source1.yaml $WORK_DIR/source1.yaml
   264  	cp $cur/conf/dm-master.toml $WORK_DIR/
   265  	cp $cur/conf/dm-worker1.toml $WORK_DIR/
   266  	cp $cur/conf/dm-task.yaml $WORK_DIR/
   267  
   268  	# start DM worker and master
   269  	run_dm_master $WORK_DIR/master $MASTER_PORT $WORK_DIR/dm-master.toml
   270  	check_rpc_alive $cur/../bin/check_master_online 127.0.0.1:$MASTER_PORT
   271  	run_dm_worker $WORK_DIR/worker1 $WORKER1_PORT $WORK_DIR/dm-worker1.toml
   272  	check_rpc_alive $cur/../bin/check_worker_online 127.0.0.1:$WORKER1_PORT
   273  
   274  	# operate mysql config to worker
   275  	run_dm_ctl_with_retry $WORK_DIR "127.0.0.1:$MASTER_PORT" \
   276  		"operate-source create $WORK_DIR/source1.yaml" \
   277  		"\"result\": true" 2 \
   278  		"\"source\": \"$SOURCE_ID1\"" 1
   279  
   280  	echo "check master alive"
   281  	run_dm_ctl_with_retry $WORK_DIR "127.0.0.1:$MASTER_PORT" \
   282  		"list-member" \
   283  		"\"alive\": true" 1
   284  
   285  	echo "start task and check stage"
   286  	run_dm_ctl_with_retry $WORK_DIR "127.0.0.1:$MASTER_PORT" \
   287  		"start-task $WORK_DIR/dm-task.yaml --remove-meta=true" \
   288  		"\"result\": true" 2
   289  
   290  	run_sql "CREATE TABLE start_task.t2(i TINYINT, j INT UNIQUE KEY) engine=MyISAM;" $MYSQL_PORT1 $MYSQL_PASSWORD1
   291  	run_sql 'INSERT INTO start_task.t1 VALUES (99,9999);' $MYSQL_PORT1 $MYSQL_PASSWORD1
   292  	run_sql 'INSERT INTO start_task.t2 VALUES (99,9999);' $MYSQL_PORT1 $MYSQL_PASSWORD1
   293  
   294  	run_dm_ctl_with_retry $WORK_DIR "127.0.0.1:$MASTER_PORT" \
   295  		"query-status test" \
   296  		"\"result\": true" 2 \
   297  		"\"unit\": \"Sync\"" 1 \
   298  		"\"stage\": \"Running\"" 2
   299  
   300  	echo "check data"
   301  	check_sync_diff $WORK_DIR $cur/conf/diff_config.toml
   302  
   303  	check_log_not_contains $WORK_DIR/worker1/log/dm-worker.log "originSQL: COMMIT"
   304  
   305  	echo "<<<<<< test_COMMIT_in_QueryEvent success! >>>>>>"
   306  }
   307  
   308  cleanup_data start_task
   309  
   310  cleanup_process $*
   311  run $*
   312  cleanup_process $*
   313  
   314  echo "[$(date)] <<<<<< test case $TEST_NAME success! >>>>>>"