Added RetryingFunction for simplified retry support.
Moved retry logic out of primitives (AtomicCounter) to the caller site.
Change-Id: I319d61f153f98d421baf32a1b5cd69d20dc63427
diff --git a/utils/misc/src/main/java/org/onlab/util/RetryingFunction.java b/utils/misc/src/main/java/org/onlab/util/RetryingFunction.java
new file mode 100644
index 0000000..484e236
--- /dev/null
+++ b/utils/misc/src/main/java/org/onlab/util/RetryingFunction.java
@@ -0,0 +1,60 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * Licensed 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.onlab.util;
+
+import java.util.function.Function;
+
+import com.google.common.base.Throwables;
+
+/**
+ * Function that retries execution on failure.
+ *
+ * @param <U> input type
+ * @param <V> output type
+ */
+public class RetryingFunction<U, V> implements Function<U, V> {
+
+ private final Function<U, V> baseFunction;
+ private final Class<? extends Throwable> exceptionClass;
+ private final int maxRetries;
+ private final int maxDelayBetweenRetries;
+
+ public RetryingFunction(Function<U, V> baseFunction,
+ Class<? extends Throwable> exceptionClass,
+ int maxRetries,
+ int maxDelayBetweenRetries) {
+ this.baseFunction = baseFunction;
+ this.exceptionClass = exceptionClass;
+ this.maxRetries = maxRetries;
+ this.maxDelayBetweenRetries = maxDelayBetweenRetries;
+ }
+
+ @Override
+ public V apply(U input) {
+ int retryAttempts = 0;
+ while (true) {
+ try {
+ return baseFunction.apply(input);
+ } catch (Throwable t) {
+ if (!exceptionClass.isAssignableFrom(t.getClass()) || retryAttempts == maxRetries) {
+ Throwables.propagate(t);
+ }
+ Tools.randomDelay(maxDelayBetweenRetries);
+ retryAttempts++;
+ }
+ }
+ }
+}
diff --git a/utils/misc/src/main/java/org/onlab/util/Tools.java b/utils/misc/src/main/java/org/onlab/util/Tools.java
index 301f411..b12d15c 100644
--- a/utils/misc/src/main/java/org/onlab/util/Tools.java
+++ b/utils/misc/src/main/java/org/onlab/util/Tools.java
@@ -46,6 +46,8 @@
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
+import java.util.function.Function;
+import java.util.function.Supplier;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
@@ -227,6 +229,41 @@
}
/**
+ * Returns a function that retries execution on failure.
+ * @param base base function
+ * @param exceptionClass type of exception for which to retry
+ * @param maxRetries max number of retries before giving up
+ * @param maxDelayBetweenRetries max delay between successive retries. The actual delay is randomly picked from
+ * the interval (0, maxDelayBetweenRetries]
+ * @return function
+ */
+ public static <U, V> Function<U, V> retryable(Function<U, V> base,
+ Class<? extends Throwable> exceptionClass,
+ int maxRetries,
+ int maxDelayBetweenRetries) {
+ return new RetryingFunction<>(base, exceptionClass, maxRetries, maxDelayBetweenRetries);
+ }
+
+ /**
+ * Returns a Supplier that retries execution on failure.
+ * @param base base supplier
+ * @param exceptionClass type of exception for which to retry
+ * @param maxRetries max number of retries before giving up
+ * @param maxDelayBetweenRetries max delay between successive retries. The actual delay is randomly picked from
+ * the interval (0, maxDelayBetweenRetries]
+ * @return supplier
+ */
+ public static <V> Supplier<V> retryable(Supplier<V> base,
+ Class<? extends Throwable> exceptionClass,
+ int maxRetries,
+ int maxDelayBetweenRetries) {
+ return () -> new RetryingFunction<>(v -> base.get(),
+ exceptionClass,
+ maxRetries,
+ maxDelayBetweenRetries).apply(null);
+ }
+
+ /**
* Suspends the current thread for a random number of millis between 0 and
* the indicated limit.
*
diff --git a/utils/misc/src/test/java/org/onlab/util/RetryingFunctionTest.java b/utils/misc/src/test/java/org/onlab/util/RetryingFunctionTest.java
new file mode 100644
index 0000000..4b08d2f
--- /dev/null
+++ b/utils/misc/src/test/java/org/onlab/util/RetryingFunctionTest.java
@@ -0,0 +1,94 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * Licensed 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.onlab.util;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * Unit tests for RetryingFunction.
+ *
+ */
+public class RetryingFunctionTest {
+
+ private int round;
+
+ @Before
+ public void setUp() {
+ round = 1;
+ }
+
+ @After
+ public void tearDown() {
+ round = 0;
+ }
+
+ @Test(expected = RetryableException.class)
+ public void testNoRetries() {
+ new RetryingFunction<>(this::succeedAfterOneFailure, RetryableException.class, 0, 10).apply(null);
+ }
+
+ @Test
+ public void testSuccessAfterOneRetry() {
+ new RetryingFunction<>(this::succeedAfterOneFailure, RetryableException.class, 1, 10).apply(null);
+ }
+
+ @Test(expected = RetryableException.class)
+ public void testFailureAfterOneRetry() {
+ new RetryingFunction<>(this::succeedAfterTwoFailures, RetryableException.class, 1, 10).apply(null);
+ }
+
+ @Test
+ public void testFailureAfterTwoRetries() {
+ new RetryingFunction<>(this::succeedAfterTwoFailures, RetryableException.class, 2, 10).apply(null);
+ }
+
+ @Test(expected = NonRetryableException.class)
+ public void testFailureWithNonRetryableFailure() {
+ new RetryingFunction<>(this::failCompletely, RetryableException.class, 2, 10).apply(null);
+ }
+
+ private String succeedAfterOneFailure(String input) {
+ if (round++ <= 1) {
+ throw new RetryableException();
+ } else {
+ return "pass";
+ }
+ }
+
+ private String succeedAfterTwoFailures(String input) {
+ if (round++ <= 2) {
+ throw new RetryableException();
+ } else {
+ return "pass";
+ }
+ }
+
+ private String failCompletely(String input) {
+ if (round++ <= 1) {
+ throw new NonRetryableException();
+ } else {
+ return "pass";
+ }
+ }
+
+ private class RetryableException extends RuntimeException {
+ }
+
+ private class NonRetryableException extends RuntimeException {
+ }
+}