HBASE-26916 Fix missing braces warnings in DefaultVisibilityExpressionResolver (...
[hbase.git] / bin / graceful_stop.sh
blobfc18239830b2b6fc1aa14fe2e8f34ca5b6e0123a
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] [-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 \
32 moving regions"
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"
43 exit 1
46 if [ $# -lt 1 ]; then
47 usage
50 bin=`dirname "$0"`
51 bin=`cd "$bin">/dev/null; pwd`
52 # This will set HBASE_HOME, etc.
53 . "$bin"/hbase-config.sh
54 # Get arguments
55 restart=
56 reload=
57 noack=
58 thrift=
59 rest=
60 movetimeout=2147483647
61 maxthreads=1
62 failfast=
63 nob=false
64 designatedfile=
65 excludefile=
66 while [ $# -gt 0 ]
68 case "$1" in
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;;
80 --) shift; break;;
81 -*) usage ;;
82 *) break;; # terminate while loop
83 esac
84 done
86 # "$@" contains the rest. Must be at least the hostname left.
87 if [ $# -lt 1 ]; then
88 usage
91 # Emit a log line w/ iso8901 date prefixed
92 log() {
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"
99 set -e
102 hostname=$1
103 filename="/tmp/$hostname"
105 local=
106 localhostname=`/bin/hostname -f`
108 if [ "$localhostname" == "$hostname" ] || [ "$hostname" == "localhost" ]; then
109 local=true
110 hostname=$localhostname
113 if [ "$nob" == "true" ]; then
114 log "[ $0 ] skipping disabling balancer -nob argument is used"
115 HBASE_BALANCER_STATE=false
116 else
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 \
135 $unload_args
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
145 else
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
153 else
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
160 else
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
167 else
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
175 else
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
183 else
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
200 else
201 log "[ $0 ] skipping restoring balancer"
204 # Cleanup tmp files.
205 trap "rm -f /tmp/$(basename $0).*.tmp &> /dev/null" EXIT