bbeaudreault commented on code in PR #5275:
URL: https://github.com/apache/hbase/pull/5275#discussion_r1240875045


##########
hbase-client/src/main/java/org/apache/hadoop/hbase/client/backoff/HBaseServerExceptionPauseManager.java:
##########
@@ -0,0 +1,95 @@
+/*
+ * 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.hadoop.hbase.client.backoff;
+
+import static org.apache.hadoop.hbase.client.ConnectionUtils.SLEEP_DELTA_NS;
+import static org.apache.hadoop.hbase.client.ConnectionUtils.getPauseTime;
+
+import java.util.OptionalLong;
+import java.util.concurrent.TimeUnit;
+import org.apache.hadoop.hbase.HBaseServerException;
+import org.apache.hadoop.hbase.quotas.RpcThrottlingException;
+import org.apache.yetus.audience.InterfaceAudience;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
[email protected]
+public class HBaseServerExceptionPauseManager {
+  private static final Logger LOG = 
LoggerFactory.getLogger(HBaseServerExceptionPauseManager.class);
+
+  private final long pauseNs;
+  private final long pauseNsForServerOverloaded;
+  private final long timeoutNs;
+  private final long startNs;
+
+  public HBaseServerExceptionPauseManager(long pauseNs, long 
pauseNsForServerOverloaded,
+    long timeoutNs, long startNs) {
+    this.pauseNs = pauseNs;
+    this.pauseNsForServerOverloaded = pauseNsForServerOverloaded;
+    this.timeoutNs = timeoutNs;
+    this.startNs = startNs;
+  }
+
+  /**
+   * Returns the nanos, if any, for which the client should wait
+   * @param error The exception from the server
+   * @param tries The current retry count
+   * @return The time, in nanos, to pause. If empty then pausing would exceed 
our timeout, so we
+   *         should throw now
+   */
+  public OptionalLong getPauseNsFromException(Throwable error, int tries) {
+    long expectedSleepNs;
+    if (error instanceof RpcThrottlingException) {
+      RpcThrottlingException rpcThrottlingException = (RpcThrottlingException) 
error;
+      expectedSleepNs = 
TimeUnit.MILLISECONDS.toNanos(rpcThrottlingException.getWaitInterval());
+      long remainingTimeNs = remainingTimeNs();
+      if (expectedSleepNs > remainingTimeNs && remainingTimeNs > 0) {
+        if (LOG.isDebugEnabled()) {
+          LOG.debug("RpcThrottlingException suggested pause of {}ns which 
would exceed "
+            + "the timeout. We should throw instead.", expectedSleepNs, 
rpcThrottlingException);
+        }
+        return OptionalLong.empty();
+      }
+      if (LOG.isDebugEnabled()) {
+        LOG.debug("Sleeping for {}ns after catching RpcThrottlingException", 
expectedSleepNs,
+          rpcThrottlingException);
+      }
+    } else {
+      expectedSleepNs =
+        HBaseServerException.isServerOverloaded(error) ? 
pauseNsForServerOverloaded : pauseNs;
+      // RpcThrottlingException tells us exactly how long the client should 
wait for,
+      // so we should not factor in the retry count for said exception
+      expectedSleepNs = getPauseTime(expectedSleepNs, tries - 1);
+    }
+
+    if (timeoutNs > 0) {
+      long remainingTimeNs = remainingTimeNs();
+      if (remainingTimeNs <= 0) {
+        return OptionalLong.empty();
+      }
+      expectedSleepNs = Math.min(remainingTimeNs, expectedSleepNs);
+    }
+
+    return OptionalLong.of(expectedSleepNs);
+  }
+
+  private long remainingTimeNs() {

Review Comment:
   Correct me if I'm wrong, but in the context of 
AsyncScanSingleRegionRpcRetryingCaller, `remainingTimeNs()` is only called once 
prior to this patch. The call moved into our new PauseManager code, so I assume 
that method is now unused.
   
   In the context of AsyncBatchRpcRetryingCaller, it is called 3 times... so 
not unused at this point, but maybe could still be unified. I feel like we 
could do this:
   
   1. Remove `startNs` arg from PauseManager constructor, since as you said 
it's not final for scans.
   2. expose `PauseManager.remainingTimeNs()` as public or package protected, 
and add a `long startNs` argument.
   3. Also add `long startNs` as argument in `getPauseNsFromException()` since 
it seems we need that for scans
   4. In getPauseNsFromException(), use `remainingTimeNs(startNs) - 
SLEEP_DELTA_NS` (so the SLEEP_DELTA_NS gets removed from 
`PauseManager.remainingTimeNs(long startNs)` method
   5. Replace all the calls to existing `remainingTimeNs()` methods in 
AsyncBatchCaller with `pauseManager.remainingTimeNs(startNs)`
   
   Thoughts?



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