Skip to content

Conversation

@thjarvin
Copy link
Contributor

@thjarvin thjarvin commented May 6, 2025

Fixes AB#57775, Fixes AB#57799

@thjarvin thjarvin requested review from LofhJann and haphut May 6, 2025 12:37
Copy link
Contributor

@haphut haphut left a comment

Choose a reason for hiding this comment

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

Good start! This PR aims to fix AB#57799 instead of AB#57775 but we could take out both bugs now.

I think unless we update to Java 21 to get Executors.newVirtualThreadPerTaskExecutor, Executors.newCachedThreadPool is a smart choice. However, we should use only one of them or it gets computationally expensive.

And the problematic line for AB#57775 is httpServer.setExecutor(Executors.newSingleThreadExecutor());.

Recommendations:

  1. Create the ExecutorService as a private member. Use it both for httpServer and the checks.

    1. Shut it down in close:

      public void close() {
          if (httpServer != null) {
              httpServer.stop(0);
          }
          if (healthCheckExecutor != null) {
              healthCheckExecutor.shutdown();
              try {
                  if (!healthCheckExecutor.awaitTermination(3, TimeUnit.SECONDS)) {
                      healthCheckExecutor.shutdownNow();
                  }
              } catch (InterruptedException ie) {
                  healthCheckExecutor.shutdownNow();
                  Thread.currentThread().interrupt();
              }
          }
      }
  2. ArrayList is not thread-safe. It is accessed from checkHealth and, I assume, by another thread via addCheck, removeCheck and clearChecks. Switch to CopyOnWriteArrayList for checks.

  3. Optional: Checks could be traversed in order of completion to get to failure faster. Try ExecutorCompletionService for that.

  4. As not all futures necessarily finish before finding failure, I would cancel the remaining futures:

    for (Future<Boolean> f : futures) {
        if (!f.isDone()) {
            f.cancel(true);
        }
    }

@haphut
Copy link
Contributor

haphut commented May 20, 2025

Oops, pushed suggestions early. I wanted to add at least a single test.

Great work regardless!

Copy link
Contributor

@haphut haphut left a comment

Choose a reason for hiding this comment

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

LGTM

@thjarvin thjarvin merged commit 9482bf3 into develop May 22, 2025
4 checks passed
@thjarvin thjarvin deleted the healthcheck-many-threads branch May 22, 2025 11:58
thjarvin added a commit that referenced this pull request Jun 12, 2025
* Fix topic prefix parsing

* Update version to 2.0.3-RC

* Revert "Fix topic prefix parsing"

This reverts commit fd8b2fc.

* Fix topic prefix parsing for passenger count

* Update version to 2.0.3-RC1

* Revert "Fix topic prefix parsing for passenger count"

This reverts commit 28c5c9e.

* Ignore '.DS_Store'

* Implement duplicate log filtering

* Update version 2.0.3-RC2

* Add tests for log duplicate filtering

* Fix small logics bug in log filtering

* Update version 2.0.3-RC4

* Healthcheck uses many threads (#371)

* AB#57775: HealthServer starts a new thread for each health checker

* AB#57775: Update version to 2.0.3-RC3

* AB#57775: Add instance variable healthCheckExecutor

* AB#57775: ExecutorCompletionService is used to get the failure faster

* refactor: Add final for attributes

* refactor: Use StandardCharsets for UTF-8

* style: Remove extra whitespace

* refactor: Handle two exceptions explicitly

* test: Add a start for unit tests of HealthServer

* Update version to 2.0.3-RC5

---------

Co-authored-by: haphut <[email protected]>

* Update version to 2.0.3-RC6

* Update version to 2.0.3

* Delete DuplicateLogFilter

---------

Co-authored-by: Janne Löfhjelm <[email protected]>
Co-authored-by: haphut <[email protected]>
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.

4 participants