github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/dm/tests/sharding/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  
     9  function checksum() {
    10  	read -d '' sql <<EOF
    11  SELECT BIT_XOR(CAST(CRC32(CONCAT_WS(',', uid, name, info, age, id_gen,
    12      CONCAT(ISNULL(uid), ISNULL(name), ISNULL(info), ISNULL(age), ISNULL(id_gen)))) AS UNSIGNED)) AS checksum
    13      FROM db_target.t_target WHERE (uid > 70000);
    14  EOF
    15  	run_sql "$sql" $TIDB_PORT $TIDB_PASSWORD
    16  	echo $(tail -n 1 "$TEST_DIR/sql_res.$TEST_NAME.txt")
    17  }
    18  
    19  function run() {
    20  	run_sql "SET @@GLOBAL.SQL_MODE='NO_ZERO_IN_DATE,NO_ZERO_DATE'" $MYSQL_PORT1 $MYSQL_PASSWORD1
    21  	run_sql "SET @@GLOBAL.SQL_MODE='ANSI_QUOTES'" $MYSQL_PORT2 $MYSQL_PASSWORD2
    22  
    23  	run_sql_file $cur/data/db1.prepare.sql $MYSQL_HOST1 $MYSQL_PORT1 $MYSQL_PASSWORD1
    24  	check_contains 'Query OK, 2 rows affected'
    25  	run_sql_file $cur/data/db2.prepare.sql $MYSQL_HOST2 $MYSQL_PORT2 $MYSQL_PASSWORD2
    26  	check_contains 'Query OK, 3 rows affected'
    27  
    28  	run_dm_master $WORK_DIR/master $MASTER_PORT $cur/conf/dm-master.toml
    29  	check_rpc_alive $cur/../bin/check_master_online 127.0.0.1:$MASTER_PORT
    30  	check_metric $MASTER_PORT 'start_leader_counter' 3 0 2
    31  
    32  	# operate mysql config to worker
    33  	cp $cur/conf/source1.yaml $WORK_DIR/source1.yaml
    34  	cp $cur/conf/source2.yaml $WORK_DIR/source2.yaml
    35  	sed -i "/relay-binlog-name/i\relay-dir: $WORK_DIR/worker1/relay_log" $WORK_DIR/source1.yaml
    36  	sed -i "/relay-binlog-name/i\relay-dir: $WORK_DIR/worker2/relay_log" $WORK_DIR/source2.yaml
    37  
    38  	# now, for pessimistic shard DDL, if interrupted after executed DDL but before flush checkpoint,
    39  	# re-sync this DDL will cause the source try to sync the DDL of the previous lock again,
    40  	# this will need to recover the replication manually,
    41  	# so we do not interrupt the replication after executed DDL for this test case.
    42  	#
    43  	# now, for pessimistic shard DDL, owner and non-owner will reach a stage often not at the same time,
    44  	# in order to simply the check and resume flow, only enable the failpoint for one DM-worker.
    45  	export GO_FAILPOINTS="github.com/pingcap/tiflow/dm/syncer/FlushCheckpointStage=return(2)"
    46  	run_dm_worker $WORK_DIR/worker1 $WORKER1_PORT $cur/conf/dm-worker1.toml
    47  	check_rpc_alive $cur/../bin/check_worker_online 127.0.0.1:$WORKER1_PORT
    48  	dmctl_operate_source create $WORK_DIR/source1.yaml $SOURCE_ID1
    49  	export GO_FAILPOINTS=''
    50  
    51  	run_dm_worker $WORK_DIR/worker2 $WORKER2_PORT $cur/conf/dm-worker2.toml
    52  	check_rpc_alive $cur/../bin/check_worker_online 127.0.0.1:$WORKER2_PORT
    53  	dmctl_operate_source create $WORK_DIR/source2.yaml $SOURCE_ID2
    54  
    55  	# start DM task only
    56  	dmctl_start_task "$cur/conf/dm-task.yaml" "--remove-meta"
    57  	run_dm_ctl_with_retry $WORK_DIR "127.0.0.1:$MASTER_PORT" \
    58  		"query-status test" \
    59  		"Sync" 2
    60  
    61  	check_metric $WORKER1_PORT "dm_worker_task_state{source_id=\"mysql-replica-01\",task=\"test\",worker=\"worker1\"}" 3 1 3
    62  	check_metric $WORKER2_PORT "dm_worker_task_state{source_id=\"mysql-replica-02\",task=\"test\",worker=\"worker2\"}" 3 1 3
    63  
    64  	# TODO: check sharding partition id
    65  	# use sync_diff_inspector to check full dump loader
    66  	echo "check sync diff for full dump and load"
    67  	run_sql "SET @@GLOBAL.SQL_MODE=''" $MYSQL_PORT2 $MYSQL_PASSWORD2
    68  	check_sync_diff $WORK_DIR $cur/conf/diff_config.toml
    69  
    70  	run_sql_file $cur/data/db1.increment.sql $MYSQL_HOST1 $MYSQL_PORT1 $MYSQL_PASSWORD1
    71  	run_sql_file $cur/data/db2.increment.sql $MYSQL_HOST2 $MYSQL_PORT2 $MYSQL_PASSWORD2
    72  
    73  	# the task should paused by `FlushCheckpointStage` failpoint before flush old checkpoint.
    74  	run_dm_ctl_with_retry $WORK_DIR "127.0.0.1:$MASTER_PORT" \
    75  		"query-status test" \
    76  		"failpoint error for FlushCheckpointStage before flush old checkpoint" 1
    77  	# worker1 will failed and worker2 will still running.
    78  	check_metric $WORKER1_PORT "dm_worker_task_state{source_id=\"mysql-replica-01\",task=\"test\",worker=\"worker1\"}" 3 2 4
    79  	check_metric $WORKER2_PORT "dm_worker_task_state{source_id=\"mysql-replica-02\",task=\"test\",worker=\"worker2\"}" 3 1 3
    80  
    81  	# resume-task to next stage
    82  	run_dm_ctl $WORK_DIR "127.0.0.1:$MASTER_PORT" \
    83  		"resume-task test"
    84  
    85  	run_dm_ctl_with_retry $WORK_DIR "127.0.0.1:$MASTER_PORT" \
    86  		"query-status test" \
    87  		"failpoint error for FlushCheckpointStage before track DDL" 1
    88  
    89  	# resume-task to next stage
    90  	run_dm_ctl $WORK_DIR "127.0.0.1:$MASTER_PORT" \
    91  		"resume-task test"
    92  
    93  	run_dm_ctl_with_retry $WORK_DIR "127.0.0.1:$MASTER_PORT" \
    94  		"query-status test" \
    95  		"failpoint error for FlushCheckpointStage before execute DDL" 1
    96  
    97  	# resume-task to next stage
    98  	run_dm_ctl $WORK_DIR "127.0.0.1:$MASTER_PORT" \
    99  		"resume-task test"
   100  
   101  	# TODO: check sharding partition id
   102  	# use sync_diff_inspector to check data now!
   103  	echo "check sync diff for the first increment replication"
   104  	check_sync_diff $WORK_DIR $cur/conf/diff_config.toml
   105  
   106  	# test create database, create table in sharding mode
   107  	run_sql_file $cur/data/db1.increment2.sql $MYSQL_HOST1 $MYSQL_PORT1 $MYSQL_PASSWORD1
   108  	run_sql_file $cur/data/db2.increment2.sql $MYSQL_HOST2 $MYSQL_PORT2 $MYSQL_PASSWORD2
   109  	cp $cur/conf/diff_config.toml $WORK_DIR/diff_config.toml
   110  	sed "s/sharding1\"#pattern1/sharding[1-2]\"/g" $WORK_DIR/diff_config.toml | sed "s/sharding1\"#pattern2/sharding[1-2]\"/g" >$WORK_DIR/diff_config_2.toml
   111  	echo "check sync diff for the second increment replication"
   112  	check_sync_diff $WORK_DIR $WORK_DIR/diff_config_2.toml
   113  
   114  	old_checksum=$(checksum)
   115  
   116  	# test drop table, drop database, truncate table in sharding mode
   117  	run_sql_file $cur/data/db1.increment3.sql $MYSQL_HOST1 $MYSQL_PORT1 $MYSQL_PASSWORD1
   118  	run_sql_file $cur/data/db2.increment3.sql $MYSQL_HOST2 $MYSQL_PORT2 $MYSQL_PASSWORD2
   119  	cp $cur/conf/diff_config.toml $WORK_DIR/diff_config.toml
   120  	sed "s/sharding1\"#pattern1/sharding[1-2]\"/g" $WORK_DIR/diff_config.toml >$WORK_DIR/diff_config_2.toml
   121  	sed -i "s/^# range-placeholder/range = \"uid < 70000\"/g" $WORK_DIR/diff_config_2.toml
   122  	echo "check sync diff for the third increment replication"
   123  	check_sync_diff $WORK_DIR $WORK_DIR/diff_config_2.toml
   124  
   125  	new_checksum=$(checksum)
   126  	echo "checksum before drop/truncate: $old_checksum, checksum after drop/truncate: $new_checksum"
   127  	[ "$old_checksum" == "$new_checksum" ]
   128  
   129  	# test conflict ddl in single worker
   130  	run_sql "alter table sharding1.t1 add column new_col1 int;" $MYSQL_PORT1 $MYSQL_PASSWORD1
   131  	run_sql "alter table sharding1.t2 add column new_col2 int;" $MYSQL_PORT1 $MYSQL_PASSWORD1
   132  
   133  	run_dm_ctl_with_retry $WORK_DIR "127.0.0.1:$MASTER_PORT" \
   134  		"query-status test" \
   135  		"detect inconsistent DDL sequence" 1
   136  
   137  	run_dm_ctl $WORK_DIR "127.0.0.1:$MASTER_PORT" \
   138  		"resume-task test"
   139  
   140  	# still conflict
   141  	run_dm_ctl_with_retry $WORK_DIR "127.0.0.1:$MASTER_PORT" \
   142  		"query-status test" \
   143  		"detect inconsistent DDL sequence" 1
   144  
   145  	# stop twice, just used to test stop by the way
   146  	run_dm_ctl $WORK_DIR "127.0.0.1:$MASTER_PORT" \
   147  		"stop-task test" \
   148  		"\"result\": true" 3
   149  	run_dm_ctl_with_retry $WORK_DIR "127.0.0.1:$MASTER_PORT" \
   150  		"stop-task test" \
   151  		"task test has no source or not exist" 1
   152  
   153  	check_metric_not_contains $WORKER1_PORT "dm_worker_task_state{source_id=\"mysql-replica-01\",task=\"test\",worker=\"worker1\"}" 3
   154  	check_metric_not_contains $WORKER2_PORT "dm_worker_task_state{source_id=\"mysql-replica-02\",task=\"test\",worker=\"worker2\"}" 3
   155  
   156  	run_sql_both_source "SET @@GLOBAL.SQL_MODE='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'"
   157  }
   158  
   159  cleanup_data db_target
   160  # also cleanup dm processes in case of last run failed
   161  cleanup_process $*
   162  run $*
   163  cleanup_process $*
   164  
   165  echo "[$(date)] <<<<<< test case $TEST_NAME success! >>>>>>"