HBASE-26080 Implement a new mini cluster class for end users (#3470)
[hbase.git] / bin / hbase-daemon.sh
blob6fafab0ccec014e6bb03441f268d4305ab89e3de
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_LEVEL}" ]; then
159 export HBASE_ROOT_LOGGER_LEVEL=${HBASE_ROOT_LOGGER_LEVEL:-"INFO"}
162 if [ -z "${HBASE_ROOT_LOGGER_APPENDER}" ]; then
163 export HBASE_ROOT_LOGGER_APPENDER=${HBASE_ROOT_LOGGER_APPENDER:-"RFA"}
166 if [ -z "${HBASE_SECURITY_LOGGER_LEVEL}" ]; then
167 export HBASE_SECURITY_LOGGER_LEVEL=${HBASE_SECURITY_LOGGER_LEVEL:-"INFO"}
170 if [ -z "${HBASE_SECURITY_LOGGER_APPENDER}" ]; then
171 export HBASE_SECURITY_LOGGER_APPENDER=${HBASE_SECURITY_LOGGER_APPENDER:-"RFAS"}
174 HBASE_LOGOUT=${HBASE_LOGOUT:-"$HBASE_LOG_DIR/$HBASE_LOG_PREFIX.out"}
175 HBASE_LOGGC=${HBASE_LOGGC:-"$HBASE_LOG_DIR/$HBASE_LOG_PREFIX.gc"}
176 HBASE_LOGLOG=${HBASE_LOGLOG:-"${HBASE_LOG_DIR}/${HBASE_LOGFILE}"}
177 HBASE_PID=$HBASE_PID_DIR/hbase-$HBASE_IDENT_STRING-$command.pid
178 export HBASE_ZNODE_FILE=$HBASE_PID_DIR/hbase-$HBASE_IDENT_STRING-$command.znode
179 export HBASE_AUTOSTART_FILE=$HBASE_PID_DIR/hbase-$HBASE_IDENT_STRING-$command.autostart
181 if [ -n "$SERVER_GC_OPTS" ]; then
182 export SERVER_GC_OPTS=${SERVER_GC_OPTS/"-Xloggc:<FILE-PATH>"/"-Xloggc:${HBASE_LOGGC}"}
184 if [ -n "$CLIENT_GC_OPTS" ]; then
185 export CLIENT_GC_OPTS=${CLIENT_GC_OPTS/"-Xloggc:<FILE-PATH>"/"-Xloggc:${HBASE_LOGGC}"}
188 # Set default scheduling priority
189 if [ "$HBASE_NICENESS" = "" ]; then
190 export HBASE_NICENESS=0
193 thiscmd="$bin/$(basename ${BASH_SOURCE-$0})"
194 args=$@
196 case $startStop in
198 (start)
199 check_before_start
200 hbase_rotate_log $HBASE_LOGOUT
201 hbase_rotate_log $HBASE_LOGGC
202 echo running $command, logging to $HBASE_LOGOUT
203 $thiscmd --config "${HBASE_CONF_DIR}" \
204 foreground_start $command $args < /dev/null > ${HBASE_LOGOUT} 2>&1 &
205 disown -h -r
206 sleep 1; head "${HBASE_LOGOUT}"
209 (autostart)
210 check_before_start
211 hbase_rotate_log $HBASE_LOGOUT
212 hbase_rotate_log $HBASE_LOGGC
213 echo running $command, logging to $HBASE_LOGOUT
214 nohup $thiscmd --config "${HBASE_CONF_DIR}" --autostart-window-size ${AUTOSTART_WINDOW_SIZE} --autostart-window-retry-limit ${AUTOSTART_WINDOW_RETRY_LIMIT} \
215 internal_autostart $command $args < /dev/null > ${HBASE_LOGOUT} 2>&1 &
218 (autorestart)
219 echo running $command, logging to $HBASE_LOGOUT
220 # stop the command
221 $thiscmd --config "${HBASE_CONF_DIR}" stop $command $args &
222 wait_until_done $!
223 # wait a user-specified sleep period
224 sp=${HBASE_RESTART_SLEEP:-3}
225 if [ $sp -gt 0 ]; then
226 sleep $sp
229 check_before_start
230 hbase_rotate_log $HBASE_LOGOUT
231 nohup $thiscmd --config "${HBASE_CONF_DIR}" --autostart-window-size ${AUTOSTART_WINDOW_SIZE} --autostart-window-retry-limit ${AUTOSTART_WINDOW_RETRY_LIMIT} \
232 internal_autostart $command $args < /dev/null > ${HBASE_LOGOUT} 2>&1 &
235 (foreground_start)
236 trap cleanAfterRun SIGHUP SIGINT SIGTERM EXIT
237 if [ "$HBASE_NO_REDIRECT_LOG" != "" ]; then
238 # NO REDIRECT
239 echo "`date` Starting $command on `hostname`"
240 echo "`ulimit -a`"
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 &
246 else
247 echo "`date` Starting $command on `hostname`" >> ${HBASE_LOGLOG}
248 echo "`ulimit -a`" >> "$HBASE_LOGLOG" 2>&1
249 # in case the parent shell gets the kill make sure to trap signals.
250 # Only one will get called. Either the trap or the flow will go through.
251 nice -n $HBASE_NICENESS "$HBASE_HOME"/bin/hbase \
252 --config "${HBASE_CONF_DIR}" \
253 $command "$@" start >> ${HBASE_LOGOUT} 2>&1 &
255 # Add to the command log file vital stats on our environment.
256 hbase_pid=$!
257 echo $hbase_pid > ${HBASE_PID}
258 wait $hbase_pid
261 (internal_autostart)
262 ONE_HOUR_IN_SECS=3600
263 autostartWindowStartDate=`date +%s`
264 autostartCount=0
265 touch "$HBASE_AUTOSTART_FILE"
267 # keep starting the command until asked to stop. Reloop on software crash
268 while true
270 hbase_rotate_log $HBASE_LOGGC
271 if [ -f $HBASE_PID ] && kill -0 "$(cat "$HBASE_PID")" > /dev/null 2>&1 ; then
272 wait "$(cat "$HBASE_PID")"
273 else
274 #if the file does not exist it means that it was not stopped properly by the stop command
275 if [ ! -f "$HBASE_AUTOSTART_FILE" ]; then
276 echo "`date` HBase might be stopped removing the autostart file. Exiting Autostart process" >> ${HBASE_LOGOUT}
277 exit 1
280 echo "`date` Autostarting hbase $command service. Attempt no: $(( $autostartCount + 1))" >> ${HBASE_LOGLOG}
281 touch "$HBASE_AUTOSTART_FILE"
282 $thiscmd --config "${HBASE_CONF_DIR}" foreground_start $command $args
283 autostartCount=$(( $autostartCount + 1 ))
285 # HBASE-6504 - only take the first line of the output in case verbose gc is on
286 distMode=`$bin/hbase --config "$HBASE_CONF_DIR" org.apache.hadoop.hbase.util.HBaseConfTool hbase.cluster.distributed | head -n 1`
288 if [ "$distMode" != 'false' ]; then
289 #if the cluster is being stopped then do not restart it again.
290 zparent=`$bin/hbase org.apache.hadoop.hbase.util.HBaseConfTool zookeeper.znode.parent`
291 if [ "$zparent" == "null" ]; then zparent="/hbase"; fi
292 zkrunning=`$bin/hbase org.apache.hadoop.hbase.util.HBaseConfTool zookeeper.znode.state`
293 if [ "$zkrunning" == "null" ]; then zkrunning="running"; fi
294 zkFullRunning=$zparent/$zkrunning
295 $bin/hbase zkcli stat $zkFullRunning 2>&1 | grep "Node does not exist" 1>/dev/null 2>&1
297 #grep returns 0 if it found something, 1 otherwise
298 if [ $? -eq 0 ]; then
299 echo "`date` hbase znode does not exist. Exiting Autostart process" >> ${HBASE_LOGOUT}
300 rm -f "$HBASE_AUTOSTART_FILE"
301 exit 1
304 #If ZooKeeper cannot be found, then do not restart
305 $bin/hbase zkcli stat $zkFullRunning 2>&1 | grep Exception | grep ConnectionLoss 1>/dev/null 2>&1
306 if [ $? -eq 0 ]; then
307 echo "`date` zookeeper not found. Exiting Autostart process" >> ${HBASE_LOGOUT}
308 rm -f "$HBASE_AUTOSTART_FILE"
309 exit 1
314 curDate=`date +%s`
315 autostartWindowReset=false
317 # reset the auto start window size if it exceeds
318 if [ $AUTOSTART_WINDOW_SIZE -gt 0 ] && [ $(( $curDate - $autostartWindowStartDate )) -gt $(( $AUTOSTART_WINDOW_SIZE * $ONE_HOUR_IN_SECS )) ]; then
319 echo "Resetting Autorestart window size: $autostartWindowStartDate" >> ${HBASE_LOGOUT}
320 autostartWindowStartDate=$curDate
321 autostartWindowReset=true
322 autostartCount=0
325 # kill autostart if the retry limit is exceeded within the given window size (window size other then 0)
326 if ! $autostartWindowReset && [ $AUTOSTART_WINDOW_RETRY_LIMIT -gt 0 ] && [ $autostartCount -gt $AUTOSTART_WINDOW_RETRY_LIMIT ]; then
327 echo "`date` Autostart window retry limit: $AUTOSTART_WINDOW_RETRY_LIMIT exceeded for given window size: $AUTOSTART_WINDOW_SIZE hours.. Exiting..." >> ${HBASE_LOGLOG}
328 rm -f "$HBASE_AUTOSTART_FILE"
329 exit 1
332 # wait for shutdown hook to complete
333 sleep 20
334 done
337 (stop)
338 echo running $command, logging to $HBASE_LOGOUT
339 rm -f "$HBASE_AUTOSTART_FILE"
340 if [ -f $HBASE_PID ]; then
341 pidToKill=`cat $HBASE_PID`
342 # kill -0 == see if the PID exists
343 if kill -0 $pidToKill > /dev/null 2>&1; then
344 echo -n stopping $command
345 echo "`date` Terminating $command" >> $HBASE_LOGLOG
346 kill $pidToKill > /dev/null 2>&1
347 waitForProcessEnd $pidToKill $command
348 else
349 retval=$?
350 echo no $command to stop because kill -0 of pid $pidToKill failed with status $retval
352 else
353 echo no $command to stop because no pid file $HBASE_PID
355 rm -f $HBASE_PID
358 (restart)
359 echo running $command, logging to $HBASE_LOGOUT
360 # stop the command
361 $thiscmd --config "${HBASE_CONF_DIR}" stop $command $args &
362 wait_until_done $!
363 # wait a user-specified sleep period
364 sp=${HBASE_RESTART_SLEEP:-3}
365 if [ $sp -gt 0 ]; then
366 sleep $sp
368 # start the command
369 $thiscmd --config "${HBASE_CONF_DIR}" start $command $args &
370 wait_until_done $!
374 echo $usage
375 exit 1
377 esac