Apache9 commented on code in PR #1822:
URL: 
https://github.com/apache/incubator-pegasus/pull/1822#discussion_r1508545770


##########
java-client/src/main/java/org/apache/pegasus/client/retry/DefaultRetryPolicy.java:
##########
@@ -0,0 +1,47 @@
+/*
+ * 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.pegasus.client.retry;
+
+import java.time.Duration;
+import java.util.concurrent.TimeUnit;
+import org.apache.pegasus.client.ClientOptions;
+
+/**
+ * The default retry policy, which is the only policy before we introduce the 
retry policy
+ * mechanism. It only considers the timeout value to calculate the retry 
delay, that's why we need
+ * to pass the {@code timeout} value in the {@link #shouldRetry(int, long, 
Duration)} method.
+ */
+public class DefaultRetryPolicy implements RetryPolicy {
+  public DefaultRetryPolicy(ClientOptions opts) {
+    // do nothing, just keep the constructor.
+  }
+
+  @Override
+  public RetryAction shouldRetry(int retries, long deadlineNanos, Duration 
timeout) {
+    long now = System.nanoTime();
+    if (now >= deadlineNanos) {
+      return new RetryAction(RetryDecision.FAIL, Duration.ZERO, "request 
deadline reached");
+    }
+    long timeoutMs = timeout.toMillis();
+    long retryDelayMs =
+        Math.min(
+            timeoutMs > 3 ? timeoutMs / 3 : 1, 
TimeUnit.NANOSECONDS.toMillis(deadlineNanos - now));
+    return new RetryAction(RetryDecision.RETRY, 
Duration.ofMillis(retryDelayMs), "");

Review Comment:
   Since here we also pass a Duration in, let's use nanos instead of millis?



##########
java-client/src/main/java/org/apache/pegasus/client/retry/ExponentialBackoffRetryPolicy.java:
##########
@@ -0,0 +1,64 @@
+/*
+ * 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.pegasus.client.retry;
+
+import java.time.Duration;
+import java.util.concurrent.ThreadLocalRandom;
+import java.util.concurrent.TimeUnit;
+import org.apache.pegasus.client.ClientOptions;
+
+/** A retry policy which generates retry interval with exponential backoff 
policy. */
+public class ExponentialBackoffRetryPolicy implements RetryPolicy {
+
+  private static final int[] RETRY_BACKOFF = {1, 2, 3, 5, 10, 20, 40, 100, 
100, 100, 100, 200, 200};
+
+  private final long retryBaseIntervalMs;
+
+  private final int retryMaxTimes;
+
+  public ExponentialBackoffRetryPolicy(ClientOptions opts) {
+    // The minimum unit of retry interval time is milliseconds.
+    // When the user sets the interval time unit to nano, it will change to 
default interval ms.
+    if (opts.getRetryBaseInterval().toMillis() < 1) {
+      this.retryBaseIntervalMs = ClientOptions.DEFAULT_RETRY_BASE_INTERVAL_MS;
+    } else {
+      this.retryBaseIntervalMs = opts.getRetryBaseInterval().toMillis();
+    }
+    this.retryMaxTimes = opts.getRetryMaxTimes();
+  }
+
+  @Override
+  public RetryAction shouldRetry(int retries, long deadlineNanos, Duration 
timeout) {
+    if (retries >= retryMaxTimes) {
+      return new RetryAction(
+          RetryDecision.FAIL, Duration.ZERO, "max retry times " + 
retryMaxTimes + " reached");
+    }
+    long now = System.nanoTime();
+    if (now >= deadlineNanos) {
+      return new RetryAction(RetryDecision.FAIL, Duration.ZERO, "request 
deadline reached");
+    }
+    long normalIntervalMs =

Review Comment:
   Use nanos too.



-- 
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]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to