Search in sources :

Example 1 with ElasticsearchException

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

the class V20170607164210_MigrateReopenedIndicesToAliases method getReopenedIndices.

private Set<String> getReopenedIndices(final Collection<String> indices) {
    final SearchVersion elasticsearchVersion = node.getVersion().orElseThrow(() -> new ElasticsearchException("Unable to retrieve Elasticsearch version."));
    final JsonNode clusterStateJson = clusterState.getForIndices(indices);
    final JsonNode indicesJson = clusterStateJson.path("metadata").path("indices");
    final ImmutableSet.Builder<String> reopenedIndices = ImmutableSet.builder();
    if (indicesJson.isMissingNode()) {
        LOG.error("Retrieved cluster state is invalid (no metadata.indices key).");
        LOG.debug("Received cluster state was: {}", clusterStateJson.toString());
        return Collections.emptySet();
    }
    for (Iterator<Map.Entry<String, JsonNode>> it = indicesJson.fields(); it.hasNext(); ) {
        final Map.Entry<String, JsonNode> entry = it.next();
        final String indexName = entry.getKey();
        final JsonNode value = entry.getValue();
        final JsonNode indexSettings = value.path("settings");
        if (indexSettings.isMissingNode()) {
            LOG.error("Unable to retrieve index settings from metadata for index {} - skipping.", indexName);
            LOG.debug("Index metadata was: {}", value.toString());
            continue;
        }
        if (checkForReopened(indexSettings, elasticsearchVersion)) {
            LOG.debug("Adding {} to list of indices to be migrated.", indexName);
            reopenedIndices.add(indexName);
        }
    }
    return reopenedIndices.build();
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) JsonNode(com.fasterxml.jackson.databind.JsonNode) ElasticsearchException(org.graylog2.indexer.ElasticsearchException) SearchVersion(org.graylog2.storage.SearchVersion) Map(java.util.Map)

Example 2 with ElasticsearchException

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

the class JestUtilsTest method executeWithUnsuccessfulResponseAndErrorDetails.

@Test
public void executeWithUnsuccessfulResponseAndErrorDetails() throws Exception {
    final Ping request = new Ping.Builder().build();
    final JestResult resultMock = mock(JestResult.class);
    when(resultMock.isSucceeded()).thenReturn(false);
    final ObjectNode rootCauseStub = objectMapper.createObjectNode();
    rootCauseStub.set("reason", new TextNode("foobar"));
    final ArrayNode rootCausesStub = objectMapper.createArrayNode();
    rootCausesStub.add(rootCauseStub);
    final ObjectNode errorStub = objectMapper.createObjectNode();
    errorStub.set("root_cause", rootCausesStub);
    final ObjectNode responseStub = objectMapper.createObjectNode();
    responseStub.set("error", errorStub);
    when(resultMock.getJsonObject()).thenReturn(responseStub);
    when(clientMock.execute(request)).thenReturn(resultMock);
    try {
        JestUtils.execute(clientMock, request, () -> "BOOM");
        fail("Expected ElasticsearchException to be thrown");
    } catch (ElasticsearchException e) {
        assertThat(e).hasMessageStartingWith("BOOM").hasMessageEndingWith("foobar").hasNoSuppressedExceptions();
        assertThat(e.getErrorDetails()).containsExactly("foobar");
    }
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Ping(io.searchbox.core.Ping) TextNode(com.fasterxml.jackson.databind.node.TextNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) ElasticsearchException(org.graylog2.indexer.ElasticsearchException) JestResult(io.searchbox.client.JestResult) Test(org.junit.Test)

Example 3 with ElasticsearchException

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

the class JestUtilsTest method executeFailsWithCustomMessage.

@Test
public void executeFailsWithCustomMessage() throws Exception {
    final Ping request = new Ping.Builder().build();
    final JestResult resultMock = mock(JestResult.class);
    when(resultMock.isSucceeded()).thenReturn(false);
    final ObjectNode responseStub = objectMapper.createObjectNode();
    final ObjectNode errorStub = objectMapper.createObjectNode();
    responseStub.set("Message", new TextNode("Authorization header requires 'Credential' parameter."));
    errorStub.set("error", responseStub);
    when(resultMock.getJsonObject()).thenReturn(errorStub);
    when(clientMock.execute(request)).thenReturn(resultMock);
    try {
        JestUtils.execute(clientMock, request, () -> "BOOM");
        fail("Expected ElasticsearchException to be thrown");
    } catch (ElasticsearchException e) {
        assertThat(e).hasMessageStartingWith("BOOM").hasMessageEndingWith("{\"Message\":\"Authorization header requires 'Credential' parameter.\"}").hasNoSuppressedExceptions();
        assertThat(e.getErrorDetails()).containsExactly("{\"Message\":\"Authorization header requires 'Credential' parameter.\"}");
    }
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Ping(io.searchbox.core.Ping) TextNode(com.fasterxml.jackson.databind.node.TextNode) ElasticsearchException(org.graylog2.indexer.ElasticsearchException) JestResult(io.searchbox.client.JestResult) Test(org.junit.Test)

Example 4 with ElasticsearchException

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

the class MessagesAdapterES7 method runBulkRequest.

private BulkResponse runBulkRequest(int indexedSuccessfully, List<IndexingRequest> chunk) throws ChunkedBulkIndexer.EntityTooLargeException {
    final BulkRequest bulkRequest = createBulkRequest(chunk);
    final BulkResponse result;
    try {
        result = this.client.execute((c, requestOptions) -> c.bulk(bulkRequest, requestOptions));
    } catch (ElasticsearchException e) {
        for (ElasticsearchException cause : e.guessRootCauses()) {
            if (cause.status().equals(RestStatus.REQUEST_ENTITY_TOO_LARGE)) {
                throw new ChunkedBulkIndexer.EntityTooLargeException(indexedSuccessfully, indexingErrorsFrom(chunk));
            }
        }
        throw new org.graylog2.indexer.ElasticsearchException(e);
    }
    return result;
}
Also used : Iterables(com.google.common.collect.Iterables) Arrays(java.util.Arrays) IndexRequest(org.graylog.shaded.elasticsearch7.org.elasticsearch.action.index.IndexRequest) MessagesAdapter(org.graylog2.indexer.messages.MessagesAdapter) ElasticsearchException(org.graylog.shaded.elasticsearch7.org.elasticsearch.ElasticsearchException) LoggerFactory(org.slf4j.LoggerFactory) XContentType(org.graylog.shaded.elasticsearch7.org.elasticsearch.common.xcontent.XContentType) GetResponse(org.graylog.shaded.elasticsearch7.org.elasticsearch.action.get.GetResponse) Function(java.util.function.Function) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) Meter(com.codahale.metrics.Meter) Indexable(org.graylog2.indexer.messages.Indexable) ResultMessage(org.graylog2.indexer.results.ResultMessage) Locale(java.util.Locale) Map(java.util.Map) Messages(org.graylog2.indexer.messages.Messages) AnalyzeResponse(org.graylog.shaded.elasticsearch7.org.elasticsearch.client.indices.AnalyzeResponse) MetricRegistry(com.codahale.metrics.MetricRegistry) Logger(org.slf4j.Logger) IndexingRequest(org.graylog2.indexer.messages.IndexingRequest) BulkItemResponse(org.graylog.shaded.elasticsearch7.org.elasticsearch.action.bulk.BulkItemResponse) AnalyzeRequest(org.graylog.shaded.elasticsearch7.org.elasticsearch.client.indices.AnalyzeRequest) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) GetRequest(org.graylog.shaded.elasticsearch7.org.elasticsearch.action.get.GetRequest) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) RestStatus(org.graylog.shaded.elasticsearch7.org.elasticsearch.rest.RestStatus) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) List(java.util.List) BulkResponse(org.graylog.shaded.elasticsearch7.org.elasticsearch.action.bulk.BulkResponse) BulkRequest(org.graylog.shaded.elasticsearch7.org.elasticsearch.action.bulk.BulkRequest) ChunkedBulkIndexer(org.graylog2.indexer.messages.ChunkedBulkIndexer) Collections(java.util.Collections) MetricRegistry.name(com.codahale.metrics.MetricRegistry.name) DocumentNotFoundException(org.graylog2.indexer.messages.DocumentNotFoundException) BulkRequest(org.graylog.shaded.elasticsearch7.org.elasticsearch.action.bulk.BulkRequest) BulkResponse(org.graylog.shaded.elasticsearch7.org.elasticsearch.action.bulk.BulkResponse) ElasticsearchException(org.graylog.shaded.elasticsearch7.org.elasticsearch.ElasticsearchException) ChunkedBulkIndexer(org.graylog2.indexer.messages.ChunkedBulkIndexer)

Example 5 with ElasticsearchException

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

the class ElasticsearchBackend method checkForFailedShards.

private Optional<ElasticsearchException> checkForFailedShards(MultiSearchResponse.Item multiSearchResponse) {
    if (multiSearchResponse.isFailure()) {
        return Optional.of(new ElasticsearchException(multiSearchResponse.getFailureMessage(), multiSearchResponse.getFailure()));
    }
    final SearchResponse searchResponse = multiSearchResponse.getResponse();
    if (searchResponse != null && searchResponse.getFailedShards() > 0) {
        final List<Throwable> shardFailures = Arrays.stream(searchResponse.getShardFailures()).map(ShardOperationFailedException::getCause).collect(Collectors.toList());
        final List<String> nonNumericFieldErrors = shardFailures.stream().filter(shardFailure -> shardFailure.getMessage().contains("Expected numeric type on field")).map(Throwable::getMessage).distinct().collect(Collectors.toList());
        if (!nonNumericFieldErrors.isEmpty()) {
            return Optional.of(new FieldTypeException("Unable to perform search query: ", nonNumericFieldErrors));
        }
        final List<String> errors = shardFailures.stream().map(Throwable::getMessage).distinct().collect(Collectors.toList());
        return Optional.of(new ElasticsearchException("Unable to perform search query: ", errors));
    }
    return Optional.empty();
}
Also used : ESSearchTypeHandler(org.graylog.storage.elasticsearch7.views.searchtypes.ESSearchTypeHandler) AndFilter(org.graylog.plugins.views.search.filter.AndFilter) ElasticsearchClient(org.graylog.storage.elasticsearch7.ElasticsearchClient) Arrays(java.util.Arrays) BackendQuery(org.graylog.plugins.views.search.engine.BackendQuery) QueryBackend(org.graylog.plugins.views.search.engine.QueryBackend) Provider(javax.inject.Provider) LoggerFactory(org.slf4j.LoggerFactory) FieldTypeException(org.graylog2.indexer.FieldTypeException) MultiSearchResponse(org.graylog.shaded.elasticsearch7.org.elasticsearch.action.search.MultiSearchResponse) StreamFilter(org.graylog.plugins.views.search.filter.StreamFilter) Map(java.util.Map) IndicesOptions(org.graylog.shaded.elasticsearch7.org.elasticsearch.action.support.IndicesOptions) SearchConfig(org.graylog.plugins.views.search.engine.SearchConfig) Set(java.util.Set) Collectors(java.util.stream.Collectors) BoolQueryBuilder(org.graylog.shaded.elasticsearch7.org.elasticsearch.index.query.BoolQueryBuilder) Objects(java.util.Objects) List(java.util.List) Filter(org.graylog.plugins.views.search.Filter) Optional(java.util.Optional) SearchResponse(org.graylog.shaded.elasticsearch7.org.elasticsearch.action.search.SearchResponse) Query(org.graylog.plugins.views.search.Query) SearchTypeErrorParser(org.graylog.plugins.views.search.errors.SearchTypeErrorParser) HashMap(java.util.HashMap) ElasticsearchException(org.graylog2.indexer.ElasticsearchException) ShardOperationFailedException(org.graylog.shaded.elasticsearch7.org.elasticsearch.action.ShardOperationFailedException) ArrayList(java.util.ArrayList) GlobalOverride(org.graylog.plugins.views.search.GlobalOverride) Inject(javax.inject.Inject) HashSet(java.util.HashSet) OrFilter(org.graylog.plugins.views.search.filter.OrFilter) SearchType(org.graylog.plugins.views.search.SearchType) QueryStringFilter(org.graylog.plugins.views.search.filter.QueryStringFilter) SearchTypeError(org.graylog.plugins.views.search.errors.SearchTypeError) QueryResult(org.graylog.plugins.views.search.QueryResult) TimeRangeQueryFactory(org.graylog.storage.elasticsearch7.TimeRangeQueryFactory) SearchJob(org.graylog.plugins.views.search.SearchJob) QueryBuilder(org.graylog.shaded.elasticsearch7.org.elasticsearch.index.query.QueryBuilder) SearchSourceBuilder(org.graylog.shaded.elasticsearch7.org.elasticsearch.search.builder.SearchSourceBuilder) Logger(org.slf4j.Logger) SearchRequest(org.graylog.shaded.elasticsearch7.org.elasticsearch.action.search.SearchRequest) Maps(com.google.common.collect.Maps) QueryStringDecorators(org.graylog.plugins.views.search.elasticsearch.QueryStringDecorators) Named(com.google.inject.name.Named) IndexLookup(org.graylog.plugins.views.search.elasticsearch.IndexLookup) QueryBuilders(org.graylog.shaded.elasticsearch7.org.elasticsearch.index.query.QueryBuilders) Message(org.graylog2.plugin.Message) Collections(java.util.Collections) FieldTypeException(org.graylog2.indexer.FieldTypeException) ElasticsearchException(org.graylog2.indexer.ElasticsearchException) MultiSearchResponse(org.graylog.shaded.elasticsearch7.org.elasticsearch.action.search.MultiSearchResponse) SearchResponse(org.graylog.shaded.elasticsearch7.org.elasticsearch.action.search.SearchResponse)

Aggregations

ElasticsearchException (org.graylog2.indexer.ElasticsearchException)17 JestResult (io.searchbox.client.JestResult)8 List (java.util.List)7 Collectors (java.util.stream.Collectors)7 Inject (javax.inject.Inject)7 ArrayList (java.util.ArrayList)6 Collections (java.util.Collections)6 Map (java.util.Map)6 Optional (java.util.Optional)6 Set (java.util.Set)6 Logger (org.slf4j.Logger)6 LoggerFactory (org.slf4j.LoggerFactory)6 JsonNode (com.fasterxml.jackson.databind.JsonNode)5 Arrays (java.util.Arrays)5 HashMap (java.util.HashMap)5 IOException (java.io.IOException)4 Message (org.graylog2.plugin.Message)4 IndicesOptions (org.graylog.shaded.elasticsearch7.org.elasticsearch.action.support.IndicesOptions)3 Test (org.junit.Test)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2