You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hudi.apache.org by GitBox <gi...@apache.org> on 2022/02/08 03:33:42 UTC

[GitHub] [hudi] danny0405 commented on a change in pull request #3887: [HUDI-2648] Retry FileSystem action instead of failed directly.

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



##########
File path: hudi-common/src/main/java/org/apache/hudi/common/util/RetryHelper.java
##########
@@ -0,0 +1,109 @@
+/*
+ * 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.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 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(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;
+    T functionResult = null;
+
+    while (true) {
+      long waitTime = Math.min(getWaitTimeExp(retries), maxIntervalTime);
+      try {
+        functionResult = func.get();
+        break;
+      } catch (IOException | RuntimeException e) {
+        if (retries++ >= num) {

Review comment:
       Generally i think catch any `IOException` or `RuntimeException` is overprotection and irrational.




-- 
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-unsubscribe@hudi.apache.org

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