Search in sources :

Example 1 with BalancedShardsAllocator

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

use of org.elasticsearch.cluster.routing.allocation.allocator.BalancedShardsAllocator in project elasticsearch by elastic.

the class BalanceConfigurationTests method testPersistedSettings.

public void testPersistedSettings() {
    Settings.Builder settings = Settings.builder();
    settings.put(BalancedShardsAllocator.INDEX_BALANCE_FACTOR_SETTING.getKey(), 0.2);
    settings.put(BalancedShardsAllocator.SHARD_BALANCE_FACTOR_SETTING.getKey(), 0.3);
    settings.put(BalancedShardsAllocator.THRESHOLD_SETTING.getKey(), 2.0);
    ClusterSettings service = new ClusterSettings(Settings.builder().build(), ClusterSettings.BUILT_IN_CLUSTER_SETTINGS);
    BalancedShardsAllocator allocator = new BalancedShardsAllocator(settings.build(), service);
    assertThat(allocator.getIndexBalance(), Matchers.equalTo(0.2f));
    assertThat(allocator.getShardBalance(), Matchers.equalTo(0.3f));
    assertThat(allocator.getThreshold(), Matchers.equalTo(2.0f));
    settings = Settings.builder();
    settings.put(BalancedShardsAllocator.INDEX_BALANCE_FACTOR_SETTING.getKey(), 0.2);
    settings.put(BalancedShardsAllocator.SHARD_BALANCE_FACTOR_SETTING.getKey(), 0.3);
    settings.put(BalancedShardsAllocator.THRESHOLD_SETTING.getKey(), 2.0);
    settings.put(ClusterRebalanceAllocationDecider.CLUSTER_ROUTING_ALLOCATION_ALLOW_REBALANCE_SETTING.getKey(), ClusterRebalanceAllocationDecider.ClusterRebalanceType.ALWAYS.toString());
    service.applySettings(settings.build());
    assertThat(allocator.getIndexBalance(), Matchers.equalTo(0.2f));
    assertThat(allocator.getShardBalance(), Matchers.equalTo(0.3f));
    assertThat(allocator.getThreshold(), Matchers.equalTo(2.0f));
    settings = Settings.builder();
    settings.put(BalancedShardsAllocator.INDEX_BALANCE_FACTOR_SETTING.getKey(), 0.5);
    settings.put(BalancedShardsAllocator.SHARD_BALANCE_FACTOR_SETTING.getKey(), 0.1);
    settings.put(BalancedShardsAllocator.THRESHOLD_SETTING.getKey(), 3.0);
    service.applySettings(settings.build());
    assertThat(allocator.getIndexBalance(), Matchers.equalTo(0.5f));
    assertThat(allocator.getShardBalance(), Matchers.equalTo(0.1f));
    assertThat(allocator.getThreshold(), Matchers.equalTo(3.0f));
}
Also used : ClusterSettings(org.elasticsearch.common.settings.ClusterSettings) BalancedShardsAllocator(org.elasticsearch.cluster.routing.allocation.allocator.BalancedShardsAllocator) Settings(org.elasticsearch.common.settings.Settings) ClusterSettings(org.elasticsearch.common.settings.ClusterSettings)

Example 3 with BalancedShardsAllocator

use of org.elasticsearch.cluster.routing.allocation.allocator.BalancedShardsAllocator in project elasticsearch by elastic.

the class BalancedSingleShardTests method executeRebalanceFor.

private MoveDecision executeRebalanceFor(final ShardRouting shardRouting, final ClusterState clusterState, final Set<String> noDecisionNodes, final float threshold) {
    Settings settings = Settings.EMPTY;
    if (Float.compare(-1.0f, threshold) != 0) {
        settings = Settings.builder().put(BalancedShardsAllocator.THRESHOLD_SETTING.getKey(), threshold).build();
    }
    AllocationDecider allocationDecider = new AllocationDecider(Settings.EMPTY) {

        @Override
        public Decision canAllocate(ShardRouting shardRouting, RoutingNode node, RoutingAllocation allocation) {
            if (noDecisionNodes.contains(node.nodeId())) {
                return Decision.NO;
            }
            return Decision.YES;
        }
    };
    AllocationDecider rebalanceDecider = new AllocationDecider(Settings.EMPTY) {

        @Override
        public Decision canRebalance(ShardRouting shardRouting, RoutingAllocation allocation) {
            return Decision.YES;
        }
    };
    BalancedShardsAllocator allocator = new BalancedShardsAllocator(settings);
    RoutingAllocation routingAllocation = newRoutingAllocation(new AllocationDeciders(Settings.EMPTY, Arrays.asList(allocationDecider, rebalanceDecider)), clusterState);
    return allocator.decideShardAllocation(shardRouting, routingAllocation).getMoveDecision();
}
Also used : RoutingNode(org.elasticsearch.cluster.routing.RoutingNode) BalancedShardsAllocator(org.elasticsearch.cluster.routing.allocation.allocator.BalancedShardsAllocator) AllocationDeciders(org.elasticsearch.cluster.routing.allocation.decider.AllocationDeciders) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) Settings(org.elasticsearch.common.settings.Settings) AllocationDecider(org.elasticsearch.cluster.routing.allocation.decider.AllocationDecider)

Example 4 with BalancedShardsAllocator

use of org.elasticsearch.cluster.routing.allocation.allocator.BalancedShardsAllocator in project elasticsearch by elastic.

the class BalancedSingleShardTests method testRebalanceNotAllowedDuringPendingAsyncFetch.

public void testRebalanceNotAllowedDuringPendingAsyncFetch() {
    BalancedShardsAllocator allocator = new BalancedShardsAllocator(Settings.EMPTY);
    ClusterState clusterState = ClusterStateCreationUtils.state("idx", randomBoolean(), ShardRoutingState.STARTED);
    ShardRouting shard = clusterState.routingTable().index("idx").shard(0).primaryShard();
    RoutingAllocation routingAllocation = newRoutingAllocation(new AllocationDeciders(Settings.EMPTY, Collections.emptyList()), clusterState);
    routingAllocation.setHasPendingAsyncFetch();
    MoveDecision rebalanceDecision = allocator.decideShardAllocation(shard, routingAllocation).getMoveDecision();
    assertNotNull(rebalanceDecision.getClusterRebalanceDecision());
    assertEquals(AllocationDecision.AWAITING_INFO, rebalanceDecision.getAllocationDecision());
    assertThat(rebalanceDecision.getExplanation(), startsWith("cannot rebalance as information about existing copies of this shard in the cluster is still being gathered"));
    assertEquals(clusterState.nodes().getSize() - 1, rebalanceDecision.getNodeDecisions().size());
    assertNull(rebalanceDecision.getTargetNode());
    assertAssignedNodeRemainsSame(allocator, routingAllocation, shard);
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) BalancedShardsAllocator(org.elasticsearch.cluster.routing.allocation.allocator.BalancedShardsAllocator) AllocationDeciders(org.elasticsearch.cluster.routing.allocation.decider.AllocationDeciders) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting)

Example 5 with BalancedShardsAllocator

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

Aggregations

BalancedShardsAllocator (org.elasticsearch.cluster.routing.allocation.allocator.BalancedShardsAllocator)24 ClusterState (org.elasticsearch.cluster.ClusterState)21 TestGatewayAllocator (org.elasticsearch.test.gateway.TestGatewayAllocator)16 RoutingTable (org.elasticsearch.cluster.routing.RoutingTable)15 AllocationDeciders (org.elasticsearch.cluster.routing.allocation.decider.AllocationDeciders)15 AllocationService (org.elasticsearch.cluster.routing.allocation.AllocationService)12 IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)11 MetaData (org.elasticsearch.cluster.metadata.MetaData)11 Settings (org.elasticsearch.common.settings.Settings)11 ShardRouting (org.elasticsearch.cluster.routing.ShardRouting)10 ClusterSettings (org.elasticsearch.common.settings.ClusterSettings)10 IndexRoutingTable (org.elasticsearch.cluster.routing.IndexRoutingTable)8 IndexShardRoutingTable (org.elasticsearch.cluster.routing.IndexShardRoutingTable)8 Matchers.containsString (org.hamcrest.Matchers.containsString)8 ClusterInfo (org.elasticsearch.cluster.ClusterInfo)7 ClusterInfoService (org.elasticsearch.cluster.ClusterInfoService)7 DiskUsage (org.elasticsearch.cluster.DiskUsage)7 DevNullClusterInfo (org.elasticsearch.cluster.MockInternalClusterInfoService.DevNullClusterInfo)7 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)7 DiskThresholdSettings (org.elasticsearch.cluster.routing.allocation.DiskThresholdSettings)7