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