Search in sources :

Example 1 with GridClientConnection

use of org.apache.ignite.internal.client.impl.connection.GridClientConnection in project ignite by apache.

the class GridClientAbstractProjection method withReconnectHandling.

/**
 * This method executes request to a communication layer and handles connection error, if it occurs.
 * In case of communication exception client instance is notified and new instance of client is created.
 * If none of the grid servers can be reached, an exception is thrown.
 *
 * @param c Closure to be executed.
 * @param <R> Result future type.
 * @return Future returned by closure.
 */
protected <R> GridClientFuture<R> withReconnectHandling(ClientProjectionClosure<R> c) {
    try {
        GridClientNode node = null;
        boolean changeNode = false;
        Throwable cause = null;
        for (int i = 0; i < RETRY_CNT; i++) {
            if (node == null || changeNode)
                try {
                    node = balancedNode(node);
                } catch (GridClientException e) {
                    if (node == null)
                        throw e;
                    else
                        throw new GridServerUnreachableException("All nodes in projection failed when retrying to perform request. Attempts made: " + i, cause);
                }
            GridClientConnection conn = null;
            try {
                conn = client.connectionManager().connection(node);
                return c.apply(conn, node.nodeId());
            } catch (GridConnectionIdleClosedException e) {
                client.connectionManager().terminateConnection(conn, node, e);
                // It's ok, just reconnect to the same node.
                changeNode = false;
                cause = e;
            } catch (GridClientConnectionResetException e) {
                client.connectionManager().terminateConnection(conn, node, e);
                changeNode = true;
                cause = e;
            } catch (GridServerUnreachableException e) {
                changeNode = true;
                cause = e;
            }
            U.sleep(RETRY_DELAY);
        }
        assert cause != null;
        throw new GridServerUnreachableException("Failed to communicate with grid nodes " + "(maximum count of retries reached).", cause);
    } catch (GridClientException e) {
        return new GridClientFutureAdapter<>(e);
    } catch (IgniteInterruptedCheckedException | InterruptedException e) {
        Thread.currentThread().interrupt();
        return new GridClientFutureAdapter<>(new GridClientException("Interrupted when (re)trying to perform request.", e));
    }
}
Also used : GridClientNode(org.apache.ignite.internal.client.GridClientNode) GridClientException(org.apache.ignite.internal.client.GridClientException) GridConnectionIdleClosedException(org.apache.ignite.internal.client.impl.connection.GridConnectionIdleClosedException) GridClientConnectionResetException(org.apache.ignite.internal.client.impl.connection.GridClientConnectionResetException) GridClientConnection(org.apache.ignite.internal.client.impl.connection.GridClientConnection) GridServerUnreachableException(org.apache.ignite.internal.client.GridServerUnreachableException) IgniteInterruptedCheckedException(org.apache.ignite.internal.IgniteInterruptedCheckedException)

Example 2 with GridClientConnection

use of org.apache.ignite.internal.client.impl.connection.GridClientConnection 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;
}
Also used : GridClientNode(org.apache.ignite.internal.client.GridClientNode) GridClientException(org.apache.ignite.internal.client.GridClientException) GridClientTopology(org.apache.ignite.internal.client.impl.connection.GridClientTopology) GridClientConnectionResetException(org.apache.ignite.internal.client.impl.connection.GridClientConnectionResetException) GridClientConnectionManager(org.apache.ignite.internal.client.impl.connection.GridClientConnectionManager) GridClientConnection(org.apache.ignite.internal.client.impl.connection.GridClientConnection) GridClientFutureAdapter(org.apache.ignite.internal.client.impl.GridClientFutureAdapter) GridServerUnreachableException(org.apache.ignite.internal.client.GridServerUnreachableException) GridClientNodeImpl(org.apache.ignite.internal.client.impl.GridClientNodeImpl)

Example 3 with GridClientConnection

use of org.apache.ignite.internal.client.impl.connection.GridClientConnection in project ignite by apache.

the class GridClientAbstractProjection method withReconnectHandling.

/**
 * This method executes request to a communication layer and handles connection error, if it occurs. Server
 * is picked up according to the projection affinity and key given. Connection will be made with the node
 * on which key is cached. In case of communication exception client instance is notified and new instance
 * of client is created. If none of servers can be reached, an exception is thrown.
 *
 * @param c Closure to be executed.
 * @param cacheName Cache name for which mapped node will be calculated.
 * @param affKey Affinity key.
 * @param <R> Type of result in future.
 * @return Operation future.
 */
protected <R> GridClientFuture<R> withReconnectHandling(ClientProjectionClosure<R> c, String cacheName, @Nullable Object affKey) {
    GridClientDataAffinity affinity = client.affinity(cacheName);
    // If pinned (fixed-nodes) or no affinity provided use balancer.
    if (nodes != null || affinity == null || affKey == null)
        return withReconnectHandling(c);
    try {
        Collection<? extends GridClientNode> prjNodes = projectionNodes();
        if (prjNodes.isEmpty())
            throw new GridServerUnreachableException("Failed to get affinity node (no nodes in topology were " + "accepted by the filter): " + filter);
        GridClientNode node = affinity.node(affKey, prjNodes);
        for (int i = 0; i < RETRY_CNT; i++) {
            GridClientConnection conn = null;
            try {
                conn = client.connectionManager().connection(node);
                return c.apply(conn, node.nodeId());
            } catch (GridConnectionIdleClosedException e) {
                client.connectionManager().terminateConnection(conn, node, e);
            } catch (GridClientConnectionResetException e) {
                client.connectionManager().terminateConnection(conn, node, e);
                if (!checkNodeAlive(node.nodeId()))
                    throw new GridServerUnreachableException("Failed to communicate with mapped grid node for " + "given affinity key (node left the grid) [nodeId=" + node.nodeId() + ", affKey=" + affKey + ']', e);
            } catch (RuntimeException | Error e) {
                if (conn != null)
                    client.connectionManager().terminateConnection(conn, node, e);
                throw e;
            }
            U.sleep(RETRY_DELAY);
        }
        throw new GridServerUnreachableException("Failed to communicate with mapped grid node for given affinity " + "key (did node leave the grid?) [nodeId=" + node.nodeId() + ", affKey=" + affKey + ']');
    } catch (GridClientException e) {
        return new GridClientFutureAdapter<>(e);
    } catch (IgniteInterruptedCheckedException | InterruptedException e) {
        Thread.currentThread().interrupt();
        return new GridClientFutureAdapter<>(new GridClientException("Interrupted when (re)trying to perform " + "request.", e));
    }
}
Also used : GridClientNode(org.apache.ignite.internal.client.GridClientNode) GridClientException(org.apache.ignite.internal.client.GridClientException) GridConnectionIdleClosedException(org.apache.ignite.internal.client.impl.connection.GridConnectionIdleClosedException) GridClientConnectionResetException(org.apache.ignite.internal.client.impl.connection.GridClientConnectionResetException) GridClientDataAffinity(org.apache.ignite.internal.client.GridClientDataAffinity) GridClientConnection(org.apache.ignite.internal.client.impl.connection.GridClientConnection) GridServerUnreachableException(org.apache.ignite.internal.client.GridServerUnreachableException) IgniteInterruptedCheckedException(org.apache.ignite.internal.IgniteInterruptedCheckedException)

Aggregations

GridClientException (org.apache.ignite.internal.client.GridClientException)3 GridClientNode (org.apache.ignite.internal.client.GridClientNode)3 GridServerUnreachableException (org.apache.ignite.internal.client.GridServerUnreachableException)3 GridClientConnection (org.apache.ignite.internal.client.impl.connection.GridClientConnection)3 GridClientConnectionResetException (org.apache.ignite.internal.client.impl.connection.GridClientConnectionResetException)3 IgniteInterruptedCheckedException (org.apache.ignite.internal.IgniteInterruptedCheckedException)2 GridConnectionIdleClosedException (org.apache.ignite.internal.client.impl.connection.GridConnectionIdleClosedException)2 GridClientDataAffinity (org.apache.ignite.internal.client.GridClientDataAffinity)1 GridClientFutureAdapter (org.apache.ignite.internal.client.impl.GridClientFutureAdapter)1 GridClientNodeImpl (org.apache.ignite.internal.client.impl.GridClientNodeImpl)1 GridClientConnectionManager (org.apache.ignite.internal.client.impl.connection.GridClientConnectionManager)1 GridClientTopology (org.apache.ignite.internal.client.impl.connection.GridClientTopology)1