Skip to content

Commit d6a7427

Browse files
plugins: Add 'break' at RedifshClient request re-try loop (fixed issue from 4846) (#4864)
* Break loop if no exception on http request * Add new tests ensuring the correct execution flow of the RedfishClient retry * Log retry as "retry attempt %d/%d" * Fix string.format parameters order at RedfishClient.retryHttpRequest
1 parent 8911111 commit d6a7427

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

utils/src/main/java/org/apache/cloudstack/utils/redfish/RedfishClient.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,9 @@ protected HttpResponse retryHttpRequest(String url, HttpRequestBase httpReq, Htt
231231
for (int attempt = 1; attempt < redfishRequestMaxRetries + 1; attempt++) {
232232
try {
233233
TimeUnit.SECONDS.sleep(WAIT_FOR_REQUEST_RETRY);
234-
LOGGER.debug(String.format("Retry HTTP %s request [URL: %s], attempt %d/%d.", httpReq.getMethod(), url, attempt, redfishRequestMaxRetries));
234+
LOGGER.debug(String.format("HTTP %s request retry attempt %d/%d [URL: %s].", httpReq.getMethod(), attempt, redfishRequestMaxRetries, url));
235235
response = client.execute(httpReq);
236+
break;
236237
} catch (IOException | InterruptedException e) {
237238
if (attempt == redfishRequestMaxRetries) {
238239
throw new RedfishException(String.format("Failed to execute HTTP %s request retry attempt %d/%d [URL: %s] due to exception %s", httpReq.getMethod(), attempt, redfishRequestMaxRetries,url, e));

utils/src/test/java/org/apache/cloudstack/utils/redfish/RedfishClientTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,4 +207,26 @@ public void retryHttpRequestNoException() throws IOException {
207207
Mockito.verify(newRedfishClientspy, Mockito.times(1)).retryHttpRequest(Mockito.anyString(), Mockito.any(), Mockito.any());
208208
Mockito.verify(client, Mockito.times(3)).execute(Mockito.any());
209209
}
210+
211+
@Test(expected = RedfishException.class)
212+
public void retryHttpRequestExceptionAfterTwoRetries() throws IOException {
213+
Mockito.when(client.execute(httpReq)).thenThrow(IOException.class).thenThrow(IOException.class);
214+
215+
RedfishClient newRedfishClientspy = Mockito.spy(new RedfishClient(USERNAME, PASSWORD, true, true, REDFISHT_REQUEST_RETRIES));
216+
newRedfishClientspy.retryHttpRequest(url, httpReq, client);
217+
218+
Mockito.verify(newRedfishClientspy, Mockito.never()).retryHttpRequest(Mockito.anyString(), Mockito.any(), Mockito.any());
219+
Mockito.verify(client, Mockito.never()).execute(Mockito.any());
220+
}
221+
222+
@Test
223+
public void retryHttpRequestSuccessAtTheSecondRetry() throws IOException {
224+
Mockito.when(client.execute(httpReq)).thenThrow(IOException.class).thenReturn(httpResponse);
225+
226+
RedfishClient newRedfishClientspy = Mockito.spy(new RedfishClient(USERNAME, PASSWORD, true, true, REDFISHT_REQUEST_RETRIES));
227+
newRedfishClientspy.retryHttpRequest(url, httpReq, client);
228+
229+
Mockito.verify(newRedfishClientspy, Mockito.times(1)).retryHttpRequest(Mockito.anyString(), Mockito.any(), Mockito.any());
230+
Mockito.verify(client, Mockito.times(REDFISHT_REQUEST_RETRIES)).execute(Mockito.any());
231+
}
210232
}

0 commit comments

Comments
 (0)