Java Code Examples for org.elasticsearch.cluster.node.DiscoveryNodes#getSize()

The following examples show how to use org.elasticsearch.cluster.node.DiscoveryNodes#getSize() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NodeConnectionsService.java    From crate with Apache License 2.0 5 votes vote down vote up
/**
 * Connect to all the given nodes, but do not disconnect from any extra nodes. Calls the completion handler on completion of all
 * connection attempts to _new_ nodes, but not on attempts to re-establish connections to nodes that are already known.
 */
public void connectToNodes(DiscoveryNodes discoveryNodes, Runnable onCompletion) {

    if (discoveryNodes.getSize() == 0) {
        onCompletion.run();
        return;
    }

    final GroupedActionListener<Void> listener
        = new GroupedActionListener<>(ActionListener.wrap(onCompletion), discoveryNodes.getSize());

    final List<Runnable> runnables = new ArrayList<>(discoveryNodes.getSize());
    synchronized (mutex) {
        for (final DiscoveryNode discoveryNode : discoveryNodes) {
            ConnectionTarget connectionTarget = targetsByNode.get(discoveryNode);
            final boolean isNewNode;
            if (connectionTarget == null) {
                // new node, set up target and listener
                connectionTarget = new ConnectionTarget(discoveryNode);
                targetsByNode.put(discoveryNode, connectionTarget);
                isNewNode = true;
            } else {
                // existing node, but maybe we're disconnecting from it, in which case it was recently removed from the cluster
                // state and has now been re-added so we should wait for the re-connection
                isNewNode = connectionTarget.isPendingDisconnection();
            }

            if (isNewNode) {
                runnables.add(connectionTarget.connect(listener));
            } else {
                // known node, try and ensure it's connected but do not wait
                runnables.add(connectionTarget.connect(null));
                runnables.add(() -> listener.onResponse(null));
            }
        }
    }
    runnables.forEach(Runnable::run);
}
 
Example 2
Source File: CopyFromPlan.java    From crate with Apache License 2.0 5 votes vote down vote up
private static Collection<String> getExecutionNodes(DiscoveryNodes allNodes,
                                                    int maxNodes,
                                                    final Predicate<DiscoveryNode> nodeFilters) {
    int counter = maxNodes;
    final List<String> nodes = new ArrayList<>(allNodes.getSize());
    for (ObjectCursor<DiscoveryNode> cursor : allNodes.getDataNodes().values()) {
        if (nodeFilters.test(cursor.value) && counter-- > 0) {
            nodes.add(cursor.value.getId());
        }
    }
    return nodes;
}
 
Example 3
Source File: ExplainAnalyzeIntegrationTest.java    From crate with Apache License 2.0 5 votes vote down vote up
@Test
public void testExplainSelectWithoutJobExecutionContexts() {
    execute("explain analyze select 1");
    Map<String, Object> analysis = (Map<String, Object>) response.rows()[0][0];
    Map<String, Object> executeAnalysis = (Map<String, Object>) analysis.get("Execute");
    assertTrue(executeAnalysis.keySet().contains("Total"));
    DiscoveryNodes nodes = clusterService().state().nodes();
    List<Matcher<String>> nodeIds = new ArrayList<>(nodes.getSize());
    for (DiscoveryNode discoveryNode : nodes) {
        nodeIds.add(is(discoveryNode.getId()));
    }
    assertThat(executeAnalysis.keySet(), hasItems(is("Total"), anyOf(nodeIds.toArray(new Matcher[]{}))));
}