Skip to content

Commit 8e70f89

Browse files
author
Ubuntu
committed
Add max retries override + unit test
Signed-off-by: Ubuntu <ubuntu@ip-172-30-2-35.ec2.internal>
1 parent dbb5ed2 commit 8e70f89

File tree

4 files changed

+68
-1
lines changed

4 files changed

+68
-1
lines changed

databricks-sdk-java/src/main/java/com/databricks/sdk/core/ApiClient.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public static class Builder {
3737
private Function<Void, String> getHostFunc;
3838
private Function<Void, String> getAuthTypeFunc;
3939
private Integer debugTruncateBytes;
40+
private Integer maxRetries;
4041
private HttpClient httpClient;
4142
private String accountId;
4243
private RetryStrategyPicker retryStrategyPicker;
@@ -48,6 +49,7 @@ public Builder withDatabricksConfig(DatabricksConfig config) {
4849
this.getAuthTypeFunc = v -> config.getAuthType();
4950
this.httpClient = config.getHttpClient();
5051
this.debugTruncateBytes = config.getDebugTruncateBytes();
52+
this.maxRetries = config.getMaxRetries();
5153
this.accountId = config.getAccountId();
5254
this.retryStrategyPicker = new RequestBasedRetryStrategyPicker(config.getHost());
5355
this.isDebugHeaders = config.isDebugHeaders();
@@ -84,6 +86,11 @@ public Builder withRetryStrategyPicker(RetryStrategyPicker retryStrategyPicker)
8486
return this;
8587
}
8688

89+
public Builder withMaxRetries(int maxRetries) {
90+
this.maxRetries = maxRetries;
91+
return this;
92+
}
93+
8794
public ApiClient build() {
8895
return new ApiClient(this);
8996
}
@@ -145,7 +152,7 @@ private ApiClient(Builder builder) {
145152
debugTruncateBytes = 96;
146153
}
147154

148-
maxAttempts = 4;
155+
maxAttempts = builder.maxRetries != null ? builder.maxRetries : 4;
149156
mapper = SerDeUtils.createMapper();
150157
random = new Random();
151158
bodyLogger = new BodyLogger(mapper, 1024, debugTruncateBytes);

databricks-sdk-java/src/main/java/com/databricks/sdk/core/DatabricksConfig.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,10 @@ public class DatabricksConfig {
124124
/** Number of seconds for HTTP timeout */
125125
@ConfigAttribute() private Integer httpTimeoutSeconds;
126126

127+
/** Maximum number of retry attempts for failed requests. Default is 4. */
128+
@ConfigAttribute(env = "DATABRICKS_MAX_RETRIES")
129+
private Integer maxRetries;
130+
127131
/** Truncate JSON fields in JSON above this limit. Default is 96. */
128132
@ConfigAttribute(env = "DATABRICKS_DEBUG_TRUNCATE_BYTES")
129133
private Integer debugTruncateBytes;
@@ -537,6 +541,15 @@ public DatabricksConfig setHttpTimeoutSeconds(int httpTimeoutSeconds) {
537541
return this;
538542
}
539543

544+
public Integer getMaxRetries() {
545+
return maxRetries;
546+
}
547+
548+
public DatabricksConfig setMaxRetries(int maxRetries) {
549+
this.maxRetries = maxRetries;
550+
return this;
551+
}
552+
540553
public Integer getDebugTruncateBytes() {
541554
return debugTruncateBytes;
542555
}

databricks-sdk-java/src/test/java/com/databricks/sdk/core/ApiClientTest.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,36 @@ void failIdempotentRequestAfterTooManyRetries() throws IOException {
278278
"Request GET /api/2.0/sql/sessions/ failed after 4 retries");
279279
}
280280

281+
@Test
282+
void failAfterConfigurableRetries() throws IOException {
283+
Request req = getBasicRequest();
284+
285+
// Test with custom maxRetries=2
286+
DatabricksConfig config =
287+
new DatabricksConfig()
288+
.setHost("http://my.host")
289+
.setMaxRetries(2)
290+
.setCredentialsProvider(new DummyCredentialsProvider());
291+
292+
ApiClient client =
293+
getApiClient(
294+
config,
295+
req,
296+
Arrays.asList(
297+
getTooManyRequestsResponse(req),
298+
getTooManyRequestsResponse(req),
299+
getSuccessResponse(req))); // Would succeed on attempt 3, but maxRetries=2
300+
301+
DatabricksException exception =
302+
assertThrows(
303+
DatabricksException.class,
304+
() ->
305+
client.execute(
306+
new Request("GET", req.getUri().getPath()), MyEndpointResponse.class));
307+
308+
assertTrue(exception.getMessage().contains("failed after 2 retries"));
309+
}
310+
281311
@Test
282312
void testEmptyBody() throws IOException {
283313
MyEndpointResponse response = new MyEndpointResponse();

databricks-sdk-java/src/test/java/com/databricks/sdk/core/DatabricksConfigTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,4 +282,21 @@ public void testEnvironmentVariableLoading() {
282282
assertEquals(Integer.valueOf(100), config.getDebugTruncateBytes());
283283
assertEquals(Integer.valueOf(50), config.getRateLimit());
284284
}
285+
286+
@Test
287+
public void testMaxRetriesSetAndGet() {
288+
DatabricksConfig config = new DatabricksConfig().setMaxRetries(10);
289+
assertEquals(Integer.valueOf(10), config.getMaxRetries());
290+
}
291+
292+
@Test
293+
public void testMaxRetriesEnvironmentVariable() {
294+
Map<String, String> env = new HashMap<>();
295+
env.put("DATABRICKS_MAX_RETRIES", "15");
296+
297+
DatabricksConfig config = new DatabricksConfig();
298+
config.resolve(new Environment(env, new ArrayList<>(), System.getProperty("os.name")));
299+
300+
assertEquals(Integer.valueOf(15), config.getMaxRetries());
301+
}
285302
}

0 commit comments

Comments
 (0)