Search in sources :

Example 51 with DiscoveryNodes

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

the class PublicationTests method testClusterStatePublishingWithFaultyNodeAfterCommit.

public void testClusterStatePublishingWithFaultyNodeAfterCommit() throws InterruptedException {
    VotingConfiguration singleNodeConfig = new VotingConfiguration(Set.of(n1.getId()));
    initializeCluster(singleNodeConfig);
    AssertingAckListener ackListener = new AssertingAckListener(nodes.size());
    DiscoveryNodes discoveryNodes = DiscoveryNodes.builder().add(n1).add(n2).add(n3).localNodeId(n1.getId()).build();
    boolean publicationDidNotMakeItToNode2 = randomBoolean();
    AtomicInteger remainingActions = new AtomicInteger(publicationDidNotMakeItToNode2 ? 2 : 3);
    int injectFaultAt = randomInt(remainingActions.get() - 1);
    logger.info("Injecting fault at: {}, publicationDidNotMakeItToNode2: {}", injectFaultAt, publicationDidNotMakeItToNode2);
    MockPublication publication = node1.publish(CoordinationStateTests.clusterState(1L, 2L, discoveryNodes, singleNodeConfig, singleNodeConfig, 42L), ackListener, Collections.emptySet());
    publication.pendingPublications.entrySet().stream().collect(shuffle()).forEach(e -> {
        if (e.getKey().equals(n2) == false || publicationDidNotMakeItToNode2 == false) {
            PublishResponse publishResponse = nodeResolver.apply(e.getKey()).coordinationState.handlePublishRequest(publication.publishRequest);
            e.getValue().onResponse(new PublishWithJoinResponse(publishResponse, Optional.empty()));
        }
    });
    publication.pendingCommits.entrySet().stream().collect(shuffle()).forEach(e -> {
        if (e.getKey().equals(n2)) {
            // we must fail node before committing for the node, otherwise failing the node is ignored
            publication.onFaultyNode(n2);
        }
        if (remainingActions.decrementAndGet() == injectFaultAt) {
            publication.onFaultyNode(n2);
        }
        if (e.getKey().equals(n2) == false || randomBoolean()) {
            nodeResolver.apply(e.getKey()).coordinationState.handleCommit(publication.applyCommit);
            e.getValue().onResponse(TransportResponse.Empty.INSTANCE);
        }
    });
    // we need to complete publication by failing the node
    if (publicationDidNotMakeItToNode2 && remainingActions.get() > injectFaultAt) {
        publication.onFaultyNode(n2);
    }
    assertTrue(publication.completed);
    assertTrue(publication.committed);
    // has no influence
    publication.onFaultyNode(randomFrom(n1, n3));
    List<Tuple<DiscoveryNode, Throwable>> errors = ackListener.awaitErrors(0L, TimeUnit.SECONDS);
    assertThat(errors.size(), equalTo(1));
    assertThat(errors.get(0).v1(), equalTo(n2));
    assertThat(errors.get(0).v2().getMessage(), containsString("faulty node"));
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes) Tuple(io.crate.common.collections.Tuple) VotingConfiguration(org.elasticsearch.cluster.coordination.CoordinationMetadata.VotingConfiguration)

Example 52 with DiscoveryNodes

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

the class PublicationTests method testClusterStatePublishingTimesOutAfterCommit.

public void testClusterStatePublishingTimesOutAfterCommit() throws InterruptedException {
    VotingConfiguration config = new VotingConfiguration(randomBoolean() ? Set.of(n1.getId(), n2.getId()) : Set.of(n1.getId(), n2.getId(), n3.getId()));
    initializeCluster(config);
    AssertingAckListener ackListener = new AssertingAckListener(nodes.size());
    DiscoveryNodes discoveryNodes = DiscoveryNodes.builder().add(n1).add(n2).add(n3).localNodeId(n1.getId()).build();
    MockPublication publication = node1.publish(CoordinationStateTests.clusterState(1L, 2L, discoveryNodes, config, config, 42L), ackListener, Collections.emptySet());
    boolean publishedToN3 = randomBoolean();
    publication.pendingPublications.entrySet().stream().collect(shuffle()).forEach(e -> {
        if (e.getKey().equals(n3) == false || publishedToN3) {
            PublishResponse publishResponse = nodeResolver.apply(e.getKey()).coordinationState.handlePublishRequest(publication.publishRequest);
            e.getValue().onResponse(new PublishWithJoinResponse(publishResponse, Optional.empty()));
        }
    });
    assertNotNull(publication.applyCommit);
    Set<DiscoveryNode> committingNodes = new HashSet<>(randomSubsetOf(discoNodes));
    if (publishedToN3 == false) {
        committingNodes.remove(n3);
    }
    logger.info("Committing nodes: {}", committingNodes);
    publication.pendingCommits.entrySet().stream().collect(shuffle()).forEach(e -> {
        if (committingNodes.contains(e.getKey())) {
            nodeResolver.apply(e.getKey()).coordinationState.handleCommit(publication.applyCommit);
            e.getValue().onResponse(TransportResponse.Empty.INSTANCE);
        }
    });
    publication.cancel("timed out");
    assertTrue(publication.completed);
    assertTrue(publication.committed);
    assertEquals(committingNodes, ackListener.await(0L, TimeUnit.SECONDS));
    // check that acking still works after publication completed
    if (publishedToN3 == false) {
        publication.pendingPublications.get(n3).onResponse(new PublishWithJoinResponse(node3.coordinationState.handlePublishRequest(publication.publishRequest), Optional.empty()));
    }
    assertEquals(discoNodes, publication.pendingCommits.keySet());
    Set<DiscoveryNode> nonCommittedNodes = Sets.difference(discoNodes, committingNodes);
    logger.info("Non-committed nodes: {}", nonCommittedNodes);
    nonCommittedNodes.stream().collect(shuffle()).forEach(n -> publication.pendingCommits.get(n).onResponse(TransportResponse.Empty.INSTANCE));
    assertEquals(discoNodes, ackListener.await(0L, TimeUnit.SECONDS));
}
Also used : DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes) VotingConfiguration(org.elasticsearch.cluster.coordination.CoordinationMetadata.VotingConfiguration) HashSet(java.util.HashSet)

Example 53 with DiscoveryNodes

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

the class IndicesClusterStateService method createOrUpdateShards.

private void createOrUpdateShards(final ClusterState state) {
    RoutingNode localRoutingNode = state.getRoutingNodes().node(state.nodes().getLocalNodeId());
    if (localRoutingNode == null) {
        return;
    }
    DiscoveryNodes nodes = state.nodes();
    RoutingTable routingTable = state.routingTable();
    for (final ShardRouting shardRouting : localRoutingNode) {
        ShardId shardId = shardRouting.shardId();
        if (failedShardsCache.containsKey(shardId) == false) {
            AllocatedIndex<? extends Shard> indexService = indicesService.indexService(shardId.getIndex());
            assert indexService != null : "index " + shardId.getIndex() + " should have been created by createIndices";
            Shard shard = indexService.getShardOrNull(shardId.id());
            if (shard == null) {
                assert shardRouting.initializing() : shardRouting + " should have been removed by failMissingShards";
                createShard(nodes, routingTable, shardRouting, state);
            } else {
                updateShard(nodes, shardRouting, shard, routingTable, state);
            }
        }
    }
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) RoutingNode(org.elasticsearch.cluster.routing.RoutingNode) IndexShardRoutingTable(org.elasticsearch.cluster.routing.IndexShardRoutingTable) RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) IndexShard(org.elasticsearch.index.shard.IndexShard) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes)

Example 54 with DiscoveryNodes

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

the class InternalTestCluster method validateClusterFormed.

/**
 * ensure a cluster is formed with all published nodes.
 */
public synchronized void validateClusterFormed() {
    final Set<DiscoveryNode> expectedNodes = new HashSet<>();
    for (NodeAndClient nodeAndClient : nodes.values()) {
        expectedNodes.add(getInstanceFromNode(ClusterService.class, nodeAndClient.node()).localNode());
    }
    logger.trace("validating cluster formed, expecting {}", expectedNodes);
    try {
        assertBusy(() -> {
            final List<ClusterState> states = nodes.values().stream().map(node -> getInstanceFromNode(ClusterService.class, node.node())).map(ClusterService::state).collect(Collectors.toList());
            final String debugString = ", expected nodes: " + expectedNodes + " and actual cluster states " + states;
            // all nodes have a master
            assertTrue("Missing master" + debugString, states.stream().allMatch(cs -> cs.nodes().getMasterNodeId() != null));
            // all nodes have the same master (in same term)
            assertEquals("Not all masters in same term" + debugString, 1, states.stream().mapToLong(ClusterState::term).distinct().count());
            // all nodes know about all other nodes
            states.forEach(cs -> {
                DiscoveryNodes discoveryNodes = cs.nodes();
                assertEquals("Node size mismatch" + debugString, expectedNodes.size(), discoveryNodes.getSize());
                for (DiscoveryNode expectedNode : expectedNodes) {
                    assertTrue("Expected node to exist: " + expectedNode + debugString, discoveryNodes.nodeExists(expectedNode));
                }
            });
        }, 30, TimeUnit.SECONDS);
    } catch (AssertionError ae) {
        throw new IllegalStateException("cluster failed to form", ae);
    } catch (Exception e) {
        throw new IllegalStateException(e);
    }
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) Arrays(java.util.Arrays) ByteSizeUnit(org.elasticsearch.common.unit.ByteSizeUnit) Releasables(org.elasticsearch.common.lease.Releasables) IndexMetadata(org.elasticsearch.cluster.metadata.IndexMetadata) Matchers.not(org.hamcrest.Matchers.not) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) ClusterBootstrapService(org.elasticsearch.cluster.coordination.ClusterBootstrapService) TOTAL_CIRCUIT_BREAKER_LIMIT_SETTING(org.elasticsearch.indices.breaker.HierarchyCircuitBreakerService.TOTAL_CIRCUIT_BREAKER_LIMIT_SETTING) NodeValidationException(org.elasticsearch.node.NodeValidationException) SeqNoStats(org.elasticsearch.index.seqno.SeqNoStats) RecoverySettings(org.elasticsearch.indices.recovery.RecoverySettings) AddVotingConfigExclusionsRequest(org.elasticsearch.action.admin.cluster.configuration.AddVotingConfigExclusionsRequest) Future(java.util.concurrent.Future) ClusterState(org.elasticsearch.cluster.ClusterState) RandomNumbers(com.carrotsearch.randomizedtesting.generators.RandomNumbers) Matchers.nullValue(org.hamcrest.Matchers.nullValue) Map(java.util.Map) ObjectLongMap(com.carrotsearch.hppc.ObjectLongMap) Path(java.nio.file.Path) INITIAL_MASTER_NODES_SETTING(org.elasticsearch.cluster.coordination.ClusterBootstrapService.INITIAL_MASTER_NODES_SETTING) RandomStrings(com.carrotsearch.randomizedtesting.generators.RandomStrings) ServiceDisruptionScheme(org.elasticsearch.test.disruption.ServiceDisruptionScheme) MappingUpdatedAction(org.elasticsearch.cluster.action.index.MappingUpdatedAction) INITIAL_STATE_TIMEOUT_SETTING(org.elasticsearch.node.Node.INITIAL_STATE_TIMEOUT_SETTING) Set(java.util.Set) ESTestCase.assertBusy(org.elasticsearch.test.ESTestCase.assertBusy) PageCacheRecycler(org.elasticsearch.common.util.PageCacheRecycler) RandomizedTest(com.carrotsearch.randomizedtesting.RandomizedTest) Stream(java.util.stream.Stream) Logger(org.apache.logging.log4j.Logger) TransportSettings(org.elasticsearch.transport.TransportSettings) ZEN2_DISCOVERY_TYPE(org.elasticsearch.discovery.DiscoveryModule.ZEN2_DISCOVERY_TYPE) Matchers.greaterThan(org.hamcrest.Matchers.greaterThan) DiscoveryNodeRole(org.elasticsearch.cluster.node.DiscoveryNodeRole) IntObjectCursor(com.carrotsearch.hppc.cursors.IntObjectCursor) ThrottlingAllocationDecider(org.elasticsearch.cluster.routing.allocation.decider.ThrottlingAllocationDecider) RandomPicks(com.carrotsearch.randomizedtesting.generators.RandomPicks) DiskThresholdSettings(org.elasticsearch.cluster.routing.allocation.DiskThresholdSettings) ClusterService(org.elasticsearch.cluster.service.ClusterService) ArrayList(java.util.ArrayList) CircuitBreakerService(org.elasticsearch.indices.breaker.CircuitBreakerService) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) NamedWriteableRegistry(org.elasticsearch.common.io.stream.NamedWriteableRegistry) CircuitBreaker(org.elasticsearch.common.breaker.CircuitBreaker) IndicesService(org.elasticsearch.indices.IndicesService) TransportService(org.elasticsearch.transport.TransportService) Nullable(javax.annotation.Nullable) Matchers.greaterThanOrEqualTo(org.hamcrest.Matchers.greaterThanOrEqualTo) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes) FileSystemUtils(org.elasticsearch.common.io.FileSystemUtils) OperationRouting(org.elasticsearch.cluster.routing.OperationRouting) FutureUtils(org.elasticsearch.common.util.concurrent.FutureUtils) SequenceNumbers(org.elasticsearch.index.seqno.SequenceNumbers) Files(java.nio.file.Files) Client(org.elasticsearch.client.Client) IndexService(org.elasticsearch.index.IndexService) IndexShard(org.elasticsearch.index.shard.IndexShard) IOUtils(io.crate.common.io.IOUtils) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) SeedUtils(com.carrotsearch.randomizedtesting.SeedUtils) ExecutionException(java.util.concurrent.ExecutionException) LuceneTestCase.rarely(org.apache.lucene.util.LuceneTestCase.rarely) UNICAST_HOSTS_FILE(org.elasticsearch.discovery.FileBasedSeedHostsProvider.UNICAST_HOSTS_FILE) InternalEngine(org.elasticsearch.index.engine.InternalEngine) HierarchyCircuitBreakerService(org.elasticsearch.indices.breaker.HierarchyCircuitBreakerService) TreeMap(java.util.TreeMap) Randomness(org.elasticsearch.common.Randomness) Assert.assertEquals(org.junit.Assert.assertEquals) Builder(org.elasticsearch.common.settings.Settings.Builder) Environment(org.elasticsearch.env.Environment) Random(java.util.Random) TEST_NIGHTLY(org.apache.lucene.util.LuceneTestCase.TEST_NIGHTLY) Assert.assertThat(org.junit.Assert.assertThat) ObjectObjectCursor(com.carrotsearch.hppc.cursors.ObjectObjectCursor) ClearVotingConfigExclusionsRequest(org.elasticsearch.action.admin.cluster.configuration.ClearVotingConfigExclusionsRequest) Settings(org.elasticsearch.common.settings.Settings) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) LifecycleListener(org.elasticsearch.common.component.LifecycleListener) Assert.fail(org.junit.Assert.fail) ClusterName(org.elasticsearch.cluster.ClusterName) ByteSizeValue(org.elasticsearch.common.unit.ByteSizeValue) AddVotingConfigExclusionsAction(org.elasticsearch.action.admin.cluster.configuration.AddVotingConfigExclusionsAction) Predicate(java.util.function.Predicate) Collection(java.util.Collection) IndexShardRoutingTable(org.elasticsearch.cluster.routing.IndexShardRoutingTable) ShardLockObtainFailedException(org.elasticsearch.env.ShardLockObtainFailedException) NavigableMap(java.util.NavigableMap) InetSocketAddress(java.net.InetSocketAddress) Collectors(java.util.stream.Collectors) Engine(org.elasticsearch.index.engine.Engine) Objects(java.util.Objects) ESTestCase.randomFrom(org.elasticsearch.test.ESTestCase.randomFrom) List(java.util.List) IndexRoutingTable(org.elasticsearch.cluster.routing.IndexRoutingTable) Matchers.equalTo(org.hamcrest.Matchers.equalTo) TimeValue(io.crate.common.unit.TimeValue) IntStream(java.util.stream.IntStream) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) HttpServerTransport(org.elasticsearch.http.HttpServerTransport) CommitStats(org.elasticsearch.index.engine.CommitStats) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) ClearVotingConfigExclusionsAction(org.elasticsearch.action.admin.cluster.configuration.ClearVotingConfigExclusionsAction) Index(org.elasticsearch.index.Index) Strings(org.elasticsearch.common.Strings) HashSet(java.util.HashSet) Node(org.elasticsearch.node.Node) DocIdSeqNoAndSource(org.elasticsearch.index.engine.DocIdSeqNoAndSource) MockTransportService(org.elasticsearch.test.transport.MockTransportService) ExecutorService(java.util.concurrent.ExecutorService) TimeValue.timeValueSeconds(io.crate.common.unit.TimeValue.timeValueSeconds) MockNode(org.elasticsearch.node.MockNode) EsExecutors(org.elasticsearch.common.util.concurrent.EsExecutors) Iterator(java.util.Iterator) Plugin(org.elasticsearch.plugins.Plugin) TimeUnit(java.util.concurrent.TimeUnit) IndexShardTestCase(org.elasticsearch.index.shard.IndexShardTestCase) NodeEnvironment(org.elasticsearch.env.NodeEnvironment) DISCOVERY_TYPE_SETTING(org.elasticsearch.discovery.DiscoveryModule.DISCOVERY_TYPE_SETTING) Closeable(java.io.Closeable) Collections(java.util.Collections) LogManager(org.apache.logging.log4j.LogManager) ClusterState(org.elasticsearch.cluster.ClusterState) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) NodeValidationException(org.elasticsearch.node.NodeValidationException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) ShardLockObtainFailedException(org.elasticsearch.env.ShardLockObtainFailedException) ClusterService(org.elasticsearch.cluster.service.ClusterService) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes) HashSet(java.util.HashSet)

Example 55 with DiscoveryNodes

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

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)

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