Search in sources :

Example 6 with ClusterName

use of org.elasticsearch.cluster.ClusterName in project elasticsearch by elastic.

the class ZenDiscoveryUnitTests method testFilterNonMasterPingResponse.

public void testFilterNonMasterPingResponse() {
    ArrayList<ZenPing.PingResponse> responses = new ArrayList<>();
    ArrayList<DiscoveryNode> masterNodes = new ArrayList<>();
    ArrayList<DiscoveryNode> allNodes = new ArrayList<>();
    for (int i = randomIntBetween(10, 20); i >= 0; i--) {
        Set<Role> roles = new HashSet<>(randomSubsetOf(Arrays.asList(Role.values())));
        DiscoveryNode node = new DiscoveryNode("node_" + i, "id_" + i, buildNewFakeTransportAddress(), Collections.emptyMap(), roles, Version.CURRENT);
        responses.add(new ZenPing.PingResponse(node, randomBoolean() ? null : node, new ClusterName("test"), randomLong()));
        allNodes.add(node);
        if (node.isMasterNode()) {
            masterNodes.add(node);
        }
    }
    boolean ignore = randomBoolean();
    List<ZenPing.PingResponse> filtered = ZenDiscovery.filterPingResponses(responses, ignore, logger);
    final List<DiscoveryNode> filteredNodes = filtered.stream().map(ZenPing.PingResponse::node).collect(Collectors.toList());
    if (ignore) {
        assertThat(filteredNodes, equalTo(masterNodes));
    } else {
        assertThat(filteredNodes, equalTo(allNodes));
    }
}
Also used : DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) ArrayList(java.util.ArrayList) Role(org.elasticsearch.cluster.node.DiscoveryNode.Role) ClusterName(org.elasticsearch.cluster.ClusterName) HashSet(java.util.HashSet)

Example 7 with ClusterName

use of org.elasticsearch.cluster.ClusterName in project elasticsearch by elastic.

the class UnicastZenPingTests method testPingingTemporalPings.

public void testPingingTemporalPings() throws ExecutionException, InterruptedException {
    final Settings settings = Settings.builder().put("cluster.name", "test").put(TransportSettings.PORT.getKey(), 0).build();
    NetworkService networkService = new NetworkService(settings, Collections.emptyList());
    final BiFunction<Settings, Version, Transport> supplier = (s, v) -> new MockTcpTransport(s, threadPool, BigArrays.NON_RECYCLING_INSTANCE, new NoneCircuitBreakerService(), new NamedWriteableRegistry(Collections.emptyList()), networkService, v);
    NetworkHandle handleA = startServices(settings, threadPool, "UZP_A", Version.CURRENT, supplier, EnumSet.allOf(Role.class));
    closeables.push(handleA.transportService);
    NetworkHandle handleB = startServices(settings, threadPool, "UZP_B", Version.CURRENT, supplier, EnumSet.allOf(Role.class));
    closeables.push(handleB.transportService);
    final Settings hostsSettings = Settings.builder().put("cluster.name", "test").put("discovery.zen.ping.unicast.hosts", // use nodes for simplicity
    (String) null).build();
    final ClusterState state = ClusterState.builder(new ClusterName("test")).version(randomNonNegativeLong()).build();
    final TestUnicastZenPing zenPingA = new TestUnicastZenPing(hostsSettings, threadPool, handleA, EMPTY_HOSTS_PROVIDER);
    zenPingA.start(new PingContextProvider() {

        @Override
        public DiscoveryNodes nodes() {
            return DiscoveryNodes.builder().add(handleA.node).add(handleB.node).localNodeId("UZP_A").build();
        }

        @Override
        public ClusterState clusterState() {
            return ClusterState.builder(state).blocks(ClusterBlocks.builder().addGlobalBlock(STATE_NOT_RECOVERED_BLOCK)).build();
        }
    });
    closeables.push(zenPingA);
    // Node B doesn't know about A!
    TestUnicastZenPing zenPingB = new TestUnicastZenPing(hostsSettings, threadPool, handleB, EMPTY_HOSTS_PROVIDER);
    zenPingB.start(new PingContextProvider() {

        @Override
        public DiscoveryNodes nodes() {
            return DiscoveryNodes.builder().add(handleB.node).localNodeId("UZP_B").build();
        }

        @Override
        public ClusterState clusterState() {
            return state;
        }
    });
    closeables.push(zenPingB);
    {
        logger.info("pinging from UZP_A so UZP_B will learn about it");
        Collection<ZenPing.PingResponse> pingResponses = zenPingA.pingAndWait().toList();
        assertThat(pingResponses.size(), equalTo(1));
        ZenPing.PingResponse ping = pingResponses.iterator().next();
        assertThat(ping.node().getId(), equalTo("UZP_B"));
        assertThat(ping.getClusterStateVersion(), equalTo(state.version()));
    }
    {
        logger.info("pinging from UZP_B");
        Collection<ZenPing.PingResponse> pingResponses = zenPingB.pingAndWait().toList();
        assertThat(pingResponses.size(), equalTo(1));
        ZenPing.PingResponse ping = pingResponses.iterator().next();
        assertThat(ping.node().getId(), equalTo("UZP_A"));
        // A has a block
        assertThat(ping.getClusterStateVersion(), equalTo(-1L));
    }
}
Also used : Arrays(java.util.Arrays) BigArrays(org.elasticsearch.common.util.BigArrays) ConcurrentCollections(org.elasticsearch.common.util.concurrent.ConcurrentCollections) BiFunction(java.util.function.BiFunction) ClusterBlocks(org.elasticsearch.cluster.block.ClusterBlocks) InetAddress(java.net.InetAddress) STATE_NOT_RECOVERED_BLOCK(org.elasticsearch.gateway.GatewayService.STATE_NOT_RECOVERED_BLOCK) ClusterState(org.elasticsearch.cluster.ClusterState) ConnectTransportException(org.elasticsearch.transport.ConnectTransportException) Settings(org.elasticsearch.common.settings.Settings) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) Matchers.eq(org.mockito.Matchers.eq) After(org.junit.After) Map(java.util.Map) ThreadPool(org.elasticsearch.threadpool.ThreadPool) ClusterName(org.elasticsearch.cluster.ClusterName) Role(org.elasticsearch.cluster.node.DiscoveryNode.Role) ThreadFactory(java.util.concurrent.ThreadFactory) EnumSet(java.util.EnumSet) Transport(org.elasticsearch.transport.Transport) Collection(java.util.Collection) MockTcpTransport(org.elasticsearch.transport.MockTcpTransport) Set(java.util.Set) InetSocketAddress(java.net.InetSocketAddress) Collectors(java.util.stream.Collectors) CountDownLatch(java.util.concurrent.CountDownLatch) AbstractRunnable(org.elasticsearch.common.util.concurrent.AbstractRunnable) List(java.util.List) Logger(org.apache.logging.log4j.Logger) Version(org.elasticsearch.Version) TransportAddress(org.elasticsearch.common.transport.TransportAddress) TransportConnectionListener(org.elasticsearch.transport.TransportConnectionListener) TransportSettings(org.elasticsearch.transport.TransportSettings) Matchers.equalTo(org.hamcrest.Matchers.equalTo) TransportResponseHandler(org.elasticsearch.transport.TransportResponseHandler) TransportRequestOptions(org.elasticsearch.transport.TransportRequestOptions) TransportException(org.elasticsearch.transport.TransportException) NetworkAddress(org.elasticsearch.common.network.NetworkAddress) Mockito.mock(org.mockito.Mockito.mock) IntStream(java.util.stream.IntStream) Matchers(org.mockito.Matchers) HashMap(java.util.HashMap) BoundTransportAddress(org.elasticsearch.common.transport.BoundTransportAddress) AtomicReference(java.util.concurrent.atomic.AtomicReference) CheckedBiConsumer(org.elasticsearch.common.CheckedBiConsumer) Stack(java.util.Stack) ArrayList(java.util.ArrayList) ConcurrentMap(java.util.concurrent.ConcurrentMap) HashSet(java.util.HashSet) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) NetworkService(org.elasticsearch.common.network.NetworkService) NoneCircuitBreakerService(org.elasticsearch.indices.breaker.NoneCircuitBreakerService) NamedWriteableRegistry(org.elasticsearch.common.io.stream.NamedWriteableRegistry) TimeValue(org.elasticsearch.common.unit.TimeValue) Matchers.hasSize(org.hamcrest.Matchers.hasSize) ESTestCase(org.elasticsearch.test.ESTestCase) MockTransportService(org.elasticsearch.test.transport.MockTransportService) TransportService(org.elasticsearch.transport.TransportService) ExecutorService(java.util.concurrent.ExecutorService) Before(org.junit.Before) ConnectionProfile(org.elasticsearch.transport.ConnectionProfile) Collections.emptyMap(java.util.Collections.emptyMap) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes) TestThreadPool(org.elasticsearch.threadpool.TestThreadPool) Matchers.empty(org.hamcrest.Matchers.empty) EsExecutors(org.elasticsearch.common.util.concurrent.EsExecutors) Collections.emptySet(java.util.Collections.emptySet) IOUtils(org.apache.lucene.util.IOUtils) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) VersionUtils(org.elasticsearch.test.VersionUtils) Mockito.verify(org.mockito.Mockito.verify) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Closeable(java.io.Closeable) Collections(java.util.Collections) NamedWriteableRegistry(org.elasticsearch.common.io.stream.NamedWriteableRegistry) ClusterState(org.elasticsearch.cluster.ClusterState) MockTcpTransport(org.elasticsearch.transport.MockTcpTransport) Role(org.elasticsearch.cluster.node.DiscoveryNode.Role) Version(org.elasticsearch.Version) NetworkService(org.elasticsearch.common.network.NetworkService) ClusterName(org.elasticsearch.cluster.ClusterName) Collection(java.util.Collection) Transport(org.elasticsearch.transport.Transport) MockTcpTransport(org.elasticsearch.transport.MockTcpTransport) Settings(org.elasticsearch.common.settings.Settings) TransportSettings(org.elasticsearch.transport.TransportSettings) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes) NoneCircuitBreakerService(org.elasticsearch.indices.breaker.NoneCircuitBreakerService)

Example 8 with ClusterName

use of org.elasticsearch.cluster.ClusterName in project elasticsearch by elastic.

the class UnicastZenPingTests method testResolveReuseExistingNodeConnections.

public void testResolveReuseExistingNodeConnections() throws ExecutionException, InterruptedException {
    final Settings settings = Settings.builder().put("cluster.name", "test").put(TransportSettings.PORT.getKey(), 0).build();
    NetworkService networkService = new NetworkService(settings, Collections.emptyList());
    final BiFunction<Settings, Version, Transport> supplier = (s, v) -> new MockTcpTransport(s, threadPool, BigArrays.NON_RECYCLING_INSTANCE, new NoneCircuitBreakerService(), new NamedWriteableRegistry(Collections.emptyList()), networkService, v);
    NetworkHandle handleA = startServices(settings, threadPool, "UZP_A", Version.CURRENT, supplier, EnumSet.allOf(Role.class));
    closeables.push(handleA.transportService);
    NetworkHandle handleB = startServices(settings, threadPool, "UZP_B", Version.CURRENT, supplier, EnumSet.allOf(Role.class));
    closeables.push(handleB.transportService);
    final boolean useHosts = randomBoolean();
    final Settings.Builder hostsSettingsBuilder = Settings.builder().put("cluster.name", "test");
    if (useHosts) {
        hostsSettingsBuilder.putArray("discovery.zen.ping.unicast.hosts", NetworkAddress.format(new InetSocketAddress(handleB.address.address().getAddress(), handleB.address.address().getPort())));
    } else {
        hostsSettingsBuilder.put("discovery.zen.ping.unicast.hosts", (String) null);
    }
    final Settings hostsSettings = hostsSettingsBuilder.build();
    final ClusterState state = ClusterState.builder(new ClusterName("test")).version(randomNonNegativeLong()).build();
    // connection to reuse
    handleA.transportService.connectToNode(handleB.node);
    // install a listener to check that no new connections are made
    handleA.transportService.addConnectionListener(new TransportConnectionListener() {

        @Override
        public void onConnectionOpened(DiscoveryNode node) {
            fail("should not open any connections. got [" + node + "]");
        }
    });
    final TestUnicastZenPing zenPingA = new TestUnicastZenPing(hostsSettings, threadPool, handleA, EMPTY_HOSTS_PROVIDER);
    zenPingA.start(new PingContextProvider() {

        @Override
        public DiscoveryNodes nodes() {
            return DiscoveryNodes.builder().add(handleA.node).add(handleB.node).localNodeId("UZP_A").build();
        }

        @Override
        public ClusterState clusterState() {
            return ClusterState.builder(state).blocks(ClusterBlocks.builder().addGlobalBlock(STATE_NOT_RECOVERED_BLOCK)).build();
        }
    });
    closeables.push(zenPingA);
    TestUnicastZenPing zenPingB = new TestUnicastZenPing(hostsSettings, threadPool, handleB, EMPTY_HOSTS_PROVIDER);
    zenPingB.start(new PingContextProvider() {

        @Override
        public DiscoveryNodes nodes() {
            return DiscoveryNodes.builder().add(handleB.node).localNodeId("UZP_B").build();
        }

        @Override
        public ClusterState clusterState() {
            return state;
        }
    });
    closeables.push(zenPingB);
    Collection<ZenPing.PingResponse> pingResponses = zenPingA.pingAndWait().toList();
    assertThat(pingResponses.size(), equalTo(1));
    ZenPing.PingResponse ping = pingResponses.iterator().next();
    assertThat(ping.node().getId(), equalTo("UZP_B"));
    assertThat(ping.getClusterStateVersion(), equalTo(state.version()));
}
Also used : Arrays(java.util.Arrays) BigArrays(org.elasticsearch.common.util.BigArrays) ConcurrentCollections(org.elasticsearch.common.util.concurrent.ConcurrentCollections) BiFunction(java.util.function.BiFunction) ClusterBlocks(org.elasticsearch.cluster.block.ClusterBlocks) InetAddress(java.net.InetAddress) STATE_NOT_RECOVERED_BLOCK(org.elasticsearch.gateway.GatewayService.STATE_NOT_RECOVERED_BLOCK) ClusterState(org.elasticsearch.cluster.ClusterState) ConnectTransportException(org.elasticsearch.transport.ConnectTransportException) Settings(org.elasticsearch.common.settings.Settings) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) Matchers.eq(org.mockito.Matchers.eq) After(org.junit.After) Map(java.util.Map) ThreadPool(org.elasticsearch.threadpool.ThreadPool) ClusterName(org.elasticsearch.cluster.ClusterName) Role(org.elasticsearch.cluster.node.DiscoveryNode.Role) ThreadFactory(java.util.concurrent.ThreadFactory) EnumSet(java.util.EnumSet) Transport(org.elasticsearch.transport.Transport) Collection(java.util.Collection) MockTcpTransport(org.elasticsearch.transport.MockTcpTransport) Set(java.util.Set) InetSocketAddress(java.net.InetSocketAddress) Collectors(java.util.stream.Collectors) CountDownLatch(java.util.concurrent.CountDownLatch) AbstractRunnable(org.elasticsearch.common.util.concurrent.AbstractRunnable) List(java.util.List) Logger(org.apache.logging.log4j.Logger) Version(org.elasticsearch.Version) TransportAddress(org.elasticsearch.common.transport.TransportAddress) TransportConnectionListener(org.elasticsearch.transport.TransportConnectionListener) TransportSettings(org.elasticsearch.transport.TransportSettings) Matchers.equalTo(org.hamcrest.Matchers.equalTo) TransportResponseHandler(org.elasticsearch.transport.TransportResponseHandler) TransportRequestOptions(org.elasticsearch.transport.TransportRequestOptions) TransportException(org.elasticsearch.transport.TransportException) NetworkAddress(org.elasticsearch.common.network.NetworkAddress) Mockito.mock(org.mockito.Mockito.mock) IntStream(java.util.stream.IntStream) Matchers(org.mockito.Matchers) HashMap(java.util.HashMap) BoundTransportAddress(org.elasticsearch.common.transport.BoundTransportAddress) AtomicReference(java.util.concurrent.atomic.AtomicReference) CheckedBiConsumer(org.elasticsearch.common.CheckedBiConsumer) Stack(java.util.Stack) ArrayList(java.util.ArrayList) ConcurrentMap(java.util.concurrent.ConcurrentMap) HashSet(java.util.HashSet) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) NetworkService(org.elasticsearch.common.network.NetworkService) NoneCircuitBreakerService(org.elasticsearch.indices.breaker.NoneCircuitBreakerService) NamedWriteableRegistry(org.elasticsearch.common.io.stream.NamedWriteableRegistry) TimeValue(org.elasticsearch.common.unit.TimeValue) Matchers.hasSize(org.hamcrest.Matchers.hasSize) ESTestCase(org.elasticsearch.test.ESTestCase) MockTransportService(org.elasticsearch.test.transport.MockTransportService) TransportService(org.elasticsearch.transport.TransportService) ExecutorService(java.util.concurrent.ExecutorService) Before(org.junit.Before) ConnectionProfile(org.elasticsearch.transport.ConnectionProfile) Collections.emptyMap(java.util.Collections.emptyMap) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes) TestThreadPool(org.elasticsearch.threadpool.TestThreadPool) Matchers.empty(org.hamcrest.Matchers.empty) EsExecutors(org.elasticsearch.common.util.concurrent.EsExecutors) Collections.emptySet(java.util.Collections.emptySet) IOUtils(org.apache.lucene.util.IOUtils) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) VersionUtils(org.elasticsearch.test.VersionUtils) Mockito.verify(org.mockito.Mockito.verify) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Closeable(java.io.Closeable) Collections(java.util.Collections) NamedWriteableRegistry(org.elasticsearch.common.io.stream.NamedWriteableRegistry) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) InetSocketAddress(java.net.InetSocketAddress) MockTcpTransport(org.elasticsearch.transport.MockTcpTransport) TransportConnectionListener(org.elasticsearch.transport.TransportConnectionListener) Version(org.elasticsearch.Version) ClusterName(org.elasticsearch.cluster.ClusterName) Settings(org.elasticsearch.common.settings.Settings) TransportSettings(org.elasticsearch.transport.TransportSettings) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes) ClusterState(org.elasticsearch.cluster.ClusterState) Role(org.elasticsearch.cluster.node.DiscoveryNode.Role) NetworkService(org.elasticsearch.common.network.NetworkService) Transport(org.elasticsearch.transport.Transport) MockTcpTransport(org.elasticsearch.transport.MockTcpTransport) NoneCircuitBreakerService(org.elasticsearch.indices.breaker.NoneCircuitBreakerService)

Example 9 with ClusterName

use of org.elasticsearch.cluster.ClusterName in project elasticsearch by elastic.

the class NodeRemovalClusterStateTaskExecutorTests method testRerouteAfterRemovingNodes.

public void testRerouteAfterRemovingNodes() throws Exception {
    final ElectMasterService electMasterService = mock(ElectMasterService.class);
    when(electMasterService.hasEnoughMasterNodes(any(Iterable.class))).thenReturn(true);
    final AllocationService allocationService = mock(AllocationService.class);
    when(allocationService.deassociateDeadNodes(any(ClusterState.class), eq(true), any(String.class))).thenAnswer(im -> im.getArguments()[0]);
    final Consumer<String> submitRejoin = source -> fail("rejoin should not be invoked");
    final AtomicReference<ClusterState> remainingNodesClusterState = new AtomicReference<>();
    final ZenDiscovery.NodeRemovalClusterStateTaskExecutor executor = new ZenDiscovery.NodeRemovalClusterStateTaskExecutor(allocationService, electMasterService, submitRejoin, logger) {

        @Override
        ClusterState remainingNodesClusterState(ClusterState currentState, DiscoveryNodes.Builder remainingNodesBuilder) {
            remainingNodesClusterState.set(super.remainingNodesClusterState(currentState, remainingNodesBuilder));
            return remainingNodesClusterState.get();
        }
    };
    final DiscoveryNodes.Builder builder = DiscoveryNodes.builder();
    final int nodes = randomIntBetween(2, 16);
    final List<ZenDiscovery.NodeRemovalClusterStateTaskExecutor.Task> tasks = new ArrayList<>();
    // to ensure that there is at least one removal
    boolean first = true;
    for (int i = 0; i < nodes; i++) {
        final DiscoveryNode node = node(i);
        builder.add(node);
        if (first || randomBoolean()) {
            tasks.add(new ZenDiscovery.NodeRemovalClusterStateTaskExecutor.Task(node, randomBoolean() ? "left" : "failed"));
        }
        first = false;
    }
    final ClusterState clusterState = ClusterState.builder(new ClusterName("test")).nodes(builder).build();
    final ClusterStateTaskExecutor.ClusterTasksResult<ZenDiscovery.NodeRemovalClusterStateTaskExecutor.Task> result = executor.execute(clusterState, tasks);
    verify(electMasterService).hasEnoughMasterNodes(eq(remainingNodesClusterState.get().nodes()));
    verifyNoMoreInteractions(electMasterService);
    verify(allocationService).deassociateDeadNodes(eq(remainingNodesClusterState.get()), eq(true), any(String.class));
    for (final ZenDiscovery.NodeRemovalClusterStateTaskExecutor.Task task : tasks) {
        assertNull(result.resultingState.nodes().get(task.node().getId()));
    }
}
Also used : DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) AllocationService(org.elasticsearch.cluster.routing.allocation.AllocationService) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ClusterStateTaskExecutor(org.elasticsearch.cluster.ClusterStateTaskExecutor) Mockito.when(org.mockito.Mockito.when) AtomicReference(java.util.concurrent.atomic.AtomicReference) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) Mockito.verify(org.mockito.Mockito.verify) Consumer(java.util.function.Consumer) Matchers.any(org.mockito.Matchers.any) ClusterState(org.elasticsearch.cluster.ClusterState) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) List(java.util.List) Version(org.elasticsearch.Version) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) Matchers.eq(org.mockito.Matchers.eq) StreamSupport(java.util.stream.StreamSupport) ClusterName(org.elasticsearch.cluster.ClusterName) ESTestCase(org.elasticsearch.test.ESTestCase) Mockito.mock(org.mockito.Mockito.mock) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) ArrayList(java.util.ArrayList) ClusterName(org.elasticsearch.cluster.ClusterName) AllocationService(org.elasticsearch.cluster.routing.allocation.AllocationService) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes) ClusterState(org.elasticsearch.cluster.ClusterState) AtomicReference(java.util.concurrent.atomic.AtomicReference) ClusterStateTaskExecutor(org.elasticsearch.cluster.ClusterStateTaskExecutor)

Example 10 with ClusterName

use of org.elasticsearch.cluster.ClusterName in project elasticsearch by elastic.

the class PublishClusterStateActionTests method testSimpleClusterStatePublishing.

public void testSimpleClusterStatePublishing() throws Exception {
    MockNode nodeA = createMockNode("nodeA").setAsMaster();
    MockNode nodeB = createMockNode("nodeB");
    // Initial cluster state
    ClusterState clusterState = nodeA.clusterState;
    // cluster state update - add nodeB
    DiscoveryNodes discoveryNodes = DiscoveryNodes.builder(clusterState.nodes()).add(nodeB.discoveryNode).build();
    ClusterState previousClusterState = clusterState;
    clusterState = ClusterState.builder(clusterState).nodes(discoveryNodes).incrementVersion().build();
    publishStateAndWait(nodeA.action, clusterState, previousClusterState);
    assertSameStateFromFull(nodeB.clusterState, clusterState);
    // cluster state update - add block
    previousClusterState = clusterState;
    clusterState = ClusterState.builder(clusterState).blocks(ClusterBlocks.builder().addGlobalBlock(MetaData.CLUSTER_READ_ONLY_BLOCK)).incrementVersion().build();
    publishStateAndWait(nodeA.action, clusterState, previousClusterState);
    assertSameStateFromDiff(nodeB.clusterState, clusterState);
    assertThat(nodeB.clusterState.blocks().global().size(), equalTo(1));
    // cluster state update - remove block
    previousClusterState = clusterState;
    clusterState = ClusterState.builder(clusterState).blocks(ClusterBlocks.EMPTY_CLUSTER_BLOCK).incrementVersion().build();
    publishStateAndWait(nodeA.action, clusterState, previousClusterState);
    assertSameStateFromDiff(nodeB.clusterState, clusterState);
    assertTrue(nodeB.clusterState.wasReadFromDiff());
    // Adding new node - this node should get full cluster state while nodeB should still be getting diffs
    MockNode nodeC = createMockNode("nodeC");
    // cluster state update 3 - register node C
    previousClusterState = clusterState;
    discoveryNodes = DiscoveryNodes.builder(discoveryNodes).add(nodeC.discoveryNode).build();
    clusterState = ClusterState.builder(clusterState).nodes(discoveryNodes).incrementVersion().build();
    publishStateAndWait(nodeA.action, clusterState, previousClusterState);
    assertSameStateFromDiff(nodeB.clusterState, clusterState);
    // First state
    assertSameStateFromFull(nodeC.clusterState, clusterState);
    // cluster state update 4 - update settings
    previousClusterState = clusterState;
    MetaData metaData = MetaData.builder(clusterState.metaData()).transientSettings(Settings.builder().put("foo", "bar").build()).build();
    clusterState = ClusterState.builder(clusterState).metaData(metaData).incrementVersion().build();
    publishStateAndWait(nodeA.action, clusterState, previousClusterState);
    assertSameStateFromDiff(nodeB.clusterState, clusterState);
    assertThat(nodeB.clusterState.blocks().global().size(), equalTo(0));
    assertSameStateFromDiff(nodeC.clusterState, clusterState);
    assertThat(nodeC.clusterState.blocks().global().size(), equalTo(0));
    // cluster state update - skipping one version change - should request full cluster state
    previousClusterState = ClusterState.builder(clusterState).incrementVersion().build();
    clusterState = ClusterState.builder(clusterState).incrementVersion().build();
    publishStateAndWait(nodeA.action, clusterState, previousClusterState);
    assertSameStateFromFull(nodeB.clusterState, clusterState);
    assertSameStateFromFull(nodeC.clusterState, clusterState);
    assertFalse(nodeC.clusterState.wasReadFromDiff());
    // node A steps down from being master
    nodeA.resetMasterId();
    nodeB.resetMasterId();
    nodeC.resetMasterId();
    // node B becomes the master and sends a version of the cluster state that goes back
    discoveryNodes = DiscoveryNodes.builder(discoveryNodes).add(nodeA.discoveryNode).add(nodeB.discoveryNode).add(nodeC.discoveryNode).masterNodeId(nodeB.discoveryNode.getId()).localNodeId(nodeB.discoveryNode.getId()).build();
    previousClusterState = ClusterState.builder(new ClusterName("test")).nodes(discoveryNodes).build();
    clusterState = ClusterState.builder(clusterState).nodes(discoveryNodes).incrementVersion().build();
    publishStateAndWait(nodeB.action, clusterState, previousClusterState);
    assertSameStateFromFull(nodeA.clusterState, clusterState);
    assertSameStateFromFull(nodeC.clusterState, clusterState);
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) MetaData(org.elasticsearch.cluster.metadata.MetaData) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) ClusterName(org.elasticsearch.cluster.ClusterName) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes)

Aggregations

ClusterName (org.elasticsearch.cluster.ClusterName)95 ClusterState (org.elasticsearch.cluster.ClusterState)76 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)22 DiscoveryNodes (org.elasticsearch.cluster.node.DiscoveryNodes)20 Settings (org.elasticsearch.common.settings.Settings)20 Matchers.containsString (org.hamcrest.Matchers.containsString)17 Version (org.elasticsearch.Version)14 AtomicReference (java.util.concurrent.atomic.AtomicReference)12 IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)12 MetaData (org.elasticsearch.cluster.metadata.MetaData)12 ClusterSettings (org.elasticsearch.common.settings.ClusterSettings)12 ArrayList (java.util.ArrayList)11 RoutingTable (org.elasticsearch.cluster.routing.RoutingTable)10 List (java.util.List)9 IndexShardRoutingTable (org.elasticsearch.cluster.routing.IndexShardRoutingTable)9 HashMap (java.util.HashMap)8 HashSet (java.util.HashSet)8 Collectors (java.util.stream.Collectors)8 BytesArray (org.elasticsearch.common.bytes.BytesArray)8 ESTestCase (org.elasticsearch.test.ESTestCase)8