Skip to content
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 29 additions & 12 deletions bin/zkCleanup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,44 @@
# use POSIX interface, symlink is followed automatically
ZOOBIN="${BASH_SOURCE-$0}"
ZOOBIN="$(dirname "$ZOOBIN")"
ZOOBINDIR="$(cd "$ZOOBIN" && pwd)"
ZOOBINDIR="$(cd "$ZOOBIN" || exit && pwd)"

if [[ -e "$ZOOBIN/../libexec/zkEnv.sh" ]]; then
# shellcheck source=bin/zkEnv.sh
. "$ZOOBINDIR"/../libexec/zkEnv.sh
. "$ZOOBINDIR"/../libexec/zkEnv.sh "$@"
else
# shellcheck source=bin/zkEnv.sh
. "$ZOOBINDIR"/zkEnv.sh
. "$ZOOBINDIR"/zkEnv.sh "$@"
fi

ZOODATADIR="$(grep "^[[:space:]]*dataDir=" "$ZOOCFG" | sed -e 's/.*=//')"
ZOODATALOGDIR="$(grep "^[[:space:]]*dataLogDir=" "$ZOOCFG" | sed -e 's/.*=//')"
ZOODATADIR=""
ZOODATALOGDIR=""

# Only try to read config if ZOOCFG exists
if [[ -f $ZOOCFG ]]; then
ZOODATADIR="$(grep "^[[:space:]]*dataDir=" "$ZOOCFG" 2>/dev/null | sed -e 's/.*=//')"
ZOODATALOGDIR="$(grep "^[[:space:]]*dataLogDir=" "$ZOOCFG" 2>/dev/null | sed -e 's/.*=//')"
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is perhaps something for another PR, since it's a bit out of scope here, and there may be other scripts that would also benefit from this change, but I noticed that sed has greedy matching here and doesn't trim the whitespace in the value. cut -f2- -d= could be used instead to avoid the greedy matching, but still doesn't trim whitespace in the extracted property value.

It can be done with bash regular expressions and a simple capture group, though, without calling another process:

  re='^[^=]*=[[:space:]]*(.*)[[:space:]]*$'
  ZOODATADIR="$(grep "^[[:space:]]*dataDir=" "$ZOOCFG" 2>/dev/null)"
  [[ $ZOODATADIR =~ $re ]] && ZOODATADIR="${BASH_REMATCH[1]}"
  ZOODATALOGDIR="$(grep "^[[:space:]]*dataLogDir=" "$ZOOCFG" 2>/dev/null)"
  [[ $ZOODATALOGDIR =~ $re ]] && ZOODATALOGDIR="${BASH_REMATCH[1]}"

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your bash/regex skills are far beyond mine :) I'd never have caught this.

I have included your suggestion as-is. I believe I understand the regex and it looks good to me

e31a646 (this PR)

fi

ZOO_LOG_FILE=zookeeper-$USER-cleanup-$HOSTNAME.log

# shellcheck disable=SC2206
flags=($JVMFLAGS)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like "-Dzookeeper.log.dir=$ZOO_LOG_DIR" "-Dzookeeper.log.file=$ZOO_LOG_FILE" is added in all cases. So, it could just be added to flags here and removed from the subsequent calls:

Suggested change
flags=($JVMFLAGS)
flags=("-Dzookeeper.log.dir=$ZOO_LOG_DIR" "-Dzookeeper.log.file=$ZOO_LOG_FILE" $JVMFLAGS)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed manually (see below for a reason why) in 63b2e59 (this PR)

if [[ -z $ZOODATALOGDIR ]]; then
"$JAVA" "-Dzookeeper.log.dir=$ZOO_LOG_DIR" "-Dzookeeper.log.file=$ZOO_LOG_FILE" "${flags[@]}" \
org.apache.zookeeper.server.PurgeTxnLog "$ZOODATADIR" "$@"

# If config provides directories, use them; otherwise pass all args to PurgeTxnLog
if [[ -n $ZOODATADIR ]]; then
if [[ -z $ZOODATALOGDIR ]]; then
# Only dataDir specified
"$JAVA" "-Dzookeeper.log.dir=$ZOO_LOG_DIR" "-Dzookeeper.log.file=$ZOO_LOG_FILE" \
-cp "$CLASSPATH" "${flags[@]}" \
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The CLASSPATH environment variable is already exported. Passing it again as -cp $CLASSPATH is redundant, and makes really really long output in ps, and sometimes breaks the ability to use pkill or pgrep on the process, due to the length of the command-line. It is enough to export the variable. You don't need -cp.

(Same comment applies in the other cases where you've added it, as well.)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I honestly can't remember why I did this. I just assume it was a remnant from debugging the issue.

org.apache.zookeeper.server.PurgeTxnLog "$ZOODATADIR" "$@"
else
# Both dataDir and dataLogDir specified
"$JAVA" "-Dzookeeper.log.dir=$ZOO_LOG_DIR" "-Dzookeeper.log.file=$ZOO_LOG_FILE" \
-cp "$CLASSPATH" "${flags[@]}" \
org.apache.zookeeper.server.PurgeTxnLog "$ZOODATALOGDIR" "$ZOODATADIR" "$@"
fi
else
"$JAVA" "-Dzookeeper.log.dir=$ZOO_LOG_DIR" "-Dzookeeper.log.file=$ZOO_LOG_FILE" "${flags[@]}" \
org.apache.zookeeper.server.PurgeTxnLog "$ZOODATALOGDIR" "$ZOODATADIR" "$@"
# No config or config doesn't specify directories - pass all args to PurgeTxnLog
"$JAVA" "-Dzookeeper.log.dir=$ZOO_LOG_DIR" "-Dzookeeper.log.file=$ZOO_LOG_FILE" \
-cp "$CLASSPATH" "${flags[@]}" \
org.apache.zookeeper.server.PurgeTxnLog "$@"
fi
Loading