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 c1de1dfe24ff CAMEL-27438: camel-core - ThrottlingExceptionRoutePolicy 
checks the open circuit again when its half open check fails (#30837)
c1de1dfe24ff is described below

commit c1de1dfe24ff2423a22b5a0bfff5ff3501d3bb84
Author: allthingssecurity <[email protected]>
AuthorDate: Thu Nov 26 13:33:32 2026 +0530

    CAMEL-27438: camel-core - ThrottlingExceptionRoutePolicy checks the open 
circuit again when its half open check fails (#30837)
    
    The half open check is the only check scheduled for an open circuit. When
    it half opened the circuit and resuming the consumer threw, the exception
    went to the exception handler and the circuit stayed open, but no other
    check was scheduled, and the suspended consumer completes no exchange that
    would check the circuit. The circuit stayed open for good. The same with a
    halfOpenHandler: closing the circuit when the resume throws, or keeping it
    open when suspending the consumer again throws.
    
    The half open task now remembers the future of its check, and when the
    check ends with the circuit still open and no other check scheduled since,
    it logs a WARN and schedules the next check after halfOpenAfter. The
    exception still goes to the exception handler. A check that is stopped
    (route removed, policy stopped) schedules nothing, as before.
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
 ...ceptionRoutePolicyHalfOpenCheckFailureTest.java | 139 +++++++++++++++++++++
 .../throttling/ThrottlingExceptionRoutePolicy.java |  12 ++
 2 files changed, 151 insertions(+)

diff --git 
a/core/camel-core/src/test/java/org/apache/camel/processor/throttle/ThrottlingExceptionRoutePolicyHalfOpenCheckFailureTest.java
 
b/core/camel-core/src/test/java/org/apache/camel/processor/throttle/ThrottlingExceptionRoutePolicyHalfOpenCheckFailureTest.java
new file mode 100644
index 000000000000..bc8195f66b56
--- /dev/null
+++ 
b/core/camel-core/src/test/java/org/apache/camel/processor/throttle/ThrottlingExceptionRoutePolicyHalfOpenCheckFailureTest.java
@@ -0,0 +1,139 @@
+/*
+ * 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.processor.throttle;
+
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import org.apache.camel.CamelExecutionException;
+import org.apache.camel.Consumer;
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.support.service.ServiceSupport;
+import org.apache.camel.throttling.ThrottlingExceptionRoutePolicy;
+import org.junit.jupiter.api.Test;
+
+import static org.awaitility.Awaitility.await;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * When suspending or resuming the consumer fails in the half open check of 
{@link ThrottlingExceptionRoutePolicy}, the
+ * circuit stays open, and the policy checks it again after halfOpenAfter: the 
consumer is suspended, so no exchange
+ * would check it.
+ * <p>
+ * The first suspend or resume of each check path fails, the later ones work.
+ */
+class ThrottlingExceptionRoutePolicyHalfOpenCheckFailureTest extends 
ContextTestSupport {
+
+    private final FailOncePolicy halfOpenPolicy = new FailOncePolicy();
+    private final FailOncePolicy closePolicy = new FailOncePolicy();
+    private final FailOncePolicy reopenPolicy = new FailOncePolicy();
+    private final AtomicInteger closeChecks = new AtomicInteger();
+    private final AtomicInteger reopenChecks = new AtomicInteger();
+
+    @Test
+    void halfOpenCheckIsRepeatedWhenResumingTheConsumerFails() {
+        ServiceSupport consumer = (ServiceSupport) 
context.getRoute("halfOpen").getConsumer();
+
+        // the failure opens the circuit and suspends the consumer (the 
suspend is counted; the state is not asserted
+        // here, as the first check may already have run 100 millis later on a 
slow machine)
+        assertThrows(CamelExecutionException.class, () -> 
template.sendBody("direct:halfOpen", "fail"));
+        assertEquals(1, halfOpenPolicy.suspends.get());
+
+        // the first check fails to resume the consumer, the next one resumes 
it
+        await().atMost(10, TimeUnit.SECONDS).until(() -> "half 
opened".equals(halfOpenPolicy.getStateAsString()));
+        assertEquals(2, halfOpenPolicy.resumes.get());
+        assertTrue(consumer.isStarted());
+    }
+
+    @Test
+    void halfOpenCheckIsRepeatedWhenResumingTheConsumerFailsWhileClosing() {
+        ServiceSupport consumer = (ServiceSupport) 
context.getRoute("close").getConsumer();
+
+        assertThrows(CamelExecutionException.class, () -> 
template.sendBody("direct:close", "fail"));
+        assertEquals(1, closePolicy.suspends.get());
+
+        // the half open handler is ready: the first check fails to resume the 
consumer, the next one closes the circuit
+        await().atMost(10, TimeUnit.SECONDS).until(() -> 
"closed".equals(closePolicy.getStateAsString()));
+        assertEquals(2, closeChecks.get());
+        assertEquals(2, closePolicy.resumes.get());
+        assertTrue(consumer.isStarted());
+    }
+
+    @Test
+    void 
halfOpenCheckIsRepeatedWhenSuspendingTheConsumerFailsWhileKeepingItOpen() {
+        assertThrows(CamelExecutionException.class, () -> 
template.sendBody("direct:reopen", "fail"));
+        assertEquals("opened", reopenPolicy.getStateAsString());
+
+        // the half open handler is not ready: the first check fails to 
suspend the consumer again, and the circuit is
+        // still checked afterwards
+        await().atMost(10, TimeUnit.SECONDS).until(() -> reopenChecks.get() >= 
3);
+        assertEquals("opened", reopenPolicy.getStateAsString());
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() {
+        closePolicy.setHalfOpenHandler(() -> {
+            closeChecks.incrementAndGet();
+            return true;
+        });
+        reopenPolicy.setHalfOpenHandler(() -> {
+            reopenChecks.incrementAndGet();
+            return false;
+        });
+        return new RouteBuilder() {
+            @Override
+            public void configure() {
+                
from("direct:halfOpen").routeId("halfOpen").routePolicy(halfOpenPolicy)
+                        .throwException(new IllegalStateException("Downstream 
failure"));
+                from("direct:close").routeId("close").routePolicy(closePolicy)
+                        .throwException(new IllegalStateException("Downstream 
failure"));
+                
from("direct:reopen").routeId("reopen").routePolicy(reopenPolicy)
+                        .throwException(new IllegalStateException("Downstream 
failure"));
+            }
+        };
+    }
+
+    // opens the circuit on the first failure; the first resume fails, and so 
does the second suspend (the first one
+    // opens the circuit)
+    private static class FailOncePolicy extends ThrottlingExceptionRoutePolicy 
{
+        private final AtomicInteger resumes = new AtomicInteger();
+        private final AtomicInteger suspends = new AtomicInteger();
+
+        FailOncePolicy() {
+            super(1, 60000, 100, null);
+        }
+
+        @Override
+        public boolean resumeOrStartConsumer(Consumer consumer) throws 
Exception {
+            if (resumes.incrementAndGet() == 1) {
+                throw new IllegalStateException("Cannot resume the consumer");
+            }
+            return super.resumeOrStartConsumer(consumer);
+        }
+
+        @Override
+        public boolean suspendOrStopConsumer(Consumer consumer) throws 
Exception {
+            if (suspends.incrementAndGet() == 2) {
+                throw new IllegalStateException("Cannot suspend the consumer");
+            }
+            return super.suspendOrStopConsumer(consumer);
+        }
+    }
+}
diff --git 
a/core/camel-support/src/main/java/org/apache/camel/throttling/ThrottlingExceptionRoutePolicy.java
 
b/core/camel-support/src/main/java/org/apache/camel/throttling/ThrottlingExceptionRoutePolicy.java
index ebdcf84572aa..e8b77db388aa 100644
--- 
a/core/camel-support/src/main/java/org/apache/camel/throttling/ThrottlingExceptionRoutePolicy.java
+++ 
b/core/camel-support/src/main/java/org/apache/camel/throttling/ThrottlingExceptionRoutePolicy.java
@@ -636,11 +636,23 @@ public class ThrottlingExceptionRoutePolicy extends 
RoutePolicySupport implement
                 // the route was removed or the policy stopped since this 
check was scheduled
                 return;
             }
+            // the future of this check, unless another check has been 
scheduled since
+            ScheduledFuture<?> check = halfOpenCheck;
             try {
                 calculateState(route, null);
             } catch (Exception e) {
                 // the scheduler would keep it in the future of this check, 
where no one sees it
                 handleException(e);
+            } finally {
+                if (state.get() == STATE_OPEN && halfOpenCheck == check && 
scheduler == halfOpenScheduler) {
+                    // the circuit is still open and this check scheduled no 
other one, as suspending or resuming the
+                    // consumer failed (the exception went to the exception 
handler). The consumer is suspended, so no
+                    // exchange checks the circuit either: check again after 
halfOpenAfter
+                    LOG.warn("The circuit of route {} is still open after its 
half open check failed: checking again in {}"
+                             + " millis",
+                            route.getRouteId(), halfOpenAfter);
+                    addHalfOpenTimer(route);
+                }
             }
         }
     }

Reply via email to