Skip to content
Snippets Groups Projects
Commit d6a2de67 authored by Neelesh Thakur's avatar Neelesh Thakur
Browse files

add retries for schema service and all other calls

parent 5a02fe9c
No related branches found
No related tags found
1 merge request!201add retries for schema service and all other service calls
Pipeline #66088 failed
...@@ -28,6 +28,7 @@ import org.springframework.boot.context.properties.ConfigurationProperties; ...@@ -28,6 +28,7 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.time.Duration; import java.time.Duration;
import java.util.function.Predicate;
/** /**
* This class handles retry configuration logic for calls made to <prefix>/storage/v2/query/records:batch * This class handles retry configuration logic for calls made to <prefix>/storage/v2/query/records:batch
...@@ -50,23 +51,25 @@ public class RetryPolicy { ...@@ -50,23 +51,25 @@ public class RetryPolicy {
/** /**
* @return RetryConfig with 3 attempts and 1 sec wait time * @return RetryConfig with 3 attempts and 1 sec wait time
*/ */
public RetryConfig retryConfig() { public RetryConfig retryConfig(Predicate<HttpResponse> predicate) {
return RetryConfig.<HttpResponse>custom() return RetryConfig.<HttpResponse>custom()
.maxAttempts(attempts) .maxAttempts(attempts)
.waitDuration(Duration.ofMillis(waitDuration)) .waitDuration(Duration.ofMillis(waitDuration))
.retryOnResult(response -> isRetryRequired(response)) .retryOnResult(predicate)
.build(); .build();
} }
/** /**
* Unfound records get listed under a JsonArray "notFound" in the http json response * Unfound records get listed under a JsonArray "notFound" in the http json response
*
* @param response * @param response
* @return if there are elements in "notFound" returns true, else false * @return if there are elements in "notFound" returns true, else false
*/ */
private boolean isRetryRequired(HttpResponse response) { public boolean batchRetryPolicy(HttpResponse response) {
if (response == null || response.getBody().isEmpty()) { if (response == null || response.getBody().isEmpty()) {
return false; return false;
} }
JsonObject jsonObject = new JsonParser().parse(response.getBody()).getAsJsonObject(); JsonObject jsonObject = new JsonParser().parse(response.getBody()).getAsJsonObject();
JsonElement notFoundElement = (JsonArray) jsonObject.get(RECORD_NOT_FOUND); JsonElement notFoundElement = (JsonArray) jsonObject.get(RECORD_NOT_FOUND);
if (notFoundElement == null || if (notFoundElement == null ||
...@@ -78,4 +81,30 @@ public class RetryPolicy { ...@@ -78,4 +81,30 @@ public class RetryPolicy {
log.info("Retry is set true"); log.info("Retry is set true");
return true; return true;
} }
public boolean schemaRetryPolicy(HttpResponse response) {
if (response == null || response.getBody().isEmpty()) {
return false;
}
if (response.getResponseCode() == 404) {
log.info("Retry is set true");
return true;
}
return false;
}
public boolean defaultRetryPolicy(HttpResponse response) {
if (response == null || response.getBody().isEmpty()) {
return false;
}
if (response.getResponseCode() > 501) {
log.info("Retry is set true");
return true;
}
return false;
}
} }
...@@ -41,6 +41,7 @@ import java.util.function.Supplier; ...@@ -41,6 +41,7 @@ import java.util.function.Supplier;
public class UrlFetchServiceAzureImpl implements IUrlFetchService { public class UrlFetchServiceAzureImpl implements IUrlFetchService {
public static final String STORAGE_QUERY_RECORD_FOR_CONVERSION_HOST_URL = "storage/v2/query/records:batch"; public static final String STORAGE_QUERY_RECORD_FOR_CONVERSION_HOST_URL = "storage/v2/query/records:batch";
public static final String SCHEMA_SERVICE_HOST_URL = "api/schema-service/v1/schema";
@Autowired @Autowired
private RetryPolicy policy; private RetryPolicy policy;
...@@ -50,6 +51,7 @@ public class UrlFetchServiceAzureImpl implements IUrlFetchService { ...@@ -50,6 +51,7 @@ public class UrlFetchServiceAzureImpl implements IUrlFetchService {
@Autowired @Autowired
private JaxRsDpsLog logger; private JaxRsDpsLog logger;
/** /**
* this method invokes retryFunction only for <prefix>/storage/v2/query/records:batch * this method invokes retryFunction only for <prefix>/storage/v2/query/records:batch
* calls otherwise invokes UrlFetchService.sendRequest(FetchServiceHttpRequest request) * calls otherwise invokes UrlFetchService.sendRequest(FetchServiceHttpRequest request)
...@@ -60,14 +62,7 @@ public class UrlFetchServiceAzureImpl implements IUrlFetchService { ...@@ -60,14 +62,7 @@ public class UrlFetchServiceAzureImpl implements IUrlFetchService {
*/ */
@Override @Override
public HttpResponse sendRequest(FetchServiceHttpRequest httpRequest) throws URISyntaxException { public HttpResponse sendRequest(FetchServiceHttpRequest httpRequest) throws URISyntaxException {
HttpResponse output; return this.retryFunction(httpRequest);
if (httpRequest.getUrl().contains(STORAGE_QUERY_RECORD_FOR_CONVERSION_HOST_URL)) {
output = this.retryFunction(httpRequest);
if (output != null) {
return output;
}
}
return this.urlFetchService.sendRequest(httpRequest);
} }
/** /**
...@@ -78,7 +73,15 @@ public class UrlFetchServiceAzureImpl implements IUrlFetchService { ...@@ -78,7 +73,15 @@ public class UrlFetchServiceAzureImpl implements IUrlFetchService {
* @return null if URISyntaxException is caught else returns HttpResponse * @return null if URISyntaxException is caught else returns HttpResponse
*/ */
private HttpResponse retryFunction(FetchServiceHttpRequest request) { private HttpResponse retryFunction(FetchServiceHttpRequest request) {
RetryConfig config = this.policy.retryConfig(); RetryConfig config;
if (request.getUrl().contains(STORAGE_QUERY_RECORD_FOR_CONVERSION_HOST_URL)) {
config = this.policy.retryConfig(response -> this.policy.batchRetryPolicy(response));
} else if (request.getUrl().contains(SCHEMA_SERVICE_HOST_URL)) {
config = this.policy.retryConfig(response -> this.policy.schemaRetryPolicy(response));
} else {
config = this.policy.retryConfig(response -> this.policy.defaultRetryPolicy(response));
}
RetryRegistry registry = RetryRegistry.of(config); RetryRegistry registry = RetryRegistry.of(config);
Retry retry = registry.retry("retryPolicy", config); Retry retry = registry.retry("retryPolicy", config);
......
...@@ -131,7 +131,7 @@ public class RetryPolicyTest { ...@@ -131,7 +131,7 @@ public class RetryPolicyTest {
@Test @Test
public void retry_should_be_true_for_jsonResponseWithNotFound() { public void retry_should_be_true_for_jsonResponseWithNotFound() {
RetryConfig config = this.retryPolicy.retryConfig(); RetryConfig config = this.retryPolicy.retryConfig(response -> this.retryPolicy.batchRetryPolicy(response));
Predicate<HttpResponse> retry = config.getResultPredicate(); Predicate<HttpResponse> retry = config.getResultPredicate();
response.setBody(JSON_RESPONSE_WITH_NOT_FOUND); response.setBody(JSON_RESPONSE_WITH_NOT_FOUND);
assert retry != null; assert retry != null;
...@@ -142,7 +142,7 @@ public class RetryPolicyTest { ...@@ -142,7 +142,7 @@ public class RetryPolicyTest {
@Test @Test
public void retry_should_be_false_for_jsonResponse1WithOut_NotFound() { public void retry_should_be_false_for_jsonResponse1WithOut_NotFound() {
RetryConfig config = this.retryPolicy.retryConfig(); RetryConfig config = this.retryPolicy.retryConfig(response -> this.retryPolicy.batchRetryPolicy(response));
Predicate<HttpResponse> retry = config.getResultPredicate(); Predicate<HttpResponse> retry = config.getResultPredicate();
response.setBody(JSON_RESPONSE1_WITHOUT_NOT_FOUND); response.setBody(JSON_RESPONSE1_WITHOUT_NOT_FOUND);
boolean value = retry.test(response); boolean value = retry.test(response);
...@@ -152,7 +152,7 @@ public class RetryPolicyTest { ...@@ -152,7 +152,7 @@ public class RetryPolicyTest {
@Test @Test
public void retry_should_be_false_for_jsonResponse2WithOut_NotFound() { public void retry_should_be_false_for_jsonResponse2WithOut_NotFound() {
RetryConfig config = this.retryPolicy.retryConfig(); RetryConfig config = this.retryPolicy.retryConfig(response -> this.retryPolicy.batchRetryPolicy(response));
Predicate<HttpResponse> retry = config.getResultPredicate(); Predicate<HttpResponse> retry = config.getResultPredicate();
response.setBody(JSON_RESPONSE2_WITHOUT_NOT_FOUND); response.setBody(JSON_RESPONSE2_WITHOUT_NOT_FOUND);
boolean value = retry.test(response); boolean value = retry.test(response);
......
...@@ -14,8 +14,6 @@ ...@@ -14,8 +14,6 @@
package org.opengroup.osdu.indexer.azure.service; package org.opengroup.osdu.indexer.azure.service;
import io.github.resilience4j.retry.RetryConfig;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.InjectMocks; import org.mockito.InjectMocks;
...@@ -26,12 +24,8 @@ import org.opengroup.osdu.core.common.http.UrlFetchServiceImpl; ...@@ -26,12 +24,8 @@ import org.opengroup.osdu.core.common.http.UrlFetchServiceImpl;
import org.opengroup.osdu.core.common.logging.JaxRsDpsLog; import org.opengroup.osdu.core.common.logging.JaxRsDpsLog;
import org.opengroup.osdu.core.common.model.http.HttpResponse; import org.opengroup.osdu.core.common.model.http.HttpResponse;
import java.time.Duration; import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.atMost;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class) @RunWith(MockitoJUnitRunner.class)
...@@ -95,45 +89,43 @@ public class UrlFetchServiceAzureImplTest { ...@@ -95,45 +89,43 @@ public class UrlFetchServiceAzureImplTest {
" \"conversionStatuses\":[]\n" + " \"conversionStatuses\":[]\n" +
"}"; "}";
private static final String url = "https://demo/api/storage/v2/query/records:batch"; private static final String BATCH_API_URL = "https://demo/api/storage/v2/query/records:batch";
private static final String url2 = "https://demo/api/storage/v2/schemas"; private static final String STORAGE_API_URL = "https://demo/api/storage/v2/schemas";
private static final String SCHEMA_API_URL = "https://demo/api/schema-service/v1/schema/osdu:file:gom:1.0.0";
@Before
public void setUp() {
when(this.retryPolicy.retryConfig()).thenReturn(new RetryPolicy().retryConfig());
}
@Test @Test
public void shouldRetry_ForJSON1_when_storageQueryRecordCallIsMade() throws Exception { public void shouldRetry_ForJSON1_when_storageQueryRecordCallIsMade() throws Exception {
response.setBody(JSON1); response.setBody(JSON1);
httpRequest.setUrl(url); httpRequest.setUrl(BATCH_API_URL);
when(this.retryPolicy.retryConfig(any())).thenReturn(new RetryPolicy().retryConfig(response -> this.retryPolicy.batchRetryPolicy(response)));
when(urlFetchServiceAzure.sendRequest(httpRequest)).thenReturn(response); when(urlFetchService.sendRequest(httpRequest)).thenReturn(response);
urlFetchServiceAzure.sendRequest(httpRequest); urlFetchServiceAzure.sendRequest(httpRequest);
verify(urlFetchService, atMost(4)).sendRequest(httpRequest); verify(urlFetchService, atMost(4)).sendRequest(httpRequest);
} }
@Test @Test
public void shouldNotRetry_ForJSON2_when_storageQueryRecordCallIsMade() throws Exception { public void shouldRetry_ForJSON1_when_schemaRecordCallIsMade() throws Exception {
response.setBody(JSON2); response.setBody(JSON1);
httpRequest.setUrl(url); httpRequest.setUrl(SCHEMA_API_URL);
when(this.retryPolicy.retryConfig(any())).thenReturn(new RetryPolicy().retryConfig(response -> this.retryPolicy.schemaRetryPolicy(response)));
when(urlFetchServiceAzure.sendRequest(httpRequest)).thenReturn(response); when(urlFetchService.sendRequest(httpRequest)).thenReturn(response);
urlFetchServiceAzure.sendRequest(httpRequest); urlFetchServiceAzure.sendRequest(httpRequest);
verify(urlFetchService, atMost(2)).sendRequest(httpRequest);
}
verify(urlFetchService, atMost(4)).sendRequest(httpRequest);
}
@Test @Test
public void retryFunction_shouldNotBeCalled() throws Exception { public void shouldRetry_when_anyOtherCallIsMade() throws Exception {
httpRequest.setUrl(url2); response.setBody(JSON2);
httpRequest.setUrl(STORAGE_API_URL);
when(this.retryPolicy.retryConfig(any())).thenReturn(new RetryPolicy().retryConfig(response -> this.retryPolicy.defaultRetryPolicy(response)));
when(urlFetchService.sendRequest(httpRequest)).thenReturn(response); when(urlFetchService.sendRequest(httpRequest)).thenReturn(response);
urlFetchServiceAzure.sendRequest(httpRequest); urlFetchServiceAzure.sendRequest(httpRequest);
verify(urlFetchService, times(1)).sendRequest(httpRequest);
}
verify(urlFetchService, atMost(4)).sendRequest(httpRequest);
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment