From eb43f0a5d0375a0f33a4b7b62ff91eca42fa4a34 Mon Sep 17 00:00:00 2001 From: tedyu Date: Mon, 18 Jul 2016 03:35:10 -0700 Subject: [PATCH] HBASE-16172 Unify the retry logic in ScannerCallableWithReplicas and RpcRetryingCallerWithReadReplicas --- .../org/apache/hadoop/hbase/client/HTable.java | 2 +- .../client/RpcRetryingCallerWithReadReplicas.java | 14 +++++++++++--- .../hbase/client/ScannerCallableWithReplicas.java | 5 ++++- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/HTable.java b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/HTable.java index 54fbfe9e63f..fbd9f51cf7f 100644 --- a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/HTable.java +++ b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/HTable.java @@ -440,7 +440,7 @@ public class HTable implements Table { connConfiguration.getRetriesNumber(), operationTimeout, connConfiguration.getPrimaryCallTimeoutMicroSecond()); - return callable.call(); + return callable.call(operationTimeout); } diff --git a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/RpcRetryingCallerWithReadReplicas.java b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/RpcRetryingCallerWithReadReplicas.java index 425d314018b..65dbb107445 100644 --- a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/RpcRetryingCallerWithReadReplicas.java +++ b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/RpcRetryingCallerWithReadReplicas.java @@ -27,6 +27,7 @@ import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -188,7 +189,7 @@ public class RpcRetryingCallerWithReadReplicas { * Globally, the number of retries, timeout and so on still applies, but it's per replica, * not global. We continue until all retries are done, or all timeouts are exceeded. */ - public synchronized Result call() + public Result call(int operationTimeout) throws DoNotRetryIOException, InterruptedIOException, RetriesExhaustedException { boolean isTargetReplicaSpecified = (get.getReplicaId() >= 0); @@ -221,10 +222,17 @@ public class RpcRetryingCallerWithReadReplicas { try { try { - Future f = cs.take(); - return f.get(); + long start = EnvironmentEdgeManager.currentTime(); + Future f = cs.poll(operationTimeout, TimeUnit.MILLISECONDS); + long duration = EnvironmentEdgeManager.currentTime() - start; + if (f == null) { + throw new RetriesExhaustedException("timed out after " + duration + " ms"); + } + return f.get(operationTimeout - duration, TimeUnit.MILLISECONDS); } catch (ExecutionException e) { throwEnrichedException(e, retries); + } catch (TimeoutException te) { + throw new RetriesExhaustedException("timed out after " + operationTimeout + " ms"); } } catch (CancellationException e) { throw new InterruptedIOException(); diff --git a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/ScannerCallableWithReplicas.java b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/ScannerCallableWithReplicas.java index 0fbb2e7cc64..c3a38340db0 100644 --- a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/ScannerCallableWithReplicas.java +++ b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/ScannerCallableWithReplicas.java @@ -41,6 +41,7 @@ import org.apache.hadoop.hbase.RegionLocations; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.classification.InterfaceAudience; import org.apache.hadoop.hbase.util.Bytes; +import org.apache.hadoop.hbase.util.EnvironmentEdgeManager; import org.apache.hadoop.hbase.util.Pair; import com.google.common.annotations.VisibleForTesting; @@ -195,9 +196,11 @@ class ScannerCallableWithReplicas implements RetryingCallable { addCallsForOtherReplicas(cs, rl, 0, rl.size() - 1); try { + long start = EnvironmentEdgeManager.currentTime(); Future> f = cs.poll(timeout, TimeUnit.MILLISECONDS); + long duration = EnvironmentEdgeManager.currentTime() - start; if (f != null) { - Pair r = f.get(timeout, TimeUnit.MILLISECONDS); + Pair r = f.get(timeout - duration, TimeUnit.MILLISECONDS); if (r != null && r.getSecond() != null) { updateCurrentlyServingReplica(r.getSecond(), r.getFirst(), done, pool); }