Search in sources :

Example 11 with SearchPhaseExecutionException

use of org.elasticsearch.action.search.SearchPhaseExecutionException in project elasticsearch by elastic.

the class CorruptedTranslogIT method testCorruptTranslogFiles.

public void testCorruptTranslogFiles() throws Exception {
    internalCluster().startNodes(1, Settings.EMPTY);
    assertAcked(prepareCreate("test").setSettings(Settings.builder().put("index.number_of_shards", 1).put("index.number_of_replicas", 0).put("index.refresh_interval", "-1").put(MockEngineSupport.DISABLE_FLUSH_ON_CLOSE.getKey(), // never flush - always recover from translog
    true)));
    // Index some documents
    int numDocs = scaledRandomIntBetween(100, 1000);
    IndexRequestBuilder[] builders = new IndexRequestBuilder[numDocs];
    for (int i = 0; i < builders.length; i++) {
        builders[i] = client().prepareIndex("test", "type").setSource("foo", "bar");
    }
    disableTranslogFlush("test");
    // this one
    indexRandom(false, false, false, Arrays.asList(builders));
    // Corrupt the translog file(s)
    corruptRandomTranslogFiles();
    // Restart the single node
    internalCluster().fullRestart();
    client().admin().cluster().prepareHealth().setWaitForYellowStatus().setTimeout(new TimeValue(1000, TimeUnit.MILLISECONDS)).setWaitForEvents(Priority.LANGUID).get();
    try {
        client().prepareSearch("test").setQuery(matchAllQuery()).get();
        fail("all shards should be failed due to a corrupted translog");
    } catch (SearchPhaseExecutionException e) {
    // Good, all shards should be failed because there is only a
    // single shard and its translog is corrupt
    }
}
Also used : IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) SearchPhaseExecutionException(org.elasticsearch.action.search.SearchPhaseExecutionException) TimeValue(org.elasticsearch.common.unit.TimeValue)

Example 12 with SearchPhaseExecutionException

use of org.elasticsearch.action.search.SearchPhaseExecutionException in project elasticsearch by elastic.

the class RejectionActionIT method testSimulatedSearchRejectionLoad.

public void testSimulatedSearchRejectionLoad() throws Throwable {
    for (int i = 0; i < 10; i++) {
        client().prepareIndex("test", "type", Integer.toString(i)).setSource("field", "1").get();
    }
    int numberOfAsyncOps = randomIntBetween(200, 700);
    final CountDownLatch latch = new CountDownLatch(numberOfAsyncOps);
    final CopyOnWriteArrayList<Object> responses = new CopyOnWriteArrayList<>();
    for (int i = 0; i < numberOfAsyncOps; i++) {
        client().prepareSearch("test").setSearchType(SearchType.QUERY_THEN_FETCH).setQuery(QueryBuilders.matchQuery("field", "1")).execute(new ActionListener<SearchResponse>() {

            @Override
            public void onResponse(SearchResponse searchResponse) {
                responses.add(searchResponse);
                latch.countDown();
            }

            @Override
            public void onFailure(Exception e) {
                responses.add(e);
                latch.countDown();
            }
        });
    }
    latch.await();
    // validate all responses
    for (Object response : responses) {
        if (response instanceof SearchResponse) {
            SearchResponse searchResponse = (SearchResponse) response;
            for (ShardSearchFailure failure : searchResponse.getShardFailures()) {
                assertTrue("got unexpected reason..." + failure.reason(), failure.reason().toLowerCase(Locale.ENGLISH).contains("rejected"));
            }
        } else {
            Exception t = (Exception) response;
            Throwable unwrap = ExceptionsHelper.unwrapCause(t);
            if (unwrap instanceof SearchPhaseExecutionException) {
                SearchPhaseExecutionException e = (SearchPhaseExecutionException) unwrap;
                for (ShardSearchFailure failure : e.shardFailures()) {
                    assertTrue("got unexpected reason..." + failure.reason(), failure.reason().toLowerCase(Locale.ENGLISH).contains("rejected"));
                }
            } else if ((unwrap instanceof EsRejectedExecutionException) == false) {
                throw new AssertionError("unexpected failure", (Throwable) response);
            }
        }
    }
    assertThat(responses.size(), equalTo(numberOfAsyncOps));
}
Also used : SearchPhaseExecutionException(org.elasticsearch.action.search.SearchPhaseExecutionException) CountDownLatch(java.util.concurrent.CountDownLatch) SearchPhaseExecutionException(org.elasticsearch.action.search.SearchPhaseExecutionException) EsRejectedExecutionException(org.elasticsearch.common.util.concurrent.EsRejectedExecutionException) SearchResponse(org.elasticsearch.action.search.SearchResponse) ShardSearchFailure(org.elasticsearch.action.search.ShardSearchFailure) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) EsRejectedExecutionException(org.elasticsearch.common.util.concurrent.EsRejectedExecutionException)

Example 13 with SearchPhaseExecutionException

use of org.elasticsearch.action.search.SearchPhaseExecutionException in project elasticsearch by elastic.

the class IndexLookupIT method checkExceptions.

private void checkExceptions(Script script) {
    try {
        SearchResponse sr = client().prepareSearch("test").setQuery(QueryBuilders.matchAllQuery()).addScriptField("tvtest", script).execute().actionGet();
        assertThat(sr.getHits().getHits().length, equalTo(0));
        ShardSearchFailure[] shardFails = sr.getShardFailures();
        for (ShardSearchFailure fail : shardFails) {
            assertThat(fail.reason().indexOf("Cannot iterate twice! If you want to iterate more that once, add _CACHE explicitly."), Matchers.greaterThan(-1));
        }
    } catch (SearchPhaseExecutionException ex) {
        assertThat("got " + ex.toString(), ex.toString().indexOf("Cannot iterate twice! If you want to iterate more that once, add _CACHE explicitly."), Matchers.greaterThan(-1));
    }
}
Also used : SearchPhaseExecutionException(org.elasticsearch.action.search.SearchPhaseExecutionException) ShardSearchFailure(org.elasticsearch.action.search.ShardSearchFailure) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 14 with SearchPhaseExecutionException

use of org.elasticsearch.action.search.SearchPhaseExecutionException in project elasticsearch by elastic.

the class IndexLookupIT method testCallWithDifferentFlagsFails.

public void testCallWithDifferentFlagsFails() throws Exception {
    initTestData();
    final int numPrimaries = getNumShards("test").numPrimaries;
    final String expectedError = "You must call get with all required flags! " + "Instead of  _index['int_payload_field'].get('b', _FREQUENCIES) and _index['int_payload_field'].get('b', _POSITIONS)" + " call  _index['int_payload_field'].get('b', _FREQUENCIES | _POSITIONS)  once]";
    // should throw an exception, we cannot call with different flags twice
    // if the flags of the second call were not included in the first call.
    Script script = createScript("Call with different flags twice");
    try {
        SearchResponse response = client().prepareSearch("test").setQuery(QueryBuilders.matchAllQuery()).addScriptField("tvtest", script).get();
        // (partial) success when at least one shard succeeds
        assertThat(numPrimaries, greaterThan(response.getShardFailures().length));
        assertThat(response.getFailedShards(), greaterThanOrEqualTo(1));
        for (ShardSearchFailure failure : response.getShardFailures()) {
            assertThat(failure.reason(), containsString(expectedError));
        }
    } catch (SearchPhaseExecutionException e) {
        // Exception thrown when *all* shards fail
        assertThat(numPrimaries, equalTo(e.shardFailures().length));
        for (ShardSearchFailure failure : e.shardFailures()) {
            assertThat(failure.reason(), containsString(expectedError));
        }
    }
    // Should not throw an exception this way round
    script = createScript("Call with same flags twice");
    assertThat(client().prepareSearch("test").setQuery(QueryBuilders.matchAllQuery()).addScriptField("tvtest", script).get().getHits().getTotalHits(), greaterThan(0L));
}
Also used : SearchPhaseExecutionException(org.elasticsearch.action.search.SearchPhaseExecutionException) Matchers.containsString(org.hamcrest.Matchers.containsString) ShardSearchFailure(org.elasticsearch.action.search.ShardSearchFailure) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 15 with SearchPhaseExecutionException

use of org.elasticsearch.action.search.SearchPhaseExecutionException in project elasticsearch by elastic.

the class ElasticsearchExceptionTests method testFailureToAndFromXContentWithDetails.

public void testFailureToAndFromXContentWithDetails() throws IOException {
    final XContent xContent = randomFrom(XContentType.values()).xContent();
    Exception failure;
    Throwable failureCause;
    ElasticsearchException expected;
    ElasticsearchException expectedCause;
    ElasticsearchException suppressed;
    switch(randomIntBetween(0, 6)) {
        case // Simple elasticsearch exception without cause
        0:
            failure = new NoNodeAvailableException("A");
            expected = new ElasticsearchException("Elasticsearch exception [type=no_node_available_exception, reason=A]");
            expected.addSuppressed(new ElasticsearchException("Elasticsearch exception [type=no_node_available_exception, reason=A]"));
            break;
        case // Simple elasticsearch exception with headers (other metadata of type number are not parsed)
        1:
            failure = new CircuitBreakingException("B", 5_000, 2_000);
            ((ElasticsearchException) failure).addHeader("header_name", "0", "1");
            expected = new ElasticsearchException("Elasticsearch exception [type=circuit_breaking_exception, reason=B]");
            expected.addHeader("header_name", "0", "1");
            suppressed = new ElasticsearchException("Elasticsearch exception [type=circuit_breaking_exception, reason=B]");
            suppressed.addHeader("header_name", "0", "1");
            expected.addSuppressed(suppressed);
            break;
        case // Elasticsearch exception with a cause, headers and parsable metadata
        2:
            failureCause = new NullPointerException("var is null");
            failure = new ScriptException("C", failureCause, singletonList("stack"), "test", "painless");
            ((ElasticsearchException) failure).addHeader("script_name", "my_script");
            expectedCause = new ElasticsearchException("Elasticsearch exception [type=null_pointer_exception, reason=var is null]");
            expected = new ElasticsearchException("Elasticsearch exception [type=script_exception, reason=C]", expectedCause);
            expected.addHeader("script_name", "my_script");
            expected.addMetadata("es.lang", "painless");
            expected.addMetadata("es.script", "test");
            expected.addMetadata("es.script_stack", "stack");
            suppressed = new ElasticsearchException("Elasticsearch exception [type=script_exception, reason=C]");
            suppressed.addHeader("script_name", "my_script");
            suppressed.addMetadata("es.lang", "painless");
            suppressed.addMetadata("es.script", "test");
            suppressed.addMetadata("es.script_stack", "stack");
            expected.addSuppressed(suppressed);
            break;
        case // JDK exception without cause
        3:
            failure = new IllegalStateException("D");
            expected = new ElasticsearchException("Elasticsearch exception [type=illegal_state_exception, reason=D]");
            suppressed = new ElasticsearchException("Elasticsearch exception [type=illegal_state_exception, reason=D]");
            expected.addSuppressed(suppressed);
            break;
        case // JDK exception with cause
        4:
            failureCause = new RoutingMissingException("idx", "type", "id");
            failure = new RuntimeException("E", failureCause);
            expectedCause = new ElasticsearchException("Elasticsearch exception [type=routing_missing_exception, " + "reason=routing is required for [idx]/[type]/[id]]");
            expectedCause.addMetadata("es.index", "idx");
            expectedCause.addMetadata("es.index_uuid", "_na_");
            expected = new ElasticsearchException("Elasticsearch exception [type=runtime_exception, reason=E]", expectedCause);
            suppressed = new ElasticsearchException("Elasticsearch 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 ElasticsearchException("Elasticsearch exception [type=file_already_exists_exception, reason=File exists]");
            // strangely, the wrapped exception appears as the root cause...
            suppressed = new ElasticsearchException("Elasticsearch exception [type=broadcast_shard_operation_failed_exception, " + "reason=F]");
            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))), new ShardSearchFailure(new RepositoryException("repository_g", "Repo"), new SearchShardTarget("node_g", new ShardId(new Index("_index_g", "_uuid_g"), 62))), new ShardSearchFailure(new SearchContextMissingException(0L), null) };
            failure = new SearchPhaseExecutionException("phase_g", "G", failureCause, shardFailures);
            expectedCause = new ElasticsearchException("Elasticsearch exception [type=node_closed_exception, " + "reason=node closed " + node + "]");
            expectedCause = new ElasticsearchException("Elasticsearch exception [type=no_shard_available_action_exception, " + "reason=node_g]", expectedCause);
            expectedCause.addMetadata("es.index", "_index_g");
            expectedCause.addMetadata("es.index_uuid", "_uuid_g");
            expectedCause.addMetadata("es.shard", "6");
            expected = new ElasticsearchException("Elasticsearch exception [type=search_phase_execution_exception, " + "reason=G]", expectedCause);
            expected.addMetadata("es.phase", "phase_g");
            expected.addSuppressed(new ElasticsearchException("Elasticsearch exception [type=parsing_exception, reason=Parsing g]"));
            expected.addSuppressed(new ElasticsearchException("Elasticsearch exception [type=repository_exception, " + "reason=[repository_g] Repo]"));
            expected.addSuppressed(new ElasticsearchException("Elasticsearch 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 = XContentHelper.toXContent((builder, params) -> {
        ElasticsearchException.generateFailureXContent(builder, params, finalFailure, true);
        return builder;
    }, xContent.type(), randomBoolean());
    try (XContentParser parser = createParser(xContent, failureBytes)) {
        failureBytes = shuffleXContent(parser, randomBoolean()).bytes();
    }
    ElasticsearchException parsedFailure;
    try (XContentParser parser = createParser(xContent, failureBytes)) {
        assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken());
        assertEquals(XContentParser.Token.FIELD_NAME, parser.nextToken());
        parsedFailure = ElasticsearchException.failureFromXContent(parser);
        assertEquals(XContentParser.Token.END_OBJECT, parser.nextToken());
        assertNull(parser.nextToken());
    }
    assertDeepEquals(expected, parsedFailure);
}
Also used : FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) SearchPhaseExecutionException(org.elasticsearch.action.search.SearchPhaseExecutionException) Index(org.elasticsearch.index.Index) ShardId(org.elasticsearch.index.shard.ShardId) ScriptException(org.elasticsearch.script.ScriptException) XContent(org.elasticsearch.common.xcontent.XContent) ToXContent(org.elasticsearch.common.xcontent.ToXContent) ParsingException(org.elasticsearch.common.ParsingException) NodeClosedException(org.elasticsearch.node.NodeClosedException) BroadcastShardOperationFailedException(org.elasticsearch.action.support.broadcast.BroadcastShardOperationFailedException) ShardSearchFailure(org.elasticsearch.action.search.ShardSearchFailure) RoutingMissingException(org.elasticsearch.action.RoutingMissingException) BytesReference(org.elasticsearch.common.bytes.BytesReference) SearchContextMissingException(org.elasticsearch.search.SearchContextMissingException) RepositoryException(org.elasticsearch.repositories.RepositoryException) NoNodeAvailableException(org.elasticsearch.client.transport.NoNodeAvailableException) SearchParseException(org.elasticsearch.search.SearchParseException) NodeClosedException(org.elasticsearch.node.NodeClosedException) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) RoutingMissingException(org.elasticsearch.action.RoutingMissingException) BroadcastShardOperationFailedException(org.elasticsearch.action.support.broadcast.BroadcastShardOperationFailedException) RepositoryException(org.elasticsearch.repositories.RepositoryException) QueryShardException(org.elasticsearch.index.query.QueryShardException) SearchContextMissingException(org.elasticsearch.search.SearchContextMissingException) ScriptException(org.elasticsearch.script.ScriptException) NoNodeAvailableException(org.elasticsearch.client.transport.NoNodeAvailableException) EOFException(java.io.EOFException) FileNotFoundException(java.io.FileNotFoundException) SearchPhaseExecutionException(org.elasticsearch.action.search.SearchPhaseExecutionException) RemoteTransportException(org.elasticsearch.transport.RemoteTransportException) ParsingException(org.elasticsearch.common.ParsingException) IndexShardRecoveringException(org.elasticsearch.index.shard.IndexShardRecoveringException) ClusterBlockException(org.elasticsearch.cluster.block.ClusterBlockException) NoShardAvailableActionException(org.elasticsearch.action.NoShardAvailableActionException) IOException(java.io.IOException) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) CircuitBreakingException(org.elasticsearch.common.breaker.CircuitBreakingException) NoShardAvailableActionException(org.elasticsearch.action.NoShardAvailableActionException) CircuitBreakingException(org.elasticsearch.common.breaker.CircuitBreakingException) SearchShardTarget(org.elasticsearch.search.SearchShardTarget) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Aggregations

SearchPhaseExecutionException (org.elasticsearch.action.search.SearchPhaseExecutionException)42 SearchResponse (org.elasticsearch.action.search.SearchResponse)17 ShardSearchFailure (org.elasticsearch.action.search.ShardSearchFailure)10 Timed (com.codahale.metrics.annotation.Timed)6 ApiOperation (io.swagger.annotations.ApiOperation)6 ApiResponses (io.swagger.annotations.ApiResponses)6 IOException (java.io.IOException)6 GET (javax.ws.rs.GET)6 Produces (javax.ws.rs.Produces)6 TimeRange (org.graylog2.plugin.indexer.searches.timeranges.TimeRange)6 ArrayList (java.util.ArrayList)5 ElasticsearchException (org.elasticsearch.ElasticsearchException)5 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)5 Matchers.containsString (org.hamcrest.Matchers.containsString)5 RefreshResponse (org.elasticsearch.action.admin.indices.refresh.RefreshResponse)4 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)4 TimeValue (org.elasticsearch.common.unit.TimeValue)4 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)4 HashMap (java.util.HashMap)2 List (java.util.List)2