github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/dm/tests/shardddl3_1/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=$TEST_DIR/$TEST_NAME
     8  source $cur/../_utils/shardddl_lib.sh
     9  
    10  function DM_099_CASE() {
    11  	# here we run ddl to make sure we flush first check point in syncer
    12  	# otherwise the worker may dump again when restart
    13  	run_sql_source1 "alter table ${shardddl1}.${tb1} add column col int;"
    14  	run_sql_source1 "alter table ${shardddl1}.${tb2} add column col int;"
    15  	run_sql_source2 "alter table ${shardddl1}.${tb1} add column col int;"
    16  	run_sql_source2 "alter table ${shardddl1}.${tb2} add column col int;"
    17  	check_sync_diff $WORK_DIR $cur/conf/diff_config.toml
    18  
    19  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(1,1);"
    20  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(2,2);"
    21  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(3,3);"
    22  	run_sql_source1 "insert into ${shardddl1}.${tb2} values(4,4);"
    23  
    24  	run_sql_source1 "alter table ${shardddl1}.${tb1} add column new_col1 int;"
    25  	run_sql_source2 "alter table ${shardddl1}.${tb1} add column new_col1 int;"
    26  
    27  	kill_process dm-worker2
    28  	check_port_offline $WORKER2_PORT 20
    29  
    30  	run_sql_source1 "alter table ${shardddl1}.${tb2} add column new_col1 int;"
    31  
    32  	run_dm_worker $WORK_DIR/worker2 $WORKER2_PORT $cur/conf/dm-worker2.toml
    33  	check_rpc_alive $cur/../bin/check_worker_online 127.0.0.1:$WORKER2_PORT
    34  
    35  	run_sql_source2 "alter table ${shardddl1}.${tb2} add column new_col1 int;"
    36  
    37  	check_sync_diff $WORK_DIR $cur/conf/diff_config.toml
    38  }
    39  
    40  function DM_099() {
    41  	run_case 099 "double-source-pessimistic" "init_table 111 112 211 212" "clean_table" "pessimistic"
    42  }
    43  
    44  function DM_100_CASE() {
    45  	# here we run ddl to make sure we flush first check point in syncer
    46  	# otherwise the worker may dump again when restart
    47  	run_sql_source1 "alter table ${shardddl1}.${tb1} add column col int;"
    48  	run_sql_source1 "alter table ${shardddl1}.${tb2} add column col int;"
    49  	run_sql_source2 "alter table ${shardddl1}.${tb1} add column col int;"
    50  	run_sql_source2 "alter table ${shardddl1}.${tb2} add column col int;"
    51  	check_sync_diff $WORK_DIR $cur/conf/diff_config.toml
    52  
    53  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(1,1);"
    54  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(2,2);"
    55  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(3,3);"
    56  	run_sql_source1 "insert into ${shardddl1}.${tb2} values(4,4);"
    57  
    58  	run_sql_source1 "alter table ${shardddl1}.${tb1} add column new_col1 int;"
    59  
    60  	kill_process dm-worker1
    61  	check_port_offline $WORKER1_PORT 20
    62  
    63  	run_sql_source2 "alter table ${shardddl1}.${tb1} add column new_col1 int;"
    64  	run_sql_source1 "alter table ${shardddl1}.${tb2} add column new_col1 int;"
    65  
    66  	run_dm_worker $WORK_DIR/worker1 $WORKER1_PORT $cur/conf/dm-worker1.toml
    67  	check_rpc_alive $cur/../bin/check_worker_online 127.0.0.1:$WORKER1_PORT
    68  
    69  	run_sql_source2 "alter table ${shardddl1}.${tb2} add column new_col1 int;"
    70  
    71  	check_sync_diff $WORK_DIR $cur/conf/diff_config.toml
    72  }
    73  
    74  function DM_100() {
    75  	run_case 100 "double-source-optimistic" "init_table 111 112 211 212" "clean_table" "optimistic"
    76  }
    77  
    78  function DM_101_CASE() {
    79  	# here we run ddl to make sure we flush first check point in syncer
    80  	# otherwise the worker may dump again when restart
    81  	run_sql_source1 "alter table ${shardddl1}.${tb1} add column col int;"
    82  	run_sql_source1 "alter table ${shardddl1}.${tb2} add column col int;"
    83  	run_sql_source2 "alter table ${shardddl1}.${tb1} add column col int;"
    84  	run_sql_source2 "alter table ${shardddl1}.${tb2} add column col int;"
    85  	check_sync_diff $WORK_DIR $cur/conf/diff_config.toml
    86  
    87  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(1,1);"
    88  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(2,2);"
    89  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(3,3);"
    90  	run_sql_source1 "insert into ${shardddl1}.${tb2} values(4,4);"
    91  
    92  	run_sql_source1 "alter table ${shardddl1}.${tb1} add column new_col1 int;"
    93  	run_sql_source2 "alter table ${shardddl1}.${tb1} add column new_col1 int;"
    94  
    95  	kill_process dm-worker2
    96  	check_port_offline $WORKER2_PORT 20
    97  
    98  	run_sql_source1 "alter table ${shardddl1}.${tb2} add column new_col1 int;"
    99  
   100  	run_dm_worker $WORK_DIR/worker2 $WORKER2_PORT $cur/conf/dm-worker2.toml
   101  	check_rpc_alive $cur/../bin/check_worker_online 127.0.0.1:$WORKER2_PORT
   102  
   103  	run_sql_source2 "alter table ${shardddl1}.${tb2} add column new_col1 int;"
   104  
   105  	check_sync_diff $WORK_DIR $cur/conf/diff_config.toml
   106  }
   107  
   108  function DM_101() {
   109  	run_case 101 "double-source-optimistic" "init_table 111 112 211 212" "clean_table" "optimistic"
   110  }
   111  
   112  function DM_102_CASE() {
   113  	run_sql_source1 "alter table ${shardddl1}.${tb1} add column new_col1 int default 0;"
   114  	run_sql_source1 "insert into ${shardddl1}.${tb1} values (1,1);"
   115  	run_sql_source2 "alter table ${shardddl1}.${tb1} add column new_col1 int default -1;"
   116  
   117  	sleep 1
   118  	# wait DM receive source2's DDL
   119  	found=false
   120  	for ((k = 0; k < 10; k++)); do
   121  		content=$($PWD/bin/dmctl.test DEVEL --master-addr=127.0.0.1:$MASTER_PORT query-status test)
   122  		master2=$(echo $content | sed 's/"masterBinlog":/"masterBinlog":\n/g' | awk -F')' 'FNR==3{print $1}')
   123  		syncer2=$(echo $content | sed 's/"syncerBinlog":/"syncerBinlog":\n/g' | awk -F')' 'FNR==3{print $1}')
   124  		if [ "$master2" != "$syncer2" ]; then
   125  			found=true
   126  			break
   127  		fi
   128  	done
   129  	if [[ $found == false ]]; then
   130  		echo "didn't receive mismatched DDL"
   131  		exit 2
   132  	fi
   133  
   134  	run_dm_ctl_with_retry $WORK_DIR "127.0.0.1:$MASTER_PORT" \
   135  		"shard-ddl-lock" \
   136  		"\"ID\": \"test-\`shardddl\`.\`tb\`\"" 1
   137  
   138  	run_dm_ctl_with_retry $WORK_DIR "127.0.0.1:$MASTER_PORT" \
   139  		"shard-ddl-lock unlock test-\`shardddl\`.\`tb\`" \
   140  		"\"result\": true" 1
   141  
   142  	run_sql_source2 "insert into ${shardddl1}.${tb1} values (2,2);"
   143  
   144  	run_sql_tidb_with_retry "select count(1) from ${shardddl}.${tb};" "count(1): 2"
   145  }
   146  
   147  function DM_102() {
   148  	run_case 102 "double-source-pessimistic" "init_table 111 211" "clean_table" ""
   149  }
   150  
   151  function DM_103_CASE() {
   152  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(1,'aaa');"
   153  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(2,'bbb');"
   154  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(3,'ccc');"
   155  
   156  	run_sql_source1 "alter table ${shardddl1}.${tb1} add column c double;"
   157  	run_sql_source1 "alter table ${shardddl1}.${tb1} drop column b;"
   158  	run_sql_source2 "alter table ${shardddl1}.${tb1} add column c double;"
   159  	run_sql_source2 "alter table ${shardddl1}.${tb2} add column c double;"
   160  	run_sql_source1 "alter table ${shardddl1}.${tb1} change a a bigint default 10;"
   161  	run_sql_source2 "alter table ${shardddl1}.${tb1} drop column b;"
   162  	run_sql_source2 "alter table ${shardddl1}.${tb1} change a a bigint default 10;"
   163  	run_sql_source2 "alter table ${shardddl1}.${tb2} drop column b;"
   164  	run_sql_source2 "alter table ${shardddl1}.${tb2} change a a bigint default 10;"
   165  
   166  	check_sync_diff $WORK_DIR $cur/conf/diff_config.toml
   167  }
   168  
   169  function DM_103() {
   170  	run_case 103 "double-source-pessimistic" \
   171  		"run_sql_source1 \"create table ${shardddl1}.${tb1} (a int primary key, b varchar(10));\"; \
   172       run_sql_source2 \"create table ${shardddl1}.${tb1} (a int primary key, b varchar(10));\"; \
   173       run_sql_source2 \"create table ${shardddl1}.${tb2} (a int primary key, b varchar(10));\"" \
   174  		"clean_table" "pessimistic"
   175  	run_case 103 "double-source-optimistic" \
   176  		"run_sql_source1 \"create table ${shardddl1}.${tb1} (a int primary key, b varchar(10));\"; \
   177       run_sql_source2 \"create table ${shardddl1}.${tb1} (a int primary key, b varchar(10));\"; \
   178       run_sql_source2 \"create table ${shardddl1}.${tb2} (a int primary key, b varchar(10));\"" \
   179  		"clean_table" "optimistic"
   180  }
   181  
   182  function DM_104_CASE() {
   183  	run_sql_source1 "alter table ${shardddl1}.${tb1} add new_col1 int not null default 10;"
   184  	run_sql_source1 "insert into ${shardddl1}.${tb1} (id) values(1);"
   185  	run_sql_source1 "alter table ${shardddl1}.${tb1} add new_col2 int not null default 20;"
   186  	run_sql_source1 "insert into ${shardddl1}.${tb1} (id) values(2);"
   187  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(3);"
   188  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(4);"
   189  	run_sql_source2 "alter table ${shardddl1}.${tb1} add new_col2 int not null default 20;"
   190  	run_sql_source2 "insert into ${shardddl1}.${tb1} (id) values(5);"
   191  	run_sql_source2 "alter table ${shardddl1}.${tb1} add new_col1 int not null default 10;"
   192  	run_sql_source2 "insert into ${shardddl1}.${tb1} (id) values(6);"
   193  	run_sql_source2 "alter table ${shardddl1}.${tb2} add new_col1 int not null default 10;"
   194  	run_sql_source2 "alter table ${shardddl1}.${tb2} add new_col2 int not null default 20;"
   195  	run_sql_source1 "insert into ${shardddl1}.${tb1} (id) values(7);"
   196  	run_sql_source1 "insert into ${shardddl1}.${tb1} (id) values(8);"
   197  	run_sql_source2 "insert into ${shardddl1}.${tb2} (id) values(9);"
   198  	run_sql_tidb_with_retry "select count(1) from ${shardddl}.${tb};" "count(1): 9"
   199  }
   200  
   201  function DM_104() {
   202  	# currently not support pessimistic
   203  	# run_case 104 "double-source-pessimistic" "init_table 111 211 212" "clean_table" "pessimistic"
   204  	run_case 104 "double-source-optimistic" "init_table 111 211 212" "clean_table" "optimistic"
   205  }
   206  
   207  function add_drop_index_test() {
   208  	action=$1
   209  	col1=$2
   210  	col2=$3
   211  	run_sql_source1 "alter table ${shardddl1}.${tb1} ${action} index new_idx1${col1};"
   212  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(1,1,1);"
   213  	run_sql_source1 "alter table ${shardddl1}.${tb1} ${action} index new_idx2${col2};"
   214  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(2,2,2);"
   215  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(3,3,3);"
   216  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(4,4,4);"
   217  	run_sql_source2 "alter table ${shardddl1}.${tb1} ${action} index new_idx2${col2};"
   218  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(5,5,5);"
   219  	run_sql_source2 "alter table ${shardddl1}.${tb1} ${action} index new_idx1${col1};"
   220  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(6,6,6);"
   221  	run_sql_source2 "alter table ${shardddl1}.${tb2} ${action} index new_idx1${col1};"
   222  	run_sql_source2 "alter table ${shardddl1}.${tb2} ${action} index new_idx2${col2};"
   223  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(7,7,7);"
   224  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(8,8,8);"
   225  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(9,9,9);"
   226  	run_sql_tidb_with_retry "select count(1) from ${shardddl}.${tb};" "count(1): 9"
   227  }
   228  
   229  function DM_105_CASE() {
   230  	add_drop_index_test "add" "(b)" "(c)"
   231  	run_sql_tidb_with_retry "select count(b) from ${shardddl}.${tb};" "count(b): 9"
   232  	run_sql_tidb_with_retry "select count(c) from ${shardddl}.${tb};" "count(c): 9"
   233  }
   234  
   235  function DM_105() {
   236  	# currently not support pessimistic
   237  	# run_case 105 "double-source-pessimistic" \
   238  	# "run_sql_source1 \"create table ${shardddl1}.${tb1} (a int primary key, b int, c int);\"; \
   239  	# run_sql_source2 \"create table ${shardddl1}.${tb1} (a int primary key, b int, c int);\"; \
   240  	# run_sql_source2 \"create table ${shardddl1}.${tb2} (a int primary key, b int, c int);\"" \
   241  	# "clean_table" "pessimistic"
   242  	run_case 105 "double-source-optimistic" \
   243  		"run_sql_source1 \"create table ${shardddl1}.${tb1} (a int primary key, b int, c int);\"; \
   244       run_sql_source2 \"create table ${shardddl1}.${tb1} (a int primary key, b int, c int);\"; \
   245       run_sql_source2 \"create table ${shardddl1}.${tb2} (a int primary key, b int, c int);\"" \
   246  		"clean_table" "optimistic"
   247  }
   248  
   249  function DM_106_CASE() {
   250  	add_drop_index_test "drop" "" ""
   251  }
   252  
   253  function DM_106() {
   254  	# currently not support pessimistic
   255  	# run_case 106 "double-source-pessimistic" \
   256  	# "run_sql_source1 \"create table ${shardddl1}.${tb1} (a int primary key, b int, c int, index new_idx1(b), index new_idx2(c));\"; \
   257  	# run_sql_source2 \"create table ${shardddl1}.${tb1} (a int primary key, b int, c int, index new_idx1(b), index new_idx2(c));\"; \
   258  	# run_sql_source2 \"create table ${shardddl1}.${tb2} (a int primary key, b int, c int, index new_idx1(b), index new_idx2(c));\"" \
   259  	# "clean_table" "pessimistic"
   260  	run_case 106 "double-source-optimistic" \
   261  		"run_sql_source1 \"create table ${shardddl1}.${tb1} (a int primary key, b int, c int, index new_idx1(b), index new_idx2(c));\"; \
   262       run_sql_source2 \"create table ${shardddl1}.${tb1} (a int primary key, b int, c int, index new_idx1(b), index new_idx2(c));\"; \
   263       run_sql_source2 \"create table ${shardddl1}.${tb2} (a int primary key, b int, c int, index new_idx1(b), index new_idx2(c));\"" \
   264  		"clean_table" "optimistic"
   265  }
   266  
   267  function DM_107_CASE() {
   268  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(1);"
   269  	run_sql_source1 "alter table ${shardddl1}.${tb1} add column col1 int not null"
   270  	run_sql_source1 "insert into ${shardddl1}.${tb1} values (2,2);"
   271  	run_dm_ctl_with_retry $WORK_DIR "127.0.0.1:$MASTER_PORT" \
   272  		"query-status test" \
   273  		"Running" 3
   274  
   275  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(3);"
   276  	run_sql_source2 "alter table ${shardddl1}.${tb1} add column col1 int not null;"
   277  	run_sql_source2 "insert into ${shardddl1}.${tb1} values (4,4);"
   278  
   279  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(5);"
   280  	run_sql_source2 "alter table ${shardddl1}.${tb2} add column col1 int not null"
   281  	run_sql_source2 "insert into ${shardddl1}.${tb2} values (6,6);"
   282  
   283  	# insert 3 record to make sure optimistic mode sharding resolve can finish fast
   284  	sleep 3
   285  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(7,7);"
   286  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(8,8);"
   287  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(9,9);"
   288  	check_sync_diff $WORK_DIR $cur/conf/diff_config.toml
   289  }
   290  
   291  function DM_107() {
   292  	# FIXME: should be positive in the future
   293  	# run_case 107 "double-source-pessimistic" "init_table 111 211 212" "clean_table" "pessimistic"
   294  	run_case 107 "double-source-optimistic" "init_table 111 211 212" "clean_table" "optimistic"
   295  }
   296  
   297  function different_field_flag_test() {
   298  	type1=$1
   299  	val1=$2
   300  	type2=$3
   301  	val2=$4
   302  	type3=$5
   303  	val3=$6
   304  	locked=$7
   305  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(1);"
   306  	run_sql_source1 "alter table ${shardddl1}.${tb1} add column col1 $type1"
   307  	run_sql_source1 "insert into ${shardddl1}.${tb1} values (2,${val1});"
   308  
   309  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(3);"
   310  	run_sql_source2 "alter table ${shardddl1}.${tb1} add column col1 $type2"
   311  	run_sql_source2 "insert into ${shardddl1}.${tb1} values (4,${val2});"
   312  
   313  	# we can't sure SQL on which source comes first, so only check the common pattern
   314  	if [[ $locked == true ]]; then
   315  		run_dm_ctl_with_retry $WORK_DIR "127.0.0.1:$MASTER_PORT" \
   316  			"query-status test" \
   317  			"ALTER TABLE \`${shardddl}\`.\`${tb}\` ADD COLUMN \`col1\`" 1 \
   318  			"\`${shardddl1}\`.\`${tb1}\`\"" 1
   319  	else
   320  		run_dm_ctl_with_retry $WORK_DIR "127.0.0.1:$MASTER_PORT" \
   321  			"query-status test" \
   322  			"ALTER TABLE \`${shardddl}\`.\`${tb}\` ADD COLUMN \`col1\`" 2 \
   323  			"because schema conflict detected" 1
   324  	fi
   325  
   326  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(5);"
   327  	run_sql_source2 "alter table ${shardddl1}.${tb2} add column col1 $type3"
   328  	run_sql_source2 "insert into ${shardddl1}.${tb2} values (6,${val3});"
   329  
   330  	check_sync_diff $WORK_DIR $cur/conf/diff_config.toml 3 'fail'
   331  }
   332  
   333  function DM_108_CASE() {
   334  	different_field_flag_test \
   335  		"decimal(5,2)" "2" \
   336  		"decimal(7,4)" "4" \
   337  		"decimal(9,6)" "6" true
   338  }
   339  
   340  function DM_108() {
   341  	run_case 108 "double-source-optimistic" "init_table 111 211 212" "clean_table" "optimistic"
   342  }
   343  
   344  function DM_109_CASE() {
   345  	different_field_flag_test \
   346  		"varchar(3)" "'222'" \
   347  		"varchar(4)" "'4444'" \
   348  		"varchar(5)" "'66666'" false
   349  }
   350  
   351  function DM_109() {
   352  	run_case 109 "double-source-optimistic" "init_table 111 211 212" "clean_table" "optimistic"
   353  }
   354  
   355  function DM_110_CASE() {
   356  	different_field_flag_test \
   357  		"varchar(5)" "'22222'" \
   358  		"varchar(4)" "'4444'" \
   359  		"varchar(3)" "'666'" false
   360  }
   361  
   362  function DM_110() {
   363  	run_case 110 "double-source-optimistic" "init_table 111 211 212" "clean_table" "optimistic"
   364  }
   365  
   366  function DM_111_CASE() {
   367  	different_field_flag_test \
   368  		"int(11) zerofill" "2" \
   369  		"int(11)" "4" \
   370  		"int(11) zerofill" "'66666'" true
   371  }
   372  
   373  function DM_111() {
   374  	run_case 111 "double-source-optimistic" "init_table 111 211 212" "clean_table" "optimistic"
   375  }
   376  
   377  function DM_112_CASE() {
   378  	different_field_flag_test \
   379  		"int(11) unsigned" "2" \
   380  		"int(11)" "4" \
   381  		"int(11) unsigned" "'66666'" true
   382  }
   383  
   384  function DM_112() {
   385  	run_case 112 "double-source-optimistic" "init_table 111 211 212" "clean_table" "optimistic"
   386  }
   387  
   388  function DM_113_CASE {
   389  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(1);"
   390  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(2);"
   391  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(3);"
   392  
   393  	run_sql_source1 "alter table ${shardddl1}.${tb1} add column (b int, c int);"
   394  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(4,4,4);"
   395  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(5);"
   396  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(6);"
   397  
   398  	run_sql_source2 "alter table ${shardddl1}.${tb1} add column (b int, c int);"
   399  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(7,7,7);"
   400  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(8,8,8);"
   401  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(9);"
   402  
   403  	run_sql_source2 "alter table ${shardddl1}.${tb2} add column (b int, c int);"
   404  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(10,10,10);"
   405  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(11,11,11);"
   406  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(12,12,12);"
   407  
   408  	check_sync_diff $WORK_DIR $cur/conf/diff_config.toml
   409  }
   410  
   411  # Add multiple fileds to a single table.
   412  function DM_113 {
   413  	run_case 113 "double-source-pessimistic" "init_table 111 211 212" "clean_table" "pessimistic"
   414  	run_case 113 "double-source-optimistic" "init_table 111 211 212" "clean_table" "optimistic"
   415  }
   416  
   417  function DM_114_CASE {
   418  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(1,1,1);"
   419  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(2,2,2);"
   420  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(3,3,3);"
   421  
   422  	run_sql_source1 "alter table ${shardddl1}.${tb1} drop column b, drop column c;"
   423  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(4);"
   424  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(5,5,5);"
   425  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(6,6,6);"
   426  
   427  	run_sql_source2 "alter table ${shardddl1}.${tb1} drop column b, drop column c;"
   428  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(7);"
   429  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(8);"
   430  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(9,9,9);"
   431  
   432  	run_sql_source2 "alter table ${shardddl1}.${tb2} drop column b, drop column c;"
   433  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(10);"
   434  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(11);"
   435  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(12);"
   436  
   437  	check_sync_diff $WORK_DIR $cur/conf/diff_config.toml
   438  }
   439  
   440  # Drop multiple fields from a single table.
   441  function DM_114 {
   442  	run_case 114 "double-source-pessimistic" \
   443  		"run_sql_source1 \"create table ${shardddl1}.${tb1} (a int primary key, b int, c int);\"; \
   444       run_sql_source2 \"create table ${shardddl1}.${tb1} (a int primary key, b int, c int);\"; \
   445       run_sql_source2 \"create table ${shardddl1}.${tb2} (a int primary key, b int, c int);\"" \
   446  		"clean_table" "pessimistic"
   447  	run_case 114 "double-source-optimistic" \
   448  		"run_sql_source1 \"create table ${shardddl1}.${tb1} (a int primary key, b int, c int);\"; \
   449       run_sql_source2 \"create table ${shardddl1}.${tb1} (a int primary key, b int, c int);\"; \
   450       run_sql_source2 \"create table ${shardddl1}.${tb2} (a int primary key, b int, c int);\"" \
   451  		"clean_table" "optimistic"
   452  }
   453  
   454  function DM_115_CASE {
   455  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(1);"
   456  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(2);"
   457  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(3);"
   458  
   459  	run_sql_source1 "alter table ${shardddl1}.${tb1} add column (b int, c int);"
   460  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(4,4,4);"
   461  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(5);"
   462  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(6);"
   463  
   464  	run_sql_source2 "alter table ${shardddl1}.${tb1} add column (b int, c int);"
   465  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(7,7,7);"
   466  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(8,8,8);"
   467  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(9);"
   468  
   469  	check_log_contain_with_retry 'finish to handle ddls in optimistic shard mode.*add column' \
   470  		$WORK_DIR/worker1/log/dm-worker.log
   471  	check_log_contain_with_retry 'finish to handle ddls in optimistic shard mode.*add column' \
   472  		$WORK_DIR/worker2/log/dm-worker.log
   473  
   474  	run_sql_source2 "alter table ${shardddl1}.${tb1} drop column b, drop column c;"
   475  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(10,10,10);"
   476  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(11);"
   477  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(12);"
   478  
   479  	run_sql_source1 "alter table ${shardddl1}.${tb1} drop column b, drop column c;"
   480  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(13);"
   481  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(14);"
   482  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(15);"
   483  
   484  	check_sync_diff $WORK_DIR $cur/conf/diff_config.toml
   485  }
   486  
   487  # Add multiple fields and rollback.
   488  function DM_115 {
   489  	run_case 115 "double-source-optimistic" "init_table 111 211 212" "clean_table" "optimistic"
   490  }
   491  
   492  function DM_116_CASE {
   493  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(1,1);"
   494  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(2,2);"
   495  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(3,3);"
   496  
   497  	run_sql_source1 "alter table ${shardddl1}.${tb1} add column b int, drop column c;"
   498  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(4,4);"
   499  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(5,5);"
   500  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(6,6);"
   501  
   502  	run_sql_source2 "alter table ${shardddl1}.${tb1} add column b int, drop column c;"
   503  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(7,7);"
   504  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(8,8);"
   505  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(9,9);"
   506  
   507  	run_sql_source2 "alter table ${shardddl1}.${tb2} add column b int, drop column c;"
   508  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(10,10);"
   509  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(11,11);"
   510  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(12,12);"
   511  
   512  	# FIXME: add,drop,add a same column may cause data inconsistency.
   513  	# For example:
   514  	# table1: add column b(t1) -> drop column b(t3) -> add column b(t5)
   515  	# table2: add column b(t2) -> drop column b(dm master update etcd t4, dm worker execute ddl t6)
   516  	# timeline:
   517  	# t1 < t2 < .. < t6
   518  	# Under this condition, DM should pause the task and report an error.
   519  	check_sync_diff $WORK_DIR $cur/conf/diff_config.toml
   520  }
   521  
   522  # Add and Drop multiple fields at the same time.
   523  function DM_116 {
   524  	run_case 116 "double-source-pessimistic" \
   525  		"run_sql_source1 \"create table ${shardddl1}.${tb1} (a int primary key, c int);\"; \
   526       run_sql_source2 \"create table ${shardddl1}.${tb1} (a int primary key, c int);\"; \
   527       run_sql_source2 \"create table ${shardddl1}.${tb2} (a int primary key, c int);\"" \
   528  		"clean_table" "pessimistic"
   529  	run_case 116 "double-source-optimistic" \
   530  		"run_sql_source1 \"create table ${shardddl1}.${tb1} (a int primary key, c int);\"; \
   531       run_sql_source2 \"create table ${shardddl1}.${tb1} (a int primary key, c int);\"; \
   532       run_sql_source2 \"create table ${shardddl1}.${tb2} (a int primary key, c int);\"" \
   533  		"clean_table" "optimistic"
   534  }
   535  
   536  function DM_117_CASE {
   537  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(1,1,1);"
   538  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(2,2,2);"
   539  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(3,3,3);"
   540  
   541  	run_sql_source1 "alter table ${shardddl1}.${tb1} drop column b, drop column c;"
   542  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(4);"
   543  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(5,5,5);"
   544  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(6,6,6);"
   545  
   546  	run_sql_source1 "alter table ${shardddl1}.${tb1} add column (b int, c int);"
   547  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(7,7,7);"
   548  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(8,8,8);"
   549  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(9,9,9);"
   550  
   551  	run_dm_ctl_with_retry $WORK_DIR "127.0.0.1:$MASTER_PORT" \
   552  		"query-status test" \
   553  		"because schema conflict detected" 1 \
   554  		"add column b that wasn't fully dropped in downstream" 1
   555  
   556  	# try to fix data
   557  	echo 'create table tb1(a int primary key, b int, c int) engine=innodb default charset=latin1 collate=latin1_bin;' >${WORK_DIR}/schema.sql
   558  	run_dm_ctl $WORK_DIR "127.0.0.1:$MASTER_PORT" \
   559  		"binlog-schema update test ${shardddl1} ${tb1} ${WORK_DIR}/schema.sql -s mysql-replica-01" \
   560  		"\"result\": true" 2
   561  
   562  	# skip this error
   563  	run_dm_ctl_with_retry $WORK_DIR "127.0.0.1:$MASTER_PORT" \
   564  		"binlog skip test" \
   565  		"\"result\": true" 2 \
   566  		"\"source 'mysql-replica-02' has no error\"" 1
   567  
   568  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(10,10,10);"
   569  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(11,11,11);"
   570  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(12,12,12);"
   571  
   572  	run_sql_tidb "update ${shardddl}.${tb} set b=null, c=null where a=1;"
   573  	check_sync_diff $WORK_DIR $cur/conf/diff_config.toml
   574  }
   575  
   576  # Drop multiple fields and rollback.
   577  function DM_117 {
   578  	run_case 117 "double-source-optimistic" \
   579  		"run_sql_source1 \"create table ${shardddl1}.${tb1} (a int primary key, b int, c int) engine=innodb default charset=latin1 collate=latin1_bin;\"; \
   580       run_sql_source2 \"create table ${shardddl1}.${tb1} (a int primary key, b int, c int) engine=innodb default charset=latin1 collate=latin1_bin;\"; \
   581       run_sql_source2 \"create table ${shardddl1}.${tb2} (a int primary key, b int, c int) engine=innodb default charset=latin1 collate=latin1_bin;\"" \
   582  		"clean_table" "optimistic"
   583  }
   584  
   585  function DM_118_CASE {
   586  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(1,1,1,1);"
   587  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(2,2,2,2);"
   588  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(3,3,3,3);"
   589  
   590  	run_sql_source1 "alter table ${shardddl1}.${tb1} drop index idx, add index idx(c3,c1,c2);"
   591  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(4,4,4,4);"
   592  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(5,5,5,5);"
   593  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(6,6,6,6);"
   594  
   595  	run_sql_source2 "alter table ${shardddl1}.${tb1} drop index idx, add index idx(c3,c1,c2);"
   596  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(7,7,7,7);"
   597  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(8,8,8,8);"
   598  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(9,9,9,9);"
   599  
   600  	run_sql_source2 "alter table ${shardddl1}.${tb2} drop index idx, add index idx(c3,c1,c2);"
   601  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(10,10,10,10);"
   602  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(11,11,11,11);"
   603  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(12,12,12,12);"
   604  
   605  	check_sync_diff $WORK_DIR $cur/conf/diff_config.toml
   606  }
   607  
   608  # Adjust index fields combination.
   609  function DM_118 {
   610  	run_case 118 "double-source-pessimistic" \
   611  		"run_sql_source1 \"create table ${shardddl1}.${tb1} (a int primary key, c1 int, c2 int, c3 int, index idx(c1, c2, c3));\"; \
   612       run_sql_source2 \"create table ${shardddl1}.${tb1} (a int primary key, c1 int, c2 int, c3 int, index idx(c1, c2, c3));\"; \
   613       run_sql_source2 \"create table ${shardddl1}.${tb2} (a int primary key, c1 int, c2 int, c3 int, index idx(c1, c2, c3));\"" \
   614  		"clean_table" "pessimistic"
   615  	run_case 118 "double-source-optimistic" \
   616  		"run_sql_source1 \"create table ${shardddl1}.${tb1} (a int primary key, c1 int, c2 int, c3 int, index idx(c1, c2, c3));\"; \
   617       run_sql_source2 \"create table ${shardddl1}.${tb1} (a int primary key, c1 int, c2 int, c3 int, index idx(c1, c2, c3));\"; \
   618       run_sql_source2 \"create table ${shardddl1}.${tb2} (a int primary key, c1 int, c2 int, c3 int, index idx(c1, c2, c3));\"" \
   619  		"clean_table" "optimistic"
   620  }
   621  
   622  function run() {
   623  	init_cluster
   624  	init_database
   625  	start=99
   626  	end=118
   627  	for i in $(seq -f "%03g" ${start} ${end}); do
   628  		DM_${i}
   629  		sleep 1
   630  	done
   631  }
   632  
   633  cleanup_data $shardddl
   634  cleanup_data $shardddl1
   635  cleanup_data $shardddl2
   636  # also cleanup dm processes in case of last run failed
   637  cleanup_process $*
   638  run $*
   639  cleanup_process $*
   640  
   641  echo "[$(date)] <<<<<< test case $TEST_NAME success! >>>>>>"