Search in sources :

Example 1 with GridServerUnreachableException

use of org.apache.ignite.internal.client.GridServerUnreachableException in project ignite by apache.

the class GridClientConnectionManagerAdapter method init.

/**
 * {@inheritDoc}
 */
@SuppressWarnings("BusyWait")
@Override
public void init(Collection<InetSocketAddress> srvs) throws GridClientException, InterruptedException {
    init0();
    GridClientException firstEx = null;
    for (int i = 0; i < INIT_RETRY_CNT; i++) {
        Collection<InetSocketAddress> srvsCp = new ArrayList<>(srvs);
        while (!srvsCp.isEmpty()) {
            GridClientConnection conn = null;
            try {
                conn = connect(null, srvsCp);
                conn.topology(cfg.isAutoFetchAttributes(), cfg.isAutoFetchMetrics(), null).get();
                return;
            } catch (GridServerUnreachableException e) {
                // No connection could be opened to any of initial addresses - exit to retry loop.
                assert conn == null : "GridClientConnectionResetException was thrown from GridClientConnection#topology";
                if (firstEx == null)
                    firstEx = e;
                break;
            } catch (GridClientConnectionResetException e) {
                // trying other initial addresses if any.
                assert conn != null : "GridClientConnectionResetException was thrown from connect()";
                if (firstEx == null)
                    firstEx = e;
                if (!srvsCp.remove(conn.serverAddress()))
                    // We have misbehaving collection or equals - just exit to avoid infinite loop.
                    break;
            }
        }
        Thread.sleep(INIT_RETRY_INTERVAL);
    }
    for (GridClientConnection c : conns.values()) {
        conns.remove(c.serverAddress(), c);
        c.close(FAILED, false);
    }
    throw firstEx;
}
Also used : GridClientException(org.apache.ignite.internal.client.GridClientException) GridServerUnreachableException(org.apache.ignite.internal.client.GridServerUnreachableException) InetSocketAddress(java.net.InetSocketAddress) ArrayList(java.util.ArrayList)

Example 2 with GridServerUnreachableException

use of org.apache.ignite.internal.client.GridServerUnreachableException in project ignite by apache.

the class GridClientConnectionManagerAdapter method connect.

/**
 * Create new connection to specified server.
 *
 * @param nodeId {@code UUID} of node for mapping with connection.
 *      {@code null} if no need of mapping.
 * @param addr Remote socket to connect.
 * @return Established connection.
 * @throws IOException If connection failed.
 * @throws GridClientException If protocol error happened.
 * @throws InterruptedException If thread was interrupted before connection was established.
 */
protected GridClientConnection connect(@Nullable UUID nodeId, InetSocketAddress addr) throws IOException, GridClientException, InterruptedException {
    endpointStripedLock.lock(addr);
    try {
        GridClientConnection old = conns.get(addr);
        if (old != null) {
            if (old.isClosed()) {
                conns.remove(addr, old);
                if (nodeId != null)
                    nodeConns.remove(nodeId, old);
            } else {
                if (nodeId != null)
                    nodeConns.put(nodeId, old);
                return old;
            }
        }
        SecurityCredentials cred = null;
        try {
            if (cfg.getSecurityCredentialsProvider() != null)
                cred = cfg.getSecurityCredentialsProvider().credentials();
        } catch (IgniteCheckedException e) {
            throw new GridClientException("Failed to obtain client credentials.", e);
        }
        GridClientConnection conn;
        if (cfg.getProtocol() == GridClientProtocol.TCP) {
            GridClientMarshaller marsh = cfg.getMarshaller();
            try {
                conn = new GridClientNioTcpConnection(srv, clientId, addr, sslCtx, pingExecutor, cfg.getConnectTimeout(), cfg.getPingInterval(), cfg.getPingTimeout(), cfg.isTcpNoDelay(), marsh, marshId, top, cred, cfg.getUserAttributes());
            } catch (GridClientException e) {
                if (marsh instanceof GridClientZipOptimizedMarshaller) {
                    log.warning("Failed to connect with GridClientZipOptimizedMarshaller," + " trying to fallback to default marshaller: " + e);
                    conn = new GridClientNioTcpConnection(srv, clientId, addr, sslCtx, pingExecutor, cfg.getConnectTimeout(), cfg.getPingInterval(), cfg.getPingTimeout(), cfg.isTcpNoDelay(), ((GridClientZipOptimizedMarshaller) marsh).defaultMarshaller(), marshId, top, cred, cfg.getUserAttributes());
                } else
                    throw e;
            }
        } else
            throw new GridServerUnreachableException("Failed to create client (protocol is not supported): " + cfg.getProtocol());
        old = conns.putIfAbsent(addr, conn);
        assert old == null;
        if (nodeId != null)
            nodeConns.put(nodeId, conn);
        return conn;
    } finally {
        endpointStripedLock.unlock(addr);
    }
}
Also used : GridClientException(org.apache.ignite.internal.client.GridClientException) GridServerUnreachableException(org.apache.ignite.internal.client.GridServerUnreachableException) SecurityCredentials(org.apache.ignite.plugin.security.SecurityCredentials) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridClientMarshaller(org.apache.ignite.internal.client.marshaller.GridClientMarshaller) GridClientZipOptimizedMarshaller(org.apache.ignite.internal.client.marshaller.optimized.GridClientZipOptimizedMarshaller)

Example 3 with GridServerUnreachableException

use of org.apache.ignite.internal.client.GridServerUnreachableException in project ignite by apache.

the class GridClientConnectionManagerAdapter method connect.

/**
 * Returns connection to node using given server addresses.
 *
 * @param srvs Server addresses.
 * @param clo Client connection closure.
 * @return Established connection.
 * @throws GridClientException If failed.
 * @throws InterruptedException If was interrupted while waiting for connection to be established.
 */
private GridClientConnection connect(Collection<InetSocketAddress> srvs, @Nullable GridClientConnectionInClosure clo) throws InterruptedException, GridClientException {
    GridClientException firstEx = null;
    for (int i = 0; i < INIT_RETRY_CNT; i++) {
        Collection<InetSocketAddress> srvsCp = new ArrayList<>(srvs);
        while (!srvsCp.isEmpty()) {
            GridClientConnection conn = null;
            try {
                conn = connect(null, srvsCp);
                if (clo != null)
                    clo.apply(conn);
                return conn;
            } catch (GridServerUnreachableException e) {
                // No connection could be opened to any of initial addresses - exit to retry loop.
                assert conn == null : "GridClientConnectionResetException was thrown from GridClientConnection#topology";
                if (firstEx == null)
                    firstEx = e;
                break;
            } catch (GridClientConnectionResetException e) {
                // trying other initial addresses if any.
                assert conn != null : "GridClientConnectionResetException was thrown from connect()";
                if (firstEx == null)
                    firstEx = e;
                if (!srvsCp.remove(conn.serverAddress()))
                    // We have misbehaving collection or equals - just exit to avoid infinite loop.
                    break;
            }
        }
        Thread.sleep(INIT_RETRY_INTERVAL);
    }
    for (GridClientConnection c : conns.values()) {
        conns.remove(c.serverAddress(), c);
        c.close(FAILED, false);
    }
    throw firstEx;
}
Also used : GridClientException(org.apache.ignite.internal.client.GridClientException) GridServerUnreachableException(org.apache.ignite.internal.client.GridServerUnreachableException) InetSocketAddress(java.net.InetSocketAddress) ArrayList(java.util.ArrayList)

Example 4 with GridServerUnreachableException

use of org.apache.ignite.internal.client.GridServerUnreachableException in project ignite by apache.

the class GridClientAbstractProjection method balancedNode.

/**
 * Return balanced node for current projection.
 *
 * @param exclude Nodes to exclude.
 * @return Balanced node.
 * @throws GridServerUnreachableException If topology is empty.
 */
private GridClientNode balancedNode(@Nullable final GridClientNode exclude) throws GridClientException {
    GridClientPredicate<GridClientNode> excludeFilter = exclude == null ? new GridClientPredicate<GridClientNode>() {

        @Override
        public boolean apply(GridClientNode e) {
            return restAvailable(e, client.cfg.getProtocol());
        }

        @Override
        public String toString() {
            return "Filter nodes with available REST.";
        }
    } : new GridClientPredicate<GridClientNode>() {

        @Override
        public boolean apply(GridClientNode e) {
            return !exclude.equals(e) && restAvailable(e, client.cfg.getProtocol());
        }

        @Override
        public String toString() {
            return "Filter nodes with available REST and " + "exclude (probably due to connection failure) node: " + exclude.nodeId();
        }
    };
    Collection<? extends GridClientNode> prjNodes = projectionNodes(excludeFilter);
    if (prjNodes.isEmpty())
        throw new GridServerUnreachableException("Failed to get balanced node (no nodes in topology were " + "accepted by the filters): " + Arrays.asList(filter, excludeFilter));
    if (prjNodes.size() == 1) {
        GridClientNode ret = GridClientUtils.first(prjNodes);
        assert ret != null;
        return ret;
    }
    return balancer.balancedNode(prjNodes);
}
Also used : GridClientNode(org.apache.ignite.internal.client.GridClientNode) GridServerUnreachableException(org.apache.ignite.internal.client.GridServerUnreachableException)

Example 5 with GridServerUnreachableException

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

Aggregations

GridServerUnreachableException (org.apache.ignite.internal.client.GridServerUnreachableException)9 GridClientException (org.apache.ignite.internal.client.GridClientException)6 GridClientNode (org.apache.ignite.internal.client.GridClientNode)5 InetSocketAddress (java.net.InetSocketAddress)3 ArrayList (java.util.ArrayList)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 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)1 GridClient (org.apache.ignite.internal.client.GridClient)1 GridClientClosedException (org.apache.ignite.internal.client.GridClientClosedException)1 GridClientCompute (org.apache.ignite.internal.client.GridClientCompute)1 GridClientData (org.apache.ignite.internal.client.GridClientData)1