Skip to content

[DE-528] non-blocking acquireHostList #521

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions core/src/main/java/com/arangodb/ArangoDB.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import com.arangodb.entity.*;
import com.arangodb.internal.ArangoDBImpl;
import com.arangodb.internal.ArangoExecutorSync;
import com.arangodb.internal.InternalArangoDBBuilder;
import com.arangodb.internal.net.*;
import com.arangodb.model.*;
Expand Down Expand Up @@ -363,10 +364,13 @@ public ArangoDB build() {
HostHandler hostHandler = createHostHandler(hostResolver);
hostHandler.setJwt(config.getJwt());

CommunicationProtocol protocol = protocolProvider.createProtocol(config, hostHandler);
ArangoExecutorSync executor = new ArangoExecutorSync(protocol, config);
hostResolver.init(executor, config.getInternalSerde());

return new ArangoDBImpl(
config,
hostResolver,
protocolProvider,
protocol,
hostHandler
);
}
Expand Down
7 changes: 5 additions & 2 deletions core/src/main/java/com/arangodb/ArangoDBException.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public ArangoDBException(final ErrorEntity errorEntity) {
super(String.format("Response: %s, Error: %s - %s", errorEntity.getCode(), errorEntity.getErrorNum(),
errorEntity.getErrorMessage()));
this.entity = errorEntity;
this.responseCode = null;
this.responseCode = errorEntity.getCode();
this.requestId = null;
}

Expand Down Expand Up @@ -117,6 +117,9 @@ public static ArangoDBException of(Throwable t, Long requestId) {

private static ArangoDBException of(String message, Throwable t, Long requestId) {
Objects.requireNonNull(t);
if (t instanceof CompletionException) {
return of(message, t.getCause(), requestId);
}
Throwable cause = unwrapCause(t);
String msg = message != null ? message
: t.getMessage() != null ? t.getMessage()
Expand All @@ -141,7 +144,7 @@ private static ArangoDBException of(String message, Throwable t, Long requestId)
}

private static Throwable unwrapCause(Throwable t) {
if (t instanceof ArangoDBException || t instanceof CompletionException) {
if (t instanceof ArangoDBException) {
return unwrapCause(t.getCause());
}
return t;
Expand Down
8 changes: 3 additions & 5 deletions core/src/main/java/com/arangodb/internal/ArangoDBImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@
import com.arangodb.*;
import com.arangodb.entity.*;
import com.arangodb.internal.config.ArangoConfig;
import com.arangodb.internal.net.CommunicationProtocol;
import com.arangodb.internal.net.HostHandler;
import com.arangodb.internal.net.HostResolver;
import com.arangodb.internal.net.ProtocolProvider;
import com.arangodb.internal.serde.SerdeUtils;
import com.arangodb.model.*;
import org.slf4j.Logger;
Expand All @@ -44,11 +43,10 @@ public class ArangoDBImpl extends InternalArangoDB implements ArangoDB {
private final HostHandler hostHandler;

public ArangoDBImpl(final ArangoConfig config,
final HostResolver hostResolver, final ProtocolProvider protocolProvider,
final CommunicationProtocol protocol,
final HostHandler hostHandler) {
super(protocolProvider.createProtocol(config, hostHandler), config, config.getInternalSerde());
super(protocol, config);
this.hostHandler = hostHandler;
hostResolver.init(executorSync(), getSerde());
LOGGER.debug("ArangoDB Client is ready to use");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,8 @@ public abstract class ArangoExecuteable implements ArangoSerdeAccessor {
private final ArangoExecutorAsync executorAsync;
private final InternalSerde serde;

protected ArangoExecuteable(final CommunicationProtocol protocol,
final ArangoConfig config,
final InternalSerde serde) {
this(new ArangoExecutorSync(protocol, config), new ArangoExecutorAsync(protocol, config), serde);
protected ArangoExecuteable(final CommunicationProtocol protocol, final ArangoConfig config) {
this(new ArangoExecutorSync(protocol, config), new ArangoExecutorAsync(protocol, config), config.getInternalSerde());
}

protected ArangoExecuteable(final ArangoExecuteable other) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ public abstract class InternalArangoDB extends ArangoExecuteable {
private static final String PATH_API_USER = "/_api/user";
private static final String PATH_API_QUERY_RULES = "/_api/query/rules";

protected InternalArangoDB(final CommunicationProtocol protocol, final ArangoConfig config, final InternalSerde util) {
super(protocol, config, util);
protected InternalArangoDB(final CommunicationProtocol protocol, final ArangoConfig config) {
super(protocol, config);
}

protected InternalArangoDB(final ArangoExecuteable other) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -337,10 +337,9 @@ protected HostResolver createHostResolver(final Collection<Host> hosts, final Co
LOG.debug("Use SimpleHostResolver");
return new SimpleHostResolver(new ArrayList<>(hosts));
}

}

protected <C extends Connection> Collection<Host> createHostList(final ConnectionFactory connectionFactory) {
protected Collection<Host> createHostList(final ConnectionFactory connectionFactory) {
final Collection<Host> hostList = new ArrayList<>();
for (final HostDescription host : config.getHosts()) {
hostList.add(HostUtils.createHost(host, config, connectionFactory));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,27 +72,12 @@ public void reset() {
determineHostHandler().reset();
}

@Override
public void confirm() {
determineHostHandler().confirm();
}

@Override
public void close() throws IOException {
master.close();
follower.close();
}

@Override
public void closeCurrentOnError() {
determineHostHandler().closeCurrentOnError();
}

@Override
public void closeCurrentOnErrorIfNotMatch(HostDescription host) {
determineHostHandler().closeCurrentOnErrorIfNotMatch(host);
}

@Override
public void setJwt(String jwt) {
master.setJwt(jwt);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
import org.slf4j.LoggerFactory;

import java.util.*;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

import static com.arangodb.internal.serde.SerdeUtils.constructParametricType;

Expand All @@ -48,10 +52,10 @@ public class ExtendedHostResolver implements HostResolver {
private final ArangoConfig config;
private final ConnectionFactory connectionFactory;
private final Integer acquireHostListInterval;
private long lastUpdate;
private final ScheduledExecutorService scheduler;
private ArangoExecutorSync executor;
private InternalSerde arangoSerialization;

private ScheduledFuture<?> schedule;

public ExtendedHostResolver(final List<Host> hosts, final ArangoConfig config,
final ConnectionFactory connectionFactory, Integer acquireHostListInterval) {
Expand All @@ -61,22 +65,34 @@ public ExtendedHostResolver(final List<Host> hosts, final ArangoConfig config,
this.config = config;
this.connectionFactory = connectionFactory;

lastUpdate = 0;
scheduler = Executors.newSingleThreadScheduledExecutor(r -> {
Thread t = Executors.defaultThreadFactory().newThread(r);
t.setDaemon(true);
return t;
}
);
}

@Override
public void init(ArangoExecutorSync executor, InternalSerde arangoSerialization) {
this.executor = executor;
this.arangoSerialization = arangoSerialization;
resolve();
schedule = scheduler.scheduleAtFixedRate(this::resolve, acquireHostListInterval, acquireHostListInterval, TimeUnit.MILLISECONDS);
}

@Override
public HostSet resolve(boolean initial, boolean closeConnections) {

if (!initial && isExpired()) {
public void close() {
schedule.cancel(false);
scheduler.shutdown();
}

lastUpdate = System.currentTimeMillis();
@Override
public HostSet getHosts() {
return hosts;
}

private void resolve() {
final Collection<String> endpoints = resolveFromServer();
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Resolve {} Endpoints", endpoints.size());
Expand Down Expand Up @@ -110,17 +126,11 @@ public HostSet resolve(boolean initial, boolean closeConnections) {
}
}
hosts.clearAllMarkedForDeletion();
}

return hosts;
}

private Collection<String> resolveFromServer() {

Collection<String> response;

try {

response = executor.execute(
new InternalRequest(ArangoRequestParam.SYSTEM, RequestType.GET, "/_api/cluster/endpoints"),
response1 -> {
Expand All @@ -136,7 +146,6 @@ private Collection<String> resolveFromServer() {
}, null);
} catch (final ArangoDBException e) {
final Integer responseCode = e.getResponseCode();

// responseCode == 403: single server < 3.7
// responseCode == 501: single server >= 3.7
if (responseCode != null && (responseCode == 403 || responseCode == 501)) {
Expand All @@ -145,12 +154,6 @@ private Collection<String> resolveFromServer() {
throw e;
}
}

return response;
}

private boolean isExpired() {
return System.currentTimeMillis() > (lastUpdate + acquireHostListInterval);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,14 @@ public class FallbackHostHandler implements HostHandler {
private Host current;
private Host lastSuccess;
private int iterations;
private boolean firstOpened;
private HostSet hosts;

public FallbackHostHandler(final HostResolver resolver) {
this.resolver = resolver;
lastFailExceptions = new ArrayList<>();
reset();
hosts = resolver.resolve(true, false);
hosts = resolver.getHosts();
current = lastSuccess = hosts.getHostsList().get(0);
firstOpened = true;
}

@Override
Expand All @@ -69,7 +67,7 @@ public void success() {

@Override
public void fail(Exception exception) {
hosts = resolver.resolve(false, false);
hosts = resolver.getHosts();
final List<Host> hostList = hosts.getHostsList();
final int index = hostList.indexOf(current) + 1;
final boolean inBound = index < hostList.size();
Expand All @@ -93,30 +91,10 @@ public void reset() {
lastFailExceptions.clear();
}

@Override
public void confirm() {
if (firstOpened) {
// after first successful established connection, update host list
hosts = resolver.resolve(false, false);
firstOpened = false;
}
}

@Override
public void close() {
hosts.close();
}

@Override
public void closeCurrentOnError() {
current.closeOnError();
}

@Override
public synchronized void closeCurrentOnErrorIfNotMatch(HostDescription host) {
if (!host.equals(current.getDescription())) {
closeCurrentOnError();
}
resolver.close();
}

@Override
Expand Down
6 changes: 0 additions & 6 deletions core/src/main/java/com/arangodb/internal/net/HostHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,8 @@ public interface HostHandler {

void reset();

void confirm();

void close() throws IOException;

void closeCurrentOnError();

void closeCurrentOnErrorIfNotMatch(HostDescription host);

void setJwt(String jwt);

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,12 @@
*/
public interface HostResolver {

void init(ArangoExecutorSync executorSync, InternalSerde arangoSerialization);
default void init(ArangoExecutorSync executorSync, InternalSerde arangoSerialization) {
}

HostSet resolve(boolean initial, boolean closeConnections);
default void close() {
}

HostSet getHosts();

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,15 @@ public RandomHostHandler(final HostResolver resolver, final HostHandler fallback
super();
this.resolver = resolver;
this.fallback = fallback;
current = getRandomHost(true, false);
hosts = resolver.getHosts();
current = getRandomHost();
}

@Override
public Host get(final HostHandle hostHandle, AccessType accessType) {
if (current == null) {
current = getRandomHost(false, true);
hosts = resolver.getHosts();
current = getRandomHost();
}
return current;
}
Expand All @@ -68,8 +70,7 @@ public synchronized void failIfNotMatch(HostDescription host, Exception exceptio
}
}

private Host getRandomHost(final boolean initial, final boolean closeConnections) {
hosts = resolver.resolve(initial, closeConnections);
private Host getRandomHost() {
final ArrayList<Host> hostList = new ArrayList<>(hosts.getHostsList());
Collections.shuffle(hostList);
return hostList.get(0);
Expand All @@ -80,25 +81,10 @@ public void reset() {
fallback.reset();
}

@Override
public void confirm() {
}

@Override
public void close() {
hosts.close();
}

@Override
public void closeCurrentOnError() {
current.closeOnError();
}

@Override
public synchronized void closeCurrentOnErrorIfNotMatch(HostDescription host) {
if (!host.equals(current.getDescription())) {
closeCurrentOnError();
}
resolver.close();
}

@Override
Expand Down
Loading