Search in sources :

Example 36 with BulkItemResponse

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.bulk.BulkItemResponse in project fess by codelibs.

the class FessEsClient method addAll.

public void addAll(final String index, final String type, final List<Map<String, Object>> docList) {
    final FessConfig fessConfig = ComponentUtil.getFessConfig();
    final BulkRequestBuilder bulkRequestBuilder = client.prepareBulk();
    for (final Map<String, Object> doc : docList) {
        final Object id = doc.remove(fessConfig.getIndexFieldId());
        bulkRequestBuilder.add(client.prepareIndex(index, type, id.toString()).setSource(new DocMap(doc)));
    }
    final BulkResponse response = bulkRequestBuilder.execute().actionGet(ComponentUtil.getFessConfig().getIndexBulkTimeout());
    if (response.hasFailures()) {
        if (logger.isDebugEnabled()) {
            @SuppressWarnings("rawtypes") final List<DocWriteRequest> requests = bulkRequestBuilder.request().requests();
            final BulkItemResponse[] items = response.getItems();
            if (requests.size() == items.length) {
                for (int i = 0; i < requests.size(); i++) {
                    final BulkItemResponse resp = items[i];
                    if (resp.isFailed() && resp.getFailure() != null) {
                        final DocWriteRequest<?> req = requests.get(i);
                        final Failure failure = resp.getFailure();
                        logger.debug("Failed Request: " + req + "\n=>" + failure.getMessage());
                    }
                }
            }
        }
        throw new FessEsClientException(response.buildFailureMessage());
    }
}
Also used : BulkItemResponse(org.elasticsearch.action.bulk.BulkItemResponse) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) FessConfig(org.codelibs.fess.mylasta.direction.FessConfig) DocMap(org.codelibs.fess.util.DocMap) BulkRequestBuilder(org.elasticsearch.action.bulk.BulkRequestBuilder) DocWriteRequest(org.elasticsearch.action.DocWriteRequest) Failure(org.elasticsearch.action.bulk.BulkItemResponse.Failure)

Example 37 with BulkItemResponse

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

the class ElasticsearchWriter method buildWriteReponse.

protected BulkWriterResponse buildWriteReponse(Iterable<Tuple> tuples, BulkResponse bulkResponse) throws Exception {
    // Elasticsearch responses are in the same order as the request, giving us an implicit mapping with Tuples
    BulkWriterResponse writerResponse = new BulkWriterResponse();
    if (bulkResponse.hasFailures()) {
        Iterator<BulkItemResponse> respIter = bulkResponse.iterator();
        Iterator<Tuple> tupleIter = tuples.iterator();
        while (respIter.hasNext() && tupleIter.hasNext()) {
            BulkItemResponse item = respIter.next();
            Tuple tuple = tupleIter.next();
            if (item.isFailed()) {
                writerResponse.addError(item.getFailure().getCause(), tuple);
            } else {
                writerResponse.addSuccess(tuple);
            }
            // Should never happen, so fail the entire batch if it does.
            if (respIter.hasNext() != tupleIter.hasNext()) {
                throw new Exception(bulkResponse.buildFailureMessage());
            }
        }
    } else {
        writerResponse.addAllSuccesses(tuples);
    }
    return writerResponse;
}
Also used : BulkItemResponse(org.elasticsearch.action.bulk.BulkItemResponse) BulkWriterResponse(org.apache.metron.common.writer.BulkWriterResponse) Tuple(org.apache.storm.tuple.Tuple)

Example 38 with BulkItemResponse

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

the class ElasticsearchWriterTest method testSuccessAndFailure.

@Test
public void testSuccessAndFailure() throws Exception {
    Tuple tuple1 = mock(Tuple.class);
    Tuple tuple2 = mock(Tuple.class);
    BulkResponse response = mock(BulkResponse.class);
    when(response.hasFailures()).thenReturn(true);
    Exception e = new IllegalStateException("Cause");
    BulkItemResponse itemResponse = buildBulkItemFailure(e);
    BulkItemResponse itemResponse2 = mock(BulkItemResponse.class);
    when(itemResponse2.isFailed()).thenReturn(false);
    when(response.iterator()).thenReturn(ImmutableList.of(itemResponse, itemResponse2).iterator());
    BulkWriterResponse expected = new BulkWriterResponse();
    expected.addError(e, tuple1);
    expected.addSuccess(tuple2);
    ElasticsearchWriter esWriter = new ElasticsearchWriter();
    BulkWriterResponse actual = esWriter.buildWriteReponse(ImmutableList.of(tuple1, tuple2), response);
    assertEquals("Response should have one error and one success", expected, actual);
}
Also used : BulkItemResponse(org.elasticsearch.action.bulk.BulkItemResponse) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) Tuple(org.apache.storm.tuple.Tuple) BulkWriterResponse(org.apache.metron.common.writer.BulkWriterResponse) Test(org.junit.Test)

Example 39 with BulkItemResponse

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

the class ElasticsearchWriterTest method testTwoSameFailure.

@Test
public void testTwoSameFailure() throws Exception {
    Tuple tuple1 = mock(Tuple.class);
    Tuple tuple2 = mock(Tuple.class);
    BulkResponse response = mock(BulkResponse.class);
    when(response.hasFailures()).thenReturn(true);
    Exception e = new IllegalStateException();
    BulkItemResponse itemResponse = buildBulkItemFailure(e);
    BulkItemResponse itemResponse2 = buildBulkItemFailure(e);
    when(response.iterator()).thenReturn(ImmutableList.of(itemResponse, itemResponse2).iterator());
    BulkWriterResponse expected = new BulkWriterResponse();
    expected.addError(e, tuple1);
    expected.addError(e, tuple2);
    ElasticsearchWriter esWriter = new ElasticsearchWriter();
    BulkWriterResponse actual = esWriter.buildWriteReponse(ImmutableList.of(tuple1, tuple2), response);
    assertEquals("Response should have two errors and no successes", expected, actual);
    // Ensure the errors actually get collapsed together
    Map<Throwable, Collection<Tuple>> actualErrors = actual.getErrors();
    HashMap<Throwable, Collection<Tuple>> expectedErrors = new HashMap<>();
    expectedErrors.put(e, ImmutableList.of(tuple1, tuple2));
    assertEquals("Errors should have collapsed together", expectedErrors, actualErrors);
}
Also used : HashMap(java.util.HashMap) BulkItemResponse(org.elasticsearch.action.bulk.BulkItemResponse) Collection(java.util.Collection) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) Tuple(org.apache.storm.tuple.Tuple) BulkWriterResponse(org.apache.metron.common.writer.BulkWriterResponse) Test(org.junit.Test)

Example 40 with BulkItemResponse

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

the class ElasticsearchWriterTest method testTwoDifferentFailure.

@Test
public void testTwoDifferentFailure() throws Exception {
    Tuple tuple1 = mock(Tuple.class);
    Tuple tuple2 = mock(Tuple.class);
    BulkResponse response = mock(BulkResponse.class);
    when(response.hasFailures()).thenReturn(true);
    Exception e = new IllegalStateException("Cause");
    Exception e2 = new IllegalStateException("Different Cause");
    BulkItemResponse itemResponse = buildBulkItemFailure(e);
    BulkItemResponse itemResponse2 = buildBulkItemFailure(e2);
    when(response.iterator()).thenReturn(ImmutableList.of(itemResponse, itemResponse2).iterator());
    BulkWriterResponse expected = new BulkWriterResponse();
    expected.addError(e, tuple1);
    expected.addError(e2, tuple2);
    ElasticsearchWriter esWriter = new ElasticsearchWriter();
    BulkWriterResponse actual = esWriter.buildWriteReponse(ImmutableList.of(tuple1, tuple2), response);
    assertEquals("Response should have two errors and no successes", expected, actual);
    // Ensure the errors did not get collapsed together
    Map<Throwable, Collection<Tuple>> actualErrors = actual.getErrors();
    HashMap<Throwable, Collection<Tuple>> expectedErrors = new HashMap<>();
    expectedErrors.put(e, ImmutableList.of(tuple1));
    expectedErrors.put(e2, ImmutableList.of(tuple2));
    assertEquals("Errors should not have collapsed together", expectedErrors, actualErrors);
}
Also used : HashMap(java.util.HashMap) BulkItemResponse(org.elasticsearch.action.bulk.BulkItemResponse) Collection(java.util.Collection) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) Tuple(org.apache.storm.tuple.Tuple) BulkWriterResponse(org.apache.metron.common.writer.BulkWriterResponse) Test(org.junit.Test)

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