Search in sources :

Example 56 with ElasticsearchException

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.ElasticsearchException in project elasticsearch by elastic.

the class LocalCheckpointTrackerTests method testConcurrentPrimary.

public void testConcurrentPrimary() throws InterruptedException {
    Thread[] threads = new Thread[randomIntBetween(2, 5)];
    final int opsPerThread = randomIntBetween(10, 20);
    final int maxOps = opsPerThread * threads.length;
    // make sure we always index the last seqNo to simplify maxSeq checks
    final long unFinishedSeq = randomIntBetween(0, maxOps - 2);
    logger.info("--> will run [{}] threads, maxOps [{}], unfinished seq no [{}]", threads.length, maxOps, unFinishedSeq);
    final CyclicBarrier barrier = new CyclicBarrier(threads.length);
    for (int t = 0; t < threads.length; t++) {
        final int threadId = t;
        threads[t] = new Thread(new AbstractRunnable() {

            @Override
            public void onFailure(Exception e) {
                throw new ElasticsearchException("failure in background thread", e);
            }

            @Override
            protected void doRun() throws Exception {
                barrier.await();
                for (int i = 0; i < opsPerThread; i++) {
                    long seqNo = tracker.generateSeqNo();
                    logger.info("[t{}] started   [{}]", threadId, seqNo);
                    if (seqNo != unFinishedSeq) {
                        tracker.markSeqNoAsCompleted(seqNo);
                        logger.info("[t{}] completed [{}]", threadId, seqNo);
                    }
                }
            }
        }, "testConcurrentPrimary_" + threadId);
        threads[t].start();
    }
    for (Thread thread : threads) {
        thread.join();
    }
    assertThat(tracker.getMaxSeqNo(), equalTo(maxOps - 1L));
    assertThat(tracker.getCheckpoint(), equalTo(unFinishedSeq - 1L));
    tracker.markSeqNoAsCompleted(unFinishedSeq);
    assertThat(tracker.getCheckpoint(), equalTo(maxOps - 1L));
    assertThat(tracker.processedSeqNo.size(), isOneOf(0, 1));
    assertThat(tracker.firstProcessedSeqNo, equalTo(((long) maxOps / SMALL_CHUNK_SIZE) * SMALL_CHUNK_SIZE));
}
Also used : AbstractRunnable(org.elasticsearch.common.util.concurrent.AbstractRunnable) ElasticsearchException(org.elasticsearch.ElasticsearchException) ElasticsearchException(org.elasticsearch.ElasticsearchException) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) CyclicBarrier(java.util.concurrent.CyclicBarrier)

Example 57 with ElasticsearchException

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.ElasticsearchException in project elasticsearch by elastic.

the class IngestClientIT method testBulkWithIngestFailures.

public void testBulkWithIngestFailures() throws Exception {
    createIndex("index");
    BytesReference source = jsonBuilder().startObject().field("description", "my_pipeline").startArray("processors").startObject().startObject("test").endObject().endObject().endArray().endObject().bytes();
    PutPipelineRequest putPipelineRequest = new PutPipelineRequest("_id", source, XContentType.JSON);
    client().admin().cluster().putPipeline(putPipelineRequest).get();
    int numRequests = scaledRandomIntBetween(32, 128);
    BulkRequest bulkRequest = new BulkRequest();
    for (int i = 0; i < numRequests; i++) {
        IndexRequest indexRequest = new IndexRequest("index", "type", Integer.toString(i)).setPipeline("_id");
        indexRequest.source(Requests.INDEX_CONTENT_TYPE, "field", "value", "fail", i % 2 == 0);
        bulkRequest.add(indexRequest);
    }
    BulkResponse response = client().bulk(bulkRequest).actionGet();
    assertThat(response.getItems().length, equalTo(bulkRequest.requests().size()));
    for (int i = 0; i < bulkRequest.requests().size(); i++) {
        BulkItemResponse itemResponse = response.getItems()[i];
        if (i % 2 == 0) {
            BulkItemResponse.Failure failure = itemResponse.getFailure();
            ElasticsearchException compoundProcessorException = (ElasticsearchException) failure.getCause();
            assertThat(compoundProcessorException.getRootCause().getMessage(), equalTo("test processor failed"));
        } else {
            IndexResponse indexResponse = itemResponse.getResponse();
            assertThat("Expected a successful response but found failure [" + itemResponse.getFailure() + "].", itemResponse.isFailed(), is(false));
            assertThat(indexResponse, notNullValue());
            assertThat(indexResponse.getId(), equalTo(Integer.toString(i)));
            assertEquals(DocWriteResponse.Result.CREATED, indexResponse.getResult());
        }
    }
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) IndexResponse(org.elasticsearch.action.index.IndexResponse) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) BulkItemResponse(org.elasticsearch.action.bulk.BulkItemResponse) PutPipelineRequest(org.elasticsearch.action.ingest.PutPipelineRequest) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) ElasticsearchException(org.elasticsearch.ElasticsearchException) IndexRequest(org.elasticsearch.action.index.IndexRequest)

Example 58 with ElasticsearchException

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.ElasticsearchException in project elasticsearch by elastic.

the class RestHighLevelClient method parseResponseException.

/**
     * Converts a {@link ResponseException} obtained from the low level REST client into an {@link ElasticsearchException}.
     * If a response body was returned, tries to parse it as an error returned from Elasticsearch.
     * If no response body was returned or anything goes wrong while parsing the error, returns a new {@link ElasticsearchStatusException}
     * that wraps the original {@link ResponseException}. The potential exception obtained while parsing is added to the returned
     * exception as a suppressed exception. This method is guaranteed to not throw any exception eventually thrown while parsing.
     */
ElasticsearchStatusException parseResponseException(ResponseException responseException) {
    Response response = responseException.getResponse();
    HttpEntity entity = response.getEntity();
    ElasticsearchStatusException elasticsearchException;
    if (entity == null) {
        elasticsearchException = new ElasticsearchStatusException(responseException.getMessage(), RestStatus.fromCode(response.getStatusLine().getStatusCode()), responseException);
    } else {
        try {
            elasticsearchException = parseEntity(entity, BytesRestResponse::errorFromXContent);
            elasticsearchException.addSuppressed(responseException);
        } catch (Exception e) {
            RestStatus restStatus = RestStatus.fromCode(response.getStatusLine().getStatusCode());
            elasticsearchException = new ElasticsearchStatusException("Unable to parse response body", restStatus, responseException);
            elasticsearchException.addSuppressed(e);
        }
    }
    return elasticsearchException;
}
Also used : GetResponse(org.elasticsearch.action.get.GetResponse) UpdateResponse(org.elasticsearch.action.update.UpdateResponse) IndexResponse(org.elasticsearch.action.index.IndexResponse) DeleteResponse(org.elasticsearch.action.delete.DeleteResponse) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) MainResponse(org.elasticsearch.action.main.MainResponse) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) HttpEntity(org.apache.http.HttpEntity) RestStatus(org.elasticsearch.rest.RestStatus) ElasticsearchStatusException(org.elasticsearch.ElasticsearchStatusException) ElasticsearchException(org.elasticsearch.ElasticsearchException) ElasticsearchStatusException(org.elasticsearch.ElasticsearchStatusException) IOException(java.io.IOException) ActionRequestValidationException(org.elasticsearch.action.ActionRequestValidationException)

Example 59 with ElasticsearchException

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.ElasticsearchException in project elasticsearch by elastic.

the class CrudIT method testGet.

public void testGet() throws IOException {
    {
        GetRequest getRequest = new GetRequest("index", "type", "id");
        ElasticsearchException exception = expectThrows(ElasticsearchException.class, () -> execute(getRequest, highLevelClient()::get, highLevelClient()::getAsync));
        assertEquals(RestStatus.NOT_FOUND, exception.status());
        assertEquals("Elasticsearch exception [type=index_not_found_exception, reason=no such index]", exception.getMessage());
        assertEquals("index", exception.getMetadata("es.index").get(0));
    }
    String document = "{\"field1\":\"value1\",\"field2\":\"value2\"}";
    StringEntity stringEntity = new StringEntity(document, ContentType.APPLICATION_JSON);
    Response response = client().performRequest("PUT", "/index/type/id", Collections.singletonMap("refresh", "wait_for"), stringEntity);
    assertEquals(201, response.getStatusLine().getStatusCode());
    {
        GetRequest getRequest = new GetRequest("index", "type", "id").version(2);
        ElasticsearchException exception = expectThrows(ElasticsearchException.class, () -> execute(getRequest, highLevelClient()::get, highLevelClient()::getAsync));
        assertEquals(RestStatus.CONFLICT, exception.status());
        assertEquals("Elasticsearch exception [type=version_conflict_engine_exception, " + "reason=[type][id]: " + "version conflict, current version [1] is different than the one provided [2]]", exception.getMessage());
        assertEquals("index", exception.getMetadata("es.index").get(0));
    }
    {
        GetRequest getRequest = new GetRequest("index", "type", "id");
        if (randomBoolean()) {
            getRequest.version(1L);
        }
        GetResponse getResponse = execute(getRequest, highLevelClient()::get, highLevelClient()::getAsync);
        assertEquals("index", getResponse.getIndex());
        assertEquals("type", getResponse.getType());
        assertEquals("id", getResponse.getId());
        assertTrue(getResponse.isExists());
        assertFalse(getResponse.isSourceEmpty());
        assertEquals(1L, getResponse.getVersion());
        assertEquals(document, getResponse.getSourceAsString());
    }
    {
        GetRequest getRequest = new GetRequest("index", "type", "does_not_exist");
        GetResponse getResponse = execute(getRequest, highLevelClient()::get, highLevelClient()::getAsync);
        assertEquals("index", getResponse.getIndex());
        assertEquals("type", getResponse.getType());
        assertEquals("does_not_exist", getResponse.getId());
        assertFalse(getResponse.isExists());
        assertEquals(-1, getResponse.getVersion());
        assertTrue(getResponse.isSourceEmpty());
        assertNull(getResponse.getSourceAsString());
    }
    {
        GetRequest getRequest = new GetRequest("index", "type", "id");
        getRequest.fetchSourceContext(new FetchSourceContext(false, Strings.EMPTY_ARRAY, Strings.EMPTY_ARRAY));
        GetResponse getResponse = execute(getRequest, highLevelClient()::get, highLevelClient()::getAsync);
        assertEquals("index", getResponse.getIndex());
        assertEquals("type", getResponse.getType());
        assertEquals("id", getResponse.getId());
        assertTrue(getResponse.isExists());
        assertTrue(getResponse.isSourceEmpty());
        assertEquals(1L, getResponse.getVersion());
        assertNull(getResponse.getSourceAsString());
    }
    {
        GetRequest getRequest = new GetRequest("index", "type", "id");
        if (randomBoolean()) {
            getRequest.fetchSourceContext(new FetchSourceContext(true, new String[] { "field1" }, Strings.EMPTY_ARRAY));
        } else {
            getRequest.fetchSourceContext(new FetchSourceContext(true, Strings.EMPTY_ARRAY, new String[] { "field2" }));
        }
        GetResponse getResponse = execute(getRequest, highLevelClient()::get, highLevelClient()::getAsync);
        assertEquals("index", getResponse.getIndex());
        assertEquals("type", getResponse.getType());
        assertEquals("id", getResponse.getId());
        assertTrue(getResponse.isExists());
        assertFalse(getResponse.isSourceEmpty());
        assertEquals(1L, getResponse.getVersion());
        Map<String, Object> sourceAsMap = getResponse.getSourceAsMap();
        assertEquals(1, sourceAsMap.size());
        assertEquals("value1", sourceAsMap.get("field1"));
    }
}
Also used : GetResponse(org.elasticsearch.action.get.GetResponse) UpdateResponse(org.elasticsearch.action.update.UpdateResponse) IndexResponse(org.elasticsearch.action.index.IndexResponse) DeleteResponse(org.elasticsearch.action.delete.DeleteResponse) BulkItemResponse(org.elasticsearch.action.bulk.BulkItemResponse) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) DocWriteResponse(org.elasticsearch.action.DocWriteResponse) StringEntity(org.apache.http.entity.StringEntity) FetchSourceContext(org.elasticsearch.search.fetch.subphase.FetchSourceContext) GetRequest(org.elasticsearch.action.get.GetRequest) ElasticsearchException(org.elasticsearch.ElasticsearchException) GetResponse(org.elasticsearch.action.get.GetResponse) Map(java.util.Map) Collections.singletonMap(java.util.Collections.singletonMap)

Example 60 with ElasticsearchException

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.ElasticsearchException in project elasticsearch by elastic.

the class RestHighLevelClientTests method testPerformRequestOnResponseExceptionWithIgnoresErrorNoBody.

public void testPerformRequestOnResponseExceptionWithIgnoresErrorNoBody() throws IOException {
    MainRequest mainRequest = new MainRequest();
    CheckedFunction<MainRequest, Request, IOException> requestConverter = request -> new Request("GET", "/", Collections.emptyMap(), null);
    HttpResponse httpResponse = new BasicHttpResponse(newStatusLine(RestStatus.NOT_FOUND));
    Response mockResponse = new Response(REQUEST_LINE, new HttpHost("localhost", 9200), httpResponse);
    ResponseException responseException = new ResponseException(mockResponse);
    when(restClient.performRequest(anyString(), anyString(), anyMapOf(String.class, String.class), anyObject(), anyVararg())).thenThrow(responseException);
    ElasticsearchException elasticsearchException = expectThrows(ElasticsearchException.class, () -> restHighLevelClient.performRequest(mainRequest, requestConverter, response -> {
        throw new IllegalStateException();
    }, Collections.singleton(404)));
    assertEquals(RestStatus.NOT_FOUND, elasticsearchException.status());
    assertSame(responseException, elasticsearchException.getCause());
    assertEquals(responseException.getMessage(), elasticsearchException.getMessage());
}
Also used : ElasticsearchException(org.elasticsearch.ElasticsearchException) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) Header(org.apache.http.Header) StatusLine(org.apache.http.StatusLine) CoreMatchers.instanceOf(org.hamcrest.CoreMatchers.instanceOf) ArgumentMatcher(org.mockito.ArgumentMatcher) RequestLine(org.apache.http.RequestLine) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Matchers.eq(org.mockito.Matchers.eq) ClusterName(org.elasticsearch.cluster.ClusterName) JsonParseException(com.fasterxml.jackson.core.JsonParseException) NamedXContentRegistry(org.elasticsearch.common.xcontent.NamedXContentRegistry) Matchers.anyVararg(org.mockito.Matchers.anyVararg) ActionRequest(org.elasticsearch.action.ActionRequest) HttpEntity(org.apache.http.HttpEntity) ContentType(org.apache.http.entity.ContentType) StringEntity(org.apache.http.entity.StringEntity) XContentHelper.toXContent(org.elasticsearch.common.xcontent.XContentHelper.toXContent) MainResponse(org.elasticsearch.action.main.MainResponse) CheckedFunction(org.elasticsearch.common.CheckedFunction) List(java.util.List) Version(org.elasticsearch.Version) ArrayEquals(org.mockito.internal.matchers.ArrayEquals) ActionRequestValidationException(org.elasticsearch.action.ActionRequestValidationException) Matchers.argThat(org.mockito.Matchers.argThat) RestStatus(org.elasticsearch.rest.RestStatus) Mockito.mock(org.mockito.Mockito.mock) XContentType(org.elasticsearch.common.xcontent.XContentType) BasicStatusLine(org.apache.http.message.BasicStatusLine) Matchers(org.mockito.Matchers) ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) AtomicReference(java.util.concurrent.atomic.AtomicReference) Matchers.anyString(org.mockito.Matchers.anyString) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) Matchers.anyMapOf(org.mockito.Matchers.anyMapOf) SocketTimeoutException(java.net.SocketTimeoutException) Matchers.anyObject(org.mockito.Matchers.anyObject) ESTestCase(org.elasticsearch.test.ESTestCase) MainRequest(org.elasticsearch.action.main.MainRequest) Before(org.junit.Before) CborXContent(org.elasticsearch.common.xcontent.cbor.CborXContent) BasicRequestLine(org.apache.http.message.BasicRequestLine) SmileXContent(org.elasticsearch.common.xcontent.smile.SmileXContent) IOException(java.io.IOException) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) XContentParser(org.elasticsearch.common.xcontent.XContentParser) ProtocolVersion(org.apache.http.ProtocolVersion) HttpResponse(org.apache.http.HttpResponse) HttpHost(org.apache.http.HttpHost) Build(org.elasticsearch.Build) VarargMatcher(org.mockito.internal.matchers.VarargMatcher) Collections(java.util.Collections) ActionListener(org.elasticsearch.action.ActionListener) MainRequest(org.elasticsearch.action.main.MainRequest) ActionRequest(org.elasticsearch.action.ActionRequest) MainRequest(org.elasticsearch.action.main.MainRequest) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) Matchers.anyString(org.mockito.Matchers.anyString) ElasticsearchException(org.elasticsearch.ElasticsearchException) MainResponse(org.elasticsearch.action.main.MainResponse) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) HttpResponse(org.apache.http.HttpResponse) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) HttpHost(org.apache.http.HttpHost)

Aggregations

ElasticsearchException (org.elasticsearch.ElasticsearchException)310 IOException (java.io.IOException)128 Settings (org.elasticsearch.common.settings.Settings)32 HashMap (java.util.HashMap)31 ArrayList (java.util.ArrayList)30 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)30 ClusterState (org.elasticsearch.cluster.ClusterState)29 Matchers.containsString (org.hamcrest.Matchers.containsString)25 List (java.util.List)23 Map (java.util.Map)22 AtomicReference (java.util.concurrent.atomic.AtomicReference)20 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)18 XContentParser (org.elasticsearch.common.xcontent.XContentParser)17 Path (java.nio.file.Path)16 Test (org.junit.Test)16 ActionListener (org.elasticsearch.action.ActionListener)15 Collections (java.util.Collections)14 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)14 Version (org.elasticsearch.Version)14 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)13