Search in sources :

Example 1 with GridClientException

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

the class ClientAbstractSelfTest method testShutdown.

/**
     * @throws Exception If failed.
     */
public void testShutdown() throws Exception {
    GridClient c = client();
    GridClientCompute compute = c.compute();
    String taskName = getTaskName();
    Object taskArg = getTaskArgument();
    Collection<GridClientFuture<Object>> futs = new ArrayList<>();
    // Validate connection works.
    compute.execute(taskName, taskArg);
    info(">>> First task executed successfully, running batch.");
    for (int i = 0; i < 10; i++) futs.add(compute.executeAsync(taskName, taskArg));
    // Stop client.
    GridClientFactory.stop(c.id(), true);
    info(">>> Completed stop request.");
    int failed = 0;
    for (GridClientFuture<Object> fut : futs) {
        try {
            assertEquals(17, fut.get());
        } catch (GridClientException e) {
            failed++;
            log.warning("Task execution failed.", e);
        }
    }
    assertEquals(0, failed);
}
Also used : GridClientCompute(org.apache.ignite.internal.client.GridClientCompute) GridClientException(org.apache.ignite.internal.client.GridClientException) GridClientFuture(org.apache.ignite.internal.client.GridClientFuture) ArrayList(java.util.ArrayList) GridClient(org.apache.ignite.internal.client.GridClient)

Example 2 with GridClientException

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

use of org.apache.ignite.internal.client.GridClientException 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, keepBinariesThreadLocal());
            } 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, keepBinariesThreadLocal());
                } 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 4 with GridClientException

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

the class GridTcpRouterImpl method start.

/**
     * Starts router.
     *
     * @throws IgniteException If failed.
     */
@Override
public void start() throws IgniteException {
    try {
        client = createClient(cfg);
    } catch (GridClientException e) {
        throw new IgniteException("Failed to initialise embedded client.", e);
    }
    GridNioServerListener<GridClientMessage> lsnr;
    try {
        Class<?> cls = Class.forName(ENT_NIO_LSNR_CLS);
        Constructor<?> cons = cls.getDeclaredConstructor(IgniteLogger.class, GridRouterClientImpl.class);
        cons.setAccessible(true);
        lsnr = (GridNioServerListener<GridClientMessage>) cons.newInstance(log, client);
    } catch (ClassNotFoundException ignored) {
        lsnr = new GridTcpRouterNioListenerOsImpl(log, client);
    } catch (NoSuchMethodException | IllegalAccessException | InstantiationException | InvocationTargetException e) {
        throw new IgniteException("Failed to create NIO listener.", e);
    }
    parser = new GridTcpRouterNioParser();
    final InetAddress hostAddr;
    try {
        hostAddr = InetAddress.getByName(cfg.getHost());
    } catch (UnknownHostException e) {
        throw new IgniteException("Failed to resolve grid address for configured host: " + cfg.getHost(), e);
    }
    SSLContext sslCtx;
    try {
        GridSslContextFactory sslCtxFactory = cfg.getSslContextFactory();
        sslCtx = sslCtxFactory == null ? null : sslCtxFactory.createSslContext();
    } catch (SSLException e) {
        throw new IgniteException("Failed to create SSL context.", e);
    }
    for (int port = cfg.getPort(), last = port + cfg.getPortRange(); port <= last; port++) {
        if (startTcpServer(hostAddr, port, lsnr, parser, cfg.isNoDelay(), sslCtx, cfg.isSslClientAuth(), cfg.isSslClientAuth())) {
            if (log.isInfoEnabled())
                log.info("TCP router successfully started for endpoint: " + hostAddr.getHostAddress() + ":" + port);
            bindPort = port;
            bindHost = hostAddr.getHostName();
            break;
        } else
            U.warn(log, "TCP REST router failed to start on endpoint: " + hostAddr.getHostAddress() + ":" + port + ". Will try next port within allowed port range.");
    }
    if (bindPort == 0)
        throw new IgniteException("Failed to bind TCP router server (possibly all ports in range " + "are in use) [firstPort=" + cfg.getPort() + ", lastPort=" + (cfg.getPort() + cfg.getPortRange()) + ", addr=" + hostAddr + ']');
    try {
        ObjectName objName = U.registerMBean(ManagementFactory.getPlatformMBeanServer(), "Router", "TCP Router " + id, getClass().getSimpleName(), this, GridTcpRouterMBean.class);
        if (log.isDebugEnabled())
            log.debug("Registered MBean: " + objName);
        mbeanName = objName;
    } catch (JMException e) {
        U.error(log, "Failed to register MBean.", e);
    }
}
Also used : GridClientException(org.apache.ignite.internal.client.GridClientException) UnknownHostException(java.net.UnknownHostException) SSLContext(javax.net.ssl.SSLContext) SSLException(javax.net.ssl.SSLException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ObjectName(javax.management.ObjectName) GridSslContextFactory(org.apache.ignite.internal.client.ssl.GridSslContextFactory) IgniteException(org.apache.ignite.IgniteException) JMException(javax.management.JMException) InetAddress(java.net.InetAddress) GridClientMessage(org.apache.ignite.internal.processors.rest.client.message.GridClientMessage)

Example 5 with GridClientException

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

the class GridClientImpl method data.

/** {@inheritDoc} */
@Override
public GridClientData data(@Nullable final String cacheName) throws GridClientException {
    checkClosed();
    Object key = maskNull(cacheName);
    GridClientDataImpl data = dataMap.get(key);
    if (data == null) {
        GridClientDataConfiguration dataCfg = cfg.getDataConfiguration(cacheName);
        if (dataCfg == null && cacheName != null)
            throw new GridClientException("Data configuration for given cache name was not provided: " + cacheName);
        GridClientLoadBalancer balancer = dataCfg != null ? dataCfg.getPinnedBalancer() : new GridClientRandomBalancer();
        GridClientPredicate<GridClientNode> cacheNodes = new GridClientPredicate<GridClientNode>() {

            @Override
            public boolean apply(GridClientNode e) {
                return e.caches().containsKey(cacheName);
            }

            @Override
            public String toString() {
                return "GridClientHasCacheFilter [cacheName=" + cacheName + "]";
            }
        };
        data = new GridClientDataImpl(cacheName, this, null, cacheNodes, balancer, null, cfg.isEnableMetricsCache());
        GridClientDataImpl old = dataMap.putIfAbsent(key, data);
        if (old != null)
            data = old;
    }
    return data;
}
Also used : GridClientException(org.apache.ignite.internal.client.GridClientException) GridClientNode(org.apache.ignite.internal.client.GridClientNode) GridClientPredicate(org.apache.ignite.internal.client.GridClientPredicate) GridClientLoadBalancer(org.apache.ignite.internal.client.balancer.GridClientLoadBalancer) GridClientDataConfiguration(org.apache.ignite.internal.client.GridClientDataConfiguration) GridClientRandomBalancer(org.apache.ignite.internal.client.balancer.GridClientRandomBalancer)

Aggregations

GridClientException (org.apache.ignite.internal.client.GridClientException)25 GridClientNode (org.apache.ignite.internal.client.GridClientNode)6 GridServerUnreachableException (org.apache.ignite.internal.client.GridServerUnreachableException)5 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 SQLException (java.sql.SQLException)3 List (java.util.List)3 UUID (java.util.UUID)3 GridClient (org.apache.ignite.internal.client.GridClient)3 GridClientConfiguration (org.apache.ignite.internal.client.GridClientConfiguration)3 GridClientConnection (org.apache.ignite.internal.client.impl.connection.GridClientConnection)3 GridClientConnectionResetException (org.apache.ignite.internal.client.impl.connection.GridClientConnectionResetException)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 InetSocketAddress (java.net.InetSocketAddress)2 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)2 IgniteInterruptedCheckedException (org.apache.ignite.internal.IgniteInterruptedCheckedException)2 GridClientCompute (org.apache.ignite.internal.client.GridClientCompute)2 GridClientDataAffinity (org.apache.ignite.internal.client.GridClientDataAffinity)2 GridClientFuture (org.apache.ignite.internal.client.GridClientFuture)2 GridClientConnectionManager (org.apache.ignite.internal.client.impl.connection.GridClientConnectionManager)2