github.com/pingcap/ticdc@v0.0.0-20220526033649-485a10ef2652/tests/gc_safepoint/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  MAX_RETRIES=10
    11  
    12  function get_safepoint() {
    13      pd_addr=$1
    14      pd_cluster_id=$2
    15      safe_point=$(ETCDCTL_API=3 etcdctl --endpoints=$pd_addr get /pd/$pd_cluster_id/gc/safe_point/service/ticdc|grep -oE "safe_point\":[0-9]+"|grep -oE "[0-9]+")
    16      echo $safe_point
    17  }
    18  
    19  function check_safepoint_cleared() {
    20      pd_addr=$1
    21      pd_cluster_id=$2
    22      query=$(ETCDCTL_API=3 etcdctl --endpoints=$pd_addr get /pd/$pd_cluster_id/gc/safe_point/service/ticdc)
    23      if [ ! -z "$query" ]; then
    24          echo "gc safepoint is not cleared: $query"
    25      fi
    26  }
    27  
    28  function check_safepoint_forward() {
    29      pd_addr=$1
    30      pd_cluster_id=$2
    31      safe_point1=$3
    32      sleep 1
    33      safe_point2=$(get_safepoint $pd_addr $pd_cluster_id)
    34      if [[ "$safe_point1" == "$safe_point2" ]]; then
    35          echo "safepoint $safe_point1 is not forward"
    36          exit 1
    37      fi
    38  }
    39  
    40  function check_safepoint_equal() {
    41      pd_addr=$1
    42      pd_cluster_id=$2
    43      safe_point1=$(get_safepoint $pd_addr $pd_cluster_id)
    44      for i in $(seq 1 3); do
    45          sleep 1
    46          safe_point2=$(get_safepoint $pd_addr $pd_cluster_id)
    47          if [[ "$safe_point1" != "$safe_point2" ]]; then
    48              echo "safepoint is unexpected forward: $safe_point1 -> $safe_point2"
    49              exit 1
    50          fi
    51      done
    52  }
    53  
    54  function check_changefeed_state() {
    55      pd_addr=$1
    56      changefeed_id=$2
    57      expected=$3
    58      state=$(cdc cli --pd=$pd_addr changefeed query -s -c $changefeed_id|jq -r ".state")
    59      if [[ "$state" != "$expected" ]];then
    60          echo "unexpected state $state, expected $expected"
    61          exit 1
    62      fi
    63  }
    64  
    65  export -f get_safepoint
    66  export -f check_safepoint_forward
    67  export -f check_safepoint_cleared
    68  export -f check_safepoint_equal
    69  export -f check_changefeed_state
    70  
    71  function run() {
    72      rm -rf $WORK_DIR && mkdir -p $WORK_DIR
    73      start_tidb_cluster --workdir $WORK_DIR
    74      cd $WORK_DIR
    75  
    76      pd_addr="http://$UP_PD_HOST_1:$UP_PD_PORT_1"
    77      TOPIC_NAME="ticdc-gc-safepoint-$RANDOM"
    78      case $SINK_TYPE in
    79          kafka) SINK_URI="kafka://127.0.0.1:9092/$TOPIC_NAME?partition-num=4&kafka-version=${KAFKA_VERSION}";;
    80          *) SINK_URI="mysql://normal:123456@127.0.0.1:3306/?max-txn-row=1";;
    81      esac
    82      if [ "$SINK_TYPE" == "kafka" ]; then
    83        run_kafka_consumer $WORK_DIR "kafka://127.0.0.1:9092/$TOPIC_NAME?partition-num=4&version=${KAFKA_VERSION}"
    84      fi
    85      run_cdc_server --workdir $WORK_DIR --binary $CDC_BINARY --addr "127.0.0.1:8300" --pd $pd_addr
    86      changefeed_id=$(cdc cli changefeed create --pd=$pd_addr --sink-uri="$SINK_URI" 2>&1|tail -n2|head -n1|awk '{print $2}')
    87  
    88      run_sql "CREATE DATABASE gc_safepoint;" ${UP_TIDB_HOST} ${UP_TIDB_PORT}
    89      run_sql "CREATE table gc_safepoint.simple(id int primary key auto_increment, val int);" ${UP_TIDB_HOST} ${UP_TIDB_PORT}
    90      run_sql "INSERT INTO gc_safepoint.simple VALUES (),();" ${UP_TIDB_HOST} ${UP_TIDB_PORT}
    91      check_sync_diff $WORK_DIR $CUR/conf/diff_config.toml
    92  
    93      pd_cluster_id=$(curl -s $pd_addr/pd/api/v1/cluster|grep -oE "id\":\s[0-9]+"|grep -oE "[0-9]+")
    94      start_safepoint=$(get_safepoint $pd_addr $pd_cluster_id)
    95      ensure $MAX_RETRIES check_safepoint_forward $pd_addr $pd_cluster_id $start_safepoint
    96  
    97      # after the changefeed is paused, the safe_point will be not updated
    98      cdc cli changefeed pause --changefeed-id=$changefeed_id --pd=$pd_addr
    99      ensure $MAX_RETRIES check_changefeed_state $pd_addr $changefeed_id "stopped"
   100      ensure $MAX_RETRIES check_safepoint_equal $pd_addr $pd_cluster_id
   101  
   102      # resume changefeed will recover the safe_point forward
   103      cdc cli changefeed resume --changefeed-id=$changefeed_id --pd=$pd_addr
   104      ensure $MAX_RETRIES check_changefeed_state $pd_addr $changefeed_id "normal"
   105      start_safepoint=$(get_safepoint $pd_addr $pd_cluster_id)
   106      ensure $MAX_RETRIES check_safepoint_forward $pd_addr $pd_cluster_id $start_safepoint
   107  
   108      cdc cli changefeed pause --changefeed-id=$changefeed_id --pd=$pd_addr
   109      ensure $MAX_RETRIES check_changefeed_state $pd_addr $changefeed_id "stopped"
   110      # create another changefeed, because there exists a paused changefeed,
   111      # the safe_point still does not forward
   112      changefeed_id2=$(cdc cli changefeed create --pd=$pd_addr --sink-uri="$SINK_URI" 2>&1|tail -n2|head -n1|awk '{print $2}')
   113      ensure $MAX_RETRIES check_changefeed_state $pd_addr $changefeed_id2 "normal"
   114      ensure $MAX_RETRIES check_safepoint_equal $pd_addr $pd_cluster_id
   115  
   116      # remove paused changefeed, the safe_point forward will recover
   117      cdc cli changefeed remove --changefeed-id=$changefeed_id --pd=$pd_addr
   118      # remove this line when new owner is enabled, because `removed` state is deprecated
   119      ensure $MAX_RETRIES check_changefeed_state $pd_addr $changefeed_id "removed"
   120      start_safepoint=$(get_safepoint $pd_addr $pd_cluster_id)
   121      ensure $MAX_RETRIES check_safepoint_forward $pd_addr $pd_cluster_id $start_safepoint
   122  
   123      # remove all changefeeds, the safe_point will be cleared
   124      cdc cli changefeed remove --changefeed-id=$changefeed_id2 --pd=$pd_addr
   125      ensure $MAX_RETRIES check_changefeed_state $pd_addr $changefeed_id2 "removed"
   126      ensure $MAX_RETRIES check_safepoint_cleared $pd_addr $pd_cluster_id
   127  
   128      cleanup_process $CDC_BINARY
   129  }
   130  
   131  trap stop_tidb_cluster EXIT
   132  run $*
   133  check_logs $WORK_DIR
   134  echo "[$(date)] <<<<<< run test case $TEST_NAME success! >>>>>>"