Skip to content

Commit 599b9c5

Browse files
authored
fixed exceptions handling during shutdown (#400)
* fixed exceptions handling during shutdown * fixed executor service shutdown * logging exception using slf4j logger
1 parent a8dc77a commit 599b9c5

File tree

6 files changed

+42
-25
lines changed

6 files changed

+42
-25
lines changed

src/main/java/com/arangodb/async/internal/ArangoDBAsyncImpl.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
import com.arangodb.model.UserUpdateOptions;
4141
import com.arangodb.velocystream.Request;
4242
import com.arangodb.velocystream.Response;
43+
import org.slf4j.Logger;
44+
import org.slf4j.LoggerFactory;
4345

4446
import java.io.IOException;
4547
import java.util.Collection;
@@ -50,6 +52,8 @@
5052
*/
5153
public class ArangoDBAsyncImpl extends InternalArangoDB<ArangoExecutorAsync> implements ArangoDBAsync {
5254

55+
private static final Logger LOGGER = LoggerFactory.getLogger(ArangoDBAsyncImpl.class);
56+
5357
private final CommunicationProtocol cp;
5458

5559
public ArangoDBAsyncImpl(
@@ -82,9 +86,12 @@ protected ArangoExecutorAsync executor() {
8286
public void shutdown() throws ArangoDBException {
8387
try {
8488
executor.disconnect();
85-
cp.close();
86-
} catch (final IOException e) {
87-
throw new ArangoDBException(e);
89+
} finally {
90+
try {
91+
cp.close();
92+
} catch (final IOException e) {
93+
LOGGER.error("Got exception during shutdown:", e);
94+
}
8895
}
8996
}
9097

src/main/java/com/arangodb/async/internal/ArangoExecutorAsync.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
package com.arangodb.async.internal;
2222

23+
import com.arangodb.ArangoDBException;
2324
import com.arangodb.async.internal.velocystream.VstCommunicationAsync;
2425
import com.arangodb.internal.ArangoExecutor;
2526
import com.arangodb.internal.DocumentCache;
@@ -70,8 +71,13 @@ private <T> CompletableFuture<T> execute(
7071
.thenApplyAsync(responseDeserializer::deserialize);
7172
}
7273

73-
public void disconnect() throws IOException {
74-
communication.close();
75-
outgoingExecutor.shutdown();
74+
public void disconnect() {
75+
try {
76+
communication.close();
77+
} catch (final IOException e) {
78+
throw new ArangoDBException(e);
79+
} finally {
80+
outgoingExecutor.shutdown();
81+
}
7682
}
7783
}

src/main/java/com/arangodb/internal/ArangoDBImpl.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,12 @@ protected ArangoExecutorSync executor() {
114114
public void shutdown() throws ArangoDBException {
115115
try {
116116
executor.disconnect();
117-
cp.close();
118-
} catch (final IOException e) {
119-
throw new ArangoDBException(e);
117+
} finally {
118+
try {
119+
cp.close();
120+
} catch (final IOException e) {
121+
LOGGER.error("Got exception during shutdown:", e);
122+
}
120123
}
121124
}
122125

src/main/java/com/arangodb/internal/net/FallbackHostHandler.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,13 @@ public class FallbackHostHandler implements HostHandler {
3434
private Host lastSuccess;
3535
private int iterations;
3636
private boolean firstOpened;
37+
private HostSet hosts;
3738

3839
public FallbackHostHandler(final HostResolver resolver) {
3940
this.resolver = resolver;
4041
iterations = 0;
41-
current = lastSuccess = resolver.resolve(true, false).getHostsList().get(0);
42+
hosts = resolver.resolve(true, false);
43+
current = lastSuccess = hosts.getHostsList().get(0);
4244
firstOpened = true;
4345
}
4446

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

6163
@Override
6264
public void fail() {
63-
final List<Host> hosts = resolver.resolve(false, false).getHostsList();
64-
final int index = hosts.indexOf(current) + 1;
65-
final boolean inBound = index < hosts.size();
66-
current = hosts.get(inBound ? index : 0);
65+
hosts = resolver.resolve(false, false);
66+
final List<Host> hostList = hosts.getHostsList();
67+
final int index = hostList.indexOf(current) + 1;
68+
final boolean inBound = index < hostList.size();
69+
current = hostList.get(inBound ? index : 0);
6770
if (!inBound) {
6871
iterations++;
6972
}
@@ -78,14 +81,13 @@ public void reset() {
7881
public void confirm() {
7982
if (firstOpened) {
8083
// after first successful established connection, update host list
81-
resolver.resolve(false, false);
84+
hosts = resolver.resolve(false, false);
8285
firstOpened = false;
8386
}
8487
}
8588

8689
@Override
8790
public void close() {
88-
final HostSet hosts = resolver.resolve(false, false);
8991
hosts.close();
9092
}
9193

src/main/java/com/arangodb/internal/net/RandomHostHandler.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public class RandomHostHandler implements HostHandler {
3131
private final HostResolver resolver;
3232
private final HostHandler fallback;
3333
private Host current;
34+
private HostSet hosts;
3435

3536
public RandomHostHandler(final HostResolver resolver, final HostHandler fallback) {
3637
super();
@@ -59,9 +60,10 @@ public void fail() {
5960
}
6061

6162
private Host getRandomHost(final boolean initial, final boolean closeConnections) {
62-
final ArrayList<Host> hosts = new ArrayList<>(resolver.resolve(initial, closeConnections).getHostsList());
63-
Collections.shuffle(hosts);
64-
return hosts.get(0);
63+
hosts = resolver.resolve(initial, closeConnections);
64+
final ArrayList<Host> hostList = new ArrayList<>(hosts.getHostsList());
65+
Collections.shuffle(hostList);
66+
return hostList.get(0);
6567
}
6668

6769
@Override
@@ -75,7 +77,6 @@ public void confirm() {
7577

7678
@Override
7779
public void close() {
78-
final HostSet hosts = resolver.resolve(false, false);
7980
hosts.close();
8081
}
8182

src/main/java/com/arangodb/internal/net/RoundRobinHostHandler.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,22 @@
2828
public class RoundRobinHostHandler implements HostHandler {
2929

3030
private final HostResolver resolver;
31-
3231
private int current;
3332
private int fails;
3433
private Host currentHost;
34+
private HostSet hosts;
3535

3636
public RoundRobinHostHandler(final HostResolver resolver) {
3737
super();
3838
this.resolver = resolver;
39-
resolver.resolve(true, false);
39+
hosts = resolver.resolve(true, false);
4040
current = 0;
4141
fails = 0;
4242
}
4343

4444
@Override
4545
public Host get(final HostHandle hostHandle, AccessType accessType) {
46-
47-
final HostSet hosts = resolver.resolve(false, false);
46+
hosts = resolver.resolve(false, false);
4847
final int size = hosts.getHostsList().size();
4948

5049
if (fails > size) {
@@ -92,7 +91,6 @@ public void confirm() {
9291

9392
@Override
9493
public void close() {
95-
final HostSet hosts = resolver.resolve(false, false);
9694
hosts.close();
9795
}
9896

0 commit comments

Comments
 (0)