Search in sources :

Example 26 with SearchShardTarget

use of org.elasticsearch.search.SearchShardTarget in project elasticsearch by elastic.

the class SearchPhaseExecutionExceptionTests method testToAndFromXContent.

public void testToAndFromXContent() throws IOException {
    final XContent xContent = randomFrom(XContentType.values()).xContent();
    ShardSearchFailure[] shardSearchFailures = new ShardSearchFailure[randomIntBetween(1, 5)];
    for (int i = 0; i < shardSearchFailures.length; i++) {
        Exception cause = randomFrom(new ParsingException(1, 2, "foobar", null), new InvalidIndexTemplateException("foo", "bar"), new TimestampParsingException("foo", null), new NullPointerException());
        shardSearchFailures[i] = new ShardSearchFailure(cause, new SearchShardTarget("node_" + i, new Index("test", "_na_"), i));
    }
    final String phase = randomFrom("query", "search", "other");
    SearchPhaseExecutionException actual = new SearchPhaseExecutionException(phase, "unexpected failures", shardSearchFailures);
    BytesReference exceptionBytes = XContentHelper.toXContent(actual, xContent.type(), randomBoolean());
    ElasticsearchException parsedException;
    try (XContentParser parser = createParser(xContent, exceptionBytes)) {
        assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken());
        parsedException = ElasticsearchException.fromXContent(parser);
        assertEquals(XContentParser.Token.END_OBJECT, parser.currentToken());
        assertNull(parser.nextToken());
    }
    assertNotNull(parsedException);
    assertThat(parsedException.getHeaderKeys(), hasSize(0));
    assertThat(parsedException.getMetadataKeys(), hasSize(1));
    assertThat(parsedException.getMetadata("es.phase"), hasItem(phase));
    // SearchPhaseExecutionException has no cause field
    assertNull(parsedException.getCause());
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) InvalidIndexTemplateException(org.elasticsearch.indices.InvalidIndexTemplateException) Index(org.elasticsearch.index.Index) ElasticsearchException(org.elasticsearch.ElasticsearchException) ElasticsearchException(org.elasticsearch.ElasticsearchException) InvalidIndexTemplateException(org.elasticsearch.indices.InvalidIndexTemplateException) IndexShardClosedException(org.elasticsearch.index.shard.IndexShardClosedException) ParsingException(org.elasticsearch.common.ParsingException) IOException(java.io.IOException) TimestampParsingException(org.elasticsearch.action.TimestampParsingException) XContent(org.elasticsearch.common.xcontent.XContent) ToXContent(org.elasticsearch.common.xcontent.ToXContent) ParsingException(org.elasticsearch.common.ParsingException) TimestampParsingException(org.elasticsearch.action.TimestampParsingException) SearchShardTarget(org.elasticsearch.search.SearchShardTarget) TimestampParsingException(org.elasticsearch.action.TimestampParsingException) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 27 with SearchShardTarget

use of org.elasticsearch.search.SearchShardTarget in project elasticsearch by elastic.

the class SearchPhaseExecutionExceptionTests method testToXContent.

public void testToXContent() throws IOException {
    SearchPhaseExecutionException exception = new SearchPhaseExecutionException("test", "all shards failed", new ShardSearchFailure[] { new ShardSearchFailure(new ParsingException(1, 2, "foobar", null), new SearchShardTarget("node_1", new Index("foo", "_na_"), 0)), new ShardSearchFailure(new IndexShardClosedException(new ShardId(new Index("foo", "_na_"), 1)), new SearchShardTarget("node_2", new Index("foo", "_na_"), 1)), new ShardSearchFailure(new ParsingException(5, 7, "foobar", null), new SearchShardTarget("node_3", new Index("foo", "_na_"), 2)) });
    // Failures are grouped (by default)
    assertEquals("{" + "\"type\":\"search_phase_execution_exception\"," + "\"reason\":\"all shards failed\"," + "\"phase\":\"test\"," + "\"grouped\":true," + "\"failed_shards\":[" + "{" + "\"shard\":0," + "\"index\":\"foo\"," + "\"node\":\"node_1\"," + "\"reason\":{" + "\"type\":\"parsing_exception\"," + "\"reason\":\"foobar\"," + "\"line\":1," + "\"col\":2" + "}" + "}," + "{" + "\"shard\":1," + "\"index\":\"foo\"," + "\"node\":\"node_2\"," + "\"reason\":{" + "\"type\":\"index_shard_closed_exception\"," + "\"reason\":\"CurrentState[CLOSED] Closed\"," + "\"index_uuid\":\"_na_\"," + "\"shard\":\"1\"," + "\"index\":\"foo\"" + "}" + "}" + "]}", Strings.toString(exception));
    // Failures are NOT grouped
    ToXContent.MapParams params = new ToXContent.MapParams(singletonMap("group_shard_failures", "false"));
    try (XContentBuilder builder = jsonBuilder()) {
        builder.startObject();
        exception.toXContent(builder, params);
        builder.endObject();
        assertEquals("{" + "\"type\":\"search_phase_execution_exception\"," + "\"reason\":\"all shards failed\"," + "\"phase\":\"test\"," + "\"grouped\":false," + "\"failed_shards\":[" + "{" + "\"shard\":0," + "\"index\":\"foo\"," + "\"node\":\"node_1\"," + "\"reason\":{" + "\"type\":\"parsing_exception\"," + "\"reason\":\"foobar\"," + "\"line\":1," + "\"col\":2" + "}" + "}," + "{" + "\"shard\":1," + "\"index\":\"foo\"," + "\"node\":\"node_2\"," + "\"reason\":{" + "\"type\":\"index_shard_closed_exception\"," + "\"reason\":\"CurrentState[CLOSED] Closed\"," + "\"index_uuid\":\"_na_\"," + "\"shard\":\"1\"," + "\"index\":\"foo\"" + "}" + "}," + "{" + "\"shard\":2," + "\"index\":\"foo\"," + "\"node\":\"node_3\"," + "\"reason\":{" + "\"type\":\"parsing_exception\"," + "\"reason\":\"foobar\"," + "\"line\":5," + "\"col\":7" + "}" + "}" + "]}", builder.string());
    }
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) ToXContent(org.elasticsearch.common.xcontent.ToXContent) IndexShardClosedException(org.elasticsearch.index.shard.IndexShardClosedException) ParsingException(org.elasticsearch.common.ParsingException) TimestampParsingException(org.elasticsearch.action.TimestampParsingException) SearchShardTarget(org.elasticsearch.search.SearchShardTarget) Index(org.elasticsearch.index.Index) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 28 with SearchShardTarget

use of org.elasticsearch.search.SearchShardTarget in project elasticsearch by elastic.

the class ShardSearchFailureTests method createTestItem.

public static ShardSearchFailure createTestItem() {
    String randomMessage = randomAsciiOfLengthBetween(3, 20);
    Exception ex = new ParsingException(0, 0, randomMessage, new IllegalArgumentException("some bad argument"));
    String nodeId = randomAsciiOfLengthBetween(5, 10);
    String indexName = randomAsciiOfLengthBetween(5, 10);
    String indexUuid = randomAsciiOfLengthBetween(5, 10);
    int shardId = randomInt();
    return new ShardSearchFailure(ex, new SearchShardTarget(nodeId, new ShardId(new Index(indexName, indexUuid), shardId)));
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) ParsingException(org.elasticsearch.common.ParsingException) SearchShardTarget(org.elasticsearch.search.SearchShardTarget) Index(org.elasticsearch.index.Index) ParsingException(org.elasticsearch.common.ParsingException) IOException(java.io.IOException)

Example 29 with SearchShardTarget

use of org.elasticsearch.search.SearchShardTarget in project elasticsearch by elastic.

the class ElasticsearchExceptionTests method randomExceptions.

public static Tuple<Throwable, ElasticsearchException> randomExceptions() {
    Throwable actual;
    ElasticsearchException expected;
    int type = randomIntBetween(0, 5);
    switch(type) {
        case 0:
            actual = new ClusterBlockException(singleton(DiscoverySettings.NO_MASTER_BLOCK_WRITES));
            expected = new ElasticsearchException("Elasticsearch exception [type=cluster_block_exception, " + "reason=blocked by: [SERVICE_UNAVAILABLE/2/no master];]");
            break;
        case 1:
            actual = new CircuitBreakingException("Data too large", 123, 456);
            expected = new ElasticsearchException("Elasticsearch exception [type=circuit_breaking_exception, reason=Data too large]");
            break;
        case 2:
            actual = new SearchParseException(new TestSearchContext(null), "Parse failure", new XContentLocation(12, 98));
            expected = new ElasticsearchException("Elasticsearch exception [type=search_parse_exception, reason=Parse failure]");
            break;
        case 3:
            actual = new IllegalArgumentException("Closed resource", new RuntimeException("Resource"));
            expected = new ElasticsearchException("Elasticsearch exception [type=illegal_argument_exception, reason=Closed resource]", new ElasticsearchException("Elasticsearch exception [type=runtime_exception, reason=Resource]"));
            break;
        case 4:
            actual = new SearchPhaseExecutionException("search", "all shards failed", new ShardSearchFailure[] { new ShardSearchFailure(new ParsingException(1, 2, "foobar", null), new SearchShardTarget("node_1", new Index("foo", "_na_"), 1)) });
            expected = new ElasticsearchException("Elasticsearch exception [type=search_phase_execution_exception, " + "reason=all shards failed]");
            expected.addMetadata("es.phase", "search");
            break;
        case 5:
            actual = new ElasticsearchException("Parsing failed", new ParsingException(9, 42, "Wrong state", new NullPointerException("Unexpected null value")));
            ElasticsearchException expectedCause = new ElasticsearchException("Elasticsearch exception [type=parsing_exception, " + "reason=Wrong state]", new ElasticsearchException("Elasticsearch exception [type=null_pointer_exception, " + "reason=Unexpected null value]"));
            expected = new ElasticsearchException("Elasticsearch exception [type=exception, reason=Parsing failed]", expectedCause);
            break;
        default:
            throw new UnsupportedOperationException("No randomized exceptions generated for type [" + type + "]");
    }
    if (actual instanceof ElasticsearchException) {
        ElasticsearchException actualException = (ElasticsearchException) actual;
        if (randomBoolean()) {
            int nbHeaders = randomIntBetween(1, 5);
            Map<String, List<String>> randomHeaders = new HashMap<>(nbHeaders);
            for (int i = 0; i < nbHeaders; i++) {
                List<String> values = new ArrayList<>();
                int nbValues = randomIntBetween(1, 3);
                for (int j = 0; j < nbValues; j++) {
                    values.add(frequently() ? randomAsciiOfLength(5) : "");
                }
                randomHeaders.put("header_" + i, values);
            }
            for (Map.Entry<String, List<String>> entry : randomHeaders.entrySet()) {
                actualException.addHeader(entry.getKey(), entry.getValue());
                expected.addHeader(entry.getKey(), entry.getValue());
            }
            if (rarely()) {
                // Empty or null headers are not printed out by the toXContent method
                actualException.addHeader("ignored", randomBoolean() ? emptyList() : null);
            }
        }
        if (randomBoolean()) {
            int nbMetadata = randomIntBetween(1, 5);
            Map<String, List<String>> randomMetadata = new HashMap<>(nbMetadata);
            for (int i = 0; i < nbMetadata; i++) {
                List<String> values = new ArrayList<>();
                int nbValues = randomIntBetween(1, 3);
                for (int j = 0; j < nbValues; j++) {
                    values.add(frequently() ? randomAsciiOfLength(5) : "");
                }
                randomMetadata.put("es.metadata_" + i, values);
            }
            for (Map.Entry<String, List<String>> entry : randomMetadata.entrySet()) {
                actualException.addMetadata(entry.getKey(), entry.getValue());
                expected.addMetadata(entry.getKey(), entry.getValue());
            }
            if (rarely()) {
                // Empty or null metadata are not printed out by the toXContent method
                actualException.addMetadata("es.ignored", randomBoolean() ? emptyList() : null);
            }
        }
        if (randomBoolean()) {
            int nbResources = randomIntBetween(1, 5);
            for (int i = 0; i < nbResources; i++) {
                String resourceType = "type_" + i;
                String[] resourceIds = new String[randomIntBetween(1, 3)];
                for (int j = 0; j < resourceIds.length; j++) {
                    resourceIds[j] = frequently() ? randomAsciiOfLength(5) : "";
                }
                actualException.setResources(resourceType, resourceIds);
                expected.setResources(resourceType, resourceIds);
            }
        }
    }
    return new Tuple<>(actual, expected);
}
Also used : HashMap(java.util.HashMap) SearchPhaseExecutionException(org.elasticsearch.action.search.SearchPhaseExecutionException) ArrayList(java.util.ArrayList) Index(org.elasticsearch.index.Index) ParsingException(org.elasticsearch.common.ParsingException) Collections.singletonList(java.util.Collections.singletonList) Collections.emptyList(java.util.Collections.emptyList) List(java.util.List) ArrayList(java.util.ArrayList) ShardSearchFailure(org.elasticsearch.action.search.ShardSearchFailure) XContentLocation(org.elasticsearch.common.xcontent.XContentLocation) ClusterBlockException(org.elasticsearch.cluster.block.ClusterBlockException) TestSearchContext(org.elasticsearch.test.TestSearchContext) SearchParseException(org.elasticsearch.search.SearchParseException) CircuitBreakingException(org.elasticsearch.common.breaker.CircuitBreakingException) SearchShardTarget(org.elasticsearch.search.SearchShardTarget) Map(java.util.Map) HashMap(java.util.HashMap) Tuple(org.elasticsearch.common.collect.Tuple)

Aggregations

SearchShardTarget (org.elasticsearch.search.SearchShardTarget)29 Index (org.elasticsearch.index.Index)22 IOException (java.io.IOException)14 ScoreDoc (org.apache.lucene.search.ScoreDoc)12 TopDocs (org.apache.lucene.search.TopDocs)11 QuerySearchResult (org.elasticsearch.search.query.QuerySearchResult)11 QuerySearchResultProvider (org.elasticsearch.search.query.QuerySearchResultProvider)10 ActionListener (org.elasticsearch.action.ActionListener)9 AtomicReference (java.util.concurrent.atomic.AtomicReference)8 ParsingException (org.elasticsearch.common.ParsingException)7 AtomicArray (org.elasticsearch.common.util.concurrent.AtomicArray)7 ShardId (org.elasticsearch.index.shard.ShardId)6 ArrayList (java.util.ArrayList)5 SearchHit (org.elasticsearch.search.SearchHit)5 SearchHits (org.elasticsearch.search.SearchHits)5 DfsSearchResult (org.elasticsearch.search.dfs.DfsSearchResult)5 FetchSearchResult (org.elasticsearch.search.fetch.FetchSearchResult)5 QueryFetchSearchResult (org.elasticsearch.search.fetch.QueryFetchSearchResult)5 ShardFetchSearchRequest (org.elasticsearch.search.fetch.ShardFetchSearchRequest)5 ShardSearchFailure (org.elasticsearch.action.search.ShardSearchFailure)4