Skip to content

Commit 14cb945

Browse files
shawkinsmanusa
authored andcommitted
fix #4365 correcting backoff interval
also adding stacktraces for blocking request exceptions
1 parent 15f8335 commit 14cb945

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed

kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/KubernetesClientException.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ private KubernetesClientException(String message, Throwable t, int code, Status
6060
super(message, t);
6161
this.code = code;
6262
this.status = status;
63-
this.requestMetadata = requestMetadata;
63+
this.requestMetadata = requestMetadata == null ? RequestMetadata.EMPTY : requestMetadata;
6464
}
6565

6666
/**
@@ -230,4 +230,12 @@ static RequestMetadata from(URI request) {
230230
}
231231
}
232232
}
233+
234+
/**
235+
* Create a new {@link KubernetesClientException} with this exception as the cause
236+
*/
237+
public KubernetesClientException copyAsCause() {
238+
return new KubernetesClientException(this.getMessage(), this, this.getCode(), this.getStatus(),
239+
this.requestMetadata);
240+
}
233241
}

kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/internal/OperationSupport.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ public class OperationSupport {
8383
protected String apiGroupVersion;
8484
protected boolean dryRun;
8585
private final int requestRetryBackoffLimit;
86+
private final int requestRetryBackoffInterval;
8687

8788
public OperationSupport(Client client) {
8889
this(new OperationContext().withClient(client));
@@ -106,8 +107,10 @@ public OperationSupport(OperationContext ctx) {
106107
}
107108

108109
if (ctx.getConfig() != null) {
110+
requestRetryBackoffInterval = ctx.getConfig().getRequestRetryBackoffInterval();
109111
this.requestRetryBackoffLimit = ctx.getConfig().getRequestRetryBackoffLimit();
110112
} else {
113+
requestRetryBackoffInterval = Config.DEFAULT_REQUEST_RETRY_BACKOFFINTERVAL;
111114
this.requestRetryBackoffLimit = Config.DEFAULT_REQUEST_RETRY_BACKOFFLIMIT;
112115
}
113116
}
@@ -508,15 +511,14 @@ protected <T> T waitForResult(CompletableFuture<T> future) throws IOException {
508511
if (e.getCause() != null) {
509512
t = e.getCause();
510513
}
514+
// throw a new exception to preserve the calling stack trace
511515
if (t instanceof IOException) {
512-
throw (IOException) t;
516+
throw new IOException(t.getMessage(), t);
513517
}
514-
if (t instanceof RuntimeException) {
515-
throw (RuntimeException) t;
518+
if (t instanceof KubernetesClientException) {
519+
throw ((KubernetesClientException) t).copyAsCause();
516520
}
517-
InterruptedIOException ie = new InterruptedIOException();
518-
ie.initCause(e);
519-
throw ie;
521+
throw new KubernetesClientException(t.getMessage(), t);
520522
}
521523
}
522524

@@ -568,7 +570,7 @@ protected <T> CompletableFuture<T> handleResponse(HttpClient client, HttpRequest
568570
HttpRequest request = requestBuilder.build();
569571
CompletableFuture<HttpResponse<byte[]>> futureResponse = new CompletableFuture<>();
570572
retryWithExponentialBackoff(futureResponse,
571-
new ExponentialBackoffIntervalCalculator(requestRetryBackoffLimit, MAX_RETRY_INTERVAL_EXPONENT),
573+
new ExponentialBackoffIntervalCalculator(requestRetryBackoffInterval, MAX_RETRY_INTERVAL_EXPONENT),
572574
Utils.getNonNullOrElse(client, httpClient), request);
573575

574576
return futureResponse.thenApply(response -> {

kubernetes-client/src/test/java/io/fabric8/kubernetes/client/dsl/internal/BaseOperationTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@ void testNoHttpRetryWithDefaultConfig() {
296296
void testHttpRetryWithMoreFailuresThanRetries() {
297297
final AtomicInteger httpExecutionCounter = new AtomicInteger(0);
298298
HttpClient mockClient = newHttpClientWithSomeFailures(httpExecutionCounter, 1000);
299+
long start = System.currentTimeMillis();
299300
BaseOperation<Pod, PodList, Resource<Pod>> baseOp = new BaseOperation(new OperationContext()
300301
.withClient(mockClient(mockClient,
301302
new ConfigBuilder().withMasterUrl("https://172.17.0.2:8443").withNamespace("default")
@@ -309,7 +310,10 @@ void testHttpRetryWithMoreFailuresThanRetries() {
309310
Pod result = baseOp.get();
310311
});
311312

313+
long stop = System.currentTimeMillis();
314+
312315
// Then
316+
assertTrue(stop - start >= 700); //100+200+400
313317
assertTrue(exception.getMessage().contains("Internal Server Error"),
314318
"As the last failure, the 3rd one, is not an IOException the message expected to contain: 'Internal Server Error'!");
315319
assertEquals(4, httpExecutionCounter.get(), "Expected 4 calls: one normal try and 3 backoff retries!");

0 commit comments

Comments
 (0)