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
12 # * http://www.apache.org/licenses/LICENSE-2.0
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.
21 # Move regions off a server then stop it. Optionally restart and reload.
22 # Turn off the balancer before running this script.
24 echo "Usage: graceful_stop.sh [--config <conf-dir>] [-e] [--restart [--reload]] [--thrift] \
25 [--rest] [-n |--noack] [--maxthreads <number of threads>] [--movetimeout <timeout in seconds>] \
26 [-nob |--nobalancer] [-d |--designatedfile <file path>] [-x |--excludefile <file path>] <hostname>"
27 echo " thrift If we should stop/start thrift before/after the hbase stop/start"
28 echo " rest If we should stop/start rest before/after the hbase stop/start"
29 echo " restart If we should restart after graceful stop"
30 echo " reload Move offloaded regions back on to the restarted server"
31 echo " n|noack Enable noAck mode in RegionMover. This is a best effort mode for \
33 echo " maxthreads xx Limit the number of threads used by the region mover. Default value is 1."
34 echo " movetimeout xx Timeout for moving regions. If regions are not moved by the timeout value,\
35 exit with error. Default value is INT_MAX."
36 echo " hostname Hostname to stop; match what HBase uses; pass 'localhost' if local to avoid ssh"
37 echo " e|failfast Set -e so exit immediately if any command exits with non-zero status"
38 echo " nob|nobalancer Do not manage balancer states. This is only used as optimization in \
39 rolling_restart.sh to avoid multiple calls to hbase shell"
40 echo " d|designatedfile xx Designated file with <hostname:port> per line as unload targets"
41 echo " x|excludefile xx Exclude file should have <hostname:port> per line. We do not unload \
42 regions to hostnames given in exclude file"
51 bin
=`cd "$bin">/dev/null; pwd`
52 # This will set HBASE_HOME, etc.
53 .
"$bin"/hbase-config.sh
60 movetimeout
=2147483647
69 --thrift) thrift
=true
; shift;;
70 --rest) rest
=true
; shift;;
71 --restart) restart
=true
; shift;;
72 --reload) reload
=true
; shift;;
73 --failfast |
-e) failfast
=true
; shift;;
74 --noack |
-n) noack
="--noack"; shift;;
75 --maxthreads) shift; maxthreads
=$1; shift;;
76 --movetimeout) shift; movetimeout
=$1; shift;;
77 --nobalancer |
-nob) nob
=true
; shift;;
78 --designatedfile |
-d) shift; designatedfile
=$1; shift;;
79 --excludefile |
-x) shift; excludefile
=$1; shift;;
82 *) break;; # terminate while loop
86 # "$@" contains the rest. Must be at least the hostname left.
91 # Emit a log line w/ iso8901 date prefixed
93 echo `date +%Y-%m-%dT%H:%M:%S` $1
96 # See if we should set fail fast before we do anything.
97 if [ "$failfast" != "" ]; then
98 log
"Set failfast, will exit immediately if any command exits with non-zero status"
103 filename
="/tmp/$hostname"
106 localhostname
=`/bin/hostname -f`
108 if [ "$localhostname" == "$hostname" ] ||
[ "$hostname" == "localhost" ]; then
110 hostname
=$localhostname
113 if [ "$nob" == "true" ]; then
114 log
"[ $0 ] skipping disabling balancer -nob argument is used"
115 HBASE_BALANCER_STATE
=false
117 log
"Disabling load balancer"
118 HBASE_BALANCER_STATE
=$
(echo 'balance_switch false' |
"$bin"/hbase
--config "${HBASE_CONF_DIR}" shell
-n |
tail -1)
119 log
"Previous balancer state was $HBASE_BALANCER_STATE"
122 unload_args
="--filename $filename --maxthreads $maxthreads $noack --operation unload \
123 --timeout $movetimeout --regionserverhost $hostname"
125 if [ "$designatedfile" != "" ]; then
126 unload_args
="$unload_args --designatedfile $designatedfile"
129 if [ "$excludefile" != "" ]; then
130 unload_args
="$unload_args --excludefile $excludefile"
133 log
"Unloading $hostname region(s)"
134 HBASE_NOEXEC
=true
"$bin"/hbase
--config ${HBASE_CONF_DIR} org.apache.hadoop.hbase.util.RegionMover \
136 log
"Unloaded $hostname region(s)"
138 # Stop the server(s). Have to put hostname into its own little file for hbase-daemons.sh
139 hosts
="/tmp/$(basename $0).$$.tmp"
140 echo $hostname >> $hosts
141 if [ "$thrift" != "" ]; then
142 log
"Stopping thrift server on $hostname"
143 if [ "$local" == true
]; then
144 "$bin"/hbase-daemon.sh
--config ${HBASE_CONF_DIR} stop thrift
146 "$bin"/hbase-daemons.sh
--config ${HBASE_CONF_DIR} --hosts ${hosts} stop thrift
149 if [ "$rest" != "" ]; then
150 log
"Stopping rest server on $hostname"
151 if [ "$local" == true
]; then
152 "$bin"/hbase-daemon.sh
--config ${HBASE_CONF_DIR} stop rest
154 "$bin"/hbase-daemons.sh
--config ${HBASE_CONF_DIR} --hosts ${hosts} stop rest
157 log
"Stopping regionserver on $hostname"
158 if [ "$local" == true
]; then
159 "$bin"/hbase-daemon.sh
--config ${HBASE_CONF_DIR} stop regionserver
161 "$bin"/hbase-daemons.sh
--config ${HBASE_CONF_DIR} --hosts ${hosts} stop regionserver
163 if [ "$restart" != "" ]; then
164 log
"Restarting regionserver on $hostname"
165 if [ "$local" == true
]; then
166 "$bin"/hbase-daemon.sh
--config ${HBASE_CONF_DIR} start regionserver
168 "$bin"/hbase-daemons.sh
--config ${HBASE_CONF_DIR} --hosts ${hosts} start regionserver
170 if [ "$thrift" != "" ]; then
171 log
"Restarting thrift server on $hostname"
172 # -b 0.0.0.0 says listen on all interfaces rather than just default.
173 if [ "$local" == true
]; then
174 "$bin"/hbase-daemon.sh
--config ${HBASE_CONF_DIR} start thrift
-b 0.0.0.0
176 "$bin"/hbase-daemons.sh
--config ${HBASE_CONF_DIR} --hosts ${hosts} start thrift
-b 0.0.0.0
179 if [ "$rest" != "" ]; then
180 log
"Restarting rest server on $hostname"
181 if [ "$local" == true
]; then
182 "$bin"/hbase-daemon.sh
--config ${HBASE_CONF_DIR} start rest
184 "$bin"/hbase-daemons.sh
--config ${HBASE_CONF_DIR} --hosts ${hosts} start rest
187 if [ "$reload" != "" ]; then
188 log
"Reloading $hostname region(s)"
189 HBASE_NOEXEC
=true
"$bin"/hbase
--config ${HBASE_CONF_DIR} \
190 org.apache.hadoop.hbase.util.RegionMover
--filename $filename --maxthreads $maxthreads $noack \
191 --operation "load" --timeout $movetimeout --regionserverhost $hostname
192 log
"Reloaded $hostname region(s)"
196 # Restore balancer state
197 if [ "$HBASE_BALANCER_STATE" != "false" ] && [ "$nob" != "true" ]; then
198 log
"Restoring balancer state to $HBASE_BALANCER_STATE"
199 echo "balance_switch $HBASE_BALANCER_STATE" |
"$bin"/hbase
--config ${HBASE_CONF_DIR} shell
&> /dev
/null
201 log
"[ $0 ] skipping restoring balancer"
205 trap "rm -f /tmp/$(basename $0).*.tmp &> /dev/null" EXIT