HBASE-21138 Close HRegion instance at the end of every test in TestHRegion
[hbase.git] / bin / graceful_stop.sh
blob89e3dd939c21364b7efe610435ed1653508fa381
1 #!/usr/bin/env bash
3 #/**
4 # * Licensed to the Apache Software Foundation (ASF) under one
5 # * or more contributor license agreements. See the NOTICE file
6 # * distributed with this work for additional information
7 # * regarding copyright ownership. The ASF licenses this file
8 # * to you under the Apache License, Version 2.0 (the
9 # * "License"); you may not use this file except in compliance
10 # * with the License. You may obtain a copy of the License at
11 # *
12 # * http://www.apache.org/licenses/LICENSE-2.0
13 # *
14 # * Unless required by applicable law or agreed to in writing, software
15 # * distributed under the License is distributed on an "AS IS" BASIS,
16 # * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 # * See the License for the specific language governing permissions and
18 # * limitations under the License.
19 # */
21 # Move regions off a server then stop it. Optionally restart and reload.
22 # Turn off the balancer before running this script.
23 function usage {
24 echo "Usage: graceful_stop.sh [--config <conf-dir>] [-e] [--restart [--reload]] [--thrift] \
25 [--rest] [-nob |--nobalancer ] <hostname>"
26 echo " thrift If we should stop/start thrift before/after the hbase stop/start"
27 echo " rest If we should stop/start rest before/after the hbase stop/start"
28 echo " restart If we should restart after graceful stop"
29 echo " reload Move offloaded regions back on to the restarted server"
30 echo " n|noack Enable noAck mode in RegionMover. This is a best effort mode for \
31 moving regions"
32 echo " maxthreads xx Limit the number of threads used by the region mover. Default value is 1."
33 echo " movetimeout xx Timeout for moving regions. If regions are not moved by the timeout value,\
34 exit with error. Default value is INT_MAX."
35 echo " hostname Hostname of server we are to stop"
36 echo " e|failfast Set -e so exit immediately if any command exits with non-zero status"
37 echo " nob| nobalancer Do not manage balancer states. This is only used as optimization in \
38 rolling_restart.sh to avoid multiple calls to hbase shell"
39 exit 1
42 if [ $# -lt 1 ]; then
43 usage
46 bin=`dirname "$0"`
47 bin=`cd "$bin">/dev/null; pwd`
48 # This will set HBASE_HOME, etc.
49 . "$bin"/hbase-config.sh
50 # Get arguments
51 restart=
52 reload=
53 noack=
54 thrift=
55 rest=
56 movetimeout=2147483647
57 maxthreads=1
58 failfast=
59 nob=false
60 while [ $# -gt 0 ]
62 case "$1" in
63 --thrift) thrift=true; shift;;
64 --rest) rest=true; shift;;
65 --restart) restart=true; shift;;
66 --reload) reload=true; shift;;
67 --failfast | -e) failfast=true; shift;;
68 --noack | -n) noack="--noack"; shift;;
69 --maxthreads) shift; maxthreads=$1; shift;;
70 --movetimeout) shift; movetimeout=$1; shift;;
71 --nobalancer | -nob) nob=true; shift;;
72 --) shift; break;;
73 -*) usage ;;
74 *) break;; # terminate while loop
75 esac
76 done
78 # "$@" contains the rest. Must be at least the hostname left.
79 if [ $# -lt 1 ]; then
80 usage
83 # Emit a log line w/ iso8901 date prefixed
84 log() {
85 echo `date +%Y-%m-%dT%H:%M:%S` $1
88 # See if we should set fail fast before we do anything.
89 if [ "$failfast" != "" ]; then
90 log "Set failfast, will exit immediately if any command exits with non-zero status"
91 set -e
94 hostname=$1
95 filename="/tmp/$hostname"
97 local=
98 localhostname=`/bin/hostname`
100 if [ "$localhostname" == "$hostname" ]; then
101 local=true
104 if [ "$nob" == "true" ]; then
105 log "[ $0 ] skipping disabling balancer -nob argument is used"
106 HBASE_BALANCER_STATE=false
107 else
108 log "Disabling load balancer"
109 HBASE_BALANCER_STATE=$(echo 'balance_switch false' | "$bin"/hbase --config "${HBASE_CONF_DIR}" shell -n | tail -1)
110 log "Previous balancer state was $HBASE_BALANCER_STATE"
113 log "Unloading $hostname region(s)"
114 HBASE_NOEXEC=true "$bin"/hbase --config ${HBASE_CONF_DIR} org.apache.hadoop.hbase.util.RegionMover \
115 --filename $filename --maxthreads $maxthreads $noack --operation "unload" --timeout $movetimeout \
116 --regionserverhost $hostname
117 log "Unloaded $hostname region(s)"
119 # Stop the server(s). Have to put hostname into its own little file for hbase-daemons.sh
120 hosts="/tmp/$(basename $0).$$.tmp"
121 echo $hostname >> $hosts
122 if [ "$thrift" != "" ]; then
123 log "Stopping thrift server on $hostname"
124 if [ "$local" == true ]; then
125 "$bin"/hbase-daemon.sh --config ${HBASE_CONF_DIR} stop thrift
126 else
127 "$bin"/hbase-daemons.sh --config ${HBASE_CONF_DIR} --hosts ${hosts} stop thrift
130 if [ "$rest" != "" ]; then
131 log "Stopping rest server on $hostname"
132 if [ "$local" == true ]; then
133 "$bin"/hbase-daemon.sh --config ${HBASE_CONF_DIR} stop rest
134 else
135 "$bin"/hbase-daemons.sh --config ${HBASE_CONF_DIR} --hosts ${hosts} stop rest
138 log "Stopping regionserver on $hostname"
139 if [ "$local" == true ]; then
140 "$bin"/hbase-daemon.sh --config ${HBASE_CONF_DIR} stop regionserver
141 else
142 "$bin"/hbase-daemons.sh --config ${HBASE_CONF_DIR} --hosts ${hosts} stop regionserver
144 if [ "$restart" != "" ]; then
145 log "Restarting regionserver on $hostname"
146 if [ "$local" == true ]; then
147 "$bin"/hbase-daemon.sh --config ${HBASE_CONF_DIR} start regionserver
148 else
149 "$bin"/hbase-daemons.sh --config ${HBASE_CONF_DIR} --hosts ${hosts} start regionserver
151 if [ "$thrift" != "" ]; then
152 log "Restarting thrift server on $hostname"
153 # -b 0.0.0.0 says listen on all interfaces rather than just default.
154 if [ "$local" == true ]; then
155 "$bin"/hbase-daemon.sh --config ${HBASE_CONF_DIR} start thrift -b 0.0.0.0
156 else
157 "$bin"/hbase-daemons.sh --config ${HBASE_CONF_DIR} --hosts ${hosts} start thrift -b 0.0.0.0
160 if [ "$rest" != "" ]; then
161 log "Restarting rest server on $hostname"
162 if [ "$local" == true ]; then
163 "$bin"/hbase-daemon.sh --config ${HBASE_CONF_DIR} start rest
164 else
165 "$bin"/hbase-daemons.sh --config ${HBASE_CONF_DIR} --hosts ${hosts} start rest
168 if [ "$reload" != "" ]; then
169 log "Reloading $hostname region(s)"
170 HBASE_NOEXEC=true "$bin"/hbase --config ${HBASE_CONF_DIR} \
171 org.apache.hadoop.hbase.util.RegionMover --filename $filename --maxthreads $maxthreads $noack \
172 --operation "load" --timeout $movetimeout --regionserverhost $hostname
173 log "Reloaded $hostname region(s)"
177 # Restore balancer state
178 if [ "$HBASE_BALANCER_STATE" != "false" ] && [ "$nob" != "true" ]; then
179 log "Restoring balancer state to $HBASE_BALANCER_STATE"
180 echo "balance_switch $HBASE_BALANCER_STATE" | "$bin"/hbase --config ${HBASE_CONF_DIR} shell &> /dev/null
181 else
182 log "[ $0 ] skipping restoring balancer"
185 # Cleanup tmp files.
186 trap "rm -f "/tmp/$(basename $0).*.tmp" &> /dev/null" EXIT