github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/scripts/canal/canal-local-test.sh (about)

     1  #!/bin/sh
     2  
     3  # Copyright 2020 PingCAP, Inc.
     4  #
     5  # Licensed under the Apache License, Version 2.0 (the "License");
     6  # you may not use this file except in compliance with the License.
     7  # You may obtain a copy of the License at
     8  #
     9  #     http://www.apache.org/licenses/LICENSE-2.0
    10  #
    11  # Unless required by applicable law or agreed to in writing, software
    12  # distributed under the License is distributed on an "AS IS" BASIS,
    13  # See the License for the specific language governing permissions and
    14  # limitations under the License.
    15  
    16  # This is a script designed for testing canal support for Kafka sink.
    17  # The data flow is (upstream TiDB) ==> (TiCDC) =canal=> (kafka) =canal=> (canal consumer/adapter) =sql=> (downstream TiDB)
    18  # Developers should run "canal-local-test.sh up" first, wait for a while, run "canal-local-test.sh init", and only
    19  # then will the data flow be established.
    20  #
    21  # Upstream mysql://127.0.0.1:4000
    22  # Downstream mysql://127.0.0.1:5000
    23  #
    24  # This script only supports syncing (dbname = testdb).
    25  # Developers should adapt this file to their own needs.
    26  #
    27  
    28  ProgName=$(basename $0)
    29  UPSTREAM_DB_HOST=127.0.0.1
    30  UPSTREAM_DB_PORT=4000
    31  DOWNSTREAM_DB_HOST=127.0.0.1
    32  DOWNSTREAM_DB_PORT=5000
    33  DB_NAME=testdb
    34  TOPIC_NAME=${DB_NAME}
    35  cd "$(dirname "$0")"
    36  
    37  prepare_db() {
    38  	echo "Verifying Upstream TiDB is started..."
    39  	i=0
    40  	while ! mysql -uroot -h${UPSTREAM_DB_HOST} -P${UPSTREAM_DB_PORT} -e 'select * from mysql.tidb;'; do
    41  		i=$((i + 1))
    42  		if [ "$i" -gt 60 ]; then
    43  			echo 'Failed to start upstream TiDB'
    44  			exit 2
    45  		fi
    46  		sleep 2
    47  	done
    48  
    49  	echo "Verifying Downstream TiDB is started..."
    50  	i=0
    51  	while ! mysql -uroot -h${DOWNSTREAM_DB_HOST} -P${DOWNSTREAM_DB_PORT} -e 'select * from mysql.tidb;'; do
    52  		i=$((i + 1))
    53  		if [ "$i" -gt 60 ]; then
    54  			echo 'Failed to start downstream TiDB'
    55  			exit 1
    56  		fi
    57  		sleep 2
    58  	done
    59  	sql="drop database if exists ${DB_NAME}; create database ${DB_NAME};"
    60  	echo "[$(date)] Executing SQL: ${sql}"
    61  	mysql -uroot -h ${UPSTREAM_DB_HOST} -P ${UPSTREAM_DB_PORT} -E -e "${sql}"
    62  	mysql -uroot -h ${DOWNSTREAM_DB_HOST} -P ${DOWNSTREAM_DB_PORT} -E -e "${sql}"
    63  }
    64  
    65  sub_help() {
    66  	echo "Usage: $ProgName <subcommand> [options]\n"
    67  	echo "Subcommands:"
    68  	echo "    init    Prepare database and Create changefeed"
    69  	echo "    up      Bring up the containers"
    70  	echo "    down    Pause the containers"
    71  	echo ""
    72  }
    73  
    74  sub_init() {
    75  	prepare_db
    76  	sudo docker exec -it ticdc_controller_1 sh -c "
    77    /cdc cli changefeed create --pd=\"http://upstream-pd:2379\" --sink-uri=\"kafka://kafka:9092/testdb\" --config=\"/config/canal-test-config.toml\"
    78    "
    79  }
    80  
    81  sub_up() {
    82  	sudo docker-compose -f ../../docker-compose-canal.yml up -d
    83  }
    84  
    85  sub_down() {
    86  	sudo docker-compose -f ../../docker-compose-canal.yml down
    87  	sudo rm -r ../../docker/logs ../../docker/data
    88  }
    89  
    90  subcommand=$1
    91  case $subcommand in
    92  "" | "-h" | "--help")
    93  	sub_help
    94  	;;
    95  *)
    96  	shift
    97  	sub_${subcommand} '$@'
    98  	if [ $? = 127 ]; then
    99  		echo "Error: '$subcommand' is not a known subcommand." >&2
   100  		echo "       Run '$ProgName --help' for a list of known subcommands." >&2
   101  		exit 1
   102  	fi
   103  	;;
   104  esac