use of org.graylog2.plugin.InstantMillisProvider in project graylog2-server by Graylog2.
the class TimeBasedRotationStrategyTest method shouldRotateNonIntegralPeriod.
@Test
public void shouldRotateNonIntegralPeriod() throws Exception {
// start 5 minutes before full hour
final DateTime initialTime = new DateTime(2014, 1, 1, 1, 55, 0, 0, DateTimeZone.UTC);
final Period period = minutes(10);
final InstantMillisProvider clock = new InstantMillisProvider(initialTime);
DateTimeUtils.setCurrentMillisProvider(clock);
when(indexSet.getConfig()).thenReturn(indexSetConfig);
when(indexSetConfig.rotationStrategy()).thenReturn(TimeBasedRotationStrategyConfig.create(period));
when(indices.indexCreationDate(anyString())).thenReturn(initialTime.minus(minutes(11)));
// Should rotate the first index.
// time is 01:55:00, index was created at 01:44:00, so we missed one period, and should rotate
when(indexSet.getNewestIndex()).thenReturn("ignored");
when(indexSet.getConfig()).thenReturn(indexSetConfig);
when(indexSetConfig.rotationStrategy()).thenReturn(TimeBasedRotationStrategyConfig.create(period));
rotationStrategy.rotate(indexSet);
verify(indexSet, times(1)).cycle();
reset(indexSet);
// advance time to 01:55:01
clock.tick(seconds(1));
// Did not cross rotation period.
when(indexSet.getNewestIndex()).thenReturn("ignored");
when(indexSet.getConfig()).thenReturn(indexSetConfig);
when(indexSetConfig.rotationStrategy()).thenReturn(TimeBasedRotationStrategyConfig.create(period));
rotationStrategy.rotate(indexSet);
verify(indexSet, never()).cycle();
reset(indexSet);
// advance time to 02:00:00
clock.tick(minutes(4).withSeconds(59));
// Crossed rotation period.
when(indexSet.getNewestIndex()).thenReturn("ignored");
when(indexSet.getConfig()).thenReturn(indexSetConfig);
when(indexSetConfig.rotationStrategy()).thenReturn(TimeBasedRotationStrategyConfig.create(period));
rotationStrategy.rotate(indexSet);
verify(indexSet, times(1)).cycle();
reset(indexSet);
// advance time multiple rotation periods into the future
// to time 02:51:00
clock.tick(minutes(51));
// Crossed multiple rotation periods.
when(indexSet.getNewestIndex()).thenReturn("ignored");
when(indexSet.getConfig()).thenReturn(indexSetConfig);
when(indexSetConfig.rotationStrategy()).thenReturn(TimeBasedRotationStrategyConfig.create(period));
rotationStrategy.rotate(indexSet);
verify(indexSet, times(1)).cycle();
reset(indexSet);
// move time to 2:52:00
// this should not cycle again, because next valid rotation time is 3:00:00
clock.tick(minutes(1));
when(indexSet.getNewestIndex()).thenReturn("ignored");
when(indexSet.getConfig()).thenReturn(indexSetConfig);
when(indexSetConfig.rotationStrategy()).thenReturn(TimeBasedRotationStrategyConfig.create(period));
rotationStrategy.rotate(indexSet);
verify(indexSet, never()).cycle();
reset(indexSet);
}
use of org.graylog2.plugin.InstantMillisProvider in project graylog2-server by Graylog2.
the class TimeBasedRotationStrategyTest method shouldRotateHourly.
@Test
public void shouldRotateHourly() throws Exception {
final DateTime initialTime = new DateTime(2014, 1, 1, 1, 59, 59, 0, DateTimeZone.UTC);
final Period period = Period.hours(1);
final InstantMillisProvider clock = new InstantMillisProvider(initialTime);
DateTimeUtils.setCurrentMillisProvider(clock);
when(indexSet.getConfig()).thenReturn(indexSetConfig);
when(indexSetConfig.rotationStrategy()).thenReturn(TimeBasedRotationStrategyConfig.create(period));
when(indices.indexCreationDate(anyString())).thenReturn(initialTime.minus(minutes(5)));
// Should not rotate the first index.
when(indexSet.getNewestIndex()).thenReturn("ignored");
rotationStrategy.rotate(indexSet);
verify(indexSet, never()).cycle();
reset(indexSet);
clock.tick(seconds(2));
// Crossed rotation period.
when(indexSet.getNewestIndex()).thenReturn("ignored");
when(indexSet.getConfig()).thenReturn(indexSetConfig);
when(indexSetConfig.rotationStrategy()).thenReturn(TimeBasedRotationStrategyConfig.create(period));
rotationStrategy.rotate(indexSet);
verify(indexSet, times(1)).cycle();
reset(indexSet);
clock.tick(seconds(2));
// Did not cross rotation period.
when(indexSet.getNewestIndex()).thenReturn("ignored");
when(indexSet.getConfig()).thenReturn(indexSetConfig);
when(indexSetConfig.rotationStrategy()).thenReturn(TimeBasedRotationStrategyConfig.create(period));
rotationStrategy.rotate(indexSet);
verify(indexSet, never()).cycle();
reset(indexSet);
}
use of org.graylog2.plugin.InstantMillisProvider in project graylog2-server by Graylog2.
the class GelfChunkAggregatorTest method missingChunk.
@Test
public void missingChunk() {
final DateTime initialTime = new DateTime(2014, 1, 1, 1, 59, 59, 0, DateTimeZone.UTC);
final InstantMillisProvider clock = new InstantMillisProvider(initialTime);
DateTimeUtils.setCurrentMillisProvider(clock);
// we don't want the clean up task to run automatically
poolExecutor = mock(ScheduledThreadPoolExecutor.class);
final MetricRegistry metricRegistry = new MetricRegistry();
aggregator = new GelfChunkAggregator(poolExecutor, metricRegistry);
final GelfChunkAggregator.ChunkEvictionTask evictionTask = aggregator.new ChunkEvictionTask();
// creates 5 chunks
final ChannelBuffer[] chunks = createChunkedMessage(4096 + 512, 1024);
int i = 0;
for (final ChannelBuffer chunk : chunks) {
final CodecAggregator.Result result;
// skip first chunk
if (i++ == 0) {
continue;
}
result = aggregator.addChunk(chunk);
assertTrue(result.isValid());
assertNull("chunks not complete", result.getMessage());
}
// move clock forward enough to evict all of the chunks
clock.tick(Period.seconds(10));
evictionTask.run();
final CodecAggregator.Result result = aggregator.addChunk(chunks[0]);
assertNull("message should not be complete because chunks were evicted already", result.getMessage());
assertTrue(result.isValid());
// we send all chunks but the last one comes too late
assertEquals("no message is complete", 0, counterValueNamed(metricRegistry, COMPLETE_MESSAGES));
assertEquals("received 5 chunks", 5, counterValueNamed(metricRegistry, CHUNK_COUNTER));
assertEquals("last chunk creates another waiting message", 1, counterValueNamed(metricRegistry, WAITING_MESSAGES));
assertEquals("4 chunks expired", 4, counterValueNamed(metricRegistry, EXPIRED_CHUNKS));
assertEquals("one message expired", 1, counterValueNamed(metricRegistry, EXPIRED_MESSAGES));
assertEquals("no duplicate chunks", 0, counterValueNamed(metricRegistry, DUPLICATE_CHUNKS));
// reset clock for other tests
DateTimeUtils.setCurrentMillisSystem();
}
use of org.graylog2.plugin.InstantMillisProvider in project graylog2-server by Graylog2.
the class TimeBasedRotationStrategyTest method determineAnchor.
@Test
public void determineAnchor() {
final DateTime initialTime = new DateTime(2014, 3, 15, 14, 48, 35, 0, DateTimeZone.UTC);
final InstantMillisProvider clock = new InstantMillisProvider(initialTime);
DateTimeUtils.setCurrentMillisProvider(clock);
Period period;
// should snap to 14:00:00
period = Period.hours(1);
final DateTime hourAnchor = TimeBasedRotationStrategy.determineRotationPeriodAnchor(null, period);
assertEquals(hourAnchor.getHourOfDay(), 14);
assertEquals(hourAnchor.getMinuteOfHour(), 0);
assertEquals(hourAnchor.getSecondOfMinute(), 0);
// should snap to 14:45:00
period = minutes(5);
final DateTime fiveMins = TimeBasedRotationStrategy.determineRotationPeriodAnchor(null, period);
assertEquals(fiveMins.getHourOfDay(), 14);
assertEquals(fiveMins.getMinuteOfHour(), 45);
assertEquals(fiveMins.getSecondOfMinute(), 0);
// should snap to 2014-3-15 00:00:00
period = Period.days(1).withHours(6);
final DateTime dayAnd6Hours = TimeBasedRotationStrategy.determineRotationPeriodAnchor(null, period);
assertEquals(dayAnd6Hours.getYear(), 2014);
assertEquals(dayAnd6Hours.getMonthOfYear(), 3);
assertEquals(dayAnd6Hours.getDayOfMonth(), 15);
assertEquals(dayAnd6Hours.getHourOfDay(), 0);
assertEquals(dayAnd6Hours.getMinuteOfHour(), 0);
assertEquals(dayAnd6Hours.getSecondOfMinute(), 0);
period = Period.days(30);
final DateTime thirtyDays = TimeBasedRotationStrategy.determineRotationPeriodAnchor(null, period);
assertEquals(thirtyDays.getYear(), 2014);
assertEquals(thirtyDays.getMonthOfYear(), 2);
assertEquals(thirtyDays.getDayOfMonth(), 17);
assertEquals(thirtyDays.getHourOfDay(), 0);
assertEquals(thirtyDays.getMinuteOfHour(), 0);
assertEquals(thirtyDays.getSecondOfMinute(), 0);
period = Period.hours(1);
final DateTime diffAnchor = TimeBasedRotationStrategy.determineRotationPeriodAnchor(initialTime.minusMinutes(61), period);
assertEquals(diffAnchor.getYear(), 2014);
assertEquals(diffAnchor.getMonthOfYear(), 3);
assertEquals(diffAnchor.getDayOfMonth(), 15);
assertEquals(diffAnchor.getHourOfDay(), 13);
assertEquals(diffAnchor.getMinuteOfHour(), 0);
assertEquals(diffAnchor.getSecondOfMinute(), 0);
}
use of org.graylog2.plugin.InstantMillisProvider in project graylog2-server by Graylog2.
the class TimeBasedRotationStrategyTest method shouldRotateConcurrently.
@Test
public void shouldRotateConcurrently() throws Exception {
final DateTime initialTime = new DateTime(2014, 1, 1, 1, 59, 59, 0, DateTimeZone.UTC);
final Period period = Period.hours(1);
final InstantMillisProvider clock = new InstantMillisProvider(initialTime);
DateTimeUtils.setCurrentMillisProvider(clock);
final IndexSet indexSet1 = mock(IndexSet.class);
final IndexSet indexSet2 = mock(IndexSet.class);
final IndexSetConfig indexSetConfig1 = mock(IndexSetConfig.class);
final IndexSetConfig indexSetConfig2 = mock(IndexSetConfig.class);
when(indexSetConfig1.id()).thenReturn("id1");
when(indexSetConfig2.id()).thenReturn("id2");
when(indexSet1.getConfig()).thenReturn(indexSetConfig1);
when(indexSet2.getConfig()).thenReturn(indexSetConfig2);
when(indexSetConfig1.rotationStrategy()).thenReturn(TimeBasedRotationStrategyConfig.create(period));
when(indexSetConfig2.rotationStrategy()).thenReturn(TimeBasedRotationStrategyConfig.create(period));
when(indices.indexCreationDate(anyString())).thenReturn(initialTime.minus(minutes(5)));
// Should not rotate the initial index.
when(indexSet1.getNewestIndex()).thenReturn("index1");
rotationStrategy.rotate(indexSet1);
verify(indexSet1, never()).cycle();
reset(indexSet1);
when(indexSet2.getNewestIndex()).thenReturn("index2");
rotationStrategy.rotate(indexSet2);
verify(indexSet2, never()).cycle();
reset(indexSet2);
clock.tick(seconds(2));
// Crossed rotation period.
when(indexSet1.getNewestIndex()).thenReturn("index1");
when(indexSet1.getConfig()).thenReturn(indexSetConfig1);
when(indexSetConfig1.rotationStrategy()).thenReturn(TimeBasedRotationStrategyConfig.create(period));
rotationStrategy.rotate(indexSet1);
verify(indexSet1, times(1)).cycle();
reset(indexSet1);
when(indexSet2.getNewestIndex()).thenReturn("index2");
when(indexSet2.getConfig()).thenReturn(indexSetConfig2);
when(indexSetConfig2.rotationStrategy()).thenReturn(TimeBasedRotationStrategyConfig.create(period));
rotationStrategy.rotate(indexSet2);
verify(indexSet2, times(1)).cycle();
reset(indexSet2);
clock.tick(seconds(2));
// Did not cross rotation period.
when(indexSet1.getNewestIndex()).thenReturn("index1");
when(indexSet1.getConfig()).thenReturn(indexSetConfig1);
when(indexSetConfig1.rotationStrategy()).thenReturn(TimeBasedRotationStrategyConfig.create(period));
rotationStrategy.rotate(indexSet1);
verify(indexSet1, never()).cycle();
reset(indexSet1);
when(indexSet2.getNewestIndex()).thenReturn("index2");
when(indexSet2.getConfig()).thenReturn(indexSetConfig2);
when(indexSetConfig2.rotationStrategy()).thenReturn(TimeBasedRotationStrategyConfig.create(period));
rotationStrategy.rotate(indexSet2);
verify(indexSet2, never()).cycle();
reset(indexSet2);
}
Aggregations