You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jira@kafka.apache.org by "Hangleton (via GitHub)" <gi...@apache.org> on 2023/01/27 13:03:15 UTC

[GitHub] [kafka] Hangleton commented on a diff in pull request #13169: Port opening fix

Hangleton commented on code in PR #13169:
URL: https://github.com/apache/kafka/pull/13169#discussion_r1088918527


##########
clients/src/main/java/org/apache/kafka/common/utils/Time.java:
##########
@@ -86,4 +89,30 @@ default Timer timer(Duration timeout) {
         return timer(timeout.toMillis());
     }
 
+    /**
+     * Wait for a future to complete, or time out.
+     *
+     * @param future        The future to wait for.
+     * @param deadlineNs    The time in the future, in monotonic nanoseconds, to time out.
+     * @return              The result of the future.
+     * @param <T>           The type of the future.
+     */
+    default <T> T waitForFuture(
+        CompletableFuture<T> future,
+        long deadlineNs
+    ) throws TimeoutException, InterruptedException, ExecutionException  {
+        TimeoutException timeoutException = null;
+        while (true) {
+            long nowNs = nanoseconds();
+            if (deadlineNs <= nowNs) {
+                throw (timeoutException == null) ? new TimeoutException() : timeoutException;
+            }
+            long deltaNs = deadlineNs - nowNs;
+            try {
+                return future.get(deltaNs, TimeUnit.NANOSECONDS);

Review Comment:
   Naive question - what is incorrect with 
   
   ```
   default <T> T waitForFuture(
           CompletableFuture<T> future,
           long deadlineNs
       ) throws TimeoutException, InterruptedException, ExecutionException  {
   
     return future.get(Math.max(0, deadlineNs - nanoseconds()))
   }
   ```



##########
server-common/src/main/java/org/apache/kafka/server/util/FutureUtils.java:
##########
@@ -0,0 +1,135 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.kafka.server.util;
+
+import org.apache.kafka.common.utils.Time;
+import org.slf4j.Logger;
+
+import java.math.BigInteger;
+import java.util.Collection;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeoutException;
+import java.util.function.BiConsumer;
+
+
+public class FutureUtils {
+    /**
+     * Based on the current time and a delay, computes a monotonic deadline in the future.
+     *
+     * @param nowNs     The current time in monotonic nanoseconds.
+     * @param delayMs   The delay in milliseconds.
+     * @return          The monotonic deadline in the future. This value is capped at
+     *                  Long.MAX_VALUE.
+     */
+    public static long getDeadlineNsFromDelayMs(
+        long nowNs,
+        long delayMs
+    ) {
+        if (delayMs < 0) {
+            throw new RuntimeException("Negative delays are not allowed.");
+        }
+        BigInteger delayNs = BigInteger.valueOf(delayMs).multiply(BigInteger.valueOf(1_000_000));

Review Comment:
   Curious - which use cases result in overflow of the long values in this context?



-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org