HBASE-24770 Reimplement the Constraints API and revisit the IA annotations on related...
[hbase.git] / bin / graceful_stop.sh
bloba2b4efeada184dff19faa6bb9b00e613a53961ec
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 of server we are to stop"
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`
108 if [ "$localhostname" == "$hostname" ]; then
109 local=true
112 if [ "$nob" == "true" ]; then
113 log "[ $0 ] skipping disabling balancer -nob argument is used"
114 HBASE_BALANCER_STATE=false
115 else
116 log "Disabling load balancer"
117 HBASE_BALANCER_STATE=$(echo 'balance_switch false' | "$bin"/hbase --config "${HBASE_CONF_DIR}" shell -n | tail -1)
118 log "Previous balancer state was $HBASE_BALANCER_STATE"
121 unload_args="--filename $filename --maxthreads $maxthreads $noack --operation unload \
122 --timeout $movetimeout --regionserverhost $hostname"
124 if [ "$designatedfile" != "" ]; then
125 unload_args="$unload_args --designatedfile $designatedfile"
128 if [ "$excludefile" != "" ]; then
129 unload_args="$unload_args --excludefile $excludefile"
132 log "Unloading $hostname region(s)"
133 HBASE_NOEXEC=true "$bin"/hbase --config ${HBASE_CONF_DIR} org.apache.hadoop.hbase.util.RegionMover \
134 $unload_args
135 log "Unloaded $hostname region(s)"
137 # Stop the server(s). Have to put hostname into its own little file for hbase-daemons.sh
138 hosts="/tmp/$(basename $0).$$.tmp"
139 echo $hostname >> $hosts
140 if [ "$thrift" != "" ]; then
141 log "Stopping thrift server on $hostname"
142 if [ "$local" == true ]; then
143 "$bin"/hbase-daemon.sh --config ${HBASE_CONF_DIR} stop thrift
144 else
145 "$bin"/hbase-daemons.sh --config ${HBASE_CONF_DIR} --hosts ${hosts} stop thrift
148 if [ "$rest" != "" ]; then
149 log "Stopping rest server on $hostname"
150 if [ "$local" == true ]; then
151 "$bin"/hbase-daemon.sh --config ${HBASE_CONF_DIR} stop rest
152 else
153 "$bin"/hbase-daemons.sh --config ${HBASE_CONF_DIR} --hosts ${hosts} stop rest
156 log "Stopping regionserver on $hostname"
157 if [ "$local" == true ]; then
158 "$bin"/hbase-daemon.sh --config ${HBASE_CONF_DIR} stop regionserver
159 else
160 "$bin"/hbase-daemons.sh --config ${HBASE_CONF_DIR} --hosts ${hosts} stop regionserver
162 if [ "$restart" != "" ]; then
163 log "Restarting regionserver on $hostname"
164 if [ "$local" == true ]; then
165 "$bin"/hbase-daemon.sh --config ${HBASE_CONF_DIR} start regionserver
166 else
167 "$bin"/hbase-daemons.sh --config ${HBASE_CONF_DIR} --hosts ${hosts} start regionserver
169 if [ "$thrift" != "" ]; then
170 log "Restarting thrift server on $hostname"
171 # -b 0.0.0.0 says listen on all interfaces rather than just default.
172 if [ "$local" == true ]; then
173 "$bin"/hbase-daemon.sh --config ${HBASE_CONF_DIR} start thrift -b 0.0.0.0
174 else
175 "$bin"/hbase-daemons.sh --config ${HBASE_CONF_DIR} --hosts ${hosts} start thrift -b 0.0.0.0
178 if [ "$rest" != "" ]; then
179 log "Restarting rest server on $hostname"
180 if [ "$local" == true ]; then
181 "$bin"/hbase-daemon.sh --config ${HBASE_CONF_DIR} start rest
182 else
183 "$bin"/hbase-daemons.sh --config ${HBASE_CONF_DIR} --hosts ${hosts} start rest
186 if [ "$reload" != "" ]; then
187 log "Reloading $hostname region(s)"
188 HBASE_NOEXEC=true "$bin"/hbase --config ${HBASE_CONF_DIR} \
189 org.apache.hadoop.hbase.util.RegionMover --filename $filename --maxthreads $maxthreads $noack \
190 --operation "load" --timeout $movetimeout --regionserverhost $hostname
191 log "Reloaded $hostname region(s)"
195 # Restore balancer state
196 if [ "$HBASE_BALANCER_STATE" != "false" ] && [ "$nob" != "true" ]; then
197 log "Restoring balancer state to $HBASE_BALANCER_STATE"
198 echo "balance_switch $HBASE_BALANCER_STATE" | "$bin"/hbase --config ${HBASE_CONF_DIR} shell &> /dev/null
199 else
200 log "[ $0 ] skipping restoring balancer"
203 # Cleanup tmp files.
204 trap "rm -f "/tmp/$(basename $0).*.tmp" &> /dev/null" EXIT