forked from JanusGraph/janusgraph
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added retry logic if client request returns an error code that is con…
…figured for retrying Closes JanusGraph#4408 Added additional tests for coverage Included example of comma separated values for retry-error-codes config parameter Simplified retry loop logic Moved retry configurations into constructor and removed setters Signed-off-by: Allan Clements <[email protected]>
- Loading branch information
1 parent
0d601cf
commit d75cfe6
Showing
6 changed files
with
283 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
156 changes: 156 additions & 0 deletions
156
janusgraph-es/src/test/java/org/janusgraph/diskstorage/es/rest/RestClientRetryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
// Copyright 2024 JanusGraph Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package org.janusgraph.diskstorage.es.rest; | ||
|
||
import com.google.common.collect.Sets; | ||
import org.apache.http.StatusLine; | ||
import org.elasticsearch.client.Request; | ||
import org.elasticsearch.client.Response; | ||
import org.elasticsearch.client.ResponseException; | ||
import org.elasticsearch.client.RestClient; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.ArgumentCaptor; | ||
import org.mockito.Captor; | ||
import org.mockito.Mock; | ||
import org.mockito.Mockito; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
import java.io.IOException; | ||
import java.util.Collections; | ||
import java.util.Set; | ||
|
||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.doReturn; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.times; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
public class RestClientRetryTest { | ||
@Mock | ||
private RestClient restClientMock; | ||
|
||
@Mock | ||
private ResponseException responseException; | ||
|
||
@Mock | ||
private Response response; | ||
|
||
@Mock | ||
private StatusLine statusLine; | ||
|
||
@Captor | ||
private ArgumentCaptor<Request> requestCaptor; | ||
|
||
RestElasticSearchClient createClient(int retryAttemptLimit, Set<Integer> retryErrorCodes) throws IOException { | ||
//Just throw an exception when there's an attempt to look up the ES version during instantiation | ||
when(restClientMock.performRequest(any())).thenThrow(new IOException()); | ||
|
||
RestElasticSearchClient clientUnderTest = new RestElasticSearchClient(restClientMock, 0, false, | ||
retryAttemptLimit, retryErrorCodes, 0, 0); | ||
//There's an initial query to get the ES version we need to accommodate, and then reset for the actual test | ||
Mockito.reset(restClientMock); | ||
return clientUnderTest; | ||
} | ||
|
||
@Test | ||
public void testRetryOnConfiguredErrorStatus() throws IOException { | ||
Integer retryCode = 429; | ||
int expectedNumberOfRequestAttempts = 2; | ||
doReturn(retryCode).when(statusLine).getStatusCode(); | ||
doReturn(statusLine).when(response).getStatusLine(); | ||
doReturn(response).when(responseException).getResponse(); | ||
//Just throw an expected exception the second time to confirm the retry occurred | ||
//rather than mock out a parsable response | ||
IOException expectedFinalException = new IOException("Expected"); | ||
|
||
try (RestElasticSearchClient restClientUnderTest = createClient(expectedNumberOfRequestAttempts - 1, | ||
Sets.newHashSet(retryCode))) { | ||
//prime the restClientMock again after it's reset after creation | ||
when(restClientMock.performRequest(any())) | ||
.thenThrow(responseException) | ||
.thenThrow(expectedFinalException); | ||
restClientUnderTest.bulkRequest(Collections.emptyList(), null); | ||
Assertions.fail("Should have thrown the expected exception after retry"); | ||
} catch (Exception actualException) { | ||
Assertions.assertSame(expectedFinalException, actualException); | ||
} | ||
verify(restClientMock, times(expectedNumberOfRequestAttempts)).performRequest(requestCaptor.capture()); | ||
} | ||
|
||
@Test | ||
public void testRetriesExhaustedReturnsLastRetryException() throws IOException { | ||
Integer retryCode = 429; | ||
int expectedNumberOfRequestAttempts = 2; | ||
doReturn(retryCode).when(statusLine).getStatusCode(); | ||
doReturn(statusLine).when(response).getStatusLine(); | ||
doReturn(response).when(responseException).getResponse(); | ||
ResponseException initialRetryException = mock(ResponseException.class); | ||
doReturn(response).when(initialRetryException).getResponse(); | ||
|
||
try (RestElasticSearchClient restClientUnderTest = createClient(expectedNumberOfRequestAttempts - 1, | ||
Sets.newHashSet(retryCode))) { | ||
//prime the restClientMock again after it's reset after creation | ||
when(restClientMock.performRequest(any())) | ||
//first throw a different retry exception instance, then make sure it's the latter one | ||
//that was retained and then thrown | ||
.thenThrow(initialRetryException) | ||
.thenThrow(responseException); | ||
|
||
|
||
restClientUnderTest.bulkRequest(Collections.emptyList(), null); | ||
Assertions.fail("Should have thrown the expected exception after retry"); | ||
} catch (Exception e) { | ||
Assertions.assertSame(responseException, e); | ||
} | ||
verify(restClientMock, times(expectedNumberOfRequestAttempts)).performRequest(requestCaptor.capture()); | ||
} | ||
|
||
@Test | ||
public void testNonRetryErrorCodeException() throws IOException { | ||
doReturn(503).when(statusLine).getStatusCode(); | ||
doReturn(statusLine).when(response).getStatusLine(); | ||
doReturn(response).when(responseException).getResponse(); | ||
try (RestElasticSearchClient restClientUnderTest = createClient(0, | ||
//Other retry error code is configured | ||
Sets.newHashSet(429))) { | ||
//prime the restClientMock again after it's reset after creation | ||
when(restClientMock.performRequest(any())) | ||
.thenThrow(responseException); | ||
restClientUnderTest.bulkRequest(Collections.emptyList(), null); | ||
Assertions.fail("Should have thrown the expected exception"); | ||
} catch (Exception e) { | ||
Assertions.assertSame(responseException, e); | ||
} | ||
verify(restClientMock, times(1)).performRequest(requestCaptor.capture()); | ||
} | ||
|
||
@Test | ||
public void testNonResponseExceptionErrorThrown() throws IOException { | ||
IOException differentExceptionType = new IOException(); | ||
when(restClientMock.performRequest(any())) | ||
.thenThrow(differentExceptionType); | ||
try (RestElasticSearchClient restClientUnderTest = createClient(0, Collections.emptySet())) { | ||
restClientUnderTest.bulkRequest(Collections.emptyList(), null); | ||
Assertions.fail("Should have thrown the expected exception"); | ||
} catch (Exception e) { | ||
Assertions.assertSame(differentExceptionType, e); | ||
} | ||
verify(restClientMock, times(1)).performRequest(requestCaptor.capture()); | ||
} | ||
} |
Oops, something went wrong.
d75cfe6
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Benchmark
org.janusgraph.JanusGraphSpeedBenchmark.basicAddAndDelete
13060.812343174288
ms/op13882.523057793705
ms/op0.94
org.janusgraph.GraphCentricQueryBenchmark.getVertices
898.7254346514046
ms/op938.7454327039508
ms/op0.96
org.janusgraph.MgmtOlapJobBenchmark.runClearIndex
216.41343561956523
ms/op216.6581155155797
ms/op1.00
org.janusgraph.MgmtOlapJobBenchmark.runReindex
338.0366200345238
ms/op347.79107886769236
ms/op0.97
org.janusgraph.JanusGraphSpeedBenchmark.basicCount
213.63426028180274
ms/op230.14241005780215
ms/op0.93
org.janusgraph.CQLMultiQueryMultiSlicesBenchmark.getValuesAllPropertiesWithAllMultiQuerySlicesUnderMaxRequestsPerConnection
4734.969248491554
ms/op4979.698797423356
ms/op0.95
org.janusgraph.CQLMultiQueryBenchmark.getElementsWithUsingEmitRepeatSteps
17066.95763009286
ms/op16436.099606292057
ms/op1.04
org.janusgraph.CQLMultiQueryMultiSlicesBenchmark.getValuesMultiplePropertiesWithSmallBatch
18477.66787504762
ms/op19329.0038558103
ms/op0.96
org.janusgraph.CQLMultiQueryMultiSlicesBenchmark.vertexCentricPropertiesFetching
53717.76240903333
ms/op56673.909406633335
ms/op0.95
org.janusgraph.CQLMultiQueryBenchmark.getAllElementsTraversedFromOuterVertex
8195.151031248442
ms/op8185.252311468057
ms/op1.00
org.janusgraph.CQLMultiQueryBenchmark.getVerticesWithDoubleUnion
370.4809368506361
ms/op366.2127994291684
ms/op1.01
org.janusgraph.CQLMultiQueryMultiSlicesBenchmark.getValuesAllPropertiesWithUnlimitedBatch
4149.503219661898
ms/op4155.013576386989
ms/op1.00
org.janusgraph.CQLMultiQueryBenchmark.getNames
8162.812156355291
ms/op7846.475429399913
ms/op1.04
org.janusgraph.CQLMultiQueryMultiSlicesBenchmark.getValuesThreePropertiesWithAllMultiQuerySlicesUnderMaxRequestsPerConnection
5661.618940538105
ms/op5879.818323385454
ms/op0.96
org.janusgraph.CQLMultiQueryBenchmark.getLabels
7208.119404727526
ms/op7009.9235684485575
ms/op1.03
org.janusgraph.CQLMultiQueryBenchmark.getVerticesFilteredByAndStep
423.9792960434415
ms/op406.65979183393114
ms/op1.04
org.janusgraph.CQLMultiQueryBenchmark.getVerticesFromMultiNestedRepeatStepStartingFromSingleVertex
12137.60505813065
ms/op12538.50282688309
ms/op0.97
org.janusgraph.CQLMultiQueryBenchmark.getVerticesWithCoalesceUsage
357.6897097125631
ms/op354.5867459421563
ms/op1.01
org.janusgraph.CQLMultiQueryMultiSlicesBenchmark.getValuesMultiplePropertiesWithAllMultiQuerySlicesUnderMaxRequestsPerConnection
14453.598089354924
ms/op13625.019270800296
ms/op1.06
org.janusgraph.CQLMultiQueryBenchmark.getIdToOutVerticesProjection
251.6403959273193
ms/op239.03739323588258
ms/op1.05
org.janusgraph.CQLMultiQueryMultiSlicesBenchmark.getValuesMultiplePropertiesWithUnlimitedBatch
14161.706561144163
ms/op13396.492433014544
ms/op1.06
org.janusgraph.CQLMultiQueryBenchmark.getNeighborNames
8365.485376687184
ms/op7823.291265104974
ms/op1.07
org.janusgraph.CQLMultiQueryBenchmark.getElementsWithUsingRepeatUntilSteps
9194.375511734524
ms/op9162.429751679501
ms/op1.00
org.janusgraph.CQLMultiQueryBenchmark.getAdjacentVerticesLocalCounts
8524.871066793352
ms/op8753.949937235953
ms/op0.97
This comment was automatically generated by workflow using github-action-benchmark.