Search in sources :

Example 11 with IndexSetConfig

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

the class V20161122174500_AssignIndexSetsToStreamsMigrationTest method upgrade.

@Test
public void upgrade() throws Exception {
    final Stream stream1 = mock(Stream.class);
    final Stream stream2 = mock(Stream.class);
    final IndexSetConfig indexSetConfig = mock(IndexSetConfig.class);
    when(indexSetService.findAll()).thenReturn(Collections.singletonList(indexSetConfig));
    when(indexSetConfig.id()).thenReturn("abc123");
    when(stream1.getId()).thenReturn("stream1");
    when(stream2.getId()).thenReturn("stream2");
    when(streamService.loadAll()).thenReturn(Lists.newArrayList(stream1, stream2));
    migration.upgrade();
    verify(stream1).setIndexSetId(indexSetConfig.id());
    verify(stream2).setIndexSetId(indexSetConfig.id());
    verify(streamService, times(1)).save(stream1);
    verify(streamService, times(1)).save(stream2);
    verify(clusterConfigService, times(1)).write(V20161122174500_AssignIndexSetsToStreamsMigration.MigrationCompleted.create(indexSetConfig.id(), Sets.newHashSet("stream1", "stream2"), Collections.emptySet()));
    verify(clusterEventBus, times(1)).post(StreamsChangedEvent.create(ImmutableSet.of("stream1", "stream2")));
}
Also used : IndexSetConfig(org.graylog2.indexer.indexset.IndexSetConfig) Stream(org.graylog2.plugin.streams.Stream) Test(org.junit.Test)

Example 12 with IndexSetConfig

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

the class V20161122174500_AssignIndexSetsToStreamsMigrationTest method upgradeWithAlreadyAssignedIndexSet.

@Test
public void upgradeWithAlreadyAssignedIndexSet() throws Exception {
    final Stream stream1 = mock(Stream.class);
    final Stream stream2 = mock(Stream.class);
    final IndexSetConfig indexSetConfig = mock(IndexSetConfig.class);
    when(indexSetService.findAll()).thenReturn(Collections.singletonList(indexSetConfig));
    when(indexSetConfig.id()).thenReturn("abc123");
    when(stream1.getId()).thenReturn("stream1");
    when(stream2.getId()).thenReturn("stream2");
    when(streamService.loadAll()).thenReturn(Lists.newArrayList(stream1, stream2));
    when(stream2.getIndexSetId()).thenReturn("abc123");
    migration.upgrade();
    verify(stream1).setIndexSetId(indexSetConfig.id());
    verify(stream2, never()).setIndexSetId(indexSetConfig.id());
    verify(streamService, times(1)).save(stream1);
    verify(streamService, never()).save(stream2);
    verify(clusterConfigService, times(1)).write(V20161122174500_AssignIndexSetsToStreamsMigration.MigrationCompleted.create(indexSetConfig.id(), Sets.newHashSet("stream1"), Collections.emptySet()));
    verify(clusterEventBus, times(1)).post(StreamsChangedEvent.create(ImmutableSet.of("stream1")));
}
Also used : IndexSetConfig(org.graylog2.indexer.indexset.IndexSetConfig) Stream(org.graylog2.plugin.streams.Stream) Test(org.junit.Test)

Example 13 with IndexSetConfig

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

the class V20161122174500_AssignIndexSetsToStreamsMigrationTest method upgradeWithFailedStreamUpdate.

@Test
public void upgradeWithFailedStreamUpdate() throws Exception {
    final Stream stream1 = mock(Stream.class);
    final Stream stream2 = mock(Stream.class);
    final IndexSetConfig indexSetConfig = mock(IndexSetConfig.class);
    when(indexSetService.findAll()).thenReturn(Collections.singletonList(indexSetConfig));
    when(indexSetConfig.id()).thenReturn("abc123");
    when(stream1.getId()).thenReturn("stream1");
    when(stream2.getId()).thenReturn("stream2");
    when(streamService.loadAll()).thenReturn(Lists.newArrayList(stream1, stream2));
    // Updating stream1 should fail!
    when(streamService.save(stream1)).thenThrow(ValidationException.class);
    migration.upgrade();
    verify(stream1).setIndexSetId(indexSetConfig.id());
    verify(stream2).setIndexSetId(indexSetConfig.id());
    verify(streamService, times(1)).save(stream1);
    verify(streamService, times(1)).save(stream2);
    // Check that the failed stream1 will be recorded as failed!
    verify(clusterConfigService, times(1)).write(V20161122174500_AssignIndexSetsToStreamsMigration.MigrationCompleted.create(indexSetConfig.id(), Sets.newHashSet("stream2"), Sets.newHashSet("stream1")));
    verify(clusterEventBus, times(1)).post(StreamsChangedEvent.create(ImmutableSet.of("stream2")));
}
Also used : IndexSetConfig(org.graylog2.indexer.indexset.IndexSetConfig) Stream(org.graylog2.plugin.streams.Stream) Test(org.junit.Test)

Example 14 with IndexSetConfig

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

the class MongoIndexSetService method delete.

/**
     * {@inheritDoc}
     */
@Override
public int delete(ObjectId id) {
    if (!isDeletable(id)) {
        return 0;
    }
    final DBQuery.Query query = DBQuery.is("_id", id);
    final WriteResult<IndexSetConfig, ObjectId> writeResult = collection.remove(query);
    final int removedEntries = writeResult.getN();
    if (removedEntries > 0) {
        final IndexSetDeletedEvent deletedEvent = IndexSetDeletedEvent.create(id.toHexString());
        clusterEventBus.post(deletedEvent);
    }
    return removedEntries;
}
Also used : ObjectId(org.bson.types.ObjectId) DBQuery(org.mongojack.DBQuery) IndexSetDeletedEvent(org.graylog2.indexer.indexset.events.IndexSetDeletedEvent)

Example 15 with IndexSetConfig

use of org.graylog2.indexer.indexset.IndexSetConfig 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)

Aggregations

IndexSetConfig (org.graylog2.indexer.indexset.IndexSetConfig)53 Test (org.junit.Test)39 DefaultIndexSetConfig (org.graylog2.indexer.indexset.DefaultIndexSetConfig)21 IndexSet (org.graylog2.indexer.IndexSet)11 NoopRetentionStrategy (org.graylog2.indexer.retention.strategies.NoopRetentionStrategy)10 MessageCountRotationStrategy (org.graylog2.indexer.rotation.strategies.MessageCountRotationStrategy)10 RetentionStrategyConfig (org.graylog2.plugin.indexer.retention.RetentionStrategyConfig)8 Timed (com.codahale.metrics.annotation.Timed)5 ApiOperation (io.swagger.annotations.ApiOperation)5 ApiResponses (io.swagger.annotations.ApiResponses)5 AuditEvent (org.graylog2.audit.jersey.AuditEvent)5 IndexSetSummary (org.graylog2.rest.resources.system.indexer.responses.IndexSetSummary)5 Collectors (java.util.stream.Collectors)4 Inject (javax.inject.Inject)4 ClientErrorException (javax.ws.rs.ClientErrorException)4 NotFoundException (javax.ws.rs.NotFoundException)4 PUT (javax.ws.rs.PUT)4 Path (javax.ws.rs.Path)4 IndexSetService (org.graylog2.indexer.indexset.IndexSetService)4 IndexSetCleanupJob (org.graylog2.indexer.indices.jobs.IndexSetCleanupJob)4