Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

server(cdc): avoid printing help messages when cdc server exits #5524

Merged
merged 15 commits into from
May 26, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion pkg/cmd/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,9 @@ func NewCmdServer() *cobra.Command {
if err != nil {
return err
}
return o.run(cmd)
err = o.run(cmd)
cobra.CheckErr(err)
return nil
},
}

Expand Down
27 changes: 27 additions & 0 deletions tests/integration_tests/_utils/check_usage_tips
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/bash
# parameter 1: stdout file
# parameter 2: cmd_status true: cmd is valid and should not print usage tips, false: otherwise
stdout_file=$1
cmd_status=$2
should_print=
set +e

## check data race
if [ ! -f "$stdout_file" ]; then
exit 0
crelax marked this conversation as resolved.
Show resolved Hide resolved
fi

if [[ "$cmd_status" == "true" ]]; then
should_print=0
else
should_print=1
fi

grep -q 'Usage:' "$stdout_file"

if [ ! $? -eq "$should_print" ]; then
exit 0
else
echo "Check Usage Tips Failed!"
exit 1
fi
11 changes: 11 additions & 0 deletions tests/integration_tests/_utils/run_cdc_server
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ failpoint=$GO_FAILPOINTS
config_path=
data_dir=
curl_status_cmd=
supposed_to_fail="no"

while [[ ${1} ]]; do
case "${1}" in
Expand Down Expand Up @@ -77,6 +78,10 @@ while [[ ${1} ]]; do
config_path="--config ${2}"
shift
;;
--supposed-to-fail)
supposed_to_fail="${2}"
shift
;;
--data-dir)
data_dir=${2}
shift
Expand Down Expand Up @@ -151,6 +156,12 @@ else
curl_status_cmd="curl --cacert $tls_dir/ca.pem --cert $tls_dir/$certcn_name.pem --key $tls_dir/$certcn_name-key.pem -vsL --max-time 20 https://$addr_url/debug/info"
fi

# If the command is supposed to fail (in check usage tips test), just exit without retry
if [[ "$supposed_to_fail" != "no" ]]; then
set +x
exit 0
fi

for ((i = 0; i <= 50; i++)); do
res=$($curl_status_cmd)
# Make sure we get the capture info(etcd_info_msg) and that there are no errors(get_info_fail_msg).
Expand Down
97 changes: 97 additions & 0 deletions tests/integration_tests/cdc_server_tips/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#!/bin/bash

set -eu

CUR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
source $CUR/../_utils/test_prepare
WORK_DIR=$OUT_DIR/$TEST_NAME
CDC_BINARY=cdc.test
SINK_TYPE="mysql"
stdout_file=$WORK_DIR/stdout.log
cdc_launched=

function try_to_run_cdc() {
# $*: invalid args to avoid lauching cdc server
rm -rf $WORK_DIR && mkdir -p $WORK_DIR
start_tidb_cluster --workdir $WORK_DIR
crelax marked this conversation as resolved.
Show resolved Hide resolved

cd $WORK_DIR

# record tso before we create tables to skip the system table DDLs
start_ts=$(run_cdc_cli_tso_query ${UP_PD_HOST_1} ${UP_PD_PORT_1})

run_sql "CREATE table test.simple1(id int primary key, val int);"

if [[ $1 == "valid" ]]; then
echo "try a VALID cdc server command"
run_cdc_server --workdir $WORK_DIR --binary $CDC_BINARY
else
echo "try an INVALID cdc server command"
run_cdc_server --supposed-to-fail "true" --workdir $WORK_DIR --binary $CDC_BINARY --pd "None"
fi

#Wait the failed cdc to quit
sleep 20

if [[ $(ps -a | grep "cdc.test") == "" ]]; then
cdc_launched="false"
echo 'Failed to start cdc, the usage tips should be printed'
return 0
else
cdc_launched="true"
echo 'Succeed to run cdc, now create a changefeed, no usage tips should be printed'
echo "pid"$(ps -a | grep "cdc.test")
fi

TOPIC_NAME="ticdc-server-tips-test-$RANDOM"
case $SINK_TYPE in
kafka) SINK_URI="kafka+ssl://127.0.0.1:9092/$TOPIC_NAME?protocol=open-protocol&partition-num=4&kafka-client-id=cdc_server_tips&kafka-version=${KAFKA_VERSION}&max-message-bytes=10485760" ;;
*) SINK_URI="mysql+ssl://normal:123456@127.0.0.1:3306/" ;;
esac
run_cdc_cli changefeed create --start-ts=$start_ts --sink-uri="$SINK_URI"
if [ "$SINK_TYPE" == "kafka" ]; then
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"
fi

echo 'Succeed to create a changefeed, no usage tips should be printed'
crelax marked this conversation as resolved.
Show resolved Hide resolved
}

function region_label_test() {
i=0
while [ -z "$(curl -X GET http://127.0.0.1:2379/pd/api/v1/config/region-label/rules 2>/dev/null | grep 'meta')" ]; do
i=$((i + 1))
if [ "$i" -gt 5 ]; then
echo 'Failed to verify meta region labels'
exit 1
fi
sleep 1
done
echo 'succeed to verify meta placement rules'
}

stop_cdc_and_do_check() {
if [[ "$cdc_launched" == "true" ]]; then
region_label_test
crelax marked this conversation as resolved.
Show resolved Hide resolved
echo "Later, cdc will receive a signal(SIGINT) and exit"
sleep 60
cdc_pid=$(ps -a | grep -m 1 "cdc.test" | awk '{print $1}')
echo "cdc pid is "$cdc_pid
kill -2 $cdc_pid
fi
sleep 10
check_usage_tips $stdout_file $cdc_launched
}

# invalid command and should print usage tips
trap stop_tidb_cluster EXIT
try_to_run_cdc "invalid"
stop_cdc_and_do_check
echo " 1st test case $TEST_NAME success! "

# should not print usage tips
crelax marked this conversation as resolved.
Show resolved Hide resolved
trap stop_tidb_cluster EXIT
crelax marked this conversation as resolved.
Show resolved Hide resolved
try_to_run_cdc "valid"
stop_cdc_and_do_check
echo " 2nd test case $TEST_NAME success! "

echo "[$(date)] <<<<<< run all test cases $TEST_NAME success! >>>>>> "