use of org.apache.ignite.internal.client.impl.GridClientFutureAdapter in project ignite by apache.
the class GridRouterClientImpl method forwardMessage.
/**
* Send a raw packet "as is" directly to the given node.
* The exact types of acceptable arguments and return values depends on underlying connections.
*
* @param msg Raw message to send.
* @param destId Id of node to send message to. If {@code null} than node will be chosen
* from the topology randomly.
* @return Future, representing forwarded message.
* @throws GridServerUnreachableException If destination node can't be reached.
* @throws GridClientClosedException If client is closed.
* @throws GridClientException If any other client-related error occurs.
* @throws InterruptedException If router was interrupted while trying.
* to establish connection with destination node.
*/
GridClientFutureAdapter<?> forwardMessage(Object msg, @Nullable UUID destId, byte marshId) throws GridClientException, InterruptedException {
GridClientTopology top = clientImpl.topology();
GridClientNode dest = destId != null ? top.node(destId) : cliCfg.getBalancer().balancedNode(applyFilter(top.nodes(), new GridClientPredicate<GridClientNodeImpl>() {
@Override
public boolean apply(GridClientNodeImpl e) {
return restAvailable(e, cliCfg.getProtocol());
}
}));
if (dest == null)
throw new GridServerUnreachableException("Failed to resolve node for specified destination ID: " + destId);
GridClientConnectionManager connMgr = connectionManager(marshId);
GridClientConnection conn = null;
// No reconnection handling there. Let client to do it if needed.
GridClientException cause;
try {
conn = connMgr.connection(dest);
return conn.forwardMessage(msg);
} catch (GridClientConnectionResetException e) {
if (destId != null)
connMgr.terminateConnection(conn, top.node(destId), e);
else
connMgr.terminateConnection(conn, null, e);
cause = e;
} catch (GridClientException e) {
cause = e;
}
GridClientFutureAdapter<Object> fail = new GridClientFutureAdapter<>();
fail.onDone(cause);
return fail;
}
use of org.apache.ignite.internal.client.impl.GridClientFutureAdapter in project ignite by apache.
the class GridClientNioTcpConnection method makeRequest.
/**
* Makes request to server via tcp protocol and returns a future that will be completed when response is received.
*
* @param msg Message to request,
* @param fut Future that will handle response.
* @param routeMode If {@code true} then this method should overwrite session token by the cached one,
* otherwise keep original value.
* @return Response object.
* @throws GridClientConnectionResetException If request failed.
* @throws GridClientClosedException If client closed.
*/
private <R> GridClientFutureAdapter<R> makeRequest(GridClientMessage msg, final TcpClientFuture<R> fut, boolean routeMode) throws GridClientConnectionResetException, GridClientClosedException {
assert msg != null;
if (msg instanceof GridClientPingPacket) {
long now = System.currentTimeMillis();
if (Math.min(now, lastPingRcvTime) - lastPingSndTime >= pingTimeout)
close(FAILED, false, new IOException("Did not receive any packets within ping response interval (connection is " + "considered to be half-opened) [lastPingReceiveTime=" + lastPingRcvTime + ", lastPingSendTime=" + lastPingSndTime + ", now=" + now + ", timeout=" + pingTimeout + ", addr=" + serverAddress() + ']'));
else // or we've already waiting for ping response.
if (now - lastPingSndTime > pingInterval && lastPingRcvTime != Long.MAX_VALUE) {
lastPingRcvTime = Long.MAX_VALUE;
ses.send(GridClientPingPacket.PING_MESSAGE);
lastPingSndTime = now;
}
} else {
long reqId = reqIdCntr.getAndIncrement();
msg.requestId(reqId);
if (!routeMode) {
msg.clientId(clientId);
msg.sessionToken(sesTok);
}
fut.pendingMessage(msg);
checkClosed(closeReason);
GridClientFutureAdapter old = pendingReqs.putIfAbsent(reqId, fut);
assert old == null;
GridNioFuture<?> sndFut = ses.send(msg);
lastMsgSndTime = System.currentTimeMillis();
if (routeMode) {
sndFut.listen(new CI1<IgniteInternalFuture<?>>() {
@Override
public void apply(IgniteInternalFuture<?> sndFut) {
try {
sndFut.get();
} catch (Exception e) {
close(FAILED, false, e);
fut.onDone(getCloseReasonAsException(FAILED, e));
}
}
});
} else {
try {
sndFut.get();
} catch (Exception e) {
throw new GridClientConnectionResetException("Failed to send message over connection " + "(will try to reconnect): " + serverAddress(), e);
}
}
}
return fut;
}
use of org.apache.ignite.internal.client.impl.GridClientFutureAdapter in project ignite by apache.
the class GridClientNioTcpConnection method close.
/**
* Closes connection facade.
*
* @param reason Why this connection should be closed.
* @param waitCompletion If {@code true} this method will wait for all pending requests to be completed.
* @param cause The cause of connection close, or {@code null} if it is an ordinal close.
*/
@SuppressWarnings("NonPrivateFieldAccessedInSynchronizedContext")
private void close(GridClientConnectionCloseReason reason, boolean waitCompletion, @Nullable Throwable cause) {
synchronized (this) {
if (closeReason != null)
return;
closeReason = reason;
}
try {
// Wait for all pending requests to be processed.
if (waitCompletion && !pendingReqs.isEmpty() && ses.closeTime() == 0)
closedLatch.await();
} catch (InterruptedException ignored) {
log.warning("Interrupted while waiting for all requests to be processed (all pending " + "requests will be failed): " + serverAddress());
Thread.currentThread().interrupt();
}
if (pingTask != null)
pingTask.cancel(false);
if (ses != null)
// Async close.
ses.close();
for (Iterator<TcpClientFuture> it = pendingReqs.values().iterator(); it.hasNext(); ) {
GridClientFutureAdapter fut = it.next();
fut.onDone(getCloseReasonAsException(closeReason, cause));
it.remove();
}
if (log.isLoggable(Level.INFO))
log.info("Client TCP connection closed: " + serverAddress());
}
use of org.apache.ignite.internal.client.impl.GridClientFutureAdapter in project ignite by apache.
the class GridClientNioTcpConnection method node.
/**
* {@inheritDoc}
*/
@SuppressWarnings("unchecked")
@Override
public GridClientFuture<GridClientNode> node(final UUID id, boolean inclAttrs, boolean inclMetrics, UUID destNodeId) throws GridClientConnectionResetException, GridClientClosedException {
assert id != null;
TcpClientFuture fut = refreshNodeReqs.get(id);
// Return request that is in progress.
if (fut != null)
return fut;
GridClientTopologyRequest msg = new GridClientTopologyRequest();
fut = new TcpClientFuture() {
@Override
public void onDone(Object res) {
// Clean up the node id requests map.
refreshNodeReqs.remove(id);
GridClientNodeImpl node = nodeBeanToNode((GridClientNodeBean) res);
if (node != null)
top.updateNode(node);
super.onDone(node);
}
};
GridClientFutureAdapter old = refreshNodeReqs.putIfAbsent(id, fut);
// If concurrent thread put request, do not send the message.
if (old != null)
return old;
msg.nodeId(id);
setupMessage(inclAttrs, inclMetrics, destNodeId, msg);
return makeRequest(msg, fut);
}
Aggregations