You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2023/05/19 14:02:55 UTC

[camel] 01/01: CAMEL-19371: camel-core - Avoid suppressed exception added for same callsite causing deep nesting if having many redelivery attempts.

This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch 19371
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 5e65974f54389ecd0e3fdd63817bbe943eb07917
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Fri May 19 16:02:38 2023 +0200

    CAMEL-19371: camel-core - Avoid suppressed exception added for same callsite causing deep nesting if having many redelivery attempts.
---
 .../component/jms/JmsTransferExceptionTest.java    | 42 ++++++++++++-
 .../errorhandler/RedeliveryErrorHandler.java       | 26 +++++++-
 .../ErrorHandlerSuppressExceptionTest.java         | 69 ++++++++++++++++++++++
 3 files changed, 133 insertions(+), 4 deletions(-)

diff --git a/components/camel-jms/src/test/java/org/apache/camel/component/jms/JmsTransferExceptionTest.java b/components/camel-jms/src/test/java/org/apache/camel/component/jms/JmsTransferExceptionTest.java
index 67741692086..e5a0c108f28 100644
--- a/components/camel-jms/src/test/java/org/apache/camel/component/jms/JmsTransferExceptionTest.java
+++ b/components/camel-jms/src/test/java/org/apache/camel/component/jms/JmsTransferExceptionTest.java
@@ -18,9 +18,11 @@ package org.apache.camel.component.jms;
 
 import org.apache.camel.CamelContext;
 import org.apache.camel.ConsumerTemplate;
+import org.apache.camel.LoggingLevel;
 import org.apache.camel.ProducerTemplate;
 import org.apache.camel.RuntimeCamelException;
 import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.spi.CamelLogger;
 import org.apache.camel.test.infra.core.CamelContextExtension;
 import org.apache.camel.test.infra.core.DefaultCamelContextExtension;
 import org.junit.jupiter.api.BeforeEach;
@@ -41,6 +43,7 @@ public class JmsTransferExceptionTest extends AbstractJMSTest {
     protected CamelContext context;
     protected ProducerTemplate template;
     protected ConsumerTemplate consumer;
+    protected MyErrorLogger errorLogger = new MyErrorLogger();
 
     protected String getUri() {
         return "activemq:queue:JmsTransferExceptionTest?transferException=true";
@@ -72,7 +75,15 @@ public class JmsTransferExceptionTest extends AbstractJMSTest {
         assertNotNull(e.getCause().getStackTrace(), "Should contain a remote stacktrace");
 
         // we still try redeliver
-        assertEquals(3, counter);
+        assertEquals(5, counter);
+
+        // it's all the same exception so no suppressed
+        assertEquals(0, e.getSuppressed().length);
+
+        // and check what camel logged
+        Throwable t = errorLogger.getException();
+        assertNotNull(t);
+        assertEquals(0, t.getSuppressed().length);
     }
 
     @Override
@@ -85,7 +96,7 @@ public class JmsTransferExceptionTest extends AbstractJMSTest {
         return new RouteBuilder() {
             @Override
             public void configure() {
-                errorHandler(defaultErrorHandler().maximumRedeliveries(2));
+                errorHandler(defaultErrorHandler().maximumRedeliveries(4).logger(errorLogger));
 
                 from(getUri())
                         .process(exchange -> {
@@ -112,4 +123,31 @@ public class JmsTransferExceptionTest extends AbstractJMSTest {
         template = camelContextExtension.getProducerTemplate();
         consumer = camelContextExtension.getConsumerTemplate();
     }
+
+    private static class MyErrorLogger extends CamelLogger {
+
+        private Throwable exception;
+        private String message;
+        private LoggingLevel loggingLevel;
+
+        @Override
+        public void log(String message, Throwable exception, LoggingLevel loggingLevel) {
+            super.log(message, exception, loggingLevel);
+            this.message = message;
+            this.exception = exception;
+            this.loggingLevel = loggingLevel;
+        }
+
+        public Throwable getException() {
+            return exception;
+        }
+
+        public String getMessage() {
+            return message;
+        }
+
+        public LoggingLevel getLoggingLevel() {
+            return loggingLevel;
+        }
+    }
 }
diff --git a/core/camel-core-processor/src/main/java/org/apache/camel/processor/errorhandler/RedeliveryErrorHandler.java b/core/camel-core-processor/src/main/java/org/apache/camel/processor/errorhandler/RedeliveryErrorHandler.java
index 396d6f23b33..320bec4b600 100644
--- a/core/camel-core-processor/src/main/java/org/apache/camel/processor/errorhandler/RedeliveryErrorHandler.java
+++ b/core/camel-core-processor/src/main/java/org/apache/camel/processor/errorhandler/RedeliveryErrorHandler.java
@@ -499,7 +499,18 @@ public abstract class RedeliveryErrorHandler extends ErrorHandlerSupport
                     }
                 }
                 if (!found) {
-                    e.addSuppressed(previous);
+                    // okay before adding suppression then we must be sure its not referring to same method
+                    // which otherwise can lead to add the same exception over and over again
+                    StackTraceElement[] ste1 = e.getStackTrace();
+                    StackTraceElement[] ste2 = previous.getStackTrace();
+                    boolean same = false;
+                    if (ste1 != null && ste2 != null && ste1.length > 0 && ste2.length > 0) {
+                        same = ste1[0].getClassName().equals(ste2[0].getClassName())
+                                && ste1[0].getLineNumber() == ste2[0].getLineNumber();
+                    }
+                    if (!same) {
+                        e.addSuppressed(previous);
+                    }
                 }
             }
 
@@ -989,7 +1000,18 @@ public abstract class RedeliveryErrorHandler extends ErrorHandlerSupport
                     }
                 }
                 if (!found) {
-                    e.addSuppressed(previous);
+                    // okay before adding suppression then we must be sure its not referring to same method
+                    // which otherwise can lead to add the same exception over and over again
+                    StackTraceElement[] ste1 = e.getStackTrace();
+                    StackTraceElement[] ste2 = previous.getStackTrace();
+                    boolean same = false;
+                    if (ste1 != null && ste2 != null && ste1.length > 0 && ste2.length > 0) {
+                        same = ste1[0].getClassName().equals(ste2[0].getClassName())
+                                && ste1[0].getLineNumber() == ste2[0].getLineNumber();
+                    }
+                    if (!same) {
+                        e.addSuppressed(previous);
+                    }
                 }
             }
 
diff --git a/core/camel-core/src/test/java/org/apache/camel/processor/onexception/ErrorHandlerSuppressExceptionTest.java b/core/camel-core/src/test/java/org/apache/camel/processor/onexception/ErrorHandlerSuppressExceptionTest.java
new file mode 100644
index 00000000000..0aea0d88483
--- /dev/null
+++ b/core/camel-core/src/test/java/org/apache/camel/processor/onexception/ErrorHandlerSuppressExceptionTest.java
@@ -0,0 +1,69 @@
+/*
+ * 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.onexception;
+
+import java.io.IOException;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.builder.RouteBuilder;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+public class ErrorHandlerSuppressExceptionTest extends ContextTestSupport {
+
+    @Test
+    public void testSuppressException() throws Exception {
+        Exchange out = template.send("direct:start", new Processor() {
+            @Override
+            public void process(Exchange exchange) throws Exception {
+                exchange.getIn().setBody("Hello World");
+            }
+        });
+
+        Assertions.assertTrue(out.isFailed());
+        Exception t = out.getException();
+        Assertions.assertNotNull(t);
+        Assertions.assertEquals("Root exception", t.getMessage());
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                errorHandler(defaultErrorHandler().maximumRedeliveries(3).onRedelivery(new Processor() {
+                    @Override
+                    public void process(Exchange exchange) throws Exception {
+                        throw new IllegalArgumentException("Forced error during handling");
+                    }
+                }));
+
+                from("direct:start")
+                        .throwException(new IOException("Root exception"));
+                //                        .process(new Processor() {
+                //                            @Override
+                //                            public void process(Exchange exchange) throws Exception {
+                //                                throw new IOException("Root exception");
+                //                            }
+                //                        });
+            }
+        };
+    }
+
+}