Skip to content

Commit 3ff7b95

Browse files
BCaxelbeckerhkernbach
authored andcommitted
fix for issue 233 (#250)
* fix for #233
1 parent ab5a5ab commit 3ff7b95

File tree

13 files changed

+187
-46
lines changed

13 files changed

+187
-46
lines changed

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,19 @@ public class ExtendedHostResolver implements HostResolver {
3838
private static final Logger LOGGER = LoggerFactory.getLogger(ExtendedHostResolver.class);
3939

4040
private static final long MAX_CACHE_TIME = 60 * 60 * 1000;
41+
4142
private EndpointResolver resolver;
42-
private final List<Host> hosts;
43+
private HostSet hosts;
44+
4345
private final Integer maxConnections;
4446
private final ConnectionFactory connectionFactory;
47+
4548
private long lastUpdate;
4649

4750
public ExtendedHostResolver(final List<Host> hosts, final Integer maxConnections,
4851
final ConnectionFactory connectionFactory) {
4952
super();
50-
this.hosts = hosts;
53+
this.hosts = new HostSet(hosts);
5154
this.maxConnections = maxConnections;
5255
this.connectionFactory = connectionFactory;
5356
lastUpdate = 0;
@@ -59,7 +62,8 @@ public void init(final EndpointResolver resolver) {
5962
}
6063

6164
@Override
62-
public List<Host> resolve(final boolean initial, final boolean closeConnections) {
65+
66+
public HostSet resolve(final boolean initial, final boolean closeConnections) {
6367

6468
if (!initial && isExpired()) {
6569

@@ -69,7 +73,6 @@ public List<Host> resolve(final boolean initial, final boolean closeConnections)
6973
LOGGER.info("Resolve " + endpoints.size() + " Endpoints");
7074
LOGGER.debug("Endpoints " + Arrays.deepToString(endpoints.toArray()));
7175

72-
7376
if (!endpoints.isEmpty()) {
7477
hosts.clear();
7578
}
@@ -82,7 +85,7 @@ public List<Host> resolve(final boolean initial, final boolean closeConnections)
8285
final String[] s = endpoint.replaceAll(".*://", "").split(":");
8386
if (s.length == 2) {
8487
final HostDescription description = new HostDescription(s[0], Integer.valueOf(s[1]));
85-
hosts.add(HostUtils.createHost(description, maxConnections, connectionFactory));
88+
hosts.addHost(HostUtils.createHost(description, maxConnections, connectionFactory));
8689
} else {
8790
LOGGER.warn("Skip Endpoint (Missung Port)" + endpoint);
8891
}

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public class FallbackHostHandler implements HostHandler {
3838
public FallbackHostHandler(final HostResolver resolver) {
3939
this.resolver = resolver;
4040
iterations = 0;
41-
current = lastSuccess = resolver.resolve(true, false).get(0);
41+
current = lastSuccess = resolver.resolve(true, false).getHostsList().get(0);
4242
firstOpened = true;
4343
}
4444

@@ -54,7 +54,7 @@ public void success() {
5454

5555
@Override
5656
public void fail() {
57-
final List<Host> hosts = resolver.resolve(false, false);
57+
final List<Host> hosts = resolver.resolve(false, false).getHostsList();
5858
final int index = hosts.indexOf(current) + 1;
5959
final boolean inBound = index < hosts.size();
6060
current = hosts.get(inBound ? index : 0);
@@ -79,10 +79,8 @@ public void confirm() {
7979

8080
@Override
8181
public void close() throws IOException {
82-
final List<Host> hosts = resolver.resolve(false, false);
83-
for (final Host host : hosts) {
84-
host.close();
85-
}
82+
final HostSet hosts = resolver.resolve(false, false);
83+
hosts.close();
8684
}
8785

8886
@Override

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,9 @@ public void closeOnError() {
6363
}
6464
}
6565

66+
@Override
67+
public String toString() {
68+
return "HostImpl [connectionPool=" + connectionPool + ", description=" + description + "]";
69+
}
70+
6671
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
package com.arangodb.internal.net;
2222

2323
import java.util.Collection;
24-
import java.util.List;
2524

2625
import com.arangodb.ArangoDBException;
2726

@@ -37,6 +36,6 @@ public interface EndpointResolver {
3736

3837
void init(final EndpointResolver resolver);
3938

40-
List<Host> resolve(boolean initial, boolean closeConnections);
39+
HostSet resolve(boolean initial, boolean closeConnections);
4140

4241
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.arangodb.internal.net;
2+
3+
import java.io.IOException;
4+
import java.util.ArrayList;
5+
import java.util.Collections;
6+
import java.util.List;
7+
8+
import org.slf4j.Logger;
9+
import org.slf4j.LoggerFactory;
10+
11+
public class HostSet {
12+
13+
private static final Logger LOGGER = LoggerFactory.getLogger(HostSet.class);
14+
15+
private ArrayList<Host> hosts = new ArrayList<Host>();
16+
17+
public HostSet() {
18+
super();
19+
}
20+
21+
public HostSet(List<Host> hosts) {
22+
super();
23+
24+
for (Host host : hosts) {
25+
addHost(host);
26+
}
27+
28+
}
29+
30+
public List<Host> getHostsList() {
31+
return Collections.unmodifiableList(hosts);
32+
}
33+
34+
public void addHost(Host newHost) {
35+
36+
if(hosts.contains(newHost)) {
37+
LOGGER.debug("Host" + newHost + " allready in Set");
38+
} else {
39+
hosts.add(newHost);
40+
LOGGER.info("Added Host " + newHost + " - now " + hosts.size() + " Hosts in List");
41+
}
42+
43+
}
44+
45+
public void close() {
46+
47+
LOGGER.info("Close all Hosts in Set");
48+
49+
for (Host host : hosts) {
50+
51+
try {
52+
53+
LOGGER.debug("Try to close Host " + host);
54+
host.close();
55+
56+
} catch (IOException e) {
57+
LOGGER.warn("Error during closing the Host " + host, e);
58+
}
59+
60+
}
61+
62+
}
63+
64+
public void clear() {
65+
66+
LOGGER.info("Clear all Hosts in Set");
67+
68+
close();
69+
hosts.clear();
70+
71+
}
72+
73+
}

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ public void fail() {
6363
}
6464

6565
private Host getRandomHost(final boolean initial, final boolean closeConnections) {
66-
final ArrayList<Host> hosts = new ArrayList<Host>(resolver.resolve(initial, closeConnections));
66+
67+
final ArrayList<Host> hosts = new ArrayList<Host>(resolver.resolve(initial, closeConnections).getHostsList());
6768
Collections.shuffle(hosts);
6869
return hosts.get(0);
6970
}
@@ -79,10 +80,8 @@ public void confirm() {
7980

8081
@Override
8182
public void close() throws IOException {
82-
final List<Host> hosts = resolver.resolve(false, false);
83-
for (final Host host : hosts) {
84-
host.close();
85-
}
83+
final HostSet hosts = resolver.resolve(false, false);
84+
hosts.close();
8685
}
8786

8887
@Override

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
package com.arangodb.internal.net;
2222

2323
import java.io.IOException;
24-
import java.util.List;
2524

2625
/**
2726
* @author Mark Vollmary
@@ -30,6 +29,7 @@
3029
public class RoundRobinHostHandler implements HostHandler {
3130

3231
private final HostResolver resolver;
32+
3333
private int current;
3434
private int fails;
3535

@@ -43,18 +43,21 @@ public RoundRobinHostHandler(final HostResolver resolver) {
4343

4444
@Override
4545
public Host get(final HostHandle hostHandle, AccessType accessType) {
46-
final List<Host> hosts = resolver.resolve(false, false);
47-
final int size = hosts.size();
46+
47+
final HostSet hosts = resolver.resolve(false, false);
48+
final int size = hosts.getHostsList().size();
49+
4850
if (fails > size) {
4951
return null;
5052
}
53+
5154
final int index = (current++) % size;
52-
Host host = hosts.get(index);
55+
Host host = hosts.getHostsList().get(index);
5356
if (hostHandle != null) {
5457
final HostDescription hostDescription = hostHandle.getHost();
5558
if (hostDescription != null) {
5659
for (int i = index; i < index + size; i++) {
57-
host = hosts.get(i % size);
60+
host = hosts.getHostsList().get(i % size);
5861
if (hostDescription.equals(host.getDescription())) {
5962
break;
6063
}
@@ -87,16 +90,13 @@ public void confirm() {
8790

8891
@Override
8992
public void close() throws IOException {
90-
final List<Host> hosts = resolver.resolve(false, false);
91-
for (final Host host : hosts) {
92-
host.close();
93-
}
93+
final HostSet hosts = resolver.resolve(false, false);
94+
hosts.close();
9495
}
9596

9697
@Override
9798
public void closeCurrentOnError() {
98-
final List<Host> hosts = resolver.resolve(false, false);
99-
hosts.get(current).closeOnError();
99+
currentHost.closeOnError();
100100
}
101101

102102
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ public void init(final EndpointResolver resolver) {
4040
}
4141

4242
@Override
43-
public List<Host> resolve(final boolean initial, final boolean closeConnections) {
44-
return hosts;
43+
public HostSet resolve(final boolean initial, final boolean closeConnections) {
44+
return new HostSet(hosts);
4545
}
4646

4747
}

src/main/java/com/arangodb/internal/util/IOUtils.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,19 @@
2727
import java.io.InputStreamReader;
2828
import java.io.UnsupportedEncodingException;
2929

30+
import org.slf4j.Logger;
31+
import org.slf4j.LoggerFactory;
32+
33+
import com.arangodb.internal.net.Host;
34+
import com.arangodb.internal.net.HostSet;
35+
3036
/**
3137
* @author Mark Vollmary
3238
*
3339
*/
3440
public final class IOUtils {
41+
42+
private static final Logger LOGGER = LoggerFactory.getLogger(IOUtils.class);
3543

3644
private IOUtils() {
3745
}
@@ -73,4 +81,5 @@ public static byte[] toByteArray(final InputStream input) throws IOException {
7381
buffer.flush();
7482
return buffer.toByteArray();
7583
}
84+
7685
}

src/main/java/com/arangodb/internal/velocystream/internal/VstConnection.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ public boolean isOpen() {
8787
}
8888

8989
public synchronized void open() throws IOException {
90+
9091
if (isOpen()) {
9192
return;
9293
}
@@ -102,8 +103,7 @@ public synchronized void open() throws IOException {
102103
} else {
103104
socket = SocketFactory.getDefault().createSocket();
104105
}
105-
socket.connect(new InetSocketAddress(host.getHost(), host.getPort()),
106-
timeout != null ? timeout : ArangoDefaults.DEFAULT_TIMEOUT);
106+
socket.connect(new InetSocketAddress(host.getHost(), host.getPort()), timeout != null ? timeout : ArangoDefaults.DEFAULT_TIMEOUT);
107107
socket.setKeepAlive(true);
108108
socket.setTcpNoDelay(true);
109109
if (LOGGER.isDebugEnabled()) {
@@ -124,6 +124,9 @@ public synchronized void open() throws IOException {
124124
executor.submit(new Callable<Void>() {
125125
@Override
126126
public Void call() throws Exception {
127+
128+
LOGGER.info("Start Callable for " + this.toString());
129+
127130
final long openTime = new Date().getTime();
128131
final Long ttlTime = ttl != null ? openTime + ttl : null;
129132
final ChunkStore chunkStore = new ChunkStore(messageStore);
@@ -152,6 +155,9 @@ public Void call() throws Exception {
152155
break;
153156
}
154157
}
158+
159+
LOGGER.info("Stop Callable for " + this.toString());
160+
155161
return null;
156162
}
157163
});

src/test/java/com/arangodb/ArangoVertexCollectionTest.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,12 @@
2525
import static org.hamcrest.Matchers.not;
2626
import static org.hamcrest.Matchers.notNullValue;
2727
import static org.hamcrest.Matchers.nullValue;
28+
import static org.junit.Assert.assertEquals;
2829
import static org.junit.Assert.assertThat;
2930
import static org.junit.Assert.fail;
3031

3132
import java.util.Collection;
33+
import java.util.UUID;
3234

3335
import org.junit.After;
3436
import org.junit.Test;
@@ -92,6 +94,43 @@ public void insertVertex() {
9294
assertThat(document.getKey(), is(vertex.getKey()));
9395
}
9496

97+
@Test
98+
public void duplicateInsertSameObjectVertex() {
99+
100+
final ArangoVertexCollection vertexCollection = db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME);
101+
102+
// #########################################################
103+
// Create a new BaseDocument
104+
// #########################################################
105+
106+
UUID uuid = UUID.randomUUID();
107+
BaseDocument bd = new BaseDocument();
108+
bd.setKey(uuid.toString());
109+
bd.addAttribute("name", "Paul");
110+
111+
vertexCollection.insertVertex(bd);
112+
113+
UUID uuid2 = UUID.randomUUID();
114+
BaseDocument bd2 = new BaseDocument();
115+
bd2.setKey(uuid2.toString());
116+
bd2.addAttribute("name", "Paul");
117+
118+
vertexCollection.insertVertex(bd2);
119+
120+
// final BaseDocument document = db.collection(COLLECTION_NAME).getDocument(uuid.toString(), BaseDocument.class, null);
121+
// assertThat(document, is(notNullValue()));
122+
// assertThat(document.getKey(), is(uuid.toString()));
123+
//
124+
// try {
125+
// vertexCollection.insertVertex(bd);
126+
// } catch (ArangoDBException e) {
127+
// assertEquals(e.getResponseCode().intValue(), 409);
128+
// }
129+
130+
System.out.println("aaaaa");
131+
132+
}
133+
95134
@Test
96135
public void insertVertexUpdateRev() {
97136
final BaseDocument doc = new BaseDocument();

0 commit comments

Comments
 (0)