github.com/pingcap/ticdc@v0.0.0-20220526033649-485a10ef2652/tests/syncpoint/run.sh (about)

     1  #!/bin/bash
     2  
     3  set -e
     4  
     5  CUR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
     6  source $CUR/../_utils/test_prepare
     7  WORK_DIR=$OUT_DIR/$TEST_NAME
     8  CDC_BINARY=cdc.test
     9  SINK_TYPE=$1
    10  
    11  CDC_COUNT=3
    12  DB_COUNT=4
    13  
    14  # The follow `sleep 2` make sure the ddl and dml operation can always execute during the test.
    15  # So every syncpoint will have different data and the diff tool can judge the syncpoint's validity.
    16  function ddl() {
    17      run_sql "DROP table IF EXISTS testSync.simple1"
    18      sleep 2
    19      run_sql "DROP table IF EXISTS testSync.simple2"
    20      sleep 2
    21      run_sql "CREATE table testSync.simple1(id int primary key, val int);"
    22      sleep 2
    23      run_sql "INSERT INTO testSync.simple1(id, val) VALUES (1, 1);"
    24      sleep 2
    25      run_sql "INSERT INTO testSync.simple1(id, val) VALUES (2, 2);"
    26      sleep 2
    27      run_sql "INSERT INTO testSync.simple1(id, val) VALUES (3, 3);"
    28      sleep 2
    29      run_sql "INSERT INTO testSync.simple1(id, val) VALUES (4, 4);"
    30      sleep 2
    31      run_sql "INSERT INTO testSync.simple1(id, val) VALUES (5, 5);"
    32      sleep 2
    33      run_sql "INSERT INTO testSync.simple1(id, val) VALUES (6, 6);"
    34      sleep 2
    35      run_sql "CREATE table testSync.simple2(id int primary key, val int);"
    36      sleep 2
    37      run_sql "INSERT INTO testSync.simple2(id, val) VALUES (1, 1);"
    38      sleep 2
    39      run_sql "INSERT INTO testSync.simple2(id, val) VALUES (2, 2);"
    40      sleep 2
    41      run_sql "INSERT INTO testSync.simple2(id, val) VALUES (3, 3);"
    42      sleep 2
    43      run_sql "INSERT INTO testSync.simple2(id, val) VALUES (4, 4);"
    44      sleep 2
    45      run_sql "INSERT INTO testSync.simple2(id, val) VALUES (5, 5);"
    46      sleep 2
    47      run_sql "INSERT INTO testSync.simple2(id, val) VALUES (6, 6);"
    48      sleep 2
    49      run_sql "CREATE index simple1_val ON testSync.simple1(val);"
    50      sleep 2
    51      run_sql "CREATE index simple2_val ON testSync.simple2(val);"
    52      sleep 2
    53      run_sql "DELETE FROM testSync.simple1 where id=1;"
    54      sleep 2
    55      run_sql "DELETE FROM testSync.simple2 where id=1;"
    56      sleep 2
    57      run_sql "DELETE FROM testSync.simple1 where id=2;"
    58      sleep 2
    59      run_sql "DELETE FROM testSync.simple2 where id=2;"
    60      sleep 2
    61      run_sql "DROP index simple1_val ON testSync.simple1;"
    62      sleep 2
    63      run_sql "DROP index simple2_val ON testSync.simple2;"
    64      sleep 2
    65  }
    66  
    67  function goSql() {
    68      for i in {1..3}
    69      do
    70          go-ycsb load mysql -P $CUR/conf/workload -p mysql.host=${UP_TIDB_HOST} -p mysql.port=${UP_TIDB_PORT} -p mysql.user=root -p mysql.db=testSync
    71          sleep 2
    72          ddl
    73          sleep 2
    74      done
    75  }
    76  
    77  function deployConfig() {
    78      cat $CUR/conf/diff_config_part1.toml > $CUR/conf/diff_config.toml
    79      echo "snapshot = \"$1\"" >> $CUR/conf/diff_config.toml
    80      cat $CUR/conf/diff_config_part2.toml >> $CUR/conf/diff_config.toml
    81      echo "snapshot = \"$2\"" >> $CUR/conf/diff_config.toml
    82  }
    83  
    84  # check wheter the given tso happens in a DDL job, if true returns the start_ts
    85  # and commit_ts of the DDL job
    86  function checkPrimaryTsNotInDDL() {
    87      primary_ts=$1
    88      tsos=$2
    89      count=$(( ${#tsos[@]} / 2 ))
    90      for i in $(seq 1 $count); do
    91          start_ts=${tsos[(( 2 * $i - 2 ))]}
    92          commit_ts=${tsos[(( 2 * $i - 1 ))]}
    93          if [[ ($primary_ts > $start_ts) && ($primary_ts < $commit_ts) ]]; then
    94              echo "$start_ts $commit_ts"
    95          fi
    96      done
    97  }
    98  
    99  function checkDiff() {
   100      primaryArr=(`grep primary_ts $OUT_DIR/sql_res.$TEST_NAME.txt | awk -F ": " '{print $2}'`)
   101      secondaryArr=(`grep secondary_ts $OUT_DIR/sql_res.$TEST_NAME.txt | awk -F ": " '{print $2}'`)
   102      tsos=($(curl -s http://$UP_TIDB_HOST:$UP_TIDB_STATUS/ddl/history |grep -E "start_ts|FinishedTS"|grep -oE "[0-9]*"))
   103      num=${#primaryArr[*]}
   104      for ((i=0;i<$num;i++))
   105      do
   106          check_in_ddl=$(checkPrimaryTsNotInDDL ${primaryArr[$i]} $tsos)
   107          if [[ ! -z $check_in_ddl ]]; then
   108              echo "syncpoint ${primaryArr[$i]} ${secondaryArr[$i]} " \
   109                  "is recorded in a DDL event(${check_in_ddl[0]}), skip the check of it"
   110              continue
   111          fi
   112          deployConfig ${primaryArr[$i]} ${secondaryArr[$i]}
   113          check_sync_diff $WORK_DIR $CUR/conf/diff_config.toml
   114      done
   115      rm $CUR/conf/diff_config.toml
   116  }
   117  
   118  function run() {
   119      if [ "$SINK_TYPE" != "mysql" ]; then
   120          echo "kafka downstream isn't support syncpoint record"
   121          return 
   122      fi
   123  
   124      rm -rf $WORK_DIR && mkdir -p $WORK_DIR
   125  
   126      start_tidb_cluster --workdir $WORK_DIR
   127  
   128      cd $WORK_DIR
   129  
   130      start_ts=$(run_cdc_cli tso query --pd=http://$UP_PD_HOST_1:$UP_PD_PORT_1)
   131      run_sql "CREATE DATABASE testSync;"
   132      run_cdc_server --workdir $WORK_DIR --binary $CDC_BINARY
   133  
   134      
   135      SINK_URI="mysql://normal:123456@127.0.0.1:3306/?max-txn-row=1"
   136      run_cdc_cli changefeed create --start-ts=$start_ts --sink-uri="$SINK_URI" --sync-point --sync-interval=10s
   137  
   138      goSql
   139  
   140      check_table_exists "testSync.USERTABLE" ${DOWN_TIDB_HOST} ${DOWN_TIDB_PORT}
   141      check_table_exists "testSync.simple1" ${DOWN_TIDB_HOST} ${DOWN_TIDB_PORT}
   142      check_table_exists "testSync.simple2" ${DOWN_TIDB_HOST} ${DOWN_TIDB_PORT}
   143  
   144      sleep 60
   145  
   146      run_sql "SELECT primary_ts, secondary_ts FROM tidb_cdc.syncpoint_v1;" ${DOWN_TIDB_HOST} ${DOWN_TIDB_PORT}
   147      echo "____________________________________"
   148      cat "$OUT_DIR/sql_res.$TEST_NAME.txt"
   149      checkDiff
   150      check_sync_diff $WORK_DIR $CUR/conf/diff_config_final.toml
   151  
   152      cleanup_process $CDC_BINARY
   153  }
   154  
   155  trap stop_tidb_cluster EXIT
   156  run $*
   157  check_logs $WORK_DIR
   158  echo "[$(date)] <<<<<< run test case $TEST_NAME success! >>>>>>"