Search in sources :

Example 36 with AllocationService

use of org.elasticsearch.cluster.routing.allocation.AllocationService 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)

Example 37 with AllocationService

use of org.elasticsearch.cluster.routing.allocation.AllocationService in project crate by crate.

the class BulkRetryCoordinatorPoolTest method testReturnDifferentCoordinatorForRelocatedShardFromRemovedNode.

@Test
public void testReturnDifferentCoordinatorForRelocatedShardFromRemovedNode() throws Exception {
    ShardId shardId = new ShardId(TEST_INDEX, 1);
    BulkRetryCoordinator coordinator = pool.coordinator(shardId);
    ClusterState newState = ClusterState.builder(state).nodes(DiscoveryNodes.builder().put(newNode(NODE_IDS[1]))).build();
    AllocationService allocationService = createAllocationService();
    RoutingTable routingTable = allocationService.reroute(newState, "test").routingTable();
    newState = ClusterState.builder(newState).routingTable(routingTable).build();
    pool.clusterChanged(new ClusterChangedEvent("bla", newState, state));
    BulkRetryCoordinator otherCoordinator = pool.coordinator(shardId);
    assertThat(coordinator, not(sameInstance(otherCoordinator)));
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) ClusterState(org.elasticsearch.cluster.ClusterState) RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) ClusterChangedEvent(org.elasticsearch.cluster.ClusterChangedEvent) AllocationService(org.elasticsearch.cluster.routing.allocation.AllocationService) ESAllocationTestCase.createAllocationService(org.elasticsearch.test.ESAllocationTestCase.createAllocationService) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Aggregations

ClusterState (org.elasticsearch.cluster.ClusterState)37 AllocationService (org.elasticsearch.cluster.routing.allocation.AllocationService)37 RoutingTable (org.elasticsearch.cluster.routing.RoutingTable)30 IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)29 MetaData (org.elasticsearch.cluster.metadata.MetaData)28 BalancedShardsAllocator (org.elasticsearch.cluster.routing.allocation.allocator.BalancedShardsAllocator)12 Settings (org.elasticsearch.common.settings.Settings)12 TestGatewayAllocator (org.elasticsearch.test.gateway.TestGatewayAllocator)12 ClusterSettings (org.elasticsearch.common.settings.ClusterSettings)11 DiscoveryNodes (org.elasticsearch.cluster.node.DiscoveryNodes)9 Matchers.containsString (org.hamcrest.Matchers.containsString)9 ClusterInfo (org.elasticsearch.cluster.ClusterInfo)8 DevNullClusterInfo (org.elasticsearch.cluster.MockInternalClusterInfoService.DevNullClusterInfo)8 IndexShardRoutingTable (org.elasticsearch.cluster.routing.IndexShardRoutingTable)8 ShardRouting (org.elasticsearch.cluster.routing.ShardRouting)8 ImmutableOpenMap (org.elasticsearch.common.collect.ImmutableOpenMap)8 ClusterInfoService (org.elasticsearch.cluster.ClusterInfoService)7 DiskUsage (org.elasticsearch.cluster.DiskUsage)7 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)7 IndexRoutingTable (org.elasticsearch.cluster.routing.IndexRoutingTable)7