allthingssecurity opened a new pull request, #30809: URL: https://github.com/apache/camel/pull/30809
# Description [CAMEL-27444](https://issues.apache.org/jira/browse/CAMEL-27444) Follow-up of [CAMEL-27212](https://issues.apache.org/jira/browse/CAMEL-27212) (#30459). In asynchronous mode with `timeoutEnabled`, the timeouts run on the `CircuitBreakerTimeout` scheduled thread pool of the Circuit Breaker. It was created with `newDefaultScheduledThreadPool`, so from the default thread pool profile: a `SizedScheduledExecutorService` that rejects a task once 1000 tasks are pending (`maxQueueSize`). `TimeLimiter.decorateCompletionStage` (resilience4j 2.4.0) first calls the supplier, which submits the call to the thread pool of the calls (`processAsync`), and then schedules the timeout. It cancels the scheduled timeout only when the call succeeds. So every call in flight has a pending timeout, and every call that failed keeps one until `timeoutDuration` has passed. With 1000 pending, scheduling the timeout throws `RejectedExecutionException` after the call was submitted: - the exchange fails with `RejectedExecutionException` (or goes to the fallback), - the circuit breaker records a failure, - the call still runs against the backend, and with `inheritErrorHandler` a redelivery runs it a second time. No concurrency is needed: 1000 calls that fail fast within `timeoutDuration` (a backend that answers with errors at once) make the next call to a healthy backend fail. 1000 calls in flight at once do the same. The limit is older than CAMEL-27212 (the timeouts were scheduled on the thread pool of the calls, which has the same limit); #30459 moved them to a thread pool of their own with the same profile. The change: `doStart` creates the `CircuitBreakerTimeout` scheduler from a profile that only sets `maxQueueSize` to -1, so it gets the threads of the default thread pool profile (as before) and no queue limit (`camel.threadpool.maxQueueSize` no longer applies to this thread pool). The thread pool of the calls keeps its limit: when it is full, `submit` is rejected before anything runs, and the exchange fails without a call, as before. What the queue holds now: a timeout stays in it until it fires or is cancelled, and the time limiter cancels it only when the call succeeds (a cancelled timeout is removed from the queue: the default thread pool factory sets remove-on-cancel on its scheduled thread pools). So the queue holds the timeouts of the calls started within the last `timeoutDuration` that have not succeeded: one per call in flight, and one per call that failed within `timeoutDuration`. It does not grow over time, but it is not bounded by the thread pool of the calls either. A pending timeout of a call in flight is small (about 80 bytes); the one of a failed call holds the future of the call, and so its exception, until the timeout fires: in a plain JVM measurement, 1.5 KB per entry for an exception thrown 40 frames deep, 3.7 KB once its stack trace has been read (as when it is logged), 8 KB at 100 frames, plus whatever the exception refers to. For example, 1000 failed calls per second with `timeoutDuration` 60 seconds keep 60,000 timeouts, about 90 to 220 MB. That needs a circuit breaker that stays closed while the calls fail (a failure rate below the threshold, or exceptions that the breaker ignores): an open breaker rejects the calls before the time limiter schedules anything. With the default `timeoutDuration` of 1 second it is about 1000 entries at that rate. Why no limit instead of a larger one: any limit on this queue brings the bug back at that limit (the call is submitted before its timeout is rejected), and a limit tied to the thread pool of the calls would not cover the failed calls, which have left it. What would bound the queue by the calls in flight is cancelling the timeout of a failed call too; the time limiter of resilience4j does not do that, and doing it here needs a wrapper around the scheduler, so it is not part of this change. Found with a TLA+ model of the circuit breaker, the time limiter, the thread pools, the bulkhead and the route redelivery: at its smallest bound (2 route threads, 1 worker, room for 1 pending timeout, 1 failure, 1 redelivery) it finds an exchange that fails as rejected while its call was submitted, after 25 distinct states. Tests (both package-private, no sleeps; the timeouts are 60 seconds, so the pending ones stay pending for the whole test): - new `ResilienceAsyncTimeoutSchedulerQueueTest`: the default thread pool profile scaled down to 1 thread and a queue of 1; one call that fails, then a call to a healthy backend must succeed, and the breaker counts 1 failure and 1 success; - new `ResilienceAsyncTimeoutSchedulerDefaultSizeTest`: the default profile; 1000 calls that fail, then a call to a healthy backend must succeed, the backend gets exactly one call for it, and the breaker counts 1000 failures and 1 success (under a second); - `ResilienceThreadPoolLifecycleTest` records the scheduled thread pools from `newScheduledThreadPool`, which the timeout scheduler is now created with (it recorded them from `newDefaultScheduledThreadPool`). On `main` both fail: the healthy call fails with `RejectedExecutionException: Task rejected due queue size limit reached` although its call ran. With the change both pass, and the camel-resilience4j unit tests pass (104 run, 0 failures, 0 skipped; run offline). The two new tests with `ResilienceThreadPoolLifecycleTest`, 20 times in one JVM: 60 run, 0 failures. Not changed: - A `timeoutExecutorService` set by the user runs the calls, as before, and a `ScheduledExecutorService` set on the processor with `setScheduledExecutorService` runs the timeouts as it is: a Camel thread pool from a profile keeps the queue limit of that profile. - No upgrade guide entry: CAMEL-27212 is not released yet, and its 4.24 entry describes the name and the threads of this thread pool, which stay. Only the queue limit of the default thread pool profile no longer applies to it. This builds on CAMEL-27212, which is only on `main` (4.24). Older lines schedule the timeouts on the thread pool of the calls, which has the same limit, so they have the same problem with a different fix. Related changes in the same area, each its own PR and merging cleanly with this one: [CAMEL-27356](https://issues.apache.org/jira/browse/CAMEL-27356) (in asynchronous mode the fallback after a timeout runs off the timeout thread), the fix for a timed-out call that is started later (asynchronous mode, and synchronous mode with a bulkhead), and the fix for the synchronous mode running a call on the route thread when the thread pool is full. # 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-27444](https://issues.apache.org/jira/browse/CAMEL-27444). # 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]
