Search in sources :

Example 26 with RoutingTable

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

the class ShardFailedClusterStateTaskExecutorTests method createClusterStateWithStartedShards.

private ClusterState createClusterStateWithStartedShards(String reason) {
    int numberOfNodes = 1 + numberOfReplicas;
    DiscoveryNodes.Builder nodes = DiscoveryNodes.builder();
    IntStream.rangeClosed(1, numberOfNodes).mapToObj(node -> newNode("node" + node)).forEach(nodes::add);
    ClusterState stateAfterAddingNode = ClusterState.builder(clusterState).nodes(nodes).build();
    RoutingTable afterReroute = allocationService.reroute(stateAfterAddingNode, reason).routingTable();
    ClusterState stateAfterReroute = ClusterState.builder(stateAfterAddingNode).routingTable(afterReroute).build();
    RoutingNodes routingNodes = stateAfterReroute.getRoutingNodes();
    return allocationService.applyStartedShards(stateAfterReroute, routingNodes.shardsWithState(ShardRoutingState.INITIALIZING));
}
Also used : MetaData(org.elasticsearch.cluster.metadata.MetaData) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) ShardId(org.elasticsearch.index.shard.ShardId) IntStream(java.util.stream.IntStream) ESAllocationTestCase(org.elasticsearch.cluster.ESAllocationTestCase) ShardIterator(org.elasticsearch.cluster.routing.ShardIterator) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) AllocationService(org.elasticsearch.cluster.routing.allocation.AllocationService) Index(org.elasticsearch.index.Index) ShardRoutingState(org.elasticsearch.cluster.routing.ShardRoutingState) Function(java.util.function.Function) CorruptIndexException(org.apache.lucene.index.CorruptIndexException) GroupShardsIterator(org.elasticsearch.cluster.routing.GroupShardsIterator) ArrayList(java.util.ArrayList) CoreMatchers.instanceOf(org.hamcrest.CoreMatchers.instanceOf) ClusterState(org.elasticsearch.cluster.ClusterState) Settings(org.elasticsearch.common.settings.Settings) Map(java.util.Map) ClusterName(org.elasticsearch.cluster.ClusterName) RoutingNodes(org.elasticsearch.cluster.routing.RoutingNodes) ClusterRebalanceAllocationDecider(org.elasticsearch.cluster.routing.allocation.decider.ClusterRebalanceAllocationDecider) Before(org.junit.Before) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes) UUIDs(org.elasticsearch.common.UUIDs) ClusterStateTaskExecutor(org.elasticsearch.cluster.ClusterStateTaskExecutor) StaleShard(org.elasticsearch.cluster.routing.allocation.StaleShard) ObjectCursor(com.carrotsearch.hppc.cursors.ObjectCursor) Collectors(java.util.stream.Collectors) List(java.util.List) Version(org.elasticsearch.Version) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) FailedShard(org.elasticsearch.cluster.routing.allocation.FailedShard) Collections(java.util.Collections) TestShardRouting(org.elasticsearch.cluster.routing.TestShardRouting) ClusterState(org.elasticsearch.cluster.ClusterState) RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) RoutingNodes(org.elasticsearch.cluster.routing.RoutingNodes) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes)

Example 27 with RoutingTable

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

the class SimpleClusterStateIT method testFilteringByIndexWorks.

/**
     * Retrieves the cluster state for the given indices and then checks
     * that the cluster state returns coherent data for both routing table and metadata.
     */
private void testFilteringByIndexWorks(String[] indices, String[] expected) {
    ClusterStateResponse clusterState = client().admin().cluster().prepareState().clear().setMetaData(true).setRoutingTable(true).setIndices(indices).get();
    ImmutableOpenMap<String, IndexMetaData> metaData = clusterState.getState().getMetaData().indices();
    assertThat(metaData.size(), is(expected.length));
    RoutingTable routingTable = clusterState.getState().getRoutingTable();
    assertThat(routingTable.indicesRouting().size(), is(expected.length));
    for (String expectedIndex : expected) {
        assertThat(metaData, CollectionAssertions.hasKey(expectedIndex));
        assertThat(routingTable.hasIndex(expectedIndex), is(true));
    }
}
Also used : RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) ClusterStateResponse(org.elasticsearch.action.admin.cluster.state.ClusterStateResponse) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData)

Example 28 with RoutingTable

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

the class MetaDataCreateIndexServiceTests method testShrinkIndexSettings.

public void testShrinkIndexSettings() {
    String indexName = randomAsciiOfLength(10);
    List<Version> versions = Arrays.asList(VersionUtils.randomVersion(random()), VersionUtils.randomVersion(random()), VersionUtils.randomVersion(random()));
    versions.sort((l, r) -> Long.compare(l.id, r.id));
    Version version = versions.get(0);
    Version minCompat = versions.get(1);
    Version upgraded = versions.get(2);
    // create one that won't fail
    ClusterState clusterState = ClusterState.builder(createClusterState(indexName, randomIntBetween(2, 10), 0, Settings.builder().put("index.blocks.write", true).put("index.similarity.default.type", "BM25").put("index.version.created", version).put("index.version.upgraded", upgraded).put("index.version.minimum_compatible", minCompat.luceneVersion).put("index.analysis.analyzer.my_analyzer.tokenizer", "keyword").build())).nodes(DiscoveryNodes.builder().add(newNode("node1"))).build();
    AllocationService service = new AllocationService(Settings.builder().build(), new AllocationDeciders(Settings.EMPTY, Collections.singleton(new MaxRetryAllocationDecider(Settings.EMPTY))), new TestGatewayAllocator(), new BalancedShardsAllocator(Settings.EMPTY), EmptyClusterInfoService.INSTANCE);
    RoutingTable routingTable = service.reroute(clusterState, "reroute").routingTable();
    clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build();
    // now we start the shard
    routingTable = service.applyStartedShards(clusterState, routingTable.index(indexName).shardsWithState(ShardRoutingState.INITIALIZING)).routingTable();
    clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build();
    Settings.Builder builder = Settings.builder();
    MetaDataCreateIndexService.prepareShrinkIndexSettings(clusterState, Collections.emptySet(), builder, clusterState.metaData().index(indexName).getIndex(), "target");
    assertEquals("similarity settings must be copied", "BM25", builder.build().get("index.similarity.default.type"));
    assertEquals("analysis settings must be copied", "keyword", builder.build().get("index.analysis.analyzer.my_analyzer.tokenizer"));
    assertEquals("node1", builder.build().get("index.routing.allocation.initial_recovery._id"));
    assertEquals("1", builder.build().get("index.allocation.max_retries"));
    assertEquals(version, builder.build().getAsVersion("index.version.created", null));
    assertEquals(upgraded, builder.build().getAsVersion("index.version.upgraded", null));
    assertEquals(minCompat.luceneVersion.toString(), builder.build().get("index.version.minimum_compatible", null));
}
Also used : TestGatewayAllocator(org.elasticsearch.test.gateway.TestGatewayAllocator) ClusterState(org.elasticsearch.cluster.ClusterState) RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) Version(org.elasticsearch.Version) BalancedShardsAllocator(org.elasticsearch.cluster.routing.allocation.allocator.BalancedShardsAllocator) AllocationDeciders(org.elasticsearch.cluster.routing.allocation.decider.AllocationDeciders) MaxRetryAllocationDecider(org.elasticsearch.cluster.routing.allocation.decider.MaxRetryAllocationDecider) AllocationService(org.elasticsearch.cluster.routing.allocation.AllocationService) Settings(org.elasticsearch.common.settings.Settings)

Example 29 with RoutingTable

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

the class ClusterStateHealthTests method generateClusterStates.

private List<ClusterState> generateClusterStates(final ClusterState originalClusterState, final String indexName, final int numberOfReplicas, final boolean withPrimaryAllocationFailures) {
    // generate random node ids
    final Set<String> nodeIds = new HashSet<>();
    final int numNodes = randomIntBetween(numberOfReplicas + 1, 10);
    for (int i = 0; i < numNodes; i++) {
        nodeIds.add(randomAsciiOfLength(8));
    }
    final List<ClusterState> clusterStates = new ArrayList<>();
    clusterStates.add(originalClusterState);
    ClusterState clusterState = originalClusterState;
    // initialize primaries
    RoutingTable routingTable = originalClusterState.routingTable();
    IndexRoutingTable indexRoutingTable = routingTable.index(indexName);
    IndexRoutingTable.Builder newIndexRoutingTable = IndexRoutingTable.builder(indexRoutingTable.getIndex());
    for (final ObjectCursor<IndexShardRoutingTable> shardEntry : indexRoutingTable.getShards().values()) {
        final IndexShardRoutingTable shardRoutingTable = shardEntry.value;
        for (final ShardRouting shardRouting : shardRoutingTable.getShards()) {
            if (shardRouting.primary()) {
                newIndexRoutingTable.addShard(shardRouting.initialize(randomFrom(nodeIds), null, shardRouting.getExpectedShardSize()));
            } else {
                newIndexRoutingTable.addShard(shardRouting);
            }
        }
    }
    routingTable = RoutingTable.builder(routingTable).add(newIndexRoutingTable).build();
    clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build();
    clusterStates.add(clusterState);
    // some primaries started
    indexRoutingTable = routingTable.index(indexName);
    newIndexRoutingTable = IndexRoutingTable.builder(indexRoutingTable.getIndex());
    ImmutableOpenIntMap.Builder<Set<String>> allocationIds = ImmutableOpenIntMap.<Set<String>>builder();
    for (final ObjectCursor<IndexShardRoutingTable> shardEntry : indexRoutingTable.getShards().values()) {
        final IndexShardRoutingTable shardRoutingTable = shardEntry.value;
        for (final ShardRouting shardRouting : shardRoutingTable.getShards()) {
            if (shardRouting.primary() && randomBoolean()) {
                final ShardRouting newShardRouting = shardRouting.moveToStarted();
                allocationIds.fPut(newShardRouting.getId(), Sets.newHashSet(newShardRouting.allocationId().getId()));
                newIndexRoutingTable.addShard(newShardRouting);
            } else {
                newIndexRoutingTable.addShard(shardRouting);
            }
        }
    }
    routingTable = RoutingTable.builder(routingTable).add(newIndexRoutingTable).build();
    IndexMetaData.Builder idxMetaBuilder = IndexMetaData.builder(clusterState.metaData().index(indexName));
    for (final IntObjectCursor<Set<String>> entry : allocationIds.build()) {
        idxMetaBuilder.putInSyncAllocationIds(entry.key, entry.value);
    }
    MetaData.Builder metaDataBuilder = MetaData.builder(clusterState.metaData()).put(idxMetaBuilder);
    clusterState = ClusterState.builder(clusterState).routingTable(routingTable).metaData(metaDataBuilder).build();
    clusterStates.add(clusterState);
    if (withPrimaryAllocationFailures) {
        boolean alreadyFailedPrimary = false;
        // some primaries failed to allocate
        indexRoutingTable = routingTable.index(indexName);
        newIndexRoutingTable = IndexRoutingTable.builder(indexRoutingTable.getIndex());
        for (final ObjectCursor<IndexShardRoutingTable> shardEntry : indexRoutingTable.getShards().values()) {
            final IndexShardRoutingTable shardRoutingTable = shardEntry.value;
            for (final ShardRouting shardRouting : shardRoutingTable.getShards()) {
                if (shardRouting.primary() && (shardRouting.started() == false || alreadyFailedPrimary == false)) {
                    newIndexRoutingTable.addShard(shardRouting.moveToUnassigned(new UnassignedInfo(UnassignedInfo.Reason.ALLOCATION_FAILED, "unlucky shard")));
                    alreadyFailedPrimary = true;
                } else {
                    newIndexRoutingTable.addShard(shardRouting);
                }
            }
        }
        routingTable = RoutingTable.builder(routingTable).add(newIndexRoutingTable).build();
        clusterStates.add(ClusterState.builder(clusterState).routingTable(routingTable).build());
        return clusterStates;
    }
    // all primaries started
    indexRoutingTable = routingTable.index(indexName);
    newIndexRoutingTable = IndexRoutingTable.builder(indexRoutingTable.getIndex());
    allocationIds = ImmutableOpenIntMap.<Set<String>>builder();
    for (final ObjectCursor<IndexShardRoutingTable> shardEntry : indexRoutingTable.getShards().values()) {
        final IndexShardRoutingTable shardRoutingTable = shardEntry.value;
        for (final ShardRouting shardRouting : shardRoutingTable.getShards()) {
            if (shardRouting.primary() && shardRouting.started() == false) {
                final ShardRouting newShardRouting = shardRouting.moveToStarted();
                allocationIds.fPut(newShardRouting.getId(), Sets.newHashSet(newShardRouting.allocationId().getId()));
                newIndexRoutingTable.addShard(newShardRouting);
            } else {
                newIndexRoutingTable.addShard(shardRouting);
            }
        }
    }
    routingTable = RoutingTable.builder(routingTable).add(newIndexRoutingTable).build();
    idxMetaBuilder = IndexMetaData.builder(clusterState.metaData().index(indexName));
    for (final IntObjectCursor<Set<String>> entry : allocationIds.build()) {
        idxMetaBuilder.putInSyncAllocationIds(entry.key, entry.value);
    }
    metaDataBuilder = MetaData.builder(clusterState.metaData()).put(idxMetaBuilder);
    clusterState = ClusterState.builder(clusterState).routingTable(routingTable).metaData(metaDataBuilder).build();
    clusterStates.add(clusterState);
    // initialize replicas
    indexRoutingTable = routingTable.index(indexName);
    newIndexRoutingTable = IndexRoutingTable.builder(indexRoutingTable.getIndex());
    for (final ObjectCursor<IndexShardRoutingTable> shardEntry : indexRoutingTable.getShards().values()) {
        final IndexShardRoutingTable shardRoutingTable = shardEntry.value;
        final String primaryNodeId = shardRoutingTable.primaryShard().currentNodeId();
        Set<String> allocatedNodes = new HashSet<>();
        allocatedNodes.add(primaryNodeId);
        for (final ShardRouting shardRouting : shardRoutingTable.getShards()) {
            if (shardRouting.primary() == false) {
                // give the replica a different node id than the primary
                String replicaNodeId = randomFrom(Sets.difference(nodeIds, allocatedNodes));
                newIndexRoutingTable.addShard(shardRouting.initialize(replicaNodeId, null, shardRouting.getExpectedShardSize()));
                allocatedNodes.add(replicaNodeId);
            } else {
                newIndexRoutingTable.addShard(shardRouting);
            }
        }
    }
    routingTable = RoutingTable.builder(routingTable).add(newIndexRoutingTable).build();
    clusterStates.add(ClusterState.builder(clusterState).routingTable(routingTable).build());
    // some replicas started
    indexRoutingTable = routingTable.index(indexName);
    newIndexRoutingTable = IndexRoutingTable.builder(indexRoutingTable.getIndex());
    for (final ObjectCursor<IndexShardRoutingTable> shardEntry : indexRoutingTable.getShards().values()) {
        final IndexShardRoutingTable shardRoutingTable = shardEntry.value;
        for (final ShardRouting shardRouting : shardRoutingTable.getShards()) {
            if (shardRouting.primary() == false && randomBoolean()) {
                newIndexRoutingTable.addShard(shardRouting.moveToStarted());
            } else {
                newIndexRoutingTable.addShard(shardRouting);
            }
        }
    }
    routingTable = RoutingTable.builder(routingTable).add(newIndexRoutingTable).build();
    clusterStates.add(ClusterState.builder(clusterState).routingTable(routingTable).build());
    // all replicas started
    boolean replicaStateChanged = false;
    indexRoutingTable = routingTable.index(indexName);
    newIndexRoutingTable = IndexRoutingTable.builder(indexRoutingTable.getIndex());
    for (final ObjectCursor<IndexShardRoutingTable> shardEntry : indexRoutingTable.getShards().values()) {
        final IndexShardRoutingTable shardRoutingTable = shardEntry.value;
        for (final ShardRouting shardRouting : shardRoutingTable.getShards()) {
            if (shardRouting.primary() == false && shardRouting.started() == false) {
                newIndexRoutingTable.addShard(shardRouting.moveToStarted());
                replicaStateChanged = true;
            } else {
                newIndexRoutingTable.addShard(shardRouting);
            }
        }
    }
    // all of the replicas may have moved to started in the previous phase already
    if (replicaStateChanged) {
        routingTable = RoutingTable.builder(routingTable).add(newIndexRoutingTable).build();
        clusterStates.add(ClusterState.builder(clusterState).routingTable(routingTable).build());
    }
    return clusterStates;
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) IndexRoutingTable(org.elasticsearch.cluster.routing.IndexRoutingTable) IndexShardRoutingTable(org.elasticsearch.cluster.routing.IndexShardRoutingTable) Set(java.util.Set) HashSet(java.util.HashSet) UnassignedInfo(org.elasticsearch.cluster.routing.UnassignedInfo) ArrayList(java.util.ArrayList) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) IndexShardRoutingTable(org.elasticsearch.cluster.routing.IndexShardRoutingTable) IndexRoutingTable(org.elasticsearch.cluster.routing.IndexRoutingTable) RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) ImmutableOpenIntMap(org.elasticsearch.common.collect.ImmutableOpenIntMap) MetaData(org.elasticsearch.cluster.metadata.MetaData) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) HashSet(java.util.HashSet)

Example 30 with RoutingTable

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

the class ClusterStateHealthTests method testClusterHealth.

public void testClusterHealth() throws IOException {
    RoutingTableGenerator routingTableGenerator = new RoutingTableGenerator();
    RoutingTableGenerator.ShardCounter counter = new RoutingTableGenerator.ShardCounter();
    RoutingTable.Builder routingTable = RoutingTable.builder();
    MetaData.Builder metaData = MetaData.builder();
    for (int i = randomInt(4); i >= 0; i--) {
        int numberOfShards = randomInt(3) + 1;
        int numberOfReplicas = randomInt(4);
        IndexMetaData indexMetaData = IndexMetaData.builder("test_" + Integer.toString(i)).settings(settings(Version.CURRENT)).numberOfShards(numberOfShards).numberOfReplicas(numberOfReplicas).build();
        IndexRoutingTable indexRoutingTable = routingTableGenerator.genIndexRoutingTable(indexMetaData, counter);
        metaData.put(indexMetaData, true);
        routingTable.add(indexRoutingTable);
    }
    ClusterState clusterState = ClusterState.builder(ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(routingTable.build()).build();
    String[] concreteIndices = indexNameExpressionResolver.concreteIndexNames(clusterState, IndicesOptions.strictExpand(), (String[]) null);
    ClusterStateHealth clusterStateHealth = new ClusterStateHealth(clusterState, concreteIndices);
    logger.info("cluster status: {}, expected {}", clusterStateHealth.getStatus(), counter.status());
    clusterStateHealth = maybeSerialize(clusterStateHealth);
    assertClusterHealth(clusterStateHealth, counter);
}
Also used : IndexRoutingTable(org.elasticsearch.cluster.routing.IndexRoutingTable) ClusterState(org.elasticsearch.cluster.ClusterState) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) IndexShardRoutingTable(org.elasticsearch.cluster.routing.IndexShardRoutingTable) IndexRoutingTable(org.elasticsearch.cluster.routing.IndexRoutingTable) RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) MetaData(org.elasticsearch.cluster.metadata.MetaData) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) RoutingTableGenerator(org.elasticsearch.cluster.routing.RoutingTableGenerator)

Aggregations

RoutingTable (org.elasticsearch.cluster.routing.RoutingTable)228 ClusterState (org.elasticsearch.cluster.ClusterState)201 IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)140 MetaData (org.elasticsearch.cluster.metadata.MetaData)135 ShardRouting (org.elasticsearch.cluster.routing.ShardRouting)74 IndexShardRoutingTable (org.elasticsearch.cluster.routing.IndexShardRoutingTable)66 RoutingNodes (org.elasticsearch.cluster.routing.RoutingNodes)64 IndexMetadata (org.elasticsearch.cluster.metadata.IndexMetadata)59 Metadata (org.elasticsearch.cluster.metadata.Metadata)57 IndexRoutingTable (org.elasticsearch.cluster.routing.IndexRoutingTable)51 AllocationService (org.elasticsearch.cluster.routing.allocation.AllocationService)48 ShardId (org.elasticsearch.index.shard.ShardId)37 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)33 Settings (org.elasticsearch.common.settings.Settings)33 Index (org.elasticsearch.index.Index)31 Matchers.containsString (org.hamcrest.Matchers.containsString)30 HashSet (java.util.HashSet)29 DiscoveryNodes (org.elasticsearch.cluster.node.DiscoveryNodes)29 ImmutableOpenMap (org.elasticsearch.common.collect.ImmutableOpenMap)29 ClusterSettings (org.elasticsearch.common.settings.ClusterSettings)28