Search in sources :

Example 6 with ShardSearchFailure

use of org.opensearch.action.search.ShardSearchFailure in project OpenSearch by opensearch-project.

the class StatsIT method assertShardExecutionState.

private void assertShardExecutionState(SearchResponse response, int expectedFailures) throws Exception {
    ShardSearchFailure[] failures = response.getShardFailures();
    if (failures.length != expectedFailures) {
        for (ShardSearchFailure failure : failures) {
            logger.error(new ParameterizedMessage("Shard Failure: {}", failure), failure.getCause());
        }
        fail("Unexpected shard failures!");
    }
    assertThat("Not all shards are initialized", response.getSuccessfulShards(), equalTo(response.getTotalShards()));
}
Also used : ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) ShardSearchFailure(org.opensearch.action.search.ShardSearchFailure)

Example 7 with ShardSearchFailure

use of org.opensearch.action.search.ShardSearchFailure in project OpenSearch by opensearch-project.

the class OpenSearchExceptionTests method testFailureToAndFromXContentWithDetails.

public void testFailureToAndFromXContentWithDetails() throws IOException {
    final XContent xContent = randomFrom(XContentType.values()).xContent();
    Exception failure;
    Throwable failureCause;
    OpenSearchException expected;
    OpenSearchException expectedCause;
    OpenSearchException suppressed;
    switch(randomIntBetween(0, 6)) {
        case // Simple opensearch exception without cause
        0:
            failure = new NoNodeAvailableException("A");
            expected = new OpenSearchException("OpenSearch exception [type=no_node_available_exception, reason=A]");
            expected.addSuppressed(new OpenSearchException("OpenSearch exception [type=no_node_available_exception, reason=A]"));
            break;
        case // Simple opensearch exception with headers (other metadata of type number are not parsed)
        1:
            failure = new ParsingException(3, 2, "B", null);
            ((OpenSearchException) failure).addHeader("header_name", "0", "1");
            expected = new OpenSearchException("OpenSearch exception [type=parsing_exception, reason=B]");
            expected.addHeader("header_name", "0", "1");
            suppressed = new OpenSearchException("OpenSearch exception [type=parsing_exception, reason=B]");
            suppressed.addHeader("header_name", "0", "1");
            expected.addSuppressed(suppressed);
            break;
        case // OpenSearch exception with a cause, headers and parsable metadata
        2:
            failureCause = new NullPointerException("var is null");
            failure = new ScriptException("C", failureCause, singletonList("stack"), "test", "painless");
            ((OpenSearchException) failure).addHeader("script_name", "my_script");
            expectedCause = new OpenSearchException("OpenSearch exception [type=null_pointer_exception, reason=var is null]");
            expected = new OpenSearchException("OpenSearch exception [type=script_exception, reason=C]", expectedCause);
            expected.addHeader("script_name", "my_script");
            expected.addMetadata("opensearch.lang", "painless");
            expected.addMetadata("opensearch.script", "test");
            expected.addMetadata("opensearch.script_stack", "stack");
            suppressed = new OpenSearchException("OpenSearch exception [type=script_exception, reason=C]");
            suppressed.addHeader("script_name", "my_script");
            suppressed.addMetadata("opensearch.lang", "painless");
            suppressed.addMetadata("opensearch.script", "test");
            suppressed.addMetadata("opensearch.script_stack", "stack");
            expected.addSuppressed(suppressed);
            break;
        case // JDK exception without cause
        3:
            failure = new IllegalStateException("D");
            expected = new OpenSearchException("OpenSearch exception [type=illegal_state_exception, reason=D]");
            suppressed = new OpenSearchException("OpenSearch exception [type=illegal_state_exception, reason=D]");
            expected.addSuppressed(suppressed);
            break;
        case // JDK exception with cause
        4:
            failureCause = new RoutingMissingException("idx", "id");
            failure = new RuntimeException("E", failureCause);
            expectedCause = new OpenSearchException("OpenSearch exception [type=routing_missing_exception, " + "reason=routing is required for [idx]/[id]]");
            expectedCause.addMetadata("opensearch.index", "idx");
            expectedCause.addMetadata("opensearch.index_uuid", "_na_");
            expected = new OpenSearchException("OpenSearch exception [type=runtime_exception, reason=E]", expectedCause);
            suppressed = new OpenSearchException("OpenSearch exception [type=runtime_exception, reason=E]");
            expected.addSuppressed(suppressed);
            break;
        case // Wrapped exception with cause
        5:
            failureCause = new FileAlreadyExistsException("File exists");
            failure = new BroadcastShardOperationFailedException(new ShardId("_index", "_uuid", 5), "F", failureCause);
            expected = new OpenSearchException("OpenSearch exception [type=file_already_exists_exception, reason=File exists]");
            suppressed = new OpenSearchException("OpenSearch exception [type=file_already_exists_exception, reason=File exists]");
            expected.addSuppressed(suppressed);
            break;
        case // SearchPhaseExecutionException with cause and multiple failures
        6:
            DiscoveryNode node = new DiscoveryNode("node_g", buildNewFakeTransportAddress(), Version.CURRENT);
            failureCause = new NodeClosedException(node);
            failureCause = new NoShardAvailableActionException(new ShardId("_index_g", "_uuid_g", 6), "node_g", failureCause);
            ShardSearchFailure[] shardFailures = new ShardSearchFailure[] { new ShardSearchFailure(new ParsingException(0, 0, "Parsing g", null), new SearchShardTarget("node_g", new ShardId(new Index("_index_g", "_uuid_g"), 61), null, OriginalIndices.NONE)), new ShardSearchFailure(new RepositoryException("repository_g", "Repo"), new SearchShardTarget("node_g", new ShardId(new Index("_index_g", "_uuid_g"), 62), null, OriginalIndices.NONE)), new ShardSearchFailure(new SearchContextMissingException(new ShardSearchContextId(UUIDs.randomBase64UUID(), 0L)), null) };
            failure = new SearchPhaseExecutionException("phase_g", "G", failureCause, shardFailures);
            expectedCause = new OpenSearchException("OpenSearch exception [type=node_closed_exception, " + "reason=node closed " + node + "]");
            expectedCause = new OpenSearchException("OpenSearch exception [type=no_shard_available_action_exception, " + "reason=node_g]", expectedCause);
            expectedCause.addMetadata("opensearch.index", "_index_g");
            expectedCause.addMetadata("opensearch.index_uuid", "_uuid_g");
            expectedCause.addMetadata("opensearch.shard", "6");
            expected = new OpenSearchException("OpenSearch exception [type=search_phase_execution_exception, " + "reason=G]", expectedCause);
            expected.addMetadata("opensearch.phase", "phase_g");
            expected.addSuppressed(new OpenSearchException("OpenSearch exception [type=parsing_exception, reason=Parsing g]"));
            expected.addSuppressed(new OpenSearchException("OpenSearch exception [type=repository_exception, " + "reason=[repository_g] Repo]"));
            expected.addSuppressed(new OpenSearchException("OpenSearch exception [type=search_context_missing_exception, " + "reason=No search context found for id [0]]"));
            break;
        default:
            throw new UnsupportedOperationException("Failed to generate randomized failure");
    }
    Exception finalFailure = failure;
    BytesReference failureBytes = toShuffledXContent((builder, params) -> {
        OpenSearchException.generateFailureXContent(builder, params, finalFailure, true);
        return builder;
    }, xContent.type(), ToXContent.EMPTY_PARAMS, randomBoolean());
    try (XContentParser parser = createParser(xContent, failureBytes)) {
        failureBytes = BytesReference.bytes(shuffleXContent(parser, randomBoolean()));
    }
    OpenSearchException parsedFailure;
    try (XContentParser parser = createParser(xContent, failureBytes)) {
        assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken());
        assertEquals(XContentParser.Token.FIELD_NAME, parser.nextToken());
        parsedFailure = OpenSearchException.failureFromXContent(parser);
        assertEquals(XContentParser.Token.END_OBJECT, parser.nextToken());
        assertNull(parser.nextToken());
    }
    assertDeepEquals(expected, parsedFailure);
}
Also used : FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) SearchPhaseExecutionException(org.opensearch.action.search.SearchPhaseExecutionException) Index(org.opensearch.index.Index) ShardId(org.opensearch.index.shard.ShardId) ScriptException(org.opensearch.script.ScriptException) ToXContent(org.opensearch.common.xcontent.ToXContent) XContent(org.opensearch.common.xcontent.XContent) ParsingException(org.opensearch.common.ParsingException) NodeClosedException(org.opensearch.node.NodeClosedException) BroadcastShardOperationFailedException(org.opensearch.action.support.broadcast.BroadcastShardOperationFailedException) ShardSearchFailure(org.opensearch.action.search.ShardSearchFailure) RoutingMissingException(org.opensearch.action.RoutingMissingException) BytesReference(org.opensearch.common.bytes.BytesReference) SearchContextMissingException(org.opensearch.search.SearchContextMissingException) RepositoryException(org.opensearch.repositories.RepositoryException) NoNodeAvailableException(org.opensearch.client.transport.NoNodeAvailableException) IndexShardRecoveringException(org.opensearch.index.shard.IndexShardRecoveringException) NoNodeAvailableException(org.opensearch.client.transport.NoNodeAvailableException) ScriptException(org.opensearch.script.ScriptException) NodeClosedException(org.opensearch.node.NodeClosedException) BroadcastShardOperationFailedException(org.opensearch.action.support.broadcast.BroadcastShardOperationFailedException) ParsingException(org.opensearch.common.ParsingException) RepositoryException(org.opensearch.repositories.RepositoryException) RemoteTransportException(org.opensearch.transport.RemoteTransportException) ClusterBlockException(org.opensearch.cluster.block.ClusterBlockException) QueryShardException(org.opensearch.index.query.QueryShardException) SearchParseException(org.opensearch.search.SearchParseException) SearchContextMissingException(org.opensearch.search.SearchContextMissingException) EOFException(java.io.EOFException) FileNotFoundException(java.io.FileNotFoundException) RoutingMissingException(org.opensearch.action.RoutingMissingException) IndexNotFoundException(org.opensearch.index.IndexNotFoundException) IOException(java.io.IOException) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) SearchPhaseExecutionException(org.opensearch.action.search.SearchPhaseExecutionException) XContentParseException(org.opensearch.common.xcontent.XContentParseException) NoShardAvailableActionException(org.opensearch.action.NoShardAvailableActionException) ShardSearchContextId(org.opensearch.search.internal.ShardSearchContextId) NoShardAvailableActionException(org.opensearch.action.NoShardAvailableActionException) SearchShardTarget(org.opensearch.search.SearchShardTarget) XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 8 with ShardSearchFailure

use of org.opensearch.action.search.ShardSearchFailure in project OpenSearch by opensearch-project.

the class OpenSearchAssertions method assertFailures.

public static void assertFailures(SearchRequestBuilder searchRequestBuilder, RestStatus restStatus, Matcher<String> reasonMatcher) {
    // we can either run into partial or total failures depending on the current number of shards
    try {
        SearchResponse searchResponse = searchRequestBuilder.get();
        assertThat("Expected shard failures, got none", searchResponse.getShardFailures().length, greaterThan(0));
        for (ShardSearchFailure shardSearchFailure : searchResponse.getShardFailures()) {
            assertThat(shardSearchFailure.status(), equalTo(restStatus));
            assertThat(shardSearchFailure.reason(), reasonMatcher);
        }
    } catch (SearchPhaseExecutionException e) {
        assertThat(e.status(), equalTo(restStatus));
        assertThat(e.toString(), reasonMatcher);
        for (ShardSearchFailure shardSearchFailure : e.shardFailures()) {
            assertThat(shardSearchFailure.status(), equalTo(restStatus));
            assertThat(shardSearchFailure.reason(), reasonMatcher);
        }
    } catch (Exception e) {
        fail("SearchPhaseExecutionException expected but got " + e.getClass());
    }
}
Also used : SearchPhaseExecutionException(org.opensearch.action.search.SearchPhaseExecutionException) ShardSearchFailure(org.opensearch.action.search.ShardSearchFailure) DefaultShardOperationFailedException(org.opensearch.action.support.DefaultShardOperationFailedException) OpenSearchException(org.opensearch.OpenSearchException) ClusterBlockException(org.opensearch.cluster.block.ClusterBlockException) IOException(java.io.IOException) SearchPhaseExecutionException(org.opensearch.action.search.SearchPhaseExecutionException) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 9 with ShardSearchFailure

use of org.opensearch.action.search.ShardSearchFailure in project OpenSearch by opensearch-project.

the class OpenSearchAssertions method formatShardStatus.

public static String formatShardStatus(SearchResponse response) {
    StringBuilder msg = new StringBuilder();
    msg.append(" Total shards: ").append(response.getTotalShards()).append(" Successful shards: ").append(response.getSuccessfulShards()).append(" & ").append(response.getFailedShards()).append(" shard failures:");
    for (ShardSearchFailure failure : response.getShardFailures()) {
        msg.append("\n ").append(failure);
    }
    return msg.toString();
}
Also used : ShardSearchFailure(org.opensearch.action.search.ShardSearchFailure)

Example 10 with ShardSearchFailure

use of org.opensearch.action.search.ShardSearchFailure in project OpenSearch by opensearch-project.

the class SearchDocumentationIT method testCount.

@SuppressWarnings({ "unused", "unchecked" })
public void testCount() throws Exception {
    indexCountTestData();
    RestHighLevelClient client = highLevelClient();
    {
        // tag::count-request-basic
        // <1>
        CountRequest countRequest = new CountRequest();
        // <2>
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        // <3>
        searchSourceBuilder.query(QueryBuilders.matchAllQuery());
        // <4>
        countRequest.source(searchSourceBuilder);
    // end::count-request-basic
    }
    {
        // tag::count-request-args
        CountRequest countRequest = // <1>
        new CountRequest("blog").routing(// <2>
        "routing").indicesOptions(// <3>
        IndicesOptions.lenientExpandOpen()).preference(// <4>
        "_local");
        // end::count-request-args
        assertNotNull(client.count(countRequest, RequestOptions.DEFAULT));
    }
    {
        // tag::count-source-basics
        // <1>
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        // <2>
        sourceBuilder.query(QueryBuilders.termQuery("user", "foobar"));
        // end::count-source-basics
        // tag::count-source-setter
        CountRequest countRequest = new CountRequest();
        countRequest.indices("blog", "author");
        countRequest.source(sourceBuilder);
        // end::count-source-setter
        // tag::count-execute
        CountResponse countResponse = client.count(countRequest, RequestOptions.DEFAULT);
        // end::count-execute
        // tag::count-execute-listener
        ActionListener<CountResponse> listener = new ActionListener<CountResponse>() {

            @Override
            public void onResponse(CountResponse countResponse) {
            // <1>
            }

            @Override
            public void onFailure(Exception e) {
            // <2>
            }
        };
        // end::count-execute-listener
        // Replace the empty listener by a blocking listener in test
        final CountDownLatch latch = new CountDownLatch(1);
        listener = new LatchedActionListener<>(listener, latch);
        // tag::count-execute-async
        // <1>
        client.countAsync(countRequest, RequestOptions.DEFAULT, listener);
        // end::count-execute-async
        assertTrue(latch.await(30L, TimeUnit.SECONDS));
        // tag::count-response-1
        long count = countResponse.getCount();
        RestStatus status = countResponse.status();
        Boolean terminatedEarly = countResponse.isTerminatedEarly();
        // end::count-response-1
        // tag::count-response-2
        int totalShards = countResponse.getTotalShards();
        int skippedShards = countResponse.getSkippedShards();
        int successfulShards = countResponse.getSuccessfulShards();
        int failedShards = countResponse.getFailedShards();
        for (ShardSearchFailure failure : countResponse.getShardFailures()) {
        // failures should be handled here
        }
        // end::count-response-2
        assertNotNull(countResponse);
        assertEquals(4, countResponse.getCount());
    }
}
Also used : CountResponse(org.opensearch.client.core.CountResponse) RestHighLevelClient(org.opensearch.client.RestHighLevelClient) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder) CountRequest(org.opensearch.client.core.CountRequest) LatchedActionListener(org.opensearch.action.LatchedActionListener) LatchedActionListener(org.opensearch.action.LatchedActionListener) ActionListener(org.opensearch.action.ActionListener) RestStatus(org.opensearch.rest.RestStatus) ShardSearchFailure(org.opensearch.action.search.ShardSearchFailure)

Aggregations

ShardSearchFailure (org.opensearch.action.search.ShardSearchFailure)23 SearchPhaseExecutionException (org.opensearch.action.search.SearchPhaseExecutionException)8 SearchResponse (org.opensearch.action.search.SearchResponse)7 ParsingException (org.opensearch.common.ParsingException)7 IOException (java.io.IOException)5 Matchers.containsString (org.hamcrest.Matchers.containsString)4 ShardId (org.opensearch.index.shard.ShardId)4 SearchShardTarget (org.opensearch.search.SearchShardTarget)4 CountDownLatch (java.util.concurrent.CountDownLatch)3 ClusterBlockException (org.opensearch.cluster.block.ClusterBlockException)3 Index (org.opensearch.index.Index)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 ActionListener (org.opensearch.action.ActionListener)2 LatchedActionListener (org.opensearch.action.LatchedActionListener)2 MultiSearchResponse (org.opensearch.action.search.MultiSearchResponse)2 RestHighLevelClient (org.opensearch.client.RestHighLevelClient)2 OpenSearchRejectedExecutionException (org.opensearch.common.util.concurrent.OpenSearchRejectedExecutionException)2