From 772a8e2b3750ea7a0a615fb4a0a775fbd5af8fb4 Mon Sep 17 00:00:00 2001 From: "gabry.wu" Date: Tue, 19 May 2020 10:18:21 +0800 Subject: [PATCH] add retryCallSilent to RetryerUtils making it easy to use (#2729) * add retryCallSilent to RetryerUtils making it easy to use * add more detail to java doc of retryCallSilent --- .../common/utils/RetryerUtils.java | 30 +++++ .../common/utils/RetryerUtilsTest.java | 109 ++++++++++++++++++ 2 files changed, 139 insertions(+) diff --git a/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/RetryerUtils.java b/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/RetryerUtils.java index e6f4c502de..a3a935831f 100644 --- a/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/RetryerUtils.java +++ b/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/RetryerUtils.java @@ -19,6 +19,8 @@ package org.apache.dolphinscheduler.common.utils; import com.github.rholder.retry.*; import org.apache.dolphinscheduler.common.Constants; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; @@ -28,6 +30,7 @@ import java.util.concurrent.TimeUnit; * The Retryer util. */ public class RetryerUtils { + private static final Logger logger = LoggerFactory.getLogger(RetryerUtils.class); private static Retryer defaultRetryerResultCheck; private static Retryer defaultRetryerResultNoCheck; @@ -105,4 +108,31 @@ public class RetryerUtils { public static Boolean retryCall(final Callable callable) throws ExecutionException, RetryException { return retryCall(callable, true); } + + /** + * Retry call silent without exceptions thrown + * + * @param callable the callable + * @param checkResult whether check result + * @return if no exceptions ,it's result returned by callable ,else always false + */ + public static boolean retryCallSilent(final Callable callable, boolean checkResult) { + boolean result = false; + try { + result = getDefaultRetryer(checkResult).call(callable); + } catch (ExecutionException | RetryException e) { + logger.warn("Retry call {} failed {}", callable, e.getMessage(), e); + } + return result; + } + + /** + * Retry call silent without exceptions thrown + * + * @param callable the callable + * @return if no exceptions ,it's result returned by callable ,else always false + */ + public static boolean retryCallSilent(final Callable callable) { + return retryCallSilent(callable, true); + } } diff --git a/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/RetryerUtilsTest.java b/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/RetryerUtilsTest.java index 7841e46585..19b7853de3 100644 --- a/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/RetryerUtilsTest.java +++ b/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/RetryerUtilsTest.java @@ -213,4 +213,113 @@ public class RetryerUtilsTest { testRetryExceptionWithPara(true); testRetryExceptionWithPara(false); } + + @Test + public void testRetrySilent() { + try { + for (int execTarget = 1; execTarget <= 3; execTarget++) { + int finalExecTarget = execTarget; + int[] execTime = {0}; + boolean result = RetryerUtils.retryCallSilent(() -> { + execTime[0]++; + return execTime[0] == finalExecTarget; + }); + Assert.assertEquals(finalExecTarget, execTime[0]); + Assert.assertTrue(result); + } + } catch (Exception e) { + Assert.fail("Unexpected exception " + e.getMessage()); + } + int[] execTime = {0}; + try { + boolean result = RetryerUtils.retryCallSilent(() -> { + execTime[0]++; + return execTime[0] == 4; + }); + Assert.assertFalse(result); + } catch (Exception e) { + Assert.fail("Unexpected exception " + e.getMessage()); + } + } + + @Test + public void testRetrySilentWithPara() { + try { + for (int execTarget = 1; execTarget <= 3; execTarget++) { + int finalExecTarget = execTarget; + int[] execTime = {0}; + boolean result = RetryerUtils.retryCallSilent(() -> { + execTime[0]++; + return execTime[0] == finalExecTarget; + }, true); + Assert.assertEquals(finalExecTarget, execTime[0]); + Assert.assertTrue(result); + } + } catch (Exception e) { + Assert.fail("Unexpected exception " + e.getMessage()); + } + int[] execTime = {0}; + try { + boolean result = RetryerUtils.retryCallSilent(() -> { + execTime[0]++; + return execTime[0] == 4; + }, true); + Assert.assertFalse(result); + } catch (Exception e) { + Assert.fail("Unexpected exception " + e.getMessage()); + } + } + @Test + public void testRetrySilentNoCheckResult(){ + try { + for (int execTarget = 1; execTarget <= 5; execTarget++) { + int[] execTime = {0}; + boolean result = RetryerUtils.retryCallSilent(() -> { + execTime[0]++; + return execTime[0] > 1; + }, false); + Assert.assertEquals(1, execTime[0]); + Assert.assertFalse(result); + } + } catch (Exception e) { + Assert.fail("Unexpected exception " + e.getMessage()); + } + } + private void testRetrySilentExceptionWithPara(boolean checkResult) { + try { + for (int execTarget = 1; execTarget <= 3; execTarget++) { + int finalExecTarget = execTarget; + int[] execTime = {0}; + boolean result = RetryerUtils.retryCallSilent(() -> { + execTime[0]++; + if (execTime[0] != finalExecTarget) { + throw new IllegalArgumentException(String.valueOf(execTime[0])); + } + return true; + }, checkResult); + Assert.assertEquals(finalExecTarget, execTime[0]); + Assert.assertTrue(result); + } + } catch (Exception e) { + Assert.fail("Unexpected exception " + e.getMessage()); + } + int[] execTime = {0}; + try { + boolean result = RetryerUtils.retryCallSilent(() -> { + execTime[0]++; + if (execTime[0] != 4) { + throw new IllegalArgumentException(String.valueOf(execTime[0])); + } + return true; + }, checkResult); + Assert.assertFalse(result); + } catch (Exception e) { + Assert.fail("Unexpected exception " + e.getMessage()); + } + } + @Test + public void testRetrySilentException() { + testRetrySilentExceptionWithPara(true); + testRetrySilentExceptionWithPara(false); + } }