From be038f34849e8af9006bab87e777744ab121d00b Mon Sep 17 00:00:00 2001 From: Sean Busbey Date: Thu, 24 May 2018 09:20:13 -0500 Subject: [PATCH 1/2] WIP fix shellcheck concerns on bin/considerAsDead.sh --- bin/considerAsDead.sh | 59 ++++++++++++++++++++++++++++++++++----------------- 1 file changed, 39 insertions(+), 20 deletions(-) diff --git a/bin/considerAsDead.sh b/bin/considerAsDead.sh index ae1b8d885b..873bc7ef86 100755 --- a/bin/considerAsDead.sh +++ b/bin/considerAsDead.sh @@ -17,45 +17,64 @@ # * See the License for the specific language governing permissions and # * limitations under the License. # */ -# +# -usage="Usage: considerAsDead.sh --hostname serverName" +usage="Usage: considerAsDead.sh serverName [serverName [...]]" +set -e # if no args specified, show usage if [ $# -le 1 ]; then - echo $usage + echo "${usage}" exit 1 fi -bin=`dirname "${BASH_SOURCE-$0}"` -bin=`cd "$bin">/dev/null; pwd` +bin=$(dirname "${BASH_SOURCE-$0}") +bin=$(cd "$bin">/dev/null; pwd) + +# shellcheck source=bin/hbase-config.sh +. "${bin}"/hbase-config.sh -. $bin/hbase-config.sh +## @param needle what you're looking for +## @param haystack where you're looking +## @audience private +## @stability stable +function considerAsDead_array_contains { + declare needle=$1 + shift + declare hay + for hay in "${@}"; do + if [[ "${needle}" == "${hay}" ]]; then + return 0 + fi + done + return 1 +} shift -deadhost=$@ +deadhosts=("$@") remote_cmd="cd ${HBASE_HOME}; $bin/hbase-daemon.sh --config ${HBASE_CONF_DIR} restart" -zparent=`$bin/hbase org.apache.hadoop.hbase.util.HBaseConfTool zookeeper.znode.parent` +zparent=$("${bin}/hbase" org.apache.hadoop.hbase.util.HBaseConfTool zookeeper.znode.parent) if [ "$zparent" == "null" ]; then zparent="/hbase"; fi -zkrs=`$bin/hbase org.apache.hadoop.hbase.util.HBaseConfTool zookeeper.znode.rs` +zkrs=$("${bin}/hbase" org.apache.hadoop.hbase.util.HBaseConfTool zookeeper.znode.rs) if [ "$zkrs" == "null" ]; then zkrs="rs"; fi zkrs="$zparent/$zkrs" -online_regionservers=`$bin/hbase zkcli ls $zkrs 2>&1 | tail -1 | sed "s/\[//" | sed "s/\]//"` +online_regionservers=$("${bin}/hbase" zkcli ls "${zkrs}" 2>&1 | tail -1 | sed 's/\[//' | sed 's/\]//') for rs in $online_regionservers do - rs_parts=(${rs//,/ }) + IFS="," read -r -a rs_parts <<< "${rs}" hostname=${rs_parts[0]} - echo $deadhost - echo $hostname - if [ "$deadhost" == "$hostname" ]; then - znode="$zkrs/$rs" - echo "ZNode Deleting:" $znode - $bin/hbase zkcli delete $znode > /dev/null 2>&1 - sleep 1 - ssh $HBASE_SSH_OPTS $hostname $remote_cmd 2>&1 | sed "s/^/$hostname: /" - fi + if considerAsDead_array_contains "${hostname}" "${deadhosts[@]}" ; then + echo "Found live host ${hostname} in list of hosts to consider dead." + znode="$zkrs/$rs" + echo "ZNode Deleting: '${znode}'" + "${bin}/hbase" zkcli delete "${znode}" > /dev/null 2>&1 + sleep 1 + echo "Attempting remote restart on '${hostname}'." + # shellcheck disable=SC2029 + ssh "${HBASE_SSH_OPTS}" "${hostname}" "${remote_cmd}" 2>&1 | sed "s/^/${hostname}: /" + fi done -- 2.16.1 From d665c1690713b90bbef5e36a2f40e52533c10d48 Mon Sep 17 00:00:00 2001 From: Sean Busbey Date: Thu, 24 May 2018 11:00:04 -0500 Subject: [PATCH 2/2] WIP cleanup shellcheck for bin/hbase-daemon.sh --- bin/hbase-daemon.sh | 203 ++++++++++++++++++++++++++++------------------------ 1 file changed, 108 insertions(+), 95 deletions(-) diff --git a/bin/hbase-daemon.sh b/bin/hbase-daemon.sh index 11c13eb523..bb215403c4 100755 --- a/bin/hbase-daemon.sh +++ b/bin/hbase-daemon.sh @@ -32,6 +32,8 @@ # # Modelled after $HADOOP_HOME/bin/hadoop-daemon.sh +set -e + usage="Usage: hbase-daemon.sh [--config ]\ [--autostart-window-size ]\ [--autostart-window-retry-limit ]\ @@ -40,7 +42,7 @@ usage="Usage: hbase-daemon.sh [--config ]\ # if no args specified, show usage if [ $# -le 1 ]; then - echo $usage + echo "${usage}" exit 1 fi @@ -48,10 +50,12 @@ fi AUTOSTART_WINDOW_SIZE=0 AUTOSTART_WINDOW_RETRY_LIMIT=0 -bin=`dirname "${BASH_SOURCE-$0}"` -bin=`cd "$bin">/dev/null; pwd` +bin=$(dirname "${BASH_SOURCE-$0}") +bin=$(cd "$bin">/dev/null; pwd) +# shellcheck source=bin/hbase-config.sh . "$bin"/hbase-config.sh +# shellcheck source=bin/hbase-common.sh . "$bin"/hbase-common.sh # get arguments @@ -69,8 +73,8 @@ hbase_rotate_log () num=$2 fi if [ -f "$log" ]; then # rotate logs - while [ $num -gt 1 ]; do - prev=`expr $num - 1` + while [ "${num}" -gt 1 ]; do + prev=$(( num - 1 )) [ -f "$log.$prev" ] && mv -f "$log.$prev" "$log.$num" num=$prev done @@ -79,30 +83,30 @@ hbase_rotate_log () } cleanAfterRun() { - if [ -f ${HBASE_PID} ]; then + if [ -f "${HBASE_PID}" ]; then # If the process is still running time to tear it down. - kill -9 `cat ${HBASE_PID}` > /dev/null 2>&1 - rm -f ${HBASE_PID} > /dev/null 2>&1 + kill -9 "$(cat "${HBASE_PID}")" > /dev/null 2>&1 + rm -f "${HBASE_PID}" > /dev/null 2>&1 fi - if [ -f ${HBASE_ZNODE_FILE} ]; then + if [ -f "${HBASE_ZNODE_FILE}" ]; then if [ "$command" = "master" ]; then - HBASE_OPTS="$HBASE_OPTS $HBASE_MASTER_OPTS" $bin/hbase master clear > /dev/null 2>&1 + HBASE_OPTS="$HBASE_OPTS $HBASE_MASTER_OPTS" "${bin}/hbase" master clear > /dev/null 2>&1 else #call ZK to delete the node - ZNODE=`cat ${HBASE_ZNODE_FILE}` - HBASE_OPTS="$HBASE_OPTS $HBASE_REGIONSERVER_OPTS" $bin/hbase zkcli delete ${ZNODE} > /dev/null 2>&1 + ZNODE=$(cat "${HBASE_ZNODE_FILE}") + HBASE_OPTS="$HBASE_OPTS $HBASE_REGIONSERVER_OPTS" "${bin}/hbase" zkcli delete "${ZNODE}" > /dev/null 2>&1 fi - rm ${HBASE_ZNODE_FILE} + rm "${HBASE_ZNODE_FILE}" fi } check_before_start(){ #ckeck if the process is not running mkdir -p "$HBASE_PID_DIR" - if [ -f $HBASE_PID ]; then - if kill -0 `cat $HBASE_PID` > /dev/null 2>&1; then - echo $command running as process `cat $HBASE_PID`. Stop it first. + if [ -f "$HBASE_PID" ]; then + if kill -0 "$(cat "$HBASE_PID")" > /dev/null 2>&1; then + echo "${command} running as process $(cat "$HBASE_PID"). Stop it first." exit 1 fi fi @@ -111,15 +115,15 @@ check_before_start(){ wait_until_done () { p=$1 - cnt=${HBASE_SLAVE_TIMEOUT:-300} + cnt="${HBASE_SLAVE_TIMEOUT:-300}" origcnt=$cnt - while kill -0 $p > /dev/null 2>&1; do - if [ $cnt -gt 1 ]; then - cnt=`expr $cnt - 1` + while kill -0 "$p" > /dev/null 2>&1; do + if [ "$cnt" -gt 1 ]; then + cnt=$(( cnt - 1 )) sleep 1 else echo "Process did not complete after $origcnt seconds, killing." - kill -9 $p + kill -9 "$p" exit 1 fi done @@ -142,14 +146,11 @@ fi # Some variables # Work out java location so can print version into log. -if [ "$JAVA_HOME" != "" ]; then - #echo "run java in $JAVA_HOME" - JAVA_HOME=$JAVA_HOME -fi -if [ "$JAVA_HOME" = "" ]; then - echo "Error: JAVA_HOME is not set." +if [ -z "${JAVA_HOME}" ]; then + echo "Error: JAVA_HOME is not set." >&2 exit 1 fi +JAVA_HOME="${JAVA_HOME}" JAVA=$JAVA_HOME/bin/java export HBASE_LOG_PREFIX=hbase-$HBASE_IDENT_STRING-$command-$HOSTNAME @@ -170,6 +171,18 @@ HBASE_PID=$HBASE_PID_DIR/hbase-$HBASE_IDENT_STRING-$command.pid export HBASE_ZNODE_FILE=$HBASE_PID_DIR/hbase-$HBASE_IDENT_STRING-$command.znode export HBASE_AUTOSTART_FILE=$HBASE_PID_DIR/hbase-$HBASE_IDENT_STRING-$command.autostart +function hbase_log { + declare message=$1 + + echo "$(date) ${message}" >>"${HBASE_LOGLOG}" 2>/dev/null +} + +function hbase_out { + declare message=$1 + + echo "$(date) ${message}" >>"${HBASE_LOGOUT}" 2>/dev/null +} + if [ -n "$SERVER_GC_OPTS" ]; then export SERVER_GC_OPTS=${SERVER_GC_OPTS/"-Xloggc:"/"-Xloggc:${HBASE_LOGGC}"} fi @@ -182,133 +195,133 @@ if [ "$HBASE_NICENESS" = "" ]; then export HBASE_NICENESS=0 fi -thiscmd="$bin/$(basename ${BASH_SOURCE-$0})" -args=$@ +thiscmd="$bin/$(basename "${BASH_SOURCE-$0}")" +args=("${@}") + +hbase_log "Java version: $("${JAVA}" -version | tr '\n' '\t')" case $startStop in (start) check_before_start - hbase_rotate_log $HBASE_LOGOUT - hbase_rotate_log $HBASE_LOGGC - echo running $command, logging to $HBASE_LOGOUT - $thiscmd --config "${HBASE_CONF_DIR}" \ - foreground_start $command $args < /dev/null > ${HBASE_LOGOUT} 2>&1 & + hbase_rotate_log "$HBASE_LOGOUT" + hbase_rotate_log "${HBASE_LOGGC}" + echo "running ${command}, logging to ${HBASE_LOGOUT}" + "$thiscmd" --config "${HBASE_CONF_DIR}" \ + foreground_start "${command}" "${args[@]}" < /dev/null > "${HBASE_LOGOUT}" 2>&1 & disown -h -r sleep 1; head "${HBASE_LOGOUT}" ;; (autostart) check_before_start - hbase_rotate_log $HBASE_LOGOUT - hbase_rotate_log $HBASE_LOGGC - echo running $command, logging to $HBASE_LOGOUT - nohup $thiscmd --config "${HBASE_CONF_DIR}" --autostart-window-size ${AUTOSTART_WINDOW_SIZE} --autostart-window-retry-limit ${AUTOSTART_WINDOW_RETRY_LIMIT} \ - internal_autostart $command $args < /dev/null > ${HBASE_LOGOUT} 2>&1 & + hbase_rotate_log "$HBASE_LOGOUT" + hbase_rotate_log "${HBASE_LOGGC}" + echo "running ${command}, logging to ${HBASE_LOGOUT}" + nohup "$thiscmd" --config "${HBASE_CONF_DIR}" --autostart-window-size ${AUTOSTART_WINDOW_SIZE} --autostart-window-retry-limit ${AUTOSTART_WINDOW_RETRY_LIMIT} \ + internal_autostart "${command}" "${args[@]}" < /dev/null > "${HBASE_LOGOUT}" 2>&1 & ;; (autorestart) - echo running $command, logging to $HBASE_LOGOUT + echo "running ${command}, logging to ${HBASE_LOGOUT}" # stop the command - $thiscmd --config "${HBASE_CONF_DIR}" stop $command $args & + "$thiscmd" --config "${HBASE_CONF_DIR}" stop "${command}" "${args[@]}" & wait_until_done $! # wait a user-specified sleep period sp=${HBASE_RESTART_SLEEP:-3} - if [ $sp -gt 0 ]; then - sleep $sp + if [ "${sp}" -gt 0 ]; then + sleep "${sp}" fi check_before_start - hbase_rotate_log $HBASE_LOGOUT - nohup $thiscmd --config "${HBASE_CONF_DIR}" --autostart-window-size ${AUTOSTART_WINDOW_SIZE} --autostart-window-retry-limit ${AUTOSTART_WINDOW_RETRY_LIMIT} \ - internal_autostart $command $args < /dev/null > ${HBASE_LOGOUT} 2>&1 & + hbase_rotate_log "$HBASE_LOGOUT" + nohup "$thiscmd" --config "${HBASE_CONF_DIR}" --autostart-window-size ${AUTOSTART_WINDOW_SIZE} --autostart-window-retry-limit ${AUTOSTART_WINDOW_RETRY_LIMIT} \ + internal_autostart "${command}" "${args[@]}" < /dev/null > "${HBASE_LOGOUT}" 2>&1 & ;; (foreground_start) trap cleanAfterRun SIGHUP SIGINT SIGTERM EXIT if [ "$HBASE_NO_REDIRECT_LOG" != "" ]; then # NO REDIRECT - echo "`date` Starting $command on `hostname`" - echo "`ulimit -a`" + echo "$(date$) Starting $command on $(hostname)" + ulimit -a # in case the parent shell gets the kill make sure to trap signals. # Only one will get called. Either the trap or the flow will go through. - nice -n $HBASE_NICENESS "$HBASE_HOME"/bin/hbase \ + nice -n "${HBASE_NICENESS}" "${HBASE_HOME}"/bin/hbase \ --config "${HBASE_CONF_DIR}" \ - $command "$@" start & + "${command}" "$@" start & else - echo "`date` Starting $command on `hostname`" >> ${HBASE_LOGLOG} - echo "`ulimit -a`" >> "$HBASE_LOGLOG" 2>&1 + hbase_log "Starting ${command} on $(hostname)" + hbase_log "$(ulimit -a 2>&1)" # in case the parent shell gets the kill make sure to trap signals. # Only one will get called. Either the trap or the flow will go through. - nice -n $HBASE_NICENESS "$HBASE_HOME"/bin/hbase \ + nice -n "${HBASE_NICENESS}" "${HBASE_HOME}"/bin/hbase \ --config "${HBASE_CONF_DIR}" \ - $command "$@" start >> ${HBASE_LOGOUT} 2>&1 & + "${command}" "$@" start >> "${HBASE_LOGOUT}" 2>&1 & fi # Add to the command log file vital stats on our environment. hbase_pid=$! - echo $hbase_pid > ${HBASE_PID} - wait $hbase_pid + echo "${hbase_pid}" > "${HBASE_PID}" + wait "${hbase_pid}" ;; (internal_autostart) ONE_HOUR_IN_SECS=3600 - autostartWindowStartDate=`date +%s` + autostartWindowStartDate=$(date +%s) autostartCount=0 touch "$HBASE_AUTOSTART_FILE" # keep starting the command until asked to stop. Reloop on software crash while true do - hbase_rotate_log $HBASE_LOGGC - if [ -f $HBASE_PID ] && kill -0 "$(cat "$HBASE_PID")" > /dev/null 2>&1 ; then + hbase_rotate_log "${HBASE_LOGGC}" + if [ -f "${HBASE_PID}" ] && kill -0 "$(cat "$HBASE_PID")" > /dev/null 2>&1 ; then wait "$(cat "$HBASE_PID")" else #if the file does not exist it means that it was not stopped properly by the stop command if [ ! -f "$HBASE_AUTOSTART_FILE" ]; then - echo "`date` HBase might be stopped removing the autostart file. Exiting Autostart process" >> ${HBASE_LOGOUT} + hbase_out "HBase might be stopped removing the autostart file. Exiting Autostart process" exit 1 fi - echo "`date` Autostarting hbase $command service. Attempt no: $(( $autostartCount + 1))" >> ${HBASE_LOGLOG} + hbase_log "Autostarting hbase ${command} service. Attempt no: $(( autostartCount + 1))" touch "$HBASE_AUTOSTART_FILE" - $thiscmd --config "${HBASE_CONF_DIR}" foreground_start $command $args - autostartCount=$(( $autostartCount + 1 )) + "$thiscmd" --config "${HBASE_CONF_DIR}" foreground_start "${command}" "${args[@]}" + autostartCount=$(( autostartCount + 1 )) # HBASE-6504 - only take the first line of the output in case verbose gc is on - distMode=`$bin/hbase --config "$HBASE_CONF_DIR" org.apache.hadoop.hbase.util.HBaseConfTool hbase.cluster.distributed | head -n 1` + distMode=$("${bin}/hbase" --config "$HBASE_CONF_DIR" org.apache.hadoop.hbase.util.HBaseConfTool hbase.cluster.distributed | head -n 1) if [ "$distMode" != 'false' ]; then #if the cluster is being stopped then do not restart it again. - zparent=`$bin/hbase org.apache.hadoop.hbase.util.HBaseConfTool zookeeper.znode.parent` + zparent=$("${bin}/hbase" org.apache.hadoop.hbase.util.HBaseConfTool zookeeper.znode.parent) if [ "$zparent" == "null" ]; then zparent="/hbase"; fi - zkrunning=`$bin/hbase org.apache.hadoop.hbase.util.HBaseConfTool zookeeper.znode.state` + zkrunning=$("${bin}/hbase" org.apache.hadoop.hbase.util.HBaseConfTool zookeeper.znode.state) if [ "$zkrunning" == "null" ]; then zkrunning="running"; fi - zkFullRunning=$zparent/$zkrunning - $bin/hbase zkcli stat $zkFullRunning 2>&1 | grep "Node does not exist" 1>/dev/null 2>&1 - + zkFullRunning="${zparent}/${zkrunning}" + #grep returns 0 if it found something, 1 otherwise - if [ $? -eq 0 ]; then - echo "`date` hbase znode does not exist. Exiting Autostart process" >> ${HBASE_LOGOUT} + if "${bin}/hbase" zkcli stat "${zkFullRunning}" 2>&1 | grep "Node does not exist" 1>/dev/null 2>&1 ; then + hbase_out "hbase znode does not exist. Exiting Autostart process" rm -f "$HBASE_AUTOSTART_FILE" exit 1 fi #If ZooKeeper cannot be found, then do not restart - $bin/hbase zkcli stat $zkFullRunning 2>&1 | grep Exception | grep ConnectionLoss 1>/dev/null 2>&1 - if [ $? -eq 0 ]; then - echo "`date` zookeeper not found. Exiting Autostart process" >> ${HBASE_LOGOUT} + if "${bin}/hbase" zkcli stat "${zkFullRunning}" 2>&1 | grep Exception | grep ConnectionLoss 1>/dev/null 2>&1 ; then + hbase_out "zookeeper not found. Exiting Autostart process" rm -f "$HBASE_AUTOSTART_FILE" exit 1 fi fi fi - curDate=`date +%s` + curDate=$(date +%s) autostartWindowReset=false # reset the auto start window size if it exceeds - if [ $AUTOSTART_WINDOW_SIZE -gt 0 ] && [ $(( $curDate - $autostartWindowStartDate )) -gt $(( $AUTOSTART_WINDOW_SIZE * $ONE_HOUR_IN_SECS )) ]; then - echo "Resetting Autorestart window size: $autostartWindowStartDate" >> ${HBASE_LOGOUT} + if [ $AUTOSTART_WINDOW_SIZE -gt 0 ] && [ $(( curDate - autostartWindowStartDate )) -gt $(( AUTOSTART_WINDOW_SIZE * ONE_HOUR_IN_SECS )) ]; then + hbase_out "Resetting Autorestart window size: $autostartWindowStartDate" autostartWindowStartDate=$curDate autostartWindowReset=true autostartCount=0 @@ -316,7 +329,7 @@ case $startStop in # kill autostart if the retry limit is exceeded within the given window size (window size other then 0) if ! $autostartWindowReset && [ $AUTOSTART_WINDOW_RETRY_LIMIT -gt 0 ] && [ $autostartCount -gt $AUTOSTART_WINDOW_RETRY_LIMIT ]; then - echo "`date` Autostart window retry limit: $AUTOSTART_WINDOW_RETRY_LIMIT exceeded for given window size: $AUTOSTART_WINDOW_SIZE hours.. Exiting..." >> ${HBASE_LOGLOG} + hbase_log "Autostart window retry limit: ${AUTOSTART_WINDOW_RETRY_LIMIT} exceeded for given window size: ${AUTOSTART_WINDOW_SIZE} hours.. Exiting..." rm -f "$HBASE_AUTOSTART_FILE" exit 1 fi @@ -327,43 +340,43 @@ case $startStop in ;; (stop) - echo running $command, logging to $HBASE_LOGOUT + echo "running ${command}, logging to ${HBASE_LOGOUT}" rm -f "$HBASE_AUTOSTART_FILE" - if [ -f $HBASE_PID ]; then - pidToKill=`cat $HBASE_PID` + if [ -f "${HBASE_PID}" ]; then + pidToKill=$(cat "${HBASE_PID}") # kill -0 == see if the PID exists - if kill -0 $pidToKill > /dev/null 2>&1; then - echo -n stopping $command - echo "`date` Terminating $command" >> $HBASE_LOGLOG - kill $pidToKill > /dev/null 2>&1 - waitForProcessEnd $pidToKill $command + if kill -0 "${pidToKill}" > /dev/null 2>&1; then + echo -n "stopping $command" + hbase_log "Terminating ${command}" + kill "${pidToKill}" > /dev/null 2>&1 + waitForProcessEnd "${pidToKill}" "${command}" else retval=$? - echo no $command to stop because kill -0 of pid $pidToKill failed with status $retval + echo "no $command to stop because kill -0 of pid $pidToKill failed with status $retval" fi else - echo no $command to stop because no pid file $HBASE_PID + echo "no $command to stop because no pid file $HBASE_PID" fi - rm -f $HBASE_PID + rm -f "${HBASE_PID}" ;; (restart) - echo running $command, logging to $HBASE_LOGOUT + echo "running ${command}, logging to ${HBASE_LOGOUT}" # stop the command - $thiscmd --config "${HBASE_CONF_DIR}" stop $command $args & + "$thiscmd" --config "${HBASE_CONF_DIR}" stop "${command}" "${args[@]}" & wait_until_done $! # wait a user-specified sleep period sp=${HBASE_RESTART_SLEEP:-3} - if [ $sp -gt 0 ]; then - sleep $sp + if [ "${sp}" -gt 0 ]; then + sleep "${sp}" fi # start the command - $thiscmd --config "${HBASE_CONF_DIR}" start $command $args & + "$thiscmd" --config "${HBASE_CONF_DIR}" start "${command}" "${args[@]}" & wait_until_done $! ;; (*) - echo $usage + echo "$usage" exit 1 ;; esac -- 2.16.1