Search in sources :

Example 6 with IndexingRequest

use of org.graylog2.indexer.messages.IndexingRequest in project graylog2-server by Graylog2.

the class MessagesAdapterES6 method bulkIndexChunked.

private List<Messages.IndexingError> bulkIndexChunked(ChunkedBulkIndexer.Chunk command) throws ChunkedBulkIndexer.EntityTooLargeException, IOException {
    final List<IndexingRequest> messageList = command.requests;
    final int offset = command.offset;
    int chunkSize = Math.min(messageList.size(), command.size);
    final List<BulkResult.BulkResultItem> failedItems = new ArrayList<>();
    final Iterable<List<IndexingRequest>> chunks = Iterables.partition(messageList.subList(offset, messageList.size()), chunkSize);
    int chunkCount = 1;
    int indexedSuccessfully = 0;
    for (List<IndexingRequest> chunk : chunks) {
        final BulkResult result = bulkIndexChunk(chunk);
        if (result.getResponseCode() == 413) {
            throw new ChunkedBulkIndexer.EntityTooLargeException(indexedSuccessfully, indexingErrorsFrom(failedItems, messageList));
        }
        if (result.getResponseCode() >= 400) {
            throw JestUtils.specificException(() -> "Error during bulk indexing: ", result.getJsonObject().get("error"));
        }
        indexedSuccessfully += chunk.size();
        final List<BulkResult.BulkResultItem> remainingFailures = result.getFailedItems();
        failedItems.addAll(remainingFailures);
        if (LOG.isDebugEnabled()) {
            String chunkInfo = "";
            if (chunkSize != messageList.size()) {
                chunkInfo = String.format(Locale.ROOT, " (chunk %d/%d offset %d)", chunkCount, (int) Math.ceil((double) messageList.size() / chunkSize), offset);
            }
            LOG.debug("Index: Bulk indexed {} messages{}, failures: {}", result.getItems().size(), chunkInfo, failedItems.size());
        }
        if (!remainingFailures.isEmpty()) {
            LOG.error("Failed to index [{}] messages. Please check the index error log in your web interface for the reason. Error: {}", remainingFailures.size(), result.getErrorMessage());
        }
        chunkCount++;
    }
    return indexingErrorsFrom(failedItems, messageList);
}
Also used : IndexingRequest(org.graylog2.indexer.messages.IndexingRequest) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) BulkResult(io.searchbox.core.BulkResult)

Example 7 with IndexingRequest

use of org.graylog2.indexer.messages.IndexingRequest in project graylog2-server by Graylog2.

the class MessagesTest method bulkIndexRequests_nothingPropagatedToFailureSubmissionServiceWhenThereAreNoIndexingErrors.

@Test
public void bulkIndexRequests_nothingPropagatedToFailureSubmissionServiceWhenThereAreNoIndexingErrors() throws Exception {
    // given
    final DateTime ts = Tools.nowUTC();
    final IndexSet indexSet = mock(IndexSet.class);
    final Message message1 = message("msg-1", ts);
    final Message message2 = message("msg-2", ts);
    final List<IndexingRequest> indexingRequest = ImmutableList.of(IndexingRequest.create(indexSet, message1), IndexingRequest.create(indexSet, message2));
    when(messagesAdapter.bulkIndex(indexingRequest)).thenReturn(ImmutableList.of());
    // when
    final List<String> failureIds = messages.bulkIndexRequests(indexingRequest, false);
    // then
    assertThat(failureIds).isEmpty();
    verifyNoInteractions(failureSubmissionService);
}
Also used : Message(org.graylog2.plugin.Message) DateTime(org.joda.time.DateTime) IndexSet(org.graylog2.indexer.IndexSet) Test(org.junit.Test)

Example 8 with IndexingRequest

use of org.graylog2.indexer.messages.IndexingRequest in project graylog2-server by Graylog2.

the class MessagesTest method bulkIndexRequests_allNonIndexBlockErrorsPropagatedToTheFailureSubmissionService.

@Test
public void bulkIndexRequests_allNonIndexBlockErrorsPropagatedToTheFailureSubmissionService() throws Exception {
    // given
    final DateTime ts = Tools.nowUTC();
    final IndexSet indexSet = mock(IndexSet.class);
    final Message message1 = mock(Message.class);
    final Message message2 = message("msg-2", ts);
    final Message message3 = message("msg-3", ts);
    final Message message4 = message("msg-4", ts);
    final List<IndexingRequest> indexingRequest = ImmutableList.of(IndexingRequest.create(indexSet, message1), IndexingRequest.create(indexSet, message2), IndexingRequest.create(indexSet, message3));
    when(messagesAdapter.bulkIndex(indexingRequest)).thenReturn(ImmutableList.of(Messages.IndexingError.create(message2, "msg-index", Messages.IndexingError.ErrorType.MappingError, "Some error message"), Messages.IndexingError.create(message3, "msg-index", Messages.IndexingError.ErrorType.MappingError, "Some error message"), Messages.IndexingError.create(message4, "msg-index", Messages.IndexingError.ErrorType.IndexBlocked, "Index blocked error message")));
    // when
    final List<String> failureIds = messages.bulkIndexRequests(indexingRequest, false);
    // then
    assertThat(failureIds).hasSize(2).containsExactlyInAnyOrder("msg-2", "msg-3");
    verify(failureSubmissionService, times(1)).submitIndexingErrors(indexingErrorsArgumentCaptor.capture());
    assertThat(indexingErrorsArgumentCaptor.getValue().stream().sorted(Comparator.comparing(e -> e.message().getMessageId())).collect(Collectors.toList())).satisfies(indexingErrors -> {
        assertThat(indexingErrors.get(0)).satisfies(indexingError -> {
            assertThat(indexingError.errorType()).isEqualTo(Messages.IndexingError.ErrorType.MappingError);
            assertThat(indexingError.message()).isEqualTo(message2);
        });
        assertThat(indexingErrors.get(1)).satisfies(indexingError -> {
            assertThat(indexingError.errorType()).isEqualTo(Messages.IndexingError.ErrorType.MappingError);
            assertThat(indexingError.message()).isEqualTo(message3);
        });
    });
}
Also used : Message(org.graylog2.plugin.Message) DateTime(org.joda.time.DateTime) IndexSet(org.graylog2.indexer.IndexSet) Test(org.junit.Test)

Example 9 with IndexingRequest

use of org.graylog2.indexer.messages.IndexingRequest in project graylog2-server by Graylog2.

the class MessagesAdapterES6 method bulkIndexChunk.

private BulkResult bulkIndexChunk(List<IndexingRequest> chunk) throws IOException {
    final Bulk.Builder bulk = new Bulk.Builder();
    for (IndexingRequest entry : chunk) {
        final Indexable message = entry.message();
        bulk.addAction(new Index.Builder(message.toElasticSearchObject(objectMapper, invalidTimestampMeter)).index(entry.indexSet().getWriteIndexAlias()).type(IndexMapping.TYPE_MESSAGE).id(message.getId()).build());
    }
    return runBulkRequest(bulk.build(), chunk.size());
}
Also used : IndexingRequest(org.graylog2.indexer.messages.IndexingRequest) Indexable(org.graylog2.indexer.messages.Indexable) Index(io.searchbox.core.Index) Bulk(io.searchbox.core.Bulk)

Example 10 with IndexingRequest

use of org.graylog2.indexer.messages.IndexingRequest in project graylog2-server by Graylog2.

the class MessagesAdapterES6Test method bulkIndexingParsesPrimaryShardUnavailableErrors.

@Test
public void bulkIndexingParsesPrimaryShardUnavailableErrors() throws Exception {
    final String messageId = "BOOMID";
    final BulkResult failedJestResult = mock(BulkResult.class);
    final BulkResult.BulkResultItem bulkResultItem = new MockedBulkResult().createResultItem("index", "someindex", "message", messageId, 400, "{\"type\":\"unavailable_shards_exception\",\"reason\":\"primary shard is not active\"\"}}", null, "unavailable_shards_exception", "primary shard is not active");
    when(failedJestResult.isSucceeded()).thenReturn(false);
    when(failedJestResult.getFailedItems()).thenReturn(ImmutableList.of(bulkResultItem));
    when(jestClient.execute(any())).thenReturn(failedJestResult).thenThrow(new IllegalStateException("JestResult#execute should not be called twice."));
    final List<IndexingRequest> messageList = messageListWith(messageWithId(messageId));
    final List<Messages.IndexingError> result = messagesAdapter.bulkIndex(messageList);
    assertThat(result).hasSize(1).extracting(indexingError -> indexingError.message().getId(), Messages.IndexingError::errorType, Messages.IndexingError::errorMessage).containsExactly(tuple(messageId, Messages.IndexingError.ErrorType.IndexBlocked, "primary shard is not active"));
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) DateTimeZone(org.joda.time.DateTimeZone) Arrays(java.util.Arrays) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) INDEX_BLOCK_ERROR(org.graylog.storage.elasticsearch6.MessagesAdapterES6.INDEX_BLOCK_ERROR) JestClient(io.searchbox.client.JestClient) ImmutableList(com.google.common.collect.ImmutableList) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) Messages(org.graylog2.indexer.messages.Messages) IndexSet(org.graylog2.indexer.IndexSet) MetricRegistry(com.codahale.metrics.MetricRegistry) IndexingRequest(org.graylog2.indexer.messages.IndexingRequest) Assertions.tuple(org.assertj.core.api.Assertions.tuple) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) DateTime(org.joda.time.DateTime) ObjectMapperProvider(org.graylog2.shared.bindings.providers.ObjectMapperProvider) IOException(java.io.IOException) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) Collectors(java.util.stream.Collectors) INDEX_BLOCK_REASON(org.graylog.storage.elasticsearch6.MessagesAdapterES6.INDEX_BLOCK_REASON) Mockito.verify(org.mockito.Mockito.verify) BulkResult(io.searchbox.core.BulkResult) Test(org.junit.jupiter.api.Test) List(java.util.List) Mockito.never(org.mockito.Mockito.never) ChunkedBulkIndexer(org.graylog2.indexer.messages.ChunkedBulkIndexer) Message(org.graylog2.plugin.Message) Collections(java.util.Collections) Mockito.mock(org.mockito.Mockito.mock) IndexingRequest(org.graylog2.indexer.messages.IndexingRequest) Messages(org.graylog2.indexer.messages.Messages) BulkResult(io.searchbox.core.BulkResult) Test(org.junit.jupiter.api.Test)

Aggregations

IndexingRequest (org.graylog2.indexer.messages.IndexingRequest)9 BulkResult (io.searchbox.core.BulkResult)5 List (java.util.List)5 Message (org.graylog2.plugin.Message)5 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 IndexSet (org.graylog2.indexer.IndexSet)4 Messages (org.graylog2.indexer.messages.Messages)4 DateTime (org.joda.time.DateTime)4 Test (org.junit.jupiter.api.Test)4 MetricRegistry (com.codahale.metrics.MetricRegistry)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 Arrays (java.util.Arrays)3 Collections (java.util.Collections)3 Collectors (java.util.stream.Collectors)3 ChunkedBulkIndexer (org.graylog2.indexer.messages.ChunkedBulkIndexer)3 Indexable (org.graylog2.indexer.messages.Indexable)3 ImmutableList (com.google.common.collect.ImmutableList)2 JestClient (io.searchbox.client.JestClient)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2