From 3e80267fa56d3614b159c8149c7a2b98ad40949e Mon Sep 17 00:00:00 2001 From: Michele Rastelli Date: Thu, 30 Nov 2023 22:38:05 +0100 Subject: [PATCH] fixed resilience tests --- .../internal/net/DirtyReadHostHandler.java | 4 ++-- .../internal/net/FallbackHostHandler.java | 15 ++++++------- .../arangodb/internal/net/HostHandler.java | 2 +- .../internal/net/RandomHostHandler.java | 3 +-- .../internal/net/RoundRobinHostHandler.java | 22 +++++++++---------- .../com/arangodb/http/HttpCommunication.java | 4 ++-- 6 files changed, 23 insertions(+), 27 deletions(-) diff --git a/core/src/main/java/com/arangodb/internal/net/DirtyReadHostHandler.java b/core/src/main/java/com/arangodb/internal/net/DirtyReadHostHandler.java index 84e4d85c3..b54354dcb 100644 --- a/core/src/main/java/com/arangodb/internal/net/DirtyReadHostHandler.java +++ b/core/src/main/java/com/arangodb/internal/net/DirtyReadHostHandler.java @@ -53,9 +53,9 @@ public Host get(final HostHandle hostHandle, final AccessType accessType) { } @Override - public boolean hasNext(HostHandle hostHandle, AccessType accessType) { + public void checkNext(HostHandle hostHandle, AccessType accessType) { this.currentAccessType = accessType; - return determineHostHandler().hasNext(hostHandle, accessType); + determineHostHandler().checkNext(hostHandle, accessType); } @Override diff --git a/core/src/main/java/com/arangodb/internal/net/FallbackHostHandler.java b/core/src/main/java/com/arangodb/internal/net/FallbackHostHandler.java index 04eddf850..67ea54cb9 100644 --- a/core/src/main/java/com/arangodb/internal/net/FallbackHostHandler.java +++ b/core/src/main/java/com/arangodb/internal/net/FallbackHostHandler.java @@ -49,9 +49,13 @@ public FallbackHostHandler(final HostResolver resolver) { @Override public Host get(final HostHandle hostHandle, AccessType accessType) { - if (hasNext(hostHandle, accessType)) { - return current; - } else { + checkNext(hostHandle, accessType); + return current; + } + + @Override + public void checkNext(HostHandle hostHandle, AccessType accessType) { + if (current == lastSuccess && iterations >= 3) { ArangoDBException e = ArangoDBException.of("Cannot contact any host!", new ArangoDBMultipleException(new ArrayList<>(lastFailExceptions))); reset(); @@ -59,11 +63,6 @@ public Host get(final HostHandle hostHandle, AccessType accessType) { } } - @Override - public boolean hasNext(HostHandle hostHandle, AccessType accessType) { - return current != lastSuccess || iterations < 3; - } - @Override public void success() { lastSuccess = current; diff --git a/core/src/main/java/com/arangodb/internal/net/HostHandler.java b/core/src/main/java/com/arangodb/internal/net/HostHandler.java index 23e07604f..e54bfbe65 100644 --- a/core/src/main/java/com/arangodb/internal/net/HostHandler.java +++ b/core/src/main/java/com/arangodb/internal/net/HostHandler.java @@ -31,7 +31,7 @@ public interface HostHandler { Host get(HostHandle hostHandle, AccessType accessType); - boolean hasNext(HostHandle hostHandle, AccessType accessType); + void checkNext(HostHandle hostHandle, AccessType accessType); void success(); diff --git a/core/src/main/java/com/arangodb/internal/net/RandomHostHandler.java b/core/src/main/java/com/arangodb/internal/net/RandomHostHandler.java index 85dee8c39..60338ead1 100644 --- a/core/src/main/java/com/arangodb/internal/net/RandomHostHandler.java +++ b/core/src/main/java/com/arangodb/internal/net/RandomHostHandler.java @@ -53,8 +53,7 @@ public Host get(final HostHandle hostHandle, AccessType accessType) { } @Override - public boolean hasNext(HostHandle hostHandle, AccessType accessType) { - return true; + public void checkNext(HostHandle hostHandle, AccessType accessType) { } @Override diff --git a/core/src/main/java/com/arangodb/internal/net/RoundRobinHostHandler.java b/core/src/main/java/com/arangodb/internal/net/RoundRobinHostHandler.java index afd23779c..4c702ec4b 100644 --- a/core/src/main/java/com/arangodb/internal/net/RoundRobinHostHandler.java +++ b/core/src/main/java/com/arangodb/internal/net/RoundRobinHostHandler.java @@ -53,16 +53,8 @@ public RoundRobinHostHandler(final HostResolver resolver) { @Override public Host get(final HostHandle hostHandle, AccessType accessType) { - hosts = resolver.getHosts(); + checkNext(hostHandle, accessType); final int size = hosts.getHostsList().size(); - - if (fails > size) { - ArangoDBException e = ArangoDBException.of("Cannot contact any host!", - new ArangoDBMultipleException(new ArrayList<>(lastFailExceptions))); - reset(); - throw e; - } - final int index = (int) ((current++) % size); Host host = hosts.getHostsList().get(index); if (hostHandle != null) { @@ -83,10 +75,16 @@ public Host get(final HostHandle hostHandle, AccessType accessType) { } @Override - public boolean hasNext(HostHandle hostHandle, AccessType accessType) { + public void checkNext(HostHandle hostHandle, AccessType accessType) { hosts = resolver.getHosts(); - int size = hosts.getHostsList().size(); - return fails <= size; + final int size = hosts.getHostsList().size(); + + if (fails > size) { + ArangoDBException e = ArangoDBException.of("Cannot contact any host!", + new ArangoDBMultipleException(new ArrayList<>(lastFailExceptions))); + reset(); + throw e; + } } @Override diff --git a/http/src/main/java/com/arangodb/http/HttpCommunication.java b/http/src/main/java/com/arangodb/http/HttpCommunication.java index e52820377..7166ae3d5 100644 --- a/http/src/main/java/com/arangodb/http/HttpCommunication.java +++ b/http/src/main/java/com/arangodb/http/HttpCommunication.java @@ -134,8 +134,8 @@ private void handleException(boolean isSafe, Throwable e, HostHandle hostHandle, if (hostHandle != null && hostHandle.getHost() != null) { hostHandle.setHost(null); } - boolean hasNextHost = hostHandler.hasNext(hostHandle, RequestUtils.determineAccessType(request)); - if (hasNextHost && isSafe) { + hostHandler.checkNext(hostHandle, RequestUtils.determineAccessType(request)); + if (isSafe) { Host nextHost = hostHandler.get(hostHandle, RequestUtils.determineAccessType(request)); LOGGER.warn("Could not connect to {} while executing request [id={}]", host.getDescription(), reqId, ioEx);