Browse Source

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
pull/2/head
gabry.wu 5 years ago committed by GitHub
parent
commit
772a8e2b37
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 30
      dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/RetryerUtils.java
  2. 109
      dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/RetryerUtilsTest.java

30
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 com.github.rholder.retry.*;
import org.apache.dolphinscheduler.common.Constants; import org.apache.dolphinscheduler.common.Constants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
@ -28,6 +30,7 @@ import java.util.concurrent.TimeUnit;
* The Retryer util. * The Retryer util.
*/ */
public class RetryerUtils { public class RetryerUtils {
private static final Logger logger = LoggerFactory.getLogger(RetryerUtils.class);
private static Retryer<Boolean> defaultRetryerResultCheck; private static Retryer<Boolean> defaultRetryerResultCheck;
private static Retryer<Boolean> defaultRetryerResultNoCheck; private static Retryer<Boolean> defaultRetryerResultNoCheck;
@ -105,4 +108,31 @@ public class RetryerUtils {
public static Boolean retryCall(final Callable<Boolean> callable) throws ExecutionException, RetryException { public static Boolean retryCall(final Callable<Boolean> callable) throws ExecutionException, RetryException {
return retryCall(callable, true); 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<Boolean> 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<Boolean> callable) {
return retryCallSilent(callable, true);
}
} }

109
dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/RetryerUtilsTest.java

@ -213,4 +213,113 @@ public class RetryerUtilsTest {
testRetryExceptionWithPara(true); testRetryExceptionWithPara(true);
testRetryExceptionWithPara(false); 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);
}
} }

Loading…
Cancel
Save