Search in sources :

Example 6 with MaxRetryAllocationDecider

use of org.elasticsearch.cluster.routing.allocation.decider.MaxRetryAllocationDecider in project elasticsearch by elastic.

the class TransportShrinkActionTests method testShrinkIndexSettings.

public void testShrinkIndexSettings() {
    String indexName = randomAsciiOfLength(10);
    // create one that won't fail
    ClusterState clusterState = ClusterState.builder(createClusterState(indexName, randomIntBetween(2, 10), 0, Settings.builder().put("index.blocks.write", true).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();
    int numSourceShards = clusterState.metaData().index(indexName).getNumberOfShards();
    DocsStats stats = new DocsStats(randomIntBetween(0, (IndexWriter.MAX_DOCS) / numSourceShards), randomIntBetween(1, 1000));
    ShrinkRequest target = new ShrinkRequest("target", indexName);
    final ActiveShardCount activeShardCount = randomBoolean() ? ActiveShardCount.ALL : ActiveShardCount.ONE;
    target.setWaitForActiveShards(activeShardCount);
    CreateIndexClusterStateUpdateRequest request = TransportShrinkAction.prepareCreateIndexRequest(target, clusterState, (i) -> stats, new IndexNameExpressionResolver(Settings.EMPTY));
    assertNotNull(request.shrinkFrom());
    assertEquals(indexName, request.shrinkFrom().getName());
    assertEquals("1", request.settings().get("index.number_of_shards"));
    assertEquals("shrink_index", request.cause());
    assertEquals(request.waitForActiveShards(), activeShardCount);
}
Also used : TestGatewayAllocator(org.elasticsearch.test.gateway.TestGatewayAllocator) ClusterState(org.elasticsearch.cluster.ClusterState) BalancedShardsAllocator(org.elasticsearch.cluster.routing.allocation.allocator.BalancedShardsAllocator) AllocationDeciders(org.elasticsearch.cluster.routing.allocation.decider.AllocationDeciders) MaxRetryAllocationDecider(org.elasticsearch.cluster.routing.allocation.decider.MaxRetryAllocationDecider) ActiveShardCount(org.elasticsearch.action.support.ActiveShardCount) CreateIndexClusterStateUpdateRequest(org.elasticsearch.action.admin.indices.create.CreateIndexClusterStateUpdateRequest) RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) DocsStats(org.elasticsearch.index.shard.DocsStats) IndexNameExpressionResolver(org.elasticsearch.cluster.metadata.IndexNameExpressionResolver) AllocationService(org.elasticsearch.cluster.routing.allocation.AllocationService)

Example 7 with MaxRetryAllocationDecider

use of org.elasticsearch.cluster.routing.allocation.decider.MaxRetryAllocationDecider in project elasticsearch by elastic.

the class MetaDataCreateIndexServiceTests method testValidateShrinkIndex.

public void testValidateShrinkIndex() {
    int numShards = randomIntBetween(2, 42);
    ClusterState state = createClusterState("source", numShards, randomIntBetween(0, 10), Settings.builder().put("index.blocks.write", true).build());
    assertEquals("index [source] already exists", expectThrows(ResourceAlreadyExistsException.class, () -> MetaDataCreateIndexService.validateShrinkIndex(state, "target", Collections.emptySet(), "source", Settings.EMPTY)).getMessage());
    assertEquals("no such index", expectThrows(IndexNotFoundException.class, () -> MetaDataCreateIndexService.validateShrinkIndex(state, "no such index", Collections.emptySet(), "target", Settings.EMPTY)).getMessage());
    assertEquals("can't shrink an index with only one shard", expectThrows(IllegalArgumentException.class, () -> MetaDataCreateIndexService.validateShrinkIndex(createClusterState("source", 1, 0, Settings.builder().put("index.blocks.write", true).build()), "source", Collections.emptySet(), "target", Settings.EMPTY)).getMessage());
    assertEquals("the number of target shards must be less that the number of source shards", expectThrows(IllegalArgumentException.class, () -> MetaDataCreateIndexService.validateShrinkIndex(createClusterState("source", 5, 0, Settings.builder().put("index.blocks.write", true).build()), "source", Collections.emptySet(), "target", Settings.builder().put("index.number_of_shards", 10).build())).getMessage());
    assertEquals("index source must be read-only to shrink index. use \"index.blocks.write=true\"", expectThrows(IllegalStateException.class, () -> MetaDataCreateIndexService.validateShrinkIndex(createClusterState("source", randomIntBetween(2, 100), randomIntBetween(0, 10), Settings.EMPTY), "source", Collections.emptySet(), "target", Settings.EMPTY)).getMessage());
    assertEquals("index source must have all shards allocated on the same node to shrink index", expectThrows(IllegalStateException.class, () -> MetaDataCreateIndexService.validateShrinkIndex(state, "source", Collections.emptySet(), "target", Settings.EMPTY)).getMessage());
    assertEquals("the number of source shards [8] must be a must be a multiple of [3]", expectThrows(IllegalArgumentException.class, () -> MetaDataCreateIndexService.validateShrinkIndex(createClusterState("source", 8, randomIntBetween(0, 10), Settings.builder().put("index.blocks.write", true).build()), "source", Collections.emptySet(), "target", Settings.builder().put("index.number_of_shards", 3).build())).getMessage());
    assertEquals("mappings are not allowed when shrinking indices, all mappings are copied from the source index", expectThrows(IllegalArgumentException.class, () -> {
        MetaDataCreateIndexService.validateShrinkIndex(state, "source", Collections.singleton("foo"), "target", Settings.EMPTY);
    }).getMessage());
    // create one that won't fail
    ClusterState clusterState = ClusterState.builder(createClusterState("source", numShards, 0, Settings.builder().put("index.blocks.write", true).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("source").shardsWithState(ShardRoutingState.INITIALIZING)).routingTable();
    clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build();
    int targetShards;
    do {
        targetShards = randomIntBetween(1, numShards / 2);
    } while (isShrinkable(numShards, targetShards) == false);
    MetaDataCreateIndexService.validateShrinkIndex(clusterState, "source", Collections.emptySet(), "target", Settings.builder().put("index.number_of_shards", targetShards).build());
}
Also used : TestGatewayAllocator(org.elasticsearch.test.gateway.TestGatewayAllocator) ClusterState(org.elasticsearch.cluster.ClusterState) RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) 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)

Aggregations

MaxRetryAllocationDecider (org.elasticsearch.cluster.routing.allocation.decider.MaxRetryAllocationDecider)7 ClusterState (org.elasticsearch.cluster.ClusterState)6 RoutingTable (org.elasticsearch.cluster.routing.RoutingTable)6 AllocationService (org.elasticsearch.cluster.routing.allocation.AllocationService)6 BalancedShardsAllocator (org.elasticsearch.cluster.routing.allocation.allocator.BalancedShardsAllocator)6 AllocationDeciders (org.elasticsearch.cluster.routing.allocation.decider.AllocationDeciders)6 TestGatewayAllocator (org.elasticsearch.test.gateway.TestGatewayAllocator)5 IndexNameExpressionResolver (org.elasticsearch.cluster.metadata.IndexNameExpressionResolver)3 Settings (org.elasticsearch.common.settings.Settings)3 List (java.util.List)2 Version (org.elasticsearch.Version)2 CreateIndexClusterStateUpdateRequest (org.elasticsearch.action.admin.indices.create.CreateIndexClusterStateUpdateRequest)2 ActiveShardCount (org.elasticsearch.action.support.ActiveShardCount)2 MetaData (org.elasticsearch.cluster.metadata.MetaData)2 DocsStats (org.elasticsearch.index.shard.DocsStats)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1