You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hbase.apache.org by st...@apache.org on 2018/05/01 14:01:07 UTC
hbase git commit: HBASE-20506 Add doc and test for unused
RetryCounter, useful-looking utility
Repository: hbase
Updated Branches:
refs/heads/branch-2 da3e06afa -> 7c4182755
HBASE-20506 Add doc and test for unused RetryCounter, useful-looking utility
Project: http://git-wip-us.apache.org/repos/asf/hbase/repo
Commit: http://git-wip-us.apache.org/repos/asf/hbase/commit/7c418275
Tree: http://git-wip-us.apache.org/repos/asf/hbase/tree/7c418275
Diff: http://git-wip-us.apache.org/repos/asf/hbase/diff/7c418275
Branch: refs/heads/branch-2
Commit: 7c41827559056b1f8a892cfdcfe1835c78334910
Parents: da3e06a
Author: Michael Stack <st...@apache.org>
Authored: Sun Apr 29 21:51:05 2018 -0700
Committer: Michael Stack <st...@apache.org>
Committed: Tue May 1 07:00:51 2018 -0700
----------------------------------------------------------------------
.../apache/hadoop/hbase/util/RetryCounter.java | 26 +++++++--
.../hadoop/hbase/util/TestRetryCounter.java | 55 ++++++++++++++++++++
2 files changed, 76 insertions(+), 5 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/hbase/blob/7c418275/hbase-common/src/main/java/org/apache/hadoop/hbase/util/RetryCounter.java
----------------------------------------------------------------------
diff --git a/hbase-common/src/main/java/org/apache/hadoop/hbase/util/RetryCounter.java b/hbase-common/src/main/java/org/apache/hadoop/hbase/util/RetryCounter.java
index ffcd9ca..4c163fd 100644
--- a/hbase-common/src/main/java/org/apache/hadoop/hbase/util/RetryCounter.java
+++ b/hbase-common/src/main/java/org/apache/hadoop/hbase/util/RetryCounter.java
@@ -24,9 +24,17 @@ import org.apache.yetus.audience.InterfaceAudience;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+/**
+ * Operation retry accounting.
+ * Use to calculate wait period, {@link #getBackoffTimeAndIncrementAttempts()}}, or for performing
+ * wait, {@link #sleepUntilNextRetry()}, in accordance with a {@link RetryConfig}, initial
+ * settings, and a Retry Policy, (See org.apache.hadoop.io.retry.RetryPolicy).
+ * Like <a href=https://github.com/rholder/guava-retrying>guava-retrying</a>.
+ * @since 0.92.0
+ * @see RetryCounterFactory
+ */
@InterfaceAudience.Private
public class RetryCounter {
-
/**
* Configuration for a retry counter
*/
@@ -151,10 +159,8 @@ public class RetryCounter {
*/
public void sleepUntilNextRetry() throws InterruptedException {
int attempts = getAttemptTimes();
- long sleepTime = retryConfig.backoffPolicy.getBackoffTime(retryConfig, attempts);
- if (LOG.isTraceEnabled()) {
- LOG.trace("Sleeping " + sleepTime + "ms before retry #" + attempts + "...");
- }
+ long sleepTime = getBackoffTime();
+ LOG.trace("Sleeping {} ms before retry #{}...", sleepTime, attempts);
retryConfig.getTimeUnit().sleep(sleepTime);
useRetry();
}
@@ -174,4 +180,14 @@ public class RetryCounter {
public int getAttemptTimes() {
return attempts;
}
+
+ public long getBackoffTime() {
+ return this.retryConfig.backoffPolicy.getBackoffTime(this.retryConfig, getAttemptTimes());
+ }
+
+ public long getBackoffTimeAndIncrementAttempts() {
+ long backoffTime = getBackoffTime();
+ useRetry();
+ return backoffTime;
+ }
}
http://git-wip-us.apache.org/repos/asf/hbase/blob/7c418275/hbase-common/src/test/java/org/apache/hadoop/hbase/util/TestRetryCounter.java
----------------------------------------------------------------------
diff --git a/hbase-common/src/test/java/org/apache/hadoop/hbase/util/TestRetryCounter.java b/hbase-common/src/test/java/org/apache/hadoop/hbase/util/TestRetryCounter.java
new file mode 100644
index 0000000..c0706b7
--- /dev/null
+++ b/hbase-common/src/test/java/org/apache/hadoop/hbase/util/TestRetryCounter.java
@@ -0,0 +1,55 @@
+/**
+ * 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.util;
+
+import static junit.framework.TestCase.assertTrue;
+
+import org.apache.hadoop.hbase.HBaseClassTestRule;
+import org.apache.hadoop.hbase.testclassification.MiscTests;
+import org.apache.hadoop.hbase.testclassification.SmallTests;
+import org.junit.ClassRule;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+* Basic test for some old functionality we don't seem to have used but that looks nice.
+ */
+@Category({MiscTests.class, SmallTests.class})
+public class TestRetryCounter {
+ @ClassRule
+ public static final HBaseClassTestRule CLASS_RULE =
+ HBaseClassTestRule.forClass(TestRetryCounter.class);
+ private static final Logger LOG = LoggerFactory.getLogger(TestRetryCounter.class);
+
+ @Test
+ public void testBasics() throws InterruptedException {
+ int maxAttempts = 10;
+ RetryCounterFactory factory =
+ new RetryCounterFactory(maxAttempts, 10, 1000);
+ RetryCounter retryCounter = factory.create();
+ while (retryCounter.shouldRetry()) {
+ LOG.info("Attempt={}, backoffTime={}", retryCounter.getAttemptTimes(),
+ retryCounter.getBackoffTime());
+ retryCounter.sleepUntilNextRetry();
+ }
+ assertTrue(retryCounter.getAttemptTimes() == maxAttempts);
+ }
+}