allthingssecurity opened a new pull request, #30698: URL: https://github.com/apache/camel/pull/30698
# Description [CAMEL-27356](https://issues.apache.org/jira/browse/CAMEL-27356) Follow-up of [CAMEL-27212](https://issues.apache.org/jira/browse/CAMEL-27212) (#30459), item 2 of its review. Since CAMEL-27212 the timeouts of a Circuit Breaker with `asynchronous=true` and `timeoutEnabled=true` run on a scheduled thread pool of their own (`CircuitBreakerTimeout`, the size of the default thread pool profile, 10 threads). When a timeout fires, its thread completes the future exceptionally, and the `whenComplete` of `ResilienceProcessor.processAsync` runs the fallback and then the rest of the route (`callback.done(false)`) on that same thread. A fallback that blocks (a call to a backup HTTP service), or a blocking step after the Circuit Breaker, holds a timeout thread. With 10 of them held, the next timeouts wait: an exchange whose call hangs gets no fallback until one of the blocked fallbacks returns. #30459 documented this as a caveat and left the hand-off for this follow-up. The change: - After a timeout, the continuation (fallback, then `callback.done(false)`) is handed to a cached thread pool of the Circuit Breaker, `CircuitBreakerFallback`. It is created in `doStart` next to the timeout scheduler (asynchronous mode with a timeout, also when a `scheduledExecutorService` is set by code) and shut down in `doShutdown`, like the other pools since #30459. If the pool rejects the task (the route is being removed), the continuation runs on the timeout thread as before. `whenComplete` runs once, and the continuation runs once on one of the two threads, so the callback is still called once. - Only a completion with a `TimeoutException` is handed off. A call that completes or fails continues on its own thread as before, and a rejection by the open circuit or a full bulkhead continues on the caller thread. - Why not the thread pool of the calls: when the calls block every thread of it (the case #30459 is about), a fallback queued there would wait behind them. Why a cached pool: with a bounded pool, the next fallbacks would wait behind the blocked ones, which is the same stall. A thread is held only while a fallback (or the route after it) blocks, as the caller thread is in synchronous mode. - Docs: the note on the Resilience4j EIP page (and its catalog copy) and the 4.24 upgrade-guide entry of CAMEL-27212 (4.24 is not released, so the entry is updated in place) describe the new pool instead of the caveat. Found with a TLA+ model. A small model of the asynchronous timeouts (calls that never return, a fixed scheduler of K threads, fallbacks that block for ever, the hand-off, and the removal of the route) finds on main a timeout that never fires while K fallbacks block (3 exchanges, K=2, 35 distinct states; 5 exchanges, K=3, 419). With the change every timeout fires and every non-blocking fallback completes (36 and 432 distinct states), and the callback is called once, also when the pool is shut down (115). TLC and everystate agree. Tests: - new `ResilienceAsyncTimeoutBlockingFallbackTest`: 10 exchanges whose calls block and whose fallbacks block; an 11th exchange whose call also blocks must get its (non-blocking) fallback within 5 seconds, and all 11 complete once the fallbacks are released; - `ResilienceThreadPoolLifecycleTest`: the `CircuitBreakerFallback` pool is kept when the route is stopped and shut down when it is removed. On `main` both tests fail: the 11th exchange gets no reply within 5 seconds (`TimeoutException`), and there is no `CircuitBreakerFallback` pool. With the change both pass, and the camel-resilience4j unit tests pass (103 run, 0 failures, 0 skipped; run offline, with the Spring XML test dependencies from my local repository). The two tests with `ResilienceAsyncTimeoutBlockedPoolTest` and `ResilienceAsyncTimeoutWithFallbackTest`, 20 times in one JVM: 100 run, 0 failures. Not changed: - In asynchronous mode the reifier still requires `timeoutExecutorService` to be a `ScheduledExecutorService`, and the option description still says "uses ForkJoinPool.commonPool() by default" (item 5 of the review of #30459). Both are stale since #30459; the cleanup touches the generated metadata (catalog, XSDs, YAML DSL) and comes as a separate follow-up. - A call that completes or fails still continues the route on the thread of the call, as before. This builds on CAMEL-27212, which is only on `main` (4.24); older lines do not have the `CircuitBreakerTimeout` pool. # Target - [x] I checked that the commit is targeting the correct branch (Camel 4 uses the `main` branch) # Tracking - [x] If this is a large change, bug fix, or code improvement, I checked there is a [JIRA issue](https://issues.apache.org/jira/browse/CAMEL) filed for the change (usually before you start working on it): [CAMEL-27356](https://issues.apache.org/jira/browse/CAMEL-27356). # Apache Camel coding standards and style - [x] I checked that each commit in the pull request has a meaningful subject line and body. - [ ] I have run `mvn clean install -DskipTests` locally from root folder and I have committed all auto-generated changes. (I built and tested the affected modules, including the formatter and import-sort plugins. I did not run the full root build.) # AI-assisted contributions - [x] If this PR includes AI-generated code, commits have proper co-authorship attribution (e.g., `Co-authored-by` trailers) and the PR description identifies the AI tool used. This PR was prepared with Claude Code (Claude Opus 5.5). The commit carries a `Co-Authored-By` trailer. _Claude Code on behalf of allthingssecurity_ 🤖 Generated with [Claude Code](https://claude.com/claude-code) -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
