You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@bookkeeper.apache.org by yo...@apache.org on 2022/08/02 06:25:56 UTC

[bookkeeper] 03/22: enhance future sync wait. (#3336)

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

yong pushed a commit to branch branch-4.15
in repository https://gitbox.apache.org/repos/asf/bookkeeper.git

commit e3ab4d9306fc3109d34a80e9b65d3af1cd07df4e
Author: Yan Zhao <ho...@apache.org>
AuthorDate: Tue Jul 26 09:38:35 2022 +0800

    enhance future sync wait. (#3336)
    
    Descriptions of the changes in this PR:
    
    Enhance future sync wait.
    The same logicment:
    https://github.com/apache/bookkeeper/blob/f887f8d7a507800b71b4143a40b0e45902f5f170/bookkeeper-server/src/main/java/org/apache/bookkeeper/client/SyncCallbackUtils.java#L45-L71
    
    (cherry picked from commit 03255bbd391e8298e358c25304f7630d5ea36f74)
---
 .../apache/bookkeeper/common/concurrent/FutureUtils.java   | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)

diff --git a/bookkeeper-common/src/main/java/org/apache/bookkeeper/common/concurrent/FutureUtils.java b/bookkeeper-common/src/main/java/org/apache/bookkeeper/common/concurrent/FutureUtils.java
index ab2d1ca9ec..8b36d40c75 100644
--- a/bookkeeper-common/src/main/java/org/apache/bookkeeper/common/concurrent/FutureUtils.java
+++ b/bookkeeper-common/src/main/java/org/apache/bookkeeper/common/concurrent/FutureUtils.java
@@ -69,7 +69,19 @@ public final class FutureUtils {
     public static <T, ExceptionT extends Throwable> T result(
         CompletableFuture<T> future, Function<Throwable, ExceptionT> exceptionHandler) throws ExceptionT {
         try {
-            return future.get();
+            try {
+                /*
+                 * CompletableFuture.get() in JDK8 spins before blocking and wastes CPU time.
+                 * CompletableFuture.get(long, TimeUnit) blocks immediately (if the result is
+                 * not yet available). While the implementation of get() has changed in JDK9
+                 * (not spinning any more), using CompletableFuture.get(long, TimeUnit) allows
+                 * us to avoid spinning for all current JDK versions.
+                 */
+                return future.get(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
+            } catch (TimeoutException eignore) {
+                // it's ok to return null if we timeout after 292 years (2^63 nanos)
+                return null;
+            }
         } catch (InterruptedException e) {
             Thread.currentThread().interrupt();
             throw e;