This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push:
new ccfdfa73fb28 CAMEL-27444: camel-resilience4j - in asynchronous mode, a
submitted call no longer fails as rejected when 1000 timeouts are pending
(#30809)
ccfdfa73fb28 is described below
commit ccfdfa73fb28aba189ebb3646c49b0e3ab7d37bd
Author: allthingssecurity <[email protected]>
AuthorDate: Thu Nov 26 13:29:08 2026 +0530
CAMEL-27444: camel-resilience4j - in asynchronous mode, a submitted call no
longer fails as rejected when 1000 timeouts are pending (#30809)
In asynchronous mode the timeouts run on the CircuitBreakerTimeout
scheduled thread
pool, which was created from the default thread pool profile: it rejected a
task once
1000 tasks were pending. The time limiter of resilience4j submits the call
first and
then schedules its timeout, and cancels the timeout only when the call
succeeds, so
every call in flight and every call that failed within the timeout duration
holds a
pending timeout. At 1000, scheduling the timeout threw
RejectedExecutionException
after the call was submitted: the exchange failed, the circuit breaker
recorded a
failure, and the call still ran (twice with a redelivery). 1000 calls that
fail fast
were enough.
The scheduler now has the threads of the default thread pool profile and no
queue
limit (camel.threadpool.maxQueueSize no longer applies to it). A timeout
stays in the
queue until it fires or is cancelled, so the queue holds the timeouts of
the calls
started within the last timeout duration that have not succeeded: one per
call in
flight, and one per call that failed within the timeout duration. A
cancelled timeout
is removed from the queue. The timeout of a failed call holds the future of
the call,
and so its exception, until the timeout duration has passed.
ResilienceThreadPoolLifecycleTest now records the scheduled thread pools
from
newScheduledThreadPool, which the scheduler is created with.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
.../resilience4j/ResilienceProcessor.java | 12 ++-
...lienceAsyncTimeoutSchedulerDefaultSizeTest.java | 79 +++++++++++++++++++
.../ResilienceAsyncTimeoutSchedulerQueueTest.java | 88 ++++++++++++++++++++++
.../ResilienceThreadPoolLifecycleTest.java | 4 +-
4 files changed, 179 insertions(+), 4 deletions(-)
diff --git
a/components/camel-resilience4j/src/main/java/org/apache/camel/component/resilience4j/ResilienceProcessor.java
b/components/camel-resilience4j/src/main/java/org/apache/camel/component/resilience4j/ResilienceProcessor.java
index 4ecf74dc3f98..6791e301a9d0 100644
---
a/components/camel-resilience4j/src/main/java/org/apache/camel/component/resilience4j/ResilienceProcessor.java
+++
b/components/camel-resilience4j/src/main/java/org/apache/camel/component/resilience4j/ResilienceProcessor.java
@@ -69,6 +69,7 @@ import org.apache.camel.processor.PrototypeTaskFactory;
import org.apache.camel.spi.IdAware;
import org.apache.camel.spi.ProcessorExchangeFactory;
import org.apache.camel.spi.RouteIdAware;
+import org.apache.camel.spi.ThreadPoolProfile;
import org.apache.camel.spi.UnitOfWork;
import org.apache.camel.support.AsyncProcessorConverterHelper;
import org.apache.camel.support.EventHelper;
@@ -196,8 +197,15 @@ public class ResilienceProcessor extends
BaseProcessorSupport
if (asynchronous && timeLimiter != null && scheduledExecutorService ==
null && timeoutScheduler == null) {
// the timeouts get their own threads: when they were tasks of the
pool that runs the calls, a timeout
// could not fire while every thread of that pool was blocked in a
call, which is what the timeout is for
- timeoutScheduler =
camelContext.getExecutorServiceManager().newDefaultScheduledThreadPool(this,
- "CircuitBreakerTimeout");
+ // the threads of the default thread pool profile, without its
queue limit: the time limiter schedules the
+ // timeout after the call was submitted, so a rejected timeout
failed an exchange whose call still ran. A
+ // timeout stays queued until it fires or is cancelled, and the
time limiter cancels it (which removes it
+ // from the queue) only when the call succeeds: the queue holds
the timeouts of the calls in flight and of
+ // the calls that failed within the timeout duration, and the one
of a failed call holds its exception
+ ThreadPoolProfile profile = new
ThreadPoolProfile("CircuitBreakerTimeout");
+ profile.setMaxQueueSize(-1);
+ timeoutScheduler =
camelContext.getExecutorServiceManager().newScheduledThreadPool(this,
+ "CircuitBreakerTimeout", profile);
}
if (asynchronous && timeLimiter != null && timeoutContinuation ==
null) {
// a cached thread pool, as a fallback that blocks holds its
thread: with a bounded pool the next fallbacks
diff --git
a/components/camel-resilience4j/src/test/java/org/apache/camel/component/resilience4j/ResilienceAsyncTimeoutSchedulerDefaultSizeTest.java
b/components/camel-resilience4j/src/test/java/org/apache/camel/component/resilience4j/ResilienceAsyncTimeoutSchedulerDefaultSizeTest.java
new file mode 100644
index 000000000000..7005d405c917
--- /dev/null
+++
b/components/camel-resilience4j/src/test/java/org/apache/camel/component/resilience4j/ResilienceAsyncTimeoutSchedulerDefaultSizeTest.java
@@ -0,0 +1,79 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.resilience4j;
+
+import java.util.concurrent.atomic.AtomicInteger;
+
+import io.github.resilience4j.circuitbreaker.CircuitBreaker;
+import org.apache.camel.Exchange;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.test.junit6.CamelTestSupport;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertNull;
+
+/**
+ * In asynchronous mode, each call that fails keeps its timeout scheduled
until the timeout duration has passed (the
+ * time limiter cancels it only on success). With the default thread pool
profile (a queue of 1000), 1000 calls that
+ * fail fast must not make the next call fail because its timeout could not be
scheduled after its call was submitted.
+ */
+class ResilienceAsyncTimeoutSchedulerDefaultSizeTest extends CamelTestSupport {
+
+ private final AtomicInteger healthyCalls = new AtomicInteger();
+
+ @Test
+ void testCallAfterThousandFailedCallsIsNotRejected() {
+ for (int i = 0; i < 1000; i++) {
+ Exchange failed = template.send("direct:async", e ->
e.getIn().setHeader("fail", true));
+ assertInstanceOf(IllegalStateException.class,
failed.getException());
+ }
+
+ Exchange healthy = template.send("direct:async", e ->
e.getIn().setBody("Hello"));
+ assertNull(healthy.getException(), "the call to a healthy backend
succeeds");
+ assertEquals("Bye", healthy.getMessage().getBody());
+ assertEquals(1, healthyCalls.get(), "the backend gets exactly one call
for the healthy exchange");
+
+ CircuitBreaker cb = context.getProcessor("cbAsync",
ResilienceProcessor.class).getCircuitBreaker();
+ assertEquals(CircuitBreaker.State.CLOSED, cb.getState());
+ assertEquals(1000, cb.getMetrics().getNumberOfFailedCalls());
+ assertEquals(1, cb.getMetrics().getNumberOfSuccessfulCalls());
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() {
+ return new RouteBuilder() {
+ @Override
+ public void configure() {
+ from("direct:async").circuitBreaker().id("cbAsync")
+ // the timeouts of the failed calls stay scheduled for
a minute, longer than the test, and the
+ // window is larger than the test, so the breaker
stays closed
+
.resilience4jConfiguration().asynchronous(true).timeoutEnabled(true).timeoutDuration("60000")
+
.slidingWindowSize(5000).minimumNumberOfCalls(5000).end()
+ .process(e -> {
+ if (e.getIn().getHeader("fail", false,
Boolean.class)) {
+ throw new IllegalStateException("backend
error");
+ }
+ healthyCalls.incrementAndGet();
+ e.getMessage().setBody("Bye");
+ })
+ .end();
+ }
+ };
+ }
+}
diff --git
a/components/camel-resilience4j/src/test/java/org/apache/camel/component/resilience4j/ResilienceAsyncTimeoutSchedulerQueueTest.java
b/components/camel-resilience4j/src/test/java/org/apache/camel/component/resilience4j/ResilienceAsyncTimeoutSchedulerQueueTest.java
new file mode 100644
index 000000000000..b026c366b99a
--- /dev/null
+++
b/components/camel-resilience4j/src/test/java/org/apache/camel/component/resilience4j/ResilienceAsyncTimeoutSchedulerQueueTest.java
@@ -0,0 +1,88 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.resilience4j;
+
+import java.util.concurrent.atomic.AtomicInteger;
+
+import io.github.resilience4j.circuitbreaker.CircuitBreaker;
+import org.apache.camel.CamelContext;
+import org.apache.camel.Exchange;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.spi.ThreadPoolProfile;
+import org.apache.camel.test.junit6.CamelTestSupport;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertNull;
+
+/**
+ * In asynchronous mode, a call that fails keeps its timeout scheduled until
the timeout duration has passed (the time
+ * limiter cancels it only on success). The default thread pool profile is
scaled down to a queue of 1 here, so one
+ * failed call fills a queue that 1000 failed calls fill with the defaults:
the next call must still succeed, and not
+ * fail because its timeout could not be scheduled after its call was
submitted.
+ */
+class ResilienceAsyncTimeoutSchedulerQueueTest extends CamelTestSupport {
+
+ private final AtomicInteger healthyCalls = new AtomicInteger();
+
+ @Override
+ protected CamelContext createCamelContext() throws Exception {
+ CamelContext ctx = super.createCamelContext();
+ ThreadPoolProfile profile =
ctx.getExecutorServiceManager().getDefaultThreadPoolProfile();
+ profile.setPoolSize(1);
+ profile.setMaxPoolSize(1);
+ profile.setMaxQueueSize(1);
+ return ctx;
+ }
+
+ @Test
+ void testCallAfterFailedCallIsNotRejected() {
+ // the timeout of the failed call stays scheduled for a minute, longer
than the test
+ Exchange failed = template.send("direct:async", e ->
e.getIn().setHeader("fail", true));
+ assertInstanceOf(IllegalStateException.class, failed.getException());
+
+ Exchange healthy = template.send("direct:async", e ->
e.getIn().setBody("Hello"));
+ assertNull(healthy.getException(), "the call to a healthy backend
succeeds");
+ assertEquals("Bye", healthy.getMessage().getBody());
+ assertEquals(1, healthyCalls.get());
+
+ CircuitBreaker cb = context.getProcessor("cbAsync",
ResilienceProcessor.class).getCircuitBreaker();
+ assertEquals(1, cb.getMetrics().getNumberOfFailedCalls());
+ assertEquals(1, cb.getMetrics().getNumberOfSuccessfulCalls());
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() {
+ return new RouteBuilder() {
+ @Override
+ public void configure() {
+ from("direct:async").circuitBreaker().id("cbAsync")
+
.resilience4jConfiguration().asynchronous(true).timeoutEnabled(true).timeoutDuration("60000")
+ .end()
+ .process(e -> {
+ if (e.getIn().getHeader("fail", false,
Boolean.class)) {
+ throw new IllegalStateException("backend
error");
+ }
+ healthyCalls.incrementAndGet();
+ e.getMessage().setBody("Bye");
+ })
+ .end();
+ }
+ };
+ }
+}
diff --git
a/components/camel-resilience4j/src/test/java/org/apache/camel/component/resilience4j/ResilienceThreadPoolLifecycleTest.java
b/components/camel-resilience4j/src/test/java/org/apache/camel/component/resilience4j/ResilienceThreadPoolLifecycleTest.java
index 4aff827bc4ba..b34321ea349b 100644
---
a/components/camel-resilience4j/src/test/java/org/apache/camel/component/resilience4j/ResilienceThreadPoolLifecycleTest.java
+++
b/components/camel-resilience4j/src/test/java/org/apache/camel/component/resilience4j/ResilienceThreadPoolLifecycleTest.java
@@ -53,8 +53,8 @@ class ResilienceThreadPoolLifecycleTest extends
CamelTestSupport {
CamelContext context = super.createCamelContext();
context.setExecutorServiceManager(new
DefaultExecutorServiceManager(context) {
@Override
- public ScheduledExecutorService
newDefaultScheduledThreadPool(Object source, String name) {
- ScheduledExecutorService answer =
super.newDefaultScheduledThreadPool(source, name);
+ public ScheduledExecutorService newScheduledThreadPool(Object
source, String name, ThreadPoolProfile profile) {
+ ScheduledExecutorService answer =
super.newScheduledThreadPool(source, name, profile);
scheduledPools.put(name, answer);
return answer;
}