Search in sources :

Example 6 with IndexRoutingTable

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

the class ClusterStateDiffIT method randomIndexRoutingTable.

/**
 * Randomly updates index routing table in the cluster state
 */
private IndexRoutingTable randomIndexRoutingTable(String index, String[] nodeIds) {
    IndexRoutingTable.Builder builder = IndexRoutingTable.builder(new Index(index, "_na_"));
    int shardCount = randomInt(10);
    for (int i = 0; i < shardCount; i++) {
        IndexShardRoutingTable.Builder indexShard = new IndexShardRoutingTable.Builder(new ShardId(index, "_na_", i));
        int replicaCount = randomIntBetween(1, 10);
        Set<String> availableNodeIds = Sets.newHashSet(nodeIds);
        for (int j = 0; j < replicaCount; j++) {
            UnassignedInfo unassignedInfo = null;
            if (randomInt(5) == 1) {
                unassignedInfo = new UnassignedInfo(randomReason(), randomAlphaOfLength(10));
            }
            if (availableNodeIds.isEmpty()) {
                break;
            }
            String nodeId = randomFrom(availableNodeIds);
            availableNodeIds.remove(nodeId);
            indexShard.addShard(TestShardRouting.newShardRouting(index, i, nodeId, null, j == 0, ShardRoutingState.fromValue((byte) randomIntBetween(2, 3)), unassignedInfo));
        }
        builder.addIndexShard(indexShard.build());
    }
    return builder.build();
}
Also used : ShardId(org.opensearch.index.shard.ShardId) IndexRoutingTable(org.opensearch.cluster.routing.IndexRoutingTable) IndexShardRoutingTable(org.opensearch.cluster.routing.IndexShardRoutingTable) UnassignedInfo(org.opensearch.cluster.routing.UnassignedInfo) AliasMetadata.newAliasMetadataBuilder(org.opensearch.cluster.metadata.AliasMetadata.newAliasMetadataBuilder) Index(org.opensearch.index.Index)

Example 7 with IndexRoutingTable

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

the class SharedClusterSnapshotRestoreIT method testSnapshotCanceledOnRemovedShard.

/**
 * This test ensures that when a shard is removed from a node (perhaps due to the node
 * leaving the cluster, then returning), all snapshotting of that shard is aborted, so
 * all Store references held onto by the snapshot are released.
 *
 * See https://github.com/elastic/elasticsearch/issues/20876
 */
public void testSnapshotCanceledOnRemovedShard() throws Exception {
    final int numPrimaries = 1;
    final int numReplicas = 1;
    final String repo = "test-repo";
    final String index = "test-idx";
    final String snapshot = "test-snap";
    assertAcked(prepareCreate(index, 1, Settings.builder().put("number_of_shards", numPrimaries).put("number_of_replicas", numReplicas)));
    indexRandomDocs(index, 100);
    createRepository(repo, "mock", Settings.builder().put("location", randomRepoPath()).put("random", randomAlphaOfLength(10)).put("wait_after_unblock", 200));
    String blockedNode = blockNodeWithIndex(repo, index);
    logger.info("--> snapshot");
    clusterAdmin().prepareCreateSnapshot(repo, snapshot).setWaitForCompletion(false).execute();
    logger.info("--> waiting for block to kick in on node [{}]", blockedNode);
    waitForBlock(blockedNode, repo, TimeValue.timeValueSeconds(10));
    logger.info("--> removing primary shard that is being snapshotted");
    ClusterState clusterState = internalCluster().clusterService(internalCluster().getMasterName()).state();
    IndexRoutingTable indexRoutingTable = clusterState.getRoutingTable().index(index);
    String nodeWithPrimary = clusterState.nodes().get(indexRoutingTable.shard(0).primaryShard().currentNodeId()).getName();
    assertNotNull("should be at least one node with a primary shard", nodeWithPrimary);
    IndicesService indicesService = internalCluster().getInstance(IndicesService.class, nodeWithPrimary);
    IndexService indexService = indicesService.indexService(resolveIndex(index));
    indexService.removeShard(0, "simulate node removal");
    logger.info("--> unblocking blocked node [{}]", blockedNode);
    unblockNode(repo, blockedNode);
    logger.info("--> ensuring snapshot is aborted and the aborted shard was marked as failed");
    SnapshotInfo snapshotInfo = waitForCompletion(repo, snapshot, TimeValue.timeValueSeconds(60));
    assertEquals(1, snapshotInfo.shardFailures().size());
    assertEquals(0, snapshotInfo.shardFailures().get(0).shardId());
    assertThat(snapshotInfo.shardFailures().get(0).reason(), is("aborted"));
}
Also used : ClusterState(org.opensearch.cluster.ClusterState) IndexRoutingTable(org.opensearch.cluster.routing.IndexRoutingTable) IndexService(org.opensearch.index.IndexService) IndicesService(org.opensearch.indices.IndicesService) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 8 with IndexRoutingTable

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

the class ClusterStateCreationUtils method state.

/**
 * Creates cluster state with the given indices, each index containing #(numberOfPrimaries)
 * started primary shards and no replicas.  The cluster state contains #(numberOfNodes) nodes
 * and assigns primaries to those nodes.
 */
public static ClusterState state(final int numberOfNodes, final String[] indices, final int numberOfPrimaries) {
    DiscoveryNodes.Builder discoBuilder = DiscoveryNodes.builder();
    Set<String> nodes = new HashSet<>();
    for (int i = 0; i < numberOfNodes; i++) {
        final DiscoveryNode node = newNode(i);
        discoBuilder = discoBuilder.add(node);
        nodes.add(node.getId());
    }
    discoBuilder.localNodeId(newNode(0).getId());
    discoBuilder.masterNodeId(newNode(0).getId());
    Metadata.Builder metadata = Metadata.builder();
    RoutingTable.Builder routingTable = RoutingTable.builder();
    List<String> nodesList = new ArrayList<>(nodes);
    int currentNodeToAssign = 0;
    for (String index : indices) {
        IndexMetadata indexMetadata = IndexMetadata.builder(index).settings(Settings.builder().put(SETTING_VERSION_CREATED, Version.CURRENT).put(SETTING_NUMBER_OF_SHARDS, numberOfPrimaries).put(SETTING_NUMBER_OF_REPLICAS, 0).put(SETTING_CREATION_DATE, System.currentTimeMillis())).build();
        IndexRoutingTable.Builder indexRoutingTable = IndexRoutingTable.builder(indexMetadata.getIndex());
        for (int i = 0; i < numberOfPrimaries; i++) {
            ShardId shardId = new ShardId(indexMetadata.getIndex(), i);
            IndexShardRoutingTable.Builder indexShardRoutingBuilder = new IndexShardRoutingTable.Builder(shardId);
            indexShardRoutingBuilder.addShard(TestShardRouting.newShardRouting(shardId, nodesList.get(currentNodeToAssign++), true, ShardRoutingState.STARTED));
            if (currentNodeToAssign == nodesList.size()) {
                currentNodeToAssign = 0;
            }
            indexRoutingTable.addIndexShard(indexShardRoutingBuilder.build());
        }
        metadata.put(indexMetadata, false);
        routingTable.add(indexRoutingTable);
    }
    ClusterState.Builder state = ClusterState.builder(new ClusterName("test"));
    state.nodes(discoBuilder);
    state.metadata(metadata.generateClusterUuidIfNeeded().build());
    state.routingTable(routingTable.build());
    return state.build();
}
Also used : IndexRoutingTable(org.opensearch.cluster.routing.IndexRoutingTable) IndexShardRoutingTable(org.opensearch.cluster.routing.IndexShardRoutingTable) ClusterState(org.opensearch.cluster.ClusterState) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) Builder(org.opensearch.cluster.routing.RoutingTable.Builder) Metadata(org.opensearch.cluster.metadata.Metadata) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) ArrayList(java.util.ArrayList) Builder(org.opensearch.cluster.routing.RoutingTable.Builder) ShardId(org.opensearch.index.shard.ShardId) IndexRoutingTable(org.opensearch.cluster.routing.IndexRoutingTable) IndexShardRoutingTable(org.opensearch.cluster.routing.IndexShardRoutingTable) RoutingTable(org.opensearch.cluster.routing.RoutingTable) ClusterName(org.opensearch.cluster.ClusterName) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) DiscoveryNodes(org.opensearch.cluster.node.DiscoveryNodes) HashSet(java.util.HashSet)

Example 9 with IndexRoutingTable

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

the class ClusterStateCreationUtils method state.

/**
 * Creates cluster state with an index that has #(numberOfPrimaries) primary shards in the started state and no replicas.
 * The cluster state contains #(numberOfNodes) nodes and assigns primaries to those nodes.
 */
public static ClusterState state(String index, final int numberOfNodes, final int numberOfPrimaries) {
    DiscoveryNodes.Builder discoBuilder = DiscoveryNodes.builder();
    Set<String> nodes = new HashSet<>();
    for (int i = 0; i < numberOfNodes; i++) {
        final DiscoveryNode node = newNode(i);
        discoBuilder = discoBuilder.add(node);
        nodes.add(node.getId());
    }
    discoBuilder.localNodeId(newNode(0).getId());
    discoBuilder.masterNodeId(randomFrom(nodes));
    IndexMetadata indexMetadata = IndexMetadata.builder(index).settings(Settings.builder().put(SETTING_VERSION_CREATED, Version.CURRENT).put(SETTING_NUMBER_OF_SHARDS, numberOfPrimaries).put(SETTING_NUMBER_OF_REPLICAS, 0).put(SETTING_CREATION_DATE, System.currentTimeMillis())).build();
    IndexRoutingTable.Builder indexRoutingTable = IndexRoutingTable.builder(indexMetadata.getIndex());
    for (int i = 0; i < numberOfPrimaries; i++) {
        ShardId shardId = new ShardId(indexMetadata.getIndex(), i);
        IndexShardRoutingTable.Builder indexShardRoutingBuilder = new IndexShardRoutingTable.Builder(shardId);
        indexShardRoutingBuilder.addShard(TestShardRouting.newShardRouting(shardId, randomFrom(nodes), true, ShardRoutingState.STARTED));
        indexRoutingTable.addIndexShard(indexShardRoutingBuilder.build());
    }
    ClusterState.Builder state = ClusterState.builder(new ClusterName("test"));
    state.nodes(discoBuilder);
    state.metadata(Metadata.builder().put(indexMetadata, false).generateClusterUuidIfNeeded());
    state.routingTable(RoutingTable.builder().add(indexRoutingTable).build());
    return state.build();
}
Also used : IndexRoutingTable(org.opensearch.cluster.routing.IndexRoutingTable) IndexShardRoutingTable(org.opensearch.cluster.routing.IndexShardRoutingTable) ClusterState(org.opensearch.cluster.ClusterState) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) Builder(org.opensearch.cluster.routing.RoutingTable.Builder) ShardId(org.opensearch.index.shard.ShardId) ClusterName(org.opensearch.cluster.ClusterName) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) DiscoveryNodes(org.opensearch.cluster.node.DiscoveryNodes) HashSet(java.util.HashSet)

Example 10 with IndexRoutingTable

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

the class InternalTestCluster method assertSeqNos.

public void assertSeqNos() throws Exception {
    assertBusy(() -> {
        final ClusterState state = clusterService().state();
        for (ObjectObjectCursor<String, IndexRoutingTable> indexRoutingTable : state.routingTable().indicesRouting()) {
            for (IntObjectCursor<IndexShardRoutingTable> indexShardRoutingTable : indexRoutingTable.value.shards()) {
                ShardRouting primaryShardRouting = indexShardRoutingTable.value.primaryShard();
                final IndexShard primaryShard = getShardOrNull(state, primaryShardRouting);
                if (primaryShard == null) {
                    // just ignore - shard movement
                    continue;
                }
                final SeqNoStats primarySeqNoStats;
                final ObjectLongMap<String> syncGlobalCheckpoints;
                try {
                    primarySeqNoStats = primaryShard.seqNoStats();
                    syncGlobalCheckpoints = primaryShard.getInSyncGlobalCheckpoints();
                } catch (AlreadyClosedException ex) {
                    // shard is closed - just ignore
                    continue;
                }
                assertThat(primaryShardRouting + " should have set the global checkpoint", primarySeqNoStats.getGlobalCheckpoint(), not(equalTo(SequenceNumbers.UNASSIGNED_SEQ_NO)));
                for (ShardRouting replicaShardRouting : indexShardRoutingTable.value.replicaShards()) {
                    final IndexShard replicaShard = getShardOrNull(state, replicaShardRouting);
                    if (replicaShard == null) {
                        // just ignore - shard movement
                        continue;
                    }
                    final SeqNoStats seqNoStats;
                    try {
                        seqNoStats = replicaShard.seqNoStats();
                    } catch (AlreadyClosedException e) {
                        // shard is closed - just ignore
                        continue;
                    }
                    assertThat(replicaShardRouting + " seq_no_stats mismatch", seqNoStats, equalTo(primarySeqNoStats));
                    // the local knowledge on the primary of the global checkpoint equals the global checkpoint on the shard
                    assertThat(replicaShardRouting + " global checkpoint syncs mismatch", seqNoStats.getGlobalCheckpoint(), equalTo(syncGlobalCheckpoints.get(replicaShardRouting.allocationId().getId())));
                }
            }
        }
    }, 30, TimeUnit.SECONDS);
}
Also used : ClusterState(org.opensearch.cluster.ClusterState) IndexRoutingTable(org.opensearch.cluster.routing.IndexRoutingTable) IndexShardRoutingTable(org.opensearch.cluster.routing.IndexShardRoutingTable) SeqNoStats(org.opensearch.index.seqno.SeqNoStats) IndexShard(org.opensearch.index.shard.IndexShard) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) ShardRouting(org.opensearch.cluster.routing.ShardRouting)

Aggregations

IndexRoutingTable (org.opensearch.cluster.routing.IndexRoutingTable)46 IndexShardRoutingTable (org.opensearch.cluster.routing.IndexShardRoutingTable)34 ClusterState (org.opensearch.cluster.ClusterState)27 ShardRouting (org.opensearch.cluster.routing.ShardRouting)27 RoutingTable (org.opensearch.cluster.routing.RoutingTable)18 IndexMetadata (org.opensearch.cluster.metadata.IndexMetadata)15 ShardId (org.opensearch.index.shard.ShardId)13 Settings (org.opensearch.common.settings.Settings)12 HashSet (java.util.HashSet)10 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)9 HashMap (java.util.HashMap)8 DiscoveryNodes (org.opensearch.cluster.node.DiscoveryNodes)8 List (java.util.List)7 Matchers.containsString (org.hamcrest.Matchers.containsString)7 Metadata (org.opensearch.cluster.metadata.Metadata)7 Index (org.opensearch.index.Index)7 ArrayList (java.util.ArrayList)6 Map (java.util.Map)6 Set (java.util.Set)6 Collectors (java.util.stream.Collectors)6