Skip to content

Commit b9a3bf2

Browse files
authored
set max 3 retries for active failover redirects (#412)
1 parent 7323ace commit b9a3bf2

File tree

4 files changed

+32
-5
lines changed

4 files changed

+32
-5
lines changed

src/main/java/com/arangodb/async/internal/velocystream/VstCommunicationAsync.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ private VstCommunicationAsync(final HostHandler hostHandler, final Integer timeo
5656

5757
@Override
5858
protected CompletableFuture<Response> execute(final Request request, final VstConnectionAsync connection) {
59+
return execute(request, connection, 0);
60+
}
61+
62+
@Override
63+
protected CompletableFuture<Response> execute(final Request request, final VstConnectionAsync connection, final int attemptCount) {
5964
final CompletableFuture<Response> rfuture = new CompletableFuture<>();
6065
try {
6166
final Message message = createMessage(request);
@@ -73,11 +78,15 @@ protected CompletableFuture<Response> execute(final Request request, final VstCo
7378
try {
7479
checkError(response);
7580
} catch (final ArangoDBRedirectException e) {
81+
if (attemptCount >= 3) {
82+
rfuture.completeExceptionally(e);
83+
return;
84+
}
7685
final String location = e.getLocation();
7786
final HostDescription redirectHost = HostUtils.createFromLocation(location);
7887
hostHandler.closeCurrentOnError();
7988
hostHandler.fail();
80-
execute(request, new HostHandle().setHost(redirectHost))
89+
execute(request, new HostHandle().setHost(redirectHost), attemptCount + 1)
8190
.whenComplete((v, err) -> {
8291
if (v != null) {
8392
rfuture.complete(v);

src/main/java/com/arangodb/internal/http/HttpCommunication.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ public void close() throws IOException {
7272
}
7373

7474
public Response execute(final Request request, final HostHandle hostHandle) throws ArangoDBException, IOException {
75+
return execute(request, hostHandle, 0);
76+
}
77+
78+
private Response execute(final Request request, final HostHandle hostHandle, final int attemptCount) throws ArangoDBException, IOException {
7579
final AccessType accessType = RequestUtils.determineAccessType(request);
7680
Host host = hostHandler.get(hostHandle, accessType);
7781
try {
@@ -99,12 +103,12 @@ public Response execute(final Request request, final HostHandle hostHandle) thro
99103
}
100104
}
101105
} catch (final ArangoDBException e) {
102-
if (e instanceof ArangoDBRedirectException) {
106+
if (e instanceof ArangoDBRedirectException && attemptCount < 3) {
103107
final String location = ((ArangoDBRedirectException) e).getLocation();
104108
final HostDescription redirectHost = HostUtils.createFromLocation(location);
105109
hostHandler.closeCurrentOnError();
106110
hostHandler.fail();
107-
return execute(request, new HostHandle().setHost(redirectHost));
111+
return execute(request, new HostHandle().setHost(redirectHost), attemptCount + 1);
108112
} else {
109113
throw e;
110114
}

src/main/java/com/arangodb/internal/velocystream/VstCommunication.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,12 +138,18 @@ public void close() throws IOException {
138138
}
139139

140140
public R execute(final Request request, final HostHandle hostHandle) throws ArangoDBException {
141+
return execute(request, hostHandle, 0);
142+
}
143+
144+
protected R execute(final Request request, final HostHandle hostHandle, final int attemptCount) throws ArangoDBException {
141145
final C connection = connect(hostHandle, RequestUtils.determineAccessType(request));
142-
return execute(request, connection);
146+
return execute(request, connection, attemptCount);
143147
}
144148

145149
protected abstract R execute(final Request request, C connection) throws ArangoDBException;
146150

151+
protected abstract R execute(final Request request, C connection, final int attemptCount) throws ArangoDBException;
152+
147153
protected void checkError(final Response response) throws ArangoDBException {
148154
ResponseUtils.checkError(util, response);
149155
}

src/main/java/com/arangodb/internal/velocystream/VstCommunicationSync.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,11 @@ protected VstCommunicationSync(final HostHandler hostHandler, final Integer time
123123

124124
@Override
125125
protected Response execute(final Request request, final VstConnectionSync connection) throws ArangoDBException {
126+
return execute(request, connection, 0);
127+
}
128+
129+
@Override
130+
protected Response execute(final Request request, final VstConnectionSync connection, final int attemptCount) throws ArangoDBException {
126131
try {
127132
final Message requestMessage = createMessage(request);
128133
final Message responseMessage = send(requestMessage, connection);
@@ -132,11 +137,14 @@ protected Response execute(final Request request, final VstConnectionSync connec
132137
} catch (final VPackParserException e) {
133138
throw new ArangoDBException(e);
134139
} catch (final ArangoDBRedirectException e) {
140+
if (attemptCount >= 3) {
141+
throw e;
142+
}
135143
final String location = e.getLocation();
136144
final HostDescription redirectHost = HostUtils.createFromLocation(location);
137145
hostHandler.closeCurrentOnError();
138146
hostHandler.fail();
139-
return execute(request, new HostHandle().setHost(redirectHost));
147+
return execute(request, new HostHandle().setHost(redirectHost), attemptCount + 1);
140148
}
141149
}
142150

0 commit comments

Comments
 (0)