|
| 1 | +package com.arangodb.internal.net; |
| 2 | + |
| 3 | +import com.arangodb.ArangoDBException; |
| 4 | +import com.arangodb.config.HostDescription; |
| 5 | +import com.arangodb.internal.InternalRequest; |
| 6 | +import com.arangodb.internal.InternalResponse; |
| 7 | +import com.arangodb.internal.RequestType; |
| 8 | +import com.arangodb.internal.config.ArangoConfig; |
| 9 | +import com.arangodb.internal.serde.InternalSerde; |
| 10 | +import com.arangodb.internal.util.HostUtils; |
| 11 | +import com.arangodb.internal.util.RequestUtils; |
| 12 | +import com.arangodb.internal.util.ResponseUtils; |
| 13 | +import org.slf4j.Logger; |
| 14 | +import org.slf4j.LoggerFactory; |
| 15 | + |
| 16 | +import java.io.Closeable; |
| 17 | +import java.io.IOException; |
| 18 | +import java.net.ConnectException; |
| 19 | +import java.net.SocketTimeoutException; |
| 20 | +import java.util.concurrent.CompletableFuture; |
| 21 | +import java.util.concurrent.CompletionException; |
| 22 | +import java.util.concurrent.TimeoutException; |
| 23 | +import java.util.concurrent.atomic.AtomicLong; |
| 24 | + |
| 25 | +public abstract class Communication implements Closeable { |
| 26 | + private static final Logger LOGGER = LoggerFactory.getLogger(Communication.class); |
| 27 | + protected final HostHandler hostHandler; |
| 28 | + protected final InternalSerde serde; |
| 29 | + private final AtomicLong reqCount; |
| 30 | + |
| 31 | + |
| 32 | + protected Communication(final ArangoConfig config, final HostHandler hostHandler) { |
| 33 | + this.hostHandler = hostHandler; |
| 34 | + serde = config.getInternalSerde(); |
| 35 | + reqCount = new AtomicLong(); |
| 36 | + } |
| 37 | + |
| 38 | + protected abstract void connect(final Connection conn) throws IOException; |
| 39 | + |
| 40 | + @Override |
| 41 | + public void close() throws IOException { |
| 42 | + hostHandler.close(); |
| 43 | + } |
| 44 | + |
| 45 | + public CompletableFuture<InternalResponse> executeAsync(final InternalRequest request, final HostHandle hostHandle) { |
| 46 | + return executeAsync(request, hostHandle, hostHandler.get(hostHandle, RequestUtils.determineAccessType(request)), 0); |
| 47 | + } |
| 48 | + |
| 49 | + private CompletableFuture<InternalResponse> executeAsync(final InternalRequest request, final HostHandle hostHandle, final Host host, final int attemptCount) { |
| 50 | + long reqId = reqCount.getAndIncrement(); |
| 51 | + return doExecuteAsync(request, hostHandle, host, attemptCount, host.connection(), reqId); |
| 52 | + } |
| 53 | + |
| 54 | + private CompletableFuture<InternalResponse> doExecuteAsync( |
| 55 | + final InternalRequest request, final HostHandle hostHandle, final Host host, final int attemptCount, Connection connection, long reqId |
| 56 | + ) { |
| 57 | + if (LOGGER.isDebugEnabled()) { |
| 58 | + String body = request.getBody() == null ? "" : serde.toJsonString(request.getBody()); |
| 59 | + LOGGER.debug("Send Request [id={}]: {} {}", reqId, request, body); |
| 60 | + } |
| 61 | + final CompletableFuture<InternalResponse> rfuture = new CompletableFuture<>(); |
| 62 | + try { |
| 63 | + connect(connection); |
| 64 | + } catch (IOException e) { |
| 65 | + handleException(true, e, hostHandle, request, host, reqId, attemptCount, rfuture); |
| 66 | + return rfuture; |
| 67 | + } |
| 68 | + |
| 69 | + connection.executeAsync(request) |
| 70 | + .whenComplete((response, e) -> { |
| 71 | + try { |
| 72 | + if (e instanceof SocketTimeoutException) { |
| 73 | + // SocketTimeoutException exceptions are wrapped and rethrown. |
| 74 | + TimeoutException te = new TimeoutException(e.getMessage()); |
| 75 | + te.initCause(e); |
| 76 | + rfuture.completeExceptionally(ArangoDBException.of(te, reqId)); |
| 77 | + } else if (e instanceof TimeoutException) { |
| 78 | + rfuture.completeExceptionally(ArangoDBException.of(e, reqId)); |
| 79 | + } else if (e instanceof ConnectException) { |
| 80 | + handleException(true, e, hostHandle, request, host, reqId, attemptCount, rfuture); |
| 81 | + } else if (e != null) { |
| 82 | + handleException(isSafe(request), e, hostHandle, request, host, reqId, attemptCount, rfuture); |
| 83 | + } else { |
| 84 | + if (LOGGER.isDebugEnabled()) { |
| 85 | + String body = response.getBody() == null ? "" : serde.toJsonString(response.getBody()); |
| 86 | + LOGGER.debug("Received Response [id={}]: {} {}", reqId, response, body); |
| 87 | + } |
| 88 | + ArangoDBException errorEntityEx = ResponseUtils.translateError(serde, response); |
| 89 | + if (errorEntityEx instanceof ArangoDBRedirectException) { |
| 90 | + if (attemptCount >= 3) { |
| 91 | + rfuture.completeExceptionally(errorEntityEx); |
| 92 | + } else { |
| 93 | + final String location = ((ArangoDBRedirectException) errorEntityEx).getLocation(); |
| 94 | + final HostDescription redirectHost = HostUtils.createFromLocation(location); |
| 95 | + hostHandler.failIfNotMatch(redirectHost, errorEntityEx); |
| 96 | + mirror( |
| 97 | + executeAsync(request, new HostHandle().setHost(redirectHost), hostHandler.get(hostHandle, RequestUtils.determineAccessType(request)), attemptCount + 1), |
| 98 | + rfuture |
| 99 | + ); |
| 100 | + } |
| 101 | + } else if (errorEntityEx != null) { |
| 102 | + rfuture.completeExceptionally(errorEntityEx); |
| 103 | + } else { |
| 104 | + hostHandler.success(); |
| 105 | + rfuture.complete(response); |
| 106 | + } |
| 107 | + } |
| 108 | + } catch (Exception ex) { |
| 109 | + rfuture.completeExceptionally(ArangoDBException.of(ex, reqId)); |
| 110 | + } |
| 111 | + }); |
| 112 | + return rfuture; |
| 113 | + } |
| 114 | + |
| 115 | + private void handleException(boolean isSafe, Throwable e, HostHandle hostHandle, InternalRequest request, Host host, |
| 116 | + long reqId, int attemptCount, CompletableFuture<InternalResponse> rfuture) { |
| 117 | + IOException ioEx = wrapIOEx(e); |
| 118 | + hostHandler.fail(ioEx); |
| 119 | + if (hostHandle != null && hostHandle.getHost() != null) { |
| 120 | + hostHandle.setHost(null); |
| 121 | + } |
| 122 | + hostHandler.checkNext(hostHandle, RequestUtils.determineAccessType(request)); |
| 123 | + if (isSafe) { |
| 124 | + Host nextHost = hostHandler.get(hostHandle, RequestUtils.determineAccessType(request)); |
| 125 | + LOGGER.warn("Could not connect to {} while executing request [id={}]", |
| 126 | + host.getDescription(), reqId, ioEx); |
| 127 | + LOGGER.debug("Try connecting to {}", nextHost.getDescription()); |
| 128 | + mirror( |
| 129 | + executeAsync(request, hostHandle, nextHost, attemptCount), |
| 130 | + rfuture |
| 131 | + ); |
| 132 | + } else { |
| 133 | + ArangoDBException aEx = ArangoDBException.of(ioEx, reqId); |
| 134 | + rfuture.completeExceptionally(aEx); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + private void mirror(CompletableFuture<InternalResponse> up, CompletableFuture<InternalResponse> down) { |
| 139 | + up.whenComplete((v, err) -> { |
| 140 | + if (err != null) { |
| 141 | + down.completeExceptionally(err instanceof CompletionException ? err.getCause() : err); |
| 142 | + } else { |
| 143 | + down.complete(v); |
| 144 | + } |
| 145 | + }); |
| 146 | + } |
| 147 | + |
| 148 | + private static IOException wrapIOEx(Throwable t) { |
| 149 | + if (t instanceof IOException) { |
| 150 | + return (IOException) t; |
| 151 | + } else { |
| 152 | + return new IOException(t); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + private boolean isSafe(final InternalRequest request) { |
| 157 | + RequestType type = request.getRequestType(); |
| 158 | + return type == RequestType.GET || type == RequestType.HEAD || type == RequestType.OPTIONS; |
| 159 | + } |
| 160 | + |
| 161 | +} |
0 commit comments