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}" ]; 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})"
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 &
198 sleep 1; head "${HBASE_LOGOUT}"
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 &
211 echo running $command, logging to $HBASE_LOGOUT
213 $thiscmd --config "${HBASE_CONF_DIR}" stop $command $args &
215 # wait a user-specified sleep period
216 sp=${HBASE_RESTART_SLEEP:-3}
217 if [ $sp -gt 0 ]; then
222 hbase_rotate_log $HBASE_LOGOUT
223 hbase_rotate_log $HBASE_LOGGC
224 nohup $thiscmd --config "${HBASE_CONF_DIR}" --autostart-window-size ${AUTOSTART_WINDOW_SIZE} --autostart-window-retry-limit ${AUTOSTART_WINDOW_RETRY_LIMIT} \
225 internal_autostart
$command $args < /dev
/null
> ${HBASE_LOGOUT} 2>&1 &
229 trap cleanAfterRun SIGHUP SIGINT SIGTERM EXIT
230 if [ "$HBASE_NO_REDIRECT_LOG" != "" ]; then
232 echo "`date` Starting $command on `hostname`"
234 # in case the parent shell gets the kill make sure to trap signals.
235 # Only one will get called. Either the trap or the flow will go through.
236 nice
-n $HBASE_NICENESS "$HBASE_HOME"/bin
/hbase \
237 --config "${HBASE_CONF_DIR}" \
238 $command "$@" start
&
240 echo "`date` Starting $command on `hostname`" >> ${HBASE_LOGLOG}
241 echo "`ulimit -a`" >> "$HBASE_LOGLOG" 2>&1
242 # in case the parent shell gets the kill make sure to trap signals.
243 # Only one will get called. Either the trap or the flow will go through.
244 nice
-n $HBASE_NICENESS "$HBASE_HOME"/bin
/hbase \
245 --config "${HBASE_CONF_DIR}" \
246 $command "$@" start
>> ${HBASE_LOGOUT} 2>&1 &
248 # Add to the command log file vital stats on our environment.
250 echo $hbase_pid > ${HBASE_PID}
255 ONE_HOUR_IN_SECS
=3600
256 autostartWindowStartDate
=`date +%s`
258 touch "$HBASE_AUTOSTART_FILE"
260 # keep starting the command until asked to stop. Reloop on software crash
263 if [ -f $HBASE_PID ] && kill -0 "$(cat "$HBASE_PID")" > /dev
/null
2>&1 ; then
264 wait "$(cat "$HBASE_PID")"
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}
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"
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"
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
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"
324 # wait for shutdown hook to complete
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
342 echo no
$command to stop because
kill -0 of pid
$pidToKill failed with status
$retval
345 echo no
$command to stop because no pid
file $HBASE_PID
351 echo running
$command, logging to
$HBASE_LOGOUT
353 $thiscmd --config "${HBASE_CONF_DIR}" stop
$command $args &
355 # wait a user-specified sleep period
356 sp
=${HBASE_RESTART_SLEEP:-3}
357 if [ $sp -gt 0 ]; then
361 $thiscmd --config "${HBASE_CONF_DIR}" start
$command $args &