zhangyue19921010 commented on a change in pull request #3887:
URL: https://github.com/apache/hudi/pull/3887#discussion_r767386141



##########
File path: 
hudi-common/src/main/java/org/apache/hudi/common/util/RetryHelper.java
##########
@@ -0,0 +1,111 @@
+/*
+ * 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.hudi.common.util;
+
+import org.apache.hudi.common.fs.HoodieWrapperFileSystem;
+import org.apache.log4j.LogManager;
+import org.apache.log4j.Logger;
+
+import java.io.IOException;
+import java.util.Random;
+
+public class RetryHelper<T> {
+  private static final Logger LOG = LogManager.getLogger(RetryHelper.class);
+  private HoodieWrapperFileSystem.CheckedFunction<T> func;
+  private int num;
+  private long maxIntervalTime;
+  private long initialIntervalTime = 100L;
+  private String taskInfo = "N/A";
+
+  public RetryHelper() {
+  }
+
+  public RetryHelper(String taskInfo) {
+    this.taskInfo = taskInfo;
+  }
+
+  public RetryHelper tryWith(HoodieWrapperFileSystem.CheckedFunction<T> func) {
+    this.func = func;
+    return this;
+  }
+
+  public RetryHelper tryNum(int num) {
+    this.num = num;
+    return this;
+  }
+
+  public RetryHelper tryTaskInfo(String taskInfo) {
+    this.taskInfo = taskInfo;
+    return this;
+  }
+
+  public RetryHelper tryMaxInterval(long time) {
+    maxIntervalTime = time;
+    return this;
+  }
+
+  public RetryHelper tryInitialInterval(long time) {
+    initialIntervalTime = time;
+    return this;
+  }
+
+  public T start() throws IOException {
+    int retries = 0;
+    boolean success = false;
+    RuntimeException exception = null;
+    T t = null;
+    do {
+      long waitTime = Math.min(getWaitTimeExp(retries), maxIntervalTime);
+      try {
+        t = func.get();
+        success = true;
+        break;
+      } catch (RuntimeException e) {
+        // deal with RuntimeExceptions such like AmazonS3Exception 503
+        exception = e;
+        LOG.warn("Catch RuntimeException " + taskInfo + ", will retry after " 
+ waitTime + " ms.", e);
+        try {
+          Thread.sleep(waitTime);
+        } catch (InterruptedException ex) {
+            // ignore InterruptedException here
+        }
+        retries++;
+      }
+    } while (retries <= num);

Review comment:
       emmm, we only do `++` when caught exception, so maybe can't move it out 
of `catch() {}` block.




-- 
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: commits-unsubscr...@hudi.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to