Search in sources :

Example 1 with TestGatewayAllocator

use of org.opensearch.test.gateway.TestGatewayAllocator 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 TestGatewayAllocator

use of org.opensearch.test.gateway.TestGatewayAllocator 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 TestGatewayAllocator

use of org.opensearch.test.gateway.TestGatewayAllocator in project OpenSearch by opensearch-project.

the class ClusterRebalanceRoutingTests method testRebalanceWithIgnoredUnassignedShards.

public void testRebalanceWithIgnoredUnassignedShards() {
    final AtomicBoolean allocateTest1 = new AtomicBoolean(false);
    AllocationService strategy = createAllocationService(Settings.EMPTY, new TestGatewayAllocator() {

        @Override
        public void allocateUnassigned(ShardRouting shardRouting, RoutingAllocation allocation, UnassignedAllocationHandler unassignedAllocationHandler) {
            if (allocateTest1.get() == false && "test1".equals(shardRouting.index().getName())) {
                unassignedAllocationHandler.removeAndIgnore(UnassignedInfo.AllocationStatus.NO_ATTEMPT, allocation.changes());
            } else {
                super.allocateUnassigned(shardRouting, allocation, unassignedAllocationHandler);
            }
        }
    });
    Metadata metadata = Metadata.builder().put(IndexMetadata.builder("test").settings(settings(Version.CURRENT)).numberOfShards(2).numberOfReplicas(0)).put(IndexMetadata.builder("test1").settings(settings(Version.CURRENT)).numberOfShards(2).numberOfReplicas(0)).build();
    RoutingTable initialRoutingTable = RoutingTable.builder().addAsNew(metadata.index("test")).addAsNew(metadata.index("test1")).build();
    ClusterState clusterState = ClusterState.builder(org.opensearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metadata(metadata).routingTable(initialRoutingTable).build();
    logger.info("start two nodes");
    clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder().add(newNode("node1"))).build();
    clusterState = strategy.reroute(clusterState, "reroute");
    for (int i = 0; i < clusterState.routingTable().index("test").shards().size(); i++) {
        assertThat(clusterState.routingTable().index("test").shard(i).shards().size(), equalTo(1));
        assertThat(clusterState.routingTable().index("test").shard(i).primaryShard().state(), equalTo(INITIALIZING));
    }
    logger.debug("start all the primary shards for test");
    clusterState = startInitializingShardsAndReroute(strategy, clusterState, "test");
    for (int i = 0; i < clusterState.routingTable().index("test").shards().size(); i++) {
        assertThat(clusterState.routingTable().index("test").shard(i).shards().size(), equalTo(1));
        assertThat(clusterState.routingTable().index("test").shard(i).primaryShard().state(), equalTo(STARTED));
    }
    logger.debug("now, start 1 more node, check that rebalancing will not happen since we unassigned shards");
    clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder(clusterState.nodes()).add(newNode("node2"))).build();
    logger.debug("reroute and check that nothing has changed");
    ClusterState resultingState = strategy.reroute(clusterState, "reroute");
    assertThat(resultingState, equalTo(clusterState));
    for (int i = 0; i < clusterState.routingTable().index("test").shards().size(); i++) {
        assertThat(clusterState.routingTable().index("test").shard(i).shards().size(), equalTo(1));
        assertThat(clusterState.routingTable().index("test").shard(i).primaryShard().state(), equalTo(STARTED));
    }
    for (int i = 0; i < clusterState.routingTable().index("test1").shards().size(); i++) {
        assertThat(clusterState.routingTable().index("test1").shard(i).shards().size(), equalTo(1));
        assertThat(clusterState.routingTable().index("test1").shard(i).primaryShard().state(), equalTo(UNASSIGNED));
    }
    logger.debug("now set allocateTest1 to true and reroute we should see the [test1] index initializing");
    allocateTest1.set(true);
    resultingState = strategy.reroute(clusterState, "reroute");
    assertThat(resultingState, not(equalTo(clusterState)));
    clusterState = resultingState;
    for (int i = 0; i < clusterState.routingTable().index("test1").shards().size(); i++) {
        assertThat(clusterState.routingTable().index("test1").shard(i).shards().size(), equalTo(1));
        assertThat(clusterState.routingTable().index("test1").shard(i).primaryShard().state(), equalTo(INITIALIZING));
    }
    logger.debug("now start initializing shards and expect exactly one rebalance" + " from node1 to node 2 since index [test] is all on node1");
    clusterState = startInitializingShardsAndReroute(strategy, clusterState, "test1");
    for (int i = 0; i < clusterState.routingTable().index("test1").shards().size(); i++) {
        assertThat(clusterState.routingTable().index("test1").shard(i).shards().size(), equalTo(1));
        assertThat(clusterState.routingTable().index("test1").shard(i).primaryShard().state(), equalTo(STARTED));
    }
    int numStarted = 0;
    int numRelocating = 0;
    for (int i = 0; i < clusterState.routingTable().index("test").shards().size(); i++) {
        assertThat(clusterState.routingTable().index("test").shard(i).shards().size(), equalTo(1));
        if (clusterState.routingTable().index("test").shard(i).primaryShard().state() == STARTED) {
            numStarted++;
        } else if (clusterState.routingTable().index("test").shard(i).primaryShard().state() == RELOCATING) {
            numRelocating++;
        }
    }
    assertEquals(numStarted, 1);
    assertEquals(numRelocating, 1);
}
Also used : TestGatewayAllocator(org.opensearch.test.gateway.TestGatewayAllocator) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ClusterState(org.opensearch.cluster.ClusterState) RoutingTable(org.opensearch.cluster.routing.RoutingTable) Metadata(org.opensearch.cluster.metadata.Metadata) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) ShardRouting(org.opensearch.cluster.routing.ShardRouting)

Example 4 with TestGatewayAllocator

use of org.opensearch.test.gateway.TestGatewayAllocator in project OpenSearch by opensearch-project.

the class ClusterRebalanceRoutingTests method testRebalanceWhileShardFetching.

public void testRebalanceWhileShardFetching() {
    final AtomicBoolean hasFetches = new AtomicBoolean(true);
    AllocationService strategy = createAllocationService(Settings.builder().put(ClusterRebalanceAllocationDecider.CLUSTER_ROUTING_ALLOCATION_ALLOW_REBALANCE_SETTING.getKey(), ClusterRebalanceAllocationDecider.ClusterRebalanceType.ALWAYS.toString()).build(), new TestGatewayAllocator() {

        @Override
        public void beforeAllocation(RoutingAllocation allocation) {
            if (hasFetches.get()) {
                allocation.setHasPendingAsyncFetch();
            }
        }
    });
    Metadata metadata = Metadata.builder().put(IndexMetadata.builder("test").settings(settings(Version.CURRENT)).numberOfShards(2).numberOfReplicas(0)).put(IndexMetadata.builder("test1").settings(settings(Version.CURRENT).put(IndexMetadata.INDEX_ROUTING_EXCLUDE_GROUP_SETTING.getKey() + "_id", "node1,node2")).numberOfShards(2).numberOfReplicas(0)).build();
    // we use a second index here (test1) that never gets assigned otherwise allocateUnassigned
    // is never called if we don't have unassigned shards.
    RoutingTable initialRoutingTable = RoutingTable.builder().addAsNew(metadata.index("test")).addAsNew(metadata.index("test1")).build();
    ClusterState clusterState = ClusterState.builder(org.opensearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metadata(metadata).routingTable(initialRoutingTable).build();
    logger.info("start two nodes");
    clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder().add(newNode("node1"))).build();
    clusterState = strategy.reroute(clusterState, "reroute");
    for (int i = 0; i < clusterState.routingTable().index("test").shards().size(); i++) {
        assertThat(clusterState.routingTable().index("test").shard(i).shards().size(), equalTo(1));
        assertThat(clusterState.routingTable().index("test").shard(i).primaryShard().state(), equalTo(INITIALIZING));
    }
    logger.debug("start all the primary shards for test");
    clusterState = startInitializingShardsAndReroute(strategy, clusterState, "test");
    for (int i = 0; i < clusterState.routingTable().index("test").shards().size(); i++) {
        assertThat(clusterState.routingTable().index("test").shard(i).shards().size(), equalTo(1));
        assertThat(clusterState.routingTable().index("test").shard(i).primaryShard().state(), equalTo(STARTED));
    }
    logger.debug("now, start 1 more node, check that rebalancing will not happen since we have shard sync going on");
    clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder(clusterState.nodes()).add(newNode("node2"))).build();
    logger.debug("reroute and check that nothing has changed");
    ClusterState resultState = strategy.reroute(clusterState, "reroute");
    assertThat(resultState, equalTo(clusterState));
    for (int i = 0; i < clusterState.routingTable().index("test").shards().size(); i++) {
        assertThat(clusterState.routingTable().index("test").shard(i).shards().size(), equalTo(1));
        assertThat(clusterState.routingTable().index("test").shard(i).primaryShard().state(), equalTo(STARTED));
    }
    for (int i = 0; i < clusterState.routingTable().index("test1").shards().size(); i++) {
        assertThat(clusterState.routingTable().index("test1").shard(i).shards().size(), equalTo(1));
        assertThat(clusterState.routingTable().index("test1").shard(i).primaryShard().state(), equalTo(UNASSIGNED));
    }
    logger.debug("now set hasFetches to true and reroute we should now see exactly one relocating shard");
    hasFetches.set(false);
    resultState = strategy.reroute(clusterState, "reroute");
    assertThat(resultState, not(equalTo(clusterState)));
    clusterState = resultState;
    int numStarted = 0;
    int numRelocating = 0;
    for (int i = 0; i < clusterState.routingTable().index("test").shards().size(); i++) {
        assertThat(clusterState.routingTable().index("test").shard(i).shards().size(), equalTo(1));
        if (clusterState.routingTable().index("test").shard(i).primaryShard().state() == STARTED) {
            numStarted++;
        } else if (clusterState.routingTable().index("test").shard(i).primaryShard().state() == RELOCATING) {
            numRelocating++;
        }
    }
    for (int i = 0; i < clusterState.routingTable().index("test1").shards().size(); i++) {
        assertThat(clusterState.routingTable().index("test1").shard(i).shards().size(), equalTo(1));
        assertThat(clusterState.routingTable().index("test1").shard(i).primaryShard().state(), equalTo(UNASSIGNED));
    }
    assertEquals(numStarted, 1);
    assertEquals(numRelocating, 1);
}
Also used : TestGatewayAllocator(org.opensearch.test.gateway.TestGatewayAllocator) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ClusterState(org.opensearch.cluster.ClusterState) RoutingTable(org.opensearch.cluster.routing.RoutingTable) Metadata(org.opensearch.cluster.metadata.Metadata) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata)

Example 5 with TestGatewayAllocator

use of org.opensearch.test.gateway.TestGatewayAllocator in project OpenSearch by opensearch-project.

the class BalanceConfigurationTests method testReplicaBalance.

public void testReplicaBalance() {
    /* Tests balance over replicas only */
    final float indexBalance = 0.0f;
    final float replicaBalance = 1.0f;
    final float balanceThreshold = 1.0f;
    Settings.Builder settings = Settings.builder();
    settings.put(ClusterRebalanceAllocationDecider.CLUSTER_ROUTING_ALLOCATION_ALLOW_REBALANCE_SETTING.getKey(), ClusterRebalanceAllocationDecider.ClusterRebalanceType.ALWAYS.toString());
    settings.put(BalancedShardsAllocator.INDEX_BALANCE_FACTOR_SETTING.getKey(), indexBalance);
    settings.put(BalancedShardsAllocator.SHARD_BALANCE_FACTOR_SETTING.getKey(), replicaBalance);
    settings.put(BalancedShardsAllocator.THRESHOLD_SETTING.getKey(), balanceThreshold);
    AllocationService strategy = createAllocationService(settings.build(), new TestGatewayAllocator());
    ClusterState clusterState = initCluster(strategy);
    assertReplicaBalance(clusterState.getRoutingNodes(), numberOfNodes, numberOfIndices, numberOfReplicas, numberOfShards, balanceThreshold);
    clusterState = addNode(clusterState, strategy);
    assertReplicaBalance(clusterState.getRoutingNodes(), numberOfNodes + 1, numberOfIndices, numberOfReplicas, numberOfShards, balanceThreshold);
    clusterState = removeNodes(clusterState, strategy);
    assertReplicaBalance(clusterState.getRoutingNodes(), numberOfNodes + 1 - (numberOfNodes + 1) / 2, numberOfIndices, numberOfReplicas, numberOfShards, balanceThreshold);
}
Also used : TestGatewayAllocator(org.opensearch.test.gateway.TestGatewayAllocator) ClusterState(org.opensearch.cluster.ClusterState) ClusterSettings(org.opensearch.common.settings.ClusterSettings) Settings(org.opensearch.common.settings.Settings)

Aggregations

TestGatewayAllocator (org.opensearch.test.gateway.TestGatewayAllocator)43 ClusterState (org.opensearch.cluster.ClusterState)41 RoutingTable (org.opensearch.cluster.routing.RoutingTable)30 IndexMetadata (org.opensearch.cluster.metadata.IndexMetadata)26 Metadata (org.opensearch.cluster.metadata.Metadata)26 BalancedShardsAllocator (org.opensearch.cluster.routing.allocation.allocator.BalancedShardsAllocator)26 AllocationService (org.opensearch.cluster.routing.allocation.AllocationService)23 Settings (org.opensearch.common.settings.Settings)20 ClusterSettings (org.opensearch.common.settings.ClusterSettings)19 AllocationDeciders (org.opensearch.cluster.routing.allocation.decider.AllocationDeciders)15 IndexRoutingTable (org.opensearch.cluster.routing.IndexRoutingTable)14 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)13 ShardRouting (org.opensearch.cluster.routing.ShardRouting)13 IndexShardRoutingTable (org.opensearch.cluster.routing.IndexShardRoutingTable)12 ImmutableOpenMap (org.opensearch.common.collect.ImmutableOpenMap)11 Matchers.containsString (org.hamcrest.Matchers.containsString)10 DiscoveryNodes (org.opensearch.cluster.node.DiscoveryNodes)10 ClusterInfo (org.opensearch.cluster.ClusterInfo)9 ClusterInfoService (org.opensearch.cluster.ClusterInfoService)9 DiskUsage (org.opensearch.cluster.DiskUsage)9