github.com/pingcap/ticdc@v0.0.0-20220526033649-485a10ef2652/tests/ddl_reentrant/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  ddls=("create database ddl_reentrant" false
    12        "create table ddl_reentrant.t1 (id int primary key, id2 int not null, a varchar(10) not null, unique a(a), unique id2(id2))" false
    13        "alter table ddl_reentrant.t1 add column b int" false
    14        "alter table ddl_reentrant.t1 drop column b" false
    15        "alter table ddl_reentrant.t1 add key index_a(a)" false
    16        "alter table ddl_reentrant.t1 drop index index_a" false
    17        "truncate table ddl_reentrant.t1" true
    18        "alter table ddl_reentrant.t1 modify a varchar(20)" true
    19        "rename table ddl_reentrant.t1 to ddl_reentrant.t2" false
    20        "alter table ddl_reentrant.t2 alter a set default 'hello'" true
    21        "alter table ddl_reentrant.t2 comment='modify comment'" true
    22        "alter table ddl_reentrant.t2 rename index a to idx_a" false
    23        "create table ddl_reentrant.t3 (a int primary key, b int) partition by range(a) (partition p0 values less than (1000), partition p1 values less than (2000))" false
    24        "alter table ddl_reentrant.t3 add partition (partition p2 values less than (3000))" false
    25        "alter table ddl_reentrant.t3 drop partition p2" false
    26        "alter table ddl_reentrant.t3 truncate partition p0" true
    27        "create view ddl_reentrant.t3_view as select a, b from ddl_reentrant.t3" false
    28        "drop view ddl_reentrant.t3_view" false
    29        "alter table ddl_reentrant.t3 default character set utf8mb4 default collate utf8mb4_unicode_ci" true
    30        "alter schema ddl_reentrant default character set utf8mb4 default collate utf8mb4_unicode_ci" true
    31  )
    32  
    33  function complete_ddls() {
    34      # TODO: refine the release detection after 5.0 tag of TiDB is ready
    35      if [[ ! $tidb_build_branch =~ master ]]; then
    36          echo "skip some DDLs in tidb v4.0.x"
    37      else
    38          # DDLs that are supportted since 5.0
    39          ddls+=( "alter table ddl_reentrant.t2 add column c1 int, add column c2 int, add column c3 int" false )
    40          ddls+=( "alter table ddl_reentrant.t2 drop column c1, drop column c2, drop column c3" false )
    41      fi
    42      ddls+=( "alter table ddl_reentrant.t2 drop primary key" false )
    43      ddls+=( "alter table ddl_reentrant.t2 add primary key pk(id)" false )
    44      ddls+=( "drop table ddl_reentrant.t2" false )
    45      ddls+=( "recover table ddl_reentrant.t2" false )
    46      ddls+=( "drop database ddl_reentrant" false )
    47  }
    48  
    49  changefeedid=""
    50  # this test contains `recover table`, which requires super privilege, so we
    51  # can't use the normal user
    52  SINK_URI="mysql://root@127.0.0.1:3306/"
    53  
    54  function check_ts_forward() {
    55      changefeedid=$1
    56      rts1=$(cdc cli changefeed query --changefeed-id=${changefeedid} 2>&1|jq '.status."resolved-ts"')
    57      checkpoint1=$(cdc cli changefeed query --changefeed-id=${changefeedid} 2>&1|jq '.status."checkpoint-ts"')
    58      sleep 1
    59      rts2=$(cdc cli changefeed query --changefeed-id=${changefeedid} 2>&1|jq '.status."resolved-ts"')
    60      checkpoint2=$(cdc cli changefeed query --changefeed-id=${changefeedid} 2>&1|jq '.status."checkpoint-ts"')
    61      if [[ "$rts1" != "null" ]] && [[ "$rts1" != "0" ]]; then
    62          if [[  "$rts1" -ne "$rts2" ]] || [[ "$checkpoint1" -ne "$checkpoint2" ]]; then
    63              echo "changefeed is working normally rts: ${rts1}->${rts2} checkpoint: ${checkpoint1}->${checkpoint2}"
    64              return
    65          fi
    66      fi
    67      exit 1
    68  }
    69  
    70  function check_ddl_executed() {
    71      log_file="$1"
    72      ddl=$(cat $2)
    73      success="$3"
    74      if [[ $success == "true" ]]; then
    75          key_word="Exec DDL succeeded"
    76      else
    77          key_word="execute DDL failed, but error can be ignored"
    78      fi
    79      log=$(grep "${key_word}" ${log_file}|tail -n 1)
    80      if [[ $log == *"${ddl}"* ]]; then
    81          echo $log
    82          return
    83      else
    84          exit 1
    85      fi
    86  }
    87  
    88  export -f check_ts_forward
    89  export -f check_ddl_executed
    90  
    91  tidb_build_branch=$(mysql -uroot -h${UP_TIDB_HOST} -P${UP_TIDB_PORT} -e \
    92      "select tidb_version()\G"|grep "Git Branch"|awk -F: '{print $(NF)}'|tr -d " ")
    93  
    94  function ddl_test() {
    95      ddl=$1
    96      is_reentrant=$2
    97  
    98      echo "------------------------------------------"
    99      echo "test ddl $ddl, is_reentrant: $is_reentrant"
   100  
   101      run_sql $ddl ${UP_TIDB_HOST} ${UP_TIDB_PORT}
   102      ensure 10 check_ts_forward $changefeedid
   103  
   104      echo $ddl > ${WORK_DIR}/ddl_temp.sql
   105      ensure 10 check_ddl_executed "${WORK_DIR}/cdc.log" "${WORK_DIR}/ddl_temp.sql" true
   106      ddl_start_ts=$(grep "Execute DDL succeeded" ${WORK_DIR}/cdc.log|tail -n 1|grep -oE '"StartTs\\":[0-9]{18}'|awk -F: '{print $(NF)}')
   107      cdc cli changefeed remove --changefeed-id=${changefeedid}
   108      changefeedid=$(cdc cli changefeed create --no-confirm --start-ts=${ddl_start_ts} --sink-uri="$SINK_URI" 2>&1|tail -n2|head -n1|awk '{print $2}')
   109      echo "create new changefeed ${changefeedid} from ${ddl_start_ts}"
   110      ensure 10 check_ts_forward $changefeedid
   111      ensure 10 check_ddl_executed "${WORK_DIR}/cdc.log" "${WORK_DIR}/ddl_temp.sql" $is_reentrant
   112  }
   113  
   114  function run() {
   115      # don't test kafka in this case
   116      if [ "$SINK_TYPE" == "kafka" ]; then
   117        return
   118      fi
   119  
   120      rm -rf $WORK_DIR && mkdir -p $WORK_DIR
   121  
   122      start_tidb_cluster --workdir $WORK_DIR --tidb-config $CUR/conf/tidb_config.toml
   123  
   124      complete_ddls
   125      # TODO: refine the release detection after 5.0 tag of TiDB is ready
   126      if [[ $tidb_build_branch =~ master ]]; then
   127          # https://github.com/pingcap/tidb/pull/21533 disables multi_schema change
   128          # feature by default, turn it on first
   129          run_sql "set global tidb_enable_change_multi_schema = on" ${UP_TIDB_HOST} ${UP_TIDB_PORT}
   130          # This must be set before cdc server starts
   131          run_sql "set global tidb_enable_change_multi_schema = on" ${DOWN_TIDB_HOST} ${DOWN_TIDB_PORT}
   132          # TiDB global variables cache 2 seconds at most
   133          sleep 2
   134      fi
   135  
   136      cd $WORK_DIR
   137  
   138      run_cdc_server --workdir $WORK_DIR --binary $CDC_BINARY
   139      changefeedid=$(cdc cli changefeed create --sink-uri="$SINK_URI" 2>&1|tail -n2|head -n1|awk '{print $2}')
   140  
   141      OLDIFS=$IFS
   142      IFS=""
   143      idx=0
   144      while [ $idx -lt ${#ddls[*]} ]; do
   145          ddl=${ddls[$idx]}
   146          idx=$((idx+1))
   147          idxs_reentrant=${ddls[$idx]}
   148          idx=$((idx+1))
   149          ddl_test $ddl $idxs_reentrant
   150      done
   151      IFS=$OLDIFS
   152  
   153      cleanup_process $CDC_BINARY
   154  }
   155  
   156  trap stop_tidb_cluster EXIT
   157  run $*
   158  check_logs $WORK_DIR
   159  echo "[$(date)] <<<<<< run test case $TEST_NAME success! >>>>>>"