Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ZOOKEEPER-3766: Clean up bash scripts #2224

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

ctubbsii
Copy link
Member

@ctubbsii ctubbsii commented Feb 21, 2025

For ZOOKEEPER-3766:

  • Export CLASSPATH in scripts and remove -cp $CLASSPATH uses, so that
    java picks up the classpath from the environment, to avoid absurdly
    long command lines in process listings (this should also fix an issue
    with older versions of pgrep/pkill, such as that installed on RHEL8;
    see https://bugzilla.redhat.com/show_bug.cgi?id=1782309 for details)
  • Add a new print-classpath command to print the CLASSPATH env
    variable in the form CLASSPATH=$CLASSPATH from the script, so that
    the script's view of the CLASSPATH environment can easily be inspected
    when troubleshooting java processes

Also fix other code quality issues in all bash scripts:

  • Add CI checks for shfmt and shellcheck for all bash scripts
  • Standardize "shebang" lines
  • Standardize license header formatting
  • Format using shfmt tool and address all shellcheck problems
  • Remove unnecessary quoting and use of curly braces
  • Standardize on double square braces to avoid unpredictable use of
    /usr/bin/[ instead of built-in bash braces
  • Remove a useless check of $? in bin/zkServer-initialize.sh
  • Remove an impossible to reach exit from bin/zkServer-initialize.sh
  • Remove an impossible to fail check of $? after nohup is executed in
    bin/zkServer.sh (nohup with a trailing & always returns a zero exit
    code for success, even if the command fails; if it fails it fails
    undetectibly in the background)
  • Avoid using sed for regex, and use built-in bash regex instead
  • Delete some old scripts (some of which are broken anyway) that don't
    appear to be used anywhere, and don't seem to have any current need
  • Bump rat plugin version for https in license and add license to
    .gitattributes to make rat plugin happy again

@ctubbsii
Copy link
Member Author

This is draft because I'm still fixing the scripts outside the bin directory, and also adding CI checks for shfmt and shellcheck, to prevent regressions and further bugs.

I did find an issue that I am not yet sure how to resolve, at

defect=${${subject:$position:$length}#ZOOKEEPER-}

That line is broken... you can't have nested variables like that.

I'm wondering if that test-github-pr.sh script should just be deleted (and maybe some of the other files, too, like test-patch.sh) since GitHub Actions and JIRA exist. Those scripts don't really seem to add much value.

@ctubbsii ctubbsii changed the title [DRAFT] Clean up bash scripts in bin directory [ZOOKEEPER-3766] Clean up bash scripts in bin directory Feb 21, 2025
@ctubbsii ctubbsii changed the title [ZOOKEEPER-3766] Clean up bash scripts in bin directory ZOOKEEPER-3766: Clean up bash scripts in bin directory Feb 21, 2025
For ZOOKEEPER-3766:

* Export CLASSPATH in scripts and remove `-cp $CLASSPATH` uses, so that
  java picks up the classpath from the environment, to avoid absurdly
  long command lines in process listings (this should also fix an issue
  with older versions of pgrep/pkill, such as that installed on RHEL8;
  see https://bugzilla.redhat.com/show_bug.cgi?id=1782309 for details)
* Add a new `classpath` command to print the CLASSPATH environment
  variable in the form `CLASSPATH=$CLASSPATH` from the script, so that
  the script's view of the CLASSPATH environment can easily be inspected
  when troubleshooting java processes

Also fix other code quality issues in all bash scripts:

* Add CI checks for shfmt and shellcheck for all bash scripts
* Standardize "shebang" lines
* Standardize license header formatting
* Format using shfmt tool and address all shellcheck problems
* Remove unnecessary quoting and use of curly braces
* Standardize on double square braces to avoid unpredictable use of
  /usr/bin/\[ instead of built-in bash braces
* Remove a useless check of `$?` in bin/zkServer-initialize.sh
* Remove an impossible to reach exit from bin/zkServer-initialize.sh
* Remove an impossible to fail check of `$?` after nohup is executed in
  bin/zkServer.sh (nohup with a trailing `&` always returns a zero exit
  code for success, even if the command fails; if it fails it fails
  undetectibly in the background)
* Avoid using sed for regex, and use built-in bash regex instead
* Delete some old scripts (some of which are broken anyway) that don't
  appear to be used anywhere, and don't seem to have any current need
* Bump rat plugin version for https in license and add license to
  .gitattributes to make rat plugin happy again
@ctubbsii ctubbsii changed the title ZOOKEEPER-3766: Clean up bash scripts in bin directory ZOOKEEPER-3766: Clean up bash scripts Feb 21, 2025
@ctubbsii ctubbsii marked this pull request as ready for review February 21, 2025 22:08
@ctubbsii
Copy link
Member Author

I fixed all remaining scripts and updated the commit message to link it to the related JIRA. This is ready for review.

@ctubbsii
Copy link
Member Author

If the scripts are the same in previous branches, this might be possible to be backported. However, it's a lot of work to try to resolve differences, so if the scripts are substantially different in earlier branches, I would not bother backporting. This cleans things up and ensures quality going forward, but it's not critical to backport.

Comment on lines +47 to +52
clientflags=($CLIENT_JVMFLAGS)
# shellcheck disable=SC2206
flags=($JVMFLAGS)
"$JAVA" "-Dzookeeper.log.dir=$ZOO_LOG_DIR" "-Dzookeeper.log.file=$ZOO_LOG_FILE" \
"${clientflags[@]}" "${flags[@]}" \
org.apache.zookeeper.ZooKeeperMain "$@"
Copy link
Member

Choose a reason for hiding this comment

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

May I ask how this change makes a difference?

Copy link
Member Author

@ctubbsii ctubbsii Feb 25, 2025

Choose a reason for hiding this comment

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

This makes it clear that the intent is to treat the $JVMFLAGS and $CLIENT_JVMFLAGS scalar variables as whitespace-delimited arrays. shellcheck warns about this because it does not know the author's intent. In order to suppress that shellcheck warning, I made the parsing of the scalar as an array an explicit separate action. shellcheck will still warn about that, but I suppress that with a very narrow exception (see lines 46 and 48).

It is possible to suppress the warning without changing the line.... however, if you do that, the suppression would be far too broad, affecting all the parameters on the java command, and it would hide future programming errors that shellcheck would normally catch. The way I wrote it here keeps a very narrow suppression, so we don't hide future programming errors, but without changing any behavior.

@@ -1,4 +1,5 @@
#!/usr/bin/env bash
#! /usr/bin/env bash
Copy link
Member

Choose a reason for hiding this comment

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

Why do we need an extra whitespace between ! and /?

Copy link
Member Author

Choose a reason for hiding this comment

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

Short answer: it's not necessary, but it's helpful.

It's optional, but it improves readability, and in fact a space was used in the original example for how to use them in an email from Dennis Ritchie discussing the support for it added to the Unix kernel. Additionally, historically, there have been various rumors that a space was required on some versions of the kernel... but that appears to have just been rumors. Nevertheless, some people's beliefs about the space being important persists. Having the space there avoids unnecessary distractions and complaints from those people who are probably wrong, but might bring it up (see https://en.wikipedia.org/wiki/Shebang_(Unix)#History). If you have to standardize on something, you might as well standardize on the variant that is going to cause less conflict, if all else is equal.

So, I think the main reasons it is helpful to have the space are:

  1. improved readability and
  2. avoiding distracting complaints from people who still believe the historical rumors.

@tisonkun
Copy link
Member

Thanks for your contribution! Generally I like the direction.

Add a new classpath command to print the CLASSPATH environment

I don't find this command though. Could you point out the related lines?

Comment on lines +202 to +204
print-classpath)
echo "CLASSPATH=$CLASSPATH"
;;
Copy link
Member Author

@ctubbsii ctubbsii Feb 25, 2025

Choose a reason for hiding this comment

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

@tisonkun Here is the command added to print the classpath. (I previously commented that I might have left it out of this PR, but I found it and deleted that earlier comment, which was incorrect.)

Copy link
Member

@tisonkun tisonkun left a comment

Choose a reason for hiding this comment

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

LGTM.

Thanks for spending time on cleaning up these scripts.

@tisonkun tisonkun requested a review from eolivelli February 28, 2025 04:09
@ctubbsii
Copy link
Member Author

I have no idea why the cpp tests are failing... I suspect it's completely unrelated to any changes in this PR, which pretty much only touches the bash scripts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants