Search in sources :

Example 1 with IndexClosedException

use of org.opensearch.indices.IndexClosedException in project OpenSearch by opensearch-project.

the class RankEvalRequestIT method testIndicesOptions.

/**
 * test that multiple indices work, setting indices Options is possible and works as expected
 */
public void testIndicesOptions() {
    SearchSourceBuilder amsterdamQuery = new SearchSourceBuilder().query(new MatchAllQueryBuilder());
    List<RatedDocument> relevantDocs = createRelevant("2", "3", "4", "5", "6");
    relevantDocs.add(new RatedDocument("test2", "7", RELEVANT_RATING_1));
    List<RatedRequest> specifications = new ArrayList<>();
    specifications.add(new RatedRequest("amsterdam_query", relevantDocs, amsterdamQuery));
    RankEvalSpec task = new RankEvalSpec(specifications, new PrecisionAtK());
    RankEvalRequest request = new RankEvalRequest(task, new String[] { TEST_INDEX, "test2" });
    request.setRankEvalSpec(task);
    RankEvalResponse response = client().execute(RankEvalAction.INSTANCE, request).actionGet();
    Detail details = (PrecisionAtK.Detail) response.getPartialResults().get("amsterdam_query").getMetricDetails();
    assertEquals(7, details.getRetrieved());
    assertEquals(6, details.getRelevantRetrieved());
    // test that ignore_unavailable=true works but returns one result less
    assertTrue(client().admin().indices().prepareClose("test2").get().isAcknowledged());
    request.indicesOptions(IndicesOptions.fromParameters(null, "true", null, "false", SearchRequest.DEFAULT_INDICES_OPTIONS));
    response = client().execute(RankEvalAction.INSTANCE, request).actionGet();
    details = (PrecisionAtK.Detail) response.getPartialResults().get("amsterdam_query").getMetricDetails();
    assertEquals(6, details.getRetrieved());
    assertEquals(5, details.getRelevantRetrieved());
    // test that ignore_unavailable=false or default settings throw an IndexClosedException
    assertTrue(client().admin().indices().prepareClose("test2").get().isAcknowledged());
    request.indicesOptions(IndicesOptions.fromParameters(null, "false", null, "false", SearchRequest.DEFAULT_INDICES_OPTIONS));
    response = client().execute(RankEvalAction.INSTANCE, request).actionGet();
    assertEquals(1, response.getFailures().size());
    assertThat(response.getFailures().get("amsterdam_query"), instanceOf(IndexClosedException.class));
    // test expand_wildcards
    request = new RankEvalRequest(task, new String[] { "tes*" });
    request.indicesOptions(IndicesOptions.fromParameters("none", null, null, "false", SearchRequest.DEFAULT_INDICES_OPTIONS));
    response = client().execute(RankEvalAction.INSTANCE, request).actionGet();
    details = (PrecisionAtK.Detail) response.getPartialResults().get("amsterdam_query").getMetricDetails();
    assertEquals(0, details.getRetrieved());
    request.indicesOptions(IndicesOptions.fromParameters("open", null, null, "false", SearchRequest.DEFAULT_INDICES_OPTIONS));
    response = client().execute(RankEvalAction.INSTANCE, request).actionGet();
    details = (PrecisionAtK.Detail) response.getPartialResults().get("amsterdam_query").getMetricDetails();
    assertEquals(6, details.getRetrieved());
    assertEquals(5, details.getRelevantRetrieved());
    request.indicesOptions(IndicesOptions.fromParameters("closed", null, null, "false", SearchRequest.DEFAULT_INDICES_OPTIONS));
    response = client().execute(RankEvalAction.INSTANCE, request).actionGet();
    assertEquals(1, response.getFailures().size());
    assertThat(response.getFailures().get("amsterdam_query"), instanceOf(IndexClosedException.class));
    // test allow_no_indices
    request = new RankEvalRequest(task, new String[] { "bad*" });
    request.indicesOptions(IndicesOptions.fromParameters(null, null, "true", "false", SearchRequest.DEFAULT_INDICES_OPTIONS));
    response = client().execute(RankEvalAction.INSTANCE, request).actionGet();
    details = (PrecisionAtK.Detail) response.getPartialResults().get("amsterdam_query").getMetricDetails();
    assertEquals(0, details.getRetrieved());
    request.indicesOptions(IndicesOptions.fromParameters(null, null, "false", "false", SearchRequest.DEFAULT_INDICES_OPTIONS));
    response = client().execute(RankEvalAction.INSTANCE, request).actionGet();
    assertEquals(1, response.getFailures().size());
    assertThat(response.getFailures().get("amsterdam_query"), instanceOf(IndexNotFoundException.class));
}
Also used : ArrayList(java.util.ArrayList) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder) IndexClosedException(org.opensearch.indices.IndexClosedException) IndexNotFoundException(org.opensearch.index.IndexNotFoundException) Detail(org.opensearch.index.rankeval.PrecisionAtK.Detail) MatchAllQueryBuilder(org.opensearch.index.query.MatchAllQueryBuilder)

Example 2 with IndexClosedException

use of org.opensearch.indices.IndexClosedException in project OpenSearch by opensearch-project.

the class CloseIndexIT method assertException.

static void assertException(final Throwable throwable, final String indexName) {
    final Throwable t = ExceptionsHelper.unwrapCause(throwable);
    if (t instanceof ClusterBlockException) {
        ClusterBlockException clusterBlockException = (ClusterBlockException) t;
        assertThat(clusterBlockException.blocks(), hasSize(1));
        assertTrue(clusterBlockException.blocks().stream().allMatch(b -> b.id() == MetadataIndexStateService.INDEX_CLOSED_BLOCK_ID));
    } else if (t instanceof IndexClosedException) {
        IndexClosedException indexClosedException = (IndexClosedException) t;
        assertThat(indexClosedException.getIndex(), notNullValue());
        assertThat(indexClosedException.getIndex().getName(), equalTo(indexName));
    } else if (t instanceof IndexNotFoundException) {
        IndexNotFoundException indexNotFoundException = (IndexNotFoundException) t;
        assertThat(indexNotFoundException.getIndex(), notNullValue());
        assertThat(indexNotFoundException.getIndex().getName(), equalTo(indexName));
    } else {
        fail("Unexpected exception: " + t);
    }
}
Also used : IntStream(java.util.stream.IntStream) Arrays(java.util.Arrays) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) Matchers.not(org.hamcrest.Matchers.not) ByteSizeUnit(org.opensearch.common.unit.ByteSizeUnit) ByteSizeValue(org.opensearch.common.unit.ByteSizeValue) InternalTestCluster(org.opensearch.test.InternalTestCluster) ActionRequestValidationException(org.opensearch.action.ActionRequestValidationException) ArrayList(java.util.ArrayList) CloseIndexResponse(org.opensearch.action.admin.indices.close.CloseIndexResponse) ClusterState(org.opensearch.cluster.ClusterState) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) IndexShard(org.opensearch.index.shard.IndexShard) OpenSearchAssertions.assertHitCount(org.opensearch.test.hamcrest.OpenSearchAssertions.assertHitCount) IndexClosedException(org.opensearch.indices.IndexClosedException) RecoveryState(org.opensearch.indices.recovery.RecoveryState) Locale(java.util.Locale) Matchers.nullValue(org.hamcrest.Matchers.nullValue) Matchers.hasSize(org.hamcrest.Matchers.hasSize) BackgroundIndexer(org.opensearch.test.BackgroundIndexer) ClusterHealthStatus(org.opensearch.cluster.health.ClusterHealthStatus) MetadataIndexStateService(org.opensearch.cluster.metadata.MetadataIndexStateService) 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) Client(org.opensearch.client.Client) Matchers.empty(org.hamcrest.Matchers.empty) Collections.emptySet(java.util.Collections.emptySet) Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) IndexNotFoundException(org.opensearch.index.IndexNotFoundException) CloseIndexRequestBuilder(org.opensearch.action.admin.indices.close.CloseIndexRequestBuilder) IndicesService(org.opensearch.indices.IndicesService) ExceptionsHelper(org.opensearch.ExceptionsHelper) ClusterBlockException(org.opensearch.cluster.block.ClusterBlockException) Settings(org.opensearch.common.settings.Settings) Collectors(java.util.stream.Collectors) ActiveShardCount(org.opensearch.action.support.ActiveShardCount) ShardRouting(org.opensearch.cluster.routing.ShardRouting) CountDownLatch(java.util.concurrent.CountDownLatch) Sets(org.opensearch.common.util.set.Sets) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) Matchers.equalTo(org.hamcrest.Matchers.equalTo) IndexSettings(org.opensearch.index.IndexSettings) Matchers.is(org.hamcrest.Matchers.is) OpenSearchIntegTestCase(org.opensearch.test.OpenSearchIntegTestCase) Matchers.containsString(org.hamcrest.Matchers.containsString) IndexClosedException(org.opensearch.indices.IndexClosedException) IndexNotFoundException(org.opensearch.index.IndexNotFoundException) ClusterBlockException(org.opensearch.cluster.block.ClusterBlockException)

Example 3 with IndexClosedException

use of org.opensearch.indices.IndexClosedException in project OpenSearch by opensearch-project.

the class TransportValidateQueryAction method doExecute.

@Override
protected void doExecute(Task task, ValidateQueryRequest request, ActionListener<ValidateQueryResponse> listener) {
    request.nowInMillis = System.currentTimeMillis();
    LongSupplier timeProvider = () -> request.nowInMillis;
    ActionListener<org.opensearch.index.query.QueryBuilder> rewriteListener = ActionListener.wrap(rewrittenQuery -> {
        request.query(rewrittenQuery);
        super.doExecute(task, request, listener);
    }, ex -> {
        if (ex instanceof IndexNotFoundException || ex instanceof IndexClosedException) {
            listener.onFailure(ex);
            return;
        }
        List<QueryExplanation> explanations = new ArrayList<>();
        explanations.add(new QueryExplanation(null, QueryExplanation.RANDOM_SHARD, false, null, ex.getMessage()));
        listener.onResponse(new ValidateQueryResponse(false, explanations, // which is 0 since the failure is happening on the coordinating node.
        0, 0, 0, null));
    });
    if (request.query() == null) {
        rewriteListener.onResponse(request.query());
    } else {
        Rewriteable.rewriteAndFetch(request.query(), searchService.getRewriteContext(timeProvider), rewriteListener);
    }
}
Also used : IndexClosedException(org.opensearch.indices.IndexClosedException) ArrayList(java.util.ArrayList) IndexNotFoundException(org.opensearch.index.IndexNotFoundException) LongSupplier(java.util.function.LongSupplier)

Example 4 with IndexClosedException

use of org.opensearch.indices.IndexClosedException in project OpenSearch by opensearch-project.

the class SimpleIndexStateIT method testSimpleOpenClose.

public void testSimpleOpenClose() {
    logger.info("--> creating test index");
    createIndex("test");
    logger.info("--> waiting for green status");
    ensureGreen();
    NumShards numShards = getNumShards("test");
    ClusterStateResponse stateResponse = client().admin().cluster().prepareState().get();
    assertThat(stateResponse.getState().metadata().index("test").getState(), equalTo(IndexMetadata.State.OPEN));
    assertThat(stateResponse.getState().routingTable().index("test").shards().size(), equalTo(numShards.numPrimaries));
    assertEquals(stateResponse.getState().routingTable().index("test").shardsWithState(ShardRoutingState.STARTED).size(), numShards.totalNumShards);
    logger.info("--> indexing a simple document");
    client().prepareIndex("test").setId("1").setSource("field1", "value1").get();
    logger.info("--> closing test index...");
    assertAcked(client().admin().indices().prepareClose("test"));
    stateResponse = client().admin().cluster().prepareState().get();
    assertThat(stateResponse.getState().metadata().index("test").getState(), equalTo(IndexMetadata.State.CLOSE));
    assertThat(stateResponse.getState().routingTable().index("test"), notNullValue());
    logger.info("--> trying to index into a closed index ...");
    try {
        client().prepareIndex("test").setId("1").setSource("field1", "value1").get();
        fail();
    } catch (IndexClosedException e) {
    // all is well
    }
    logger.info("--> opening index...");
    OpenIndexResponse openIndexResponse = client().admin().indices().prepareOpen("test").get();
    assertThat(openIndexResponse.isAcknowledged(), equalTo(true));
    logger.info("--> waiting for green status");
    ensureGreen();
    stateResponse = client().admin().cluster().prepareState().get();
    assertThat(stateResponse.getState().metadata().index("test").getState(), equalTo(IndexMetadata.State.OPEN));
    assertThat(stateResponse.getState().routingTable().index("test").shards().size(), equalTo(numShards.numPrimaries));
    assertEquals(stateResponse.getState().routingTable().index("test").shardsWithState(ShardRoutingState.STARTED).size(), numShards.totalNumShards);
    logger.info("--> indexing a simple document");
    client().prepareIndex("test").setId("1").setSource("field1", "value1").get();
}
Also used : IndexClosedException(org.opensearch.indices.IndexClosedException) ClusterStateResponse(org.opensearch.action.admin.cluster.state.ClusterStateResponse) OpenIndexResponse(org.opensearch.action.admin.indices.open.OpenIndexResponse)

Example 5 with IndexClosedException

use of org.opensearch.indices.IndexClosedException in project OpenSearch by opensearch-project.

the class GatewayIndexStateIT method testSimpleOpenClose.

public void testSimpleOpenClose() throws Exception {
    logger.info("--> starting 2 nodes");
    internalCluster().startNodes(2);
    logger.info("--> creating test index");
    createIndex("test");
    NumShards test = getNumShards("test");
    logger.info("--> waiting for green status");
    ensureGreen();
    ClusterStateResponse stateResponse = client().admin().cluster().prepareState().execute().actionGet();
    assertThat(stateResponse.getState().metadata().index("test").getState(), equalTo(IndexMetadata.State.OPEN));
    assertThat(stateResponse.getState().routingTable().index("test").shards().size(), equalTo(test.numPrimaries));
    assertThat(stateResponse.getState().routingTable().index("test").shardsWithState(ShardRoutingState.STARTED).size(), equalTo(test.totalNumShards));
    logger.info("--> indexing a simple document");
    client().prepareIndex("test").setId("1").setSource("field1", "value1").get();
    logger.info("--> closing test index...");
    assertAcked(client().admin().indices().prepareClose("test"));
    stateResponse = client().admin().cluster().prepareState().execute().actionGet();
    assertThat(stateResponse.getState().metadata().index("test").getState(), equalTo(IndexMetadata.State.CLOSE));
    assertThat(stateResponse.getState().routingTable().index("test"), notNullValue());
    logger.info("--> verifying that the state is green");
    ensureGreen();
    logger.info("--> trying to index into a closed index ...");
    try {
        client().prepareIndex("test").setId("1").setSource("field1", "value1").execute().actionGet();
        fail();
    } catch (IndexClosedException e) {
    // all is well
    }
    logger.info("--> creating another index (test2) by indexing into it");
    client().prepareIndex("test2").setId("1").setSource("field1", "value1").execute().actionGet();
    logger.info("--> verifying that the state is green");
    ensureGreen();
    logger.info("--> opening the first index again...");
    assertAcked(client().admin().indices().prepareOpen("test"));
    logger.info("--> verifying that the state is green");
    ensureGreen();
    stateResponse = client().admin().cluster().prepareState().execute().actionGet();
    assertThat(stateResponse.getState().metadata().index("test").getState(), equalTo(IndexMetadata.State.OPEN));
    assertThat(stateResponse.getState().routingTable().index("test").shards().size(), equalTo(test.numPrimaries));
    assertThat(stateResponse.getState().routingTable().index("test").shardsWithState(ShardRoutingState.STARTED).size(), equalTo(test.totalNumShards));
    logger.info("--> trying to get the indexed document on the first index");
    GetResponse getResponse = client().prepareGet("test", "1").execute().actionGet();
    assertThat(getResponse.isExists(), equalTo(true));
    logger.info("--> closing test index...");
    assertAcked(client().admin().indices().prepareClose("test"));
    stateResponse = client().admin().cluster().prepareState().execute().actionGet();
    assertThat(stateResponse.getState().metadata().index("test").getState(), equalTo(IndexMetadata.State.CLOSE));
    assertThat(stateResponse.getState().routingTable().index("test"), notNullValue());
    logger.info("--> restarting nodes...");
    internalCluster().fullRestart();
    logger.info("--> waiting for two nodes and green status");
    ensureGreen();
    stateResponse = client().admin().cluster().prepareState().execute().actionGet();
    assertThat(stateResponse.getState().metadata().index("test").getState(), equalTo(IndexMetadata.State.CLOSE));
    assertThat(stateResponse.getState().routingTable().index("test"), notNullValue());
    logger.info("--> trying to index into a closed index ...");
    try {
        client().prepareIndex("test").setId("1").setSource("field1", "value1").execute().actionGet();
        fail();
    } catch (IndexClosedException e) {
    // all is well
    }
    logger.info("--> opening index...");
    client().admin().indices().prepareOpen("test").execute().actionGet();
    logger.info("--> waiting for green status");
    ensureGreen();
    stateResponse = client().admin().cluster().prepareState().execute().actionGet();
    assertThat(stateResponse.getState().metadata().index("test").getState(), equalTo(IndexMetadata.State.OPEN));
    assertThat(stateResponse.getState().routingTable().index("test").shards().size(), equalTo(test.numPrimaries));
    assertThat(stateResponse.getState().routingTable().index("test").shardsWithState(ShardRoutingState.STARTED).size(), equalTo(test.totalNumShards));
    logger.info("--> trying to get the indexed document on the first round (before close and shutdown)");
    getResponse = client().prepareGet("test", "1").execute().actionGet();
    assertThat(getResponse.isExists(), equalTo(true));
    logger.info("--> indexing a simple document");
    client().prepareIndex("test").setId("2").setSource("field1", "value1").execute().actionGet();
}
Also used : IndexClosedException(org.opensearch.indices.IndexClosedException) ClusterStateResponse(org.opensearch.action.admin.cluster.state.ClusterStateResponse) GetResponse(org.opensearch.action.get.GetResponse)

Aggregations

IndexClosedException (org.opensearch.indices.IndexClosedException)7 IndexNotFoundException (org.opensearch.index.IndexNotFoundException)4 ArrayList (java.util.ArrayList)3 Matchers.containsString (org.hamcrest.Matchers.containsString)3 ClusterState (org.opensearch.cluster.ClusterState)3 ClusterStateResponse (org.opensearch.action.admin.cluster.state.ClusterStateResponse)2 ClusterName (org.opensearch.cluster.ClusterName)2 ThreadContext (org.opensearch.common.util.concurrent.ThreadContext)2 Arrays (java.util.Arrays)1 Collections.emptySet (java.util.Collections.emptySet)1 List (java.util.List)1 Locale (java.util.Locale)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 LongSupplier (java.util.function.LongSupplier)1 Collectors (java.util.stream.Collectors)1 Collectors.toList (java.util.stream.Collectors.toList)1 IntStream (java.util.stream.IntStream)1 Matchers.empty (org.hamcrest.Matchers.empty)1 Matchers.equalTo (org.hamcrest.Matchers.equalTo)1 Matchers.hasSize (org.hamcrest.Matchers.hasSize)1