github.com/pingcap/ticdc@v0.0.0-20220526033649-485a10ef2652/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  
    66  sub_help() {
    67      echo "Usage: $ProgName <subcommand> [options]\n"
    68      echo "Subcommands:"
    69      echo "    init    Prepare database and Create changefeed"
    70      echo "    up      Bring up the containers"
    71      echo "    down    Pause the containers"
    72      echo ""
    73  }
    74  
    75  sub_init() {
    76    prepare_db
    77    sudo docker exec -it ticdc_controller_1 sh -c "
    78    /cdc cli changefeed create --pd=\"http://upstream-pd:2379\" --sink-uri=\"kafka://kafka:9092/testdb\" --config=\"/config/canal-test-config.toml\" --opts \"force-handle-key-pkey=true, support-txn=true\"
    79    "
    80  }
    81  
    82  sub_up() {
    83    sudo docker-compose -f ../../docker-compose-canal.yml up -d
    84  }
    85  
    86  sub_down() {
    87    sudo docker-compose -f ../../docker-compose-canal.yml down
    88    sudo rm -r ../../docker/logs ../../docker/data
    89  }
    90  
    91  subcommand=$1
    92  case $subcommand in
    93      "" | "-h" | "--help")
    94          sub_help
    95          ;;
    96      *)
    97          shift
    98          sub_${subcommand} '$@'
    99          if [ $? = 127 ]; then
   100              echo "Error: '$subcommand' is not a known subcommand." >&2
   101              echo "       Run '$ProgName --help' for a list of known subcommands." >&2
   102              exit 1
   103          fi
   104          ;;
   105  esac