Search in sources :

Example 31 with UnassignedInfo

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

the class GatewayIndexStateIT method testRecoverMissingAnalyzer.

/**
 * This test really tests worst case scenario where we have a missing analyzer setting.
 * 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 testRecoverMissingAnalyzer() throws Exception {
    logger.info("--> starting one node");
    internalCluster().startNode();
    prepareCreate("test").setSettings(Settings.builder().put("index.analysis.analyzer.test.tokenizer", "standard").put("index.number_of_shards", "1")).addMapping("type1", "{\n" + "    \"type1\": {\n" + "      \"properties\": {\n" + "        \"field1\": {\n" + "          \"type\": \"text\",\n" + "          \"analyzer\": \"test\"\n" + "        }\n" + "      }\n" + "    }\n" + "  }}", XContentType.JSON).get();
    logger.info("--> indexing a simple document");
    client().prepareIndex("test").setId("1").setSource("field1", "value one").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(metadata.getSettings().filter((s) -> "index.analysis.analyzer.test.tokenizer".equals(s) == false));
    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();
    // 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(MapperParsingException.class, ex.getCause().getClass());
    assertThat(ex.getCause().getMessage(), containsString("analyzer [test] has not been configured in mappings"));
}
Also used : IndexGraveyard(org.opensearch.cluster.metadata.IndexGraveyard) Metadata(org.opensearch.cluster.metadata.Metadata) ClusterStateResponse(org.opensearch.action.admin.cluster.state.ClusterStateResponse) ClusterScope(org.opensearch.test.OpenSearchIntegTestCase.ClusterScope) Version(org.opensearch.Version) OpenSearchException(org.opensearch.OpenSearchException) OpenSearchAssertions.assertHitCount(org.opensearch.test.hamcrest.OpenSearchAssertions.assertHitCount) QueryBuilders.matchAllQuery(org.opensearch.index.query.QueryBuilders.matchAllQuery) Map(java.util.Map) XContentFactory(org.opensearch.common.xcontent.XContentFactory) GetResponse(org.opensearch.action.get.GetResponse) UnassignedInfo(org.opensearch.cluster.routing.UnassignedInfo) Path(java.nio.file.Path) IndexShardRoutingTable(org.opensearch.cluster.routing.IndexShardRoutingTable) NodeEnvironment(org.opensearch.env.NodeEnvironment) Client(org.opensearch.client.Client) Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) Settings(org.opensearch.common.settings.Settings) Scope(org.opensearch.test.OpenSearchIntegTestCase.Scope) Collectors(java.util.stream.Collectors) List(java.util.List) Logger(org.apache.logging.log4j.Logger) ClusterHealthResponse(org.opensearch.action.admin.cluster.health.ClusterHealthResponse) Stream(java.util.stream.Stream) IMMEDIATE(org.opensearch.action.support.WriteRequest.RefreshPolicy.IMMEDIATE) Matchers.equalTo(org.hamcrest.Matchers.equalTo) XContentType(org.opensearch.common.xcontent.XContentType) NodeRoles.nonDataNode(org.opensearch.test.NodeRoles.nonDataNode) Matchers.greaterThan(org.hamcrest.Matchers.greaterThan) OpenSearchIntegTestCase(org.opensearch.test.OpenSearchIntegTestCase) Matchers.containsString(org.hamcrest.Matchers.containsString) MappingMetadata(org.opensearch.cluster.metadata.MappingMetadata) RestartCallback(org.opensearch.test.InternalTestCluster.RestartCallback) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) MapperParsingException(org.opensearch.index.mapper.MapperParsingException) Priority(org.opensearch.common.Priority) Function(java.util.function.Function) IndexRoutingTable(org.opensearch.cluster.routing.IndexRoutingTable) Requests(org.opensearch.client.Requests) ClusterState(org.opensearch.cluster.ClusterState) IndexClosedException(org.opensearch.indices.IndexClosedException) ShardRoutingState(org.opensearch.cluster.routing.ShardRoutingState) OpenSearchAssertions.assertAcked(org.opensearch.test.hamcrest.OpenSearchAssertions.assertAcked) Matchers.empty(org.hamcrest.Matchers.empty) ShardLimitValidator(org.opensearch.indices.ShardLimitValidator) IOException(java.io.IOException) ActiveShardCount(org.opensearch.action.support.ActiveShardCount) TimeUnit(java.util.concurrent.TimeUnit) ClusterService(org.opensearch.cluster.service.ClusterService) RoutingTable(org.opensearch.cluster.routing.RoutingTable) LogManager(org.apache.logging.log4j.LogManager) 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 32 with UnassignedInfo

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

the class AllocateEmptyPrimaryAllocationCommand method execute.

@Override
public RerouteExplanation execute(RoutingAllocation allocation, boolean explain) {
    final DiscoveryNode discoNode;
    try {
        discoNode = allocation.nodes().resolveNode(node);
    } catch (IllegalArgumentException e) {
        return explainOrThrowRejectedCommand(explain, allocation, e);
    }
    final RoutingNodes routingNodes = allocation.routingNodes();
    RoutingNode routingNode = routingNodes.node(discoNode.getId());
    if (routingNode == null) {
        return explainOrThrowMissingRoutingNode(allocation, explain, discoNode);
    }
    try {
        allocation.routingTable().shardRoutingTable(index, shardId).primaryShard();
    } catch (IndexNotFoundException | ShardNotFoundException e) {
        return explainOrThrowRejectedCommand(explain, allocation, e);
    }
    ShardRouting shardRouting = null;
    for (ShardRouting shard : allocation.routingNodes().unassigned()) {
        if (shard.getIndexName().equals(index) && shard.getId() == shardId && shard.primary()) {
            shardRouting = shard;
            break;
        }
    }
    if (shardRouting == null) {
        return explainOrThrowRejectedCommand(explain, allocation, "primary [" + index + "][" + shardId + "] is already assigned");
    }
    if (shardRouting.recoverySource().getType() != RecoverySource.Type.EMPTY_STORE && acceptDataLoss == false) {
        String dataLossWarning = "allocating an empty primary for [" + index + "][" + shardId + "] can result in data loss. Please confirm by setting the accept_data_loss parameter to true";
        return explainOrThrowRejectedCommand(explain, allocation, dataLossWarning);
    }
    UnassignedInfo unassignedInfoToUpdate = null;
    if (shardRouting.unassignedInfo().getReason() != UnassignedInfo.Reason.FORCED_EMPTY_PRIMARY) {
        String unassignedInfoMessage = "force empty allocation from previous reason " + shardRouting.unassignedInfo().getReason() + ", " + shardRouting.unassignedInfo().getMessage();
        unassignedInfoToUpdate = new UnassignedInfo(UnassignedInfo.Reason.FORCED_EMPTY_PRIMARY, unassignedInfoMessage, shardRouting.unassignedInfo().getFailure(), 0, System.nanoTime(), System.currentTimeMillis(), false, shardRouting.unassignedInfo().getLastAllocationStatus(), Collections.emptySet());
    }
    initializeUnassignedShard(allocation, routingNodes, routingNode, shardRouting, unassignedInfoToUpdate, EmptyStoreRecoverySource.INSTANCE);
    return new RerouteExplanation(this, allocation.decision(Decision.YES, name() + " (allocation command)", "ignore deciders"));
}
Also used : DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) RoutingNode(org.opensearch.cluster.routing.RoutingNode) ShardNotFoundException(org.opensearch.index.shard.ShardNotFoundException) RoutingNodes(org.opensearch.cluster.routing.RoutingNodes) UnassignedInfo(org.opensearch.cluster.routing.UnassignedInfo) IndexNotFoundException(org.opensearch.index.IndexNotFoundException) RerouteExplanation(org.opensearch.cluster.routing.allocation.RerouteExplanation) ShardRouting(org.opensearch.cluster.routing.ShardRouting)

Example 33 with UnassignedInfo

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

the class AllocationService method disassociateDeadNodes.

private void disassociateDeadNodes(RoutingAllocation allocation) {
    for (Iterator<RoutingNode> it = allocation.routingNodes().mutableIterator(); it.hasNext(); ) {
        RoutingNode node = it.next();
        if (allocation.nodes().getDataNodes().containsKey(node.nodeId())) {
            // its a live node, continue
            continue;
        }
        // now, go over all the shards routing on the node, and fail them
        for (ShardRouting shardRouting : node.copyShards()) {
            final IndexMetadata indexMetadata = allocation.metadata().getIndexSafe(shardRouting.index());
            boolean delayed = INDEX_DELAYED_NODE_LEFT_TIMEOUT_SETTING.get(indexMetadata.getSettings()).nanos() > 0;
            UnassignedInfo unassignedInfo = new UnassignedInfo(UnassignedInfo.Reason.NODE_LEFT, "node_left [" + node.nodeId() + "]", null, 0, allocation.getCurrentNanoTime(), System.currentTimeMillis(), delayed, AllocationStatus.NO_ATTEMPT, Collections.emptySet());
            allocation.routingNodes().failShard(logger, shardRouting, unassignedInfo, indexMetadata, allocation.changes());
        }
        // its a dead node, remove it, note, its important to remove it *after* we apply failed shard
        // since it relies on the fact that the RoutingNode exists in the list of nodes
        it.remove();
    }
}
Also used : RoutingNode(org.opensearch.cluster.routing.RoutingNode) UnassignedInfo(org.opensearch.cluster.routing.UnassignedInfo) ShardRouting(org.opensearch.cluster.routing.ShardRouting) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata)

Example 34 with UnassignedInfo

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

the class MaxRetryAllocationDecider method canAllocate.

@Override
public Decision canAllocate(ShardRouting shardRouting, RoutingAllocation allocation) {
    final UnassignedInfo unassignedInfo = shardRouting.unassignedInfo();
    final Decision decision;
    if (unassignedInfo != null && unassignedInfo.getNumFailedAllocations() > 0) {
        final IndexMetadata indexMetadata = allocation.metadata().getIndexSafe(shardRouting.index());
        final int maxRetry = SETTING_ALLOCATION_MAX_RETRY.get(indexMetadata.getSettings());
        if (unassignedInfo.getNumFailedAllocations() >= maxRetry) {
            decision = allocation.decision(Decision.NO, NAME, "shard has exceeded the maximum number of retries [%d] on " + "failed allocation attempts - manually call [/_cluster/reroute?retry_failed=true] to retry, [%s]", maxRetry, unassignedInfo.toString());
        } else {
            decision = allocation.decision(Decision.YES, NAME, "shard has failed allocating [%d] times but [%d] retries are allowed", unassignedInfo.getNumFailedAllocations(), maxRetry);
        }
    } else {
        decision = allocation.decision(Decision.YES, NAME, "shard has no previous failures");
    }
    return decision;
}
Also used : UnassignedInfo(org.opensearch.cluster.routing.UnassignedInfo) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata)

Example 35 with UnassignedInfo

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

the class ResizeAllocationDecider method canAllocate.

@Override
public Decision canAllocate(ShardRouting shardRouting, RoutingNode node, RoutingAllocation allocation) {
    final UnassignedInfo unassignedInfo = shardRouting.unassignedInfo();
    if (unassignedInfo != null && shardRouting.recoverySource().getType() == RecoverySource.Type.LOCAL_SHARDS) {
        // we only make decisions here if we have an unassigned info and we have to recover from another index ie. split / shrink
        final IndexMetadata indexMetadata = allocation.metadata().getIndexSafe(shardRouting.index());
        Index resizeSourceIndex = indexMetadata.getResizeSourceIndex();
        assert resizeSourceIndex != null;
        if (allocation.metadata().index(resizeSourceIndex) == null) {
            return allocation.decision(Decision.NO, NAME, "resize source index [%s] doesn't exists", resizeSourceIndex.toString());
        }
        IndexMetadata sourceIndexMetadata = allocation.metadata().getIndexSafe(resizeSourceIndex);
        if (indexMetadata.getNumberOfShards() < sourceIndexMetadata.getNumberOfShards()) {
            // this only handles splits and clone so far.
            return Decision.ALWAYS;
        }
        ShardId shardId = indexMetadata.getNumberOfShards() == sourceIndexMetadata.getNumberOfShards() ? IndexMetadata.selectCloneShard(shardRouting.id(), sourceIndexMetadata, indexMetadata.getNumberOfShards()) : IndexMetadata.selectSplitShard(shardRouting.id(), sourceIndexMetadata, indexMetadata.getNumberOfShards());
        ShardRouting sourceShardRouting = allocation.routingNodes().activePrimary(shardId);
        if (sourceShardRouting == null) {
            return allocation.decision(Decision.NO, NAME, "source primary shard [%s] is not active", shardId);
        }
        if (node != null) {
            // we might get called from the 2 param canAllocate method..
            if (sourceShardRouting.currentNodeId().equals(node.nodeId())) {
                return allocation.decision(Decision.YES, NAME, "source primary is allocated on this node");
            } else {
                return allocation.decision(Decision.NO, NAME, "source primary is allocated on another node");
            }
        } else {
            return allocation.decision(Decision.YES, NAME, "source primary is active");
        }
    }
    return super.canAllocate(shardRouting, node, allocation);
}
Also used : ShardId(org.opensearch.index.shard.ShardId) UnassignedInfo(org.opensearch.cluster.routing.UnassignedInfo) Index(org.opensearch.index.Index) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) ShardRouting(org.opensearch.cluster.routing.ShardRouting)

Aggregations

UnassignedInfo (org.opensearch.cluster.routing.UnassignedInfo)57 ShardRouting (org.opensearch.cluster.routing.ShardRouting)36 ShardId (org.opensearch.index.shard.ShardId)32 IndexMetadata (org.opensearch.cluster.metadata.IndexMetadata)27 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)25 Index (org.opensearch.index.Index)17 ShardRoutingState (org.opensearch.cluster.routing.ShardRoutingState)16 Matchers.containsString (org.hamcrest.Matchers.containsString)15 ClusterState (org.opensearch.cluster.ClusterState)15 ClusterInfo (org.opensearch.cluster.ClusterInfo)14 Metadata (org.opensearch.cluster.metadata.Metadata)11 RoutingNodes (org.opensearch.cluster.routing.RoutingNodes)11 AllocateUnassignedDecision (org.opensearch.cluster.routing.allocation.AllocateUnassignedDecision)11 Settings (org.opensearch.common.settings.Settings)11 RoutingNode (org.opensearch.cluster.routing.RoutingNode)10 RoutingTable (org.opensearch.cluster.routing.RoutingTable)10 MoveDecision (org.opensearch.cluster.routing.allocation.MoveDecision)10 NodeAllocationResult (org.opensearch.cluster.routing.allocation.NodeAllocationResult)10 RoutingAllocation (org.opensearch.cluster.routing.allocation.RoutingAllocation)10 XContentParser (org.opensearch.common.xcontent.XContentParser)10