Skip to content

Commit

Permalink
Avoid spammy logs in case of BK problems (apache#10088)
Browse files Browse the repository at this point in the history
  • Loading branch information
eolivelli authored Apr 1, 2021
1 parent efea8b2 commit cdba1c0
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@
import org.apache.pulsar.common.schema.SchemaType;
import org.apache.pulsar.common.util.FutureUtil;
import org.apache.pulsar.common.util.collections.ConcurrentLongHashMap;
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;
Expand Down Expand Up @@ -1245,7 +1246,8 @@ protected void handleProducer(final CommandProducer cmdProducer) {
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,16 @@ public static String toString(Exception ex) {
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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@

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;
Expand Down Expand Up @@ -77,4 +79,29 @@ public void testRethrowOtherExceptionAsIOException() throws IOException {
}
}

@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));
}

}

0 comments on commit cdba1c0

Please sign in to comment.