Search in sources :

Example 81 with IndexNotFoundException

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

the class ReindexValidator method validateAgainstAliases.

/**
 * Throws an ActionRequestValidationException if the request tries to index
 * back into the same index or into an index that points to two indexes.
 * This cannot be done during request validation because the cluster state
 * isn't available then. Package private for testing.
 */
static void validateAgainstAliases(SearchRequest source, IndexRequest destination, RemoteInfo remoteInfo, IndexNameExpressionResolver indexNameExpressionResolver, AutoCreateIndex autoCreateIndex, ClusterState clusterState) {
    if (remoteInfo != null) {
        return;
    }
    String target = destination.index();
    if (destination.isRequireAlias() && (false == clusterState.getMetadata().hasAlias(target))) {
        throw new IndexNotFoundException("[" + DocWriteRequest.REQUIRE_ALIAS + "] request flag is [true] and [" + target + "] is not an alias", target);
    }
    if (false == autoCreateIndex.shouldAutoCreate(target, clusterState)) {
        /*
             * If we're going to autocreate the index we don't need to resolve
             * it. This is the same sort of dance that TransportIndexRequest
             * uses to decide to autocreate the index.
             */
        target = indexNameExpressionResolver.concreteWriteIndex(clusterState, destination).getName();
    }
    for (String sourceIndex : indexNameExpressionResolver.concreteIndexNames(clusterState, source)) {
        if (sourceIndex.equals(target)) {
            ActionRequestValidationException e = new ActionRequestValidationException();
            e.addValidationError("reindex cannot write into an index its reading from [" + target + ']');
            throw e;
        }
    }
}
Also used : ActionRequestValidationException(org.opensearch.action.ActionRequestValidationException) IndexNotFoundException(org.opensearch.index.IndexNotFoundException)

Example 82 with IndexNotFoundException

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

the class SimpleBlocksIT method testAddBlockWhileDeletingIndices.

public void testAddBlockWhileDeletingIndices() throws Exception {
    final String[] indices = new String[randomIntBetween(3, 10)];
    for (int i = 0; i < indices.length; i++) {
        final String indexName = randomAlphaOfLength(10).toLowerCase(Locale.ROOT);
        createIndex(indexName);
        if (randomBoolean()) {
            indexRandom(randomBoolean(), false, randomBoolean(), IntStream.range(0, 10).mapToObj(n -> client().prepareIndex(indexName).setId(String.valueOf(n)).setSource("num", n)).collect(toList()));
        }
        indices[i] = indexName;
    }
    assertThat(client().admin().cluster().prepareState().get().getState().metadata().indices().size(), equalTo(indices.length));
    final List<Thread> threads = new ArrayList<>();
    final CountDownLatch latch = new CountDownLatch(1);
    final APIBlock block = randomAddableBlock();
    Consumer<Exception> exceptionConsumer = t -> {
        Throwable cause = ExceptionsHelper.unwrapCause(t);
        if (cause instanceof ClusterBlockException) {
            ClusterBlockException e = (ClusterBlockException) cause;
            assertThat(e.blocks(), hasSize(1));
            assertTrue(e.blocks().stream().allMatch(b -> b.id() == block.getBlock().id()));
        } else {
            assertThat(cause, instanceOf(IndexNotFoundException.class));
        }
    };
    try {
        for (final String indexToDelete : indices) {
            threads.add(new Thread(() -> {
                try {
                    latch.await();
                } catch (InterruptedException e) {
                    throw new AssertionError(e);
                }
                try {
                    assertAcked(client().admin().indices().prepareDelete(indexToDelete));
                } catch (final Exception e) {
                    exceptionConsumer.accept(e);
                }
            }));
        }
        for (final String indexToBlock : indices) {
            threads.add(new Thread(() -> {
                try {
                    latch.await();
                } catch (InterruptedException e) {
                    throw new AssertionError(e);
                }
                try {
                    client().admin().indices().prepareAddBlock(block, indexToBlock).get();
                } catch (final Exception e) {
                    exceptionConsumer.accept(e);
                }
            }));
        }
        for (Thread thread : threads) {
            thread.start();
        }
        latch.countDown();
        for (Thread thread : threads) {
            thread.join();
        }
    } finally {
        for (final String indexToBlock : indices) {
            try {
                disableIndexBlock(indexToBlock, block);
            } catch (IndexNotFoundException infe) {
            // ignore
            }
        }
    }
}
Also used : IndexRequestBuilder(org.opensearch.action.index.IndexRequestBuilder) IntStream(java.util.stream.IntStream) APIBlock(org.opensearch.cluster.metadata.IndexMetadata.APIBlock) Arrays(java.util.Arrays) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) IndexResponse(org.opensearch.action.index.IndexResponse) HashMap(java.util.HashMap) ActionRequestValidationException(org.opensearch.action.ActionRequestValidationException) AddIndexBlockRequestBuilder(org.opensearch.action.admin.indices.readonly.AddIndexBlockRequestBuilder) ArrayList(java.util.ArrayList) ClusterState(org.opensearch.cluster.ClusterState) OpenSearchAssertions.assertHitCount(org.opensearch.test.hamcrest.OpenSearchAssertions.assertHitCount) Locale(java.util.Locale) Matchers.hasSize(org.hamcrest.Matchers.hasSize) BackgroundIndexer(org.opensearch.test.BackgroundIndexer) TRACK_TOTAL_HITS_ACCURATE(org.opensearch.search.internal.SearchContext.TRACK_TOTAL_HITS_ACCURATE) IndicesOptions.lenientExpandOpen(org.opensearch.action.support.IndicesOptions.lenientExpandOpen) OpenSearchAssertions.assertAcked(org.opensearch.test.hamcrest.OpenSearchAssertions.assertAcked) OpenSearchAssertions.assertBlocked(org.opensearch.test.hamcrest.OpenSearchAssertions.assertBlocked) Collections.emptySet(java.util.Collections.emptySet) Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) ClusterBlockLevel(org.opensearch.cluster.block.ClusterBlockLevel) IndexNotFoundException(org.opensearch.index.IndexNotFoundException) ExceptionsHelper(org.opensearch.ExceptionsHelper) ClusterBlockException(org.opensearch.cluster.block.ClusterBlockException) Settings(org.opensearch.common.settings.Settings) CreateIndexResponse(org.opensearch.action.admin.indices.create.CreateIndexResponse) ActiveShardCount(org.opensearch.action.support.ActiveShardCount) AcknowledgedResponse(org.opensearch.action.support.master.AcknowledgedResponse) ShardRouting(org.opensearch.cluster.routing.ShardRouting) Consumer(java.util.function.Consumer) SETTING_READ_ONLY(org.opensearch.cluster.metadata.IndexMetadata.SETTING_READ_ONLY) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) SETTING_BLOCKS_WRITE(org.opensearch.cluster.metadata.IndexMetadata.SETTING_BLOCKS_WRITE) Matchers.equalTo(org.hamcrest.Matchers.equalTo) UpdateSettingsRequestBuilder(org.opensearch.action.admin.indices.settings.put.UpdateSettingsRequestBuilder) Matchers.is(org.hamcrest.Matchers.is) IndicesExistsResponse(org.opensearch.action.admin.indices.exists.indices.IndicesExistsResponse) OpenSearchIntegTestCase(org.opensearch.test.OpenSearchIntegTestCase) Matchers.containsString(org.hamcrest.Matchers.containsString) ArrayList(java.util.ArrayList) Matchers.containsString(org.hamcrest.Matchers.containsString) CountDownLatch(java.util.concurrent.CountDownLatch) ClusterBlockException(org.opensearch.cluster.block.ClusterBlockException) ActionRequestValidationException(org.opensearch.action.ActionRequestValidationException) IndexNotFoundException(org.opensearch.index.IndexNotFoundException) ClusterBlockException(org.opensearch.cluster.block.ClusterBlockException) APIBlock(org.opensearch.cluster.metadata.IndexMetadata.APIBlock) IndexNotFoundException(org.opensearch.index.IndexNotFoundException)

Example 83 with IndexNotFoundException

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

the class SearchService method putReaderContext.

protected void putReaderContext(ReaderContext context) {
    final ReaderContext previous = activeReaders.put(context.id().getId(), context);
    assert previous == null;
    // ensure that if we race against afterIndexRemoved, we remove the context from the active list.
    // this is important to ensure store can be cleaned up, in particular if the search is a scroll with a long timeout.
    final Index index = context.indexShard().shardId().getIndex();
    if (indicesService.hasIndex(index) == false) {
        removeReaderContext(context.id().getId());
        throw new IndexNotFoundException(index);
    }
}
Also used : LegacyReaderContext(org.opensearch.search.internal.LegacyReaderContext) ReaderContext(org.opensearch.search.internal.ReaderContext) IndexNotFoundException(org.opensearch.index.IndexNotFoundException) Index(org.opensearch.index.Index)

Example 84 with IndexNotFoundException

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

the class TransportBulkActionTests method testDeleteNonExistingDocDoesNotCreateIndex.

public void testDeleteNonExistingDocDoesNotCreateIndex() throws Exception {
    BulkRequest bulkRequest = new BulkRequest().add(new DeleteRequest("index", "id"));
    PlainActionFuture<BulkResponse> future = PlainActionFuture.newFuture();
    ActionTestUtils.execute(bulkAction, null, bulkRequest, future);
    BulkResponse response = future.actionGet();
    assertFalse(bulkAction.indexCreated);
    BulkItemResponse[] bulkResponses = ((BulkResponse) response).getItems();
    assertEquals(bulkResponses.length, 1);
    assertTrue(bulkResponses[0].isFailed());
    assertTrue(bulkResponses[0].getFailure().getCause() instanceof IndexNotFoundException);
    assertEquals("index", bulkResponses[0].getFailure().getIndex());
}
Also used : IndexNotFoundException(org.opensearch.index.IndexNotFoundException) DeleteRequest(org.opensearch.action.delete.DeleteRequest)

Example 85 with IndexNotFoundException

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

the class TransportReplicationActionTests method testBlocksInReroutePhase.

public void testBlocksInReroutePhase() {
    final ClusterBlock nonRetryableBlock = new ClusterBlock(1, "non retryable", false, true, false, RestStatus.SERVICE_UNAVAILABLE, ClusterBlockLevel.ALL);
    final ClusterBlock retryableBlock = new ClusterBlock(1, "retryable", true, true, false, RestStatus.SERVICE_UNAVAILABLE, ClusterBlockLevel.ALL);
    final boolean globalBlock = randomBoolean();
    final TestAction action = new TestAction(Settings.EMPTY, "internal:testActionWithBlocks", transportService, clusterService, shardStateAction, threadPool) {

        @Override
        protected ClusterBlockLevel globalBlockLevel() {
            return globalBlock ? ClusterBlockLevel.WRITE : null;
        }

        @Override
        public ClusterBlockLevel indexBlockLevel() {
            return globalBlock == false ? ClusterBlockLevel.WRITE : null;
        }
    };
    setState(clusterService, ClusterStateCreationUtils.stateWithActivePrimary("index", true, 0));
    ShardId shardId = new ShardId(clusterService.state().metadata().index("index").getIndex(), 0);
    {
        setStateWithBlock(clusterService, nonRetryableBlock, globalBlock);
        Request request = new Request(shardId);
        PlainActionFuture<TestResponse> listener = new PlainActionFuture<>();
        ReplicationTask task = maybeTask();
        TestAction.ReroutePhase reroutePhase = action.new ReroutePhase(task, request, listener);
        reroutePhase.run();
        ClusterBlockException exception = assertListenerThrows("primary action should fail operation", listener, ClusterBlockException.class);
        assertThat(((ClusterBlockException) exception.unwrapCause()).blocks().iterator().next(), is(nonRetryableBlock));
        assertPhase(task, "failed");
    }
    {
        setStateWithBlock(clusterService, retryableBlock, globalBlock);
        Request requestWithTimeout = (globalBlock ? new Request(shardId) : new Request(shardId)).timeout("5ms");
        PlainActionFuture<TestResponse> listener = new PlainActionFuture<>();
        ReplicationTask task = maybeTask();
        TestAction.ReroutePhase reroutePhase = action.new ReroutePhase(task, requestWithTimeout, listener);
        reroutePhase.run();
        ClusterBlockException exception = assertListenerThrows("failed to timeout on retryable block", listener, ClusterBlockException.class);
        assertThat(((ClusterBlockException) exception.unwrapCause()).blocks().iterator().next(), is(retryableBlock));
        assertPhase(task, "failed");
        assertTrue(requestWithTimeout.isRetrySet.get());
    }
    {
        setStateWithBlock(clusterService, retryableBlock, globalBlock);
        Request request = new Request(shardId);
        PlainActionFuture<TestResponse> listener = new PlainActionFuture<>();
        ReplicationTask task = maybeTask();
        TestAction.ReroutePhase reroutePhase = action.new ReroutePhase(task, request, listener);
        reroutePhase.run();
        assertFalse("primary phase should wait on retryable block", listener.isDone());
        assertPhase(task, "waiting_for_retry");
        assertTrue(request.isRetrySet.get());
        setStateWithBlock(clusterService, nonRetryableBlock, globalBlock);
        ClusterBlockException exception = assertListenerThrows("primary phase should fail operation when moving from a retryable " + "block to a non-retryable one", listener, ClusterBlockException.class);
        assertThat(((ClusterBlockException) exception.unwrapCause()).blocks().iterator().next(), is(nonRetryableBlock));
        assertIndexShardUninitialized();
    }
    {
        Request requestWithTimeout = new Request(new ShardId("unknown", "_na_", 0)).index("unknown").timeout("5ms");
        PlainActionFuture<TestResponse> listener = new PlainActionFuture<>();
        ReplicationTask task = maybeTask();
        TestAction testActionWithNoBlocks = new TestAction(Settings.EMPTY, "internal:testActionWithNoBlocks", transportService, clusterService, shardStateAction, threadPool);
        TestAction.ReroutePhase reroutePhase = testActionWithNoBlocks.new ReroutePhase(task, requestWithTimeout, listener);
        reroutePhase.run();
        assertListenerThrows("should fail with an IndexNotFoundException when no blocks", listener, IndexNotFoundException.class);
    }
}
Also used : ClusterBlock(org.opensearch.cluster.block.ClusterBlock) ShardId(org.opensearch.index.shard.ShardId) PlainActionFuture(org.opensearch.action.support.PlainActionFuture) CreateIndexRequest(org.opensearch.action.admin.indices.create.CreateIndexRequest) CloseIndexRequest(org.opensearch.action.admin.indices.close.CloseIndexRequest) TransportRequest(org.opensearch.transport.TransportRequest) IndexNotFoundException(org.opensearch.index.IndexNotFoundException) ClusterBlockException(org.opensearch.cluster.block.ClusterBlockException)

Aggregations

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