From 19c7a1982e95660d2f5921f70315e7dcc41292b3 Mon Sep 17 00:00:00 2001 From: HantingZhang2 Date: Thu, 3 Aug 2023 19:25:32 +0200 Subject: [PATCH 01/43] Adding retry config --- .../com/datadog/api/client/ApiClient.java | 14 +++++ .../com/datadog/api/client/RetryConfig.java | 55 +++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 src/main/java/com/datadog/api/client/RetryConfig.java diff --git a/src/main/java/com/datadog/api/client/ApiClient.java b/src/main/java/com/datadog/api/client/ApiClient.java index c0de105d8d4..6ffc6c35568 100644 --- a/src/main/java/com/datadog/api/client/ApiClient.java +++ b/src/main/java/com/datadog/api/client/ApiClient.java @@ -350,6 +350,7 @@ public class ApiClient { protected Map> operationServerVariables = new HashMap>(); protected boolean debugging = false; + protected RetryConfig retry ; protected boolean compress = true; protected ClientConfig clientConfig; protected int connectionTimeout = 0; @@ -445,6 +446,10 @@ public static ApiClient getDefaultApiClient() { } defaultApiClient.configureApiKeys(secrets); + //Configure default retry behavior + RetryConfig retry = new RetryConfig(false,2,2,3); + defaultApiClient.setRetry(retry); + return defaultApiClient; } @@ -517,6 +522,15 @@ public DateTimeFormatter getOffsetDateTimeFormatter() { return offsetDateTimeFormatter; } + /** + * Add custom retry object in the client + * @param retry retry object + * */ + public void setRetry(RetryConfig retry) { + this.retry = retry; + } + + /** * Set the date format used to parse/format {@code OffsetDateTime} parameters. * diff --git a/src/main/java/com/datadog/api/client/RetryConfig.java b/src/main/java/com/datadog/api/client/RetryConfig.java new file mode 100644 index 00000000000..1a1a2778ea5 --- /dev/null +++ b/src/main/java/com/datadog/api/client/RetryConfig.java @@ -0,0 +1,55 @@ +package com.datadog.api.client; + +public class RetryConfig { + public boolean enableRetry; + public int backOffMultiplier; + public int backOffBase; + public int maxRetries; + + + /** + * @param enableRetry Enable retry when rate limited + * @param backOffMultiplier Multiplier for retry backoff + * @param backOffBase Base for retry backoff + * @param maxRetries Maximum number of retries + */ + + public RetryConfig(boolean enableRetry, int backOffMultiplier, int backOffBase, int maxRetries) { + this.enableRetry = enableRetry; + this.backOffMultiplier = backOffMultiplier; + this.backOffBase = backOffBase; + this.maxRetries = maxRetries; + } + + public boolean isEnableRetry() { + return enableRetry; + } + + public int getBackOffMultiplier() { + return backOffMultiplier; + } + + public int getBackOffBase() { + return backOffBase; + } + + public int getMaxRetries() { + return maxRetries; + } + + public void setEnableRetry(boolean enableRetry) { + this.enableRetry = enableRetry; + } + + public void setBackOffMultiplier(int backOffMultiplier) { + this.backOffMultiplier = backOffMultiplier; + } + + public void setBackOffBase(int backOffBase) { + this.backOffBase = backOffBase; + } + public void setMaxRetries(int maxRetries) { + this.maxRetries = maxRetries; + } + +} From bfcdd6181e06a88dd1f2120ba15a431057b38f06 Mon Sep 17 00:00:00 2001 From: HantingZhang2 Date: Fri, 4 Aug 2023 15:15:39 +0200 Subject: [PATCH 02/43] Implement retry logic --- .../com/datadog/api/client/ApiClient.java | 78 +++++++++++++------ 1 file changed, 54 insertions(+), 24 deletions(-) diff --git a/src/main/java/com/datadog/api/client/ApiClient.java b/src/main/java/com/datadog/api/client/ApiClient.java index 6ffc6c35568..251e0ca753a 100644 --- a/src/main/java/com/datadog/api/client/ApiClient.java +++ b/src/main/java/com/datadog/api/client/ApiClient.java @@ -1539,34 +1539,43 @@ public ApiResponse invokeAPI( Response response = null; try { - response = sendRequest(method, invocationBuilder, entity); - - int statusCode = response.getStatusInfo().getStatusCode(); - Map> responseHeaders = buildResponseHeaders(response); - - if (response.getStatusInfo() == Status.NO_CONTENT) { - return new ApiResponse(statusCode, responseHeaders); - } else if (response.getStatusInfo().getFamily() == Status.Family.SUCCESSFUL) { - if (returnType == null) { + int currentRetry = 0; + while (true){ + response = sendRequest(method, invocationBuilder, entity); + int statusCode = response.getStatusInfo().getStatusCode(); + Map> responseHeaders = buildResponseHeaders(response); + if (response.getStatusInfo() == Status.NO_CONTENT) { return new ApiResponse(statusCode, responseHeaders); + } else if (response.getStatusInfo().getFamily() == Status.Family.SUCCESSFUL) { + if (returnType == null) { + return new ApiResponse(statusCode, responseHeaders); + } else { + return new ApiResponse(statusCode, responseHeaders, deserialize(response, returnType)); + } + } else if (shouldRetry(currentRetry, statusCode, retry)){ + try{ + Thread.sleep(calculateRetryIntrval(responseHeaders, retry, statusCode)); + } catch ( InterruptedException e){ + Thread.currentThread().interrupt(); + e.printStackTrace(); + } + currentRetry++; } else { - return new ApiResponse(statusCode, responseHeaders, deserialize(response, returnType)); - } - } else { - String message = "error"; - String respBody = null; - if (response.hasEntity()) { - try { - respBody = String.valueOf(response.readEntity(String.class)); - message = respBody; - } catch (RuntimeException e) { - // e.printStackTrace(); + String message = "error"; + String respBody = null; + if (response.hasEntity()) { + try { + respBody = String.valueOf(response.readEntity(String.class)); + message = respBody; + } catch (RuntimeException e) { + // e.printStackTrace(); + } } + throw new ApiException( + response.getStatus(), message, buildResponseHeaders(response), respBody); } - throw new ApiException( - response.getStatus(), message, buildResponseHeaders(response), respBody); - } - } finally { + } + } finally { try { response.close(); } catch (Exception e) { @@ -1576,6 +1585,27 @@ public ApiResponse invokeAPI( } } + private boolean shouldRetry(int retryCount, int statusCode, RetryConfig retryConfig){ + boolean rightStatus = false; + if (statusCode == 413 || statusCode == 429 || statusCode >= 500){ + rightStatus = true; + } + return (retryConfig.maxRetries>=retryCount && rightStatus && retryConfig.isEnableRetry()); + } + + private int calculateRetryIntrval(Map> responseHeaders, RetryConfig retryConfig, int retryCount){ + List ratelimitHeader = responseHeaders.get("X-Ratelimit-Reset"); + if (ratelimitHeader != null){ + return Integer.parseInt(ratelimitHeader.get(0)); + } else { + int retryInterval= (int) Math.pow (retry.backOffMultiplier, retryCount)* retryConfig.backOffBase; + if (getConnectTimeout()!=0){ + retryInterval = Math.min(retryInterval, getConnectTimeout()); + } + return retryInterval; + } + } + private Response sendRequest( String method, Invocation.Builder invocationBuilder, Entity entity) { Response response; From f5a806922e319eb1d9b2b4f165b18551c945ae32 Mon Sep 17 00:00:00 2001 From: HantingZhang2 Date: Fri, 4 Aug 2023 15:25:15 +0200 Subject: [PATCH 03/43] correct millis --- src/main/java/com/datadog/api/client/ApiClient.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/datadog/api/client/ApiClient.java b/src/main/java/com/datadog/api/client/ApiClient.java index 251e0ca753a..0faa1970477 100644 --- a/src/main/java/com/datadog/api/client/ApiClient.java +++ b/src/main/java/com/datadog/api/client/ApiClient.java @@ -1554,7 +1554,7 @@ public ApiResponse invokeAPI( } } else if (shouldRetry(currentRetry, statusCode, retry)){ try{ - Thread.sleep(calculateRetryIntrval(responseHeaders, retry, statusCode)); + Thread.sleep(calculateRetryIntrval(responseHeaders, retry, statusCode)*1000); } catch ( InterruptedException e){ Thread.currentThread().interrupt(); e.printStackTrace(); From d65da683b7bed046c831ad4ef0550709378e091a Mon Sep 17 00:00:00 2001 From: HantingZhang2 Date: Fri, 4 Aug 2023 16:42:13 +0200 Subject: [PATCH 04/43] add default to retry config --- src/main/java/com/datadog/api/client/ApiClient.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/datadog/api/client/ApiClient.java b/src/main/java/com/datadog/api/client/ApiClient.java index 0faa1970477..dd0f6fb7938 100644 --- a/src/main/java/com/datadog/api/client/ApiClient.java +++ b/src/main/java/com/datadog/api/client/ApiClient.java @@ -350,7 +350,7 @@ public class ApiClient { protected Map> operationServerVariables = new HashMap>(); protected boolean debugging = false; - protected RetryConfig retry ; + protected RetryConfig retry = new RetryConfig(false, 2, 2, 3) ; protected boolean compress = true; protected ClientConfig clientConfig; protected int connectionTimeout = 0; @@ -1586,11 +1586,11 @@ public ApiResponse invokeAPI( } private boolean shouldRetry(int retryCount, int statusCode, RetryConfig retryConfig){ - boolean rightStatus = false; + boolean statusToRetry = false; if (statusCode == 413 || statusCode == 429 || statusCode >= 500){ - rightStatus = true; + statusToRetry = true; } - return (retryConfig.maxRetries>=retryCount && rightStatus && retryConfig.isEnableRetry()); + return (retryConfig.maxRetries>=retryCount && statusToRetry && retryConfig.isEnableRetry()); } private int calculateRetryIntrval(Map> responseHeaders, RetryConfig retryConfig, int retryCount){ From fb23a96a5868efc70a22bf5db8a06635c2b12c7b Mon Sep 17 00:00:00 2001 From: HantingZhang2 Date: Fri, 4 Aug 2023 18:01:14 +0200 Subject: [PATCH 05/43] change retry interval calculation --- src/main/java/com/datadog/api/client/ApiClient.java | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/datadog/api/client/ApiClient.java b/src/main/java/com/datadog/api/client/ApiClient.java index dd0f6fb7938..56397bdec67 100644 --- a/src/main/java/com/datadog/api/client/ApiClient.java +++ b/src/main/java/com/datadog/api/client/ApiClient.java @@ -446,10 +446,6 @@ public static ApiClient getDefaultApiClient() { } defaultApiClient.configureApiKeys(secrets); - //Configure default retry behavior - RetryConfig retry = new RetryConfig(false,2,2,3); - defaultApiClient.setRetry(retry); - return defaultApiClient; } @@ -1594,9 +1590,9 @@ private boolean shouldRetry(int retryCount, int statusCode, RetryConfig retryCon } private int calculateRetryIntrval(Map> responseHeaders, RetryConfig retryConfig, int retryCount){ - List ratelimitHeader = responseHeaders.get("X-Ratelimit-Reset"); - if (ratelimitHeader != null){ - return Integer.parseInt(ratelimitHeader.get(0)); + if ( responseHeaders.get("x-ratelimit-reset")!=null){ + List rateLimitHeader = responseHeaders.get("x-ratelimit-reset"); + return Integer.parseInt(rateLimitHeader.get(0)); } else { int retryInterval= (int) Math.pow (retry.backOffMultiplier, retryCount)* retryConfig.backOffBase; if (getConnectTimeout()!=0){ From b331730358ff013670f332bf5d61c07370818340 Mon Sep 17 00:00:00 2001 From: HantingZhang2 Date: Mon, 7 Aug 2023 17:11:41 +0200 Subject: [PATCH 06/43] first test iteration --- .../datadog/api/client/v2/api/RetryTest.java | 47 +++++++++++++++++++ .../datadog/api/client/v2/api/V2APITest.java | 9 ++++ 2 files changed, 56 insertions(+) create mode 100644 src/test/java/com/datadog/api/client/v2/api/RetryTest.java diff --git a/src/test/java/com/datadog/api/client/v2/api/RetryTest.java b/src/test/java/com/datadog/api/client/v2/api/RetryTest.java new file mode 100644 index 00000000000..652002a3724 --- /dev/null +++ b/src/test/java/com/datadog/api/client/v2/api/RetryTest.java @@ -0,0 +1,47 @@ +package com.datadog.api.client.v2.api; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import java.security.NoSuchAlgorithmException; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.datadog.api.client.ApiException; +import com.datadog.api.client.v1.model.DashboardList; +import com.datadog.api.client.v2.model.DashboardListItems; + +public class RetryTest extends V2APITest{ + + private static long dashboardListID; + private static com.datadog.api.client.v1.api.DashboardListsApi dashboardListsApiV1; + private final DashboardListsApi api = new DashboardListsApi(generalApiClientWithRetry); + + @Override + public String getTracingEndpoint() { + return "dashboard-lists"; + } + + @After + public void deleteDashboardList() throws ApiException { + dashboardListsApiV1.deleteDashboardList(dashboardListID); + } + + @Before + public void createDashboardList() throws ApiException, NoSuchAlgorithmException { + dashboardListsApiV1 = new com.datadog.api.client.v1.api.DashboardListsApi(generalApiClient); + DashboardList res = + dashboardListsApiV1.createDashboardList(new DashboardList().name(getUniqueEntityName())); + dashboardListID = res.getId(); + } + + @Test + public void retryWithDashboardListItemGetTest() throws ApiException{ + DashboardListItems getResponse = api.getDashboardListItems(dashboardListID); + assertNotNull(getResponse.getTotal()); + assertEquals(0, (long) getResponse.getTotal()); + assertEquals(0, getResponse.getDashboards().size()); + } +} diff --git a/src/test/java/com/datadog/api/client/v2/api/V2APITest.java b/src/test/java/com/datadog/api/client/v2/api/V2APITest.java index f35a283562c..efa99fe6468 100644 --- a/src/test/java/com/datadog/api/client/v2/api/V2APITest.java +++ b/src/test/java/com/datadog/api/client/v2/api/V2APITest.java @@ -13,6 +13,7 @@ import com.datadog.api.client.ApiException; import com.datadog.api.client.ApiResponse; import com.datadog.api.client.Pair; +import com.datadog.api.client.RetryConfig; import jakarta.ws.rs.client.Client; import jakarta.ws.rs.client.Invocation; import jakarta.ws.rs.core.GenericType; @@ -30,6 +31,14 @@ public abstract class V2APITest extends TestUtils.APITest { protected static ApiClient generalApiClient; protected static ApiClient generalApiUnitTestClient; + protected static ApiClient generalApiClientWithRetry; + + @BeforeClass + public static void initGeneralApiClientWithRetry(){ + initGeneralApiClient(); + generalApiClientWithRetry = generalApiClient; + generalApiClientWithRetry.setRetry(new RetryConfig(true,2,2,3)); + } @BeforeClass public static void initGeneralApiClient() { From f73f60937f6492d3742ce252f82358af6945edcd Mon Sep 17 00:00:00 2001 From: "ci.datadog-api-spec" Date: Mon, 7 Aug 2023 15:12:40 +0000 Subject: [PATCH 07/43] pre-commit fixes --- .../datadog/api/client/v2/api/RetryTest.java | 40 +++++++++---------- .../datadog/api/client/v2/api/V2APITest.java | 4 +- 2 files changed, 21 insertions(+), 23 deletions(-) diff --git a/src/test/java/com/datadog/api/client/v2/api/RetryTest.java b/src/test/java/com/datadog/api/client/v2/api/RetryTest.java index 652002a3724..6a01db7bac2 100644 --- a/src/test/java/com/datadog/api/client/v2/api/RetryTest.java +++ b/src/test/java/com/datadog/api/client/v2/api/RetryTest.java @@ -3,28 +3,26 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; +import com.datadog.api.client.ApiException; +import com.datadog.api.client.v1.model.DashboardList; +import com.datadog.api.client.v2.model.DashboardListItems; import java.security.NoSuchAlgorithmException; - import org.junit.After; import org.junit.Before; import org.junit.Test; -import com.datadog.api.client.ApiException; -import com.datadog.api.client.v1.model.DashboardList; -import com.datadog.api.client.v2.model.DashboardListItems; +public class RetryTest extends V2APITest { -public class RetryTest extends V2APITest{ + private static long dashboardListID; + private static com.datadog.api.client.v1.api.DashboardListsApi dashboardListsApiV1; + private final DashboardListsApi api = new DashboardListsApi(generalApiClientWithRetry); - private static long dashboardListID; - private static com.datadog.api.client.v1.api.DashboardListsApi dashboardListsApiV1; - private final DashboardListsApi api = new DashboardListsApi(generalApiClientWithRetry); + @Override + public String getTracingEndpoint() { + return "dashboard-lists"; + } - @Override - public String getTracingEndpoint() { - return "dashboard-lists"; - } - - @After + @After public void deleteDashboardList() throws ApiException { dashboardListsApiV1.deleteDashboardList(dashboardListID); } @@ -37,11 +35,11 @@ public void createDashboardList() throws ApiException, NoSuchAlgorithmException dashboardListID = res.getId(); } - @Test - public void retryWithDashboardListItemGetTest() throws ApiException{ - DashboardListItems getResponse = api.getDashboardListItems(dashboardListID); - assertNotNull(getResponse.getTotal()); - assertEquals(0, (long) getResponse.getTotal()); - assertEquals(0, getResponse.getDashboards().size()); - } + @Test + public void retryWithDashboardListItemGetTest() throws ApiException { + DashboardListItems getResponse = api.getDashboardListItems(dashboardListID); + assertNotNull(getResponse.getTotal()); + assertEquals(0, (long) getResponse.getTotal()); + assertEquals(0, getResponse.getDashboards().size()); + } } diff --git a/src/test/java/com/datadog/api/client/v2/api/V2APITest.java b/src/test/java/com/datadog/api/client/v2/api/V2APITest.java index efa99fe6468..8e37445dedf 100644 --- a/src/test/java/com/datadog/api/client/v2/api/V2APITest.java +++ b/src/test/java/com/datadog/api/client/v2/api/V2APITest.java @@ -34,10 +34,10 @@ public abstract class V2APITest extends TestUtils.APITest { protected static ApiClient generalApiClientWithRetry; @BeforeClass - public static void initGeneralApiClientWithRetry(){ + public static void initGeneralApiClientWithRetry() { initGeneralApiClient(); generalApiClientWithRetry = generalApiClient; - generalApiClientWithRetry.setRetry(new RetryConfig(true,2,2,3)); + generalApiClientWithRetry.setRetry(new RetryConfig(true, 2, 2, 3)); } @BeforeClass From c3366319bbf9a7943880b641d5c358d4522d1521 Mon Sep 17 00:00:00 2001 From: HantingZhang2 Date: Mon, 7 Aug 2023 17:31:12 +0200 Subject: [PATCH 08/43] Using apiclient With Retry --- .../datadog/api/client/v2/api/RetryTest.java | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/test/java/com/datadog/api/client/v2/api/RetryTest.java b/src/test/java/com/datadog/api/client/v2/api/RetryTest.java index 652002a3724..d363be1b51f 100644 --- a/src/test/java/com/datadog/api/client/v2/api/RetryTest.java +++ b/src/test/java/com/datadog/api/client/v2/api/RetryTest.java @@ -25,17 +25,17 @@ public String getTracingEndpoint() { } @After - public void deleteDashboardList() throws ApiException { - dashboardListsApiV1.deleteDashboardList(dashboardListID); - } - - @Before - public void createDashboardList() throws ApiException, NoSuchAlgorithmException { - dashboardListsApiV1 = new com.datadog.api.client.v1.api.DashboardListsApi(generalApiClient); - DashboardList res = - dashboardListsApiV1.createDashboardList(new DashboardList().name(getUniqueEntityName())); - dashboardListID = res.getId(); - } + public void deleteDashboardList() throws ApiException { + dashboardListsApiV1.deleteDashboardList(dashboardListID); + } + + @Before + public void createDashboardList() throws ApiException, NoSuchAlgorithmException { + dashboardListsApiV1 = new com.datadog.api.client.v1.api.DashboardListsApi(generalApiClientWithRetry); + DashboardList res = + dashboardListsApiV1.createDashboardList(new DashboardList().name(getUniqueEntityName())); + dashboardListID = res.getId(); + } @Test public void retryWithDashboardListItemGetTest() throws ApiException{ From ef4a9b51b706b0344a097db0d038ef3359b1b2d9 Mon Sep 17 00:00:00 2001 From: "ci.datadog-api-spec" Date: Mon, 7 Aug 2023 15:34:44 +0000 Subject: [PATCH 09/43] pre-commit fixes --- .../datadog/api/client/v2/api/RetryTest.java | 47 ++++++++++--------- 1 file changed, 24 insertions(+), 23 deletions(-) diff --git a/src/test/java/com/datadog/api/client/v2/api/RetryTest.java b/src/test/java/com/datadog/api/client/v2/api/RetryTest.java index 397d058a5fb..9c624917711 100644 --- a/src/test/java/com/datadog/api/client/v2/api/RetryTest.java +++ b/src/test/java/com/datadog/api/client/v2/api/RetryTest.java @@ -13,33 +13,34 @@ public class RetryTest extends V2APITest { - private static long dashboardListID; - private static com.datadog.api.client.v1.api.DashboardListsApi dashboardListsApiV1; - private final DashboardListsApi api = new DashboardListsApi(generalApiClientWithRetry); + private static long dashboardListID; + private static com.datadog.api.client.v1.api.DashboardListsApi dashboardListsApiV1; + private final DashboardListsApi api = new DashboardListsApi(generalApiClientWithRetry); - @Override - public String getTracingEndpoint() { + @Override + public String getTracingEndpoint() { return "dashboard-lists"; - } - - @After - public void deleteDashboardList() throws ApiException { - dashboardListsApiV1.deleteDashboardList(dashboardListID); - } - - @Before - public void createDashboardList() throws ApiException, NoSuchAlgorithmException { - dashboardListsApiV1 = new com.datadog.api.client.v1.api.DashboardListsApi(generalApiClientWithRetry); - DashboardList res = - dashboardListsApiV1.createDashboardList(new DashboardList().name(getUniqueEntityName())); - dashboardListID = res.getId(); - } - - @Test - public void retryWithDashboardListItemGetTest() throws ApiException { + } + + @After + public void deleteDashboardList() throws ApiException { + dashboardListsApiV1.deleteDashboardList(dashboardListID); + } + + @Before + public void createDashboardList() throws ApiException, NoSuchAlgorithmException { + dashboardListsApiV1 = + new com.datadog.api.client.v1.api.DashboardListsApi(generalApiClientWithRetry); + DashboardList res = + dashboardListsApiV1.createDashboardList(new DashboardList().name(getUniqueEntityName())); + dashboardListID = res.getId(); + } + + @Test + public void retryWithDashboardListItemGetTest() throws ApiException { DashboardListItems getResponse = api.getDashboardListItems(dashboardListID); assertNotNull(getResponse.getTotal()); assertEquals(0, (long) getResponse.getTotal()); assertEquals(0, getResponse.getDashboards().size()); - } + } } From 4caf8ba207974477e2716c501f87ca19e982acf9 Mon Sep 17 00:00:00 2001 From: HantingZhang2 Date: Tue, 8 Aug 2023 16:29:20 +0200 Subject: [PATCH 10/43] Add retry test cassette --- .../com/datadog/api/client/ApiClient.java | 12 +- .../datadog/api/client/v2/api/RetryTest.java | 12 +- ...t.retryWithDashboardListItemGetTest.freeze | 1 + ...est.retryWithDashboardListItemGetTest.json | 279 ++++++++++++++++++ 4 files changed, 291 insertions(+), 13 deletions(-) create mode 100644 src/test/resources/cassettes/v2/RetryTest.retryWithDashboardListItemGetTest.freeze create mode 100644 src/test/resources/cassettes/v2/RetryTest.retryWithDashboardListItemGetTest.json diff --git a/src/main/java/com/datadog/api/client/ApiClient.java b/src/main/java/com/datadog/api/client/ApiClient.java index 56397bdec67..4cf8e026ac0 100644 --- a/src/main/java/com/datadog/api/client/ApiClient.java +++ b/src/main/java/com/datadog/api/client/ApiClient.java @@ -1550,12 +1550,12 @@ public ApiResponse invokeAPI( } } else if (shouldRetry(currentRetry, statusCode, retry)){ try{ - Thread.sleep(calculateRetryIntrval(responseHeaders, retry, statusCode)*1000); + Thread.sleep(calculateRetryIntrval(responseHeaders, retry, currentRetry)*1000); + currentRetry++; } catch ( InterruptedException e){ Thread.currentThread().interrupt(); e.printStackTrace(); } - currentRetry++; } else { String message = "error"; String respBody = null; @@ -1583,7 +1583,7 @@ public ApiResponse invokeAPI( private boolean shouldRetry(int retryCount, int statusCode, RetryConfig retryConfig){ boolean statusToRetry = false; - if (statusCode == 413 || statusCode == 429 || statusCode >= 500){ + if (statusCode == 429 || statusCode >= 500){ statusToRetry = true; } return (retryConfig.maxRetries>=retryCount && statusToRetry && retryConfig.isEnableRetry()); @@ -1592,10 +1592,12 @@ private boolean shouldRetry(int retryCount, int statusCode, RetryConfig retryCon private int calculateRetryIntrval(Map> responseHeaders, RetryConfig retryConfig, int retryCount){ if ( responseHeaders.get("x-ratelimit-reset")!=null){ List rateLimitHeader = responseHeaders.get("x-ratelimit-reset"); - return Integer.parseInt(rateLimitHeader.get(0)); + String ratelimitString= rateLimitHeader.get(0); + String ratelimitStringTrimmed = ratelimitString.replaceAll("\\D",""); + return Integer.parseInt(ratelimitStringTrimmed); } else { int retryInterval= (int) Math.pow (retry.backOffMultiplier, retryCount)* retryConfig.backOffBase; - if (getConnectTimeout()!=0){ + if (getConnectTimeout()>0){ retryInterval = Math.min(retryInterval, getConnectTimeout()); } return retryInterval; diff --git a/src/test/java/com/datadog/api/client/v2/api/RetryTest.java b/src/test/java/com/datadog/api/client/v2/api/RetryTest.java index 9c624917711..f6537bd1b2d 100644 --- a/src/test/java/com/datadog/api/client/v2/api/RetryTest.java +++ b/src/test/java/com/datadog/api/client/v2/api/RetryTest.java @@ -5,8 +5,9 @@ import com.datadog.api.client.ApiException; import com.datadog.api.client.v1.model.DashboardList; -import com.datadog.api.client.v2.model.DashboardListItems; +import com.datadog.api.client.v2.model.*; import java.security.NoSuchAlgorithmException; + import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -19,12 +20,7 @@ public class RetryTest extends V2APITest { @Override public String getTracingEndpoint() { - return "dashboard-lists"; - } - - @After - public void deleteDashboardList() throws ApiException { - dashboardListsApiV1.deleteDashboardList(dashboardListID); + return "dashboard-lists-retry-tests"; } @Before @@ -43,4 +39,4 @@ public void retryWithDashboardListItemGetTest() throws ApiException { assertEquals(0, (long) getResponse.getTotal()); assertEquals(0, getResponse.getDashboards().size()); } -} +} \ No newline at end of file diff --git a/src/test/resources/cassettes/v2/RetryTest.retryWithDashboardListItemGetTest.freeze b/src/test/resources/cassettes/v2/RetryTest.retryWithDashboardListItemGetTest.freeze new file mode 100644 index 00000000000..3fc05aedd1c --- /dev/null +++ b/src/test/resources/cassettes/v2/RetryTest.retryWithDashboardListItemGetTest.freeze @@ -0,0 +1 @@ +2023-08-08T10:58:09.270Z \ No newline at end of file diff --git a/src/test/resources/cassettes/v2/RetryTest.retryWithDashboardListItemGetTest.json b/src/test/resources/cassettes/v2/RetryTest.retryWithDashboardListItemGetTest.json new file mode 100644 index 00000000000..6ac58bfaf01 --- /dev/null +++ b/src/test/resources/cassettes/v2/RetryTest.retryWithDashboardListItemGetTest.json @@ -0,0 +1,279 @@ +[{ + "httpRequest" : { + "method" : "POST", + "path" : "/api/v1/dashboard/lists/manual", + "headers" : { + "x-datadog-trace-id" : [ "7753420519099277221" ], + "x-datadog-origin" : [ "ciapp-test" ], + "content-length" : [ "58" ], + "Content-Type" : [ "application/json" ], + "Content-Language" : [ "" ], + "Accept-Encoding" : [ "deflate,gzip,x-gzip,zstd1" ], + "Accept" : [ "application/json" ] + }, + "keepAlive" : false, + "secure" : true, + "body" : { + "contentType" : "application/json", + "type" : "JSON", + "json" : { + "name" : "java-retryWithDashboardListItemGetTest-local-1691492289" + } + } + }, + "httpResponse" : { + "statusCode" : 200, + "reasonPhrase" : "OK", + "headers" : { + "x-frame-options" : [ "SAMEORIGIN" ], + "x-content-type-options" : [ "nosniff" ], + "vary" : [ "Accept-Encoding" ], + "strict-transport-security" : [ "max-age=15724800;" ], + "content-security-policy" : [ "frame-ancestors 'self'; report-uri https://logs.browser-intake-datadoghq.com/api/v2/logs?dd-api-key=pube4f163c23bbf91c16b8f57f56af9fc58&dd-evp-origin=content-security-policy&ddsource=csp-report&ddtags=site%3Adatadoghq.com" ], + "Date" : [ "Mon, 17 Oct 2022 07:00:49 GMT" ], + "Content-Type" : [ "application/json" ] + }, + "body" : { + "contentType" : "application/json", + "type" : "JSON", + "json" : { + "is_favorite" : false, + "name" : "java-retryWithDashboardListItemGetTest-local-1691492289", + "dashboard_count" : 0, + "author" : { + "handle" : "frog@datadoghq.com", + "name" : null + }, + "created" : "2022-10-17T07:00:49.090621+00:00", + "type" : "manual_dashboard_list", + "dashboards" : null, + "modified" : "2022-10-17T07:00:49.090633+00:00", + "id" : 334687 + } + } + }, + "id" : "c012f980-4ed1-4d58-8e99-fe28876ebf8a", + "priority" : 0, + "timeToLive" : { + "unlimited" : true + }, + "times" : { + "remainingTimes" : 1 + } + },{ + "httpRequest" : { + "method" : "GET", + "path" : "/api/v2/dashboard/lists/manual/334687/dashboards", + "headers" : { + "x-datadog-trace-id" : [ "3402781242333921307" ], + "x-datadog-origin" : [ "ciapp-test" ], + "content-length" : [ "0" ], + "Accept-Encoding" : [ "deflate,gzip,x-gzip,zstd1" ], + "Accept" : [ "application/json" ] + }, + "keepAlive" : false, + "secure" : true + }, + "httpResponse" : { + "statusCode" : 429, + "reasonPhrase" : "Too many requests", + "headers" : { + "x-frame-options" : [ "SAMEORIGIN" ], + "x-content-type-options" : [ "nosniff" ], + "x-ratelimit-reset" : [ "1" ], + "strict-transport-security" : [ "max-age=15724800;" ], + "content-security-policy" : [ "frame-ancestors 'self'; report-uri https://logs.browser-intake-datadoghq.com/api/v2/logs?dd-api-key=pube4f163c23bbf91c16b8f57f56af9fc58&dd-evp-origin=content-security-policy&ddsource=csp-report&ddtags=site%3Adatadoghq.com" ], + "Date" : [ "Mon, 17 Oct 2022 07:00:50 GMT" ], + "Content-Type" : [ "application/json" ] + }, + "body" : { + "contentType" : "application/json", + "type" : "JSON", + "json" : { + "total" : 0, + "dashboards" : [ ] + } + } + }, + "id" : "58cb9fa4-e67d-4fab-9466-4acf4c9a37a1", + "priority" : 0, + "timeToLive" : { + "unlimited" : true + }, + "times" : { + "remainingTimes" : 1 + } + },{ + "httpRequest" : { + "method" : "GET", + "path" : "/api/v2/dashboard/lists/manual/334687/dashboards", + "headers" : { + "x-datadog-trace-id" : [ "3402781242333921307" ], + "x-datadog-origin" : [ "ciapp-test" ], + "content-length" : [ "0" ], + "Accept-Encoding" : [ "deflate,gzip,x-gzip,zstd1" ], + "Accept" : [ "application/json" ] + }, + "keepAlive" : false, + "secure" : true + }, + "httpResponse" : { + "statusCode" : 429, + "reasonPhrase" : "Too many requests", + "headers" : { + "x-frame-options" : [ "SAMEORIGIN" ], + "x-content-type-options" : [ "nosniff" ], + "x-ratelimit-reset" : [ "1" ], + "strict-transport-security" : [ "max-age=15724800;" ], + "content-security-policy" : [ "frame-ancestors 'self'; report-uri https://logs.browser-intake-datadoghq.com/api/v2/logs?dd-api-key=pube4f163c23bbf91c16b8f57f56af9fc58&dd-evp-origin=content-security-policy&ddsource=csp-report&ddtags=site%3Adatadoghq.com" ], + "Date" : [ "Mon, 17 Oct 2022 07:00:50 GMT" ], + "Content-Type" : [ "application/json" ] + }, + "body" : { + "contentType" : "application/json", + "type" : "JSON", + "json" : { + "total" : 0, + "dashboards" : [ ] + } + } + }, + "id" : "58cb9fa4-e67d-4fab-9466-4acf4c9a37a1", + "priority" : 0, + "timeToLive" : { + "unlimited" : true + }, + "times" : { + "remainingTimes" : 1 + } + },{ + "httpRequest" : { + "method" : "GET", + "path" : "/api/v2/dashboard/lists/manual/334687/dashboards", + "headers" : { + "x-datadog-trace-id" : [ "3402781242333921307" ], + "x-datadog-origin" : [ "ciapp-test" ], + "content-length" : [ "0" ], + "Accept-Encoding" : [ "deflate,gzip,x-gzip,zstd1" ], + "Accept" : [ "application/json" ] + }, + "keepAlive" : false, + "secure" : true + }, + "httpResponse" : { + "statusCode" : 429, + "reasonPhrase" : "Too many requests", + "headers" : { + "x-frame-options" : [ "SAMEORIGIN" ], + "x-content-type-options" : [ "nosniff" ], + "x-ratelimit-reset" : [ "1" ], + "strict-transport-security" : [ "max-age=15724800;" ], + "content-security-policy" : [ "frame-ancestors 'self'; report-uri https://logs.browser-intake-datadoghq.com/api/v2/logs?dd-api-key=pube4f163c23bbf91c16b8f57f56af9fc58&dd-evp-origin=content-security-policy&ddsource=csp-report&ddtags=site%3Adatadoghq.com" ], + "Date" : [ "Mon, 17 Oct 2022 07:00:50 GMT" ], + "Content-Type" : [ "application/json" ] + }, + "body" : { + "contentType" : "application/json", + "type" : "JSON", + "json" : { + "total" : 0, + "dashboards" : [ ] + } + } + }, + "id" : "58cb9fa4-e67d-4fab-9466-4acf4c9a37a1", + "priority" : 0, + "timeToLive" : { + "unlimited" : true + }, + "times" : { + "remainingTimes" : 1 + } + } + + + ,{ + "httpRequest" : { + "method" : "GET", + "path" : "/api/v2/dashboard/lists/manual/334687/dashboards", + "headers" : { + "x-datadog-trace-id" : [ "3402781242333921307" ], + "x-datadog-origin" : [ "ciapp-test" ], + "content-length" : [ "0" ], + "Accept-Encoding" : [ "deflate,gzip,x-gzip,zstd1" ], + "Accept" : [ "application/json" ] + }, + "keepAlive" : false, + "secure" : true + }, + "httpResponse" : { + "statusCode" : 429, + "reasonPhrase" : "Too many requests", + "headers" : { + "x-frame-options" : [ "SAMEORIGIN" ], + "x-content-type-options" : [ "nosniff" ], + "strict-transport-security" : [ "max-age=15724800;" ], + "content-security-policy" : [ "frame-ancestors 'self'; report-uri https://logs.browser-intake-datadoghq.com/api/v2/logs?dd-api-key=pube4f163c23bbf91c16b8f57f56af9fc58&dd-evp-origin=content-security-policy&ddsource=csp-report&ddtags=site%3Adatadoghq.com" ], + "Date" : [ "Mon, 17 Oct 2022 07:00:50 GMT" ], + "Content-Type" : [ "application/json" ] + }, + "body" : { + "contentType" : "application/json", + "type" : "JSON", + "json" : { + "total" : 0, + "dashboards" : [ ] + } + } + }, + "id" : "58cb9fa4-e67d-4fab-9466-4acf4c9a39a1", + "priority" : 0, + "timeToLive" : { + "unlimited" : true + }, + "times" : { + "remainingTimes" : 1 + } + },{ + "httpRequest" : { + "method" : "GET", + "path" : "/api/v2/dashboard/lists/manual/334687/dashboards", + "headers" : { + "x-datadog-trace-id" : [ "3402781242333921307" ], + "x-datadog-origin" : [ "ciapp-test" ], + "content-length" : [ "0" ], + "Accept-Encoding" : [ "deflate,gzip,x-gzip,zstd1" ], + "Accept" : [ "application/json" ] + }, + "keepAlive" : false, + "secure" : true + }, + "httpResponse" : { + "statusCode" : 200, + "reasonPhrase" : "OK", + "headers" : { + "x-frame-options" : [ "SAMEORIGIN" ], + "x-content-type-options" : [ "nosniff" ], + "strict-transport-security" : [ "max-age=15724800;" ], + "content-security-policy" : [ "frame-ancestors 'self'; report-uri https://logs.browser-intake-datadoghq.com/api/v2/logs?dd-api-key=pube4f163c23bbf91c16b8f57f56af9fc58&dd-evp-origin=content-security-policy&ddsource=csp-report&ddtags=site%3Adatadoghq.com" ], + "Date" : [ "Mon, 17 Oct 2022 07:00:50 GMT" ], + "Content-Type" : [ "application/json" ] + }, + "body" : { + "contentType" : "application/json", + "type" : "JSON", + "json" : { + "total" : 0, + "dashboards" : [ ] + } + } + }, + "id" : "58cb9fa4-e67d-4fab-9466-4acf4c9a47a1", + "priority" : 0, + "timeToLive" : { + "unlimited" : true + }, + "times" : { + "remainingTimes" : 1 + } + }] \ No newline at end of file From e692c7709d93f94756f02d59156bb7d8dcb4d048 Mon Sep 17 00:00:00 2001 From: "ci.datadog-api-spec" Date: Tue, 8 Aug 2023 14:30:13 +0000 Subject: [PATCH 11/43] pre-commit fixes --- src/test/java/com/datadog/api/client/v2/api/RetryTest.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/test/java/com/datadog/api/client/v2/api/RetryTest.java b/src/test/java/com/datadog/api/client/v2/api/RetryTest.java index f6537bd1b2d..4209f998615 100644 --- a/src/test/java/com/datadog/api/client/v2/api/RetryTest.java +++ b/src/test/java/com/datadog/api/client/v2/api/RetryTest.java @@ -7,8 +7,6 @@ import com.datadog.api.client.v1.model.DashboardList; import com.datadog.api.client.v2.model.*; import java.security.NoSuchAlgorithmException; - -import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -39,4 +37,4 @@ public void retryWithDashboardListItemGetTest() throws ApiException { assertEquals(0, (long) getResponse.getTotal()); assertEquals(0, getResponse.getDashboards().size()); } -} \ No newline at end of file +} From 864c584c3bc72462d77d3cc96b25422e4f4e975d Mon Sep 17 00:00:00 2001 From: HantingZhang2 Date: Tue, 8 Aug 2023 18:59:59 +0200 Subject: [PATCH 12/43] add generator code --- .../src/generator/templates/ApiClient.j2 | 108 +++++++++++++----- .../src/generator/templates/RetryConfig.j2 | 57 +++++++++ .../com/datadog/api/client/ApiClient.java | 18 +-- 3 files changed, 144 insertions(+), 39 deletions(-) create mode 100644 .generator/src/generator/templates/RetryConfig.j2 diff --git a/.generator/src/generator/templates/ApiClient.j2 b/.generator/src/generator/templates/ApiClient.j2 index 16772051087..71aca2fcb08 100644 --- a/.generator/src/generator/templates/ApiClient.j2 +++ b/.generator/src/generator/templates/ApiClient.j2 @@ -78,6 +78,7 @@ import {{ common_package_name }}.ApiException; import {{ common_package_name }}.ApiResponse; import {{ common_package_name }}.JSON; import {{ common_package_name }}.Pair; +import {{ common_package_name }}.RetryConfig; import {{ common_package_name }}.RFC3339DateFormat; import {{ common_package_name }}.ServerConfiguration; import {{ common_package_name }}.ServerVariable; @@ -162,6 +163,7 @@ public class ApiClient { protected Map operationServerIndex = new HashMap(); protected Map> operationServerVariables = new HashMap>(); protected boolean debugging = false; + protected RetryConfig retry = new RetryConfig(false, 2, 2, 3); protected boolean compress = true; protected ClientConfig clientConfig; protected int connectionTimeout = 0; @@ -288,6 +290,14 @@ public class ApiClient { return offsetDateTimeFormatter; } + /** + * Add custom retry object in the client + * @param retry retry object + * */ + public void setRetry(RetryConfig retry) { + this.retry = retry; + } + /** * Set the date format used to parse/format {@code OffsetDateTime} parameters. * @@ -1229,45 +1239,83 @@ public class ApiClient { throws ApiException { String contentEncoding = headerParams.get(HttpHeaders.CONTENT_ENCODING); - Entity entity = serialize(body, formParams, selectHeaderContentType(contentTypes), contentEncoding, isBodyNullable); + Entity entity = + serialize( + body, + formParams, + selectHeaderContentType(contentTypes), + contentEncoding, + isBodyNullable); Response response = null; try { - response = sendRequest(method, invocationBuilder, entity); - - int statusCode = response.getStatusInfo().getStatusCode(); - Map> responseHeaders = buildResponseHeaders(response); - - if (response.getStatusInfo() == Status.NO_CONTENT) { - return new ApiResponse(statusCode, responseHeaders); - } else if (response.getStatusInfo().getFamily() == Status.Family.SUCCESSFUL) { - if (returnType == null) { + int currentRetry = 0; + while (true){ + response = sendRequest(method, invocationBuilder, entity); + int statusCode = response.getStatusInfo().getStatusCode(); + Map> responseHeaders = buildResponseHeaders(response); + if (response.getStatusInfo() == Status.NO_CONTENT) { return new ApiResponse(statusCode, responseHeaders); + } else if (response.getStatusInfo().getFamily() == Status.Family.SUCCESSFUL) { + if (returnType == null) { + return new ApiResponse(statusCode, responseHeaders); + } else { + return new ApiResponse(statusCode, responseHeaders, deserialize(response, returnType)); + } + } else if (shouldRetry(currentRetry, statusCode, retry)){ + try{ + Thread.sleep(calculateRetryIntrval(responseHeaders, retry, currentRetry)*1000); + currentRetry++; + } catch ( InterruptedException e){ + Thread.currentThread().interrupt(); + e.printStackTrace(); + } } else { - return new ApiResponse(statusCode, responseHeaders, deserialize(response, returnType)); - } - } else { - String message = "error"; - String respBody = null; - if (response.hasEntity()) { - try { - respBody = String.valueOf(response.readEntity(String.class)); - message = respBody; - } catch (RuntimeException e) { - // e.printStackTrace(); + String message = "error"; + String respBody = null; + if (response.hasEntity()) { + try { + respBody = String.valueOf(response.readEntity(String.class)); + message = respBody; + } catch (RuntimeException e) { + // e.printStackTrace(); + } } - } - throw new ApiException( - response.getStatus(), message, buildResponseHeaders(response), respBody); - } + throw new ApiException( + response.getStatus(), message, buildResponseHeaders(response), respBody); + } + } } finally { - try { - response.close(); - } catch (Exception e) { - // it's not critical, since the response object is local in method invokeAPI; that's fine, - // just continue + try { + response.close(); + } catch (Exception e) { + // it's not critical, since the response object is local in method invokeAPI; that's fine, + // just continue + } + } + } + + private boolean shouldRetry(int retryCount, int statusCode, RetryConfig retryConfig){ + boolean statusToRetry = false; + if (statusCode == 429 || statusCode >= 500){ + statusToRetry = true; + } + return (retryConfig.maxRetries>=retryCount && statusToRetry && retryConfig.isEnableRetry()); + } + + private int calculateRetryIntrval(Map> responseHeaders, RetryConfig retryConfig, int retryCount){ + if ( responseHeaders.get("x-ratelimit-reset")!=null){ + List rateLimitHeader = responseHeaders.get("x-ratelimit-reset"); + String ratelimitString= rateLimitHeader.get(0); + String ratelimitStringTrimmed = ratelimitString.replaceAll("\\D",""); + return Integer.parseInt(ratelimitStringTrimmed); + } else { + int retryInterval= (int) Math.pow (retry.backOffMultiplier, retryCount)* retryConfig.backOffBase; + if (getConnectTimeout()>0){ + retryInterval = Math.min(retryInterval, getConnectTimeout()); } + return retryInterval; } } diff --git a/.generator/src/generator/templates/RetryConfig.j2 b/.generator/src/generator/templates/RetryConfig.j2 new file mode 100644 index 00000000000..1bcf8c8d29b --- /dev/null +++ b/.generator/src/generator/templates/RetryConfig.j2 @@ -0,0 +1,57 @@ +{% include "ApiInfo.j2" %} + +package {{ common_package_name }}; + +public class RetryConfig { + public boolean enableRetry; + public int backOffMultiplier; + public int backOffBase; + public int maxRetries; + + + /** + * @param enableRetry Enable retry when rate limited + * @param backOffMultiplier Multiplier for retry backoff + * @param backOffBase Base for retry backoff + * @param maxRetries Maximum number of retries + */ + + public RetryConfig(boolean enableRetry, int backOffMultiplier, int backOffBase, int maxRetries) { + this.enableRetry = enableRetry; + this.backOffMultiplier = backOffMultiplier; + this.backOffBase = backOffBase; + this.maxRetries = maxRetries; + } + + public boolean isEnableRetry() { + return enableRetry; + } + + public int getBackOffMultiplier() { + return backOffMultiplier; + } + + public int getBackOffBase() { + return backOffBase; + } + + public int getMaxRetries() { + return maxRetries; + } + + public void setEnableRetry(boolean enableRetry) { + this.enableRetry = enableRetry; + } + + public void setBackOffMultiplier(int backOffMultiplier) { + this.backOffMultiplier = backOffMultiplier; + } + + public void setBackOffBase(int backOffBase) { + this.backOffBase = backOffBase; + } + public void setMaxRetries(int maxRetries) { + this.maxRetries = maxRetries; + } + +} \ No newline at end of file diff --git a/src/main/java/com/datadog/api/client/ApiClient.java b/src/main/java/com/datadog/api/client/ApiClient.java index 4cf8e026ac0..f5cf356041f 100644 --- a/src/main/java/com/datadog/api/client/ApiClient.java +++ b/src/main/java/com/datadog/api/client/ApiClient.java @@ -350,7 +350,7 @@ public class ApiClient { protected Map> operationServerVariables = new HashMap>(); protected boolean debugging = false; - protected RetryConfig retry = new RetryConfig(false, 2, 2, 3) ; + protected RetryConfig retry = new RetryConfig(false, 2, 2, 3); protected boolean compress = true; protected ClientConfig clientConfig; protected int connectionTimeout = 0; @@ -1569,16 +1569,16 @@ public ApiResponse invokeAPI( } throw new ApiException( response.getStatus(), message, buildResponseHeaders(response), respBody); - } + } } - } finally { - try { - response.close(); - } catch (Exception e) { - // it's not critical, since the response object is local in method invokeAPI; that's fine, - // just continue + } finally { + try { + response.close(); + } catch (Exception e) { + // it's not critical, since the response object is local in method invokeAPI; that's fine, + // just continue + } } - } } private boolean shouldRetry(int retryCount, int statusCode, RetryConfig retryConfig){ From dd5b81c8af943f9fb5d9ba5a2a25e6741e05e7db Mon Sep 17 00:00:00 2001 From: "ci.datadog-api-spec" Date: Tue, 8 Aug 2023 17:01:58 +0000 Subject: [PATCH 13/43] pre-commit fixes --- .../com/datadog/api/client/ApiClient.java | 57 ++++++++++--------- .../com/datadog/api/client/RetryConfig.java | 55 ------------------ 2 files changed, 30 insertions(+), 82 deletions(-) delete mode 100644 src/main/java/com/datadog/api/client/RetryConfig.java diff --git a/src/main/java/com/datadog/api/client/ApiClient.java b/src/main/java/com/datadog/api/client/ApiClient.java index f5cf356041f..c8e6d17348b 100644 --- a/src/main/java/com/datadog/api/client/ApiClient.java +++ b/src/main/java/com/datadog/api/client/ApiClient.java @@ -350,7 +350,7 @@ public class ApiClient { protected Map> operationServerVariables = new HashMap>(); protected boolean debugging = false; - protected RetryConfig retry = new RetryConfig(false, 2, 2, 3); + protected RetryConfig retry = new RetryConfig(false, 2, 2, 3); protected boolean compress = true; protected ClientConfig clientConfig; protected int connectionTimeout = 0; @@ -518,14 +518,14 @@ public DateTimeFormatter getOffsetDateTimeFormatter() { return offsetDateTimeFormatter; } - /** + /** * Add custom retry object in the client + * * @param retry retry object - * */ + */ public void setRetry(RetryConfig retry) { this.retry = retry; } - /** * Set the date format used to parse/format {@code OffsetDateTime} parameters. @@ -1536,7 +1536,7 @@ public ApiResponse invokeAPI( try { int currentRetry = 0; - while (true){ + while (true) { response = sendRequest(method, invocationBuilder, entity); int statusCode = response.getStatusInfo().getStatusCode(); Map> responseHeaders = buildResponseHeaders(response); @@ -1546,13 +1546,14 @@ public ApiResponse invokeAPI( if (returnType == null) { return new ApiResponse(statusCode, responseHeaders); } else { - return new ApiResponse(statusCode, responseHeaders, deserialize(response, returnType)); + return new ApiResponse( + statusCode, responseHeaders, deserialize(response, returnType)); } - } else if (shouldRetry(currentRetry, statusCode, retry)){ - try{ - Thread.sleep(calculateRetryIntrval(responseHeaders, retry, currentRetry)*1000); + } else if (shouldRetry(currentRetry, statusCode, retry)) { + try { + Thread.sleep(calculateRetryIntrval(responseHeaders, retry, currentRetry) * 1000); currentRetry++; - } catch ( InterruptedException e){ + } catch (InterruptedException e) { Thread.currentThread().interrupt(); e.printStackTrace(); } @@ -1569,35 +1570,37 @@ public ApiResponse invokeAPI( } throw new ApiException( response.getStatus(), message, buildResponseHeaders(response), respBody); - } - } + } + } } finally { - try { - response.close(); - } catch (Exception e) { - // it's not critical, since the response object is local in method invokeAPI; that's fine, - // just continue - } + try { + response.close(); + } catch (Exception e) { + // it's not critical, since the response object is local in method invokeAPI; that's fine, + // just continue } + } } - private boolean shouldRetry(int retryCount, int statusCode, RetryConfig retryConfig){ + private boolean shouldRetry(int retryCount, int statusCode, RetryConfig retryConfig) { boolean statusToRetry = false; - if (statusCode == 429 || statusCode >= 500){ + if (statusCode == 429 || statusCode >= 500) { statusToRetry = true; } - return (retryConfig.maxRetries>=retryCount && statusToRetry && retryConfig.isEnableRetry()); + return (retryConfig.maxRetries >= retryCount && statusToRetry && retryConfig.isEnableRetry()); } - private int calculateRetryIntrval(Map> responseHeaders, RetryConfig retryConfig, int retryCount){ - if ( responseHeaders.get("x-ratelimit-reset")!=null){ + private int calculateRetryIntrval( + Map> responseHeaders, RetryConfig retryConfig, int retryCount) { + if (responseHeaders.get("x-ratelimit-reset") != null) { List rateLimitHeader = responseHeaders.get("x-ratelimit-reset"); - String ratelimitString= rateLimitHeader.get(0); - String ratelimitStringTrimmed = ratelimitString.replaceAll("\\D",""); + String ratelimitString = rateLimitHeader.get(0); + String ratelimitStringTrimmed = ratelimitString.replaceAll("\\D", ""); return Integer.parseInt(ratelimitStringTrimmed); } else { - int retryInterval= (int) Math.pow (retry.backOffMultiplier, retryCount)* retryConfig.backOffBase; - if (getConnectTimeout()>0){ + int retryInterval = + (int) Math.pow(retry.backOffMultiplier, retryCount) * retryConfig.backOffBase; + if (getConnectTimeout() > 0) { retryInterval = Math.min(retryInterval, getConnectTimeout()); } return retryInterval; diff --git a/src/main/java/com/datadog/api/client/RetryConfig.java b/src/main/java/com/datadog/api/client/RetryConfig.java deleted file mode 100644 index 1a1a2778ea5..00000000000 --- a/src/main/java/com/datadog/api/client/RetryConfig.java +++ /dev/null @@ -1,55 +0,0 @@ -package com.datadog.api.client; - -public class RetryConfig { - public boolean enableRetry; - public int backOffMultiplier; - public int backOffBase; - public int maxRetries; - - - /** - * @param enableRetry Enable retry when rate limited - * @param backOffMultiplier Multiplier for retry backoff - * @param backOffBase Base for retry backoff - * @param maxRetries Maximum number of retries - */ - - public RetryConfig(boolean enableRetry, int backOffMultiplier, int backOffBase, int maxRetries) { - this.enableRetry = enableRetry; - this.backOffMultiplier = backOffMultiplier; - this.backOffBase = backOffBase; - this.maxRetries = maxRetries; - } - - public boolean isEnableRetry() { - return enableRetry; - } - - public int getBackOffMultiplier() { - return backOffMultiplier; - } - - public int getBackOffBase() { - return backOffBase; - } - - public int getMaxRetries() { - return maxRetries; - } - - public void setEnableRetry(boolean enableRetry) { - this.enableRetry = enableRetry; - } - - public void setBackOffMultiplier(int backOffMultiplier) { - this.backOffMultiplier = backOffMultiplier; - } - - public void setBackOffBase(int backOffBase) { - this.backOffBase = backOffBase; - } - public void setMaxRetries(int maxRetries) { - this.maxRetries = maxRetries; - } - -} From 5f3aaef62566e2749033e4fa95841573d4582424 Mon Sep 17 00:00:00 2001 From: HantingZhang2 Date: Tue, 8 Aug 2023 19:10:32 +0200 Subject: [PATCH 14/43] Add retryconfig back --- .../com/datadog/api/client/RetryConfig.java | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 src/main/java/com/datadog/api/client/RetryConfig.java diff --git a/src/main/java/com/datadog/api/client/RetryConfig.java b/src/main/java/com/datadog/api/client/RetryConfig.java new file mode 100644 index 00000000000..defa74ab2d0 --- /dev/null +++ b/src/main/java/com/datadog/api/client/RetryConfig.java @@ -0,0 +1,55 @@ +package com.datadog.api.client; + +public class RetryConfig { + public boolean enableRetry; + public int backOffMultiplier; + public int backOffBase; + public int maxRetries; + + + /** + * @param enableRetry Enable retry when rate limited + * @param backOffMultiplier Multiplier for retry backoff + * @param backOffBase Base for retry backoff + * @param maxRetries Maximum number of retries + */ + + public RetryConfig(boolean enableRetry, int backOffMultiplier, int backOffBase, int maxRetries) { + this.enableRetry = enableRetry; + this.backOffMultiplier = backOffMultiplier; + this.backOffBase = backOffBase; + this.maxRetries = maxRetries; + } + + public boolean isEnableRetry() { + return enableRetry; + } + + public int getBackOffMultiplier() { + return backOffMultiplier; + } + + public int getBackOffBase() { + return backOffBase; + } + + public int getMaxRetries() { + return maxRetries; + } + + public void setEnableRetry(boolean enableRetry) { + this.enableRetry = enableRetry; + } + + public void setBackOffMultiplier(int backOffMultiplier) { + this.backOffMultiplier = backOffMultiplier; + } + + public void setBackOffBase(int backOffBase) { + this.backOffBase = backOffBase; + } + public void setMaxRetries(int maxRetries) { + this.maxRetries = maxRetries; + } + +} \ No newline at end of file From b470314485077e901ae1210f123d5c72a62c1994 Mon Sep 17 00:00:00 2001 From: "ci.datadog-api-spec" Date: Tue, 8 Aug 2023 17:12:37 +0000 Subject: [PATCH 15/43] pre-commit fixes --- .../com/datadog/api/client/RetryConfig.java | 55 ------------------- 1 file changed, 55 deletions(-) delete mode 100644 src/main/java/com/datadog/api/client/RetryConfig.java diff --git a/src/main/java/com/datadog/api/client/RetryConfig.java b/src/main/java/com/datadog/api/client/RetryConfig.java deleted file mode 100644 index defa74ab2d0..00000000000 --- a/src/main/java/com/datadog/api/client/RetryConfig.java +++ /dev/null @@ -1,55 +0,0 @@ -package com.datadog.api.client; - -public class RetryConfig { - public boolean enableRetry; - public int backOffMultiplier; - public int backOffBase; - public int maxRetries; - - - /** - * @param enableRetry Enable retry when rate limited - * @param backOffMultiplier Multiplier for retry backoff - * @param backOffBase Base for retry backoff - * @param maxRetries Maximum number of retries - */ - - public RetryConfig(boolean enableRetry, int backOffMultiplier, int backOffBase, int maxRetries) { - this.enableRetry = enableRetry; - this.backOffMultiplier = backOffMultiplier; - this.backOffBase = backOffBase; - this.maxRetries = maxRetries; - } - - public boolean isEnableRetry() { - return enableRetry; - } - - public int getBackOffMultiplier() { - return backOffMultiplier; - } - - public int getBackOffBase() { - return backOffBase; - } - - public int getMaxRetries() { - return maxRetries; - } - - public void setEnableRetry(boolean enableRetry) { - this.enableRetry = enableRetry; - } - - public void setBackOffMultiplier(int backOffMultiplier) { - this.backOffMultiplier = backOffMultiplier; - } - - public void setBackOffBase(int backOffBase) { - this.backOffBase = backOffBase; - } - public void setMaxRetries(int maxRetries) { - this.maxRetries = maxRetries; - } - -} \ No newline at end of file From 092528ff3d07afdf05e806786dfdbe6141e0bd1d Mon Sep 17 00:00:00 2001 From: HantingZhang2 Date: Tue, 8 Aug 2023 19:22:02 +0200 Subject: [PATCH 16/43] Update ReadMe --- README.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/README.md b/README.md index c84f50c4c15..e5ae9703110 100644 --- a/README.md +++ b/README.md @@ -168,6 +168,15 @@ If you want to enable requests logging, set the `debugging` flag on your client: defaultClient.setDebugging(true) ``` +### Enable Retry + +To enable the client to retry when rate limited (status 429) or status 500 and above: +```java +defaultClient.setRetry(new RetryConfig(true, int , int ,int )) +``` +The interval between 2 retry attempts will be the value of the `x-ratelimit-reset` response header when available. If not, it will be : +`Math.pow (multiplier_for_retry_backoff, current_retry_count)*base_for_retry_backoff`. + ### Configure proxy You can provide custom `connectorProvider` implemtation to `clientConfig` to use proxy. See example below using `ApacheConnectorProvider`: From 7daa698d53965cb297044bc77a7f1814c16967ef Mon Sep 17 00:00:00 2001 From: "ci.datadog-api-spec" Date: Tue, 8 Aug 2023 17:24:06 +0000 Subject: [PATCH 17/43] pre-commit fixes --- README.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index e5ae9703110..c14cf7b7fd5 100644 --- a/README.md +++ b/README.md @@ -170,12 +170,14 @@ defaultClient.setDebugging(true) ### Enable Retry -To enable the client to retry when rate limited (status 429) or status 500 and above: +To enable the client to retry when rate limited (status 429) or status 500 and above: + ```java defaultClient.setRetry(new RetryConfig(true, int , int ,int )) ``` + The interval between 2 retry attempts will be the value of the `x-ratelimit-reset` response header when available. If not, it will be : -`Math.pow (multiplier_for_retry_backoff, current_retry_count)*base_for_retry_backoff`. +`Math.pow (multiplier_for_retry_backoff, current_retry_count)*base_for_retry_backoff`. ### Configure proxy From 0fe83c569e0dffa0ac54efb834d2990d73ea7f59 Mon Sep 17 00:00:00 2001 From: HantingZhang2 Date: Tue, 8 Aug 2023 19:28:53 +0200 Subject: [PATCH 18/43] Add RetryConfig in generator file item --- .generator/src/generator/cli.py | 1 + 1 file changed, 1 insertion(+) diff --git a/.generator/src/generator/cli.py b/.generator/src/generator/cli.py index d3f91d0a70b..f5792cc7e74 100644 --- a/.generator/src/generator/cli.py +++ b/.generator/src/generator/cli.py @@ -87,6 +87,7 @@ def cli(specs, output): "PaginationIterator.java": env.get_template("PaginationIterator.j2"), "UnparsedObject.java": env.get_template("UnparsedObject.j2"), "ZstdEncoder.java": env.get_template("ZstdEncoder.j2"), + "RetryConfig.java": env.get_template("RetryConfig.j2"), } auth_files = { From 57dec220e5ffd9f4577f34751d92e816067afcae Mon Sep 17 00:00:00 2001 From: HantingZhang2 Date: Tue, 8 Aug 2023 19:33:44 +0200 Subject: [PATCH 19/43] add retryConfig back --- .../com/datadog/api/client/RetryConfig.java | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 src/main/java/com/datadog/api/client/RetryConfig.java diff --git a/src/main/java/com/datadog/api/client/RetryConfig.java b/src/main/java/com/datadog/api/client/RetryConfig.java new file mode 100644 index 00000000000..defa74ab2d0 --- /dev/null +++ b/src/main/java/com/datadog/api/client/RetryConfig.java @@ -0,0 +1,55 @@ +package com.datadog.api.client; + +public class RetryConfig { + public boolean enableRetry; + public int backOffMultiplier; + public int backOffBase; + public int maxRetries; + + + /** + * @param enableRetry Enable retry when rate limited + * @param backOffMultiplier Multiplier for retry backoff + * @param backOffBase Base for retry backoff + * @param maxRetries Maximum number of retries + */ + + public RetryConfig(boolean enableRetry, int backOffMultiplier, int backOffBase, int maxRetries) { + this.enableRetry = enableRetry; + this.backOffMultiplier = backOffMultiplier; + this.backOffBase = backOffBase; + this.maxRetries = maxRetries; + } + + public boolean isEnableRetry() { + return enableRetry; + } + + public int getBackOffMultiplier() { + return backOffMultiplier; + } + + public int getBackOffBase() { + return backOffBase; + } + + public int getMaxRetries() { + return maxRetries; + } + + public void setEnableRetry(boolean enableRetry) { + this.enableRetry = enableRetry; + } + + public void setBackOffMultiplier(int backOffMultiplier) { + this.backOffMultiplier = backOffMultiplier; + } + + public void setBackOffBase(int backOffBase) { + this.backOffBase = backOffBase; + } + public void setMaxRetries(int maxRetries) { + this.maxRetries = maxRetries; + } + +} \ No newline at end of file From e35b2755a8b7f3ce200394c6ef6a7b4af1def8cc Mon Sep 17 00:00:00 2001 From: "ci.datadog-api-spec" Date: Tue, 8 Aug 2023 17:36:17 +0000 Subject: [PATCH 20/43] pre-commit fixes --- .../com/datadog/api/client/RetryConfig.java | 80 ++++++++++--------- 1 file changed, 42 insertions(+), 38 deletions(-) diff --git a/src/main/java/com/datadog/api/client/RetryConfig.java b/src/main/java/com/datadog/api/client/RetryConfig.java index defa74ab2d0..a0c5c3f78a2 100644 --- a/src/main/java/com/datadog/api/client/RetryConfig.java +++ b/src/main/java/com/datadog/api/client/RetryConfig.java @@ -1,55 +1,59 @@ +/* + * Unless explicitly stated otherwise all files in this repository are licensed under the Apache-2.0 License. + * This product includes software developed at Datadog (https://www.datadoghq.com/). + * Copyright 2019-Present Datadog, Inc. + */ + package com.datadog.api.client; public class RetryConfig { - public boolean enableRetry; - public int backOffMultiplier; - public int backOffBase; - public int maxRetries; - + public boolean enableRetry; + public int backOffMultiplier; + public int backOffBase; + public int maxRetries; - /** + /** * @param enableRetry Enable retry when rate limited * @param backOffMultiplier Multiplier for retry backoff * @param backOffBase Base for retry backoff - * @param maxRetries Maximum number of retries + * @param maxRetries Maximum number of retries */ - - public RetryConfig(boolean enableRetry, int backOffMultiplier, int backOffBase, int maxRetries) { - this.enableRetry = enableRetry; - this.backOffMultiplier = backOffMultiplier; - this.backOffBase = backOffBase; - this.maxRetries = maxRetries; - } + public RetryConfig(boolean enableRetry, int backOffMultiplier, int backOffBase, int maxRetries) { + this.enableRetry = enableRetry; + this.backOffMultiplier = backOffMultiplier; + this.backOffBase = backOffBase; + this.maxRetries = maxRetries; + } public boolean isEnableRetry() { - return enableRetry; - } + return enableRetry; + } - public int getBackOffMultiplier() { - return backOffMultiplier; - } + public int getBackOffMultiplier() { + return backOffMultiplier; + } - public int getBackOffBase() { - return backOffBase; - } + public int getBackOffBase() { + return backOffBase; + } - public int getMaxRetries() { - return maxRetries; - } + public int getMaxRetries() { + return maxRetries; + } - public void setEnableRetry(boolean enableRetry) { - this.enableRetry = enableRetry; - } + public void setEnableRetry(boolean enableRetry) { + this.enableRetry = enableRetry; + } - public void setBackOffMultiplier(int backOffMultiplier) { - this.backOffMultiplier = backOffMultiplier; - } + public void setBackOffMultiplier(int backOffMultiplier) { + this.backOffMultiplier = backOffMultiplier; + } - public void setBackOffBase(int backOffBase) { - this.backOffBase = backOffBase; - } - public void setMaxRetries(int maxRetries) { - this.maxRetries = maxRetries; - } + public void setBackOffBase(int backOffBase) { + this.backOffBase = backOffBase; + } -} \ No newline at end of file + public void setMaxRetries(int maxRetries) { + this.maxRetries = maxRetries; + } +} From 474db21ea2d0fabb830cb3faaf24d0786db5e186 Mon Sep 17 00:00:00 2001 From: HantingZhang2 Date: Wed, 9 Aug 2023 11:59:34 +0200 Subject: [PATCH 21/43] fix function name --- .generator/src/generator/templates/ApiClient.j2 | 4 ++-- src/main/java/com/datadog/api/client/ApiClient.java | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.generator/src/generator/templates/ApiClient.j2 b/.generator/src/generator/templates/ApiClient.j2 index 71aca2fcb08..0f56967c97b 100644 --- a/.generator/src/generator/templates/ApiClient.j2 +++ b/.generator/src/generator/templates/ApiClient.j2 @@ -1265,7 +1265,7 @@ public class ApiClient { } } else if (shouldRetry(currentRetry, statusCode, retry)){ try{ - Thread.sleep(calculateRetryIntrval(responseHeaders, retry, currentRetry)*1000); + Thread.sleep(calculateRetryInterval(responseHeaders, retry, currentRetry)*1000); currentRetry++; } catch ( InterruptedException e){ Thread.currentThread().interrupt(); @@ -1304,7 +1304,7 @@ public class ApiClient { return (retryConfig.maxRetries>=retryCount && statusToRetry && retryConfig.isEnableRetry()); } - private int calculateRetryIntrval(Map> responseHeaders, RetryConfig retryConfig, int retryCount){ + private int calculateRetryInterval(Map> responseHeaders, RetryConfig retryConfig, int retryCount){ if ( responseHeaders.get("x-ratelimit-reset")!=null){ List rateLimitHeader = responseHeaders.get("x-ratelimit-reset"); String ratelimitString= rateLimitHeader.get(0); diff --git a/src/main/java/com/datadog/api/client/ApiClient.java b/src/main/java/com/datadog/api/client/ApiClient.java index c8e6d17348b..b994fe3623c 100644 --- a/src/main/java/com/datadog/api/client/ApiClient.java +++ b/src/main/java/com/datadog/api/client/ApiClient.java @@ -1551,7 +1551,7 @@ public ApiResponse invokeAPI( } } else if (shouldRetry(currentRetry, statusCode, retry)) { try { - Thread.sleep(calculateRetryIntrval(responseHeaders, retry, currentRetry) * 1000); + Thread.sleep(calculateRetryInterval(responseHeaders, retry, currentRetry) * 1000); currentRetry++; } catch (InterruptedException e) { Thread.currentThread().interrupt(); @@ -1590,7 +1590,7 @@ private boolean shouldRetry(int retryCount, int statusCode, RetryConfig retryCon return (retryConfig.maxRetries >= retryCount && statusToRetry && retryConfig.isEnableRetry()); } - private int calculateRetryIntrval( + private int calculateRetryInterval( Map> responseHeaders, RetryConfig retryConfig, int retryCount) { if (responseHeaders.get("x-ratelimit-reset") != null) { List rateLimitHeader = responseHeaders.get("x-ratelimit-reset"); From 0aa6aa33b41ba2e08b7034bd577cf353f8ea2f29 Mon Sep 17 00:00:00 2001 From: HantingZhang2 Date: Wed, 9 Aug 2023 13:31:29 +0200 Subject: [PATCH 22/43] Add cassette for status code 500 test --- .../datadog/api/client/v2/api/RetryTest.java | 10 +- ...tryWithDashboardListItemGetTest429.freeze} | 0 ...retryWithDashboardListItemGetTest429.json} | 4 +- ...etryWithDashboardListItemGetTest500.freeze | 1 + ....retryWithDashboardListItemGetTest500.json | 279 ++++++++++++++++++ 5 files changed, 291 insertions(+), 3 deletions(-) rename src/test/resources/cassettes/v2/{RetryTest.retryWithDashboardListItemGetTest.freeze => RetryTest.retryWithDashboardListItemGetTest429.freeze} (100%) rename src/test/resources/cassettes/v2/{RetryTest.retryWithDashboardListItemGetTest.json => RetryTest.retryWithDashboardListItemGetTest429.json} (98%) create mode 100644 src/test/resources/cassettes/v2/RetryTest.retryWithDashboardListItemGetTest500.freeze create mode 100644 src/test/resources/cassettes/v2/RetryTest.retryWithDashboardListItemGetTest500.json diff --git a/src/test/java/com/datadog/api/client/v2/api/RetryTest.java b/src/test/java/com/datadog/api/client/v2/api/RetryTest.java index 4209f998615..1c3c25e101f 100644 --- a/src/test/java/com/datadog/api/client/v2/api/RetryTest.java +++ b/src/test/java/com/datadog/api/client/v2/api/RetryTest.java @@ -31,7 +31,15 @@ public void createDashboardList() throws ApiException, NoSuchAlgorithmException } @Test - public void retryWithDashboardListItemGetTest() throws ApiException { + public void retryWithDashboardListItemGetTest429() throws ApiException { + DashboardListItems getResponse = api.getDashboardListItems(dashboardListID); + assertNotNull(getResponse.getTotal()); + assertEquals(0, (long) getResponse.getTotal()); + assertEquals(0, getResponse.getDashboards().size()); + } + + @Test + public void retryWithDashboardListItemGetTest500() throws ApiException { DashboardListItems getResponse = api.getDashboardListItems(dashboardListID); assertNotNull(getResponse.getTotal()); assertEquals(0, (long) getResponse.getTotal()); diff --git a/src/test/resources/cassettes/v2/RetryTest.retryWithDashboardListItemGetTest.freeze b/src/test/resources/cassettes/v2/RetryTest.retryWithDashboardListItemGetTest429.freeze similarity index 100% rename from src/test/resources/cassettes/v2/RetryTest.retryWithDashboardListItemGetTest.freeze rename to src/test/resources/cassettes/v2/RetryTest.retryWithDashboardListItemGetTest429.freeze diff --git a/src/test/resources/cassettes/v2/RetryTest.retryWithDashboardListItemGetTest.json b/src/test/resources/cassettes/v2/RetryTest.retryWithDashboardListItemGetTest429.json similarity index 98% rename from src/test/resources/cassettes/v2/RetryTest.retryWithDashboardListItemGetTest.json rename to src/test/resources/cassettes/v2/RetryTest.retryWithDashboardListItemGetTest429.json index 6ac58bfaf01..f91017c8887 100644 --- a/src/test/resources/cassettes/v2/RetryTest.retryWithDashboardListItemGetTest.json +++ b/src/test/resources/cassettes/v2/RetryTest.retryWithDashboardListItemGetTest429.json @@ -17,7 +17,7 @@ "contentType" : "application/json", "type" : "JSON", "json" : { - "name" : "java-retryWithDashboardListItemGetTest-local-1691492289" + "name" : "java-retryWithDashboardListItemGetTest429-local-1691492289" } } }, @@ -38,7 +38,7 @@ "type" : "JSON", "json" : { "is_favorite" : false, - "name" : "java-retryWithDashboardListItemGetTest-local-1691492289", + "name" : "java-retryWithDashboardListItemGetTest429-local-1691492289", "dashboard_count" : 0, "author" : { "handle" : "frog@datadoghq.com", diff --git a/src/test/resources/cassettes/v2/RetryTest.retryWithDashboardListItemGetTest500.freeze b/src/test/resources/cassettes/v2/RetryTest.retryWithDashboardListItemGetTest500.freeze new file mode 100644 index 00000000000..331bc9fe2ac --- /dev/null +++ b/src/test/resources/cassettes/v2/RetryTest.retryWithDashboardListItemGetTest500.freeze @@ -0,0 +1 @@ +2023-08-08T10:59:09.270Z \ No newline at end of file diff --git a/src/test/resources/cassettes/v2/RetryTest.retryWithDashboardListItemGetTest500.json b/src/test/resources/cassettes/v2/RetryTest.retryWithDashboardListItemGetTest500.json new file mode 100644 index 00000000000..672e2fa3f81 --- /dev/null +++ b/src/test/resources/cassettes/v2/RetryTest.retryWithDashboardListItemGetTest500.json @@ -0,0 +1,279 @@ +[{ + "httpRequest" : { + "method" : "POST", + "path" : "/api/v1/dashboard/lists/manual", + "headers" : { + "x-datadog-trace-id" : [ "7753420519099277221" ], + "x-datadog-origin" : [ "ciapp-test" ], + "content-length" : [ "58" ], + "Content-Type" : [ "application/json" ], + "Content-Language" : [ "" ], + "Accept-Encoding" : [ "deflate,gzip,x-gzip,zstd1" ], + "Accept" : [ "application/json" ] + }, + "keepAlive" : false, + "secure" : true, + "body" : { + "contentType" : "application/json", + "type" : "JSON", + "json" : { + "name" : "java-retryWithDashboardListItemGetTest500-local-1691492349" + } + } + }, + "httpResponse" : { + "statusCode" : 200, + "reasonPhrase" : "OK", + "headers" : { + "x-frame-options" : [ "SAMEORIGIN" ], + "x-content-type-options" : [ "nosniff" ], + "vary" : [ "Accept-Encoding" ], + "strict-transport-security" : [ "max-age=15724800;" ], + "content-security-policy" : [ "frame-ancestors 'self'; report-uri https://logs.browser-intake-datadoghq.com/api/v2/logs?dd-api-key=pube4f163c23bbf91c16b8f57f56af9fc58&dd-evp-origin=content-security-policy&ddsource=csp-report&ddtags=site%3Adatadoghq.com" ], + "Date" : [ "Mon, 17 Oct 2022 07:00:49 GMT" ], + "Content-Type" : [ "application/json" ] + }, + "body" : { + "contentType" : "application/json", + "type" : "JSON", + "json" : { + "is_favorite" : false, + "name" : "java-retryWithDashboardListItemGetTest500-local-1691492349", + "dashboard_count" : 0, + "author" : { + "handle" : "frog@datadoghq.com", + "name" : null + }, + "created" : "2022-10-17T07:00:49.090621+00:00", + "type" : "manual_dashboard_list", + "dashboards" : null, + "modified" : "2022-10-17T07:00:49.090633+00:00", + "id" : 334687 + } + } + }, + "id" : "c012f980-4ed1-4d58-8e99-fe28876ebf8a", + "priority" : 0, + "timeToLive" : { + "unlimited" : true + }, + "times" : { + "remainingTimes" : 1 + } + },{ + "httpRequest" : { + "method" : "GET", + "path" : "/api/v2/dashboard/lists/manual/334687/dashboards", + "headers" : { + "x-datadog-trace-id" : [ "3402781242333921307" ], + "x-datadog-origin" : [ "ciapp-test" ], + "content-length" : [ "0" ], + "Accept-Encoding" : [ "deflate,gzip,x-gzip,zstd1" ], + "Accept" : [ "application/json" ] + }, + "keepAlive" : false, + "secure" : true + }, + "httpResponse" : { + "statusCode" : 500, + "reasonPhrase" : "Internal Server Error", + "headers" : { + "x-frame-options" : [ "SAMEORIGIN" ], + "x-content-type-options" : [ "nosniff" ], + "x-ratelimit-reset" : [ "1" ], + "strict-transport-security" : [ "max-age=15724800;" ], + "content-security-policy" : [ "frame-ancestors 'self'; report-uri https://logs.browser-intake-datadoghq.com/api/v2/logs?dd-api-key=pube4f163c23bbf91c16b8f57f56af9fc58&dd-evp-origin=content-security-policy&ddsource=csp-report&ddtags=site%3Adatadoghq.com" ], + "Date" : [ "Mon, 17 Oct 2022 07:00:50 GMT" ], + "Content-Type" : [ "application/json" ] + }, + "body" : { + "contentType" : "application/json", + "type" : "JSON", + "json" : { + "total" : 0, + "dashboards" : [ ] + } + } + }, + "id" : "58cb9fa4-e67d-4fab-9466-4acf4c9a37a1", + "priority" : 0, + "timeToLive" : { + "unlimited" : true + }, + "times" : { + "remainingTimes" : 1 + } + },{ + "httpRequest" : { + "method" : "GET", + "path" : "/api/v2/dashboard/lists/manual/334687/dashboards", + "headers" : { + "x-datadog-trace-id" : [ "3402781242333921307" ], + "x-datadog-origin" : [ "ciapp-test" ], + "content-length" : [ "0" ], + "Accept-Encoding" : [ "deflate,gzip,x-gzip,zstd1" ], + "Accept" : [ "application/json" ] + }, + "keepAlive" : false, + "secure" : true + }, + "httpResponse" : { + "statusCode" : 500, + "reasonPhrase" : "Internal Server Error", + "headers" : { + "x-frame-options" : [ "SAMEORIGIN" ], + "x-content-type-options" : [ "nosniff" ], + "x-ratelimit-reset" : [ "1" ], + "strict-transport-security" : [ "max-age=15724800;" ], + "content-security-policy" : [ "frame-ancestors 'self'; report-uri https://logs.browser-intake-datadoghq.com/api/v2/logs?dd-api-key=pube4f163c23bbf91c16b8f57f56af9fc58&dd-evp-origin=content-security-policy&ddsource=csp-report&ddtags=site%3Adatadoghq.com" ], + "Date" : [ "Mon, 17 Oct 2022 07:00:50 GMT" ], + "Content-Type" : [ "application/json" ] + }, + "body" : { + "contentType" : "application/json", + "type" : "JSON", + "json" : { + "total" : 0, + "dashboards" : [ ] + } + } + }, + "id" : "58cb9fa4-e67d-4fab-9466-4acf4c9a37a1", + "priority" : 0, + "timeToLive" : { + "unlimited" : true + }, + "times" : { + "remainingTimes" : 1 + } + },{ + "httpRequest" : { + "method" : "GET", + "path" : "/api/v2/dashboard/lists/manual/334687/dashboards", + "headers" : { + "x-datadog-trace-id" : [ "3402781242333921307" ], + "x-datadog-origin" : [ "ciapp-test" ], + "content-length" : [ "0" ], + "Accept-Encoding" : [ "deflate,gzip,x-gzip,zstd1" ], + "Accept" : [ "application/json" ] + }, + "keepAlive" : false, + "secure" : true + }, + "httpResponse" : { + "statusCode" : 500, + "reasonPhrase" : "Internal Server Error", + "headers" : { + "x-frame-options" : [ "SAMEORIGIN" ], + "x-content-type-options" : [ "nosniff" ], + "x-ratelimit-reset" : [ "1" ], + "strict-transport-security" : [ "max-age=15724800;" ], + "content-security-policy" : [ "frame-ancestors 'self'; report-uri https://logs.browser-intake-datadoghq.com/api/v2/logs?dd-api-key=pube4f163c23bbf91c16b8f57f56af9fc58&dd-evp-origin=content-security-policy&ddsource=csp-report&ddtags=site%3Adatadoghq.com" ], + "Date" : [ "Mon, 17 Oct 2022 07:00:50 GMT" ], + "Content-Type" : [ "application/json" ] + }, + "body" : { + "contentType" : "application/json", + "type" : "JSON", + "json" : { + "total" : 0, + "dashboards" : [ ] + } + } + }, + "id" : "58cb9fa4-e67d-4fab-9466-4acf4c9a37a1", + "priority" : 0, + "timeToLive" : { + "unlimited" : true + }, + "times" : { + "remainingTimes" : 1 + } + } + + + ,{ + "httpRequest" : { + "method" : "GET", + "path" : "/api/v2/dashboard/lists/manual/334687/dashboards", + "headers" : { + "x-datadog-trace-id" : [ "3402781242333921307" ], + "x-datadog-origin" : [ "ciapp-test" ], + "content-length" : [ "0" ], + "Accept-Encoding" : [ "deflate,gzip,x-gzip,zstd1" ], + "Accept" : [ "application/json" ] + }, + "keepAlive" : false, + "secure" : true + }, + "httpResponse" : { + "statusCode" : 500, + "reasonPhrase" : "Internal Server Error", + "headers" : { + "x-frame-options" : [ "SAMEORIGIN" ], + "x-content-type-options" : [ "nosniff" ], + "strict-transport-security" : [ "max-age=15724800;" ], + "content-security-policy" : [ "frame-ancestors 'self'; report-uri https://logs.browser-intake-datadoghq.com/api/v2/logs?dd-api-key=pube4f163c23bbf91c16b8f57f56af9fc58&dd-evp-origin=content-security-policy&ddsource=csp-report&ddtags=site%3Adatadoghq.com" ], + "Date" : [ "Mon, 17 Oct 2022 07:00:50 GMT" ], + "Content-Type" : [ "application/json" ] + }, + "body" : { + "contentType" : "application/json", + "type" : "JSON", + "json" : { + "total" : 0, + "dashboards" : [ ] + } + } + }, + "id" : "58cb9fa4-e67d-4fab-9466-4acf4c9a39a1", + "priority" : 0, + "timeToLive" : { + "unlimited" : true + }, + "times" : { + "remainingTimes" : 1 + } + },{ + "httpRequest" : { + "method" : "GET", + "path" : "/api/v2/dashboard/lists/manual/334687/dashboards", + "headers" : { + "x-datadog-trace-id" : [ "3402781242333921307" ], + "x-datadog-origin" : [ "ciapp-test" ], + "content-length" : [ "0" ], + "Accept-Encoding" : [ "deflate,gzip,x-gzip,zstd1" ], + "Accept" : [ "application/json" ] + }, + "keepAlive" : false, + "secure" : true + }, + "httpResponse" : { + "statusCode" : 200, + "reasonPhrase" : "OK", + "headers" : { + "x-frame-options" : [ "SAMEORIGIN" ], + "x-content-type-options" : [ "nosniff" ], + "strict-transport-security" : [ "max-age=15724800;" ], + "content-security-policy" : [ "frame-ancestors 'self'; report-uri https://logs.browser-intake-datadoghq.com/api/v2/logs?dd-api-key=pube4f163c23bbf91c16b8f57f56af9fc58&dd-evp-origin=content-security-policy&ddsource=csp-report&ddtags=site%3Adatadoghq.com" ], + "Date" : [ "Mon, 17 Oct 2022 07:00:50 GMT" ], + "Content-Type" : [ "application/json" ] + }, + "body" : { + "contentType" : "application/json", + "type" : "JSON", + "json" : { + "total" : 0, + "dashboards" : [ ] + } + } + }, + "id" : "58cb9fa4-e67d-4fab-9466-4acf4c9a47a1", + "priority" : 0, + "timeToLive" : { + "unlimited" : true + }, + "times" : { + "remainingTimes" : 1 + } + }] \ No newline at end of file From 30dbb8d3708d3bd20f4630a99e16751213395518 Mon Sep 17 00:00:00 2001 From: HantingZhang2 Date: Wed, 9 Aug 2023 13:43:39 +0200 Subject: [PATCH 23/43] Add enable retry method on the client --- .generator/src/generator/templates/ApiClient.j2 | 9 +++++++++ src/main/java/com/datadog/api/client/ApiClient.java | 9 +++++++++ 2 files changed, 18 insertions(+) diff --git a/.generator/src/generator/templates/ApiClient.j2 b/.generator/src/generator/templates/ApiClient.j2 index 0f56967c97b..b803f17dfa9 100644 --- a/.generator/src/generator/templates/ApiClient.j2 +++ b/.generator/src/generator/templates/ApiClient.j2 @@ -298,6 +298,15 @@ public class ApiClient { this.retry = retry; } + /** + * Enable retry directly on the client instead of creating a new retry object + * @param enableRetry bool, enable retry or not + */ + + public void enableRetry(boolean enableRetry){ + this.retry.setEnableRetry(enableRetry); + } + /** * Set the date format used to parse/format {@code OffsetDateTime} parameters. * diff --git a/src/main/java/com/datadog/api/client/ApiClient.java b/src/main/java/com/datadog/api/client/ApiClient.java index b994fe3623c..1392e700b9c 100644 --- a/src/main/java/com/datadog/api/client/ApiClient.java +++ b/src/main/java/com/datadog/api/client/ApiClient.java @@ -527,6 +527,15 @@ public void setRetry(RetryConfig retry) { this.retry = retry; } + /** + * Enable retry directly on the client instead of creating a new retry object + * @param enableRetry bool, enable retry or not + */ + + public void enableRetry(boolean enableRetry){ + this.retry.setEnableRetry(enableRetry); + } + /** * Set the date format used to parse/format {@code OffsetDateTime} parameters. * From d952594b64eba3be41d6b029f876c64131af8b03 Mon Sep 17 00:00:00 2001 From: "ci.datadog-api-spec" Date: Wed, 9 Aug 2023 11:46:06 +0000 Subject: [PATCH 24/43] pre-commit fixes --- src/main/java/com/datadog/api/client/ApiClient.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/datadog/api/client/ApiClient.java b/src/main/java/com/datadog/api/client/ApiClient.java index 1392e700b9c..dcdc834bf9c 100644 --- a/src/main/java/com/datadog/api/client/ApiClient.java +++ b/src/main/java/com/datadog/api/client/ApiClient.java @@ -529,10 +529,10 @@ public void setRetry(RetryConfig retry) { /** * Enable retry directly on the client instead of creating a new retry object + * * @param enableRetry bool, enable retry or not */ - - public void enableRetry(boolean enableRetry){ + public void enableRetry(boolean enableRetry) { this.retry.setEnableRetry(enableRetry); } From 0c13e0af050ec9deab94f7fa0f3e1eb8d946dd2e Mon Sep 17 00:00:00 2001 From: HantingZhang2 Date: Wed, 9 Aug 2023 15:20:52 +0200 Subject: [PATCH 25/43] update retryConfig object and test casettes --- .../com/datadog/api/client/ApiClient.java | 5 +- .../com/datadog/api/client/RetryConfig.java | 20 ++++++++ ....retryWithDashboardListItemGetTest429.json | 44 ----------------- ....retryWithDashboardListItemGetTest500.json | 48 ------------------- 4 files changed, 23 insertions(+), 94 deletions(-) diff --git a/src/main/java/com/datadog/api/client/ApiClient.java b/src/main/java/com/datadog/api/client/ApiClient.java index dcdc834bf9c..cc3d71403c1 100644 --- a/src/main/java/com/datadog/api/client/ApiClient.java +++ b/src/main/java/com/datadog/api/client/ApiClient.java @@ -1560,7 +1560,8 @@ public ApiResponse invokeAPI( } } else if (shouldRetry(currentRetry, statusCode, retry)) { try { - Thread.sleep(calculateRetryInterval(responseHeaders, retry, currentRetry) * 1000); + retry.setCalculatedInterval(calculateRetryInterval(responseHeaders, retry, currentRetry)); + retry.sleepInterval(); currentRetry++; } catch (InterruptedException e) { Thread.currentThread().interrupt(); @@ -1596,7 +1597,7 @@ private boolean shouldRetry(int retryCount, int statusCode, RetryConfig retryCon if (statusCode == 429 || statusCode >= 500) { statusToRetry = true; } - return (retryConfig.maxRetries >= retryCount && statusToRetry && retryConfig.isEnableRetry()); + return (retryConfig.maxRetries > retryCount && statusToRetry && retryConfig.isEnableRetry()); } private int calculateRetryInterval( diff --git a/src/main/java/com/datadog/api/client/RetryConfig.java b/src/main/java/com/datadog/api/client/RetryConfig.java index a0c5c3f78a2..256054edc05 100644 --- a/src/main/java/com/datadog/api/client/RetryConfig.java +++ b/src/main/java/com/datadog/api/client/RetryConfig.java @@ -11,18 +11,21 @@ public class RetryConfig { public int backOffMultiplier; public int backOffBase; public int maxRetries; + public int calculatedInterval; /** * @param enableRetry Enable retry when rate limited * @param backOffMultiplier Multiplier for retry backoff * @param backOffBase Base for retry backoff * @param maxRetries Maximum number of retries + * @param calculatedInterval Calculated sleep interval */ public RetryConfig(boolean enableRetry, int backOffMultiplier, int backOffBase, int maxRetries) { this.enableRetry = enableRetry; this.backOffMultiplier = backOffMultiplier; this.backOffBase = backOffBase; this.maxRetries = maxRetries; + this.calculatedInterval = 0; } public boolean isEnableRetry() { @@ -41,6 +44,10 @@ public int getMaxRetries() { return maxRetries; } + public int getCalculatedInterval(){ + return calculatedInterval; + } + public void setEnableRetry(boolean enableRetry) { this.enableRetry = enableRetry; } @@ -56,4 +63,17 @@ public void setBackOffBase(int backOffBase) { public void setMaxRetries(int maxRetries) { this.maxRetries = maxRetries; } + + public void setCalculatedInterval(int calculatedInterval) { + this.calculatedInterval = calculatedInterval; + } + + public void sleepInterval() throws InterruptedException{ + try { + Thread.sleep(calculatedInterval * 1000 ); + } catch (InterruptedException e ){ + Thread.currentThread().interrupt(); + e.printStackTrace(); + } + } } diff --git a/src/test/resources/cassettes/v2/RetryTest.retryWithDashboardListItemGetTest429.json b/src/test/resources/cassettes/v2/RetryTest.retryWithDashboardListItemGetTest429.json index f91017c8887..258ec437242 100644 --- a/src/test/resources/cassettes/v2/RetryTest.retryWithDashboardListItemGetTest429.json +++ b/src/test/resources/cassettes/v2/RetryTest.retryWithDashboardListItemGetTest429.json @@ -190,51 +190,7 @@ "remainingTimes" : 1 } } - - ,{ - "httpRequest" : { - "method" : "GET", - "path" : "/api/v2/dashboard/lists/manual/334687/dashboards", - "headers" : { - "x-datadog-trace-id" : [ "3402781242333921307" ], - "x-datadog-origin" : [ "ciapp-test" ], - "content-length" : [ "0" ], - "Accept-Encoding" : [ "deflate,gzip,x-gzip,zstd1" ], - "Accept" : [ "application/json" ] - }, - "keepAlive" : false, - "secure" : true - }, - "httpResponse" : { - "statusCode" : 429, - "reasonPhrase" : "Too many requests", - "headers" : { - "x-frame-options" : [ "SAMEORIGIN" ], - "x-content-type-options" : [ "nosniff" ], - "strict-transport-security" : [ "max-age=15724800;" ], - "content-security-policy" : [ "frame-ancestors 'self'; report-uri https://logs.browser-intake-datadoghq.com/api/v2/logs?dd-api-key=pube4f163c23bbf91c16b8f57f56af9fc58&dd-evp-origin=content-security-policy&ddsource=csp-report&ddtags=site%3Adatadoghq.com" ], - "Date" : [ "Mon, 17 Oct 2022 07:00:50 GMT" ], - "Content-Type" : [ "application/json" ] - }, - "body" : { - "contentType" : "application/json", - "type" : "JSON", - "json" : { - "total" : 0, - "dashboards" : [ ] - } - } - }, - "id" : "58cb9fa4-e67d-4fab-9466-4acf4c9a39a1", - "priority" : 0, - "timeToLive" : { - "unlimited" : true - }, - "times" : { - "remainingTimes" : 1 - } - },{ "httpRequest" : { "method" : "GET", "path" : "/api/v2/dashboard/lists/manual/334687/dashboards", diff --git a/src/test/resources/cassettes/v2/RetryTest.retryWithDashboardListItemGetTest500.json b/src/test/resources/cassettes/v2/RetryTest.retryWithDashboardListItemGetTest500.json index 672e2fa3f81..956bffe5641 100644 --- a/src/test/resources/cassettes/v2/RetryTest.retryWithDashboardListItemGetTest500.json +++ b/src/test/resources/cassettes/v2/RetryTest.retryWithDashboardListItemGetTest500.json @@ -80,7 +80,6 @@ "headers" : { "x-frame-options" : [ "SAMEORIGIN" ], "x-content-type-options" : [ "nosniff" ], - "x-ratelimit-reset" : [ "1" ], "strict-transport-security" : [ "max-age=15724800;" ], "content-security-policy" : [ "frame-ancestors 'self'; report-uri https://logs.browser-intake-datadoghq.com/api/v2/logs?dd-api-key=pube4f163c23bbf91c16b8f57f56af9fc58&dd-evp-origin=content-security-policy&ddsource=csp-report&ddtags=site%3Adatadoghq.com" ], "Date" : [ "Mon, 17 Oct 2022 07:00:50 GMT" ], @@ -123,7 +122,6 @@ "headers" : { "x-frame-options" : [ "SAMEORIGIN" ], "x-content-type-options" : [ "nosniff" ], - "x-ratelimit-reset" : [ "1" ], "strict-transport-security" : [ "max-age=15724800;" ], "content-security-policy" : [ "frame-ancestors 'self'; report-uri https://logs.browser-intake-datadoghq.com/api/v2/logs?dd-api-key=pube4f163c23bbf91c16b8f57f56af9fc58&dd-evp-origin=content-security-policy&ddsource=csp-report&ddtags=site%3Adatadoghq.com" ], "Date" : [ "Mon, 17 Oct 2022 07:00:50 GMT" ], @@ -166,7 +164,6 @@ "headers" : { "x-frame-options" : [ "SAMEORIGIN" ], "x-content-type-options" : [ "nosniff" ], - "x-ratelimit-reset" : [ "1" ], "strict-transport-security" : [ "max-age=15724800;" ], "content-security-policy" : [ "frame-ancestors 'self'; report-uri https://logs.browser-intake-datadoghq.com/api/v2/logs?dd-api-key=pube4f163c23bbf91c16b8f57f56af9fc58&dd-evp-origin=content-security-policy&ddsource=csp-report&ddtags=site%3Adatadoghq.com" ], "Date" : [ "Mon, 17 Oct 2022 07:00:50 GMT" ], @@ -189,51 +186,6 @@ "times" : { "remainingTimes" : 1 } - } - - - ,{ - "httpRequest" : { - "method" : "GET", - "path" : "/api/v2/dashboard/lists/manual/334687/dashboards", - "headers" : { - "x-datadog-trace-id" : [ "3402781242333921307" ], - "x-datadog-origin" : [ "ciapp-test" ], - "content-length" : [ "0" ], - "Accept-Encoding" : [ "deflate,gzip,x-gzip,zstd1" ], - "Accept" : [ "application/json" ] - }, - "keepAlive" : false, - "secure" : true - }, - "httpResponse" : { - "statusCode" : 500, - "reasonPhrase" : "Internal Server Error", - "headers" : { - "x-frame-options" : [ "SAMEORIGIN" ], - "x-content-type-options" : [ "nosniff" ], - "strict-transport-security" : [ "max-age=15724800;" ], - "content-security-policy" : [ "frame-ancestors 'self'; report-uri https://logs.browser-intake-datadoghq.com/api/v2/logs?dd-api-key=pube4f163c23bbf91c16b8f57f56af9fc58&dd-evp-origin=content-security-policy&ddsource=csp-report&ddtags=site%3Adatadoghq.com" ], - "Date" : [ "Mon, 17 Oct 2022 07:00:50 GMT" ], - "Content-Type" : [ "application/json" ] - }, - "body" : { - "contentType" : "application/json", - "type" : "JSON", - "json" : { - "total" : 0, - "dashboards" : [ ] - } - } - }, - "id" : "58cb9fa4-e67d-4fab-9466-4acf4c9a39a1", - "priority" : 0, - "timeToLive" : { - "unlimited" : true - }, - "times" : { - "remainingTimes" : 1 - } },{ "httpRequest" : { "method" : "GET", From 1478c6757371837f065f08d464ff38e665118e96 Mon Sep 17 00:00:00 2001 From: "ci.datadog-api-spec" Date: Wed, 9 Aug 2023 13:22:58 +0000 Subject: [PATCH 26/43] pre-commit fixes --- .../com/datadog/api/client/ApiClient.java | 5 ++--- .../com/datadog/api/client/RetryConfig.java | 20 ------------------- 2 files changed, 2 insertions(+), 23 deletions(-) diff --git a/src/main/java/com/datadog/api/client/ApiClient.java b/src/main/java/com/datadog/api/client/ApiClient.java index cc3d71403c1..dcdc834bf9c 100644 --- a/src/main/java/com/datadog/api/client/ApiClient.java +++ b/src/main/java/com/datadog/api/client/ApiClient.java @@ -1560,8 +1560,7 @@ public ApiResponse invokeAPI( } } else if (shouldRetry(currentRetry, statusCode, retry)) { try { - retry.setCalculatedInterval(calculateRetryInterval(responseHeaders, retry, currentRetry)); - retry.sleepInterval(); + Thread.sleep(calculateRetryInterval(responseHeaders, retry, currentRetry) * 1000); currentRetry++; } catch (InterruptedException e) { Thread.currentThread().interrupt(); @@ -1597,7 +1596,7 @@ private boolean shouldRetry(int retryCount, int statusCode, RetryConfig retryCon if (statusCode == 429 || statusCode >= 500) { statusToRetry = true; } - return (retryConfig.maxRetries > retryCount && statusToRetry && retryConfig.isEnableRetry()); + return (retryConfig.maxRetries >= retryCount && statusToRetry && retryConfig.isEnableRetry()); } private int calculateRetryInterval( diff --git a/src/main/java/com/datadog/api/client/RetryConfig.java b/src/main/java/com/datadog/api/client/RetryConfig.java index 256054edc05..a0c5c3f78a2 100644 --- a/src/main/java/com/datadog/api/client/RetryConfig.java +++ b/src/main/java/com/datadog/api/client/RetryConfig.java @@ -11,21 +11,18 @@ public class RetryConfig { public int backOffMultiplier; public int backOffBase; public int maxRetries; - public int calculatedInterval; /** * @param enableRetry Enable retry when rate limited * @param backOffMultiplier Multiplier for retry backoff * @param backOffBase Base for retry backoff * @param maxRetries Maximum number of retries - * @param calculatedInterval Calculated sleep interval */ public RetryConfig(boolean enableRetry, int backOffMultiplier, int backOffBase, int maxRetries) { this.enableRetry = enableRetry; this.backOffMultiplier = backOffMultiplier; this.backOffBase = backOffBase; this.maxRetries = maxRetries; - this.calculatedInterval = 0; } public boolean isEnableRetry() { @@ -44,10 +41,6 @@ public int getMaxRetries() { return maxRetries; } - public int getCalculatedInterval(){ - return calculatedInterval; - } - public void setEnableRetry(boolean enableRetry) { this.enableRetry = enableRetry; } @@ -63,17 +56,4 @@ public void setBackOffBase(int backOffBase) { public void setMaxRetries(int maxRetries) { this.maxRetries = maxRetries; } - - public void setCalculatedInterval(int calculatedInterval) { - this.calculatedInterval = calculatedInterval; - } - - public void sleepInterval() throws InterruptedException{ - try { - Thread.sleep(calculatedInterval * 1000 ); - } catch (InterruptedException e ){ - Thread.currentThread().interrupt(); - e.printStackTrace(); - } - } } From d2cf66c628934069031c81f73ffa8d38f8ca5967 Mon Sep 17 00:00:00 2001 From: HantingZhang2 Date: Wed, 9 Aug 2023 16:49:45 +0200 Subject: [PATCH 27/43] Refactor tests and retry config --- .../src/generator/templates/ApiClient.j2 | 17 +++++++++------ .../src/generator/templates/RetryConfig.j2 | 21 ++++++++++++++++++- .../com/datadog/api/client/ApiClient.java | 12 ++++++++--- src/test/java/com/datadog/api/TestClient.java | 4 +++- .../datadog/api/client/v2/api/RetryTest.java | 2 ++ 5 files changed, 45 insertions(+), 11 deletions(-) diff --git a/.generator/src/generator/templates/ApiClient.j2 b/.generator/src/generator/templates/ApiClient.j2 index b803f17dfa9..1464aaa47dd 100644 --- a/.generator/src/generator/templates/ApiClient.j2 +++ b/.generator/src/generator/templates/ApiClient.j2 @@ -298,11 +298,18 @@ public class ApiClient { this.retry = retry; } - /** - * Enable retry directly on the client instead of creating a new retry object - * @param enableRetry bool, enable retry or not + /** + * Return the retryConfig object + * @return retryConfig */ + public RetryConfig getRetry() { + return retry; + } + /** + * Enable retry directly on the client instead of creating a new retry object + * @param enableRetry bool, enable retry or not + */ public void enableRetry(boolean enableRetry){ this.retry.setEnableRetry(enableRetry); } @@ -1316,9 +1323,7 @@ public class ApiClient { private int calculateRetryInterval(Map> responseHeaders, RetryConfig retryConfig, int retryCount){ if ( responseHeaders.get("x-ratelimit-reset")!=null){ List rateLimitHeader = responseHeaders.get("x-ratelimit-reset"); - String ratelimitString= rateLimitHeader.get(0); - String ratelimitStringTrimmed = ratelimitString.replaceAll("\\D",""); - return Integer.parseInt(ratelimitStringTrimmed); + return Integer.parseInt(rateLimitHeader.get(0)); } else { int retryInterval= (int) Math.pow (retry.backOffMultiplier, retryCount)* retryConfig.backOffBase; if (getConnectTimeout()>0){ diff --git a/.generator/src/generator/templates/RetryConfig.j2 b/.generator/src/generator/templates/RetryConfig.j2 index 1bcf8c8d29b..1c5946b35ea 100644 --- a/.generator/src/generator/templates/RetryConfig.j2 +++ b/.generator/src/generator/templates/RetryConfig.j2 @@ -7,13 +7,14 @@ public class RetryConfig { public int backOffMultiplier; public int backOffBase; public int maxRetries; - + public int calculatedInterval; /** * @param enableRetry Enable retry when rate limited * @param backOffMultiplier Multiplier for retry backoff * @param backOffBase Base for retry backoff * @param maxRetries Maximum number of retries + * @param calculatedInterval Calculated sleep interval */ public RetryConfig(boolean enableRetry, int backOffMultiplier, int backOffBase, int maxRetries) { @@ -21,6 +22,7 @@ public class RetryConfig { this.backOffMultiplier = backOffMultiplier; this.backOffBase = backOffBase; this.maxRetries = maxRetries; + this.calculatedInterval = 0; } public boolean isEnableRetry() { @@ -39,6 +41,10 @@ public class RetryConfig { return maxRetries; } + public int getCalculatedInterval(){ + return calculatedInterval; + } + public void setEnableRetry(boolean enableRetry) { this.enableRetry = enableRetry; } @@ -54,4 +60,17 @@ public class RetryConfig { this.maxRetries = maxRetries; } + public void setCalculatedInterval(int calculatedInterval) { + this.calculatedInterval = calculatedInterval; + } + + public void sleepInterval() throws InterruptedException{ + try { + Thread.sleep(calculatedInterval * 1000 ); + } catch (InterruptedException e ){ + Thread.currentThread().interrupt(); + e.printStackTrace(); + } + } + } \ No newline at end of file diff --git a/src/main/java/com/datadog/api/client/ApiClient.java b/src/main/java/com/datadog/api/client/ApiClient.java index dcdc834bf9c..a43762ae096 100644 --- a/src/main/java/com/datadog/api/client/ApiClient.java +++ b/src/main/java/com/datadog/api/client/ApiClient.java @@ -527,6 +527,14 @@ public void setRetry(RetryConfig retry) { this.retry = retry; } + /** + * Return the retryConfig object + * @return retryConfig + */ + public RetryConfig getRetry() { + return retry; + } + /** * Enable retry directly on the client instead of creating a new retry object * @@ -1603,9 +1611,7 @@ private int calculateRetryInterval( Map> responseHeaders, RetryConfig retryConfig, int retryCount) { if (responseHeaders.get("x-ratelimit-reset") != null) { List rateLimitHeader = responseHeaders.get("x-ratelimit-reset"); - String ratelimitString = rateLimitHeader.get(0); - String ratelimitStringTrimmed = ratelimitString.replaceAll("\\D", ""); - return Integer.parseInt(ratelimitStringTrimmed); + return Integer.parseInt(rateLimitHeader.get(0)); } else { int retryInterval = (int) Math.pow(retry.backOffMultiplier, retryCount) * retryConfig.backOffBase; diff --git a/src/test/java/com/datadog/api/TestClient.java b/src/test/java/com/datadog/api/TestClient.java index ab9432bc083..e971b217e72 100644 --- a/src/test/java/com/datadog/api/TestClient.java +++ b/src/test/java/com/datadog/api/TestClient.java @@ -658,7 +658,9 @@ public MultivaluedMap getMetadata() { public MultivaluedMap getHeaders() { MultivaluedMap newHeaders = new MultivaluedHashMap(); for (Map.Entry> entry : this.headers.entrySet()) { - newHeaders.addAll(entry.getKey(), entry.getValue()); + for (Object o: entry.getValue()){ + newHeaders.add(entry.getKey(),o); + } } return newHeaders; } diff --git a/src/test/java/com/datadog/api/client/v2/api/RetryTest.java b/src/test/java/com/datadog/api/client/v2/api/RetryTest.java index 1c3c25e101f..341cd5ba494 100644 --- a/src/test/java/com/datadog/api/client/v2/api/RetryTest.java +++ b/src/test/java/com/datadog/api/client/v2/api/RetryTest.java @@ -33,6 +33,7 @@ public void createDashboardList() throws ApiException, NoSuchAlgorithmException @Test public void retryWithDashboardListItemGetTest429() throws ApiException { DashboardListItems getResponse = api.getDashboardListItems(dashboardListID); + assertEquals(1,generalApiClient.getRetry().getCalculatedInterval()); assertNotNull(getResponse.getTotal()); assertEquals(0, (long) getResponse.getTotal()); assertEquals(0, getResponse.getDashboards().size()); @@ -41,6 +42,7 @@ public void retryWithDashboardListItemGetTest429() throws ApiException { @Test public void retryWithDashboardListItemGetTest500() throws ApiException { DashboardListItems getResponse = api.getDashboardListItems(dashboardListID); + assertEquals(8,generalApiClient.getRetry().getCalculatedInterval()); assertNotNull(getResponse.getTotal()); assertEquals(0, (long) getResponse.getTotal()); assertEquals(0, getResponse.getDashboards().size()); From 9e1b7050f9e274c2b890fa971a168edab36bce94 Mon Sep 17 00:00:00 2001 From: "ci.datadog-api-spec" Date: Wed, 9 Aug 2023 14:51:47 +0000 Subject: [PATCH 28/43] pre-commit fixes --- .../com/datadog/api/client/ApiClient.java | 3 ++- .../com/datadog/api/client/RetryConfig.java | 20 +++++++++++++++++++ src/test/java/com/datadog/api/TestClient.java | 4 ++-- .../datadog/api/client/v2/api/RetryTest.java | 4 ++-- 4 files changed, 26 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/datadog/api/client/ApiClient.java b/src/main/java/com/datadog/api/client/ApiClient.java index a43762ae096..9e9f5ecc4c2 100644 --- a/src/main/java/com/datadog/api/client/ApiClient.java +++ b/src/main/java/com/datadog/api/client/ApiClient.java @@ -528,7 +528,8 @@ public void setRetry(RetryConfig retry) { } /** - * Return the retryConfig object + * Return the retryConfig object + * * @return retryConfig */ public RetryConfig getRetry() { diff --git a/src/main/java/com/datadog/api/client/RetryConfig.java b/src/main/java/com/datadog/api/client/RetryConfig.java index a0c5c3f78a2..3a02a073764 100644 --- a/src/main/java/com/datadog/api/client/RetryConfig.java +++ b/src/main/java/com/datadog/api/client/RetryConfig.java @@ -11,18 +11,21 @@ public class RetryConfig { public int backOffMultiplier; public int backOffBase; public int maxRetries; + public int calculatedInterval; /** * @param enableRetry Enable retry when rate limited * @param backOffMultiplier Multiplier for retry backoff * @param backOffBase Base for retry backoff * @param maxRetries Maximum number of retries + * @param calculatedInterval Calculated sleep interval */ public RetryConfig(boolean enableRetry, int backOffMultiplier, int backOffBase, int maxRetries) { this.enableRetry = enableRetry; this.backOffMultiplier = backOffMultiplier; this.backOffBase = backOffBase; this.maxRetries = maxRetries; + this.calculatedInterval = 0; } public boolean isEnableRetry() { @@ -41,6 +44,10 @@ public int getMaxRetries() { return maxRetries; } + public int getCalculatedInterval() { + return calculatedInterval; + } + public void setEnableRetry(boolean enableRetry) { this.enableRetry = enableRetry; } @@ -56,4 +63,17 @@ public void setBackOffBase(int backOffBase) { public void setMaxRetries(int maxRetries) { this.maxRetries = maxRetries; } + + public void setCalculatedInterval(int calculatedInterval) { + this.calculatedInterval = calculatedInterval; + } + + public void sleepInterval() throws InterruptedException { + try { + Thread.sleep(calculatedInterval * 1000); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + e.printStackTrace(); + } + } } diff --git a/src/test/java/com/datadog/api/TestClient.java b/src/test/java/com/datadog/api/TestClient.java index e971b217e72..90219730010 100644 --- a/src/test/java/com/datadog/api/TestClient.java +++ b/src/test/java/com/datadog/api/TestClient.java @@ -658,8 +658,8 @@ public MultivaluedMap getMetadata() { public MultivaluedMap getHeaders() { MultivaluedMap newHeaders = new MultivaluedHashMap(); for (Map.Entry> entry : this.headers.entrySet()) { - for (Object o: entry.getValue()){ - newHeaders.add(entry.getKey(),o); + for (Object o : entry.getValue()) { + newHeaders.add(entry.getKey(), o); } } return newHeaders; diff --git a/src/test/java/com/datadog/api/client/v2/api/RetryTest.java b/src/test/java/com/datadog/api/client/v2/api/RetryTest.java index 341cd5ba494..65874edad3a 100644 --- a/src/test/java/com/datadog/api/client/v2/api/RetryTest.java +++ b/src/test/java/com/datadog/api/client/v2/api/RetryTest.java @@ -33,7 +33,7 @@ public void createDashboardList() throws ApiException, NoSuchAlgorithmException @Test public void retryWithDashboardListItemGetTest429() throws ApiException { DashboardListItems getResponse = api.getDashboardListItems(dashboardListID); - assertEquals(1,generalApiClient.getRetry().getCalculatedInterval()); + assertEquals(1, generalApiClient.getRetry().getCalculatedInterval()); assertNotNull(getResponse.getTotal()); assertEquals(0, (long) getResponse.getTotal()); assertEquals(0, getResponse.getDashboards().size()); @@ -42,7 +42,7 @@ public void retryWithDashboardListItemGetTest429() throws ApiException { @Test public void retryWithDashboardListItemGetTest500() throws ApiException { DashboardListItems getResponse = api.getDashboardListItems(dashboardListID); - assertEquals(8,generalApiClient.getRetry().getCalculatedInterval()); + assertEquals(8, generalApiClient.getRetry().getCalculatedInterval()); assertNotNull(getResponse.getTotal()); assertEquals(0, (long) getResponse.getTotal()); assertEquals(0, getResponse.getDashboards().size()); From b664dd8263940eacf480ca5f0800c65310659d77 Mon Sep 17 00:00:00 2001 From: HantingZhang2 Date: Wed, 9 Aug 2023 16:58:33 +0200 Subject: [PATCH 29/43] change test assert --- src/test/java/com/datadog/api/client/v2/api/RetryTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/java/com/datadog/api/client/v2/api/RetryTest.java b/src/test/java/com/datadog/api/client/v2/api/RetryTest.java index 65874edad3a..50fc422ee32 100644 --- a/src/test/java/com/datadog/api/client/v2/api/RetryTest.java +++ b/src/test/java/com/datadog/api/client/v2/api/RetryTest.java @@ -33,7 +33,7 @@ public void createDashboardList() throws ApiException, NoSuchAlgorithmException @Test public void retryWithDashboardListItemGetTest429() throws ApiException { DashboardListItems getResponse = api.getDashboardListItems(dashboardListID); - assertEquals(1, generalApiClient.getRetry().getCalculatedInterval()); + assertEquals(1, generalApiClientWithRetry.getRetry().getCalculatedInterval()); assertNotNull(getResponse.getTotal()); assertEquals(0, (long) getResponse.getTotal()); assertEquals(0, getResponse.getDashboards().size()); @@ -42,7 +42,7 @@ public void retryWithDashboardListItemGetTest429() throws ApiException { @Test public void retryWithDashboardListItemGetTest500() throws ApiException { DashboardListItems getResponse = api.getDashboardListItems(dashboardListID); - assertEquals(8, generalApiClient.getRetry().getCalculatedInterval()); + assertEquals(8, generalApiClientWithRetry.getRetry().getCalculatedInterval()); assertNotNull(getResponse.getTotal()); assertEquals(0, (long) getResponse.getTotal()); assertEquals(0, getResponse.getDashboards().size()); From e27c5e35ef99544b86a438107d71e6931cafdb06 Mon Sep 17 00:00:00 2001 From: HantingZhang2 Date: Wed, 9 Aug 2023 17:05:20 +0200 Subject: [PATCH 30/43] add back sleep method using retry object --- .generator/src/generator/templates/ApiClient.j2 | 3 ++- src/main/java/com/datadog/api/client/ApiClient.java | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/.generator/src/generator/templates/ApiClient.j2 b/.generator/src/generator/templates/ApiClient.j2 index 1464aaa47dd..f7b30d91f5e 100644 --- a/.generator/src/generator/templates/ApiClient.j2 +++ b/.generator/src/generator/templates/ApiClient.j2 @@ -1281,7 +1281,8 @@ public class ApiClient { } } else if (shouldRetry(currentRetry, statusCode, retry)){ try{ - Thread.sleep(calculateRetryInterval(responseHeaders, retry, currentRetry)*1000); + retry.setCalculatedInterval(calculateRetryInterval(responseHeaders, retry, currentRetry)); + retry.sleepInterval(); currentRetry++; } catch ( InterruptedException e){ Thread.currentThread().interrupt(); diff --git a/src/main/java/com/datadog/api/client/ApiClient.java b/src/main/java/com/datadog/api/client/ApiClient.java index 9e9f5ecc4c2..f469c46a3a8 100644 --- a/src/main/java/com/datadog/api/client/ApiClient.java +++ b/src/main/java/com/datadog/api/client/ApiClient.java @@ -1569,7 +1569,8 @@ public ApiResponse invokeAPI( } } else if (shouldRetry(currentRetry, statusCode, retry)) { try { - Thread.sleep(calculateRetryInterval(responseHeaders, retry, currentRetry) * 1000); + retry.setCalculatedInterval(calculateRetryInterval(responseHeaders, retry, currentRetry)); + retry.sleepInterval(); currentRetry++; } catch (InterruptedException e) { Thread.currentThread().interrupt(); From caa432b71ef44009fb2b9ece0b658df43e204532 Mon Sep 17 00:00:00 2001 From: "ci.datadog-api-spec" Date: Wed, 9 Aug 2023 15:07:33 +0000 Subject: [PATCH 31/43] pre-commit fixes --- src/main/java/com/datadog/api/client/ApiClient.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/datadog/api/client/ApiClient.java b/src/main/java/com/datadog/api/client/ApiClient.java index f469c46a3a8..0de73a54b7e 100644 --- a/src/main/java/com/datadog/api/client/ApiClient.java +++ b/src/main/java/com/datadog/api/client/ApiClient.java @@ -1569,7 +1569,8 @@ public ApiResponse invokeAPI( } } else if (shouldRetry(currentRetry, statusCode, retry)) { try { - retry.setCalculatedInterval(calculateRetryInterval(responseHeaders, retry, currentRetry)); + retry.setCalculatedInterval( + calculateRetryInterval(responseHeaders, retry, currentRetry)); retry.sleepInterval(); currentRetry++; } catch (InterruptedException e) { From ae5600c83db063ebcebe02c7f0cc43d70d537650 Mon Sep 17 00:00:00 2001 From: HantingZhang2 Date: Wed, 9 Aug 2023 17:30:31 +0200 Subject: [PATCH 32/43] refresh jdoc --- src/main/java/com/datadog/api/client/RetryConfig.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/com/datadog/api/client/RetryConfig.java b/src/main/java/com/datadog/api/client/RetryConfig.java index 3a02a073764..5c99e13fa67 100644 --- a/src/main/java/com/datadog/api/client/RetryConfig.java +++ b/src/main/java/com/datadog/api/client/RetryConfig.java @@ -12,6 +12,7 @@ public class RetryConfig { public int backOffBase; public int maxRetries; public int calculatedInterval; + /** * @param enableRetry Enable retry when rate limited From 533fb450e5d8d64c9afc7cd282b6790ec4559033 Mon Sep 17 00:00:00 2001 From: "ci.datadog-api-spec" Date: Wed, 9 Aug 2023 15:32:28 +0000 Subject: [PATCH 33/43] pre-commit fixes --- src/main/java/com/datadog/api/client/RetryConfig.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/com/datadog/api/client/RetryConfig.java b/src/main/java/com/datadog/api/client/RetryConfig.java index 5c99e13fa67..3a02a073764 100644 --- a/src/main/java/com/datadog/api/client/RetryConfig.java +++ b/src/main/java/com/datadog/api/client/RetryConfig.java @@ -12,7 +12,6 @@ public class RetryConfig { public int backOffBase; public int maxRetries; public int calculatedInterval; - /** * @param enableRetry Enable retry when rate limited From 4352fc2d9e5b014872d347cb4f004fac8bbe5762 Mon Sep 17 00:00:00 2001 From: HantingZhang2 Date: Wed, 9 Aug 2023 17:35:47 +0200 Subject: [PATCH 34/43] Remove param for jdoc --- .generator/src/generator/templates/RetryConfig.j2 | 1 - src/main/java/com/datadog/api/client/RetryConfig.java | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/.generator/src/generator/templates/RetryConfig.j2 b/.generator/src/generator/templates/RetryConfig.j2 index 1c5946b35ea..024ccf8ba76 100644 --- a/.generator/src/generator/templates/RetryConfig.j2 +++ b/.generator/src/generator/templates/RetryConfig.j2 @@ -14,7 +14,6 @@ public class RetryConfig { * @param backOffMultiplier Multiplier for retry backoff * @param backOffBase Base for retry backoff * @param maxRetries Maximum number of retries - * @param calculatedInterval Calculated sleep interval */ public RetryConfig(boolean enableRetry, int backOffMultiplier, int backOffBase, int maxRetries) { diff --git a/src/main/java/com/datadog/api/client/RetryConfig.java b/src/main/java/com/datadog/api/client/RetryConfig.java index 3a02a073764..715da9f326e 100644 --- a/src/main/java/com/datadog/api/client/RetryConfig.java +++ b/src/main/java/com/datadog/api/client/RetryConfig.java @@ -18,8 +18,8 @@ public class RetryConfig { * @param backOffMultiplier Multiplier for retry backoff * @param backOffBase Base for retry backoff * @param maxRetries Maximum number of retries - * @param calculatedInterval Calculated sleep interval */ + public RetryConfig(boolean enableRetry, int backOffMultiplier, int backOffBase, int maxRetries) { this.enableRetry = enableRetry; this.backOffMultiplier = backOffMultiplier; From a913feb6813d53ad43fa7b9c35971ad5151956b6 Mon Sep 17 00:00:00 2001 From: "ci.datadog-api-spec" Date: Wed, 9 Aug 2023 15:38:00 +0000 Subject: [PATCH 35/43] pre-commit fixes --- src/main/java/com/datadog/api/client/RetryConfig.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/com/datadog/api/client/RetryConfig.java b/src/main/java/com/datadog/api/client/RetryConfig.java index 715da9f326e..596a9ef2124 100644 --- a/src/main/java/com/datadog/api/client/RetryConfig.java +++ b/src/main/java/com/datadog/api/client/RetryConfig.java @@ -19,7 +19,6 @@ public class RetryConfig { * @param backOffBase Base for retry backoff * @param maxRetries Maximum number of retries */ - public RetryConfig(boolean enableRetry, int backOffMultiplier, int backOffBase, int maxRetries) { this.enableRetry = enableRetry; this.backOffMultiplier = backOffMultiplier; From 9aef306ea77546ba9114a8278fcc6530188b0634 Mon Sep 17 00:00:00 2001 From: HantingZhang2 Date: Thu, 10 Aug 2023 14:00:53 +0200 Subject: [PATCH 36/43] Add mock retry config and simplify the sleep --- .../src/generator/templates/ApiClient.j2 | 3 +-- .../src/generator/templates/RetryConfig.j2 | 8 +++---- .../com/datadog/api/client/ApiClient.java | 4 +--- .../com/datadog/api/client/RetryConfig.java | 5 +++-- .../java/com/datadog/api/MockRetryConfig.java | 21 +++++++++++++++++++ .../datadog/api/client/v2/api/V2APITest.java | 5 +++-- 6 files changed, 33 insertions(+), 13 deletions(-) create mode 100644 src/test/java/com/datadog/api/MockRetryConfig.java diff --git a/.generator/src/generator/templates/ApiClient.j2 b/.generator/src/generator/templates/ApiClient.j2 index f7b30d91f5e..bf02ee55d6d 100644 --- a/.generator/src/generator/templates/ApiClient.j2 +++ b/.generator/src/generator/templates/ApiClient.j2 @@ -1281,8 +1281,7 @@ public class ApiClient { } } else if (shouldRetry(currentRetry, statusCode, retry)){ try{ - retry.setCalculatedInterval(calculateRetryInterval(responseHeaders, retry, currentRetry)); - retry.sleepInterval(); + retry.sleepInterval(calculateRetryInterval(responseHeaders, retry, currentRetry)); currentRetry++; } catch ( InterruptedException e){ Thread.currentThread().interrupt(); diff --git a/.generator/src/generator/templates/RetryConfig.j2 b/.generator/src/generator/templates/RetryConfig.j2 index 024ccf8ba76..ee61f6d2314 100644 --- a/.generator/src/generator/templates/RetryConfig.j2 +++ b/.generator/src/generator/templates/RetryConfig.j2 @@ -63,13 +63,13 @@ public class RetryConfig { this.calculatedInterval = calculatedInterval; } - public void sleepInterval() throws InterruptedException{ + public void sleepInterval(int sleepInterval) throws InterruptedException { try { - Thread.sleep(calculatedInterval * 1000 ); - } catch (InterruptedException e ){ + setCalculatedInterval(sleepInterval); + Thread.sleep(sleepInterval * 1000); + } catch (InterruptedException e) { Thread.currentThread().interrupt(); e.printStackTrace(); } } - } \ No newline at end of file diff --git a/src/main/java/com/datadog/api/client/ApiClient.java b/src/main/java/com/datadog/api/client/ApiClient.java index 0de73a54b7e..2b5c69e51bb 100644 --- a/src/main/java/com/datadog/api/client/ApiClient.java +++ b/src/main/java/com/datadog/api/client/ApiClient.java @@ -1569,9 +1569,7 @@ public ApiResponse invokeAPI( } } else if (shouldRetry(currentRetry, statusCode, retry)) { try { - retry.setCalculatedInterval( - calculateRetryInterval(responseHeaders, retry, currentRetry)); - retry.sleepInterval(); + retry.sleepInterval(calculateRetryInterval(responseHeaders, retry, currentRetry)); currentRetry++; } catch (InterruptedException e) { Thread.currentThread().interrupt(); diff --git a/src/main/java/com/datadog/api/client/RetryConfig.java b/src/main/java/com/datadog/api/client/RetryConfig.java index 596a9ef2124..7f031fbb354 100644 --- a/src/main/java/com/datadog/api/client/RetryConfig.java +++ b/src/main/java/com/datadog/api/client/RetryConfig.java @@ -67,9 +67,10 @@ public void setCalculatedInterval(int calculatedInterval) { this.calculatedInterval = calculatedInterval; } - public void sleepInterval() throws InterruptedException { + public void sleepInterval(int sleepInterval) throws InterruptedException { try { - Thread.sleep(calculatedInterval * 1000); + setCalculatedInterval(sleepInterval); + Thread.sleep(sleepInterval * 1000); } catch (InterruptedException e) { Thread.currentThread().interrupt(); e.printStackTrace(); diff --git a/src/test/java/com/datadog/api/MockRetryConfig.java b/src/test/java/com/datadog/api/MockRetryConfig.java new file mode 100644 index 00000000000..79a4e7b7b09 --- /dev/null +++ b/src/test/java/com/datadog/api/MockRetryConfig.java @@ -0,0 +1,21 @@ +package com.datadog.api; + +import com.datadog.api.client.RetryConfig; + +public class MockRetryConfig extends RetryConfig { + + public MockRetryConfig(boolean enableRetry, int backOffMultiplier, int backOffBase, int maxRetries) { + super(enableRetry, backOffMultiplier, backOffBase, maxRetries); + } + + @Override + public void sleepInterval(int sleepInterval) throws InterruptedException { + try { + setCalculatedInterval(sleepInterval); + Thread.sleep(1); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + e.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/src/test/java/com/datadog/api/client/v2/api/V2APITest.java b/src/test/java/com/datadog/api/client/v2/api/V2APITest.java index 8e37445dedf..b1e67536c52 100644 --- a/src/test/java/com/datadog/api/client/v2/api/V2APITest.java +++ b/src/test/java/com/datadog/api/client/v2/api/V2APITest.java @@ -6,6 +6,7 @@ package com.datadog.api.client.v2.api; +import com.datadog.api.MockRetryConfig; import com.datadog.api.RecordingMode; import com.datadog.api.TestClient; import com.datadog.api.TestUtils; @@ -34,10 +35,10 @@ public abstract class V2APITest extends TestUtils.APITest { protected static ApiClient generalApiClientWithRetry; @BeforeClass - public static void initGeneralApiClientWithRetry() { + public static void initGeneralApiClientWithMockRetry() { initGeneralApiClient(); generalApiClientWithRetry = generalApiClient; - generalApiClientWithRetry.setRetry(new RetryConfig(true, 2, 2, 3)); + generalApiClientWithRetry.setRetry(new MockRetryConfig(true, 2, 2, 3)); } @BeforeClass From 999073d19bbc6abaf3c7987dffe87d77284d3ec7 Mon Sep 17 00:00:00 2001 From: "ci.datadog-api-spec" Date: Thu, 10 Aug 2023 12:03:14 +0000 Subject: [PATCH 37/43] pre-commit fixes --- src/test/java/com/datadog/api/MockRetryConfig.java | 5 +++-- src/test/java/com/datadog/api/client/v2/api/V2APITest.java | 1 - 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/test/java/com/datadog/api/MockRetryConfig.java b/src/test/java/com/datadog/api/MockRetryConfig.java index 79a4e7b7b09..f627beb8386 100644 --- a/src/test/java/com/datadog/api/MockRetryConfig.java +++ b/src/test/java/com/datadog/api/MockRetryConfig.java @@ -4,7 +4,8 @@ public class MockRetryConfig extends RetryConfig { - public MockRetryConfig(boolean enableRetry, int backOffMultiplier, int backOffBase, int maxRetries) { + public MockRetryConfig( + boolean enableRetry, int backOffMultiplier, int backOffBase, int maxRetries) { super(enableRetry, backOffMultiplier, backOffBase, maxRetries); } @@ -18,4 +19,4 @@ public void sleepInterval(int sleepInterval) throws InterruptedException { e.printStackTrace(); } } -} \ No newline at end of file +} diff --git a/src/test/java/com/datadog/api/client/v2/api/V2APITest.java b/src/test/java/com/datadog/api/client/v2/api/V2APITest.java index b1e67536c52..e569c3e85f0 100644 --- a/src/test/java/com/datadog/api/client/v2/api/V2APITest.java +++ b/src/test/java/com/datadog/api/client/v2/api/V2APITest.java @@ -14,7 +14,6 @@ import com.datadog.api.client.ApiException; import com.datadog.api.client.ApiResponse; import com.datadog.api.client.Pair; -import com.datadog.api.client.RetryConfig; import jakarta.ws.rs.client.Client; import jakarta.ws.rs.client.Invocation; import jakarta.ws.rs.core.GenericType; From 5f4938f927b2986e52f5898c46d8d31c1ffaa355 Mon Sep 17 00:00:00 2001 From: HantingZhang2 Date: Fri, 11 Aug 2023 12:45:36 +0200 Subject: [PATCH 38/43] fix for comments --- .generator/src/generator/templates/ApiClient.j2 | 7 +------ README.md | 3 +-- src/main/java/com/datadog/api/client/ApiClient.java | 7 +------ src/main/java/com/datadog/api/client/RetryConfig.java | 8 ++------ src/test/java/com/datadog/api/MockRetryConfig.java | 4 ++-- 5 files changed, 7 insertions(+), 22 deletions(-) diff --git a/.generator/src/generator/templates/ApiClient.j2 b/.generator/src/generator/templates/ApiClient.j2 index bf02ee55d6d..f982e361f72 100644 --- a/.generator/src/generator/templates/ApiClient.j2 +++ b/.generator/src/generator/templates/ApiClient.j2 @@ -1280,13 +1280,8 @@ public class ApiClient { return new ApiResponse(statusCode, responseHeaders, deserialize(response, returnType)); } } else if (shouldRetry(currentRetry, statusCode, retry)){ - try{ retry.sleepInterval(calculateRetryInterval(responseHeaders, retry, currentRetry)); currentRetry++; - } catch ( InterruptedException e){ - Thread.currentThread().interrupt(); - e.printStackTrace(); - } } else { String message = "error"; String respBody = null; @@ -1317,7 +1312,7 @@ public class ApiClient { if (statusCode == 429 || statusCode >= 500){ statusToRetry = true; } - return (retryConfig.maxRetries>=retryCount && statusToRetry && retryConfig.isEnableRetry()); + return (retryConfig.maxRetries>retryCount && statusToRetry && retryConfig.isEnableRetry()); } private int calculateRetryInterval(Map> responseHeaders, RetryConfig retryConfig, int retryCount){ diff --git a/README.md b/README.md index c14cf7b7fd5..6241bf71596 100644 --- a/README.md +++ b/README.md @@ -173,9 +173,8 @@ defaultClient.setDebugging(true) To enable the client to retry when rate limited (status 429) or status 500 and above: ```java -defaultClient.setRetry(new RetryConfig(true, int , int ,int )) +defaultClient.enableRetry(true) ``` - The interval between 2 retry attempts will be the value of the `x-ratelimit-reset` response header when available. If not, it will be : `Math.pow (multiplier_for_retry_backoff, current_retry_count)*base_for_retry_backoff`. diff --git a/src/main/java/com/datadog/api/client/ApiClient.java b/src/main/java/com/datadog/api/client/ApiClient.java index 2b5c69e51bb..ae9bc10ad84 100644 --- a/src/main/java/com/datadog/api/client/ApiClient.java +++ b/src/main/java/com/datadog/api/client/ApiClient.java @@ -1568,13 +1568,8 @@ public ApiResponse invokeAPI( statusCode, responseHeaders, deserialize(response, returnType)); } } else if (shouldRetry(currentRetry, statusCode, retry)) { - try { retry.sleepInterval(calculateRetryInterval(responseHeaders, retry, currentRetry)); currentRetry++; - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - e.printStackTrace(); - } } else { String message = "error"; String respBody = null; @@ -1605,7 +1600,7 @@ private boolean shouldRetry(int retryCount, int statusCode, RetryConfig retryCon if (statusCode == 429 || statusCode >= 500) { statusToRetry = true; } - return (retryConfig.maxRetries >= retryCount && statusToRetry && retryConfig.isEnableRetry()); + return (retryConfig.maxRetries > retryCount && statusToRetry && retryConfig.isEnableRetry()); } private int calculateRetryInterval( diff --git a/src/main/java/com/datadog/api/client/RetryConfig.java b/src/main/java/com/datadog/api/client/RetryConfig.java index 7f031fbb354..eb5697e9153 100644 --- a/src/main/java/com/datadog/api/client/RetryConfig.java +++ b/src/main/java/com/datadog/api/client/RetryConfig.java @@ -63,13 +63,9 @@ public void setMaxRetries(int maxRetries) { this.maxRetries = maxRetries; } - public void setCalculatedInterval(int calculatedInterval) { - this.calculatedInterval = calculatedInterval; - } - - public void sleepInterval(int sleepInterval) throws InterruptedException { + public void sleepInterval(int sleepInterval) { try { - setCalculatedInterval(sleepInterval); + this.calculatedInterval = sleepInterval; Thread.sleep(sleepInterval * 1000); } catch (InterruptedException e) { Thread.currentThread().interrupt(); diff --git a/src/test/java/com/datadog/api/MockRetryConfig.java b/src/test/java/com/datadog/api/MockRetryConfig.java index f627beb8386..78ea4193de2 100644 --- a/src/test/java/com/datadog/api/MockRetryConfig.java +++ b/src/test/java/com/datadog/api/MockRetryConfig.java @@ -10,9 +10,9 @@ public MockRetryConfig( } @Override - public void sleepInterval(int sleepInterval) throws InterruptedException { + public void sleepInterval(int sleepInterval) { try { - setCalculatedInterval(sleepInterval); + this.calculatedInterval = sleepInterval; Thread.sleep(1); } catch (InterruptedException e) { Thread.currentThread().interrupt(); From dcdd9224ac5f2adcf3f3c68a6f99c0cbef75898c Mon Sep 17 00:00:00 2001 From: "ci.datadog-api-spec" Date: Fri, 11 Aug 2023 10:47:44 +0000 Subject: [PATCH 39/43] pre-commit fixes --- README.md | 1 + src/main/java/com/datadog/api/client/ApiClient.java | 4 ++-- src/main/java/com/datadog/api/client/RetryConfig.java | 8 ++++++-- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 6241bf71596..9036a245ca5 100644 --- a/README.md +++ b/README.md @@ -175,6 +175,7 @@ To enable the client to retry when rate limited (status 429) or status 500 and a ```java defaultClient.enableRetry(true) ``` + The interval between 2 retry attempts will be the value of the `x-ratelimit-reset` response header when available. If not, it will be : `Math.pow (multiplier_for_retry_backoff, current_retry_count)*base_for_retry_backoff`. diff --git a/src/main/java/com/datadog/api/client/ApiClient.java b/src/main/java/com/datadog/api/client/ApiClient.java index ae9bc10ad84..8379583fed5 100644 --- a/src/main/java/com/datadog/api/client/ApiClient.java +++ b/src/main/java/com/datadog/api/client/ApiClient.java @@ -1568,8 +1568,8 @@ public ApiResponse invokeAPI( statusCode, responseHeaders, deserialize(response, returnType)); } } else if (shouldRetry(currentRetry, statusCode, retry)) { - retry.sleepInterval(calculateRetryInterval(responseHeaders, retry, currentRetry)); - currentRetry++; + retry.sleepInterval(calculateRetryInterval(responseHeaders, retry, currentRetry)); + currentRetry++; } else { String message = "error"; String respBody = null; diff --git a/src/main/java/com/datadog/api/client/RetryConfig.java b/src/main/java/com/datadog/api/client/RetryConfig.java index eb5697e9153..7f031fbb354 100644 --- a/src/main/java/com/datadog/api/client/RetryConfig.java +++ b/src/main/java/com/datadog/api/client/RetryConfig.java @@ -63,9 +63,13 @@ public void setMaxRetries(int maxRetries) { this.maxRetries = maxRetries; } - public void sleepInterval(int sleepInterval) { + public void setCalculatedInterval(int calculatedInterval) { + this.calculatedInterval = calculatedInterval; + } + + public void sleepInterval(int sleepInterval) throws InterruptedException { try { - this.calculatedInterval = sleepInterval; + setCalculatedInterval(sleepInterval); Thread.sleep(sleepInterval * 1000); } catch (InterruptedException e) { Thread.currentThread().interrupt(); From a511cc83d2a757d2665351ad8ac830adb89db17a Mon Sep 17 00:00:00 2001 From: HantingZhang2 Date: Fri, 11 Aug 2023 14:17:03 +0200 Subject: [PATCH 40/43] refactor retry test --- .../src/generator/templates/RetryConfig.j2 | 87 +++++++++---------- .../com/datadog/api/client/RetryConfig.java | 17 ++-- .../java/com/datadog/api/MockRetryConfig.java | 4 +- .../datadog/api/client/v2/api/RetryTest.java | 8 +- 4 files changed, 57 insertions(+), 59 deletions(-) diff --git a/.generator/src/generator/templates/RetryConfig.j2 b/.generator/src/generator/templates/RetryConfig.j2 index ee61f6d2314..0f028907adf 100644 --- a/.generator/src/generator/templates/RetryConfig.j2 +++ b/.generator/src/generator/templates/RetryConfig.j2 @@ -2,70 +2,67 @@ package {{ common_package_name }}; +import java.util.ArrayList; +import java.util.List; + public class RetryConfig { - public boolean enableRetry; - public int backOffMultiplier; - public int backOffBase; - public int maxRetries; - public int calculatedInterval; + public boolean enableRetry; + public int backOffMultiplier; + public int backOffBase; + public int maxRetries; + public List intervalList = new ArrayList(); - /** + /** * @param enableRetry Enable retry when rate limited * @param backOffMultiplier Multiplier for retry backoff * @param backOffBase Base for retry backoff - * @param maxRetries Maximum number of retries + * @param maxRetries Maximum number of retries */ - - public RetryConfig(boolean enableRetry, int backOffMultiplier, int backOffBase, int maxRetries) { - this.enableRetry = enableRetry; - this.backOffMultiplier = backOffMultiplier; - this.backOffBase = backOffBase; - this.maxRetries = maxRetries; - this.calculatedInterval = 0; - } + public RetryConfig(boolean enableRetry, int backOffMultiplier, int backOffBase, int maxRetries) { + this.enableRetry = enableRetry; + this.backOffMultiplier = backOffMultiplier; + this.backOffBase = backOffBase; + this.maxRetries = maxRetries; + } public boolean isEnableRetry() { - return enableRetry; - } + return enableRetry; + } - public int getBackOffMultiplier() { - return backOffMultiplier; - } + public int getBackOffMultiplier() { + return backOffMultiplier; + } - public int getBackOffBase() { - return backOffBase; - } + public int getBackOffBase() { + return backOffBase; + } - public int getMaxRetries() { - return maxRetries; - } + public int getMaxRetries() { + return maxRetries; + } - public int getCalculatedInterval(){ - return calculatedInterval; - } + public List getIntervalList() { + return intervalList; + } - public void setEnableRetry(boolean enableRetry) { - this.enableRetry = enableRetry; - } + public void setEnableRetry(boolean enableRetry) { + this.enableRetry = enableRetry; + } - public void setBackOffMultiplier(int backOffMultiplier) { - this.backOffMultiplier = backOffMultiplier; - } + public void setBackOffMultiplier(int backOffMultiplier) { + this.backOffMultiplier = backOffMultiplier; + } - public void setBackOffBase(int backOffBase) { - this.backOffBase = backOffBase; - } - public void setMaxRetries(int maxRetries) { - this.maxRetries = maxRetries; - } + public void setBackOffBase(int backOffBase) { + this.backOffBase = backOffBase; + } - public void setCalculatedInterval(int calculatedInterval) { - this.calculatedInterval = calculatedInterval; + public void setMaxRetries(int maxRetries) { + this.maxRetries = maxRetries; } - public void sleepInterval(int sleepInterval) throws InterruptedException { + public void sleepInterval(int sleepInterval) { try { - setCalculatedInterval(sleepInterval); Thread.sleep(sleepInterval * 1000); } catch (InterruptedException e) { Thread.currentThread().interrupt(); diff --git a/src/main/java/com/datadog/api/client/RetryConfig.java b/src/main/java/com/datadog/api/client/RetryConfig.java index 7f031fbb354..2836c2fddc2 100644 --- a/src/main/java/com/datadog/api/client/RetryConfig.java +++ b/src/main/java/com/datadog/api/client/RetryConfig.java @@ -6,12 +6,15 @@ package com.datadog.api.client; +import java.util.ArrayList; +import java.util.List; + public class RetryConfig { public boolean enableRetry; public int backOffMultiplier; public int backOffBase; public int maxRetries; - public int calculatedInterval; + public List intervalList = new ArrayList(); /** * @param enableRetry Enable retry when rate limited @@ -24,7 +27,6 @@ public RetryConfig(boolean enableRetry, int backOffMultiplier, int backOffBase, this.backOffMultiplier = backOffMultiplier; this.backOffBase = backOffBase; this.maxRetries = maxRetries; - this.calculatedInterval = 0; } public boolean isEnableRetry() { @@ -43,8 +45,8 @@ public int getMaxRetries() { return maxRetries; } - public int getCalculatedInterval() { - return calculatedInterval; + public List getIntervalList() { + return intervalList; } public void setEnableRetry(boolean enableRetry) { @@ -63,13 +65,8 @@ public void setMaxRetries(int maxRetries) { this.maxRetries = maxRetries; } - public void setCalculatedInterval(int calculatedInterval) { - this.calculatedInterval = calculatedInterval; - } - - public void sleepInterval(int sleepInterval) throws InterruptedException { + public void sleepInterval(int sleepInterval) { try { - setCalculatedInterval(sleepInterval); Thread.sleep(sleepInterval * 1000); } catch (InterruptedException e) { Thread.currentThread().interrupt(); diff --git a/src/test/java/com/datadog/api/MockRetryConfig.java b/src/test/java/com/datadog/api/MockRetryConfig.java index 78ea4193de2..cb46b1bfc90 100644 --- a/src/test/java/com/datadog/api/MockRetryConfig.java +++ b/src/test/java/com/datadog/api/MockRetryConfig.java @@ -3,7 +3,7 @@ import com.datadog.api.client.RetryConfig; public class MockRetryConfig extends RetryConfig { - + public MockRetryConfig( boolean enableRetry, int backOffMultiplier, int backOffBase, int maxRetries) { super(enableRetry, backOffMultiplier, backOffBase, maxRetries); @@ -12,7 +12,7 @@ public MockRetryConfig( @Override public void sleepInterval(int sleepInterval) { try { - this.calculatedInterval = sleepInterval; + intervalList.add(sleepInterval); Thread.sleep(1); } catch (InterruptedException e) { Thread.currentThread().interrupt(); diff --git a/src/test/java/com/datadog/api/client/v2/api/RetryTest.java b/src/test/java/com/datadog/api/client/v2/api/RetryTest.java index 50fc422ee32..993de78ed45 100644 --- a/src/test/java/com/datadog/api/client/v2/api/RetryTest.java +++ b/src/test/java/com/datadog/api/client/v2/api/RetryTest.java @@ -33,7 +33,9 @@ public void createDashboardList() throws ApiException, NoSuchAlgorithmException @Test public void retryWithDashboardListItemGetTest429() throws ApiException { DashboardListItems getResponse = api.getDashboardListItems(dashboardListID); - assertEquals(1, generalApiClientWithRetry.getRetry().getCalculatedInterval()); + assertEquals(1, generalApiClientWithRetry.getRetry().getIntervalList().get(0).intValue()); + assertEquals(1, generalApiClientWithRetry.getRetry().getIntervalList().get(1).intValue()); + assertEquals(1, generalApiClientWithRetry.getRetry().getIntervalList().get(2).intValue()); assertNotNull(getResponse.getTotal()); assertEquals(0, (long) getResponse.getTotal()); assertEquals(0, getResponse.getDashboards().size()); @@ -42,7 +44,9 @@ public void retryWithDashboardListItemGetTest429() throws ApiException { @Test public void retryWithDashboardListItemGetTest500() throws ApiException { DashboardListItems getResponse = api.getDashboardListItems(dashboardListID); - assertEquals(8, generalApiClientWithRetry.getRetry().getCalculatedInterval()); + assertEquals(2, generalApiClientWithRetry.getRetry().getIntervalList().get(3).intValue()); + assertEquals(4, generalApiClientWithRetry.getRetry().getIntervalList().get(4).intValue()); + assertEquals(8, generalApiClientWithRetry.getRetry().getIntervalList().get(5).intValue()); assertNotNull(getResponse.getTotal()); assertEquals(0, (long) getResponse.getTotal()); assertEquals(0, getResponse.getDashboards().size()); From 1e0adb778933be065bf8d6d1ef93df30bf5f3e40 Mon Sep 17 00:00:00 2001 From: "ci.datadog-api-spec" Date: Fri, 11 Aug 2023 12:19:36 +0000 Subject: [PATCH 41/43] pre-commit fixes --- src/test/java/com/datadog/api/MockRetryConfig.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/com/datadog/api/MockRetryConfig.java b/src/test/java/com/datadog/api/MockRetryConfig.java index cb46b1bfc90..4e424e74716 100644 --- a/src/test/java/com/datadog/api/MockRetryConfig.java +++ b/src/test/java/com/datadog/api/MockRetryConfig.java @@ -3,7 +3,7 @@ import com.datadog.api.client.RetryConfig; public class MockRetryConfig extends RetryConfig { - + public MockRetryConfig( boolean enableRetry, int backOffMultiplier, int backOffBase, int maxRetries) { super(enableRetry, backOffMultiplier, backOffBase, maxRetries); From dd420ec7140a1ada3e443f201eeb43e60de79cbe Mon Sep 17 00:00:00 2001 From: HantingZhang2 Date: Fri, 11 Aug 2023 18:03:15 +0200 Subject: [PATCH 42/43] Rearrange mockconfig --- .../src/generator/templates/RetryConfig.j2 | 8 -------- .../java/com/datadog/api/client/RetryConfig.java | 10 +--------- .../java/com/datadog/api/MockRetryConfig.java | 10 ++++++++-- .../com/datadog/api/client/v2/api/RetryTest.java | 16 ++++++++++------ 4 files changed, 19 insertions(+), 25 deletions(-) diff --git a/.generator/src/generator/templates/RetryConfig.j2 b/.generator/src/generator/templates/RetryConfig.j2 index 0f028907adf..fb1ba52f885 100644 --- a/.generator/src/generator/templates/RetryConfig.j2 +++ b/.generator/src/generator/templates/RetryConfig.j2 @@ -2,15 +2,11 @@ package {{ common_package_name }}; -import java.util.ArrayList; -import java.util.List; - public class RetryConfig { public boolean enableRetry; public int backOffMultiplier; public int backOffBase; public int maxRetries; - public List intervalList = new ArrayList(); /** * @param enableRetry Enable retry when rate limited @@ -41,10 +37,6 @@ public class RetryConfig { return maxRetries; } - public List getIntervalList() { - return intervalList; - } - public void setEnableRetry(boolean enableRetry) { this.enableRetry = enableRetry; } diff --git a/src/main/java/com/datadog/api/client/RetryConfig.java b/src/main/java/com/datadog/api/client/RetryConfig.java index 2836c2fddc2..b3adef8bb07 100644 --- a/src/main/java/com/datadog/api/client/RetryConfig.java +++ b/src/main/java/com/datadog/api/client/RetryConfig.java @@ -6,16 +6,12 @@ package com.datadog.api.client; -import java.util.ArrayList; -import java.util.List; - public class RetryConfig { public boolean enableRetry; public int backOffMultiplier; public int backOffBase; public int maxRetries; - public List intervalList = new ArrayList(); - + /** * @param enableRetry Enable retry when rate limited * @param backOffMultiplier Multiplier for retry backoff @@ -45,10 +41,6 @@ public int getMaxRetries() { return maxRetries; } - public List getIntervalList() { - return intervalList; - } - public void setEnableRetry(boolean enableRetry) { this.enableRetry = enableRetry; } diff --git a/src/test/java/com/datadog/api/MockRetryConfig.java b/src/test/java/com/datadog/api/MockRetryConfig.java index 4e424e74716..59024d426c6 100644 --- a/src/test/java/com/datadog/api/MockRetryConfig.java +++ b/src/test/java/com/datadog/api/MockRetryConfig.java @@ -1,14 +1,20 @@ package com.datadog.api; +import java.util.ArrayList; +import java.util.List; import com.datadog.api.client.RetryConfig; public class MockRetryConfig extends RetryConfig { + public List intervalList = new ArrayList(); - public MockRetryConfig( - boolean enableRetry, int backOffMultiplier, int backOffBase, int maxRetries) { + public MockRetryConfig(boolean enableRetry, int backOffMultiplier, int backOffBase, int maxRetries) { super(enableRetry, backOffMultiplier, backOffBase, maxRetries); } + public List getIntervalList() { + return intervalList; + } + @Override public void sleepInterval(int sleepInterval) { try { diff --git a/src/test/java/com/datadog/api/client/v2/api/RetryTest.java b/src/test/java/com/datadog/api/client/v2/api/RetryTest.java index 993de78ed45..ae33bada9e2 100644 --- a/src/test/java/com/datadog/api/client/v2/api/RetryTest.java +++ b/src/test/java/com/datadog/api/client/v2/api/RetryTest.java @@ -3,7 +3,9 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; +import com.datadog.api.client.RetryConfig; import com.datadog.api.client.ApiException; +import com.datadog.api.MockRetryConfig; import com.datadog.api.client.v1.model.DashboardList; import com.datadog.api.client.v2.model.*; import java.security.NoSuchAlgorithmException; @@ -33,9 +35,10 @@ public void createDashboardList() throws ApiException, NoSuchAlgorithmException @Test public void retryWithDashboardListItemGetTest429() throws ApiException { DashboardListItems getResponse = api.getDashboardListItems(dashboardListID); - assertEquals(1, generalApiClientWithRetry.getRetry().getIntervalList().get(0).intValue()); - assertEquals(1, generalApiClientWithRetry.getRetry().getIntervalList().get(1).intValue()); - assertEquals(1, generalApiClientWithRetry.getRetry().getIntervalList().get(2).intValue()); + MockRetryConfig retryConfig= (MockRetryConfig)generalApiClientWithRetry.getRetry(); + assertEquals(1, retryConfig.getIntervalList().get(0).intValue()); + assertEquals(1, retryConfig.getIntervalList().get(1).intValue()); + assertEquals(1, retryConfig.getIntervalList().get(2).intValue()); assertNotNull(getResponse.getTotal()); assertEquals(0, (long) getResponse.getTotal()); assertEquals(0, getResponse.getDashboards().size()); @@ -44,9 +47,10 @@ public void retryWithDashboardListItemGetTest429() throws ApiException { @Test public void retryWithDashboardListItemGetTest500() throws ApiException { DashboardListItems getResponse = api.getDashboardListItems(dashboardListID); - assertEquals(2, generalApiClientWithRetry.getRetry().getIntervalList().get(3).intValue()); - assertEquals(4, generalApiClientWithRetry.getRetry().getIntervalList().get(4).intValue()); - assertEquals(8, generalApiClientWithRetry.getRetry().getIntervalList().get(5).intValue()); + MockRetryConfig mockRetryConfig = (MockRetryConfig)generalApiClientWithRetry.getRetry(); + assertEquals(2, mockRetryConfig.getIntervalList().get(3).intValue()); + assertEquals(4, mockRetryConfig.getIntervalList().get(4).intValue()); + assertEquals(8, mockRetryConfig.getIntervalList().get(5).intValue()); assertNotNull(getResponse.getTotal()); assertEquals(0, (long) getResponse.getTotal()); assertEquals(0, getResponse.getDashboards().size()); From e18d64a8d3393dbe7f567aadf842ed69d16022e5 Mon Sep 17 00:00:00 2001 From: "ci.datadog-api-spec" Date: Fri, 11 Aug 2023 16:05:43 +0000 Subject: [PATCH 43/43] pre-commit fixes --- src/main/java/com/datadog/api/client/RetryConfig.java | 2 +- src/test/java/com/datadog/api/MockRetryConfig.java | 5 +++-- src/test/java/com/datadog/api/client/v2/api/RetryTest.java | 7 +++---- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/datadog/api/client/RetryConfig.java b/src/main/java/com/datadog/api/client/RetryConfig.java index b3adef8bb07..c672725323d 100644 --- a/src/main/java/com/datadog/api/client/RetryConfig.java +++ b/src/main/java/com/datadog/api/client/RetryConfig.java @@ -11,7 +11,7 @@ public class RetryConfig { public int backOffMultiplier; public int backOffBase; public int maxRetries; - + /** * @param enableRetry Enable retry when rate limited * @param backOffMultiplier Multiplier for retry backoff diff --git a/src/test/java/com/datadog/api/MockRetryConfig.java b/src/test/java/com/datadog/api/MockRetryConfig.java index 59024d426c6..9e20f53e9a3 100644 --- a/src/test/java/com/datadog/api/MockRetryConfig.java +++ b/src/test/java/com/datadog/api/MockRetryConfig.java @@ -1,13 +1,14 @@ package com.datadog.api; +import com.datadog.api.client.RetryConfig; import java.util.ArrayList; import java.util.List; -import com.datadog.api.client.RetryConfig; public class MockRetryConfig extends RetryConfig { public List intervalList = new ArrayList(); - public MockRetryConfig(boolean enableRetry, int backOffMultiplier, int backOffBase, int maxRetries) { + public MockRetryConfig( + boolean enableRetry, int backOffMultiplier, int backOffBase, int maxRetries) { super(enableRetry, backOffMultiplier, backOffBase, maxRetries); } diff --git a/src/test/java/com/datadog/api/client/v2/api/RetryTest.java b/src/test/java/com/datadog/api/client/v2/api/RetryTest.java index ae33bada9e2..0ed8b7007eb 100644 --- a/src/test/java/com/datadog/api/client/v2/api/RetryTest.java +++ b/src/test/java/com/datadog/api/client/v2/api/RetryTest.java @@ -3,9 +3,8 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; -import com.datadog.api.client.RetryConfig; -import com.datadog.api.client.ApiException; import com.datadog.api.MockRetryConfig; +import com.datadog.api.client.ApiException; import com.datadog.api.client.v1.model.DashboardList; import com.datadog.api.client.v2.model.*; import java.security.NoSuchAlgorithmException; @@ -35,7 +34,7 @@ public void createDashboardList() throws ApiException, NoSuchAlgorithmException @Test public void retryWithDashboardListItemGetTest429() throws ApiException { DashboardListItems getResponse = api.getDashboardListItems(dashboardListID); - MockRetryConfig retryConfig= (MockRetryConfig)generalApiClientWithRetry.getRetry(); + MockRetryConfig retryConfig = (MockRetryConfig) generalApiClientWithRetry.getRetry(); assertEquals(1, retryConfig.getIntervalList().get(0).intValue()); assertEquals(1, retryConfig.getIntervalList().get(1).intValue()); assertEquals(1, retryConfig.getIntervalList().get(2).intValue()); @@ -47,7 +46,7 @@ public void retryWithDashboardListItemGetTest429() throws ApiException { @Test public void retryWithDashboardListItemGetTest500() throws ApiException { DashboardListItems getResponse = api.getDashboardListItems(dashboardListID); - MockRetryConfig mockRetryConfig = (MockRetryConfig)generalApiClientWithRetry.getRetry(); + MockRetryConfig mockRetryConfig = (MockRetryConfig) generalApiClientWithRetry.getRetry(); assertEquals(2, mockRetryConfig.getIntervalList().get(3).intValue()); assertEquals(4, mockRetryConfig.getIntervalList().get(4).intValue()); assertEquals(8, mockRetryConfig.getIntervalList().get(5).intValue());