Skip to content

MINOR: Catch InvocationTargetException explicitly and propagate underlying cause #12230

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Arrays;
Expand Down Expand Up @@ -75,8 +76,12 @@ public void register() throws ReflectiveOperationException {
private Object createSignalHandler(final Map<String, Object> jvmSignalHandlers) {
InvocationHandler invocationHandler = new InvocationHandler() {

private String getName(Object signal) throws ReflectiveOperationException {
return (String) signalGetNameMethod.invoke(signal);
private String getName(Object signal) throws Throwable {
try {
return (String) signalGetNameMethod.invoke(signal);
} catch (InvocationTargetException e) {
throw e.getCause();
}
}

private void handle(Object signalHandler, Object signal) throws ReflectiveOperationException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -463,8 +463,7 @@ public static <T> T newParameterizedInstance(String className, Object... params)
throw new ClassNotFoundException(String.format("Unable to access " +
"constructor of %s", className), e);
} catch (InvocationTargetException e) {
throw new ClassNotFoundException(String.format("Unable to invoke " +
"constructor of %s", className), e);
throw new KafkaException(String.format("The constructor of %s threw an exception", className), e.getCause());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have we checked that changing this exception doesn't cause an issue?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. This method is only used in trogdor today and there is no specific handling of ClassNotFoundException exception.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we throw ClassNotFoundException like before, and pass e.getCause() to it instead? I think throwing ClassNotFoundException for consistency is better.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't a ClassNotFoundException be wrong in this context and give a wrong impression to user? I am saying this because it's not a problem with Class not being on the JVM's classpath, instead InvocationTargetException is thrown when we have found the Class and invoking the constructor is throwing an exception in the line constructor.newInstance(args

I am happy to replace KafkaException with other generic RuntimeException that you would suggest but ClassNotFoundException appears inappropriate to me.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point! You're right! When entering here, the constructor is already found and called, just there's exception thrown. Let's keep KafkaException like you did. Thank you.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, but I would disagree that it is a downside.

In my opinion, a caller should be handling the underlying cause because the InvocationTargetException is being thrown from business logic inside the constructor. If the caller catches a generic ReflectiveOperationException it doesn't get to know (without inspecting details) whether the exception is due to a "reflection op error" such as method not found, class not found etc. or whether it is an exception thrown by the business logic itself. I believe that propagating the underlying business logic exception is better in this scenario.

Having said that, I don't have a strong opinion on this one as it's a minor improvement that will only impact code debuggability when looking at exception traces. Happy to revert if you think otherwise.

Copy link
Contributor

@mdedetrich mdedetrich Aug 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also agree with @divijvaidya here, having a strong distinction between business logic and reflection based exceptions is ideal and in my opinion over the long term is less brittle (since if you catch business logic exceptions that is unlikely to change)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The caller may be itself generic code. For example, see what happened here:

https://github.com/apache/kafka/pull/12230/files#diff-ac079c35d502aee90e8bbfa3b71cd9deda5ad4ffb49364fcc23a9278a848241cR79

The method now throws Throwable. It was pretty hard for me to say whether we needed to update some code to handle other exceptions. Is it clear to you?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ijuma , thanks for the explanation. Make sense to me.
@divijvaidya I think it's better we revert it. WDYT?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@showuon sure, let's do it. I understand the POV where @ijuma is coming from.

}
}

Expand Down
44 changes: 14 additions & 30 deletions clients/src/test/java/org/apache/kafka/common/KafkaFutureTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,14 @@ private void awaitAndAssertCancelled(KafkaFuture<?> future, String expectedMessa
assertEquals(CancellationException.class, cancellationException.getClass());
}

private Object invokeOrThrow(final Method method, final Object obj, final Object... args) throws Throwable {
try {
return method.invoke(obj, args);
} catch (InvocationTargetException e) {
throw e.getCause();
}
}

@Test
public void testCompleteFutures() throws Exception {
KafkaFutureImpl<Integer> future123 = new KafkaFutureImpl<>();
Expand Down Expand Up @@ -591,7 +599,7 @@ public void testFutureTimeoutWithZeroWait() {

@Test
@SuppressWarnings("unchecked")
public void testLeakCompletableFuture() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
public void testLeakCompletableFuture() throws Throwable {
final KafkaFutureImpl<String> kfut = new KafkaFutureImpl<>();
CompletableFuture<String> comfut = kfut.toCompletionStage().toCompletableFuture();
assertThrows(UnsupportedOperationException.class, () -> comfut.complete(""));
Expand All @@ -600,44 +608,20 @@ public void testLeakCompletableFuture() throws NoSuchMethodException, Invocation
// so test reflectively
if (Java.IS_JAVA9_COMPATIBLE) {
Method completeOnTimeout = CompletableFuture.class.getDeclaredMethod("completeOnTimeout", Object.class, Long.TYPE, TimeUnit.class);
assertThrows(UnsupportedOperationException.class, () -> {
try {
completeOnTimeout.invoke(comfut, "", 1L, TimeUnit.MILLISECONDS);
} catch (InvocationTargetException e) {
throw e.getCause();
}
});
assertThrows(UnsupportedOperationException.class, () -> invokeOrThrow(completeOnTimeout, comfut, "", 1L, TimeUnit.MILLISECONDS));

Method completeAsync = CompletableFuture.class.getDeclaredMethod("completeAsync", Supplier.class);
assertThrows(UnsupportedOperationException.class, () -> {
try {
completeAsync.invoke(comfut, (Supplier<String>) () -> "");
} catch (InvocationTargetException e) {
throw e.getCause();
}
});
assertThrows(UnsupportedOperationException.class, () -> invokeOrThrow(completeAsync, comfut, (Supplier<String>) () -> ""));

Method obtrudeValue = CompletableFuture.class.getDeclaredMethod("obtrudeValue", Object.class);
assertThrows(UnsupportedOperationException.class, () -> {
try {
obtrudeValue.invoke(comfut, "");
} catch (InvocationTargetException e) {
throw e.getCause();
}
});
assertThrows(UnsupportedOperationException.class, () -> invokeOrThrow(obtrudeValue, comfut, ""));

Method obtrudeException = CompletableFuture.class.getDeclaredMethod("obtrudeException", Throwable.class);
assertThrows(UnsupportedOperationException.class, () -> {
try {
obtrudeException.invoke(comfut, new RuntimeException());
} catch (InvocationTargetException e) {
throw e.getCause();
}
});
assertThrows(UnsupportedOperationException.class, () -> invokeOrThrow(obtrudeException, comfut, new RuntimeException()));

// Check the CF from a minimal CompletionStage doesn't cause completion of the original KafkaFuture
Method minimal = CompletableFuture.class.getDeclaredMethod("minimalCompletionStage");
CompletionStage<String> cs = (CompletionStage<String>) minimal.invoke(comfut);
CompletionStage<String> cs = (CompletionStage<String>) invokeOrThrow(minimal, comfut);
cs.toCompletableFuture().complete("");

assertFalse(kfut.isDone());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public static void replayAll(Object target,
}
}
} catch (InvocationTargetException e) {
throw new RuntimeException(e);
throw new RuntimeException(e.getCause());
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
Expand Down