github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/dm/tests/sequence_sharding_optimistic/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 task_name="sequence_sharding_optimistic" 9 10 API_URL="127.0.0.1:${MASTER_PORT}/apis/v1alpha1/schema" 11 12 run() { 13 run_sql_file $cur/data/db1.prepare.sql $MYSQL_HOST1 $MYSQL_PORT1 $MYSQL_PASSWORD1 14 run_sql_file $cur/data/db2.prepare.sql $MYSQL_HOST2 $MYSQL_PORT2 $MYSQL_PASSWORD2 15 16 run_dm_master $WORK_DIR/master $MASTER_PORT $cur/conf/dm-master.toml 17 check_rpc_alive $cur/../bin/check_master_online 127.0.0.1:$MASTER_PORT 18 run_dm_worker $WORK_DIR/worker1 $WORKER1_PORT $cur/conf/dm-worker1.toml 19 check_rpc_alive $cur/../bin/check_worker_online 127.0.0.1:$WORKER1_PORT 20 21 run_dm_worker $WORK_DIR/worker2 $WORKER2_PORT $cur/conf/dm-worker2.toml 22 check_rpc_alive $cur/../bin/check_worker_online 127.0.0.1:$WORKER2_PORT 23 24 # try to get schema for the table, but the worker instance not started. 25 curl -X PUT ${API_URL} -d '{"op":1, "task":"sequence_sharding_optimistic", "sources": ["mysql-replica-01"], "database":"sharding_seq_opt", "table":"t1"}' >${WORK_DIR}/get_schema.log 26 check_log_contains ${WORK_DIR}/get_schema.log "mysql-replica-01 relevant worker-client not found" 1 27 28 # operate mysql config to worker 29 cp $cur/conf/source1.yaml $WORK_DIR/source1.yaml 30 cp $cur/conf/source2.yaml $WORK_DIR/source2.yaml 31 sed -i "/relay-binlog-name/i\relay-dir: $WORK_DIR/worker1/relay_log" $WORK_DIR/source1.yaml 32 sed -i "/relay-binlog-name/i\relay-dir: $WORK_DIR/worker2/relay_log" $WORK_DIR/source2.yaml 33 dmctl_operate_source create $WORK_DIR/source1.yaml $SOURCE_ID1 34 dmctl_operate_source create $WORK_DIR/source2.yaml $SOURCE_ID2 35 36 # try to get schema for the table, the subtask has not started. 37 curl -X PUT ${API_URL} -d '{"op":1, "task":"sequence_sharding_optimistic", "sources": ["mysql-replica-01"], "database":"sharding_seq_opt", "table":"t1"}' >${WORK_DIR}/get_schema.log 38 check_log_contains ${WORK_DIR}/get_schema.log "sub task with name sequence_sharding_optimistic not found" 1 39 40 # start DM task only 41 dmctl_start_task "$cur/conf/dm-task.yaml" "--remove-meta" 42 43 # use sync_diff_inspector to check full dump loader 44 check_sync_diff $WORK_DIR $cur/conf/diff_config.toml 45 46 # test create and alter database ddl 47 run_sql_file $cur/data/db1.increment0.sql $MYSQL_HOST1 $MYSQL_PORT1 $MYSQL_PASSWORD1 48 run_sql_file $cur/data/db2.increment0.sql $MYSQL_HOST2 $MYSQL_PORT2 $MYSQL_PASSWORD2 49 50 # check database `sharding_seq_tmp` exists 51 run_sql_tidb_with_retry "select count(*) from sharding_seq_tmp.t1;" "count(*): 1" 52 53 # try to get schema for the table, but the stage is not paused. 54 curl -X PUT ${API_URL} -d '{"op":1, "task":"sequence_sharding_optimistic", "sources": ["mysql-replica-01"], "database":"sharding_seq_opt", "table":"t1"}' >${WORK_DIR}/get_schema.log 55 check_log_contains ${WORK_DIR}/get_schema.log "current stage is Running but not paused, invalid" 1 56 57 # pause task manually. 58 run_dm_ctl_with_retry $WORK_DIR "127.0.0.1:$MASTER_PORT" \ 59 "pause-task $task_name" \ 60 "\"result\": true" 3 61 run_dm_ctl_with_retry $WORK_DIR "127.0.0.1:$MASTER_PORT" \ 62 "query-status $task_name" \ 63 "\"stage\": \"Paused\"" 2 64 65 # try to get schema for the table, table exists for optimistic. 66 curl -X PUT ${API_URL} -d '{"op":1, "task":"sequence_sharding_optimistic", "sources": ["mysql-replica-01"], "database":"sharding_seq_opt", "table":"t1"}' >${WORK_DIR}/get_schema.log 67 check_log_contains ${WORK_DIR}/get_schema.log 'CREATE TABLE `t1` ( `id` bigint(20) NOT NULL, `c1` varchar(20) DEFAULT NULL, `c2` varchar(20) DEFAULT NULL, PRIMARY KEY (`id`) .*) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_bin' 1 68 69 # resume task manually. 70 run_dm_ctl_with_retry $WORK_DIR "127.0.0.1:$MASTER_PORT" \ 71 "resume-task $task_name" \ 72 "\"result\": true" 3 73 run_dm_ctl_with_retry $WORK_DIR "127.0.0.1:$MASTER_PORT" \ 74 "query-status $task_name" \ 75 "\"stage\": \"Running\"" 3 76 77 # now, for optimistic shard DDL, different sources will reach a stage often not at the same time, 78 # in order to simply the check and resume flow, only enable the failpoint for one DM-worker. 79 export GO_FAILPOINTS="github.com/pingcap/tiflow/dm/syncer/FlushCheckpointStage=return(100)" # for all stages 80 echo "restart dm-worker1" 81 kill_process dm-worker1 82 check_port_offline $WORKER1_PORT 20 83 run_dm_worker $WORK_DIR/worker1 $WORKER1_PORT $cur/conf/dm-worker1.toml 84 check_rpc_alive $cur/../bin/check_worker_online 127.0.0.1:$WORKER1_PORT 85 export GO_FAILPOINTS='' 86 87 run_dm_ctl $WORK_DIR "127.0.0.1:$MASTER_PORT" \ 88 "shard-ddl-lock unlock non-exist-task-\`test_db\`.\`test_table\`" \ 89 "lock with ID non-exist-task-\`test_db\`.\`test_table\` not found" 1 90 91 run_sql_file $cur/data/db1.increment.sql $MYSQL_HOST1 $MYSQL_PORT1 $MYSQL_PASSWORD1 92 run_sql_file $cur/data/db2.increment.sql $MYSQL_HOST2 $MYSQL_PORT2 $MYSQL_PASSWORD2 93 94 # the task should paused by `FlushCheckpointStage` failpont before flush old checkpoint. 95 run_dm_ctl_with_retry $WORK_DIR "127.0.0.1:$MASTER_PORT" \ 96 "query-status $task_name" \ 97 "failpoint error for FlushCheckpointStage before flush old checkpoint" 1 98 99 # resume-task to next stage 100 run_dm_ctl $WORK_DIR "127.0.0.1:$MASTER_PORT" \ 101 "resume-task $task_name" 102 103 run_dm_ctl_with_retry $WORK_DIR "127.0.0.1:$MASTER_PORT" \ 104 "query-status $task_name" \ 105 "failpoint error for FlushCheckpointStage before track DDL" 1 106 107 # resume-task to next stage 108 run_dm_ctl $WORK_DIR "127.0.0.1:$MASTER_PORT" \ 109 "resume-task $task_name" 110 111 run_dm_ctl_with_retry $WORK_DIR "127.0.0.1:$MASTER_PORT" \ 112 "query-status $task_name" \ 113 "failpoint error for FlushCheckpointStage before execute DDL" 1 114 115 # resume-task to next stage 116 run_dm_ctl $WORK_DIR "127.0.0.1:$MASTER_PORT" \ 117 "resume-task $task_name" 118 119 run_dm_ctl_with_retry $WORK_DIR "127.0.0.1:$MASTER_PORT" \ 120 "query-status $task_name" \ 121 "failpoint error for FlushCheckpointStage before save checkpoint" 1 122 123 # resume-task to next stage 124 run_dm_ctl $WORK_DIR "127.0.0.1:$MASTER_PORT" \ 125 "resume-task $task_name" 126 127 run_dm_ctl_with_retry $WORK_DIR "127.0.0.1:$MASTER_PORT" \ 128 "query-status $task_name" \ 129 "failpoint error for FlushCheckpointStage before flush checkpoint" 1 130 131 # resume-task to continue the sync 132 run_dm_ctl_with_retry $WORK_DIR "127.0.0.1:$MASTER_PORT" \ 133 "resume-task $task_name" \ 134 "\"result\": true" 3 135 136 # use sync_diff_inspector to check data now! 137 check_sync_diff $WORK_DIR $cur/conf/diff_config.toml 138 139 # pause task manually again. 140 run_dm_ctl_with_retry $WORK_DIR "127.0.0.1:$MASTER_PORT" \ 141 "pause-task $task_name" \ 142 "\"result\": true" 3 143 run_dm_ctl_with_retry $WORK_DIR "127.0.0.1:$MASTER_PORT" \ 144 "query-status $task_name" \ 145 "\"stage\": \"Paused\"" 2 146 147 # try to get schema for the table, the latest schema got. 148 curl -X PUT ${API_URL} -d '{"op":1, "task":"sequence_sharding_optimistic", "sources": ["mysql-replica-01"], "database":"sharding_seq_opt", "table":"t1"}' >${WORK_DIR}/get_schema.log 149 # this is NON-CLUSTERED index 150 check_log_contains ${WORK_DIR}/get_schema.log 'CREATE TABLE `t1` ( `id` bigint(20) NOT NULL, `c2` varchar(20) DEFAULT NULL, `c3` int(11) DEFAULT NULL, PRIMARY KEY (`id`) .*) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_bin' 1 151 152 # drop the schema. 153 curl -X PUT ${API_URL} -d '{"op":3, "task":"sequence_sharding_optimistic", "sources": ["mysql-replica-01"], "database":"sharding_seq_opt", "table":"t1"}' >${WORK_DIR}/remove_schema.log 154 155 # try to set an invalid schema. 156 curl -X PUT ${API_URL} -d '{"op":2, "task":"sequence_sharding_optimistic", "sources": ["mysql-replica-01"], "database":"sharding_seq_opt", "table":"t1", "schema":"invalid create table statement"}' >${WORK_DIR}/get_schema.log >${WORK_DIR}/set_schema.log 157 check_log_contains ${WORK_DIR}/set_schema.log 'is not a valid `CREATE TABLE` statement' 1 158 159 # try to get schema again, still the old schema. 160 curl -X PUT ${API_URL} -d '{"op":1, "task":"sequence_sharding_optimistic", "sources": ["mysql-replica-01"], "database":"sharding_seq_opt", "table":"t1"}' >${WORK_DIR}/get_schema.log 161 check_log_contains ${WORK_DIR}/get_schema.log 'CREATE TABLE `t1` ( `id` bigint(20) NOT NULL, `c2` varchar(20) DEFAULT NULL, `c3` int(11) DEFAULT NULL, PRIMARY KEY (`id`) .*) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_bin' 1 162 163 run_dm_ctl $WORK_DIR "127.0.0.1:$MASTER_PORT" \ 164 "binlog-schema list -s mysql-replica-01,mysql-replica-02 sequence_sharding_optimistic sharding_seq_opt t2" \ 165 "\"result\": true" 3 166 167 run_dm_ctl $WORK_DIR "127.0.0.1:$MASTER_PORT" \ 168 "binlog-schema delete -s mysql-replica-01 sequence_sharding_optimistic sharding_seq_opt t2" \ 169 "\"result\": true" 2 170 171 # try to set another schema, `c3` `int` -> `bigint`. 172 echo 'CREATE TABLE `t1` ( `id` bigint(20) NOT NULL, `c2` varchar(20) DEFAULT NULL, `c3` bigint(11) DEFAULT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_bin' >${WORK_DIR}/schema.sql 173 run_dm_ctl $WORK_DIR "127.0.0.1:$MASTER_PORT" \ 174 "binlog-schema update -s mysql-replica-01 sequence_sharding_optimistic sharding_seq_opt t1 ${WORK_DIR}/schema.sql" \ 175 "\"result\": true" 2 176 177 # try to get schema again, the new one got. 178 curl -X PUT ${API_URL} -d '{"op":1, "task":"sequence_sharding_optimistic", "sources": ["mysql-replica-01"], "database":"sharding_seq_opt", "table":"t1"}' >${WORK_DIR}/get_schema.log 179 cat ${WORK_DIR}/get_schema.log 180 # schema tracker enables alter-primary-key, so this is NONCLUSTERED index 181 check_log_contains ${WORK_DIR}/get_schema.log 'CREATE TABLE `t1` ( `id` bigint(20) NOT NULL, `c2` varchar(20) DEFAULT NULL, `c3` bigint(11) DEFAULT NULL, PRIMARY KEY (`id`) .*) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_bin' 1 182 183 # more data 184 run_sql_file $cur/data/db1.increment2.sql $MYSQL_HOST1 $MYSQL_PORT1 $MYSQL_PASSWORD1 185 run_sql_file $cur/data/db2.increment2.sql $MYSQL_HOST2 $MYSQL_PORT2 $MYSQL_PASSWORD2 186 187 # resume task manually again. 188 run_dm_ctl_with_retry $WORK_DIR "127.0.0.1:$MASTER_PORT" \ 189 "resume-task $task_name" \ 190 "\"result\": true" 3 191 run_dm_ctl_with_retry $WORK_DIR "127.0.0.1:$MASTER_PORT" \ 192 "query-status $task_name" \ 193 "\"stage\": \"Running\"" 3 194 195 # use sync_diff_inspector to check data now! 196 check_sync_diff $WORK_DIR $cur/conf/diff_config.toml 197 } 198 199 cleanup_data sharding_target_opt 200 cleanup_data sharding_seq_tmp 201 # also cleanup dm processes in case of last run failed 202 cleanup_process $* 203 run $* 204 cleanup_process $* 205 206 echo "[$(date)] <<<<<< test case $TEST_NAME success! >>>>>>"