From 284c2a68dd08ac2be9db36e04613207545967538 Mon Sep 17 00:00:00 2001 From: Michele Rastelli Date: Thu, 11 Nov 2021 11:05:14 +0100 Subject: [PATCH] set max 3 retries for active failover redirects --- .../internal/velocystream/VstCommunicationAsync.java | 11 ++++++++++- .../com/arangodb/internal/http/HttpCommunication.java | 8 ++++++-- .../internal/velocystream/VstCommunication.java | 8 +++++++- .../internal/velocystream/VstCommunicationSync.java | 10 +++++++++- 4 files changed, 32 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/arangodb/async/internal/velocystream/VstCommunicationAsync.java b/src/main/java/com/arangodb/async/internal/velocystream/VstCommunicationAsync.java index e86e2cf0a..bd3540f04 100644 --- a/src/main/java/com/arangodb/async/internal/velocystream/VstCommunicationAsync.java +++ b/src/main/java/com/arangodb/async/internal/velocystream/VstCommunicationAsync.java @@ -56,6 +56,11 @@ private VstCommunicationAsync(final HostHandler hostHandler, final Integer timeo @Override protected CompletableFuture execute(final Request request, final VstConnectionAsync connection) { + return execute(request, connection, 0); + } + + @Override + protected CompletableFuture execute(final Request request, final VstConnectionAsync connection, final int attemptCount) { final CompletableFuture rfuture = new CompletableFuture<>(); try { final Message message = createMessage(request); @@ -73,11 +78,15 @@ protected CompletableFuture execute(final Request request, final VstCo try { checkError(response); } catch (final ArangoDBRedirectException e) { + if (attemptCount >= 3) { + rfuture.completeExceptionally(e); + return; + } final String location = e.getLocation(); final HostDescription redirectHost = HostUtils.createFromLocation(location); hostHandler.closeCurrentOnError(); hostHandler.fail(); - execute(request, new HostHandle().setHost(redirectHost)) + execute(request, new HostHandle().setHost(redirectHost), attemptCount + 1) .whenComplete((v, err) -> { if (v != null) { rfuture.complete(v); diff --git a/src/main/java/com/arangodb/internal/http/HttpCommunication.java b/src/main/java/com/arangodb/internal/http/HttpCommunication.java index e9a11a33b..558b058db 100644 --- a/src/main/java/com/arangodb/internal/http/HttpCommunication.java +++ b/src/main/java/com/arangodb/internal/http/HttpCommunication.java @@ -72,6 +72,10 @@ public void close() throws IOException { } public Response execute(final Request request, final HostHandle hostHandle) throws ArangoDBException, IOException { + return execute(request, hostHandle, 0); + } + + private Response execute(final Request request, final HostHandle hostHandle, final int attemptCount) throws ArangoDBException, IOException { final AccessType accessType = RequestUtils.determineAccessType(request); Host host = hostHandler.get(hostHandle, accessType); try { @@ -99,12 +103,12 @@ public Response execute(final Request request, final HostHandle hostHandle) thro } } } catch (final ArangoDBException e) { - if (e instanceof ArangoDBRedirectException) { + if (e instanceof ArangoDBRedirectException && attemptCount < 3) { final String location = ((ArangoDBRedirectException) e).getLocation(); final HostDescription redirectHost = HostUtils.createFromLocation(location); hostHandler.closeCurrentOnError(); hostHandler.fail(); - return execute(request, new HostHandle().setHost(redirectHost)); + return execute(request, new HostHandle().setHost(redirectHost), attemptCount + 1); } else { throw e; } diff --git a/src/main/java/com/arangodb/internal/velocystream/VstCommunication.java b/src/main/java/com/arangodb/internal/velocystream/VstCommunication.java index 44aab992b..854ca0f1c 100644 --- a/src/main/java/com/arangodb/internal/velocystream/VstCommunication.java +++ b/src/main/java/com/arangodb/internal/velocystream/VstCommunication.java @@ -138,12 +138,18 @@ public void close() throws IOException { } public R execute(final Request request, final HostHandle hostHandle) throws ArangoDBException { + return execute(request, hostHandle, 0); + } + + protected R execute(final Request request, final HostHandle hostHandle, final int attemptCount) throws ArangoDBException { final C connection = connect(hostHandle, RequestUtils.determineAccessType(request)); - return execute(request, connection); + return execute(request, connection, attemptCount); } protected abstract R execute(final Request request, C connection) throws ArangoDBException; + protected abstract R execute(final Request request, C connection, final int attemptCount) throws ArangoDBException; + protected void checkError(final Response response) throws ArangoDBException { ResponseUtils.checkError(util, response); } diff --git a/src/main/java/com/arangodb/internal/velocystream/VstCommunicationSync.java b/src/main/java/com/arangodb/internal/velocystream/VstCommunicationSync.java index 36b7bdb91..3691f65b0 100644 --- a/src/main/java/com/arangodb/internal/velocystream/VstCommunicationSync.java +++ b/src/main/java/com/arangodb/internal/velocystream/VstCommunicationSync.java @@ -123,6 +123,11 @@ protected VstCommunicationSync(final HostHandler hostHandler, final Integer time @Override protected Response execute(final Request request, final VstConnectionSync connection) throws ArangoDBException { + return execute(request, connection, 0); + } + + @Override + protected Response execute(final Request request, final VstConnectionSync connection, final int attemptCount) throws ArangoDBException { try { final Message requestMessage = createMessage(request); final Message responseMessage = send(requestMessage, connection); @@ -132,11 +137,14 @@ protected Response execute(final Request request, final VstConnectionSync connec } catch (final VPackParserException e) { throw new ArangoDBException(e); } catch (final ArangoDBRedirectException e) { + if (attemptCount >= 3) { + throw e; + } final String location = e.getLocation(); final HostDescription redirectHost = HostUtils.createFromLocation(location); hostHandler.closeCurrentOnError(); hostHandler.fail(); - return execute(request, new HostHandle().setHost(redirectHost)); + return execute(request, new HostHandle().setHost(redirectHost), attemptCount + 1); } }