Search in sources :

Example 1 with MaxRetryAllocationDecider

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

the class ClusterRerouteTests method testClusterStateUpdateTask.

public void testClusterStateUpdateTask() {
    AllocationService allocationService = new AllocationService(Settings.builder().build(), new AllocationDeciders(Settings.EMPTY, Collections.singleton(new MaxRetryAllocationDecider(Settings.EMPTY))), new TestGatewayAllocator(), new BalancedShardsAllocator(Settings.EMPTY), EmptyClusterInfoService.INSTANCE);
    ClusterState clusterState = createInitialClusterState(allocationService);
    ClusterRerouteRequest req = new ClusterRerouteRequest();
    req.dryRun(true);
    AtomicReference<ClusterRerouteResponse> responseRef = new AtomicReference<>();
    ActionListener<ClusterRerouteResponse> responseActionListener = new ActionListener<ClusterRerouteResponse>() {

        @Override
        public void onResponse(ClusterRerouteResponse clusterRerouteResponse) {
            responseRef.set(clusterRerouteResponse);
        }

        @Override
        public void onFailure(Exception e) {
        }
    };
    TransportClusterRerouteAction.ClusterRerouteResponseAckedClusterStateUpdateTask task = new TransportClusterRerouteAction.ClusterRerouteResponseAckedClusterStateUpdateTask(logger, allocationService, req, responseActionListener);
    ClusterState execute = task.execute(clusterState);
    // dry-run
    assertSame(execute, clusterState);
    task.onAllNodesAcked(null);
    assertNotSame(responseRef.get().getState(), execute);
    // now we allocate
    req.dryRun(false);
    final int retries = MaxRetryAllocationDecider.SETTING_ALLOCATION_MAX_RETRY.get(Settings.EMPTY);
    // now fail it N-1 times
    for (int i = 0; i < retries; i++) {
        ClusterState newState = task.execute(clusterState);
        // dry-run=false
        assertNotSame(newState, clusterState);
        clusterState = newState;
        RoutingTable routingTable = clusterState.routingTable();
        assertEquals(routingTable.index("idx").shards().size(), 1);
        assertEquals(routingTable.index("idx").shard(0).shards().get(0).state(), INITIALIZING);
        assertEquals(routingTable.index("idx").shard(0).shards().get(0).unassignedInfo().getNumFailedAllocations(), i);
        List<FailedShard> failedShards = Collections.singletonList(new FailedShard(routingTable.index("idx").shard(0).shards().get(0), "boom" + i, new UnsupportedOperationException()));
        newState = allocationService.applyFailedShards(clusterState, failedShards);
        assertThat(newState, not(equalTo(clusterState)));
        clusterState = newState;
        routingTable = clusterState.routingTable();
        assertEquals(routingTable.index("idx").shards().size(), 1);
        if (i == retries - 1) {
            assertEquals(routingTable.index("idx").shard(0).shards().get(0).state(), UNASSIGNED);
        } else {
            assertEquals(routingTable.index("idx").shard(0).shards().get(0).state(), INITIALIZING);
        }
        assertEquals(routingTable.index("idx").shard(0).shards().get(0).unassignedInfo().getNumFailedAllocations(), i + 1);
    }
    // without retry_failed we won't allocate that shard
    ClusterState newState = task.execute(clusterState);
    // dry-run=false
    assertNotSame(newState, clusterState);
    task.onAllNodesAcked(null);
    assertSame(responseRef.get().getState(), newState);
    RoutingTable routingTable = clusterState.routingTable();
    assertEquals(routingTable.index("idx").shards().size(), 1);
    assertEquals(routingTable.index("idx").shard(0).shards().get(0).state(), UNASSIGNED);
    assertEquals(routingTable.index("idx").shard(0).shards().get(0).unassignedInfo().getNumFailedAllocations(), retries);
    // now we manually retry and get the shard back into initializing
    req.setRetryFailed(true);
    newState = task.execute(clusterState);
    // dry-run=false
    assertNotSame(newState, clusterState);
    clusterState = newState;
    routingTable = clusterState.routingTable();
    assertEquals(routingTable.index("idx").shards().size(), 1);
    assertEquals(routingTable.index("idx").shard(0).shards().get(0).state(), INITIALIZING);
    assertEquals(routingTable.index("idx").shard(0).shards().get(0).unassignedInfo().getNumFailedAllocations(), retries);
}
Also used : TestGatewayAllocator(org.elasticsearch.test.gateway.TestGatewayAllocator) ClusterState(org.elasticsearch.cluster.ClusterState) BalancedShardsAllocator(org.elasticsearch.cluster.routing.allocation.allocator.BalancedShardsAllocator) FailedShard(org.elasticsearch.cluster.routing.allocation.FailedShard) AtomicReference(java.util.concurrent.atomic.AtomicReference) AllocationDeciders(org.elasticsearch.cluster.routing.allocation.decider.AllocationDeciders) MaxRetryAllocationDecider(org.elasticsearch.cluster.routing.allocation.decider.MaxRetryAllocationDecider) IOException(java.io.IOException) ActionListener(org.elasticsearch.action.ActionListener) RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) AllocationService(org.elasticsearch.cluster.routing.allocation.AllocationService)

Example 2 with MaxRetryAllocationDecider

use of org.elasticsearch.cluster.routing.allocation.decider.MaxRetryAllocationDecider 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 3 with MaxRetryAllocationDecider

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

the class ClusterModule method createAllocationDeciders.

// TODO: this is public so allocation benchmark can access the default deciders...can we do that in another way?
/** Return a new {@link AllocationDecider} instance with builtin deciders as well as those from plugins. */
public static Collection<AllocationDecider> createAllocationDeciders(Settings settings, ClusterSettings clusterSettings, List<ClusterPlugin> clusterPlugins) {
    // collect deciders by class so that we can detect duplicates
    Map<Class, AllocationDecider> deciders = new LinkedHashMap<>();
    addAllocationDecider(deciders, new MaxRetryAllocationDecider(settings));
    addAllocationDecider(deciders, new ReplicaAfterPrimaryActiveAllocationDecider(settings));
    addAllocationDecider(deciders, new RebalanceOnlyWhenActiveAllocationDecider(settings));
    addAllocationDecider(deciders, new ClusterRebalanceAllocationDecider(settings, clusterSettings));
    addAllocationDecider(deciders, new ConcurrentRebalanceAllocationDecider(settings, clusterSettings));
    addAllocationDecider(deciders, new EnableAllocationDecider(settings, clusterSettings));
    addAllocationDecider(deciders, new NodeVersionAllocationDecider(settings));
    addAllocationDecider(deciders, new SnapshotInProgressAllocationDecider(settings));
    addAllocationDecider(deciders, new FilterAllocationDecider(settings, clusterSettings));
    addAllocationDecider(deciders, new SameShardAllocationDecider(settings, clusterSettings));
    addAllocationDecider(deciders, new DiskThresholdDecider(settings, clusterSettings));
    addAllocationDecider(deciders, new ThrottlingAllocationDecider(settings, clusterSettings));
    addAllocationDecider(deciders, new ShardsLimitAllocationDecider(settings, clusterSettings));
    addAllocationDecider(deciders, new AwarenessAllocationDecider(settings, clusterSettings));
    clusterPlugins.stream().flatMap(p -> p.createAllocationDeciders(settings, clusterSettings).stream()).forEach(d -> addAllocationDecider(deciders, d));
    return deciders.values();
}
Also used : MetaData(org.elasticsearch.cluster.metadata.MetaData) DelayedAllocationService(org.elasticsearch.cluster.routing.DelayedAllocationService) IngestMetadata(org.elasticsearch.ingest.IngestMetadata) AwarenessAllocationDecider(org.elasticsearch.cluster.routing.allocation.decider.AwarenessAllocationDecider) Property(org.elasticsearch.common.settings.Setting.Property) SameShardAllocationDecider(org.elasticsearch.cluster.routing.allocation.decider.SameShardAllocationDecider) AllocationService(org.elasticsearch.cluster.routing.allocation.AllocationService) Reader(org.elasticsearch.common.io.stream.Writeable.Reader) ConcurrentRebalanceAllocationDecider(org.elasticsearch.cluster.routing.allocation.decider.ConcurrentRebalanceAllocationDecider) ClusterPlugin(org.elasticsearch.plugins.ClusterPlugin) Settings(org.elasticsearch.common.settings.Settings) SnapshotInProgressAllocationDecider(org.elasticsearch.cluster.routing.allocation.decider.SnapshotInProgressAllocationDecider) Map(java.util.Map) TaskResultsService(org.elasticsearch.tasks.TaskResultsService) ParseField(org.elasticsearch.common.ParseField) NamedXContentRegistry(org.elasticsearch.common.xcontent.NamedXContentRegistry) ShardsLimitAllocationDecider(org.elasticsearch.cluster.routing.allocation.decider.ShardsLimitAllocationDecider) MappingUpdatedAction(org.elasticsearch.cluster.action.index.MappingUpdatedAction) BalancedShardsAllocator(org.elasticsearch.cluster.routing.allocation.allocator.BalancedShardsAllocator) Setting(org.elasticsearch.common.settings.Setting) AbstractModule(org.elasticsearch.common.inject.AbstractModule) DiskThresholdDecider(org.elasticsearch.cluster.routing.allocation.decider.DiskThresholdDecider) Collection(java.util.Collection) NodeMappingRefreshAction(org.elasticsearch.cluster.action.index.NodeMappingRefreshAction) Objects(java.util.Objects) RebalanceOnlyWhenActiveAllocationDecider(org.elasticsearch.cluster.routing.allocation.decider.RebalanceOnlyWhenActiveAllocationDecider) List(java.util.List) ShardStateAction(org.elasticsearch.cluster.action.shard.ShardStateAction) AllocationDecider(org.elasticsearch.cluster.routing.allocation.decider.AllocationDecider) NodeVersionAllocationDecider(org.elasticsearch.cluster.routing.allocation.decider.NodeVersionAllocationDecider) FilterAllocationDecider(org.elasticsearch.cluster.routing.allocation.decider.FilterAllocationDecider) ThrottlingAllocationDecider(org.elasticsearch.cluster.routing.allocation.decider.ThrottlingAllocationDecider) MetaDataCreateIndexService(org.elasticsearch.cluster.metadata.MetaDataCreateIndexService) ReplicaAfterPrimaryActiveAllocationDecider(org.elasticsearch.cluster.routing.allocation.decider.ReplicaAfterPrimaryActiveAllocationDecider) MaxRetryAllocationDecider(org.elasticsearch.cluster.routing.allocation.decider.MaxRetryAllocationDecider) ClusterService(org.elasticsearch.cluster.service.ClusterService) NamedWriteable(org.elasticsearch.common.io.stream.NamedWriteable) IndexGraveyard(org.elasticsearch.cluster.metadata.IndexGraveyard) HashMap(java.util.HashMap) Function(java.util.function.Function) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) MetaDataUpdateSettingsService(org.elasticsearch.cluster.metadata.MetaDataUpdateSettingsService) NamedWriteableRegistry(org.elasticsearch.common.io.stream.NamedWriteableRegistry) AllocationDeciders(org.elasticsearch.cluster.routing.allocation.decider.AllocationDeciders) ClusterRebalanceAllocationDecider(org.elasticsearch.cluster.routing.allocation.decider.ClusterRebalanceAllocationDecider) EnableAllocationDecider(org.elasticsearch.cluster.routing.allocation.decider.EnableAllocationDecider) RepositoriesMetaData(org.elasticsearch.cluster.metadata.RepositoriesMetaData) MetaDataIndexAliasesService(org.elasticsearch.cluster.metadata.MetaDataIndexAliasesService) ShardsAllocator(org.elasticsearch.cluster.routing.allocation.allocator.ShardsAllocator) ClusterSettings(org.elasticsearch.common.settings.ClusterSettings) MetaDataIndexTemplateService(org.elasticsearch.cluster.metadata.MetaDataIndexTemplateService) ScriptMetaData(org.elasticsearch.script.ScriptMetaData) MetaDataMappingService(org.elasticsearch.cluster.metadata.MetaDataMappingService) RoutingService(org.elasticsearch.cluster.routing.RoutingService) MetaDataDeleteIndexService(org.elasticsearch.cluster.metadata.MetaDataDeleteIndexService) IndexNameExpressionResolver(org.elasticsearch.cluster.metadata.IndexNameExpressionResolver) Writeable(org.elasticsearch.common.io.stream.Writeable) GatewayAllocator(org.elasticsearch.gateway.GatewayAllocator) MetaDataIndexStateService(org.elasticsearch.cluster.metadata.MetaDataIndexStateService) Entry(org.elasticsearch.common.io.stream.NamedWriteableRegistry.Entry) SnapshotInProgressAllocationDecider(org.elasticsearch.cluster.routing.allocation.decider.SnapshotInProgressAllocationDecider) EnableAllocationDecider(org.elasticsearch.cluster.routing.allocation.decider.EnableAllocationDecider) RebalanceOnlyWhenActiveAllocationDecider(org.elasticsearch.cluster.routing.allocation.decider.RebalanceOnlyWhenActiveAllocationDecider) ThrottlingAllocationDecider(org.elasticsearch.cluster.routing.allocation.decider.ThrottlingAllocationDecider) MaxRetryAllocationDecider(org.elasticsearch.cluster.routing.allocation.decider.MaxRetryAllocationDecider) ReplicaAfterPrimaryActiveAllocationDecider(org.elasticsearch.cluster.routing.allocation.decider.ReplicaAfterPrimaryActiveAllocationDecider) LinkedHashMap(java.util.LinkedHashMap) ClusterRebalanceAllocationDecider(org.elasticsearch.cluster.routing.allocation.decider.ClusterRebalanceAllocationDecider) SameShardAllocationDecider(org.elasticsearch.cluster.routing.allocation.decider.SameShardAllocationDecider) FilterAllocationDecider(org.elasticsearch.cluster.routing.allocation.decider.FilterAllocationDecider) DiskThresholdDecider(org.elasticsearch.cluster.routing.allocation.decider.DiskThresholdDecider) ShardsLimitAllocationDecider(org.elasticsearch.cluster.routing.allocation.decider.ShardsLimitAllocationDecider) AwarenessAllocationDecider(org.elasticsearch.cluster.routing.allocation.decider.AwarenessAllocationDecider) NodeVersionAllocationDecider(org.elasticsearch.cluster.routing.allocation.decider.NodeVersionAllocationDecider) AwarenessAllocationDecider(org.elasticsearch.cluster.routing.allocation.decider.AwarenessAllocationDecider) SameShardAllocationDecider(org.elasticsearch.cluster.routing.allocation.decider.SameShardAllocationDecider) ConcurrentRebalanceAllocationDecider(org.elasticsearch.cluster.routing.allocation.decider.ConcurrentRebalanceAllocationDecider) SnapshotInProgressAllocationDecider(org.elasticsearch.cluster.routing.allocation.decider.SnapshotInProgressAllocationDecider) ShardsLimitAllocationDecider(org.elasticsearch.cluster.routing.allocation.decider.ShardsLimitAllocationDecider) RebalanceOnlyWhenActiveAllocationDecider(org.elasticsearch.cluster.routing.allocation.decider.RebalanceOnlyWhenActiveAllocationDecider) AllocationDecider(org.elasticsearch.cluster.routing.allocation.decider.AllocationDecider) NodeVersionAllocationDecider(org.elasticsearch.cluster.routing.allocation.decider.NodeVersionAllocationDecider) FilterAllocationDecider(org.elasticsearch.cluster.routing.allocation.decider.FilterAllocationDecider) ThrottlingAllocationDecider(org.elasticsearch.cluster.routing.allocation.decider.ThrottlingAllocationDecider) ReplicaAfterPrimaryActiveAllocationDecider(org.elasticsearch.cluster.routing.allocation.decider.ReplicaAfterPrimaryActiveAllocationDecider) MaxRetryAllocationDecider(org.elasticsearch.cluster.routing.allocation.decider.MaxRetryAllocationDecider) ClusterRebalanceAllocationDecider(org.elasticsearch.cluster.routing.allocation.decider.ClusterRebalanceAllocationDecider) EnableAllocationDecider(org.elasticsearch.cluster.routing.allocation.decider.EnableAllocationDecider) ConcurrentRebalanceAllocationDecider(org.elasticsearch.cluster.routing.allocation.decider.ConcurrentRebalanceAllocationDecider)

Example 4 with MaxRetryAllocationDecider

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

the class MaxRetryAllocationDeciderTests method testFailedAllocation.

public void testFailedAllocation() {
    ClusterState clusterState = createInitialClusterState();
    RoutingTable routingTable = clusterState.routingTable();
    final int retries = MaxRetryAllocationDecider.SETTING_ALLOCATION_MAX_RETRY.get(Settings.EMPTY);
    // now fail it N-1 times
    for (int i = 0; i < retries - 1; i++) {
        List<FailedShard> failedShards = Collections.singletonList(new FailedShard(routingTable.index("idx").shard(0).shards().get(0), "boom" + i, new UnsupportedOperationException()));
        ClusterState newState = strategy.applyFailedShards(clusterState, failedShards);
        assertThat(newState, not(equalTo(clusterState)));
        clusterState = newState;
        routingTable = newState.routingTable();
        assertEquals(routingTable.index("idx").shards().size(), 1);
        ShardRouting unassignedPrimary = routingTable.index("idx").shard(0).shards().get(0);
        assertEquals(unassignedPrimary.state(), INITIALIZING);
        assertEquals(unassignedPrimary.unassignedInfo().getNumFailedAllocations(), i + 1);
        assertEquals(unassignedPrimary.unassignedInfo().getMessage(), "boom" + i);
        // MaxRetryAllocationDecider#canForceAllocatePrimary should return YES decisions because canAllocate returns YES here
        assertEquals(Decision.YES, new MaxRetryAllocationDecider(Settings.EMPTY).canForceAllocatePrimary(unassignedPrimary, null, new RoutingAllocation(null, null, clusterState, null, 0, false)));
    }
    // now we go and check that we are actually stick to unassigned on the next failure
    {
        List<FailedShard> failedShards = Collections.singletonList(new FailedShard(routingTable.index("idx").shard(0).shards().get(0), "boom", new UnsupportedOperationException()));
        ClusterState newState = strategy.applyFailedShards(clusterState, failedShards);
        assertThat(newState, not(equalTo(clusterState)));
        clusterState = newState;
        routingTable = newState.routingTable();
        assertEquals(routingTable.index("idx").shards().size(), 1);
        ShardRouting unassignedPrimary = routingTable.index("idx").shard(0).shards().get(0);
        assertEquals(unassignedPrimary.unassignedInfo().getNumFailedAllocations(), retries);
        assertEquals(unassignedPrimary.state(), UNASSIGNED);
        assertEquals(unassignedPrimary.unassignedInfo().getMessage(), "boom");
        // MaxRetryAllocationDecider#canForceAllocatePrimary should return a NO decision because canAllocate returns NO here
        assertEquals(Decision.NO, new MaxRetryAllocationDecider(Settings.EMPTY).canForceAllocatePrimary(unassignedPrimary, null, new RoutingAllocation(null, null, clusterState, null, 0, false)));
    }
    // change the settings and ensure we can do another round of allocation for that index.
    clusterState = ClusterState.builder(clusterState).routingTable(routingTable).metaData(MetaData.builder(clusterState.metaData()).put(IndexMetaData.builder(clusterState.metaData().index("idx")).settings(Settings.builder().put(clusterState.metaData().index("idx").getSettings()).put("index.allocation.max_retries", retries + 1).build()).build(), true).build()).build();
    ClusterState newState = strategy.reroute(clusterState, "settings changed", false);
    assertThat(newState, not(equalTo(clusterState)));
    clusterState = newState;
    routingTable = newState.routingTable();
    // good we are initializing and we are maintaining failure information
    assertEquals(routingTable.index("idx").shards().size(), 1);
    ShardRouting unassignedPrimary = routingTable.index("idx").shard(0).shards().get(0);
    assertEquals(unassignedPrimary.unassignedInfo().getNumFailedAllocations(), retries);
    assertEquals(unassignedPrimary.state(), INITIALIZING);
    assertEquals(unassignedPrimary.unassignedInfo().getMessage(), "boom");
    // bumped up the max retry count, so canForceAllocatePrimary should return a YES decision
    assertEquals(Decision.YES, new MaxRetryAllocationDecider(Settings.EMPTY).canForceAllocatePrimary(routingTable.index("idx").shard(0).shards().get(0), null, new RoutingAllocation(null, null, clusterState, null, 0, false)));
    // now we start the shard
    clusterState = strategy.applyStartedShards(clusterState, Collections.singletonList(routingTable.index("idx").shard(0).shards().get(0)));
    routingTable = clusterState.routingTable();
    // all counters have been reset to 0 ie. no unassigned info
    assertEquals(routingTable.index("idx").shards().size(), 1);
    assertNull(routingTable.index("idx").shard(0).shards().get(0).unassignedInfo());
    assertEquals(routingTable.index("idx").shard(0).shards().get(0).state(), STARTED);
    // now fail again and see if it has a new counter
    List<FailedShard> failedShards = Collections.singletonList(new FailedShard(routingTable.index("idx").shard(0).shards().get(0), "ZOOOMG", new UnsupportedOperationException()));
    newState = strategy.applyFailedShards(clusterState, failedShards);
    assertThat(newState, not(equalTo(clusterState)));
    clusterState = newState;
    routingTable = newState.routingTable();
    assertEquals(routingTable.index("idx").shards().size(), 1);
    unassignedPrimary = routingTable.index("idx").shard(0).shards().get(0);
    assertEquals(unassignedPrimary.unassignedInfo().getNumFailedAllocations(), 1);
    assertEquals(unassignedPrimary.state(), UNASSIGNED);
    assertEquals(unassignedPrimary.unassignedInfo().getMessage(), "ZOOOMG");
    // Counter reset, so MaxRetryAllocationDecider#canForceAllocatePrimary should return a YES decision
    assertEquals(Decision.YES, new MaxRetryAllocationDecider(Settings.EMPTY).canForceAllocatePrimary(unassignedPrimary, null, new RoutingAllocation(null, null, clusterState, null, 0, false)));
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) List(java.util.List) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) MaxRetryAllocationDecider(org.elasticsearch.cluster.routing.allocation.decider.MaxRetryAllocationDecider)

Example 5 with MaxRetryAllocationDecider

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

the class TransportShrinkActionTests method testErrorCondition.

public void testErrorCondition() {
    ClusterState state = createClusterState("source", randomIntBetween(2, 42), randomIntBetween(0, 10), Settings.builder().put("index.blocks.write", true).build());
    assertTrue(expectThrows(IllegalStateException.class, () -> TransportShrinkAction.prepareCreateIndexRequest(new ShrinkRequest("target", "source"), state, (i) -> new DocsStats(Integer.MAX_VALUE, randomIntBetween(1, 1000)), new IndexNameExpressionResolver(Settings.EMPTY))).getMessage().startsWith("Can't merge index with more than [2147483519] docs - too many documents in shards "));
    assertTrue(expectThrows(IllegalStateException.class, () -> {
        ShrinkRequest req = new ShrinkRequest("target", "source");
        req.getShrinkIndexRequest().settings(Settings.builder().put("index.number_of_shards", 4));
        ClusterState clusterState = createClusterState("source", 8, 1, Settings.builder().put("index.blocks.write", true).build());
        TransportShrinkAction.prepareCreateIndexRequest(req, clusterState, (i) -> i == 2 || i == 3 ? new DocsStats(Integer.MAX_VALUE / 2, randomIntBetween(1, 1000)) : null, new IndexNameExpressionResolver(Settings.EMPTY));
    }).getMessage().startsWith("Can't merge index with more than [2147483519] docs - too many documents in shards "));
    // create one that won't fail
    ClusterState clusterState = ClusterState.builder(createClusterState("source", 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("source").shardsWithState(ShardRoutingState.INITIALIZING)).routingTable();
    clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build();
    TransportShrinkAction.prepareCreateIndexRequest(new ShrinkRequest("target", "source"), clusterState, (i) -> new DocsStats(randomIntBetween(1, 1000), randomIntBetween(1, 1000)), new IndexNameExpressionResolver(Settings.EMPTY));
}
Also used : MetaData(org.elasticsearch.cluster.metadata.MetaData) Arrays(java.util.Arrays) MaxRetryAllocationDecider(org.elasticsearch.cluster.routing.allocation.decider.MaxRetryAllocationDecider) AllocationService(org.elasticsearch.cluster.routing.allocation.AllocationService) ClusterBlocks(org.elasticsearch.cluster.block.ClusterBlocks) ShardRoutingState(org.elasticsearch.cluster.routing.ShardRoutingState) HashSet(java.util.HashSet) ClusterState(org.elasticsearch.cluster.ClusterState) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) Settings(org.elasticsearch.common.settings.Settings) EmptyClusterInfoService(org.elasticsearch.cluster.EmptyClusterInfoService) AllocationDeciders(org.elasticsearch.cluster.routing.allocation.decider.AllocationDeciders) ClusterName(org.elasticsearch.cluster.ClusterName) ESTestCase(org.elasticsearch.test.ESTestCase) CreateIndexClusterStateUpdateRequest(org.elasticsearch.action.admin.indices.create.CreateIndexClusterStateUpdateRequest) Collections.emptyMap(java.util.Collections.emptyMap) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes) BalancedShardsAllocator(org.elasticsearch.cluster.routing.allocation.allocator.BalancedShardsAllocator) TestGatewayAllocator(org.elasticsearch.test.gateway.TestGatewayAllocator) ActiveShardCount(org.elasticsearch.action.support.ActiveShardCount) IndexWriter(org.apache.lucene.index.IndexWriter) Version(org.elasticsearch.Version) DocsStats(org.elasticsearch.index.shard.DocsStats) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) IndexNameExpressionResolver(org.elasticsearch.cluster.metadata.IndexNameExpressionResolver) Collections(java.util.Collections) 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) DocsStats(org.elasticsearch.index.shard.DocsStats) IndexNameExpressionResolver(org.elasticsearch.cluster.metadata.IndexNameExpressionResolver) 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)8 AllocationService (org.elasticsearch.cluster.routing.allocation.AllocationService)7 BalancedShardsAllocator (org.elasticsearch.cluster.routing.allocation.allocator.BalancedShardsAllocator)7 AllocationDeciders (org.elasticsearch.cluster.routing.allocation.decider.AllocationDeciders)7 ClusterState (org.elasticsearch.cluster.ClusterState)6 RoutingTable (org.elasticsearch.cluster.routing.RoutingTable)6 TestGatewayAllocator (org.elasticsearch.test.gateway.TestGatewayAllocator)5 IndexNameExpressionResolver (org.elasticsearch.cluster.metadata.IndexNameExpressionResolver)4 Settings (org.elasticsearch.common.settings.Settings)4 List (java.util.List)3 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 Collections (java.util.Collections)2 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 Objects (java.util.Objects)2 Function (java.util.function.Function)2 Supplier (java.util.function.Supplier)2 Version (org.elasticsearch.Version)2