allthingssecurity opened a new pull request, #30808:
URL: https://github.com/apache/camel/pull/30808

   # Description
   
   [CAMEL-27443](https://issues.apache.org/jira/browse/CAMEL-27443)
   
   A Circuit Breaker with `timeoutEnabled=true` in synchronous mode (the 
default) runs each call with
   `CompletableFuture.supplyAsync(task, executorService)`, and the time limiter 
then waits for that future with
   `future.get(timeoutDuration)`. Without a `timeoutExecutorService`, the 
thread pool is created from the default thread
   pool profile: 10 threads, up to 20, a queue of 1000 calls, and the 
`CallerRuns` rejected policy.
   
   When all the threads are busy and the queue is full, `CallerRuns` runs the 
call on the route thread, inside
   `supplyAsync`, before the time limiter has a future to wait on. The call 
then cannot time out: a backend that does not
   answer blocks the route thread for good, and a backend that answers late is 
recorded by the circuit breaker as a
   successful call (only a slow call once `slowCallDurationThreshold`, 60 s by 
default, is passed). The thread pool fills up
   on its own during a slowdown: a call that timed out keeps its thread 
(cancelling a `CompletableFuture` does not interrupt
   it), and a call that timed out while it was queued is cancelled but stays in 
the queue until a thread takes it and skips
   it. With a backend that hangs, no thread takes it, so the queue fills with 
calls that already timed out and the next
   calls, half-open probes included, run on the route threads. While that lasts 
the breaker stays closed, since these slow
   calls count as successes.
   
   The change, for the thread pool that Camel creates:
   
   - `ResilienceReifier` creates it from a copy of the default thread pool 
profile with the `Abort` rejected policy (sizes
     unchanged). A call that does not fit is rejected: submitting it throws a 
`RejectedExecutionException`, the circuit
     breaker decorator records it as a failed call, and the exchange fails with 
it
     (or the fallback runs). With a bulkhead, the bulkhead decorator catches 
it, releases the permit, and the time limiter
     rethrows it. The rejected policy of the default profile is not used for 
this thread pool.
   - `ResilienceProcessor` runs the call with a small `ThreadPoolCall` (a 
`CompletableFuture` that is also the `Runnable`)
     instead of `supplyAsync`. It does what `supplyAsync` did (a call cancelled 
before it starts is skipped), and its
     `cancel` also removes it from the queue of a `ThreadPoolExecutor`. The 
time limiter cancels the call when it times out
     (`timeoutCancelRunningFuture`, default `true`), so a call that timed out 
while queued now frees its slot at once.
   - The time limiter itself is unchanged: a call that fits still runs on the 
thread pool and times out as before.
   
   Counters: the rejected call is counted by the circuit breaker as a failed 
call (`numberOfFailedCalls`), and by
   `numberOfFallbackCalls` when a fallback answers it. It is not counted as 
timed out nor as rejected by the bulkhead
   (`numberOfBulkheadRejectedCalls` stays for `BulkheadFullException`). The 
`CamelCircuitBreakerResponseRejected` property
   is not changed by this PR: without a fallback it is `true`, as for any other 
exception; with a fallback it is `false`.
   
   A thread pool given with `timeoutExecutorService` (a bean or a thread pool 
profile) keeps its own rejected policy; the
   docs say so. When it is a `ThreadPoolExecutor`, a call that timed out while 
queued leaves its queue too (the call would
   be skipped anyway; it only frees the slot sooner). When the thread pool is 
wrapped by an instrumented thread pool factory
   (camel-micrometer, camel-metrics or camel-opentelemetry), it is not a 
`ThreadPoolExecutor`: the rejection still applies,
   but a call that timed out while queued keeps its slot until a thread takes 
it and skips it, as before.
   
   With virtual threads (`camel.threads.virtual.enabled`), the thread pool that 
Camel creates is a thread per call, bounded
   to 1000 calls at once (the queue size of the profile), and a call over that 
waits for a free slot up to the keep alive
   time of the profile (60 s by default). The rejected policy applies there 
too: after that wait the call is now rejected,
   where before it ran on the route thread. The wait itself is not changed, and 
the timeout does not apply to it; the docs
   and the upgrade guide say so.
   
   Docs: a paragraph on the Resilience4j EIP page (and its catalog copy) on the 
thread pool, what happens when it is full,
   and how to keep it from filling up (a bulkhead with 
`bulkheadMaxConcurrentCalls` no larger than the queue size, or
   sizing the default profile); an entry in the 4.24 upgrade guide, since a 
saturated pool now fails calls that used to
   run on the caller, with how to keep the old behaviour (a 
`timeoutExecutorService` with the `CallerRuns` policy). The
   bulkhead bound is the queue size and not the threads plus the queue: a 
thread that has just finished a call releases
   its bulkhead permit a moment before it takes the next call from the queue, 
so with a bulkhead of the threads plus the
   queue a call could still find the queue full at that moment.
   
   Found with a TLA+ model of the circuit breaker, the time limiter, the thread 
pool, the bulkhead and the route
   redelivery. With 2 route threads, one call that times out and one 
redelivery, it finds a call that runs on the route
   thread past the timeout and is recorded as a success, and a route thread 
blocked for good in a call that hangs.
   
   Tests: new `ResilienceSyncTimeoutThreadPoolFullTest` (the default profile 
scaled down to 1 thread and a queue of 1;
   latches, no sleeps):
   
   - `testCallIsRejectedWhenTheThreadPoolIsFull`: one call holds the thread, 
one waits in the queue; the third fails at
     once with a `RejectedExecutionException`, never reaches the backend, and 
is a failed call for the breaker; the two
     others complete when the backend answers;
   - `testRejectedCallGoesToTheFallbackAndReleasesTheBulkheadPermit`: the same 
with a bulkhead of 10 and a fallback: the
     fallback answers, the bulkhead permit is released, the counters are as 
above;
   - `testTimedOutCallLeavesTheQueue`: 200 ms timeout; a call that times out 
while queued leaves the queue, and the next
     call is queued and times out instead of running on the caller thread;
   - `testUserThreadPoolKeepsItsRejectedPolicy` (control): a 
`timeoutExecutorService` with `CallerRunsPolicy` still runs
     the third call on the caller thread. It passes on `main` too.
   
   On `main` the first three fail (the third call runs on the caller thread; 
the timed-out call keeps its queue slot) and
   the control passes. With the change all four pass, and the 
camel-resilience4j unit tests pass (106 run, 0 failures,
   0 skipped; run offline). The new test 20 times in one JVM: 80 run, 0 
failures; with `ResilienceBulkheadTimeoutTest`,
   `ResilienceCallCountersTest`, `ResilienceTimeoutWriteBackRaceTest` and 
`ResilienceTimeoutDuringWriteBackTest`, 20
   times: 320 run, 0 failures.
   
   Not changed:
   
   - Asynchronous mode: its thread pool is a scheduled pool that already 
rejects when its queue is full. A call that
     timed out before it started still runs later there, and with a bulkhead in 
synchronous mode; and the timeout
     scheduler of the asynchronous mode can reject a call that was already 
submitted. These are separate issues, with
     their own fixes.
   - `camel-microprofile-fault-tolerance` does not create such a thread pool 
(it only uses a thread pool given by the
     user).
   
   # 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-27443](https://issues.apache.org/jira/browse/CAMEL-27443).
   
   # 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]

Reply via email to