Search in sources :

Example 6 with IndexSet

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

the class IndexRotationThreadTest method testDoNotPerformRotation.

@Test
public void testDoNotPerformRotation() throws NoTargetIndexException {
    final Provider<RotationStrategy> provider = new RotationStrategyProvider();
    final IndexRotationThread rotationThread = new IndexRotationThread(notificationService, indices, indexSetRegistry, cluster, new NullActivityWriter(), nodeId, ImmutableMap.<String, Provider<RotationStrategy>>builder().put("strategy", provider).build());
    when(indexSetConfig.rotationStrategyClass()).thenReturn("strategy");
    rotationThread.checkForRotation(indexSet);
    verify(indexSet, never()).cycle();
}
Also used : NullActivityWriter(org.graylog2.shared.system.activities.NullActivityWriter) RotationStrategy(org.graylog2.plugin.indexer.rotation.RotationStrategy) Test(org.junit.Test)

Example 7 with IndexSet

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

the class V20161116172200_CreateDefaultStreamMigrationTest method upgradePostsStreamsChangedEvent.

@Test
public void upgradePostsStreamsChangedEvent() throws Exception {
    when(indexSetRegistry.getDefault()).thenReturn(indexSet);
    when(streamService.load("000000000000000000000001")).thenThrow(NotFoundException.class);
    final ArgumentCaptor<StreamsChangedEvent> argumentCaptor = ArgumentCaptor.forClass(StreamsChangedEvent.class);
    migration.upgrade();
    verify(clusterEventBus).post(argumentCaptor.capture());
    final StreamsChangedEvent streamsChangedEvent = argumentCaptor.getValue();
    assertThat(streamsChangedEvent.streamIds()).containsOnly(Stream.DEFAULT_STREAM_ID);
}
Also used : StreamsChangedEvent(org.graylog2.streams.events.StreamsChangedEvent) Test(org.junit.Test)

Example 8 with IndexSet

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

the class Indices method deleteIndexTemplate.

public void deleteIndexTemplate(IndexSet indexSet) {
    final String templateName = indexSet.getConfig().indexTemplateName();
    final DeleteIndexTemplateRequest deleteRequest = c.admin().indices().prepareDeleteTemplate(templateName).request();
    try {
        final boolean acknowledged = c.admin().indices().deleteTemplate(deleteRequest).actionGet().isAcknowledged();
        if (acknowledged) {
            LOG.info("Deleted Graylog index template \"{}\" in Elasticsearch.", templateName);
        }
    } catch (Exception e) {
        LOG.error("Unable to delete the Graylog index template: " + templateName, e);
    }
}
Also used : DeleteIndexTemplateRequest(org.elasticsearch.action.admin.indices.template.delete.DeleteIndexTemplateRequest) ElasticsearchException(org.elasticsearch.ElasticsearchException) IndexClosedException(org.elasticsearch.indices.IndexClosedException) IndexNotFoundException(org.graylog2.indexer.IndexNotFoundException) IOException(java.io.IOException)

Example 9 with IndexSet

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

the class TimeBasedRotationStrategy method shouldRotate.

@Nullable
@Override
protected Result shouldRotate(String index, IndexSet indexSet) {
    final IndexSetConfig indexSetConfig = requireNonNull(indexSet.getConfig(), "Index set configuration must not be null");
    final String indexSetId = indexSetConfig.id();
    checkState(!isNullOrEmpty(index), "Index name must not be null or empty");
    checkState(!isNullOrEmpty(indexSetId), "Index set ID must not be null or empty");
    checkState(indexSetConfig.rotationStrategy() instanceof TimeBasedRotationStrategyConfig, "Invalid rotation strategy config <" + indexSetConfig.rotationStrategy().getClass().getCanonicalName() + "> for index set <" + indexSetId + ">");
    final TimeBasedRotationStrategyConfig config = (TimeBasedRotationStrategyConfig) indexSetConfig.rotationStrategy();
    final Period rotationPeriod = config.rotationPeriod().normalizedStandard();
    final DateTime now = Tools.nowUTC();
    // when first started, we might not know the last rotation time, look up the creation time of the index instead.
    if (!lastRotation.containsKey(indexSetId)) {
        final DateTime creationDate = indices.indexCreationDate(index);
        if (creationDate != null) {
            final DateTime currentAnchor = determineRotationPeriodAnchor(creationDate, rotationPeriod);
            anchor.put(indexSetId, currentAnchor);
            lastRotation.put(indexSetId, creationDate);
        }
        // still not able to figure out the last rotation time, we'll rotate forcibly
        if (!lastRotation.containsKey(indexSetId)) {
            return new SimpleResult(true, "No known previous rotation time, forcing index rotation now.");
        }
    }
    final DateTime currentAnchor = anchor.get(indexSetId);
    final DateTime nextRotation = currentAnchor.plus(rotationPeriod);
    if (nextRotation.isAfter(now)) {
        final String message = new MessageFormat("Next rotation at {0}", Locale.ENGLISH).format(new Object[] { nextRotation });
        return new SimpleResult(false, message);
    }
    // determine new anchor (push it to within less then one period before now) in case we missed one or more periods
    DateTime tmpAnchor;
    int multiplicator = 0;
    do {
        tmpAnchor = currentAnchor.withPeriodAdded(rotationPeriod, ++multiplicator);
    } while (tmpAnchor.isBefore(now));
    final DateTime nextAnchor = currentAnchor.withPeriodAdded(rotationPeriod, multiplicator - 1);
    anchor.put(indexSetId, nextAnchor);
    lastRotation.put(indexSetId, now);
    final String message = new MessageFormat("Rotation period {0} elapsed, next rotation at {1}", Locale.ENGLISH).format(new Object[] { now, nextAnchor });
    return new SimpleResult(true, message);
}
Also used : MessageFormat(java.text.MessageFormat) IndexSetConfig(org.graylog2.indexer.indexset.IndexSetConfig) Period(org.joda.time.Period) DateTime(org.joda.time.DateTime) Nullable(javax.annotation.Nullable)

Example 10 with IndexSet

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

the class Searches method determineAffectedIndicesWithRanges.

public Set<IndexRange> determineAffectedIndicesWithRanges(TimeRange range, @Nullable String filter) {
    final Optional<String> streamId = extractStreamId(filter);
    IndexSet indexSet = null;
    // a stream has changed: a stream only knows about its currently configured index set, no the history
    if (streamId.isPresent()) {
        try {
            final Stream stream = streamService.load(streamId.get());
            indexSet = stream.getIndexSet();
        } catch (NotFoundException ignored) {
        }
    }
    final ImmutableSortedSet.Builder<IndexRange> indices = ImmutableSortedSet.orderedBy(IndexRange.COMPARATOR);
    final SortedSet<IndexRange> indexRanges = indexRangeService.find(range.getFrom(), range.getTo());
    for (IndexRange indexRange : indexRanges) {
        // if we aren't in a stream search, we look at all the ranges matching the time range.
        if (indexSet == null && filter == null) {
            indices.add(indexRange);
            continue;
        }
        // A range applies to this search if either: the current index set of the stream matches or a previous index set matched.
        final boolean streamInIndexRange = streamId.isPresent() && indexRange.streamIds() != null && indexRange.streamIds().contains(streamId.get());
        final boolean streamInCurrentIndexSet = indexSet != null && indexSet.isManagedIndex(indexRange.indexName());
        if (streamInIndexRange) {
            indices.add(indexRange);
        }
        if (streamInCurrentIndexSet) {
            indices.add(indexRange);
        }
    }
    return indices.build();
}
Also used : IndexRange(org.graylog2.indexer.ranges.IndexRange) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) NotFoundException(org.graylog2.database.NotFoundException) Stream(org.graylog2.plugin.streams.Stream) IndexSet(org.graylog2.indexer.IndexSet)

Aggregations

IndexSet (org.graylog2.indexer.IndexSet)31 Test (org.junit.Test)26 IndexSetConfig (org.graylog2.indexer.indexset.IndexSetConfig)22 Timed (com.codahale.metrics.annotation.Timed)13 ApiOperation (io.swagger.annotations.ApiOperation)13 Path (javax.ws.rs.Path)12 AuditEvent (org.graylog2.audit.jersey.AuditEvent)11 ApiResponses (io.swagger.annotations.ApiResponses)10 Map (java.util.Map)9 POST (javax.ws.rs.POST)9 RequiresPermissions (org.apache.shiro.authz.annotation.RequiresPermissions)9 BadRequestException (javax.ws.rs.BadRequestException)8 NotFoundException (javax.ws.rs.NotFoundException)8 Produces (javax.ws.rs.Produces)8 DefaultIndexSetConfig (org.graylog2.indexer.indexset.DefaultIndexSetConfig)8 Set (java.util.Set)7 Collectors (java.util.stream.Collectors)7 Inject (javax.inject.Inject)7 IndexSetRegistry (org.graylog2.indexer.IndexSetRegistry)7 Api (io.swagger.annotations.Api)6