dajac commented on a change in pull request #9878:
URL: https://github.com/apache/kafka/pull/9878#discussion_r662963252



##########
File path: 
clients/src/main/java/org/apache/kafka/common/internals/KafkaFutureImpl.java
##########
@@ -247,17 +138,32 @@ public boolean completeExceptionally(Throwable 
newException) {
      */
     @Override
     public synchronized boolean cancel(boolean mayInterruptIfRunning) {
-        return completeExceptionally(new CancellationException()) || exception 
instanceof CancellationException;
+        return completableFuture.cancel(mayInterruptIfRunning);
+    }
+
+    // We need to deal with differences between KF's historic API and the API 
of CF:
+    // CF#get() does not wrap CancellationException in ExecutionException (nor 
does KF).
+    // CF#get() always wraps the _cause_ of a CompletionException in 
ExecutionException (which KF does not).
+    //
+    // The semantics for KafkaFuture are that all exceptional completions of 
the future (via #completeExceptionally() or exceptions from dependants)
+    // manifest as ExecutionException, as observed via both get() and getNow().
+    private void maybeRewrapAndThrow(Throwable cause) {
+        if (cause instanceof CancellationException) {
+            throw (CancellationException) cause;
+        }
     }
 
     /**
      * Waits if necessary for this future to complete, and then returns its 
result.
      */
     @Override
     public T get() throws InterruptedException, ExecutionException {
-        SingleWaiter<T> waiter = new SingleWaiter<>();
-        addWaiter(waiter);
-        return waiter.await();
+        try {
+            return completableFuture.get();
+        } catch (ExecutionException e) {
+            maybeRewrapAndThrow(e.getCause());
+            throw e;

Review comment:
       I don't like the fact that we throw in two places... Could we at least 
make the name of `maybeRewrapAndThrow` a bit more explicit? It is only about 
`CancellationException` in the end so we could name it 
`maybeThrowCancellationException` or something like this. Moreover, the method 
does not really "rewrap" anything, right? It just checks the type and throw it.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to