This is an automated email from the ASF dual-hosted git repository. yong pushed a commit to branch branch-2.7 in repository https://gitbox.apache.org/repos/asf/pulsar.git
commit 43c529f9465c71bebb2f17dbcd42326399a51b77 Author: Enrico Olivelli <eolive...@gmail.com> AuthorDate: Thu Apr 1 14:46:19 2021 +0200 Avoid spammy logs in case of BK problems (#10088) (cherry picked from commit cdba1c03efff2b908850b54456a1843649fb98fb) --- .../apache/pulsar/broker/service/ServerCnx.java | 4 +++- .../apache/pulsar/functions/utils/Exceptions.java | 12 ++++++++++ .../pulsar/functions/utils/ExceptionsTest.java | 27 ++++++++++++++++++++++ 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/ServerCnx.java b/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/ServerCnx.java index d8f09b9..dd8ad4b 100644 --- a/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/ServerCnx.java +++ b/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/ServerCnx.java @@ -131,6 +131,7 @@ import org.apache.pulsar.common.util.FutureUtil; import org.apache.pulsar.common.util.SafeCollectionUtils; import org.apache.pulsar.common.util.collections.ConcurrentLongHashMap; import org.apache.pulsar.shaded.com.google.protobuf.v241.GeneratedMessageLite; +import org.apache.pulsar.functions.utils.Exceptions; import org.apache.pulsar.transaction.coordinator.TransactionCoordinatorID; import org.apache.pulsar.transaction.coordinator.impl.MLTransactionMetadataStore; import org.slf4j.Logger; @@ -1179,7 +1180,8 @@ public class ServerCnx extends PulsarHandler implements TransportCnx { cause = new TopicNotFoundException("Topic Not Found."); } - if (!(cause instanceof ServiceUnitNotReadyException)) { + if (!Exceptions.areExceptionsPresentInChain(cause, + ServiceUnitNotReadyException.class, ManagedLedgerException.class)) { // Do not print stack traces for expected exceptions log.error("[{}] Failed to create topic {}, producerId={}", remoteAddress, topicName, producerId, exception); diff --git a/pulsar-functions/utils/src/main/java/org/apache/pulsar/functions/utils/Exceptions.java b/pulsar-functions/utils/src/main/java/org/apache/pulsar/functions/utils/Exceptions.java index 19b31c6..c1e804c 100644 --- a/pulsar-functions/utils/src/main/java/org/apache/pulsar/functions/utils/Exceptions.java +++ b/pulsar-functions/utils/src/main/java/org/apache/pulsar/functions/utils/Exceptions.java @@ -48,4 +48,16 @@ public class Exceptions { return sw.toString(); } + public static boolean areExceptionsPresentInChain(Throwable error, Class ... types) { + while (error != null) { + for (Class type : types) { + if (type.isInstance(error)) { + return true; + } + } + error = error.getCause(); + } + return false; + } + } diff --git a/pulsar-functions/utils/src/test/java/org/apache/pulsar/functions/utils/ExceptionsTest.java b/pulsar-functions/utils/src/test/java/org/apache/pulsar/functions/utils/ExceptionsTest.java index 67e1406..f08c143 100644 --- a/pulsar-functions/utils/src/test/java/org/apache/pulsar/functions/utils/ExceptionsTest.java +++ b/pulsar-functions/utils/src/test/java/org/apache/pulsar/functions/utils/ExceptionsTest.java @@ -21,7 +21,9 @@ package org.apache.pulsar.functions.utils; import static org.apache.pulsar.functions.utils.Exceptions.rethrowIOException; import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertFalse; import static org.testng.Assert.assertSame; +import static org.testng.Assert.assertTrue; import static org.testng.Assert.fail; import java.io.IOException; @@ -77,4 +79,29 @@ public class ExceptionsTest { } } + @Test + public void testAreExceptionsPresentInChain() { + assertFalse(Exceptions.areExceptionsPresentInChain(null, IllegalStateException.class)); + } + + @Test + public void testAreExceptionsPresentInChain2() { + assertTrue(Exceptions.areExceptionsPresentInChain(new IllegalStateException(), IllegalStateException.class)); + } + + @Test + public void testAreExceptionsPresentInChain3() { + assertTrue(Exceptions.areExceptionsPresentInChain(new IllegalArgumentException(new IllegalStateException()), IllegalStateException.class)); + } + + @Test + public void testAreExceptionsPresentInChain4() { + assertTrue(Exceptions.areExceptionsPresentInChain(new IllegalArgumentException(new IllegalStateException()), UnsupportedOperationException.class, IllegalStateException.class)); + } + + @Test + public void testAreExceptionsPresentInChain5() { + assertFalse(Exceptions.areExceptionsPresentInChain(new IllegalArgumentException(new IllegalArgumentException()), IllegalStateException.class)); + } + }