shashank created CAMEL-27444:
--------------------------------
Summary: camel-resilience4j - in asynchronous mode, when 1000
timeouts are pending, a call fails with RejectedExecutionException after it was
submitted, and still runs
Key: CAMEL-27444
URL: https://issues.apache.org/jira/browse/CAMEL-27444
Project: Camel
Issue Type: Bug
Components: camel-core
Reporter: shashank
Assignee: shashank
h3. What happens
In asynchronous mode with {{timeoutEnabled}}, the timeouts run on the
{{CircuitBreakerTimeout}} scheduled thread pool of the Circuit Breaker
(CAMEL-27212). It is created from the default thread pool profile, so it is 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, 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.
When 1000 are pending, scheduling the timeout throws
{{RejectedExecutionException}} after the call was submitted: the exchange fails
with {{RejectedExecutionException}}, the circuit breaker records a failure, and
the call still runs against the backend. With {{inheritErrorHandler}} the
redelivery runs a second call for the same exchange.
The pending timeouts add up without any concurrency: 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: before it, the timeouts were scheduled on
the thread pool of the calls, which had the same limit.
h3. How to reproduce
Asynchronous Circuit Breaker with
{{timeoutEnabled(true).timeoutDuration(60000)}} and a sliding window larger
than the test, default thread pool profile: send 1000 exchanges whose call
throws, then one whose call succeeds. That exchange fails with
{{RejectedExecutionException: Task rejected due queue size limit reached}}, the
breaker counts 1001 failures, and the backend got the call.
Found with a TLA+ model of the circuit breaker, time limiter, thread pools,
bulkhead and route redelivery.
h3. Fix
Create the {{CircuitBreakerTimeout}} scheduler with the threads of the default
thread pool profile, without its queue limit. A timeout stays in the queue
until it fires or is cancelled, and the time limiter cancels it only when the
call succeeds, so the queue holds one timeout per call in flight and per call
that failed within the timeout duration (a cancelled timeout is removed from
the queue). The one of a failed call holds the exception of the call until the
timeout duration has passed: with a breaker that stays closed while many calls
fail and a long {{timeoutDuration}} this is noticeable memory (about 60,000
entries of 1.5 to 4 KB each at 1000 failed calls per second and 60 seconds). A
limit on this queue would bring the bug back at that limit.
_Filed with Claude Code on behalf of allthingssecurity._
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)