Skip to content

fixed exceptions handling during shutdown #400

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 3 commits into from
Jun 30, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,12 @@ protected ArangoExecutorAsync executor() {
public void shutdown() throws ArangoDBException {
try {
executor.disconnect();
cp.close();
} catch (final IOException e) {
throw new ArangoDBException(e);
} finally {
try {
cp.close();
} catch (final IOException e) {
e.printStackTrace();
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

package com.arangodb.async.internal;

import com.arangodb.ArangoDBException;
import com.arangodb.async.internal.velocystream.VstCommunicationAsync;
import com.arangodb.internal.ArangoExecutor;
import com.arangodb.internal.DocumentCache;
Expand Down Expand Up @@ -70,8 +71,12 @@ private <T> CompletableFuture<T> execute(
.thenApplyAsync(responseDeserializer::deserialize);
}

public void disconnect() throws IOException {
communication.close();
outgoingExecutor.shutdown();
public void disconnect() {
try {
communication.close();
outgoingExecutor.shutdown();
} catch (final IOException e) {
throw new ArangoDBException(e);
}
}
}
9 changes: 6 additions & 3 deletions src/main/java/com/arangodb/internal/ArangoDBImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,12 @@ protected ArangoExecutorSync executor() {
public void shutdown() throws ArangoDBException {
try {
executor.disconnect();
cp.close();
} catch (final IOException e) {
throw new ArangoDBException(e);
} finally {
try {
cp.close();
} catch (final IOException e) {
e.printStackTrace();
}
}
}

Expand Down
16 changes: 9 additions & 7 deletions src/main/java/com/arangodb/internal/net/FallbackHostHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@ public class FallbackHostHandler implements HostHandler {
private Host lastSuccess;
private int iterations;
private boolean firstOpened;
private HostSet hosts;

public FallbackHostHandler(final HostResolver resolver) {
this.resolver = resolver;
iterations = 0;
current = lastSuccess = resolver.resolve(true, false).getHostsList().get(0);
hosts = resolver.resolve(true, false);
current = lastSuccess = hosts.getHostsList().get(0);
firstOpened = true;
}

Expand All @@ -60,10 +62,11 @@ public void success() {

@Override
public void fail() {
final List<Host> hosts = resolver.resolve(false, false).getHostsList();
final int index = hosts.indexOf(current) + 1;
final boolean inBound = index < hosts.size();
current = hosts.get(inBound ? index : 0);
hosts = resolver.resolve(false, false);
final List<Host> hostList = hosts.getHostsList();
final int index = hostList.indexOf(current) + 1;
final boolean inBound = index < hostList.size();
current = hostList.get(inBound ? index : 0);
if (!inBound) {
iterations++;
}
Expand All @@ -78,14 +81,13 @@ public void reset() {
public void confirm() {
if (firstOpened) {
// after first successful established connection, update host list
resolver.resolve(false, false);
hosts = resolver.resolve(false, false);
firstOpened = false;
}
}

@Override
public void close() {
final HostSet hosts = resolver.resolve(false, false);
hosts.close();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public class RandomHostHandler implements HostHandler {
private final HostResolver resolver;
private final HostHandler fallback;
private Host current;
private HostSet hosts;

public RandomHostHandler(final HostResolver resolver, final HostHandler fallback) {
super();
Expand Down Expand Up @@ -59,9 +60,10 @@ public void fail() {
}

private Host getRandomHost(final boolean initial, final boolean closeConnections) {
final ArrayList<Host> hosts = new ArrayList<>(resolver.resolve(initial, closeConnections).getHostsList());
Collections.shuffle(hosts);
return hosts.get(0);
hosts = resolver.resolve(initial, closeConnections);
final ArrayList<Host> hostList = new ArrayList<>(hosts.getHostsList());
Collections.shuffle(hostList);
return hostList.get(0);
}

@Override
Expand All @@ -75,7 +77,6 @@ public void confirm() {

@Override
public void close() {
final HostSet hosts = resolver.resolve(false, false);
hosts.close();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,22 @@
public class RoundRobinHostHandler implements HostHandler {

private final HostResolver resolver;

private int current;
private int fails;
private Host currentHost;
private HostSet hosts;

public RoundRobinHostHandler(final HostResolver resolver) {
super();
this.resolver = resolver;
resolver.resolve(true, false);
hosts = resolver.resolve(true, false);
current = 0;
fails = 0;
}

@Override
public Host get(final HostHandle hostHandle, AccessType accessType) {

final HostSet hosts = resolver.resolve(false, false);
hosts = resolver.resolve(false, false);
final int size = hosts.getHostsList().size();

if (fails > size) {
Expand Down Expand Up @@ -92,7 +91,6 @@ public void confirm() {

@Override
public void close() {
final HostSet hosts = resolver.resolve(false, false);
hosts.close();
}

Expand Down