You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@accumulo.apache.org by GitBox <gi...@apache.org> on 2018/03/14 18:40:06 UTC

[GitHub] keith-turner commented on a change in pull request #399: ACCUMULO-4847 Fix Retry utility's API

keith-turner commented on a change in pull request #399: ACCUMULO-4847 Fix Retry utility's API
URL: https://github.com/apache/accumulo/pull/399#discussion_r174559977
 
 

 ##########
 File path: fate/src/main/java/org/apache/accumulo/fate/util/Retry.java
 ##########
 @@ -0,0 +1,356 @@
+/*
+ * 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.accumulo.fate.util;
+
+import static java.util.concurrent.TimeUnit.MILLISECONDS;
+import static java.util.concurrent.TimeUnit.NANOSECONDS;
+
+import java.util.concurrent.TimeUnit;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.Preconditions;
+
+/**
+ * Encapsulates the retrying implementation for some operation. Provides bounded retry attempts with a bounded, linear backoff.
+ */
+public class Retry {
+  private static final Logger log = LoggerFactory.getLogger(Retry.class);
+
+  private long maxRetries; // not final for testing
+  private long waitIncrement; // not final for testing
+  private long maxWait; // not final for testing
+  private final long logIntervalNanoSec;
+
+  private long retriesDone;
+  private long currentWait;
+
+  private boolean hasNeverLogged;
+  private long lastRetryLog;
+
+  /**
+   * @param maxRetries
+   *          Maximum times to retry or MAX_RETRY_DISABLED if no maximum
+   * @param startWait
+   *          The amount of time (ms) to wait for the initial retry
+   * @param maxWait
+   *          The maximum wait (ms)
+   * @param waitIncrement
+   *          The amount of time (ms) to increment next wait time by
+   * @param logInterval
+   *          The amount of time (ms) between logging retries
+   */
+  private Retry(long maxRetries, long startWait, long waitIncrement, long maxWait, long logInterval) {
+    this.maxRetries = maxRetries;
+    this.maxWait = maxWait;
+    this.waitIncrement = waitIncrement;
+    this.retriesDone = 0;
+    this.currentWait = startWait;
+    this.logIntervalNanoSec = MILLISECONDS.toNanos(logInterval);
+    this.hasNeverLogged = true;
+    this.lastRetryLog = -1;
+  }
+
+  // Visible for testing
+  @VisibleForTesting
+  long getMaxRetries() {
+    return maxRetries;
+  }
+
+  // Visible for testing
+  @VisibleForTesting
+  long getCurrentWait() {
+    return currentWait;
+  }
+
+  // Visible for testing
+  @VisibleForTesting
+  long getWaitIncrement() {
+    return waitIncrement;
+  }
+
+  // Visible for testing
+  @VisibleForTesting
+  long getMaxWait() {
+    return maxWait;
+  }
+
+  // Visible for testing
+  @VisibleForTesting
+  void setMaxRetries(long maxRetries) {
+    this.maxRetries = maxRetries;
+  }
+
+  // Visible for testing
+  @VisibleForTesting
+  void setStartWait(long startWait) {
+    this.currentWait = startWait;
+  }
+
+  // Visible for testing
+  @VisibleForTesting
+  void setWaitIncrement(long waitIncrement) {
+    this.waitIncrement = waitIncrement;
+  }
+
+  // Visible for testing
+  @VisibleForTesting
+  void setMaxWait(long maxWait) {
+    this.maxWait = maxWait;
+  }
+
+  public boolean hasInfiniteRetries() {
+    return maxRetries < 0;
+  }
+
+  public long getLogInterval() {
+    return NANOSECONDS.toMillis(logIntervalNanoSec);
+  }
+
+  public boolean canRetry() {
+    return hasInfiniteRetries() || (retriesDone < maxRetries);
+  }
+
+  public void useRetry() {
+    if (!canRetry()) {
+      throw new IllegalStateException("No retries left");
+    }
+
+    retriesDone++;
+  }
+
+  public boolean hasRetried() {
+    return retriesDone > 0;
+  }
+
+  public long retriesCompleted() {
+    return retriesDone;
+  }
+
+  public void waitForNextAttempt() throws InterruptedException {
+    log.debug("Sleeping for {}ms before retrying operation", currentWait);
+    sleep(currentWait);
+    currentWait = Math.min(maxWait, currentWait + waitIncrement);
+  }
+
+  protected void sleep(long wait) throws InterruptedException {
+    Thread.sleep(wait);
+  }
+
+  public void logRetry(Logger log, String message, Throwable t) {
+    // log the first time as debug, and then after every logInterval as a warning
+    long now = System.nanoTime();
+    if (lastRetryLog < 0) {
 
 Review comment:
   should this check `hasNeverLogged`?

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services