From a8fffd8b4a562d08287595667140b676a673bf3b Mon Sep 17 00:00:00 2001 From: osandamaleesha Date: Thu, 19 Sep 2024 10:23:01 +0530 Subject: [PATCH] Add retry count server configurations and add test cases --- .../action/execution/util/APIClient.java | 2 +- .../execution/util/ActionExecutorConfig.java | 24 +++++++++++++++++++ .../action/execution/util/APIClientTest.java | 1 + .../util/ActionExecutorConfigTest.java | 19 +++++++++++++++ .../resources/identity.xml.j2 | 1 + ....identity.core.server.feature.default.json | 1 + 6 files changed, 47 insertions(+), 1 deletion(-) diff --git a/components/action-mgt/org.wso2.carbon.identity.action.execution/src/main/java/org/wso2/carbon/identity/action/execution/util/APIClient.java b/components/action-mgt/org.wso2.carbon.identity.action.execution/src/main/java/org/wso2/carbon/identity/action/execution/util/APIClient.java index a7ffd2156906..9d6e39263049 100644 --- a/components/action-mgt/org.wso2.carbon.identity.action.execution/src/main/java/org/wso2/carbon/identity/action/execution/util/APIClient.java +++ b/components/action-mgt/org.wso2.carbon.identity.action.execution/src/main/java/org/wso2/carbon/identity/action/execution/util/APIClient.java @@ -94,7 +94,7 @@ private void setRequestEntity(HttpPost httpPost, String jsonRequest, AuthMethods private ActionInvocationResponse executeRequest(HttpPost request) { int attempts = 0; - int retryCount = 2; // todo: read from server configurations + int retryCount = ActionExecutorConfig.getInstance().getHttpRequestRetryCount(); ActionInvocationResponse actionInvocationResponse = null; while (attempts < retryCount) { diff --git a/components/action-mgt/org.wso2.carbon.identity.action.execution/src/main/java/org/wso2/carbon/identity/action/execution/util/ActionExecutorConfig.java b/components/action-mgt/org.wso2.carbon.identity.action.execution/src/main/java/org/wso2/carbon/identity/action/execution/util/ActionExecutorConfig.java index ef1324e5bcf6..f8e4e7f41228 100644 --- a/components/action-mgt/org.wso2.carbon.identity.action.execution/src/main/java/org/wso2/carbon/identity/action/execution/util/ActionExecutorConfig.java +++ b/components/action-mgt/org.wso2.carbon.identity.action.execution/src/main/java/org/wso2/carbon/identity/action/execution/util/ActionExecutorConfig.java @@ -49,6 +49,8 @@ public class ActionExecutorConfig { "Actions.HTTPClient.HTTPConnectionRequestTimeout"; private static final String HTTP_CONNECTION_TIMEOUT_PROPERTY = "Actions.HTTPClient.HTTPConnectionTimeout"; private static final String HTTP_CONNECTION_POOL_SIZE_PROPERTY = "Actions.HTTPClient.HTTPConnectionPoolSize"; + private static final String HTTP_REQUEST_RETRY_COUNT_PROPERTY = "Actions.HTTPClient.HTTPRequestRetryCount"; + private static final int DEFAULT_HTTP_REQUEST_RETRY_COUNT = 2; private static final int DEFAULT_HTTP_CONNECTION_POOL_SIZE = 20; private static final int DEFAULT_HTTP_READ_TIMEOUT_IN_MILLIS = 5000; private static final int DEFAULT_HTTP_CONNECTION_REQUEST_TIMEOUT_IN_MILLIS = 2000; @@ -82,6 +84,28 @@ public boolean isExecutionForActionTypeEnabled(ActionType actionType) { } } + /** + * Returns the HTTP request retry count based on the system configuration. + * + * @return The HTTP request retry count, or the default if the property is missing or invalid. + */ + public int getHttpRequestRetryCount() { + + int retryCountPropertyValue = DEFAULT_HTTP_REQUEST_RETRY_COUNT; + String retryCountValue = (String) IdentityConfigParser.getInstance().getConfiguration(). + get(HTTP_REQUEST_RETRY_COUNT_PROPERTY); + if (StringUtils.isNotBlank(retryCountValue)) { + try { + retryCountPropertyValue = Integer.parseInt(retryCountValue); + } catch (NumberFormatException e) { + LOG.debug("Failed to read Http request retry count property in identity.xml." + + " Expects a number. Using the default value: " + + DEFAULT_HTTP_REQUEST_RETRY_COUNT, e); + } + } + return retryCountPropertyValue; + } + /** * Returns the HTTP connection pool size based on the system configuration. * diff --git a/components/action-mgt/org.wso2.carbon.identity.action.execution/src/test/java/org/wso2/carbon/identity/action/execution/util/APIClientTest.java b/components/action-mgt/org.wso2.carbon.identity.action.execution/src/test/java/org/wso2/carbon/identity/action/execution/util/APIClientTest.java index dbe1ab0423b5..eb2b0491ab06 100644 --- a/components/action-mgt/org.wso2.carbon.identity.action.execution/src/test/java/org/wso2/carbon/identity/action/execution/util/APIClientTest.java +++ b/components/action-mgt/org.wso2.carbon.identity.action.execution/src/test/java/org/wso2/carbon/identity/action/execution/util/APIClientTest.java @@ -82,6 +82,7 @@ public void setUp() throws Exception { ActionExecutorConfig actionExecutorConfig = mock(ActionExecutorConfig.class); actionExecutorConfigStatic.when(ActionExecutorConfig::getInstance).thenReturn(actionExecutorConfig); MockitoAnnotations.openMocks(this); + when(actionExecutorConfig.getHttpRequestRetryCount()).thenReturn(2); setField(apiClient, "httpClient", httpClient); } diff --git a/components/action-mgt/org.wso2.carbon.identity.action.execution/src/test/java/org/wso2/carbon/identity/action/execution/util/ActionExecutorConfigTest.java b/components/action-mgt/org.wso2.carbon.identity.action.execution/src/test/java/org/wso2/carbon/identity/action/execution/util/ActionExecutorConfigTest.java index 421835fbec05..4bfeb21ef8b2 100644 --- a/components/action-mgt/org.wso2.carbon.identity.action.execution/src/test/java/org/wso2/carbon/identity/action/execution/util/ActionExecutorConfigTest.java +++ b/components/action-mgt/org.wso2.carbon.identity.action.execution/src/test/java/org/wso2/carbon/identity/action/execution/util/ActionExecutorConfigTest.java @@ -359,4 +359,23 @@ public void testGetHttpConnectionPoolSizeForInvalidConfig() { when(mockIdentityConfigParser.getConfiguration()).thenReturn(configMap); Assert.assertEquals(20, actionExecutorConfig.getHttpConnectionPoolSize()); } + + @Test + public void testGetHttpRequestRetryCount() { + + Map configMap = new HashMap<>(); + configMap.put("Actions.HTTPClient.HTTPRequestRetryCount", "2"); + when(mockIdentityConfigParser.getConfiguration()).thenReturn(configMap); + Assert.assertEquals(2, actionExecutorConfig.getHttpRequestRetryCount()); + } + + @Test + public void testGetHttpRequestRetryCountForInvalidConfig() { + + //If the server configuration value is not a number, the default http request retry count value of 2 is parsed + Map configMap = new HashMap<>(); + configMap.put("Actions.HTTPClient.HTTPRequestRetryCount", "value"); + when(mockIdentityConfigParser.getConfiguration()).thenReturn(configMap); + Assert.assertEquals(2, actionExecutorConfig.getHttpRequestRetryCount()); + } } diff --git a/features/identity-core/org.wso2.carbon.identity.core.server.feature/resources/identity.xml.j2 b/features/identity-core/org.wso2.carbon.identity.core.server.feature/resources/identity.xml.j2 index cb471740c3ea..f044b6c75daf 100644 --- a/features/identity-core/org.wso2.carbon.identity.core.server.feature/resources/identity.xml.j2 +++ b/features/identity-core/org.wso2.carbon.identity.core.server.feature/resources/identity.xml.j2 @@ -2025,6 +2025,7 @@ {{actions.http_client.read_timeout}} {{actions.http_client.request_timeout}} {{actions.http_client.connection_pool_size}} + {{actions.http_client.retry_count}} {{actions.maximum_actions_per_action_type}} diff --git a/features/identity-core/org.wso2.carbon.identity.core.server.feature/resources/org.wso2.carbon.identity.core.server.feature.default.json b/features/identity-core/org.wso2.carbon.identity.core.server.feature/resources/org.wso2.carbon.identity.core.server.feature.default.json index 582a1d5b1395..6dfbe4771366 100644 --- a/features/identity-core/org.wso2.carbon.identity.core.server.feature/resources/org.wso2.carbon.identity.core.server.feature.default.json +++ b/features/identity-core/org.wso2.carbon.identity.core.server.feature/resources/org.wso2.carbon.identity.core.server.feature.default.json @@ -1575,6 +1575,7 @@ "actions.http_client.read_timeout": 5000, "actions.http_client.request_timeout": 2000, "actions.http_client.connection_pool_size": 20, + "actions.http_client.retry_count": 2, "actions.maximum_actions_per_action_type": 1, "actions.action_request.excluded_headers": [ "authorization",