github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/dm/tests/upstream_switch/case.sh (about)

     1  #!/bin/bash
     2  
     3  set -eu
     4  
     5  CUR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
     6  PATH=$CUR/../_utils:$PATH # for sync_diff_inspector
     7  
     8  source $CUR/lib.sh
     9  
    10  function clean_data() {
    11  	echo "-------clean_data--------"
    12  
    13  	exec_sql $master_57_host "stop slave;"
    14  	exec_sql $slave_57_host "stop slave;"
    15  	exec_sql $master_8_host "stop slave;"
    16  	exec_sql $slave_8_host "stop slave;"
    17  
    18  	exec_sql $master_57_host "drop database if exists db1;"
    19  	exec_sql $master_57_host "drop database if exists db2;"
    20  	exec_sql $master_57_host "drop database if exists ${db};"
    21  	exec_sql $master_57_host "reset master;"
    22  	exec_sql $slave_57_host "drop database if exists db1;"
    23  	exec_sql $slave_57_host "drop database if exists db2;"
    24  	exec_sql $slave_57_host "drop database if exists ${db};"
    25  	exec_sql $slave_57_host "reset master;"
    26  	exec_sql $master_8_host "drop database if exists db1;"
    27  	exec_sql $master_8_host "drop database if exists db2;"
    28  	exec_sql $master_8_host "drop database if exists ${db};"
    29  	exec_sql $master_8_host "reset master;"
    30  	exec_sql $slave_8_host "drop database if exists db1;"
    31  	exec_sql $slave_8_host "drop database if exists db2;"
    32  	exec_sql $slave_8_host "drop database if exists ${db};"
    33  	exec_sql $slave_8_host "reset master;"
    34  	exec_tidb $tidb_host "drop database if exists db1;"
    35  	exec_tidb $tidb_host "drop database if exists db2;"
    36  	exec_tidb $tidb_host "drop database if exists ${db};"
    37  	rm -rf /tmp/dm_test
    38  }
    39  
    40  function cleanup_process() {
    41  	echo "-------cleanup_process--------"
    42  	pkill -hup dm-worker.test 2>/dev/null || true
    43  	pkill -hup dm-master.test 2>/dev/null || true
    44  	pkill -hup dm-syncer.test 2>/dev/null || true
    45  
    46  }
    47  
    48  function prepare_binlogs() {
    49  	echo "-------prepare_binlogs--------"
    50  
    51  	prepare_more_binlogs $master_57_host
    52  	prepare_less_binlogs $slave_57_host
    53  	prepare_less_binlogs $master_8_host
    54  	prepare_more_binlogs $slave_8_host
    55  }
    56  
    57  function setup_replica() {
    58  	echo "-------setup_replica--------"
    59  
    60  	master_57_status=($(get_master_status $master_57_host))
    61  	slave_57_status=($(get_master_status $slave_57_host))
    62  	master_8_status=($(get_master_status $master_8_host))
    63  	slave_8_status=($(get_master_status $slave_8_host))
    64  	echo "master_57_status" ${master_57_status[@]}
    65  	echo "slave_57_status" ${slave_57_status[@]}
    66  	echo "master_8_status" ${master_8_status[@]}
    67  	echo "slave_8_status" ${slave_8_status[@]}
    68  
    69  	# master <-- slave
    70  	change_master_to_pos $slave_57_host $master_57_host ${master_57_status[0]} ${master_57_status[1]}
    71  
    72  	# master <--> master
    73  	change_master_to_gtid $slave_8_host $master_8_host
    74  	change_master_to_gtid $master_8_host $slave_8_host
    75  }
    76  
    77  function run_dm_components_and_create_sources() {
    78  	echo "-------run_dm_components--------"
    79  
    80  	pkill -9 dm-master || true
    81  	pkill -9 dm-worker || true
    82  
    83  	run_dm_master $WORK_DIR/master $MASTER_PORT $CUR/conf/dm-master.toml
    84  	run_dm_ctl_with_retry $WORK_DIR "127.0.0.1:$MASTER_PORT" \
    85  		"list-member" \
    86  		"alive" 1
    87  
    88  	run_dm_worker $WORK_DIR/worker1 $WORKER1_PORT $CUR/conf/dm-worker1.toml
    89  	run_dm_ctl_with_retry $WORK_DIR "127.0.0.1:$MASTER_PORT" \
    90  		"list-member" \
    91  		"free" 1
    92  	run_dm_ctl $WORK_DIR "127.0.0.1:$MASTER_PORT" \
    93  		"operate-source create $CUR/conf/source1.yaml" \
    94  		"\"result\": true" 2
    95  
    96  	run_dm_worker $WORK_DIR/worker2 $WORKER2_PORT $CUR/conf/dm-worker2.toml
    97  	run_dm_ctl_with_retry $WORK_DIR "127.0.0.1:$MASTER_PORT" \
    98  		"list-member" \
    99  		"free" 1
   100  	run_dm_ctl $WORK_DIR "127.0.0.1:$MASTER_PORT" \
   101  		"operate-source create $CUR/conf/source2.yaml" \
   102  		"\"result\": true" 2
   103  
   104  	run_dm_ctl_with_retry $WORK_DIR "127.0.0.1:$MASTER_PORT" \
   105  		"list-member" \
   106  		"alive" 1 \
   107  		"bound" 2
   108  }
   109  
   110  function gen_full_data() {
   111  	echo "-------gen_full_data--------"
   112  
   113  	exec_sql $host1 "create database ${db} collate latin1_bin;"
   114  	exec_sql $host1 "create table ${db}.${tb}(id int primary key, a int);"
   115  	for i in $(seq 1 100); do
   116  		exec_sql $host1 "insert into ${db}.${tb} values($i,$i);"
   117  	done
   118  
   119  	exec_sql $host2 "create database ${db} collate latin1_bin;"
   120  	exec_sql $host2 "create table ${db}.${tb}(id int primary key, a int);"
   121  	for i in $(seq 101 200); do
   122  		exec_sql $host2 "insert into ${db}.${tb} values($i,$i);"
   123  	done
   124  }
   125  
   126  function gen_incr_data() {
   127  	echo "-------gen_incr_data--------"
   128  
   129  	for i in $(seq 201 250); do
   130  		exec_sql $host1 "insert into ${db}.${tb} values($i,$i);"
   131  	done
   132  	for i in $(seq 251 300); do
   133  		exec_sql $host2 "insert into ${db}.${tb} values($i,$i);"
   134  	done
   135  	exec_sql $host1 "alter table ${db}.${tb} add column b int;"
   136  	exec_sql $host2 "alter table ${db}.${tb} add column b int;"
   137  	for i in $(seq 301 350); do
   138  		exec_sql $host1 "insert into ${db}.${tb} values($i,$i,$i);"
   139  	done
   140  	for i in $(seq 351 400); do
   141  		exec_sql $host2 "insert into ${db}.${tb} values($i,$i,$i);"
   142  	done
   143  
   144  	# make master slave switch
   145  	docker-compose -f $CUR/docker-compose.yml pause mysql57_master
   146  	docker-compose -f $CUR/docker-compose.yml pause mysql8_master
   147  	wait_mysql $host1 2
   148  	wait_mysql $host2 2
   149  
   150  	for i in $(seq 401 450); do
   151  		exec_sql $host1 "insert into ${db}.${tb} values($i,$i,$i);"
   152  	done
   153  	for i in $(seq 451 500); do
   154  		exec_sql $host2 "insert into ${db}.${tb} values($i,$i,$i);"
   155  	done
   156  	exec_sql $host1 "alter table ${db}.${tb} add column c int;"
   157  	exec_sql $host2 "alter table ${db}.${tb} add column c int;"
   158  
   159  	# make sure relay switch before unpause
   160  	run_dm_ctl_with_retry $WORK_DIR "127.0.0.1:$MASTER_PORT" \
   161  		"query-status task_pessimistic -s mysql-replica-02" \
   162  		"relaySubDir.*000002" 1
   163  
   164  	docker-compose -f $CUR/docker-compose.yml unpause mysql8_master
   165  	wait_mysql $host2 1
   166  
   167  	for i in $(seq 501 550); do
   168  		exec_sql $host1 "insert into ${db}.${tb} values($i,$i,$i,$i);"
   169  	done
   170  	for i in $(seq 551 600); do
   171  		exec_sql $host2 "insert into ${db}.${tb} values($i,$i,$i,$i);"
   172  	done
   173  }
   174  
   175  function start_task() {
   176  	run_dm_ctl $WORK_DIR "127.0.0.1:$MASTER_PORT" \
   177  		"start-task $CUR/conf/task-pessimistic.yaml --remove-meta" \
   178  		"\result\": true" 3
   179  }
   180  
   181  function verify_result() {
   182  	check_sync_diff $WORK_DIR $CUR/conf/diff_config.toml
   183  	run_dm_ctl $WORK_DIR "127.0.0.1:$MASTER_PORT" \
   184  		"query-status task_pessimistic -s mysql-replica-02" \
   185  		"relaySubDir.*000003" 1
   186  }
   187  
   188  function clean_task() {
   189  	run_dm_ctl $WORK_DIR "127.0.0.1:$MASTER_PORT" \
   190  		"stop-task task_pessimistic" \
   191  		"\result\": true" 3
   192  	run_dm_ctl $WORK_DIR "127.0.0.1:$MASTER_PORT" \
   193  		"stop-relay -s mysql-replica-02" \
   194  		"\result\": true" 1
   195  	run_dm_ctl $WORK_DIR "127.0.0.1:$MASTER_PORT" \
   196  		"operate-source stop mysql-replica-01" \
   197  		"\result\": true" 2
   198  	run_dm_ctl $WORK_DIR "127.0.0.1:$MASTER_PORT" \
   199  		"operate-source stop mysql-replica-02" \
   200  		"\result\": true" 2
   201  }
   202  
   203  function check_master() {
   204  	wait_mysql $host1 1
   205  	wait_mysql $host2 1
   206  }
   207  
   208  function test_relay() {
   209  	cleanup_process
   210  	check_master
   211  	install_sync_diff
   212  	clean_data
   213  	prepare_binlogs
   214  	setup_replica
   215  	gen_full_data
   216  	run_dm_components_and_create_sources
   217  	start_task
   218  	gen_incr_data
   219  	verify_result
   220  	clean_task
   221  	docker-compose -f $CUR/docker-compose.yml unpause mysql57_master
   222  	echo "CASE=test_relay success"
   223  }
   224  
   225  function test_binlog_filename_change_when_enable_gtid() {
   226  	cleanup_process
   227  	check_master
   228  	clean_data
   229  	setup_replica
   230  	gen_full_data
   231  	run_dm_components_and_create_sources
   232  	start_task
   233  
   234  	# flush mysql8 master binlog files -> mysql-bin.000004
   235  	exec_sql $host2 "flush logs;"
   236  	exec_sql $host2 "flush logs;"
   237  	exec_sql $host2 "flush logs;"
   238  
   239  	# make mysql8 master slave switch
   240  	docker-compose -f $CUR/docker-compose.yml pause mysql8_master
   241  	wait_mysql $host2 2
   242  
   243  	# make a flush job to dm
   244  	exec_sql $host1 "alter table ${db}.${tb} add column c int;"
   245  	exec_sql $host2 "alter table ${db}.${tb} add column c int;"
   246  
   247  	check_sync_diff $WORK_DIR $CUR/conf/diff_config.toml
   248  	# mysql8 new master's binlog file still is mysql-bin.000001
   249  	run_dm_ctl_with_retry $WORK_DIR "127.0.0.1:$MASTER_PORT" \
   250  		"query-status task_pessimistic " \
   251  		"mysql-bin.000001" 5 # source-1 have match 2, source-2 match 3 (relay 2 + task's masterBinlog 1)
   252  
   253  	docker-compose -f $CUR/docker-compose.yml unpause mysql8_master
   254  	clean_task
   255  	echo "CASE=test_binlog_filename_change_when_enable_gtid success"
   256  }
   257  
   258  function test_binlog_filename_change_when_enable_relay_and_gtid() {
   259  	cleanup_process
   260  	check_master
   261  	clean_data
   262  	setup_replica
   263  	gen_full_data
   264  	run_dm_components_and_create_sources
   265  	start_task
   266  
   267  	# flush mysql8 master binlog files -> mysql-bin.000004
   268  	exec_sql $host2 "flush logs;"
   269  	exec_sql $host2 "flush logs;"
   270  	exec_sql $host2 "flush logs;"
   271  
   272  	# make mysql8 master slave switch
   273  	docker-compose -f $CUR/docker-compose.yml pause mysql8_master
   274  	wait_mysql $host2 2
   275  
   276  	# make a flush job to dm
   277  	exec_sql $host1 "alter table ${db}.${tb} add column c int;"
   278  	exec_sql $host2 "alter table ${db}.${tb} add column c int;"
   279  
   280  	check_sync_diff $WORK_DIR $CUR/conf/diff_config.toml
   281  	# mysql8 new master's binlog file still is mysql-bin.000001
   282  	run_dm_ctl_with_retry $WORK_DIR "127.0.0.1:$MASTER_PORT" \
   283  		"query-status task_pessimistic " \
   284  		"mysql-bin.000001" 5 # source-1 have match 2, source-2 match 3 (relay 2 + task's masterBinlog 1)
   285  
   286  	docker-compose -f $CUR/docker-compose.yml unpause mysql8_master
   287  	clean_task
   288  	echo "CASE=test_binlog_filename_change_when_enable_relay_and_gtid success"
   289  }
   290  
   291  function run() {
   292  	test_relay
   293  	test_binlog_filename_change_when_enable_gtid
   294  	test_binlog_filename_change_when_enable_relay_and_gtid
   295  }
   296  
   297  run