Search in sources :

Example 21 with Indices

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

the class RebuildIndexRangesJob method execute.

@Override
public void execute() {
    info("Recalculating index ranges.");
    // for each index set we know about
    final ListMultimap<IndexSet, String> indexSets = MultimapBuilder.hashKeys().arrayListValues().build();
    for (IndexSet indexSet : this.indexSets) {
        final String[] managedIndicesNames = indexSet.getManagedIndices();
        for (String name : managedIndicesNames) {
            indexSets.put(indexSet, name);
        }
    }
    if (indexSets.size() == 0) {
        info("No indices, nothing to calculate.");
        return;
    }
    indicesToCalculate = indexSets.values().size();
    Stopwatch sw = Stopwatch.createStarted();
    for (IndexSet indexSet : indexSets.keySet()) {
        LOG.info("Recalculating index ranges for index set {} ({}): {} indices affected.", indexSet.getConfig().title(), indexSet.getIndexWildcard(), indexSets.get(indexSet).size());
        for (String index : indexSets.get(indexSet)) {
            try {
                if (index.equals(indexSet.getActiveWriteIndex())) {
                    LOG.debug("{} is current write target, do not calculate index range for it", index);
                    final IndexRange emptyRange = indexRangeService.createUnknownRange(index);
                    try {
                        final IndexRange indexRange = indexRangeService.get(index);
                        if (indexRange.begin().getMillis() != 0 || indexRange.end().getMillis() != 0) {
                            LOG.info("Invalid date ranges for write index {}, resetting it.", index);
                            indexRangeService.save(emptyRange);
                        }
                    } catch (NotFoundException e) {
                        LOG.info("No index range found for write index {}, recreating it.", index);
                        indexRangeService.save(emptyRange);
                    }
                    indicesCalculated.incrementAndGet();
                    continue;
                }
            } catch (TooManyAliasesException e) {
                LOG.error("Multiple write alias targets found, this is a bug.");
                indicesCalculated.incrementAndGet();
                continue;
            }
            if (cancelRequested) {
                info("Stop requested. Not calculating next index range, not updating ranges.");
                sw.stop();
                return;
            }
            try {
                final IndexRange indexRange = indexRangeService.calculateRange(index);
                indexRangeService.save(indexRange);
                LOG.info("Created ranges for index {}: {}", index, indexRange);
            } catch (Exception e) {
                LOG.info("Could not calculate range of index [" + index + "]. Skipping.", e);
            } finally {
                indicesCalculated.incrementAndGet();
            }
        }
    }
    info("Done calculating index ranges for " + indicesToCalculate + " indices. Took " + sw.stop().elapsed(TimeUnit.MILLISECONDS) + "ms.");
}
Also used : Stopwatch(com.google.common.base.Stopwatch) NotFoundException(org.graylog2.database.NotFoundException) TooManyAliasesException(org.graylog2.indexer.indices.TooManyAliasesException) IndexSet(org.graylog2.indexer.IndexSet) TooManyAliasesException(org.graylog2.indexer.indices.TooManyAliasesException) NotFoundException(org.graylog2.database.NotFoundException)

Example 22 with Indices

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

the class Indices method getAllMessageFieldsForIndices.

public Map<String, Set<String>> getAllMessageFieldsForIndices(final String[] writeIndexWildcards) {
    final Map<String, Set<String>> fields = new HashMap<>();
    final ClusterStateRequest csr = new ClusterStateRequest().blocks(true).nodes(true).indices(writeIndexWildcards);
    final ClusterState cs = c.admin().cluster().state(csr).actionGet().getState();
    for (ObjectObjectCursor<String, IndexMetaData> m : cs.getMetaData().indices()) {
        try {
            MappingMetaData mmd = m.value.mapping(IndexMapping.TYPE_MESSAGE);
            if (mmd == null) {
                // There is no mapping if there are no messages in the index.
                continue;
            }
            @SuppressWarnings("unchecked") final Map<String, Object> mapping = (Map<String, Object>) mmd.getSourceAsMap().get("properties");
            if (mapping != null) {
                fields.put(m.key, mapping.keySet());
            }
        } catch (Exception e) {
            LOG.error("Error while trying to get fields of <" + m.index + ">", e);
        }
    }
    return fields;
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) IndexSet(org.graylog2.indexer.IndexSet) Collectors.toSet(java.util.stream.Collectors.toSet) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) HashMap(java.util.HashMap) ClusterStateRequest(org.elasticsearch.action.admin.cluster.state.ClusterStateRequest) MappingMetaData(org.elasticsearch.cluster.metadata.MappingMetaData) ElasticsearchException(org.elasticsearch.ElasticsearchException) IndexClosedException(org.elasticsearch.indices.IndexClosedException) IndexNotFoundException(org.graylog2.indexer.IndexNotFoundException) IOException(java.io.IOException) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) ImmutableOpenMap(org.elasticsearch.common.collect.ImmutableOpenMap) HashMap(java.util.HashMap)

Example 23 with Indices

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

the class Indices method ensureIndexTemplate.

private void ensureIndexTemplate(IndexSet indexSet) {
    final Map<String, Object> template = indexMapping.messageTemplate(indexSet.getIndexWildcard(), indexSet.getConfig().indexAnalyzer());
    final PutIndexTemplateRequest itr = c.admin().indices().preparePutTemplate(indexSet.getConfig().indexTemplateName()).setOrder(// Make sure templates with "order: 0" and higher are applied after our template!
    -1).setSource(template).request();
    try {
        final boolean acknowledged = c.admin().indices().putTemplate(itr).actionGet().isAcknowledged();
        if (acknowledged) {
            LOG.info("Created Graylog index template \"{}\" in Elasticsearch.", indexSet.getConfig().indexTemplateName());
        }
    } catch (Exception e) {
        LOG.error("Unable to create the Graylog index template: " + indexSet.getConfig().indexTemplateName(), e);
    }
}
Also used : PutIndexTemplateRequest(org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateRequest) ElasticsearchException(org.elasticsearch.ElasticsearchException) IndexClosedException(org.elasticsearch.indices.IndexClosedException) IndexNotFoundException(org.graylog2.indexer.IndexNotFoundException) IOException(java.io.IOException)

Example 24 with Indices

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

the class IndexSetCleanupJob method execute.

@Override
public void execute() {
    final IndexSetConfig config = indexSet.getConfig();
    final String[] managedIndices = indexSet.getManagedIndices();
    this.total = managedIndices.length;
    try {
        LOG.info("Deleting index template <{}> from Elasticsearch", config.indexTemplateName());
        indices.deleteIndexTemplate(indexSet);
    } catch (IndexTemplateMissingException ignored) {
        LOG.debug("Unable to delete index template <{}> because it does not exist.", config.indexTemplateName());
    } catch (Exception e) {
        LOG.error("Unable to delete index template <{}>", config.indexTemplateName(), e);
    }
    for (String indexName : managedIndices) {
        if (cancel) {
            LOG.info("Cancel requested. Deleted <{}> of <{}> indices.", deleted, total);
            break;
        }
        try {
            LOG.info("Removing index range information for index: {}", indexName);
            indexRangeService.remove(indexName);
            LOG.info("Deleting index <{}> in index set <{}> ({})", indexName, config.id(), config.title());
            indices.delete(indexName);
            deleted.incrementAndGet();
        } catch (Exception e) {
            LOG.error("Unable to delete index <{}>", indexName, e);
        }
    }
}
Also used : IndexTemplateMissingException(org.elasticsearch.indices.IndexTemplateMissingException) IndexSetConfig(org.graylog2.indexer.indexset.IndexSetConfig) IndexTemplateMissingException(org.elasticsearch.indices.IndexTemplateMissingException)

Example 25 with Indices

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

the class SearchesTest method determineAffectedIndicesDoesNotIncludesDeflectorTargetIfMissing.

@Test
public void determineAffectedIndicesDoesNotIncludesDeflectorTargetIfMissing() throws Exception {
    final DateTime now = DateTime.now(DateTimeZone.UTC);
    final MongoIndexRange indexRange0 = MongoIndexRange.create("graylog_0", now, now.plusDays(1), now, 0);
    final MongoIndexRange indexRange1 = MongoIndexRange.create("graylog_1", now.plusDays(1), now.plusDays(2), now, 0);
    final SortedSet<IndexRange> indices = ImmutableSortedSet.orderedBy(IndexRange.COMPARATOR).add(indexRange0).add(indexRange1).build();
    when(indexRangeService.find(any(DateTime.class), any(DateTime.class))).thenReturn(indices);
    final TimeRange absoluteRange = AbsoluteRange.create(now.minusDays(1), now.plusDays(1));
    final TimeRange keywordRange = KeywordRange.create("1 day ago");
    final TimeRange relativeRange = RelativeRange.create(3600);
    assertThat(searches.determineAffectedIndices(absoluteRange, null)).containsOnly(indexRange0.indexName(), indexRange1.indexName());
    assertThat(searches.determineAffectedIndices(keywordRange, null)).containsOnly(indexRange0.indexName(), indexRange1.indexName());
    assertThat(searches.determineAffectedIndices(relativeRange, null)).containsOnly(indexRange0.indexName(), indexRange1.indexName());
}
Also used : MongoIndexRange(org.graylog2.indexer.ranges.MongoIndexRange) IndexRange(org.graylog2.indexer.ranges.IndexRange) TimeRange(org.graylog2.plugin.indexer.searches.timeranges.TimeRange) MongoIndexRange(org.graylog2.indexer.ranges.MongoIndexRange) ZonedDateTime(java.time.ZonedDateTime) DateTime(org.joda.time.DateTime) Test(org.junit.Test)

Aggregations

IndexSet (org.graylog2.indexer.IndexSet)14 Test (org.junit.Test)12 IndexRange (org.graylog2.indexer.ranges.IndexRange)10 Set (java.util.Set)9 ZonedDateTime (java.time.ZonedDateTime)6 Map (java.util.Map)6 DateTime (org.joda.time.DateTime)6 Timed (com.codahale.metrics.annotation.Timed)5 ApiOperation (io.swagger.annotations.ApiOperation)5 ApiResponses (io.swagger.annotations.ApiResponses)5 IOException (java.io.IOException)5 HashMap (java.util.HashMap)5 Collectors (java.util.stream.Collectors)5 Inject (javax.inject.Inject)5 POST (javax.ws.rs.POST)5 Path (javax.ws.rs.Path)5 Produces (javax.ws.rs.Produces)5 AuditEvent (org.graylog2.audit.jersey.AuditEvent)5 MongoIndexRange (org.graylog2.indexer.ranges.MongoIndexRange)5 TimeRange (org.graylog2.plugin.indexer.searches.timeranges.TimeRange)5