Search in sources :

Example 81 with DiscoveryNodes

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

the class UnicastZenPingTests method testUnknownHostNotCached.

public void testUnknownHostNotCached() throws ExecutionException, InterruptedException {
    // use ephemeral ports
    final Settings settings = Settings.builder().put("cluster.name", "test").put(TransportSettings.PORT.getKey(), 0).build();
    final NetworkService networkService = new NetworkService(settings, Collections.emptyList());
    final Map<String, TransportAddress[]> addresses = new HashMap<>();
    final BiFunction<Settings, Version, Transport> supplier = (s, v) -> new MockTcpTransport(s, threadPool, BigArrays.NON_RECYCLING_INSTANCE, new NoneCircuitBreakerService(), new NamedWriteableRegistry(Collections.emptyList()), networkService, v) {

        @Override
        public TransportAddress[] addressesFromString(String address, int perAddressLimit) throws UnknownHostException {
            final TransportAddress[] transportAddresses = addresses.get(address);
            if (transportAddresses == null) {
                throw new UnknownHostException(address);
            } else {
                return transportAddresses;
            }
        }
    };
    final NetworkHandle handleA = startServices(settings, threadPool, "UZP_A", Version.CURRENT, supplier);
    closeables.push(handleA.transportService);
    final NetworkHandle handleB = startServices(settings, threadPool, "UZP_B", Version.CURRENT, supplier);
    closeables.push(handleB.transportService);
    final NetworkHandle handleC = startServices(settings, threadPool, "UZP_C", Version.CURRENT, supplier);
    closeables.push(handleC.transportService);
    addresses.put("UZP_A", new TransportAddress[] { new TransportAddress(new InetSocketAddress(handleA.address.address().getAddress(), handleA.address.address().getPort())) });
    addresses.put("UZP_C", new TransportAddress[] { new TransportAddress(new InetSocketAddress(handleC.address.address().getAddress(), handleC.address.address().getPort())) });
    final Settings hostsSettings = Settings.builder().putArray("discovery.zen.ping.unicast.hosts", "UZP_A", "UZP_B", "UZP_C").put("cluster.name", "test").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).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);
    TestUnicastZenPing zenPingC = new TestUnicastZenPing(hostsSettings, threadPool, handleC, EMPTY_HOSTS_PROVIDER);
    zenPingC.start(new PingContextProvider() {

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

        @Override
        public ClusterState clusterState() {
            return state;
        }
    });
    closeables.push(zenPingC);
    // the presence of an unresolvable host should not prevent resolvable hosts from being pinged
    {
        final Collection<ZenPing.PingResponse> pingResponses = zenPingA.pingAndWait().toList();
        assertThat(pingResponses.size(), equalTo(1));
        ZenPing.PingResponse ping = pingResponses.iterator().next();
        assertThat(ping.node().getId(), equalTo("UZP_C"));
        assertThat(ping.getClusterStateVersion(), equalTo(state.version()));
        assertPingCount(handleA, handleB, 0);
        assertPingCount(handleA, handleC, 3);
        assertNull(handleA.counters.get(handleB.address));
    }
    final HashMap<TransportAddress, Integer> moreThan = new HashMap<>();
    // we should see at least one ping to UZP_B, and one more ping than we have already seen to UZP_C
    moreThan.put(handleB.address, 0);
    moreThan.put(handleC.address, handleA.counters.get(handleC.address).intValue());
    // now allow UZP_B to be resolvable
    addresses.put("UZP_B", new TransportAddress[] { new TransportAddress(new InetSocketAddress(handleB.address.address().getAddress(), handleB.address.address().getPort())) });
    // now we should see pings to UZP_B; this establishes that host resolutions are not cached
    {
        handleA.counters.clear();
        final Collection<ZenPing.PingResponse> secondPingResponses = zenPingA.pingAndWait().toList();
        assertThat(secondPingResponses.size(), equalTo(2));
        final Set<String> ids = new HashSet<>(secondPingResponses.stream().map(p -> p.node().getId()).collect(Collectors.toList()));
        assertThat(ids, equalTo(new HashSet<>(Arrays.asList("UZP_B", "UZP_C"))));
        assertPingCount(handleA, handleB, 3);
        assertPingCount(handleA, handleC, 3);
    }
}
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) EnumSet(java.util.EnumSet) Set(java.util.Set) HashSet(java.util.HashSet) Collections.emptySet(java.util.Collections.emptySet) HashMap(java.util.HashMap) TransportAddress(org.elasticsearch.common.transport.TransportAddress) BoundTransportAddress(org.elasticsearch.common.transport.BoundTransportAddress) InetSocketAddress(java.net.InetSocketAddress) MockTcpTransport(org.elasticsearch.transport.MockTcpTransport) 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) UnknownHostException(java.net.UnknownHostException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) NetworkService(org.elasticsearch.common.network.NetworkService) Collection(java.util.Collection) Transport(org.elasticsearch.transport.Transport) MockTcpTransport(org.elasticsearch.transport.MockTcpTransport) NoneCircuitBreakerService(org.elasticsearch.indices.breaker.NoneCircuitBreakerService)

Example 82 with DiscoveryNodes

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

the class PublishClusterStateActionTests method testSimultaneousClusterStatePublishing.

/**
     * Test not waiting on publishing works correctly (i.e., publishing times out)
     */
public void testSimultaneousClusterStatePublishing() throws Exception {
    int numberOfNodes = randomIntBetween(2, 10);
    int numberOfIterations = scaledRandomIntBetween(5, 50);
    Settings settings = Settings.builder().put(DiscoverySettings.PUBLISH_DIFF_ENABLE_SETTING.getKey(), randomBoolean()).build();
    MockNode master = createMockNode("node0", settings, new ClusterStateListener() {

        @Override
        public void clusterChanged(ClusterChangedEvent event) {
            assertProperMetaDataForVersion(event.state().metaData(), event.state().version());
        }
    }).setAsMaster();
    DiscoveryNodes.Builder discoveryNodesBuilder = DiscoveryNodes.builder(master.nodes());
    for (int i = 1; i < numberOfNodes; i++) {
        final String name = "node" + i;
        final MockNode node = createMockNode(name, settings, new ClusterStateListener() {

            @Override
            public void clusterChanged(ClusterChangedEvent event) {
                assertProperMetaDataForVersion(event.state().metaData(), event.state().version());
            }
        });
        discoveryNodesBuilder.add(node.discoveryNode);
    }
    AssertingAckListener[] listeners = new AssertingAckListener[numberOfIterations];
    DiscoveryNodes discoveryNodes = discoveryNodesBuilder.build();
    MetaData metaData = MetaData.EMPTY_META_DATA;
    ClusterState clusterState = ClusterState.builder(CLUSTER_NAME).metaData(metaData).build();
    ClusterState previousState;
    for (int i = 0; i < numberOfIterations; i++) {
        previousState = clusterState;
        metaData = buildMetaDataForVersion(metaData, i + 1);
        clusterState = ClusterState.builder(clusterState).incrementVersion().metaData(metaData).nodes(discoveryNodes).build();
        listeners[i] = publishState(master.action, clusterState, previousState);
    }
    for (int i = 0; i < numberOfIterations; i++) {
        listeners[i].await(1, TimeUnit.SECONDS);
    }
    // set the master cs
    master.clusterState = clusterState;
    for (MockNode node : nodes.values()) {
        assertSameState(node.clusterState, clusterState);
        assertThat(node.clusterState.nodes().getLocalNode(), equalTo(node.discoveryNode));
    }
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) ClusterChangedEvent(org.elasticsearch.cluster.ClusterChangedEvent) Matchers.hasToString(org.hamcrest.Matchers.hasToString) Matchers.containsString(org.hamcrest.Matchers.containsString) ClusterStateListener(org.elasticsearch.cluster.ClusterStateListener) MetaData(org.elasticsearch.cluster.metadata.MetaData) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) Settings(org.elasticsearch.common.settings.Settings) DiscoverySettings(org.elasticsearch.discovery.DiscoverySettings) ClusterSettings(org.elasticsearch.common.settings.ClusterSettings) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes)

Example 83 with DiscoveryNodes

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

the class AsyncShardFetchTests method testTwoNodesAddedInBetween.

public void testTwoNodesAddedInBetween() throws Exception {
    DiscoveryNodes nodes = DiscoveryNodes.builder().add(node1).build();
    test.addSimulation(node1.getId(), response1);
    // no fetched data, 2 requests still on going
    AsyncShardFetch.FetchResult<Response> fetchData = test.fetchData(nodes, emptySet());
    assertThat(fetchData.hasData(), equalTo(false));
    assertThat(test.reroute.get(), equalTo(0));
    // fire the first response, it should trigger a reroute
    test.fireSimulationAndWait(node1.getId());
    // now, add a second node to the nodes, it should add it to the ongoing requests
    nodes = DiscoveryNodes.builder(nodes).add(node2).build();
    test.addSimulation(node2.getId(), response2);
    // no fetch data, has a new node introduced
    fetchData = test.fetchData(nodes, emptySet());
    assertThat(fetchData.hasData(), equalTo(false));
    // fire the second simulation, this should allow us to get the data
    test.fireSimulationAndWait(node2.getId());
    // since one of those failed, we should only have one entry
    fetchData = test.fetchData(nodes, emptySet());
    assertThat(fetchData.hasData(), equalTo(true));
    assertThat(fetchData.getData().size(), equalTo(2));
    assertThat(fetchData.getData().get(node1), sameInstance(response1));
    assertThat(fetchData.getData().get(node2), sameInstance(response2));
}
Also used : BaseNodeResponse(org.elasticsearch.action.support.nodes.BaseNodeResponse) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes)

Example 84 with DiscoveryNodes

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

the class AsyncShardFetchTests method testFullCircleSingleNodeFailure.

public void testFullCircleSingleNodeFailure() throws Exception {
    DiscoveryNodes nodes = DiscoveryNodes.builder().add(node1).build();
    // add a failed response for node1
    test.addSimulation(node1.getId(), failure1);
    // first fetch, no data, still on going
    AsyncShardFetch.FetchResult<Response> fetchData = test.fetchData(nodes, emptySet());
    assertThat(fetchData.hasData(), equalTo(false));
    assertThat(test.reroute.get(), equalTo(0));
    // fire a response, wait on reroute incrementing
    test.fireSimulationAndWait(node1.getId());
    // failure, fetched data exists, but has no data
    assertThat(test.reroute.get(), equalTo(1));
    fetchData = test.fetchData(nodes, emptySet());
    assertThat(fetchData.hasData(), equalTo(true));
    assertThat(fetchData.getData().size(), equalTo(0));
    // on failure, we reset the failure on a successive call to fetchData, and try again afterwards
    test.addSimulation(node1.getId(), response1);
    fetchData = test.fetchData(nodes, emptySet());
    assertThat(fetchData.hasData(), equalTo(false));
    test.fireSimulationAndWait(node1.getId());
    // 2 reroutes, cause we have a failure that we clear
    assertThat(test.reroute.get(), equalTo(3));
    fetchData = test.fetchData(nodes, emptySet());
    assertThat(fetchData.hasData(), equalTo(true));
    assertThat(fetchData.getData().size(), equalTo(1));
    assertThat(fetchData.getData().get(node1), sameInstance(response1));
}
Also used : BaseNodeResponse(org.elasticsearch.action.support.nodes.BaseNodeResponse) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes)

Example 85 with DiscoveryNodes

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

the class AsyncShardFetchTests method testTwoNodesOnSetup.

public void testTwoNodesOnSetup() throws Exception {
    DiscoveryNodes nodes = DiscoveryNodes.builder().add(node1).add(node2).build();
    test.addSimulation(node1.getId(), response1);
    test.addSimulation(node2.getId(), response2);
    // no fetched data, 2 requests still on going
    AsyncShardFetch.FetchResult<Response> fetchData = test.fetchData(nodes, emptySet());
    assertThat(fetchData.hasData(), equalTo(false));
    assertThat(test.reroute.get(), equalTo(0));
    // fire the first response, it should trigger a reroute
    test.fireSimulationAndWait(node1.getId());
    // there is still another on going request, so no data
    assertThat(test.getNumberOfInFlightFetches(), equalTo(1));
    fetchData = test.fetchData(nodes, emptySet());
    assertThat(fetchData.hasData(), equalTo(false));
    // fire the second simulation, this should allow us to get the data
    test.fireSimulationAndWait(node2.getId());
    // no more ongoing requests, we should fetch the data
    assertThat(test.reroute.get(), equalTo(2));
    fetchData = test.fetchData(nodes, emptySet());
    assertThat(fetchData.hasData(), equalTo(true));
    assertThat(fetchData.getData().size(), equalTo(2));
    assertThat(fetchData.getData().get(node1), sameInstance(response1));
    assertThat(fetchData.getData().get(node2), sameInstance(response2));
}
Also used : BaseNodeResponse(org.elasticsearch.action.support.nodes.BaseNodeResponse) 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