Search in sources :

Example 1 with BalancedShardsAllocator

use of org.opensearch.cluster.routing.allocation.allocator.BalancedShardsAllocator in project OpenSearch by opensearch-project.

the class TransportResizeActionTests method testPassNumRoutingShardsAndFail.

public void testPassNumRoutingShardsAndFail() {
    int numShards = randomIntBetween(2, 100);
    ClusterState clusterState = ClusterState.builder(createClusterState("source", numShards, 0, numShards * 4, Settings.builder().put("index.blocks.write", true).build())).nodes(DiscoveryNodes.builder().add(newNode("node1"))).build();
    AllocationService service = new AllocationService(new AllocationDeciders(Collections.singleton(new MaxRetryAllocationDecider())), new TestGatewayAllocator(), new BalancedShardsAllocator(Settings.EMPTY), EmptyClusterInfoService.INSTANCE, EmptySnapshotsInfoService.INSTANCE);
    RoutingTable routingTable = service.reroute(clusterState, "reroute").routingTable();
    clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build();
    // now we start the shard
    routingTable = OpenSearchAllocationTestCase.startInitializingShardsAndReroute(service, clusterState, "source").routingTable();
    clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build();
    ResizeRequest resizeRequest = new ResizeRequest("target", "source");
    resizeRequest.setResizeType(ResizeType.SPLIT);
    resizeRequest.getTargetIndexRequest().settings(Settings.builder().put("index.number_of_shards", numShards * 2).build());
    TransportResizeAction.prepareCreateIndexRequest(resizeRequest, clusterState, null, "source", "target");
    resizeRequest.getTargetIndexRequest().settings(Settings.builder().put("index.number_of_shards", numShards * 2).put("index.number_of_routing_shards", numShards * 2).build());
    ClusterState finalState = clusterState;
    IllegalArgumentException iae = expectThrows(IllegalArgumentException.class, () -> TransportResizeAction.prepareCreateIndexRequest(resizeRequest, finalState, null, "source", "target"));
    assertEquals("cannot provide index.number_of_routing_shards on resize", iae.getMessage());
}
Also used : TestGatewayAllocator(org.opensearch.test.gateway.TestGatewayAllocator) ClusterState(org.opensearch.cluster.ClusterState) RoutingTable(org.opensearch.cluster.routing.RoutingTable) BalancedShardsAllocator(org.opensearch.cluster.routing.allocation.allocator.BalancedShardsAllocator) AllocationDeciders(org.opensearch.cluster.routing.allocation.decider.AllocationDeciders) MaxRetryAllocationDecider(org.opensearch.cluster.routing.allocation.decider.MaxRetryAllocationDecider) AllocationService(org.opensearch.cluster.routing.allocation.AllocationService)

Example 2 with BalancedShardsAllocator

use of org.opensearch.cluster.routing.allocation.allocator.BalancedShardsAllocator in project OpenSearch by opensearch-project.

the class TransportResizeActionTests method testPassNumRoutingShards.

public void testPassNumRoutingShards() {
    ClusterState clusterState = ClusterState.builder(createClusterState("source", 1, 0, Settings.builder().put("index.blocks.write", true).build())).nodes(DiscoveryNodes.builder().add(newNode("node1"))).build();
    AllocationService service = new AllocationService(new AllocationDeciders(Collections.singleton(new MaxRetryAllocationDecider())), new TestGatewayAllocator(), new BalancedShardsAllocator(Settings.EMPTY), EmptyClusterInfoService.INSTANCE, EmptySnapshotsInfoService.INSTANCE);
    RoutingTable routingTable = service.reroute(clusterState, "reroute").routingTable();
    clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build();
    // now we start the shard
    routingTable = OpenSearchAllocationTestCase.startInitializingShardsAndReroute(service, clusterState, "source").routingTable();
    clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build();
    ResizeRequest resizeRequest = new ResizeRequest("target", "source");
    resizeRequest.setResizeType(ResizeType.SPLIT);
    resizeRequest.getTargetIndexRequest().settings(Settings.builder().put("index.number_of_shards", 2).build());
    TransportResizeAction.prepareCreateIndexRequest(resizeRequest, clusterState, null, "source", "target");
    resizeRequest.getTargetIndexRequest().settings(Settings.builder().put("index.number_of_routing_shards", randomIntBetween(2, 10)).put("index.number_of_shards", 2).build());
    TransportResizeAction.prepareCreateIndexRequest(resizeRequest, clusterState, null, "source", "target");
}
Also used : TestGatewayAllocator(org.opensearch.test.gateway.TestGatewayAllocator) ClusterState(org.opensearch.cluster.ClusterState) RoutingTable(org.opensearch.cluster.routing.RoutingTable) BalancedShardsAllocator(org.opensearch.cluster.routing.allocation.allocator.BalancedShardsAllocator) AllocationDeciders(org.opensearch.cluster.routing.allocation.decider.AllocationDeciders) MaxRetryAllocationDecider(org.opensearch.cluster.routing.allocation.decider.MaxRetryAllocationDecider) AllocationService(org.opensearch.cluster.routing.allocation.AllocationService)

Example 3 with BalancedShardsAllocator

use of org.opensearch.cluster.routing.allocation.allocator.BalancedShardsAllocator in project OpenSearch by opensearch-project.

the class AllocationConstraintsTests method testSettings.

public void testSettings() {
    Settings.Builder settings = Settings.builder();
    ClusterSettings service = new ClusterSettings(Settings.builder().build(), ClusterSettings.BUILT_IN_CLUSTER_SETTINGS);
    BalancedShardsAllocator allocator = new BalancedShardsAllocator(settings.build(), service);
    settings = Settings.builder();
    float indexBalanceFactor = randomFloat();
    float shardBalance = randomFloat();
    float threshold = randomFloat();
    settings.put(BalancedShardsAllocator.INDEX_BALANCE_FACTOR_SETTING.getKey(), indexBalanceFactor);
    settings.put(BalancedShardsAllocator.SHARD_BALANCE_FACTOR_SETTING.getKey(), shardBalance);
    settings.put(BalancedShardsAllocator.THRESHOLD_SETTING.getKey(), threshold);
    service.applySettings(settings.build());
    assertEquals(indexBalanceFactor, allocator.getIndexBalance(), 0.01);
    assertEquals(shardBalance, allocator.getShardBalance(), 0.01);
    assertEquals(threshold, allocator.getThreshold(), 0.01);
}
Also used : ClusterSettings(org.opensearch.common.settings.ClusterSettings) BalancedShardsAllocator(org.opensearch.cluster.routing.allocation.allocator.BalancedShardsAllocator) Settings(org.opensearch.common.settings.Settings) ClusterSettings(org.opensearch.common.settings.ClusterSettings)

Example 4 with BalancedShardsAllocator

use of org.opensearch.cluster.routing.allocation.allocator.BalancedShardsAllocator in project OpenSearch by opensearch-project.

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.opensearch.common.settings.ClusterSettings) BalancedShardsAllocator(org.opensearch.cluster.routing.allocation.allocator.BalancedShardsAllocator) ClusterSettings(org.opensearch.common.settings.ClusterSettings) Settings(org.opensearch.common.settings.Settings)

Example 5 with BalancedShardsAllocator

use of org.opensearch.cluster.routing.allocation.allocator.BalancedShardsAllocator in project OpenSearch by opensearch-project.

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(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.opensearch.cluster.ClusterState) BalancedShardsAllocator(org.opensearch.cluster.routing.allocation.allocator.BalancedShardsAllocator) AllocationDeciders(org.opensearch.cluster.routing.allocation.decider.AllocationDeciders) ShardRouting(org.opensearch.cluster.routing.ShardRouting)

Aggregations

BalancedShardsAllocator (org.opensearch.cluster.routing.allocation.allocator.BalancedShardsAllocator)34 ClusterState (org.opensearch.cluster.ClusterState)28 TestGatewayAllocator (org.opensearch.test.gateway.TestGatewayAllocator)25 RoutingTable (org.opensearch.cluster.routing.RoutingTable)22 AllocationService (org.opensearch.cluster.routing.allocation.AllocationService)21 AllocationDeciders (org.opensearch.cluster.routing.allocation.decider.AllocationDeciders)18 ClusterSettings (org.opensearch.common.settings.ClusterSettings)17 Settings (org.opensearch.common.settings.Settings)16 IndexMetadata (org.opensearch.cluster.metadata.IndexMetadata)13 Metadata (org.opensearch.cluster.metadata.Metadata)13 ShardRouting (org.opensearch.cluster.routing.ShardRouting)13 Matchers.containsString (org.hamcrest.Matchers.containsString)10 IndexRoutingTable (org.opensearch.cluster.routing.IndexRoutingTable)10 IndexShardRoutingTable (org.opensearch.cluster.routing.IndexShardRoutingTable)10 ImmutableOpenMap (org.opensearch.common.collect.ImmutableOpenMap)10 ClusterInfo (org.opensearch.cluster.ClusterInfo)9 ClusterInfoService (org.opensearch.cluster.ClusterInfoService)9 DiskUsage (org.opensearch.cluster.DiskUsage)9 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)9 DiskThresholdSettings (org.opensearch.cluster.routing.allocation.DiskThresholdSettings)9