Skip to content

Commit

Permalink
Examples: only terminate vtadmin if it was started (#13433)
Browse files Browse the repository at this point in the history
* Do not error stopping vtadmin web and api if pidd files do not exist.

For fixing below.
#13432

Signed-off-by: Jean-François Gagné <jfg956@users.noreply.github.com>

* Get rid of code duplication in vtadmin-down.sh.

Signed-off-by: Jean-François Gagné <jfg956@users.noreply.github.com>

* Use bash test

Signed-off-by: Matt Lord <mattalord@gmail.com>

* Use SIGTERM and wait for process to end

Signed-off-by: Matt Lord <mattalord@gmail.com>

* Validate function parameters

Signed-off-by: Matt Lord <mattalord@gmail.com>

* Add license header

Signed-off-by: Matt Lord <mattalord@gmail.com>

* Move vtadmin stop function to utils

Signed-off-by: Matt Lord <mattalord@gmail.com>

* Generalize simple process shutdown with timeout

Signed-off-by: Matt Lord <mattalord@gmail.com>

* Correct output

Signed-off-by: Matt Lord <mattalord@gmail.com>

* Remove now redundant log message

Signed-off-by: Matt Lord <mattalord@gmail.com>

* Corrections

Signed-off-by: Matt Lord <mattalord@gmail.com>

* Add missing license header to modified file

Signed-off-by: Matt Lord <mattalord@gmail.com>

* Only check PID if we killed it

Signed-off-by: Matt Lord <mattalord@gmail.com>

* Remove mix of spaces and tabs

Signed-off-by: Matt Lord <mattalord@gmail.com>

---------

Signed-off-by: Jean-François Gagné <jfg956@users.noreply.github.com>
Signed-off-by: Matt Lord <mattalord@gmail.com>
Co-authored-by: Matt Lord <mattalord@gmail.com>
  • Loading branch information
jfg956 and mattlord authored Jul 5, 2023
1 parent 4c2fef5 commit 1518cc1
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 23 deletions.
33 changes: 33 additions & 0 deletions examples/common/lib/utils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down
4 changes: 2 additions & 2 deletions examples/common/scripts/consul-down.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"

4 changes: 2 additions & 2 deletions examples/common/scripts/etcd-down.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"

21 changes: 16 additions & 5 deletions examples/common/scripts/vtadmin-down.sh
Original file line number Diff line number Diff line change
@@ -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"
4 changes: 2 additions & 2 deletions examples/common/scripts/vtctld-down.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"

5 changes: 2 additions & 3 deletions examples/common/scripts/vtgate-down.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"

17 changes: 15 additions & 2 deletions examples/common/scripts/vtorc-down.sh
Original file line number Diff line number Diff line change
@@ -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"

9 changes: 2 additions & 7 deletions examples/common/scripts/vttablet-down.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"

0 comments on commit 1518cc1

Please sign in to comment.