Search in sources :

Example 1 with IndexRoutingTable

use of org.elasticsearch.cluster.routing.IndexRoutingTable in project elasticsearch by elastic.

the class NodeJoinControllerTests method testElectionBasedOnConflictingNodes.

/**
     * Tests tha node can become a master, even though the last cluster state it knows contains
     * nodes that conflict with the joins it got and needs to become a master
     */
public void testElectionBasedOnConflictingNodes() throws InterruptedException, ExecutionException {
    final DiscoveryNode masterNode = clusterService.localNode();
    final DiscoveryNode otherNode = new DiscoveryNode("other_node", buildNewFakeTransportAddress(), emptyMap(), EnumSet.allOf(DiscoveryNode.Role.class), Version.CURRENT);
    // simulate master going down with stale nodes in it's cluster state (for example when min master nodes is set to 2)
    // also add some shards to that node
    DiscoveryNodes.Builder discoBuilder = DiscoveryNodes.builder(clusterService.state().nodes());
    discoBuilder.masterNodeId(null);
    discoBuilder.add(otherNode);
    ClusterState.Builder stateBuilder = ClusterState.builder(clusterService.state()).nodes(discoBuilder);
    if (randomBoolean()) {
        IndexMetaData indexMetaData = IndexMetaData.builder("test").settings(Settings.builder().put(SETTING_VERSION_CREATED, Version.CURRENT).put(SETTING_NUMBER_OF_SHARDS, 1).put(SETTING_NUMBER_OF_REPLICAS, 1).put(SETTING_CREATION_DATE, System.currentTimeMillis())).build();
        IndexRoutingTable.Builder indexRoutingTableBuilder = IndexRoutingTable.builder(indexMetaData.getIndex());
        RoutingTable.Builder routing = new RoutingTable.Builder();
        routing.addAsNew(indexMetaData);
        final ShardId shardId = new ShardId("test", "_na_", 0);
        IndexShardRoutingTable.Builder indexShardRoutingBuilder = new IndexShardRoutingTable.Builder(shardId);
        final DiscoveryNode primaryNode = randomBoolean() ? masterNode : otherNode;
        final DiscoveryNode replicaNode = primaryNode.equals(masterNode) ? otherNode : masterNode;
        final boolean primaryStarted = randomBoolean();
        indexShardRoutingBuilder.addShard(TestShardRouting.newShardRouting("test", 0, primaryNode.getId(), null, true, primaryStarted ? ShardRoutingState.STARTED : ShardRoutingState.INITIALIZING, primaryStarted ? null : new UnassignedInfo(UnassignedInfo.Reason.INDEX_REOPENED, "getting there")));
        if (primaryStarted) {
            boolean replicaStared = randomBoolean();
            indexShardRoutingBuilder.addShard(TestShardRouting.newShardRouting("test", 0, replicaNode.getId(), null, false, replicaStared ? ShardRoutingState.STARTED : ShardRoutingState.INITIALIZING, replicaStared ? null : new UnassignedInfo(UnassignedInfo.Reason.INDEX_CREATED, "getting there")));
        } else {
            indexShardRoutingBuilder.addShard(TestShardRouting.newShardRouting("test", 0, null, null, false, ShardRoutingState.UNASSIGNED, new UnassignedInfo(UnassignedInfo.Reason.INDEX_CREATED, "life sucks")));
        }
        indexRoutingTableBuilder.addIndexShard(indexShardRoutingBuilder.build());
        IndexRoutingTable indexRoutingTable = indexRoutingTableBuilder.build();
        IndexMetaData updatedIndexMetaData = updateActiveAllocations(indexRoutingTable, indexMetaData);
        stateBuilder.metaData(MetaData.builder().put(updatedIndexMetaData, false).generateClusterUuidIfNeeded()).routingTable(RoutingTable.builder().add(indexRoutingTable).build());
    }
    setState(clusterService, stateBuilder.build());
    // conflict on node id or address
    final DiscoveryNode conflictingNode = randomBoolean() ? new DiscoveryNode(otherNode.getId(), randomBoolean() ? otherNode.getAddress() : buildNewFakeTransportAddress(), otherNode.getAttributes(), otherNode.getRoles(), Version.CURRENT) : new DiscoveryNode("conflicting_address_node", otherNode.getAddress(), otherNode.getAttributes(), otherNode.getRoles(), Version.CURRENT);
    nodeJoinController.startElectionContext();
    final SimpleFuture joinFuture = joinNodeAsync(conflictingNode);
    final CountDownLatch elected = new CountDownLatch(1);
    nodeJoinController.waitToBeElectedAsMaster(1, TimeValue.timeValueHours(5), new NodeJoinController.ElectionCallback() {

        @Override
        public void onElectedAsMaster(ClusterState state) {
            elected.countDown();
        }

        @Override
        public void onFailure(Throwable t) {
            logger.error("failed to be elected as master", t);
            throw new AssertionError("failed to be elected as master", t);
        }
    });
    elected.await();
    // throw any exception
    joinFuture.get();
    final ClusterState finalState = clusterService.state();
    final DiscoveryNodes finalNodes = finalState.nodes();
    assertTrue(finalNodes.isLocalNodeElectedMaster());
    assertThat(finalNodes.getLocalNode(), equalTo(masterNode));
    assertThat(finalNodes.getSize(), equalTo(2));
    assertThat(finalNodes.get(conflictingNode.getId()), equalTo(conflictingNode));
    List<ShardRouting> activeShardsOnRestartedNode = StreamSupport.stream(finalState.getRoutingNodes().node(conflictingNode.getId()).spliterator(), false).filter(ShardRouting::active).collect(Collectors.toList());
    assertThat(activeShardsOnRestartedNode, empty());
}
Also used : IndexRoutingTable(org.elasticsearch.cluster.routing.IndexRoutingTable) IndexShardRoutingTable(org.elasticsearch.cluster.routing.IndexShardRoutingTable) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) UnassignedInfo(org.elasticsearch.cluster.routing.UnassignedInfo) ShardId(org.elasticsearch.index.shard.ShardId) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes) ClusterState(org.elasticsearch.cluster.ClusterState) CountDownLatch(java.util.concurrent.CountDownLatch) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) IndexShardRoutingTable(org.elasticsearch.cluster.routing.IndexShardRoutingTable) IndexRoutingTable(org.elasticsearch.cluster.routing.IndexRoutingTable) RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) TestShardRouting(org.elasticsearch.cluster.routing.TestShardRouting) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting)

Example 2 with IndexRoutingTable

use of org.elasticsearch.cluster.routing.IndexRoutingTable in project elasticsearch by elastic.

the class ZenDiscoveryUnitTests method testValidateOnUnsupportedIndexVersionCreated.

public void testValidateOnUnsupportedIndexVersionCreated() throws Exception {
    final int iters = randomIntBetween(3, 10);
    for (int i = 0; i < iters; i++) {
        ClusterState.Builder stateBuilder = ClusterState.builder(ClusterName.DEFAULT);
        final DiscoveryNode otherNode = new DiscoveryNode("other_node", buildNewFakeTransportAddress(), emptyMap(), EnumSet.allOf(DiscoveryNode.Role.class), Version.CURRENT);
        MembershipAction.ValidateJoinRequestRequestHandler request = new MembershipAction.ValidateJoinRequestRequestHandler();
        final boolean incompatible = randomBoolean();
        IndexMetaData indexMetaData = IndexMetaData.builder("test").settings(Settings.builder().put(SETTING_VERSION_CREATED, incompatible ? VersionUtils.getPreviousVersion(Version.CURRENT.minimumIndexCompatibilityVersion()) : VersionUtils.randomVersionBetween(random(), Version.CURRENT.minimumIndexCompatibilityVersion(), Version.CURRENT)).put(SETTING_NUMBER_OF_SHARDS, 1).put(SETTING_NUMBER_OF_REPLICAS, 0).put(SETTING_CREATION_DATE, System.currentTimeMillis())).state(IndexMetaData.State.OPEN).build();
        IndexRoutingTable.Builder indexRoutingTableBuilder = IndexRoutingTable.builder(indexMetaData.getIndex());
        RoutingTable.Builder routing = new RoutingTable.Builder();
        routing.addAsNew(indexMetaData);
        final ShardId shardId = new ShardId("test", "_na_", 0);
        IndexShardRoutingTable.Builder indexShardRoutingBuilder = new IndexShardRoutingTable.Builder(shardId);
        final DiscoveryNode primaryNode = otherNode;
        indexShardRoutingBuilder.addShard(TestShardRouting.newShardRouting("test", 0, primaryNode.getId(), null, true, ShardRoutingState.INITIALIZING, new UnassignedInfo(UnassignedInfo.Reason.INDEX_REOPENED, "getting there")));
        indexRoutingTableBuilder.addIndexShard(indexShardRoutingBuilder.build());
        IndexRoutingTable indexRoutingTable = indexRoutingTableBuilder.build();
        IndexMetaData updatedIndexMetaData = updateActiveAllocations(indexRoutingTable, indexMetaData);
        stateBuilder.metaData(MetaData.builder().put(updatedIndexMetaData, false).generateClusterUuidIfNeeded()).routingTable(RoutingTable.builder().add(indexRoutingTable).build());
        if (incompatible) {
            IllegalStateException ex = expectThrows(IllegalStateException.class, () -> request.messageReceived(new MembershipAction.ValidateJoinRequest(stateBuilder.build()), null));
            assertEquals("index [test] version not supported: " + VersionUtils.getPreviousVersion(Version.CURRENT.minimumCompatibilityVersion()) + " minimum compatible index version is: " + Version.CURRENT.minimumCompatibilityVersion(), ex.getMessage());
        } else {
            AtomicBoolean sendResponse = new AtomicBoolean(false);
            request.messageReceived(new MembershipAction.ValidateJoinRequest(stateBuilder.build()), new TransportChannel() {

                @Override
                public String action() {
                    return null;
                }

                @Override
                public String getProfileName() {
                    return null;
                }

                @Override
                public long getRequestId() {
                    return 0;
                }

                @Override
                public String getChannelType() {
                    return null;
                }

                @Override
                public void sendResponse(TransportResponse response) throws IOException {
                    sendResponse.set(true);
                }

                @Override
                public void sendResponse(TransportResponse response, TransportResponseOptions options) throws IOException {
                }

                @Override
                public void sendResponse(Exception exception) throws IOException {
                }
            });
            assertTrue(sendResponse.get());
        }
    }
}
Also used : IndexRoutingTable(org.elasticsearch.cluster.routing.IndexRoutingTable) IndexShardRoutingTable(org.elasticsearch.cluster.routing.IndexShardRoutingTable) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) UnassignedInfo(org.elasticsearch.cluster.routing.UnassignedInfo) Matchers.containsString(org.hamcrest.Matchers.containsString) TransportResponse(org.elasticsearch.transport.TransportResponse) ShardId(org.elasticsearch.index.shard.ShardId) TransportChannel(org.elasticsearch.transport.TransportChannel) ClusterState(org.elasticsearch.cluster.ClusterState) ZenDiscovery.shouldIgnoreOrRejectNewClusterState(org.elasticsearch.discovery.zen.ZenDiscovery.shouldIgnoreOrRejectNewClusterState) IOException(java.io.IOException) IOException(java.io.IOException) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) Role(org.elasticsearch.cluster.node.DiscoveryNode.Role) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IndexShardRoutingTable(org.elasticsearch.cluster.routing.IndexShardRoutingTable) IndexRoutingTable(org.elasticsearch.cluster.routing.IndexRoutingTable) RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) TransportResponseOptions(org.elasticsearch.transport.TransportResponseOptions)

Example 3 with IndexRoutingTable

use of org.elasticsearch.cluster.routing.IndexRoutingTable in project elasticsearch by elastic.

the class ActiveShardCount method enoughShardsActive.

/**
     * Returns true iff the given cluster state's routing table contains enough active
     * shards for the given index to meet the required shard count represented by this instance.
     */
public boolean enoughShardsActive(final ClusterState clusterState, final String indexName) {
    if (this == ActiveShardCount.NONE) {
        // not waiting for any active shards
        return true;
    }
    final IndexMetaData indexMetaData = clusterState.metaData().index(indexName);
    if (indexMetaData == null) {
        // and we can stop waiting
        return true;
    }
    final IndexRoutingTable indexRoutingTable = clusterState.routingTable().index(indexName);
    assert indexRoutingTable != null;
    if (indexRoutingTable.allPrimaryShardsActive() == false) {
        // all primary shards aren't active yet
        return false;
    }
    ActiveShardCount waitForActiveShards = this;
    if (waitForActiveShards == ActiveShardCount.DEFAULT) {
        waitForActiveShards = SETTING_WAIT_FOR_ACTIVE_SHARDS.get(indexMetaData.getSettings());
    }
    for (final IntObjectCursor<IndexShardRoutingTable> shardRouting : indexRoutingTable.getShards()) {
        if (waitForActiveShards.enoughShardsActive(shardRouting.value) == false) {
            // not enough active shard copies yet
            return false;
        }
    }
    return true;
}
Also used : IndexRoutingTable(org.elasticsearch.cluster.routing.IndexRoutingTable) IndexShardRoutingTable(org.elasticsearch.cluster.routing.IndexShardRoutingTable) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData)

Example 4 with IndexRoutingTable

use of org.elasticsearch.cluster.routing.IndexRoutingTable in project elasticsearch by elastic.

the class ReplicationOperation method checkActiveShardCount.

/**
     * Checks whether we can perform a write based on the required active shard count setting.
     * Returns **null* if OK to proceed, or a string describing the reason to stop
     */
protected String checkActiveShardCount() {
    final ShardId shardId = primary.routingEntry().shardId();
    final String indexName = shardId.getIndexName();
    final ClusterState state = clusterStateSupplier.get();
    assert state != null : "replication operation must have access to the cluster state";
    final ActiveShardCount waitForActiveShards = request.waitForActiveShards();
    if (waitForActiveShards == ActiveShardCount.NONE) {
        // not waiting for any shards
        return null;
    }
    IndexRoutingTable indexRoutingTable = state.getRoutingTable().index(indexName);
    if (indexRoutingTable == null) {
        logger.trace("[{}] index not found in the routing table", shardId);
        return "Index " + indexName + " not found in the routing table";
    }
    IndexShardRoutingTable shardRoutingTable = indexRoutingTable.shard(shardId.getId());
    if (shardRoutingTable == null) {
        logger.trace("[{}] shard not found in the routing table", shardId);
        return "Shard " + shardId + " not found in the routing table";
    }
    if (waitForActiveShards.enoughShardsActive(shardRoutingTable)) {
        return null;
    } else {
        final String resolvedShards = waitForActiveShards == ActiveShardCount.ALL ? Integer.toString(shardRoutingTable.shards().size()) : waitForActiveShards.toString();
        logger.trace("[{}] not enough active copies to meet shard count of [{}] (have {}, needed {}), scheduling a retry. op [{}], " + "request [{}]", shardId, waitForActiveShards, shardRoutingTable.activeShards().size(), resolvedShards, opType, request);
        return "Not enough active copies to meet shard count of [" + waitForActiveShards + "] (have " + shardRoutingTable.activeShards().size() + ", needed " + resolvedShards + ").";
    }
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) ClusterState(org.elasticsearch.cluster.ClusterState) IndexRoutingTable(org.elasticsearch.cluster.routing.IndexRoutingTable) IndexShardRoutingTable(org.elasticsearch.cluster.routing.IndexShardRoutingTable) ActiveShardCount(org.elasticsearch.action.support.ActiveShardCount)

Example 5 with IndexRoutingTable

use of org.elasticsearch.cluster.routing.IndexRoutingTable in project elasticsearch by elastic.

the class SyncedFlushService method getShardRoutingTable.

final IndexShardRoutingTable getShardRoutingTable(ShardId shardId, ClusterState state) {
    final IndexRoutingTable indexRoutingTable = state.routingTable().index(shardId.getIndexName());
    if (indexRoutingTable == null) {
        IndexMetaData index = state.getMetaData().index(shardId.getIndex());
        if (index != null && index.getState() == IndexMetaData.State.CLOSE) {
            throw new IndexClosedException(shardId.getIndex());
        }
        throw new IndexNotFoundException(shardId.getIndexName());
    }
    final IndexShardRoutingTable shardRoutingTable = indexRoutingTable.shard(shardId.id());
    if (shardRoutingTable == null) {
        throw new ShardNotFoundException(shardId);
    }
    return shardRoutingTable;
}
Also used : IndexRoutingTable(org.elasticsearch.cluster.routing.IndexRoutingTable) IndexShardRoutingTable(org.elasticsearch.cluster.routing.IndexShardRoutingTable) ShardNotFoundException(org.elasticsearch.index.shard.ShardNotFoundException) IndexClosedException(org.elasticsearch.indices.IndexClosedException) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData)

Aggregations

IndexRoutingTable (org.elasticsearch.cluster.routing.IndexRoutingTable)35 IndexShardRoutingTable (org.elasticsearch.cluster.routing.IndexShardRoutingTable)29 ShardRouting (org.elasticsearch.cluster.routing.ShardRouting)21 ClusterState (org.elasticsearch.cluster.ClusterState)18 RoutingTable (org.elasticsearch.cluster.routing.RoutingTable)15 IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)14 ShardId (org.elasticsearch.index.shard.ShardId)11 HashSet (java.util.HashSet)7 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)6 DiscoveryNodes (org.elasticsearch.cluster.node.DiscoveryNodes)6 MetaData (org.elasticsearch.cluster.metadata.MetaData)5 Settings (org.elasticsearch.common.settings.Settings)5 ObjectIntHashMap (com.carrotsearch.hppc.ObjectIntHashMap)3 ArrayList (java.util.ArrayList)3 Set (java.util.Set)3 ClusterName (org.elasticsearch.cluster.ClusterName)3 TestShardRouting (org.elasticsearch.cluster.routing.TestShardRouting)3 UnassignedInfo (org.elasticsearch.cluster.routing.UnassignedInfo)3 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2