Search in sources :

Example 21 with GridClientNode

use of org.apache.ignite.internal.client.GridClientNode 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

GridClientNode (org.apache.ignite.internal.client.GridClientNode)21 GridClientException (org.apache.ignite.internal.client.GridClientException)8 GridClient (org.apache.ignite.internal.client.GridClient)7 GridServerUnreachableException (org.apache.ignite.internal.client.GridServerUnreachableException)5 UUID (java.util.UUID)4 GridClientCompute (org.apache.ignite.internal.client.GridClientCompute)4 GridClientNodeImpl (org.apache.ignite.internal.client.impl.GridClientNodeImpl)3 GridClientConnection (org.apache.ignite.internal.client.impl.connection.GridClientConnection)3 GridClientConnectionResetException (org.apache.ignite.internal.client.impl.connection.GridClientConnectionResetException)3 HashMap (java.util.HashMap)2 LinkedList (java.util.LinkedList)2 Ignite (org.apache.ignite.Ignite)2 IgniteInterruptedCheckedException (org.apache.ignite.internal.IgniteInterruptedCheckedException)2 GridClientDataAffinity (org.apache.ignite.internal.client.GridClientDataAffinity)2 GridClientPredicate (org.apache.ignite.internal.client.GridClientPredicate)2 GridConnectionIdleClosedException (org.apache.ignite.internal.client.impl.connection.GridConnectionIdleClosedException)2 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1