Search in sources :

Example 46 with BulkItemResponse

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.bulk.BulkItemResponse in project samza by apache.

the class ElasticsearchSystemProducerTest method getRespWithFailedDocument.

private BulkResponse getRespWithFailedDocument(RestStatus status) {
    BulkResponse response = mock(BulkResponse.class);
    when(response.hasFailures()).thenReturn(true);
    BulkItemResponse itemResp = mock(BulkItemResponse.class);
    when(itemResp.isFailed()).thenReturn(true);
    BulkItemResponse.Failure failure = mock(BulkItemResponse.Failure.class);
    when(failure.getStatus()).thenReturn(status);
    when(itemResp.getFailure()).thenReturn(failure);
    BulkItemResponse[] itemResponses = new BulkItemResponse[] { itemResp };
    when(response.getItems()).thenReturn(itemResponses);
    return response;
}
Also used : BulkItemResponse(org.elasticsearch.action.bulk.BulkItemResponse) BulkResponse(org.elasticsearch.action.bulk.BulkResponse)

Example 47 with BulkItemResponse

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.bulk.BulkItemResponse in project flink by apache.

the class ElasticsearchWriter method extractFailures.

private void extractFailures(BulkRequest request, BulkResponse response) {
    if (!response.hasFailures()) {
        pendingActions -= request.numberOfActions();
        return;
    }
    Throwable chainedFailures = null;
    for (int i = 0; i < response.getItems().length; i++) {
        final BulkItemResponse itemResponse = response.getItems()[i];
        if (!itemResponse.isFailed()) {
            continue;
        }
        final Throwable failure = itemResponse.getFailure().getCause();
        if (failure == null) {
            continue;
        }
        final RestStatus restStatus = itemResponse.getFailure().getStatus();
        final DocWriteRequest<?> actionRequest = request.requests().get(i);
        chainedFailures = firstOrSuppressed(wrapException(restStatus, failure, actionRequest), chainedFailures);
    }
    if (chainedFailures == null) {
        return;
    }
    throw new FlinkRuntimeException(chainedFailures);
}
Also used : RestStatus(org.elasticsearch.rest.RestStatus) FlinkRuntimeException(org.apache.flink.util.FlinkRuntimeException) BulkItemResponse(org.elasticsearch.action.bulk.BulkItemResponse)

Example 48 with BulkItemResponse

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.bulk.BulkItemResponse in project metron by apache.

the class ElasticsearchBulkDocumentWriter method write.

@Override
public BulkDocumentWriterResults<D> write() {
    BulkDocumentWriterResults<D> results = new BulkDocumentWriterResults<>();
    try {
        // create an index request for each document
        BulkRequest bulkRequest = new BulkRequest();
        bulkRequest.setRefreshPolicy(refreshPolicy);
        for (Indexable doc : documents) {
            DocWriteRequest request = createRequest(doc.document, doc.index);
            bulkRequest.add(request);
        }
        // submit the request and handle the response
        BulkResponse bulkResponse = client.getHighLevelClient().bulk(bulkRequest);
        handleBulkResponse(bulkResponse, documents, results);
        if (LOG.isDebugEnabled()) {
            String shards = Arrays.stream(bulkResponse.getItems()).map(bulkItemResponse -> bulkItemResponse.getResponse().getShardId().toString()).collect(Collectors.joining(","));
            LOG.debug("{} results written to shards {} in {} ms; batchSize={}, success={}, failed={}", bulkResponse.getItems().length, shards, bulkResponse.getTookInMillis(), documents.size(), results.getSuccesses().size(), results.getFailures().size());
        }
    } catch (IOException e) {
        // assume all documents have failed
        for (Indexable indexable : documents) {
            D failed = indexable.document;
            results.addFailure(failed, e, ExceptionUtils.getRootCauseMessage(e));
        }
        LOG.error("Failed to submit bulk request; all documents failed", e);
    } finally {
        // flush all documents no matter which ones succeeded or failed
        documents.clear();
    }
    return results;
}
Also used : Document(org.apache.metron.indexing.dao.update.Document) ElasticsearchClient(org.apache.metron.elasticsearch.client.ElasticsearchClient) Arrays(java.util.Arrays) Logger(org.slf4j.Logger) BulkItemResponse(org.elasticsearch.action.bulk.BulkItemResponse) MethodHandles(java.lang.invoke.MethodHandles) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) LoggerFactory(org.slf4j.LoggerFactory) IOException(java.io.IOException) DocWriteRequest(org.elasticsearch.action.DocWriteRequest) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) WriteRequest(org.elasticsearch.action.support.WriteRequest) IndexRequest(org.elasticsearch.action.index.IndexRequest) List(java.util.List) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) ExceptionUtils(org.apache.commons.lang3.exception.ExceptionUtils) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) IOException(java.io.IOException) DocWriteRequest(org.elasticsearch.action.DocWriteRequest)

Example 49 with BulkItemResponse

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.bulk.BulkItemResponse in project metron by apache.

the class ElasticsearchBulkDocumentWriterTest method setupElasticsearchToSucceed.

private void setupElasticsearchToSucceed() throws IOException {
    final String documentId = UUID.randomUUID().toString();
    final boolean isFailed = false;
    final int itemID = 0;
    // the write response will contain what is used as the document ID
    DocWriteResponse writeResponse = mock(DocWriteResponse.class);
    when(writeResponse.getId()).thenReturn(documentId);
    // define the item level response
    BulkItemResponse itemResponse = mock(BulkItemResponse.class);
    when(itemResponse.isFailed()).thenReturn(isFailed);
    when(itemResponse.getItemId()).thenReturn(itemID);
    when(itemResponse.getResponse()).thenReturn(writeResponse);
    List<BulkItemResponse> itemsResponses = Collections.singletonList(itemResponse);
    // define the bulk response to indicate success
    BulkResponse response = mock(BulkResponse.class);
    when(response.iterator()).thenReturn(itemsResponses.iterator());
    when(response.hasFailures()).thenReturn(isFailed);
    // have the client return the mock response
    when(highLevelClient.bulk(any(BulkRequest.class))).thenReturn(response);
}
Also used : DocWriteResponse(org.elasticsearch.action.DocWriteResponse) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) BulkItemResponse(org.elasticsearch.action.bulk.BulkItemResponse) BulkResponse(org.elasticsearch.action.bulk.BulkResponse)

Aggregations

BulkItemResponse (org.elasticsearch.action.bulk.BulkItemResponse)48 BulkResponse (org.elasticsearch.action.bulk.BulkResponse)37 BulkRequestBuilder (org.elasticsearch.action.bulk.BulkRequestBuilder)16 BulkRequest (org.elasticsearch.action.bulk.BulkRequest)14 IOException (java.io.IOException)9 ArrayList (java.util.ArrayList)8 IndexRequest (org.elasticsearch.action.index.IndexRequest)8 ElasticsearchTimeoutException (org.elasticsearch.ElasticsearchTimeoutException)7 NoNodeAvailableException (org.elasticsearch.client.transport.NoNodeAvailableException)7 EsRejectedExecutionException (org.elasticsearch.common.util.concurrent.EsRejectedExecutionException)7 NodeClosedException (org.elasticsearch.node.NodeClosedException)7 ReceiveTimeoutTransportException (org.elasticsearch.transport.ReceiveTimeoutTransportException)7 List (java.util.List)6 DocWriteRequest (org.elasticsearch.action.DocWriteRequest)5 FailedNodeException (org.elasticsearch.action.FailedNodeException)5 DeleteRequest (org.elasticsearch.action.delete.DeleteRequest)5 IndexResponse (org.elasticsearch.action.index.IndexResponse)5 UpdateRequest (org.elasticsearch.action.update.UpdateRequest)5 TransportException (org.elasticsearch.transport.TransportException)5 BulkWriterResponse (org.apache.metron.common.writer.BulkWriterResponse)4