Search in sources :

Example 21 with IndexNotFoundException

use of org.opensearch.index.IndexNotFoundException in project OpenSearch by opensearch-project.

the class IndexAbstractionResolver method resolveIndexAbstractions.

public List<String> resolveIndexAbstractions(Iterable<String> indices, IndicesOptions indicesOptions, Metadata metadata, Collection<String> availableIndexAbstractions, boolean replaceWildcards, boolean includeDataStreams) {
    List<String> finalIndices = new ArrayList<>();
    boolean wildcardSeen = false;
    for (String index : indices) {
        String indexAbstraction;
        boolean minus = false;
        if (index.charAt(0) == '-' && wildcardSeen) {
            indexAbstraction = index.substring(1);
            minus = true;
        } else {
            indexAbstraction = index;
        }
        // we always need to check for date math expressions
        final String dateMathName = indexNameExpressionResolver.resolveDateMathExpression(indexAbstraction);
        if (dateMathName != indexAbstraction) {
            assert dateMathName.equals(indexAbstraction) == false;
            if (replaceWildcards && Regex.isSimpleMatchPattern(dateMathName)) {
                // continue
                indexAbstraction = dateMathName;
            } else if (availableIndexAbstractions.contains(dateMathName) && isIndexVisible(indexAbstraction, dateMathName, indicesOptions, metadata, includeDataStreams, true)) {
                if (minus) {
                    finalIndices.remove(dateMathName);
                } else {
                    finalIndices.add(dateMathName);
                }
            } else {
                if (indicesOptions.ignoreUnavailable() == false) {
                    throw new IndexNotFoundException(dateMathName);
                }
            }
        }
        if (replaceWildcards && Regex.isSimpleMatchPattern(indexAbstraction)) {
            wildcardSeen = true;
            Set<String> resolvedIndices = new HashSet<>();
            for (String authorizedIndex : availableIndexAbstractions) {
                if (Regex.simpleMatch(indexAbstraction, authorizedIndex) && isIndexVisible(indexAbstraction, authorizedIndex, indicesOptions, metadata, includeDataStreams)) {
                    resolvedIndices.add(authorizedIndex);
                }
            }
            if (resolvedIndices.isEmpty()) {
                // es core honours allow_no_indices for each wildcard expression, we do the same here by throwing index not found.
                if (indicesOptions.allowNoIndices() == false) {
                    throw new IndexNotFoundException(indexAbstraction);
                }
            } else {
                if (minus) {
                    finalIndices.removeAll(resolvedIndices);
                } else {
                    finalIndices.addAll(resolvedIndices);
                }
            }
        } else if (dateMathName.equals(indexAbstraction)) {
            if (minus) {
                finalIndices.remove(indexAbstraction);
            } else {
                finalIndices.add(indexAbstraction);
            }
        }
    }
    return finalIndices;
}
Also used : ArrayList(java.util.ArrayList) IndexNotFoundException(org.opensearch.index.IndexNotFoundException) HashSet(java.util.HashSet)

Example 22 with IndexNotFoundException

use of org.opensearch.index.IndexNotFoundException in project ml-commons by opensearch-project.

the class GetModelTransportAction method doExecute.

@Override
protected void doExecute(Task task, ActionRequest request, ActionListener<MLModelGetResponse> actionListener) {
    MLModelGetRequest mlModelGetRequest = MLModelGetRequest.fromActionRequest(request);
    String modelId = mlModelGetRequest.getModelId();
    GetRequest getRequest = new GetRequest(ML_MODEL_INDEX).id(modelId);
    try (ThreadContext.StoredContext context = client.threadPool().getThreadContext().stashContext()) {
        client.get(getRequest, ActionListener.wrap(r -> {
            log.info("Completed Get Model Request, id:{}", modelId);
            if (r != null && r.isExists()) {
                try (XContentParser parser = createXContentParserFromRegistry(xContentRegistry, r.getSourceAsBytesRef())) {
                    ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser);
                    MLModel mlModel = MLModel.parse(parser);
                    actionListener.onResponse(MLModelGetResponse.builder().mlModel(mlModel).build());
                } catch (Exception e) {
                    log.error("Failed to parse ml model" + r.getId(), e);
                    actionListener.onFailure(e);
                }
            } else {
                actionListener.onFailure(new MLResourceNotFoundException("Fail to find model"));
            }
        }, e -> {
            if (e instanceof IndexNotFoundException) {
                actionListener.onFailure(new MLResourceNotFoundException("Fail to find model"));
            } else {
                log.error("Failed to get ML model " + modelId, e);
                actionListener.onFailure(e);
            }
        }));
    } catch (Exception e) {
        log.error("Failed to get ML model " + modelId, e);
        actionListener.onFailure(e);
    }
}
Also used : FieldDefaults(lombok.experimental.FieldDefaults) Client(org.opensearch.client.Client) HandledTransportAction(org.opensearch.action.support.HandledTransportAction) MLModelGetRequest(org.opensearch.ml.common.transport.model.MLModelGetRequest) IndexNotFoundException(org.opensearch.index.IndexNotFoundException) GetRequest(org.opensearch.action.get.GetRequest) XContentParserUtils.ensureExpectedToken(org.opensearch.common.xcontent.XContentParserUtils.ensureExpectedToken) MLNodeUtils.createXContentParserFromRegistry(org.opensearch.ml.utils.MLNodeUtils.createXContentParserFromRegistry) ActionRequest(org.opensearch.action.ActionRequest) Task(org.opensearch.tasks.Task) ThreadContext(org.opensearch.common.util.concurrent.ThreadContext) TransportService(org.opensearch.transport.TransportService) MLResourceNotFoundException(org.opensearch.ml.common.exception.MLResourceNotFoundException) XContentParser(org.opensearch.common.xcontent.XContentParser) ActionFilters(org.opensearch.action.support.ActionFilters) AccessLevel(lombok.AccessLevel) MLModelGetAction(org.opensearch.ml.common.transport.model.MLModelGetAction) ML_MODEL_INDEX(org.opensearch.ml.indices.MLIndicesHandler.ML_MODEL_INDEX) NamedXContentRegistry(org.opensearch.common.xcontent.NamedXContentRegistry) Log4j2(lombok.extern.log4j.Log4j2) Inject(org.opensearch.common.inject.Inject) ActionListener(org.opensearch.action.ActionListener) MLModel(org.opensearch.ml.common.parameter.MLModel) MLModelGetResponse(org.opensearch.ml.common.transport.model.MLModelGetResponse) MLResourceNotFoundException(org.opensearch.ml.common.exception.MLResourceNotFoundException) MLModelGetRequest(org.opensearch.ml.common.transport.model.MLModelGetRequest) GetRequest(org.opensearch.action.get.GetRequest) ThreadContext(org.opensearch.common.util.concurrent.ThreadContext) IndexNotFoundException(org.opensearch.index.IndexNotFoundException) MLModel(org.opensearch.ml.common.parameter.MLModel) MLModelGetRequest(org.opensearch.ml.common.transport.model.MLModelGetRequest) XContentParser(org.opensearch.common.xcontent.XContentParser) IndexNotFoundException(org.opensearch.index.IndexNotFoundException) MLResourceNotFoundException(org.opensearch.ml.common.exception.MLResourceNotFoundException)

Example 23 with IndexNotFoundException

use of org.opensearch.index.IndexNotFoundException in project OpenSearch by opensearch-project.

the class AutoCreateIndexTests method testAutoCreationDisabled.

public void testAutoCreationDisabled() {
    Settings settings = Settings.builder().put(AutoCreateIndex.AUTO_CREATE_INDEX_SETTING.getKey(), false).build();
    AutoCreateIndex autoCreateIndex = newAutoCreateIndex(settings);
    String randomIndex = randomAlphaOfLengthBetween(1, 10);
    IndexNotFoundException e = expectThrows(IndexNotFoundException.class, () -> autoCreateIndex.shouldAutoCreate(randomIndex, buildClusterState()));
    assertEquals("no such index [" + randomIndex + "] and [action.auto_create_index] is [false]", e.getMessage());
}
Also used : IndexNotFoundException(org.opensearch.index.IndexNotFoundException) Settings(org.opensearch.common.settings.Settings) ClusterSettings(org.opensearch.common.settings.ClusterSettings)

Example 24 with IndexNotFoundException

use of org.opensearch.index.IndexNotFoundException in project OpenSearch by opensearch-project.

the class AutoCreateIndexTests method expectNotMatch.

private void expectNotMatch(ClusterState clusterState, AutoCreateIndex autoCreateIndex, String index) {
    IndexNotFoundException e = expectThrows(IndexNotFoundException.class, () -> autoCreateIndex.shouldAutoCreate(index, clusterState));
    assertEquals("no such index [" + index + "] and [action.auto_create_index] ([" + autoCreateIndex.getAutoCreate() + "]) doesn't match", e.getMessage());
}
Also used : IndexNotFoundException(org.opensearch.index.IndexNotFoundException)

Example 25 with IndexNotFoundException

use of org.opensearch.index.IndexNotFoundException in project OpenSearch by opensearch-project.

the class TransportBulkActionIndicesThatCannotBeCreatedTests method testSomeFail.

public void testSomeFail() {
    BulkRequest bulkRequest = new BulkRequest();
    bulkRequest.add(new IndexRequest("ok"));
    bulkRequest.add(new IndexRequest("bad"));
    // Emulate auto_create_index=-bad,+*
    indicesThatCannotBeCreatedTestCase(singleton("bad"), bulkRequest, index -> {
        if (index.equals("bad")) {
            throw new IndexNotFoundException("Can't make it because I say so");
        }
        return true;
    });
    // Emulate auto_create_index=false but the "ok" index already exists
    indicesThatCannotBeCreatedTestCase(singleton("bad"), bulkRequest, index -> {
        if (index.equals("bad")) {
            throw new IndexNotFoundException("Can't make it because I say so");
        }
        return false;
    });
}
Also used : IndexNotFoundException(org.opensearch.index.IndexNotFoundException) IndexRequest(org.opensearch.action.index.IndexRequest)

Aggregations

IndexNotFoundException (org.opensearch.index.IndexNotFoundException)99 ActionListener (org.opensearch.action.ActionListener)32 ClusterState (org.opensearch.cluster.ClusterState)26 ThreadContext (org.opensearch.common.util.concurrent.ThreadContext)25 IOException (java.io.IOException)24 Client (org.opensearch.client.Client)23 ArrayList (java.util.ArrayList)21 Map (java.util.Map)20 Logger (org.apache.logging.log4j.Logger)20 List (java.util.List)19 LogManager (org.apache.logging.log4j.LogManager)19 HashMap (java.util.HashMap)18 GetRequest (org.opensearch.action.get.GetRequest)18 AnomalyDetector (org.opensearch.ad.model.AnomalyDetector)18 Settings (org.opensearch.common.settings.Settings)18 NamedXContentRegistry (org.opensearch.common.xcontent.NamedXContentRegistry)18 ClusterService (org.opensearch.cluster.service.ClusterService)17 Optional (java.util.Optional)15 XContentParser (org.opensearch.common.xcontent.XContentParser)15 DeleteRequest (org.opensearch.action.delete.DeleteRequest)14