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 # 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> \
41 # if no args specified, show usage
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
71 if [ -f "$log" ]; then # rotate logs
72 while [ $num -gt 1 ]; do
74 [ -f "$log.$prev" ] && mv -f "$log.$prev" "$log.$num"
77 mv -f "$log" "$log.$num";
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
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.
114 cnt
=${HBASE_SLAVE_TIMEOUT:-300}
116 while kill -0 $p > /dev
/null
2>&1; do
117 if [ $cnt -gt 1 ]; then
121 echo "Process did not complete after $origcnt seconds, killing."
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
139 if [ "$HBASE_IDENT_STRING" = "" ]; then
140 export HBASE_IDENT_STRING
="$USER"
144 # Work out java location so can print version into log.
145 if [ "$JAVA_HOME" != "" ]; then
146 #echo "run java in $JAVA_HOME"
149 if [ "$JAVA_HOME" = "" ]; then
150 echo "Error: JAVA_HOME is not set."
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})"
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 &
206 sleep 1; head "${HBASE_LOGOUT}"
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 &
219 echo running $command, logging to $HBASE_LOGOUT
221 $thiscmd --config "${HBASE_CONF_DIR}" stop $command $args &
223 # wait a user-specified sleep period
224 sp=${HBASE_RESTART_SLEEP:-3}
225 if [ $sp -gt 0 ]; then
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 &
236 trap cleanAfterRun SIGHUP SIGINT SIGTERM EXIT
237 if [ "$HBASE_NO_REDIRECT_LOG" != "" ]; then
239 echo "`date` Starting $command on `hostname`"
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
&
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.
257 echo $hbase_pid > ${HBASE_PID}
262 ONE_HOUR_IN_SECS
=3600
263 autostartWindowStartDate
=`date +%s`
265 touch "$HBASE_AUTOSTART_FILE"
267 # keep starting the command until asked to stop. Reloop on software crash
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")"
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}
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"
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"
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
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"
332 # wait for shutdown hook to complete
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
350 echo no
$command to stop because
kill -0 of pid
$pidToKill failed with status
$retval
353 echo no
$command to stop because no pid
file $HBASE_PID
359 echo running
$command, logging to
$HBASE_LOGOUT
361 $thiscmd --config "${HBASE_CONF_DIR}" stop
$command $args &
363 # wait a user-specified sleep period
364 sp
=${HBASE_RESTART_SLEEP:-3}
365 if [ $sp -gt 0 ]; then
369 $thiscmd --config "${HBASE_CONF_DIR}" start
$command $args &