Search in sources :

Example 6 with ClusterService

use of org.elasticsearch.cluster.service.ClusterService 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 7 with ClusterService

use of org.elasticsearch.cluster.service.ClusterService in project elasticsearch by elastic.

the class ClusterServiceUtils method createClusterService.

public static ClusterService createClusterService(ClusterState initialState, ThreadPool threadPool) {
    ClusterService clusterService = createClusterService(threadPool);
    setState(clusterService, initialState);
    return clusterService;
}
Also used : ClusterService(org.elasticsearch.cluster.service.ClusterService)

Example 8 with ClusterService

use of org.elasticsearch.cluster.service.ClusterService in project elasticsearch by elastic.

the class ZenDiscoveryIT method testHandleNodeJoin_incompatibleClusterState.

public void testHandleNodeJoin_incompatibleClusterState() throws UnknownHostException {
    Settings nodeSettings = Settings.builder().put("discovery.type", // <-- To override the local setting if set externally
    "zen").build();
    String masterOnlyNode = internalCluster().startMasterOnlyNode(nodeSettings);
    String node1 = internalCluster().startNode(nodeSettings);
    ZenDiscovery zenDiscovery = (ZenDiscovery) internalCluster().getInstance(Discovery.class, masterOnlyNode);
    ClusterService clusterService = internalCluster().getInstance(ClusterService.class, node1);
    final ClusterState state = clusterService.state();
    MetaData.Builder mdBuilder = MetaData.builder(state.metaData());
    mdBuilder.putCustom(CustomMetaData.TYPE, new CustomMetaData("data"));
    ClusterState stateWithCustomMetaData = ClusterState.builder(state).metaData(mdBuilder).build();
    final AtomicReference<IllegalStateException> holder = new AtomicReference<>();
    DiscoveryNode node = state.nodes().getLocalNode();
    zenDiscovery.handleJoinRequest(node, stateWithCustomMetaData, new MembershipAction.JoinCallback() {

        @Override
        public void onSuccess() {
        }

        @Override
        public void onFailure(Exception e) {
            holder.set((IllegalStateException) e);
        }
    });
    assertThat(holder.get(), notNullValue());
    assertThat(holder.get().getMessage(), equalTo("failure when sending a validation request to node"));
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) Discovery(org.elasticsearch.discovery.Discovery) AtomicReference(java.util.concurrent.atomic.AtomicReference) Matchers.containsString(org.hamcrest.Matchers.containsString) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) ExecutionException(java.util.concurrent.ExecutionException) TransportException(org.elasticsearch.transport.TransportException) TestCustomMetaData(org.elasticsearch.test.TestCustomMetaData) ClusterService(org.elasticsearch.cluster.service.ClusterService) MetaData(org.elasticsearch.cluster.metadata.MetaData) TestCustomMetaData(org.elasticsearch.test.TestCustomMetaData) Settings(org.elasticsearch.common.settings.Settings)

Example 9 with ClusterService

use of org.elasticsearch.cluster.service.ClusterService in project elasticsearch by elastic.

the class ZenDiscoveryUnitTests method testPendingCSQueueIsClearedWhenClusterStatePublished.

public void testPendingCSQueueIsClearedWhenClusterStatePublished() throws Exception {
    ThreadPool threadPool = new TestThreadPool(getClass().getName());
    // randomly make minimum_master_nodes a value higher than we have nodes for, so it will force failure
    int minMasterNodes = randomBoolean() ? 3 : 1;
    Settings settings = Settings.builder().put(DISCOVERY_ZEN_MINIMUM_MASTER_NODES_SETTING.getKey(), Integer.toString(minMasterNodes)).build();
    ArrayDeque<Closeable> toClose = new ArrayDeque<>();
    try {
        final MockTransportService masterTransport = MockTransportService.createNewService(settings, Version.CURRENT, threadPool, null);
        masterTransport.start();
        DiscoveryNode masterNode = masterTransport.getLocalNode();
        toClose.addFirst(masterTransport);
        ClusterState state = ClusterStateCreationUtils.state(masterNode, null, masterNode);
        // build the zen discovery and cluster service
        ClusterService masterClusterService = createClusterService(threadPool, masterNode);
        toClose.addFirst(masterClusterService);
        state = ClusterState.builder(masterClusterService.getClusterName()).nodes(state.nodes()).build();
        setState(masterClusterService, state);
        ZenDiscovery masterZen = buildZenDiscovery(settings, masterTransport, masterClusterService, threadPool);
        toClose.addFirst(masterZen);
        masterTransport.acceptIncomingRequests();
        // inject a pending cluster state
        masterZen.pendingClusterStatesQueue().addPending(ClusterState.builder(new ClusterName("foreign")).build());
        // a new cluster state with a new discovery node (we will test if the cluster state
        // was updated by the presence of this node in NodesFaultDetection)
        ClusterState newState = ClusterState.builder(masterClusterService.state()).incrementVersion().nodes(DiscoveryNodes.builder(masterClusterService.state().nodes()).masterNodeId(masterNode.getId())).build();
        try {
            // publishing a new cluster state
            ClusterChangedEvent clusterChangedEvent = new ClusterChangedEvent("testing", newState, state);
            AssertingAckListener listener = new AssertingAckListener(newState.nodes().getSize() - 1);
            masterZen.publish(clusterChangedEvent, listener);
            listener.await(1, TimeUnit.HOURS);
            // publish was a success, check that queue as cleared
            assertThat(masterZen.pendingClusterStates(), emptyArray());
        } catch (Discovery.FailedToCommitClusterStateException e) {
            // not successful, so the pending queue should stay
            assertThat(masterZen.pendingClusterStates(), arrayWithSize(1));
            assertThat(masterZen.pendingClusterStates()[0].getClusterName().value(), equalTo("foreign"));
        }
    } finally {
        IOUtils.close(toClose);
        terminate(threadPool);
    }
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) ZenDiscovery.shouldIgnoreOrRejectNewClusterState(org.elasticsearch.discovery.zen.ZenDiscovery.shouldIgnoreOrRejectNewClusterState) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) MockTransportService(org.elasticsearch.test.transport.MockTransportService) Closeable(java.io.Closeable) ThreadPool(org.elasticsearch.threadpool.ThreadPool) TestThreadPool(org.elasticsearch.threadpool.TestThreadPool) Discovery(org.elasticsearch.discovery.Discovery) ClusterChangedEvent(org.elasticsearch.cluster.ClusterChangedEvent) TestThreadPool(org.elasticsearch.threadpool.TestThreadPool) ArrayDeque(java.util.ArrayDeque) ClusterServiceUtils.createClusterService(org.elasticsearch.test.ClusterServiceUtils.createClusterService) ClusterService(org.elasticsearch.cluster.service.ClusterService) AssertingAckListener(org.elasticsearch.discovery.zen.PublishClusterStateActionTests.AssertingAckListener) ClusterName(org.elasticsearch.cluster.ClusterName) Settings(org.elasticsearch.common.settings.Settings)

Example 10 with ClusterService

use of org.elasticsearch.cluster.service.ClusterService in project elasticsearch by elastic.

the class DiscoveryModuleTests method setupDummyServices.

@Before
public void setupDummyServices() {
    transportService = MockTransportService.createNewService(Settings.EMPTY, Version.CURRENT, null, null);
    clusterService = mock(ClusterService.class);
    namedWriteableRegistry = new NamedWriteableRegistry(Collections.emptyList());
    ClusterSettings clusterSettings = new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS);
    when(clusterService.getClusterSettings()).thenReturn(clusterSettings);
    threadPool = mock(ThreadPool.class);
}
Also used : NamedWriteableRegistry(org.elasticsearch.common.io.stream.NamedWriteableRegistry) ClusterService(org.elasticsearch.cluster.service.ClusterService) ClusterSettings(org.elasticsearch.common.settings.ClusterSettings) ThreadPool(org.elasticsearch.threadpool.ThreadPool) TestThreadPool(org.elasticsearch.threadpool.TestThreadPool) Before(org.junit.Before)

Aggregations

ClusterService (org.elasticsearch.cluster.service.ClusterService)52 ClusterState (org.elasticsearch.cluster.ClusterState)31 Settings (org.elasticsearch.common.settings.Settings)25 ThreadPool (org.elasticsearch.threadpool.ThreadPool)20 TransportService (org.elasticsearch.transport.TransportService)20 TestThreadPool (org.elasticsearch.threadpool.TestThreadPool)17 CountDownLatch (java.util.concurrent.CountDownLatch)15 IOException (java.io.IOException)13 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)13 ActionFilters (org.elasticsearch.action.support.ActionFilters)12 IndexNameExpressionResolver (org.elasticsearch.cluster.metadata.IndexNameExpressionResolver)12 ClusterServiceUtils.createClusterService (org.elasticsearch.test.ClusterServiceUtils.createClusterService)12 Before (org.junit.Before)12 ShardId (org.elasticsearch.index.shard.ShardId)11 ArrayList (java.util.ArrayList)10 HashSet (java.util.HashSet)10 TimeUnit (java.util.concurrent.TimeUnit)10 ExecutionException (java.util.concurrent.ExecutionException)9 DiscoveryNodes (org.elasticsearch.cluster.node.DiscoveryNodes)9 ESTestCase (org.elasticsearch.test.ESTestCase)9