Search in sources :

Example 36 with DiscoveryNodes

use of org.elasticsearch.cluster.node.DiscoveryNodes in project crate by crate.

the class NodeDisconnectJobMonitorServiceTest method testOnParticipatingNodeDisconnectedKillsJob.

@Test
public void testOnParticipatingNodeDisconnectedKillsJob() throws Exception {
    JobContextService jobContextService = jobContextService();
    DiscoveryNode coordinator_node = new DiscoveryNode("coordinator_node_id", DummyTransportAddress.INSTANCE, Version.CURRENT);
    DiscoveryNode data_node = new DiscoveryNode("data_node_id", DummyTransportAddress.INSTANCE, Version.CURRENT);
    DiscoveryNodes discoveryNodes = DiscoveryNodes.builder().localNodeId("coordinator_node_id").put(coordinator_node).put(data_node).build();
    JobExecutionContext.Builder builder = jobContextService.newBuilder(UUID.randomUUID(), coordinator_node.getId(), Arrays.asList(coordinator_node.getId(), data_node.getId()));
    builder.addSubContext(new DummySubContext());
    jobContextService.createContext(builder);
    TransportKillJobsNodeAction killAction = mock(TransportKillJobsNodeAction.class);
    NodeDisconnectJobMonitorService monitorService = new NodeDisconnectJobMonitorService(Settings.EMPTY, mock(ThreadPool.class), jobContextService, mock(TransportService.class), killAction);
    monitorService.onNodeDisconnected(discoveryNodes.get("data_node_id"));
    verify(killAction, times(1)).broadcast(any(KillJobsRequest.class), any(ActionListener.class), eq(Arrays.asList(discoveryNodes.get("data_node_id").getId())));
}
Also used : TransportKillJobsNodeAction(io.crate.executor.transport.kill.TransportKillJobsNodeAction) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) DummySubContext(io.crate.jobs.DummySubContext) ActionListener(org.elasticsearch.action.ActionListener) TransportService(org.elasticsearch.transport.TransportService) KillJobsRequest(io.crate.executor.transport.kill.KillJobsRequest) ThreadPool(org.elasticsearch.threadpool.ThreadPool) JobExecutionContext(io.crate.jobs.JobExecutionContext) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes) JobContextService(io.crate.jobs.JobContextService) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 37 with DiscoveryNodes

use of org.elasticsearch.cluster.node.DiscoveryNodes in project elasticsearch by elastic.

the class InternalTestCluster method nodesInclude.

/**
     * Returns a set of nodes that have at least one shard of the given index.
     */
public synchronized Set<String> nodesInclude(String index) {
    if (clusterService().state().routingTable().hasIndex(index)) {
        List<ShardRouting> allShards = clusterService().state().routingTable().allShards(index);
        DiscoveryNodes discoveryNodes = clusterService().state().getNodes();
        Set<String> nodes = new HashSet<>();
        for (ShardRouting shardRouting : allShards) {
            if (shardRouting.assignedToNode()) {
                DiscoveryNode discoveryNode = discoveryNodes.get(shardRouting.currentNodeId());
                nodes.add(discoveryNode.getName());
            }
        }
        return nodes;
    }
    return Collections.emptySet();
}
Also used : DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes) HashSet(java.util.HashSet)

Example 38 with DiscoveryNodes

use of org.elasticsearch.cluster.node.DiscoveryNodes in project elasticsearch by elastic.

the class ClusterServiceUtils method createClusterService.

public static ClusterService createClusterService(Settings settings, ThreadPool threadPool, DiscoveryNode localNode) {
    ClusterService clusterService = new ClusterService(Settings.builder().put("cluster.name", "ClusterServiceTests").put(settings).build(), new ClusterSettings(settings, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS), threadPool, () -> localNode);
    clusterService.setNodeConnectionsService(new NodeConnectionsService(Settings.EMPTY, null, null) {

        @Override
        public void connectToNodes(DiscoveryNodes discoveryNodes) {
        // skip
        }

        @Override
        public void disconnectFromNodesExcept(DiscoveryNodes nodesToKeep) {
        // skip
        }
    });
    clusterService.setClusterStatePublisher((event, ackListener) -> {
    });
    clusterService.setDiscoverySettings(new DiscoverySettings(Settings.EMPTY, new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS)));
    clusterService.start();
    final DiscoveryNodes.Builder nodes = DiscoveryNodes.builder(clusterService.state().nodes());
    nodes.masterNodeId(clusterService.localNode().getId());
    setState(clusterService, ClusterState.builder(clusterService.state()).nodes(nodes));
    return clusterService;
}
Also used : ClusterService(org.elasticsearch.cluster.service.ClusterService) ClusterSettings(org.elasticsearch.common.settings.ClusterSettings) NodeConnectionsService(org.elasticsearch.cluster.NodeConnectionsService) DiscoverySettings(org.elasticsearch.discovery.DiscoverySettings) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes)

Example 39 with DiscoveryNodes

use of org.elasticsearch.cluster.node.DiscoveryNodes in project elasticsearch by elastic.

the class ClusterServiceTests method testLocalNodeMasterListenerCallbacks.

public void testLocalNodeMasterListenerCallbacks() throws Exception {
    TimedClusterService timedClusterService = createTimedClusterService(false);
    AtomicBoolean isMaster = new AtomicBoolean();
    timedClusterService.addLocalNodeMasterListener(new LocalNodeMasterListener() {

        @Override
        public void onMaster() {
            isMaster.set(true);
        }

        @Override
        public void offMaster() {
            isMaster.set(false);
        }

        @Override
        public String executorName() {
            return ThreadPool.Names.SAME;
        }
    });
    ClusterState state = timedClusterService.state();
    DiscoveryNodes nodes = state.nodes();
    DiscoveryNodes.Builder nodesBuilder = DiscoveryNodes.builder(nodes).masterNodeId(nodes.getLocalNodeId());
    state = ClusterState.builder(state).blocks(ClusterBlocks.EMPTY_CLUSTER_BLOCK).nodes(nodesBuilder).build();
    setState(timedClusterService, state);
    assertThat(isMaster.get(), is(true));
    nodes = state.nodes();
    nodesBuilder = DiscoveryNodes.builder(nodes).masterNodeId(null);
    state = ClusterState.builder(state).blocks(ClusterBlocks.builder().addGlobalBlock(DiscoverySettings.NO_MASTER_BLOCK_WRITES)).nodes(nodesBuilder).build();
    setState(timedClusterService, state);
    assertThat(isMaster.get(), is(false));
    nodesBuilder = DiscoveryNodes.builder(nodes).masterNodeId(nodes.getLocalNodeId());
    state = ClusterState.builder(state).blocks(ClusterBlocks.EMPTY_CLUSTER_BLOCK).nodes(nodesBuilder).build();
    setState(timedClusterService, state);
    assertThat(isMaster.get(), is(true));
    timedClusterService.close();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ClusterState(org.elasticsearch.cluster.ClusterState) LocalNodeMasterListener(org.elasticsearch.cluster.LocalNodeMasterListener) Matchers.hasToString(org.hamcrest.Matchers.hasToString) Matchers.containsString(org.hamcrest.Matchers.containsString) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes)

Example 40 with DiscoveryNodes

use of org.elasticsearch.cluster.node.DiscoveryNodes in project crate by crate.

the class BlobService method getRedirectAddress.

/**
 * @param index  the name of blob-enabled index
 * @param digest sha-1 hash value of the file
 * @return null if no redirect is required, Otherwise the address to which should be redirected.
 */
public String getRedirectAddress(String index, String digest) throws MissingHTTPEndpointException {
    ShardIterator shards = clusterService.operationRouting().getShards(clusterService.state(), index, null, digest, "_local");
    String localNodeId = clusterService.localNode().getId();
    DiscoveryNodes nodes = clusterService.state().getNodes();
    ShardRouting shard;
    while ((shard = shards.nextOrNull()) != null) {
        if (!shard.active()) {
            continue;
        }
        if (shard.currentNodeId().equals(localNodeId)) {
            // no redirect required if the shard is on this node
            return null;
        }
        DiscoveryNode node = nodes.get(shard.currentNodeId());
        String httpAddress = node.getAttributes().get("http_address");
        if (httpAddress != null) {
            return httpAddress + "/_blobs/" + BlobIndex.stripPrefix(index) + "/" + digest;
        }
    }
    throw new MissingHTTPEndpointException("Can't find a suitable http server to serve the blob");
}
Also used : DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) ShardIterator(org.elasticsearch.cluster.routing.ShardIterator) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) MissingHTTPEndpointException(io.crate.blob.exceptions.MissingHTTPEndpointException) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes)

Aggregations

DiscoveryNodes (org.elasticsearch.cluster.node.DiscoveryNodes)129 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)74 ClusterState (org.elasticsearch.cluster.ClusterState)45 Settings (org.elasticsearch.common.settings.Settings)37 ArrayList (java.util.ArrayList)32 IOException (java.io.IOException)27 HashSet (java.util.HashSet)25 List (java.util.List)24 Map (java.util.Map)23 TransportService (org.elasticsearch.transport.TransportService)23 Version (org.elasticsearch.Version)22 HashMap (java.util.HashMap)20 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)20 ShardRouting (org.elasticsearch.cluster.routing.ShardRouting)20 Set (java.util.Set)19 TransportException (org.elasticsearch.transport.TransportException)19 Collections (java.util.Collections)18 ThreadPool (org.elasticsearch.threadpool.ThreadPool)18 CountDownLatch (java.util.concurrent.CountDownLatch)16 Collectors (java.util.stream.Collectors)16