Search in sources :

Example 46 with RoutingTable

use of org.opensearch.cluster.routing.RoutingTable in project OpenSearch by opensearch-project.

the class MaxRetryAllocationDeciderTests method createInitialClusterState.

private ClusterState createInitialClusterState() {
    Metadata.Builder metaBuilder = Metadata.builder();
    metaBuilder.put(IndexMetadata.builder("idx").settings(settings(Version.CURRENT)).numberOfShards(1).numberOfReplicas(0));
    Metadata metadata = metaBuilder.build();
    RoutingTable.Builder routingTableBuilder = RoutingTable.builder();
    routingTableBuilder.addAsNew(metadata.index("idx"));
    RoutingTable routingTable = routingTableBuilder.build();
    ClusterState clusterState = ClusterState.builder(ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metadata(metadata).routingTable(routingTable).build();
    clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder().add(newNode("node1")).add(newNode("node2"))).build();
    RoutingTable prevRoutingTable = routingTable;
    routingTable = strategy.reroute(clusterState, "reroute").routingTable();
    clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build();
    assertEquals(prevRoutingTable.index("idx").shards().size(), 1);
    assertEquals(prevRoutingTable.index("idx").shard(0).shards().get(0).state(), UNASSIGNED);
    assertEquals(routingTable.index("idx").shards().size(), 1);
    assertEquals(routingTable.index("idx").shard(0).shards().get(0).state(), INITIALIZING);
    return clusterState;
}
Also used : ClusterState(org.opensearch.cluster.ClusterState) RoutingTable(org.opensearch.cluster.routing.RoutingTable) Metadata(org.opensearch.cluster.metadata.Metadata) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata)

Example 47 with RoutingTable

use of org.opensearch.cluster.routing.RoutingTable in project OpenSearch by opensearch-project.

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(), randomBoolean()));
        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);
        assertThat(unassignedPrimary.unassignedInfo().getMessage(), containsString("boom" + i));
        // MaxRetryAllocationDecider#canForceAllocatePrimary should return YES decisions because canAllocate returns YES here
        assertEquals(Decision.YES, new MaxRetryAllocationDecider().canForceAllocatePrimary(unassignedPrimary, null, new RoutingAllocation(null, null, clusterState, null, null, 0)));
    }
    // 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(), randomBoolean()));
        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);
        assertThat(unassignedPrimary.unassignedInfo().getMessage(), containsString("boom"));
        // MaxRetryAllocationDecider#canForceAllocatePrimary should return a NO decision because canAllocate returns NO here
        assertEquals(Decision.NO, new MaxRetryAllocationDecider().canForceAllocatePrimary(unassignedPrimary, null, new RoutingAllocation(null, null, clusterState, null, null, 0)));
    }
    // 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");
    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);
    assertThat(unassignedPrimary.unassignedInfo().getMessage(), containsString("boom"));
    // bumped up the max retry count, so canForceAllocatePrimary should return a YES decision
    assertEquals(Decision.YES, new MaxRetryAllocationDecider().canForceAllocatePrimary(routingTable.index("idx").shard(0).shards().get(0), null, new RoutingAllocation(null, null, clusterState, null, null, 0)));
    // now we start the shard
    clusterState = startShardsAndReroute(strategy, clusterState, 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(), randomBoolean()));
    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);
    assertThat(unassignedPrimary.unassignedInfo().getMessage(), containsString("ZOOOMG"));
    // Counter reset, so MaxRetryAllocationDecider#canForceAllocatePrimary should return a YES decision
    assertEquals(Decision.YES, new MaxRetryAllocationDecider().canForceAllocatePrimary(unassignedPrimary, null, new RoutingAllocation(null, null, clusterState, null, null, 0)));
}
Also used : ClusterState(org.opensearch.cluster.ClusterState) RoutingTable(org.opensearch.cluster.routing.RoutingTable) List(java.util.List) ShardRouting(org.opensearch.cluster.routing.ShardRouting) MaxRetryAllocationDecider(org.opensearch.cluster.routing.allocation.decider.MaxRetryAllocationDecider)

Example 48 with RoutingTable

use of org.opensearch.cluster.routing.RoutingTable in project OpenSearch by opensearch-project.

the class AddIncrementallyTests method addIndex.

private ClusterState addIndex(ClusterState clusterState, AllocationService service, int indexOrdinal, int numberOfShards, int numberOfReplicas) {
    Metadata.Builder metadataBuilder = Metadata.builder(clusterState.getMetadata());
    RoutingTable.Builder routingTableBuilder = RoutingTable.builder(clusterState.routingTable());
    IndexMetadata.Builder index = IndexMetadata.builder("test" + indexOrdinal).settings(settings(Version.CURRENT)).numberOfShards(numberOfShards).numberOfReplicas(numberOfReplicas);
    IndexMetadata imd = index.build();
    metadataBuilder = metadataBuilder.put(imd, true);
    routingTableBuilder.addAsNew(imd);
    Metadata metadata = metadataBuilder.build();
    clusterState = ClusterState.builder(clusterState).metadata(metadata).routingTable(routingTableBuilder.build()).build();
    clusterState = service.reroute(clusterState, "reroute");
    logger.info("restart all the primary shards, replicas will start initializing");
    clusterState = startInitializingShardsAndReroute(service, clusterState);
    logger.info("start the replica shards");
    clusterState = startInitializingShardsAndReroute(service, clusterState);
    logger.info("complete rebalancing");
    return applyStartedShardsUntilNoChange(clusterState, service);
}
Also used : RoutingTable(org.opensearch.cluster.routing.RoutingTable) Metadata(org.opensearch.cluster.metadata.Metadata) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata)

Example 49 with RoutingTable

use of org.opensearch.cluster.routing.RoutingTable in project OpenSearch by opensearch-project.

the class AddIncrementallyTests method initCluster.

private ClusterState initCluster(AllocationService service, int numberOfNodes, int numberOfIndices, int numberOfShards, int numberOfReplicas) {
    Metadata.Builder metadataBuilder = Metadata.builder();
    RoutingTable.Builder routingTableBuilder = RoutingTable.builder();
    for (int i = 0; i < numberOfIndices; i++) {
        IndexMetadata.Builder index = IndexMetadata.builder("test" + i).settings(settings(Version.CURRENT)).numberOfShards(numberOfShards).numberOfReplicas(numberOfReplicas);
        metadataBuilder = metadataBuilder.put(index);
    }
    Metadata metadata = metadataBuilder.build();
    for (ObjectCursor<IndexMetadata> cursor : metadata.indices().values()) {
        routingTableBuilder.addAsNew(cursor.value);
    }
    RoutingTable initialRoutingTable = routingTableBuilder.build();
    logger.info("start {} nodes", numberOfNodes);
    DiscoveryNodes.Builder nodes = DiscoveryNodes.builder();
    for (int i = 0; i < numberOfNodes; i++) {
        nodes.add(newNode("node" + i));
    }
    ClusterState clusterState = ClusterState.builder(org.opensearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).nodes(nodes).metadata(metadata).routingTable(initialRoutingTable).build();
    clusterState = service.reroute(clusterState, "reroute");
    logger.info("restart all the primary shards, replicas will start initializing");
    clusterState = startInitializingShardsAndReroute(service, clusterState);
    logger.info("start the replica shards");
    clusterState = startInitializingShardsAndReroute(service, clusterState);
    logger.info("complete rebalancing");
    return applyStartedShardsUntilNoChange(clusterState, service);
}
Also used : ClusterState(org.opensearch.cluster.ClusterState) RoutingTable(org.opensearch.cluster.routing.RoutingTable) Metadata(org.opensearch.cluster.metadata.Metadata) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) DiscoveryNodes(org.opensearch.cluster.node.DiscoveryNodes)

Example 50 with RoutingTable

use of org.opensearch.cluster.routing.RoutingTable in project OpenSearch by opensearch-project.

the class BalanceConfigurationTests method addNode.

private ClusterState addNode(ClusterState clusterState, AllocationService strategy) {
    logger.info("now, start 1 more node, check that rebalancing will happen because we set it to always");
    clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder(clusterState.nodes()).add(newNode("node" + numberOfNodes))).build();
    RoutingTable routingTable = strategy.reroute(clusterState, "reroute").routingTable();
    clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build();
    // move initializing to started
    return applyStartedShardsUntilNoChange(clusterState, strategy);
}
Also used : RoutingTable(org.opensearch.cluster.routing.RoutingTable)

Aggregations

RoutingTable (org.opensearch.cluster.routing.RoutingTable)227 ClusterState (org.opensearch.cluster.ClusterState)193 IndexMetadata (org.opensearch.cluster.metadata.IndexMetadata)190 Metadata (org.opensearch.cluster.metadata.Metadata)187 ShardRouting (org.opensearch.cluster.routing.ShardRouting)81 IndexShardRoutingTable (org.opensearch.cluster.routing.IndexShardRoutingTable)58 IndexRoutingTable (org.opensearch.cluster.routing.IndexRoutingTable)55 RoutingNodes (org.opensearch.cluster.routing.RoutingNodes)42 AllocationService (org.opensearch.cluster.routing.allocation.AllocationService)42 ShardId (org.opensearch.index.shard.ShardId)35 DiscoveryNodes (org.opensearch.cluster.node.DiscoveryNodes)34 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)33 Settings (org.opensearch.common.settings.Settings)33 Index (org.opensearch.index.Index)30 HashSet (java.util.HashSet)29 TestGatewayAllocator (org.opensearch.test.gateway.TestGatewayAllocator)29 ImmutableOpenMap (org.opensearch.common.collect.ImmutableOpenMap)28 ClusterSettings (org.opensearch.common.settings.ClusterSettings)28 RoutingNode (org.opensearch.cluster.routing.RoutingNode)24 BalancedShardsAllocator (org.opensearch.cluster.routing.allocation.allocator.BalancedShardsAllocator)23