Skip to content

Commit bb3f0b6

Browse files
author
mpv1989
committed
Fix load balancing stickiness
1 parent 73dd9ef commit bb3f0b6

File tree

7 files changed

+27
-24
lines changed

7 files changed

+27
-24
lines changed

Diff for: src/main/java/com/arangodb/ArangoCursor.java

+4-8
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
import com.arangodb.internal.ArangoCursorExecute;
3535
import com.arangodb.internal.ArangoCursorIterator;
3636
import com.arangodb.internal.InternalArangoDatabase;
37-
import com.arangodb.internal.net.HostHandle;
3837

3938
/**
4039
* @author Mark Vollmary
@@ -46,25 +45,22 @@ public class ArangoCursor<T> implements Iterable<T>, Iterator<T>, Closeable {
4645
protected final ArangoCursorIterator<T> iterator;
4746
private final String id;
4847
private final ArangoCursorExecute execute;
49-
private final HostHandle hostHandle;
5048

5149
protected ArangoCursor(final InternalArangoDatabase<?, ?, ?, ?> db, final ArangoCursorExecute execute,
5250
final Class<T> type, final CursorEntity result) {
5351
super();
5452
this.execute = execute;
5553
this.type = type;
56-
hostHandle = new HostHandle();
57-
iterator = createIterator(this, db, execute, result, hostHandle);
54+
iterator = createIterator(this, db, execute, result);
5855
id = result.getId();
5956
}
6057

6158
protected ArangoCursorIterator<T> createIterator(
6259
final ArangoCursor<T> cursor,
6360
final InternalArangoDatabase<?, ?, ?, ?> db,
6461
final ArangoCursorExecute execute,
65-
final CursorEntity result,
66-
final HostHandle hostHandle) {
67-
return new ArangoCursorIterator<T>(cursor, execute, db, result, hostHandle);
62+
final CursorEntity result) {
63+
return new ArangoCursorIterator<T>(cursor, execute, db, result);
6864
}
6965

7066
/**
@@ -107,7 +103,7 @@ public boolean isCached() {
107103
@Override
108104
public void close() throws IOException {
109105
if (id != null) {
110-
execute.close(id, hostHandle);
106+
execute.close(id);
111107
}
112108
}
113109

Diff for: src/main/java/com/arangodb/ArangoDatabase.java

+12-7
Original file line numberDiff line numberDiff line change
@@ -355,8 +355,9 @@ public <T> ArangoCursor<T> query(
355355
final AqlQueryOptions options,
356356
final Class<T> type) throws ArangoDBException {
357357
final Request request = queryRequest(query, bindVars, options);
358-
final CursorEntity result = executor.execute(request, CursorEntity.class);
359-
return createCursor(result, type);
358+
final HostHandle hostHandle = new HostHandle();
359+
final CursorEntity result = executor.execute(request, CursorEntity.class, hostHandle);
360+
return createCursor(result, type, hostHandle);
360361
}
361362

362363
/**
@@ -373,19 +374,23 @@ public <T> ArangoCursor<T> query(
373374
* @throws ArangoDBException
374375
*/
375376
public <T> ArangoCursor<T> cursor(final String cursorId, final Class<T> type) throws ArangoDBException {
376-
final CursorEntity result = executor.execute(queryNextRequest(cursorId), CursorEntity.class);
377-
return createCursor(result, type);
377+
final HostHandle hostHandle = new HostHandle();
378+
final CursorEntity result = executor.execute(queryNextRequest(cursorId), CursorEntity.class, hostHandle);
379+
return createCursor(result, type, hostHandle);
378380
}
379381

380-
private <T> ArangoCursor<T> createCursor(final CursorEntity result, final Class<T> type) {
382+
private <T> ArangoCursor<T> createCursor(
383+
final CursorEntity result,
384+
final Class<T> type,
385+
final HostHandle hostHandle) {
381386
final ArangoCursorExecute execute = new ArangoCursorExecute() {
382387
@Override
383-
public CursorEntity next(final String id, final HostHandle hostHandle) {
388+
public CursorEntity next(final String id) {
384389
return executor.execute(queryNextRequest(id), CursorEntity.class, hostHandle);
385390
}
386391

387392
@Override
388-
public void close(final String id, final HostHandle hostHandle) {
393+
public void close(final String id) {
389394
executor.execute(queryCloseRequest(id), Void.class, hostHandle);
390395
}
391396
};

Diff for: src/main/java/com/arangodb/internal/ArangoCursorExecute.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,15 @@
2222

2323
import com.arangodb.ArangoDBException;
2424
import com.arangodb.entity.CursorEntity;
25-
import com.arangodb.internal.net.HostHandle;
2625

2726
/**
2827
* @author Mark Vollmary
2928
*
3029
*/
3130
public interface ArangoCursorExecute {
3231

33-
CursorEntity next(String id, HostHandle hostHandle) throws ArangoDBException;
32+
CursorEntity next(String id) throws ArangoDBException;
3433

35-
void close(String id, HostHandle hostHandle) throws ArangoDBException;
34+
void close(String id) throws ArangoDBException;
3635

3736
}

Diff for: src/main/java/com/arangodb/internal/ArangoCursorIterator.java

+2-5
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525

2626
import com.arangodb.ArangoCursor;
2727
import com.arangodb.entity.CursorEntity;
28-
import com.arangodb.internal.net.HostHandle;
2928
import com.arangodb.velocypack.VPackSlice;
3029

3130
/**
@@ -41,16 +40,14 @@ public class ArangoCursorIterator<T> implements Iterator<T> {
4140
private final ArangoCursor<T> cursor;
4241
private final InternalArangoDatabase<?, ?, ?, ?> db;
4342
private final ArangoCursorExecute execute;
44-
private final HostHandle hostHandle;
4543

4644
public ArangoCursorIterator(final ArangoCursor<T> cursor, final ArangoCursorExecute execute,
47-
final InternalArangoDatabase<?, ?, ?, ?> db, final CursorEntity result, final HostHandle hostHandle) {
45+
final InternalArangoDatabase<?, ?, ?, ?> db, final CursorEntity result) {
4846
super();
4947
this.cursor = cursor;
5048
this.execute = execute;
5149
this.db = db;
5250
this.result = result;
53-
this.hostHandle = hostHandle;
5451
pos = 0;
5552
}
5653

@@ -66,7 +63,7 @@ public boolean hasNext() {
6663
@Override
6764
public T next() {
6865
if (pos >= result.getResult().size() && result.getHasMore()) {
69-
result = execute.next(cursor.getId(), hostHandle);
66+
result = execute.next(cursor.getId());
7067
pos = 0;
7168
}
7269
if (!hasNext()) {

Diff for: src/main/java/com/arangodb/internal/http/HttpConnection.java

+3
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,9 @@ public long getKeepAliveDuration(final HttpResponse response, final HttpContext
149149

150150
@Override
151151
public Host getHost() {
152+
if (host == null) {
153+
host = hostHandler.get();
154+
}
152155
return host;
153156
}
154157

Diff for: src/main/java/com/arangodb/internal/net/DelHostHandler.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public class DelHostHandler implements HostHandler {
3434
public DelHostHandler(final HostHandler hostHandler, final Host host) {
3535
super();
3636
this.hostHandler = hostHandler;
37-
this.host = host;
37+
this.host = host != null ? host : hostHandler.get();
3838
}
3939

4040
@Override

Diff for: src/main/java/com/arangodb/internal/velocystream/internal/VstConnection.java

+3
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ protected VstConnection(final HostHandler hostHandler, final Integer timeout, fi
8383

8484
@Override
8585
public Host getHost() {
86+
if (host == null) {
87+
host = hostHandler.get();
88+
}
8689
return host;
8790
}
8891

0 commit comments

Comments
 (0)