Preparing development version 2.4.4-SNAPSHOT
[hbase.git] / bin / hbase-daemon.sh
blob11c13eb52300d971f1d7105b1d03c54e2c68a5fb
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 # Runs a Hadoop hbase command as a daemon.
23 # Environment Variables
25 # HBASE_CONF_DIR Alternate hbase conf dir. Default is ${HBASE_HOME}/conf.
26 # HBASE_LOG_DIR Where log files are stored. PWD by default.
27 # HBASE_PID_DIR The pid files are stored. /tmp by default.
28 # HBASE_IDENT_STRING A string representing this instance of hadoop. $USER by default
29 # HBASE_NICENESS The scheduling priority for daemons. Defaults to 0.
30 # HBASE_STOP_TIMEOUT Time, in seconds, after which we kill -9 the server if it has not stopped.
31 # Default 1200 seconds.
33 # Modelled after $HADOOP_HOME/bin/hadoop-daemon.sh
35 usage="Usage: hbase-daemon.sh [--config <conf-dir>]\
36 [--autostart-window-size <window size in hours>]\
37 [--autostart-window-retry-limit <retry count limit for autostart>]\
38 (start|stop|restart|autostart|autorestart|foreground_start) <hbase-command> \
39 <args...>"
41 # if no args specified, show usage
42 if [ $# -le 1 ]; then
43 echo $usage
44 exit 1
47 # default autostart args value indicating infinite window size and no retry limit
48 AUTOSTART_WINDOW_SIZE=0
49 AUTOSTART_WINDOW_RETRY_LIMIT=0
51 bin=`dirname "${BASH_SOURCE-$0}"`
52 bin=`cd "$bin">/dev/null; pwd`
54 . "$bin"/hbase-config.sh
55 . "$bin"/hbase-common.sh
57 # get arguments
58 startStop=$1
59 shift
61 command=$1
62 shift
64 hbase_rotate_log ()
66 log=$1;
67 num=5;
68 if [ -n "$2" ]; then
69 num=$2
71 if [ -f "$log" ]; then # rotate logs
72 while [ $num -gt 1 ]; do
73 prev=`expr $num - 1`
74 [ -f "$log.$prev" ] && mv -f "$log.$prev" "$log.$num"
75 num=$prev
76 done
77 mv -f "$log" "$log.$num";
81 cleanAfterRun() {
82 if [ -f ${HBASE_PID} ]; then
83 # If the process is still running time to tear it down.
84 kill -9 `cat ${HBASE_PID}` > /dev/null 2>&1
85 rm -f ${HBASE_PID} > /dev/null 2>&1
88 if [ -f ${HBASE_ZNODE_FILE} ]; then
89 if [ "$command" = "master" ]; then
90 HBASE_OPTS="$HBASE_OPTS $HBASE_MASTER_OPTS" $bin/hbase master clear > /dev/null 2>&1
91 else
92 #call ZK to delete the node
93 ZNODE=`cat ${HBASE_ZNODE_FILE}`
94 HBASE_OPTS="$HBASE_OPTS $HBASE_REGIONSERVER_OPTS" $bin/hbase zkcli delete ${ZNODE} > /dev/null 2>&1
96 rm ${HBASE_ZNODE_FILE}
100 check_before_start(){
101 #ckeck if the process is not running
102 mkdir -p "$HBASE_PID_DIR"
103 if [ -f $HBASE_PID ]; then
104 if kill -0 `cat $HBASE_PID` > /dev/null 2>&1; then
105 echo $command running as process `cat $HBASE_PID`. Stop it first.
106 exit 1
111 wait_until_done ()
113 p=$1
114 cnt=${HBASE_SLAVE_TIMEOUT:-300}
115 origcnt=$cnt
116 while kill -0 $p > /dev/null 2>&1; do
117 if [ $cnt -gt 1 ]; then
118 cnt=`expr $cnt - 1`
119 sleep 1
120 else
121 echo "Process did not complete after $origcnt seconds, killing."
122 kill -9 $p
123 exit 1
125 done
126 return 0
129 # get log directory
130 if [ "$HBASE_LOG_DIR" = "" ]; then
131 export HBASE_LOG_DIR="$HBASE_HOME/logs"
133 mkdir -p "$HBASE_LOG_DIR"
135 if [ "$HBASE_PID_DIR" = "" ]; then
136 HBASE_PID_DIR=/tmp
139 if [ "$HBASE_IDENT_STRING" = "" ]; then
140 export HBASE_IDENT_STRING="$USER"
143 # Some variables
144 # Work out java location so can print version into log.
145 if [ "$JAVA_HOME" != "" ]; then
146 #echo "run java in $JAVA_HOME"
147 JAVA_HOME=$JAVA_HOME
149 if [ "$JAVA_HOME" = "" ]; then
150 echo "Error: JAVA_HOME is not set."
151 exit 1
154 JAVA=$JAVA_HOME/bin/java
155 export HBASE_LOG_PREFIX=hbase-$HBASE_IDENT_STRING-$command-$HOSTNAME
156 export HBASE_LOGFILE=$HBASE_LOG_PREFIX.log
158 if [ -z "${HBASE_ROOT_LOGGER}" ]; then
159 export HBASE_ROOT_LOGGER=${HBASE_ROOT_LOGGER:-"INFO,RFA"}
162 if [ -z "${HBASE_SECURITY_LOGGER}" ]; then
163 export HBASE_SECURITY_LOGGER=${HBASE_SECURITY_LOGGER:-"INFO,RFAS"}
166 HBASE_LOGOUT=${HBASE_LOGOUT:-"$HBASE_LOG_DIR/$HBASE_LOG_PREFIX.out"}
167 HBASE_LOGGC=${HBASE_LOGGC:-"$HBASE_LOG_DIR/$HBASE_LOG_PREFIX.gc"}
168 HBASE_LOGLOG=${HBASE_LOGLOG:-"${HBASE_LOG_DIR}/${HBASE_LOGFILE}"}
169 HBASE_PID=$HBASE_PID_DIR/hbase-$HBASE_IDENT_STRING-$command.pid
170 export HBASE_ZNODE_FILE=$HBASE_PID_DIR/hbase-$HBASE_IDENT_STRING-$command.znode
171 export HBASE_AUTOSTART_FILE=$HBASE_PID_DIR/hbase-$HBASE_IDENT_STRING-$command.autostart
173 if [ -n "$SERVER_GC_OPTS" ]; then
174 export SERVER_GC_OPTS=${SERVER_GC_OPTS/"-Xloggc:<FILE-PATH>"/"-Xloggc:${HBASE_LOGGC}"}
176 if [ -n "$CLIENT_GC_OPTS" ]; then
177 export CLIENT_GC_OPTS=${CLIENT_GC_OPTS/"-Xloggc:<FILE-PATH>"/"-Xloggc:${HBASE_LOGGC}"}
180 # Set default scheduling priority
181 if [ "$HBASE_NICENESS" = "" ]; then
182 export HBASE_NICENESS=0
185 thiscmd="$bin/$(basename ${BASH_SOURCE-$0})"
186 args=$@
188 case $startStop in
190 (start)
191 check_before_start
192 hbase_rotate_log $HBASE_LOGOUT
193 hbase_rotate_log $HBASE_LOGGC
194 echo running $command, logging to $HBASE_LOGOUT
195 $thiscmd --config "${HBASE_CONF_DIR}" \
196 foreground_start $command $args < /dev/null > ${HBASE_LOGOUT} 2>&1 &
197 disown -h -r
198 sleep 1; head "${HBASE_LOGOUT}"
201 (autostart)
202 check_before_start
203 hbase_rotate_log $HBASE_LOGOUT
204 hbase_rotate_log $HBASE_LOGGC
205 echo running $command, logging to $HBASE_LOGOUT
206 nohup $thiscmd --config "${HBASE_CONF_DIR}" --autostart-window-size ${AUTOSTART_WINDOW_SIZE} --autostart-window-retry-limit ${AUTOSTART_WINDOW_RETRY_LIMIT} \
207 internal_autostart $command $args < /dev/null > ${HBASE_LOGOUT} 2>&1 &
210 (autorestart)
211 echo running $command, logging to $HBASE_LOGOUT
212 # stop the command
213 $thiscmd --config "${HBASE_CONF_DIR}" stop $command $args &
214 wait_until_done $!
215 # wait a user-specified sleep period
216 sp=${HBASE_RESTART_SLEEP:-3}
217 if [ $sp -gt 0 ]; then
218 sleep $sp
221 check_before_start
222 hbase_rotate_log $HBASE_LOGOUT
223 nohup $thiscmd --config "${HBASE_CONF_DIR}" --autostart-window-size ${AUTOSTART_WINDOW_SIZE} --autostart-window-retry-limit ${AUTOSTART_WINDOW_RETRY_LIMIT} \
224 internal_autostart $command $args < /dev/null > ${HBASE_LOGOUT} 2>&1 &
227 (foreground_start)
228 trap cleanAfterRun SIGHUP SIGINT SIGTERM EXIT
229 if [ "$HBASE_NO_REDIRECT_LOG" != "" ]; then
230 # NO REDIRECT
231 echo "`date` Starting $command on `hostname`"
232 echo "`ulimit -a`"
233 # in case the parent shell gets the kill make sure to trap signals.
234 # Only one will get called. Either the trap or the flow will go through.
235 nice -n $HBASE_NICENESS "$HBASE_HOME"/bin/hbase \
236 --config "${HBASE_CONF_DIR}" \
237 $command "$@" start &
238 else
239 echo "`date` Starting $command on `hostname`" >> ${HBASE_LOGLOG}
240 echo "`ulimit -a`" >> "$HBASE_LOGLOG" 2>&1
241 # in case the parent shell gets the kill make sure to trap signals.
242 # Only one will get called. Either the trap or the flow will go through.
243 nice -n $HBASE_NICENESS "$HBASE_HOME"/bin/hbase \
244 --config "${HBASE_CONF_DIR}" \
245 $command "$@" start >> ${HBASE_LOGOUT} 2>&1 &
247 # Add to the command log file vital stats on our environment.
248 hbase_pid=$!
249 echo $hbase_pid > ${HBASE_PID}
250 wait $hbase_pid
253 (internal_autostart)
254 ONE_HOUR_IN_SECS=3600
255 autostartWindowStartDate=`date +%s`
256 autostartCount=0
257 touch "$HBASE_AUTOSTART_FILE"
259 # keep starting the command until asked to stop. Reloop on software crash
260 while true
262 hbase_rotate_log $HBASE_LOGGC
263 if [ -f $HBASE_PID ] && kill -0 "$(cat "$HBASE_PID")" > /dev/null 2>&1 ; then
264 wait "$(cat "$HBASE_PID")"
265 else
266 #if the file does not exist it means that it was not stopped properly by the stop command
267 if [ ! -f "$HBASE_AUTOSTART_FILE" ]; then
268 echo "`date` HBase might be stopped removing the autostart file. Exiting Autostart process" >> ${HBASE_LOGOUT}
269 exit 1
272 echo "`date` Autostarting hbase $command service. Attempt no: $(( $autostartCount + 1))" >> ${HBASE_LOGLOG}
273 touch "$HBASE_AUTOSTART_FILE"
274 $thiscmd --config "${HBASE_CONF_DIR}" foreground_start $command $args
275 autostartCount=$(( $autostartCount + 1 ))
277 # HBASE-6504 - only take the first line of the output in case verbose gc is on
278 distMode=`$bin/hbase --config "$HBASE_CONF_DIR" org.apache.hadoop.hbase.util.HBaseConfTool hbase.cluster.distributed | head -n 1`
280 if [ "$distMode" != 'false' ]; then
281 #if the cluster is being stopped then do not restart it again.
282 zparent=`$bin/hbase org.apache.hadoop.hbase.util.HBaseConfTool zookeeper.znode.parent`
283 if [ "$zparent" == "null" ]; then zparent="/hbase"; fi
284 zkrunning=`$bin/hbase org.apache.hadoop.hbase.util.HBaseConfTool zookeeper.znode.state`
285 if [ "$zkrunning" == "null" ]; then zkrunning="running"; fi
286 zkFullRunning=$zparent/$zkrunning
287 $bin/hbase zkcli stat $zkFullRunning 2>&1 | grep "Node does not exist" 1>/dev/null 2>&1
289 #grep returns 0 if it found something, 1 otherwise
290 if [ $? -eq 0 ]; then
291 echo "`date` hbase znode does not exist. Exiting Autostart process" >> ${HBASE_LOGOUT}
292 rm -f "$HBASE_AUTOSTART_FILE"
293 exit 1
296 #If ZooKeeper cannot be found, then do not restart
297 $bin/hbase zkcli stat $zkFullRunning 2>&1 | grep Exception | grep ConnectionLoss 1>/dev/null 2>&1
298 if [ $? -eq 0 ]; then
299 echo "`date` zookeeper not found. Exiting Autostart process" >> ${HBASE_LOGOUT}
300 rm -f "$HBASE_AUTOSTART_FILE"
301 exit 1
306 curDate=`date +%s`
307 autostartWindowReset=false
309 # reset the auto start window size if it exceeds
310 if [ $AUTOSTART_WINDOW_SIZE -gt 0 ] && [ $(( $curDate - $autostartWindowStartDate )) -gt $(( $AUTOSTART_WINDOW_SIZE * $ONE_HOUR_IN_SECS )) ]; then
311 echo "Resetting Autorestart window size: $autostartWindowStartDate" >> ${HBASE_LOGOUT}
312 autostartWindowStartDate=$curDate
313 autostartWindowReset=true
314 autostartCount=0
317 # kill autostart if the retry limit is exceeded within the given window size (window size other then 0)
318 if ! $autostartWindowReset && [ $AUTOSTART_WINDOW_RETRY_LIMIT -gt 0 ] && [ $autostartCount -gt $AUTOSTART_WINDOW_RETRY_LIMIT ]; then
319 echo "`date` Autostart window retry limit: $AUTOSTART_WINDOW_RETRY_LIMIT exceeded for given window size: $AUTOSTART_WINDOW_SIZE hours.. Exiting..." >> ${HBASE_LOGLOG}
320 rm -f "$HBASE_AUTOSTART_FILE"
321 exit 1
324 # wait for shutdown hook to complete
325 sleep 20
326 done
329 (stop)
330 echo running $command, logging to $HBASE_LOGOUT
331 rm -f "$HBASE_AUTOSTART_FILE"
332 if [ -f $HBASE_PID ]; then
333 pidToKill=`cat $HBASE_PID`
334 # kill -0 == see if the PID exists
335 if kill -0 $pidToKill > /dev/null 2>&1; then
336 echo -n stopping $command
337 echo "`date` Terminating $command" >> $HBASE_LOGLOG
338 kill $pidToKill > /dev/null 2>&1
339 waitForProcessEnd $pidToKill $command
340 else
341 retval=$?
342 echo no $command to stop because kill -0 of pid $pidToKill failed with status $retval
344 else
345 echo no $command to stop because no pid file $HBASE_PID
347 rm -f $HBASE_PID
350 (restart)
351 echo running $command, logging to $HBASE_LOGOUT
352 # stop the command
353 $thiscmd --config "${HBASE_CONF_DIR}" stop $command $args &
354 wait_until_done $!
355 # wait a user-specified sleep period
356 sp=${HBASE_RESTART_SLEEP:-3}
357 if [ $sp -gt 0 ]; then
358 sleep $sp
360 # start the command
361 $thiscmd --config "${HBASE_CONF_DIR}" start $command $args &
362 wait_until_done $!
366 echo $usage
367 exit 1
369 esac