Search in sources :

Example 1 with RoutingTable

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

the class AllocationBenchmark method setUp.

@Setup
public void setUp() throws Exception {
    final String[] params = indicesShardsReplicasSourceTargetRecoveries.split("\\|");
    numIndices = toInt(params[0]);
    numShards = toInt(params[1]);
    numReplicas = toInt(params[2]);
    sourceNodes = toInt(params[3]);
    targetNodes = toInt(params[4]);
    concurrentRecoveries = toInt(params[5]);
    int totalShardCount = (numReplicas + 1) * numShards * numIndices;
    initialClusterStrategy = Allocators.createAllocationService(Settings.builder().put("cluster.routing.allocation.awareness.attributes", "zone").put("cluster.routing.allocation.node_concurrent_recoveries", "20").put("cluster.routing.allocation.exclude.tag", "tag_0").build());
    // We'll try to move nodes from tag_1 to tag_0
    clusterConcurrentRecoveries = Math.min(sourceNodes, targetNodes) * concurrentRecoveries;
    Metadata.Builder mb = Metadata.builder();
    for (int i = 1; i <= numIndices; i++) {
        mb.put(IndexMetadata.builder("test_" + i).settings(Settings.builder().put("index.version.created", Version.CURRENT)).numberOfShards(numShards).numberOfReplicas(numReplicas));
    }
    Metadata metadata = mb.build();
    RoutingTable.Builder rb = RoutingTable.builder();
    for (int i = 1; i <= numIndices; i++) {
        rb.addAsNew(metadata.index("test_" + i));
    }
    RoutingTable routingTable = rb.build();
    initialClusterState = ClusterState.builder(ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metadata(metadata).routingTable(routingTable).nodes(setUpClusterNodes(sourceNodes, targetNodes)).build();
    // Start all unassigned shards
    initialClusterState = initialClusterStrategy.reroute(initialClusterState, "reroute");
    while (initialClusterState.getRoutingNodes().hasUnassignedShards()) {
        initialClusterState = initialClusterStrategy.applyStartedShards(initialClusterState, initialClusterState.getRoutingNodes().shardsWithState(ShardRoutingState.INITIALIZING));
        initialClusterState = initialClusterStrategy.reroute(initialClusterState, "reroute");
    }
    // Ensure all shards are started
    while (initialClusterState.getRoutingNodes().shardsWithState(ShardRoutingState.INITIALIZING).size() > 0) {
        initialClusterState = initialClusterStrategy.applyStartedShards(initialClusterState, initialClusterState.getRoutingNodes().shardsWithState(ShardRoutingState.INITIALIZING));
    }
    assert (initialClusterState.getRoutingNodes().shardsWithState(ShardRoutingState.STARTED).size() == totalShardCount);
    assert (initialClusterState.getRoutingNodes().shardsWithState(ShardRoutingState.INITIALIZING).size() == 0);
    assert (initialClusterState.getRoutingNodes().shardsWithState(ShardRoutingState.RELOCATING).size() == 0);
    // make sure shards are only allocated on tag1
    for (ShardRouting startedShard : initialClusterState.getRoutingNodes().shardsWithState(ShardRoutingState.STARTED)) {
        assert (initialClusterState.getRoutingNodes().node(startedShard.currentNodeId()).node().getAttributes().get("tag")).equals("tag_1");
    }
}
Also used : RoutingTable(org.opensearch.cluster.routing.RoutingTable) Metadata(org.opensearch.cluster.metadata.Metadata) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) ShardRouting(org.opensearch.cluster.routing.ShardRouting) Setup(org.openjdk.jmh.annotations.Setup)

Example 2 with RoutingTable

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

the class GatewayIndexStateIT method testRecoverBrokenIndexMetadata.

/**
 * This test really tests worst case scenario where we have a broken setting or any setting that prevents an index from being
 * allocated in our metadata that we recover. In that case we now have the ability to check the index on local recovery from disk
 * if it is sane and if we can successfully create an IndexService. This also includes plugins etc.
 */
public void testRecoverBrokenIndexMetadata() throws Exception {
    logger.info("--> starting one node");
    internalCluster().startNode();
    logger.info("--> indexing a simple document");
    client().prepareIndex("test").setId("1").setSource("field1", "value1").setRefreshPolicy(IMMEDIATE).get();
    logger.info("--> waiting for green status");
    if (usually()) {
        ensureYellow();
    } else {
        internalCluster().startNode();
        client().admin().cluster().health(Requests.clusterHealthRequest().waitForGreenStatus().waitForEvents(Priority.LANGUID).waitForNoRelocatingShards(true).waitForNodes("2")).actionGet();
    }
    ClusterState state = client().admin().cluster().prepareState().get().getState();
    final IndexMetadata metadata = state.getMetadata().index("test");
    final IndexMetadata.Builder brokenMeta = IndexMetadata.builder(metadata).settings(Settings.builder().put(metadata.getSettings()).put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT.minimumIndexCompatibilityVersion().id).put("index.similarity.BM25.type", "classic").put("index.analysis.filter.myCollator.type", "icu_collation"));
    restartNodesOnBrokenClusterState(ClusterState.builder(state).metadata(Metadata.builder(state.getMetadata()).put(brokenMeta)));
    // check that the cluster does not keep reallocating shards
    assertBusy(() -> {
        final RoutingTable routingTable = client().admin().cluster().prepareState().get().getState().routingTable();
        final IndexRoutingTable indexRoutingTable = routingTable.index("test");
        assertNotNull(indexRoutingTable);
        for (IndexShardRoutingTable shardRoutingTable : indexRoutingTable) {
            assertTrue(shardRoutingTable.primaryShard().unassigned());
            assertEquals(UnassignedInfo.AllocationStatus.DECIDERS_NO, shardRoutingTable.primaryShard().unassignedInfo().getLastAllocationStatus());
            assertThat(shardRoutingTable.primaryShard().unassignedInfo().getNumFailedAllocations(), greaterThan(0));
        }
    }, 60, TimeUnit.SECONDS);
    client().admin().indices().prepareClose("test").get();
    state = client().admin().cluster().prepareState().get().getState();
    assertEquals(IndexMetadata.State.CLOSE, state.getMetadata().index(metadata.getIndex()).getState());
    assertEquals("classic", state.getMetadata().index(metadata.getIndex()).getSettings().get("archived.index.similarity.BM25.type"));
    // try to open it with the broken setting - fail again!
    OpenSearchException ex = expectThrows(OpenSearchException.class, () -> client().admin().indices().prepareOpen("test").get());
    assertEquals(ex.getMessage(), "Failed to verify index " + metadata.getIndex());
    assertNotNull(ex.getCause());
    assertEquals(IllegalArgumentException.class, ex.getCause().getClass());
    assertEquals(ex.getCause().getMessage(), "Unknown filter type [icu_collation] for [myCollator]");
}
Also used : ClusterState(org.opensearch.cluster.ClusterState) IndexRoutingTable(org.opensearch.cluster.routing.IndexRoutingTable) IndexShardRoutingTable(org.opensearch.cluster.routing.IndexShardRoutingTable) IndexShardRoutingTable(org.opensearch.cluster.routing.IndexShardRoutingTable) IndexRoutingTable(org.opensearch.cluster.routing.IndexRoutingTable) RoutingTable(org.opensearch.cluster.routing.RoutingTable) OpenSearchException(org.opensearch.OpenSearchException) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata)

Example 3 with RoutingTable

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

the class SimpleClusterStateIT method testFilteringByIndexWorks.

/**
 * Retrieves the cluster state for the given indices and then checks
 * that the cluster state returns coherent data for both routing table and metadata.
 */
private void testFilteringByIndexWorks(String[] indices, String[] expected) {
    ClusterStateResponse clusterState = client().admin().cluster().prepareState().clear().setMetadata(true).setRoutingTable(true).setIndices(indices).get();
    ImmutableOpenMap<String, IndexMetadata> metadata = clusterState.getState().getMetadata().indices();
    assertThat(metadata.size(), is(expected.length));
    RoutingTable routingTable = clusterState.getState().getRoutingTable();
    assertThat(routingTable.indicesRouting().size(), is(expected.length));
    for (String expectedIndex : expected) {
        assertThat(metadata, CollectionAssertions.hasKey(expectedIndex));
        assertThat(routingTable.hasIndex(expectedIndex), is(true));
    }
}
Also used : RoutingTable(org.opensearch.cluster.routing.RoutingTable) ClusterStateResponse(org.opensearch.action.admin.cluster.state.ClusterStateResponse) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata)

Example 4 with RoutingTable

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

the class TransportIndicesShardStoresAction method masterOperation.

@Override
protected void masterOperation(IndicesShardStoresRequest request, ClusterState state, ActionListener<IndicesShardStoresResponse> listener) {
    final RoutingTable routingTables = state.routingTable();
    final RoutingNodes routingNodes = state.getRoutingNodes();
    final String[] concreteIndices = indexNameExpressionResolver.concreteIndexNames(state, request);
    final Set<Tuple<ShardId, String>> shardsToFetch = new HashSet<>();
    logger.trace("using cluster state version [{}] to determine shards", state.version());
    // collect relevant shard ids of the requested indices for fetching store infos
    for (String index : concreteIndices) {
        IndexRoutingTable indexShardRoutingTables = routingTables.index(index);
        if (indexShardRoutingTables == null) {
            continue;
        }
        final String customDataPath = IndexMetadata.INDEX_DATA_PATH_SETTING.get(state.metadata().index(index).getSettings());
        for (IndexShardRoutingTable routing : indexShardRoutingTables) {
            final int shardId = routing.shardId().id();
            ClusterShardHealth shardHealth = new ClusterShardHealth(shardId, routing);
            if (request.shardStatuses().contains(shardHealth.getStatus())) {
                shardsToFetch.add(Tuple.tuple(routing.shardId(), customDataPath));
            }
        }
    }
    // async fetch store infos from all the nodes
    // NOTE: instead of fetching shard store info one by one from every node (nShards * nNodes requests)
    // we could fetch all shard store info from every node once (nNodes requests)
    // we have to implement a TransportNodesAction instead of using TransportNodesListGatewayStartedShards
    // for fetching shard stores info, that operates on a list of shards instead of a single shard
    new AsyncShardStoresInfoFetches(state.nodes(), routingNodes, shardsToFetch, listener).start();
}
Also used : IndexRoutingTable(org.opensearch.cluster.routing.IndexRoutingTable) IndexShardRoutingTable(org.opensearch.cluster.routing.IndexShardRoutingTable) IndexRoutingTable(org.opensearch.cluster.routing.IndexRoutingTable) IndexShardRoutingTable(org.opensearch.cluster.routing.IndexShardRoutingTable) RoutingTable(org.opensearch.cluster.routing.RoutingTable) RoutingNodes(org.opensearch.cluster.routing.RoutingNodes) ClusterShardHealth(org.opensearch.cluster.health.ClusterShardHealth) Tuple(org.opensearch.common.collect.Tuple) HashSet(java.util.HashSet)

Example 5 with RoutingTable

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

the class IndexMetadataUpdater method removeStaleIdsWithoutRoutings.

/**
 * Removes allocation ids from the in-sync set for shard copies for which there is no routing entries in the routing table.
 * This method is called in AllocationService before any changes to the routing table are made.
 */
public static ClusterState removeStaleIdsWithoutRoutings(ClusterState clusterState, List<StaleShard> staleShards, Logger logger) {
    Metadata oldMetadata = clusterState.metadata();
    RoutingTable oldRoutingTable = clusterState.routingTable();
    Metadata.Builder metadataBuilder = null;
    // group staleShards entries by index
    for (Map.Entry<Index, List<StaleShard>> indexEntry : staleShards.stream().collect(Collectors.groupingBy(fs -> fs.getShardId().getIndex())).entrySet()) {
        final IndexMetadata oldIndexMetadata = oldMetadata.getIndexSafe(indexEntry.getKey());
        IndexMetadata.Builder indexMetadataBuilder = null;
        // group staleShards entries by shard id
        for (Map.Entry<ShardId, List<StaleShard>> shardEntry : indexEntry.getValue().stream().collect(Collectors.groupingBy(staleShard -> staleShard.getShardId())).entrySet()) {
            int shardNumber = shardEntry.getKey().getId();
            Set<String> oldInSyncAllocations = oldIndexMetadata.inSyncAllocationIds(shardNumber);
            Set<String> idsToRemove = shardEntry.getValue().stream().map(e -> e.getAllocationId()).collect(Collectors.toSet());
            assert idsToRemove.stream().allMatch(id -> oldRoutingTable.getByAllocationId(shardEntry.getKey(), id) == null) : "removing stale ids: " + idsToRemove + ", some of which have still a routing entry: " + oldRoutingTable;
            Set<String> remainingInSyncAllocations = Sets.difference(oldInSyncAllocations, idsToRemove);
            assert remainingInSyncAllocations.isEmpty() == false : "Set of in-sync ids cannot become empty for shard " + shardEntry.getKey() + " (before: " + oldInSyncAllocations + ", ids to remove: " + idsToRemove + ")";
            // (see ShardRouting#allocatedPostIndexCreate)
            if (remainingInSyncAllocations.isEmpty() == false) {
                if (indexMetadataBuilder == null) {
                    indexMetadataBuilder = IndexMetadata.builder(oldIndexMetadata);
                }
                indexMetadataBuilder.putInSyncAllocationIds(shardNumber, remainingInSyncAllocations);
            }
            logger.warn("{} marking unavailable shards as stale: {}", shardEntry.getKey(), idsToRemove);
        }
        if (indexMetadataBuilder != null) {
            if (metadataBuilder == null) {
                metadataBuilder = Metadata.builder(oldMetadata);
            }
            metadataBuilder.put(indexMetadataBuilder);
        }
    }
    if (metadataBuilder != null) {
        return ClusterState.builder(clusterState).metadata(metadataBuilder).build();
    } else {
        return clusterState;
    }
}
Also used : IndexShardRoutingTable(org.opensearch.cluster.routing.IndexShardRoutingTable) RoutingChangesObserver(org.opensearch.cluster.routing.RoutingChangesObserver) Metadata(org.opensearch.cluster.metadata.Metadata) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) Index(org.opensearch.index.Index) Set(java.util.Set) HashMap(java.util.HashMap) Collectors(java.util.stream.Collectors) RecoverySource(org.opensearch.cluster.routing.RecoverySource) ShardRouting(org.opensearch.cluster.routing.ShardRouting) ShardId(org.opensearch.index.shard.ShardId) HashSet(java.util.HashSet) Objects(java.util.Objects) ClusterState(org.opensearch.cluster.ClusterState) Sets(org.opensearch.common.util.set.Sets) List(java.util.List) Logger(org.apache.logging.log4j.Logger) Map(java.util.Map) RoutingTable(org.opensearch.cluster.routing.RoutingTable) Comparator(java.util.Comparator) UnassignedInfo(org.opensearch.cluster.routing.UnassignedInfo) Collections(java.util.Collections) Metadata(org.opensearch.cluster.metadata.Metadata) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) Index(org.opensearch.index.Index) ShardId(org.opensearch.index.shard.ShardId) IndexShardRoutingTable(org.opensearch.cluster.routing.IndexShardRoutingTable) RoutingTable(org.opensearch.cluster.routing.RoutingTable) List(java.util.List) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

RoutingTable (org.opensearch.cluster.routing.RoutingTable)226 ClusterState (org.opensearch.cluster.ClusterState)192 IndexMetadata (org.opensearch.cluster.metadata.IndexMetadata)189 Metadata (org.opensearch.cluster.metadata.Metadata)186 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 TestGatewayAllocator (org.opensearch.test.gateway.TestGatewayAllocator)29 HashSet (java.util.HashSet)28 ImmutableOpenMap (org.opensearch.common.collect.ImmutableOpenMap)27 ClusterSettings (org.opensearch.common.settings.ClusterSettings)27 RoutingNode (org.opensearch.cluster.routing.RoutingNode)24 BalancedShardsAllocator (org.opensearch.cluster.routing.allocation.allocator.BalancedShardsAllocator)23