Skip to content
Prev Previous commit
Next Next commit
Implement failover for singleServer with failover
  • Loading branch information
mpv1989 committed Nov 13, 2017
commit 340fbb90e24f0a2f916e19f8ab506a261ba0335d
15 changes: 14 additions & 1 deletion src/main/java/com/arangodb/internal/http/HttpCommunication.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
*/
public class HttpCommunication {

private static final int ERROR_REDIRECT = 307;

public static class Builder {

private final HostHandler hostHandler;
Expand Down Expand Up @@ -120,7 +122,18 @@ public void disconnect() throws IOException {

public Response execute(final Request request, final HostHandle hostHandle, final boolean closeConnection)
throws ArangoDBException, IOException {
return execute(request, connectionPool.connection(hostHandle), closeConnection);
final HttpConnection connection = connectionPool.connection(hostHandle);
try {
return execute(request, connection, closeConnection);
} catch (final ArangoDBException e) {
final Integer responseCode = e.getResponseCode();
if (responseCode != null && responseCode == ERROR_REDIRECT) {
connectionPool.closeConnectionOnError(connection);
return execute(request, hostHandle, closeConnection);
} else {
throw e;
}
}
}

protected Response execute(final Request request, final HttpConnection connection, final boolean closeConnection)
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/com/arangodb/internal/http/HttpConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,18 @@ public void close() throws IOException {
client.close();
}

@Override
public void closeOnError() throws IOException {
hostHandler.fail();
close();
}

public Response execute(final Request request) throws ArangoDBException, IOException {
host = hostHandler.get();
while (true) {
if (host == null) {
throw new ArangoDBException("Was not able to connect to any host");
}
try {
final String url = buildUrl(buildBaseUrl(host), request);
final HttpRequestBase httpRequest = buildHttpRequestBase(request, url);
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/arangodb/internal/net/Connection.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package com.arangodb.internal.net;

import java.io.Closeable;
import java.io.IOException;

import com.arangodb.internal.Host;

Expand All @@ -32,4 +33,6 @@ public interface Connection extends Closeable {

Host getHost();

void closeOnError() throws IOException;

}
18 changes: 18 additions & 0 deletions src/main/java/com/arangodb/internal/net/ConnectionPool.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.io.IOException;
import java.util.LinkedList;

import com.arangodb.ArangoDBException;
import com.arangodb.internal.Host;

/**
Expand Down Expand Up @@ -75,4 +76,21 @@ public void disconnect() throws IOException {
}
}

public void closeConnection(final C connection) {
try {
connection.close();
connections.remove(connection);
} catch (final IOException e) {
throw new ArangoDBException(e);
}
}

public void closeConnectionOnError(final C connection) {
try {
connection.closeOnError();
connections.remove(connection);
} catch (final IOException e) {
throw new ArangoDBException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
public abstract class VstCommunication<R, C extends VstConnection> {

private static final int ERROR_STATUS = 300;
private static final int ERROR_REDIRECT = 307;

private static final Logger LOGGER = LoggerFactory.getLogger(VstCommunication.class);

Expand Down Expand Up @@ -95,7 +96,18 @@ public void disconnect() throws IOException {

public R execute(final Request request, final HostHandle hostHandle, final boolean closeConnection)
throws ArangoDBException {
return execute(request, connectionPool.connection(hostHandle), closeConnection);
final C connection = connectionPool.connection(hostHandle);
try {
return execute(request, connection, closeConnection);
} catch (final ArangoDBException e) {
final Integer responseCode = e.getResponseCode();
if (responseCode != null && responseCode == ERROR_REDIRECT) {
connectionPool.closeConnectionOnError(connection);
return execute(request, hostHandle, closeConnection);
} else {
throw e;
}
}
}

protected abstract R execute(final Request request, C connection, boolean closeConnection) throws ArangoDBException;
Expand All @@ -116,10 +128,10 @@ protected void checkError(final Response response) throws ArangoDBException {
}
}

protected Response createResponse(final Message messsage) throws VPackParserException {
final Response response = util.deserialize(messsage.getHead(), Response.class);
if (messsage.getBody() != null) {
response.setBody(messsage.getBody());
protected Response createResponse(final Message message) throws VPackParserException {
final Response response = util.deserialize(message.getHead(), Response.class);
if (message.getBody() != null) {
response.setBody(message.getBody());
}
return response;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ public synchronized void open() throws IOException {
}
host = hostHandler.get();
while (true) {
if (host == null) {
throw new ArangoDBException("Was not able to connect to any host");
}
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(String.format("Open connection to %s", host));
}
Expand Down Expand Up @@ -190,6 +193,12 @@ public synchronized void close() {
}
}

@Override
public synchronized void closeOnError() {
hostHandler.fail();
close();
}

private synchronized void sendProtocolHeader() throws IOException {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(String.format("Send velocystream protocol header to %s", socket));
Expand Down