Search in sources :

Example 11 with GridClientNode

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

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

the class ClientAbstractConnectivitySelfTest method testTwoNodesDefaultHostAndPort.

/**
     * Tests correct behavior in case of 2 REST-enabled nodes with default
     * settings.
     *
     * @throws Exception If error occurs.
     */
public void testTwoNodesDefaultHostAndPort() throws Exception {
    startRestNode("grid1", null, null);
    startRestNode("grid2", null, null);
    GridClient cli = startClient(LOOPBACK_IP, defaultRestPort());
    List<GridClientNode> nodes = cli.compute().refreshTopology(true, false);
    assertEquals(2, nodes.size());
    assertTrue(F.forAll(nodes, new P1<GridClientNode>() {

        @Override
        public boolean apply(GridClientNode node) {
            return node.tcpAddresses().contains(LOOPBACK_IP);
        }
    }));
    GridTestUtils.assertOneToOne(nodes, new P1<GridClientNode>() {

        @Override
        public boolean apply(GridClientNode node) {
            try {
                return eqAddresses(getAllIps(), node) && Integer.valueOf(defaultRestPort()).equals(node.attribute(restPortAttributeName()));
            } catch (Exception ignored) {
                return false;
            }
        }
    }, new P1<GridClientNode>() {

        @Override
        public boolean apply(GridClientNode node) {
            try {
                return eqAddresses(getAllIps(), node) && Integer.valueOf(defaultRestPort() + 1).equals(node.attribute(restPortAttributeName()));
            } catch (Exception ignored) {
                return false;
            }
        }
    });
}
Also used : GridClientNode(org.apache.ignite.internal.client.GridClientNode) P1(org.apache.ignite.internal.util.typedef.P1) GridClient(org.apache.ignite.internal.client.GridClient) GridClientException(org.apache.ignite.internal.client.GridClientException)

Example 13 with GridClientNode

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

the class ClientAbstractSelfTest method testNoAsyncExceptions.

/**
     * Check async API methods don't generate exceptions.
     *
     * @throws Exception If failed.
     */
public void testNoAsyncExceptions() throws Exception {
    GridClient client = client();
    GridClientData data = client.data(CACHE_NAME);
    GridClientCompute compute = client.compute().projection(new GridClientPredicate<GridClientNode>() {

        @Override
        public boolean apply(GridClientNode e) {
            return false;
        }
    });
    Map<String, GridClientFuture<?>> futs = new LinkedHashMap<>();
    futs.put("exec", compute.executeAsync("taskName", "taskArg"));
    futs.put("affExec", compute.affinityExecuteAsync("taskName", "cacheName", "affKey", "taskArg"));
    futs.put("refreshById", compute.refreshNodeAsync(UUID.randomUUID(), true, true));
    futs.put("refreshByIP", compute.refreshNodeAsync("nodeIP", true, true));
    futs.put("refreshTop", compute.refreshTopologyAsync(true, true));
    GridClientFactory.stop(client.id(), false);
    futs.put("put", data.putAsync("key", "val"));
    futs.put("putAll", data.putAllAsync(F.asMap("key", "val")));
    futs.put("get", data.getAsync("key"));
    futs.put("getAll", data.getAllAsync(Collections.singletonList("key")));
    futs.put("remove", data.removeAsync("key"));
    futs.put("removeAll", data.removeAllAsync(Collections.singletonList("key")));
    futs.put("replace", data.replaceAsync("key", "val"));
    futs.put("cas", data.casAsync("key", "val", "val2"));
    futs.put("metrics", data.metricsAsync());
    for (Map.Entry<String, GridClientFuture<?>> e : futs.entrySet()) {
        try {
            e.getValue().get();
            info("Expects '" + e.getKey() + "' fails with grid client exception.");
        } catch (GridServerUnreachableException | GridClientClosedException ignore) {
        // No op: compute projection is empty.
        }
    }
}
Also used : GridClientCompute(org.apache.ignite.internal.client.GridClientCompute) GridClientNode(org.apache.ignite.internal.client.GridClientNode) GridClientFuture(org.apache.ignite.internal.client.GridClientFuture) GridClient(org.apache.ignite.internal.client.GridClient) LinkedHashMap(java.util.LinkedHashMap) GridServerUnreachableException(org.apache.ignite.internal.client.GridServerUnreachableException) GridClientData(org.apache.ignite.internal.client.GridClientData) GridClientClosedException(org.apache.ignite.internal.client.GridClientClosedException) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) LinkedHashMap(java.util.LinkedHashMap)

Example 14 with GridClientNode

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

the class ClientAbstractMultiNodeSelfTest method testTopologyListener.

/**
     * @throws Exception If failed.
     */
public void testTopologyListener() throws Exception {
    final Collection<UUID> added = new ArrayList<>(1);
    final Collection<UUID> rmvd = new ArrayList<>(1);
    final CountDownLatch addedLatch = new CountDownLatch(1);
    final CountDownLatch rmvLatch = new CountDownLatch(1);
    assertEquals(NODES_CNT, client.compute().refreshTopology(false, false).size());
    GridClientTopologyListener lsnr = new GridClientTopologyListener() {

        @Override
        public void onNodeAdded(GridClientNode node) {
            added.add(node.nodeId());
            addedLatch.countDown();
        }

        @Override
        public void onNodeRemoved(GridClientNode node) {
            rmvd.add(node.nodeId());
            rmvLatch.countDown();
        }
    };
    client.addTopologyListener(lsnr);
    try {
        Ignite g = startGrid(NODES_CNT + 1);
        UUID id = g.cluster().localNode().id();
        assertTrue(addedLatch.await(2 * TOP_REFRESH_FREQ, MILLISECONDS));
        assertEquals(1, added.size());
        assertEquals(id, F.first(added));
        stopGrid(NODES_CNT + 1);
        assertTrue(rmvLatch.await(2 * TOP_REFRESH_FREQ, MILLISECONDS));
        assertEquals(1, rmvd.size());
        assertEquals(id, F.first(rmvd));
    } finally {
        client.removeTopologyListener(lsnr);
        stopGrid(NODES_CNT + 1);
    }
}
Also used : GridClientNode(org.apache.ignite.internal.client.GridClientNode) GridClientTopologyListener(org.apache.ignite.internal.client.GridClientTopologyListener) ArrayList(java.util.ArrayList) Ignite(org.apache.ignite.Ignite) UUID(java.util.UUID) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 15 with GridClientNode

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

the class GridClientDataImpl method affinity.

/** {@inheritDoc} */
@SuppressWarnings("unchecked")
@Override
public <K> UUID affinity(K key) throws GridClientException {
    A.notNull(key, "key");
    GridClientDataAffinity affinity = client.affinity(cacheName);
    if (affinity == null)
        return null;
    Collection<? extends GridClientNode> prj = projectionNodes();
    if (prj.isEmpty())
        throw new GridClientException("Failed to get affinity node (projection node set for cache is empty): " + cacheName());
    GridClientNode node = affinity.node(key, prj);
    assert node != null;
    return node.nodeId();
}
Also used : GridClientException(org.apache.ignite.internal.client.GridClientException) GridClientNode(org.apache.ignite.internal.client.GridClientNode) GridClientDataAffinity(org.apache.ignite.internal.client.GridClientDataAffinity)

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