From 912ef4e39df345abc52f2581bf2008f7ed0e1129 Mon Sep 17 00:00:00 2001 From: Abhishek Jain Date: Thu, 27 Apr 2017 00:31:14 -0700 Subject: [PATCH 1/2] Added configuration to rest endpoint to specify failsafe based retry policy --- modules/export/pom.xml | 4 +++ .../wasabi/export/rest/RestEndPoint.java | 23 ++++++++++++++ .../export/rest/impl/DefaultRestEndPoint.java | 11 +++++++ .../DefaultRestEndPointConfiguration.java | 30 +++++++++++++++++++ pom.xml | 9 ++++++ 5 files changed, 77 insertions(+) diff --git a/modules/export/pom.xml b/modules/export/pom.xml index 5b83c9dcb..163f5901f 100644 --- a/modules/export/pom.xml +++ b/modules/export/pom.xml @@ -32,6 +32,10 @@ ${project.artifactId} + + net.jodah + failsafe + org.apache.httpcomponents httpclient diff --git a/modules/export/src/main/java/com/intuit/wasabi/export/rest/RestEndPoint.java b/modules/export/src/main/java/com/intuit/wasabi/export/rest/RestEndPoint.java index 181ef8848..e8935a4ef 100644 --- a/modules/export/src/main/java/com/intuit/wasabi/export/rest/RestEndPoint.java +++ b/modules/export/src/main/java/com/intuit/wasabi/export/rest/RestEndPoint.java @@ -15,6 +15,8 @@ *******************************************************************************/ package com.intuit.wasabi.export.rest; +import net.jodah.failsafe.RetryPolicy; + import java.net.URI; /** @@ -40,6 +42,12 @@ public interface RestEndPoint { */ int getRetries(); + /** + * Retry policy + * @return retry policy + */ + RetryPolicy getRetryPolicy(); + /** * Configuration parames for rest endpoint */ @@ -80,5 +88,20 @@ interface Configuration { * @return retries */ int getRetries(); + + /** + * Is exponential backoff enabled + */ + boolean isExponentialBackoffEnabled(); + + /** + * Get exponential backoff initial delay + */ + long getExponentialBackoffDelay(); + + /** + * Get exponential backoff max delay + */ + long getExponentialBackoffMaxDelay(); } } diff --git a/modules/export/src/main/java/com/intuit/wasabi/export/rest/impl/DefaultRestEndPoint.java b/modules/export/src/main/java/com/intuit/wasabi/export/rest/impl/DefaultRestEndPoint.java index 374a51658..9519503d9 100644 --- a/modules/export/src/main/java/com/intuit/wasabi/export/rest/impl/DefaultRestEndPoint.java +++ b/modules/export/src/main/java/com/intuit/wasabi/export/rest/impl/DefaultRestEndPoint.java @@ -17,11 +17,13 @@ import com.google.inject.Inject; import com.intuit.wasabi.export.rest.RestEndPoint; +import net.jodah.failsafe.RetryPolicy; import org.apache.http.client.utils.URIBuilder; import org.slf4j.Logger; import java.net.URI; import java.net.URISyntaxException; +import java.util.concurrent.TimeUnit; import static org.slf4j.LoggerFactory.getLogger; @@ -62,4 +64,13 @@ public Boolean useProxy() { public int getRetries() { return configuration.getRetries(); } + + @Override + public RetryPolicy getRetryPolicy() { + if (!configuration.isExponentialBackoffEnabled()) { + return new RetryPolicy().withMaxRetries(configuration.getRetries()); + } + return new RetryPolicy().withBackoff(configuration.getExponentialBackoffDelay(), + configuration.getExponentialBackoffMaxDelay(), TimeUnit.SECONDS); + } } diff --git a/modules/export/src/main/java/com/intuit/wasabi/export/rest/impl/DefaultRestEndPointConfiguration.java b/modules/export/src/main/java/com/intuit/wasabi/export/rest/impl/DefaultRestEndPointConfiguration.java index 9b88cb78a..528654f8d 100644 --- a/modules/export/src/main/java/com/intuit/wasabi/export/rest/impl/DefaultRestEndPointConfiguration.java +++ b/modules/export/src/main/java/com/intuit/wasabi/export/rest/impl/DefaultRestEndPointConfiguration.java @@ -93,4 +93,34 @@ public Boolean useProxy() { public int getRetries() { return Integer.parseInt((String) properties.get("export.rest.retries")); } + + /** + * Returns the export rest endpoint is exponential backoff enabled property. + * + * @return whether exponential backoff is enabled for retries + */ + @Override + public boolean isExponentialBackoffEnabled() { + return Boolean.parseBoolean(properties.getProperty("export.rest.isExponentialBackoffEnabled")); + } + + /** + * Returns the export rest endpoint exponential backoff initial delay + * + * @return initial interval size between retries with exponential backoff enabled + */ + @Override + public long getExponentialBackoffDelay() { + return Long.parseLong(properties.getProperty("export.rest.exponentialBackoffDelay")); + } + + /** + * Returns the export rest endpoint exponential backoff max delay + * + * @return the maximum interval size until which retries are going to continue with exponential backoff strategy + */ + @Override + public long getExponentialBackoffMaxDelay() { + return Long.parseLong(properties.getProperty("export.rest.exponentialBackoffMaxDelay")); + } } diff --git a/pom.xml b/pom.xml index d9cda54b2..e30a70764 100644 --- a/pom.xml +++ b/pom.xml @@ -240,6 +240,15 @@ http://www.w3.org/2001/XMLSchema-instance"> + + + + net.jodah + failsafe + 1.0.4 + + + commons-codec From b909e8f44b5508182795bd2cc30f38dadee71416 Mon Sep 17 00:00:00 2001 From: Abhishek Jain Date: Wed, 10 May 2017 17:17:00 -0700 Subject: [PATCH 2/2] Max retries still required even if exponential backoff is enabled --- .../wasabi/export/rest/impl/DefaultRestEndPoint.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/modules/export/src/main/java/com/intuit/wasabi/export/rest/impl/DefaultRestEndPoint.java b/modules/export/src/main/java/com/intuit/wasabi/export/rest/impl/DefaultRestEndPoint.java index 9519503d9..8332ee4bc 100644 --- a/modules/export/src/main/java/com/intuit/wasabi/export/rest/impl/DefaultRestEndPoint.java +++ b/modules/export/src/main/java/com/intuit/wasabi/export/rest/impl/DefaultRestEndPoint.java @@ -67,10 +67,11 @@ public int getRetries() { @Override public RetryPolicy getRetryPolicy() { - if (!configuration.isExponentialBackoffEnabled()) { - return new RetryPolicy().withMaxRetries(configuration.getRetries()); + RetryPolicy retryPolicy = new RetryPolicy().withMaxRetries(configuration.getRetries()); + if (configuration.isExponentialBackoffEnabled()) { + retryPolicy = retryPolicy.withBackoff(configuration.getExponentialBackoffDelay(), + configuration.getExponentialBackoffMaxDelay(), TimeUnit.SECONDS); } - return new RetryPolicy().withBackoff(configuration.getExponentialBackoffDelay(), - configuration.getExponentialBackoffMaxDelay(), TimeUnit.SECONDS); + return retryPolicy; } }