From 1518cc10ca91120f3f402e75fb161862eb9cbaef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Fran=C3=A7ois=20Gagn=C3=A9?= Date: Wed, 5 Jul 2023 15:03:22 -0400 Subject: [PATCH] Examples: only terminate vtadmin if it was started (#13433) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Do not error stopping vtadmin web and api if pidd files do not exist. For fixing below. https://github.com/vitessio/vitess/issues/13432 Signed-off-by: Jean-François Gagné * Get rid of code duplication in vtadmin-down.sh. Signed-off-by: Jean-François Gagné * Use bash test Signed-off-by: Matt Lord * Use SIGTERM and wait for process to end Signed-off-by: Matt Lord * Validate function parameters Signed-off-by: Matt Lord * Add license header Signed-off-by: Matt Lord * Move vtadmin stop function to utils Signed-off-by: Matt Lord * Generalize simple process shutdown with timeout Signed-off-by: Matt Lord * Correct output Signed-off-by: Matt Lord * Remove now redundant log message Signed-off-by: Matt Lord * Corrections Signed-off-by: Matt Lord * Add missing license header to modified file Signed-off-by: Matt Lord * Only check PID if we killed it Signed-off-by: Matt Lord * Remove mix of spaces and tabs Signed-off-by: Matt Lord --------- Signed-off-by: Jean-François Gagné Signed-off-by: Matt Lord Co-authored-by: Matt Lord --- examples/common/lib/utils.sh | 33 ++++++++++++++++++++++++ examples/common/scripts/consul-down.sh | 4 +-- examples/common/scripts/etcd-down.sh | 4 +-- examples/common/scripts/vtadmin-down.sh | 21 +++++++++++---- examples/common/scripts/vtctld-down.sh | 4 +-- examples/common/scripts/vtgate-down.sh | 5 ++-- examples/common/scripts/vtorc-down.sh | 17 ++++++++++-- examples/common/scripts/vttablet-down.sh | 9 ++----- 8 files changed, 74 insertions(+), 23 deletions(-) diff --git a/examples/common/lib/utils.sh b/examples/common/lib/utils.sh index 1e01da2181b..66af7d31bd7 100644 --- a/examples/common/lib/utils.sh +++ b/examples/common/lib/utils.sh @@ -139,6 +139,39 @@ function wait_for_healthy_shard() { wait_for_shard_vreplication_engine "${keyspace}" "${shard}" } +# Stop the specified binary name using the provided PID file. +# Example: +# stop_process "vtadmin-web" "$VTDATAROOT/tmp/vtadmin-web.pid" +function stop_process() { + if [[ -z ${1} || -z ${2} ]]; then + fail "A binary name and PID file must be specified when attempting to shutdown a process" + fi + + local binary_name="${1}" + local pidfile="${2}" + local pid="" + local wait_secs=90 + + if [[ -e "${pidfile}" ]]; then + pid=$(cat "${pidfile}") + echo "Stopping ${binary_name}..." + kill "${pid}" + + # Wait for the process to terminate + for _ in $(seq 1 ${wait_secs}); do + if ! ps -p "${pid}" > /dev/null; then + break + fi + sleep 1 + done + if ps -p "${pid}" > /dev/null; then + fail "Timed out after ${wait_secs} seconds waiting for the ${binary_name} using PID file ${pidfile} to terminate" + fi + else + echo "Skipping stopping ${binary_name} because the specified PID file (${pidfile}) does not exist." + fi +} + # Print error message and exit with error code. function fail() { echo "ERROR: ${1}" diff --git a/examples/common/scripts/consul-down.sh b/examples/common/scripts/consul-down.sh index 4da5694525a..2eccb8b5d96 100755 --- a/examples/common/scripts/consul-down.sh +++ b/examples/common/scripts/consul-down.sh @@ -18,5 +18,5 @@ source "$(dirname "${BASH_SOURCE[0]:-$0}")/../env.sh" -echo "Stopping consul..." -kill -9 `cat $VTDATAROOT/tmp/consul.pid` +stop_process "consul" "$VTDATAROOT/tmp/consul.pid" + diff --git a/examples/common/scripts/etcd-down.sh b/examples/common/scripts/etcd-down.sh index f9894f8659c..dbc5d8b1fd6 100755 --- a/examples/common/scripts/etcd-down.sh +++ b/examples/common/scripts/etcd-down.sh @@ -18,5 +18,5 @@ source "$(dirname "${BASH_SOURCE[0]:-$0}")/../env.sh" -echo "Stopping etcd..." -kill -9 `cat $VTDATAROOT/tmp/etcd.pid` +stop_process "etcd" "$VTDATAROOT/tmp/etcd.pid" + diff --git a/examples/common/scripts/vtadmin-down.sh b/examples/common/scripts/vtadmin-down.sh index 011e6da7f49..c592f0991ee 100755 --- a/examples/common/scripts/vtadmin-down.sh +++ b/examples/common/scripts/vtadmin-down.sh @@ -1,9 +1,20 @@ #!/bin/bash -source "$(dirname "${BASH_SOURCE[0]:-$0}")/../env.sh" +# Copyright 2023 The Vitess Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. -echo "Stopping vtadmin-web..." -kill -9 "$(cat "$VTDATAROOT/tmp/vtadmin-web.pid")" +source "$(dirname "${BASH_SOURCE[0]:-$0}")/../env.sh" -echo "Stopping vtadmin-api..." -kill -9 "$(cat "$VTDATAROOT/tmp/vtadmin-api.pid")" +stop_process "vtadmin-web" "$VTDATAROOT/tmp/vtadmin-web.pid" +stop_process "vtadmin-api" "$VTDATAROOT/tmp/vtadmin-api.pid" diff --git a/examples/common/scripts/vtctld-down.sh b/examples/common/scripts/vtctld-down.sh index a56d59b97e5..723b1a77bb6 100755 --- a/examples/common/scripts/vtctld-down.sh +++ b/examples/common/scripts/vtctld-down.sh @@ -18,5 +18,5 @@ source "$(dirname "${BASH_SOURCE[0]:-$0}")/../env.sh" -echo "Stopping vtctld..." -kill -9 `cat $VTDATAROOT/tmp/vtctld.pid` +stop_process "vtctld" "$VTDATAROOT/tmp/vtctld.pid" + diff --git a/examples/common/scripts/vtgate-down.sh b/examples/common/scripts/vtgate-down.sh index 3eea5fdf94d..c25036c7955 100755 --- a/examples/common/scripts/vtgate-down.sh +++ b/examples/common/scripts/vtgate-down.sh @@ -18,6 +18,5 @@ source "$(dirname "${BASH_SOURCE[0]:-$0}")/../env.sh" -# Stop vtgate. -echo "Stopping vtgate..." -kill `cat $VTDATAROOT/tmp/vtgate.pid` +stop_process "vtgate" "$VTDATAROOT/tmp/vtgate.pid" + diff --git a/examples/common/scripts/vtorc-down.sh b/examples/common/scripts/vtorc-down.sh index f4d2e4cb8a0..084c3e8e541 100755 --- a/examples/common/scripts/vtorc-down.sh +++ b/examples/common/scripts/vtorc-down.sh @@ -1,7 +1,20 @@ #!/bin/bash +# Copyright 2023 The Vitess Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + source "$(dirname "${BASH_SOURCE[0]:-$0}")/../env.sh" -echo "Stopping vtorc." -kill -9 "$(cat "$VTDATAROOT/tmp/vtorc.pid")" +stop_process "vtorc" "$VTDATAROOT/tmp/vtorc.pid" diff --git a/examples/common/scripts/vttablet-down.sh b/examples/common/scripts/vttablet-down.sh index 3de266def76..b81b94674ea 100755 --- a/examples/common/scripts/vttablet-down.sh +++ b/examples/common/scripts/vttablet-down.sh @@ -19,12 +19,7 @@ source "$(dirname "${BASH_SOURCE[0]:-$0}")/../env.sh" -printf -v tablet_dir 'vt_%010d' $TABLET_UID -pid=`cat $VTDATAROOT/$tablet_dir/vttablet.pid` - -kill $pid - -# Wait for vttablet to die. -while ps -p $pid > /dev/null; do sleep 1; done +printf -v tablet_dir 'vt_%010d' "$TABLET_UID" +stop_process "vttablet" "$VTDATAROOT/$tablet_dir/vttablet.pid"