use of org.joda.time.Duration in project graylog2-server by Graylog2.
the class FunctionsSnippetsTest method dateArithmetic.
@Test
public void dateArithmetic() {
final InstantMillisProvider clock = new InstantMillisProvider(GRAYLOG_EPOCH);
DateTimeUtils.setCurrentMillisProvider(clock);
try {
final Rule rule = parser.parseRule(ruleForTest(), true);
final Message message = evaluateRule(rule);
assertThat(actionsTriggered.get()).isTrue();
assertThat(message).isNotNull();
assertThat(message.getField("interval")).isInstanceOf(Duration.class).matches(o -> ((Duration) o).isEqual(Duration.standardDays(1)), "Exactly one day difference");
assertThat(message.getField("years")).isEqualTo(Period.years(2));
assertThat(message.getField("months")).isEqualTo(Period.months(2));
assertThat(message.getField("weeks")).isEqualTo(Period.weeks(2));
assertThat(message.getField("days")).isEqualTo(Period.days(2));
assertThat(message.getField("hours")).isEqualTo(Period.hours(2));
assertThat(message.getField("minutes")).isEqualTo(Period.minutes(2));
assertThat(message.getField("seconds")).isEqualTo(Period.seconds(2));
assertThat(message.getField("millis")).isEqualTo(Period.millis(2));
assertThat(message.getField("period")).isEqualTo(Period.parse("P1YT1M"));
assertThat(message.getFieldAs(DateTime.class, "long_time_ago")).matches(date -> date.plus(Period.years(10000)).equals(GRAYLOG_EPOCH));
assertThat(message.getTimestamp()).isEqualTo(GRAYLOG_EPOCH.plusHours(1));
} finally {
DateTimeUtils.setCurrentMillisSystem();
}
}
use of org.joda.time.Duration in project graylog2-server by Graylog2.
the class EventProcessorExecutionJobTest method catchupWindowTestHelper.
private void catchupWindowTestHelper(long catchUpWindowSize, long processingHopSize, long processingWindowSize) throws Exception {
when(eventsConfigurationProvider.get()).thenReturn(EventsConfiguration.builder().eventCatchupWindow(catchUpWindowSize).build());
// for easier testing. don't run into the previous day
clock.plus(1, TimeUnit.MINUTES);
final DateTime now = clock.nowUTC();
final long processingCatchUpWindowSize = eventsConfigurationProvider.get().eventCatchupWindow();
final int scheduleIntervalSeconds = (int) processingHopSize * 1000;
final DateTime from = now.minus(processingWindowSize);
final DateTime to = now;
final DateTime triggerNextTime = now;
final Duration timeSpentInEventProcessor = Duration.standardSeconds(7);
final TestEventProcessorParameters eventProcessorParameters = TestEventProcessorParameters.create(from, to);
final JobDefinitionDto jobDefinition = JobDefinitionDto.builder().id("job-1").title("Test").description("A test").config(EventProcessorExecutionJob.Config.builder().eventDefinitionId("processor-1").processingWindowSize(processingWindowSize).processingHopSize(processingHopSize).parameters(eventProcessorParameters).build()).build();
final EventProcessorExecutionJob job = new EventProcessorExecutionJob(jobScheduleStrategies, clock, eventProcessorEngine, eventsConfigurationProvider, jobDefinition);
final JobTriggerDto trigger = JobTriggerDto.builderWithClock(clock).id("trigger-1").jobDefinitionId(jobDefinition.id()).startTime(now).nextTime(triggerNextTime).status(JobTriggerStatus.RUNNABLE).schedule(IntervalJobSchedule.builder().interval(scheduleIntervalSeconds).unit(TimeUnit.SECONDS).build()).build();
final JobExecutionContext jobExecutionContext = JobExecutionContext.builder().definition(jobDefinition).trigger(trigger).isRunning(new AtomicBoolean(true)).jobTriggerUpdates(new JobTriggerUpdates(clock, jobScheduleStrategies, trigger)).build();
doAnswer(invocation -> {
// Simulate work in the event processor
clock.plus(timeSpentInEventProcessor.getStandardSeconds(), TimeUnit.SECONDS);
return null;
}).when(eventProcessorEngine).execute(any(), any());
// Simulate that we are behind at least one `processingCatchUpWindowSize`
clock.plus(EventsConfiguration.DEFAULT_CATCH_UP_WINDOW_MS, TimeUnit.MILLISECONDS);
clock.plus(1, TimeUnit.MILLISECONDS);
final JobTriggerUpdate triggerUpdate = job.execute(jobExecutionContext);
verify(eventProcessorEngine, times(1)).execute("processor-1", eventProcessorParameters);
assertThat(triggerUpdate.nextTime()).isPresent().get().isEqualTo(clock.nowUTC().minus(timeSpentInEventProcessor));
if (catchUpWindowSize > processingWindowSize && processingHopSize <= processingWindowSize) {
// We are behind at least one chunk of catchUpWindowSize
// The new nextFrom should ignore the processingHopSize and start 1ms after the last `To` Range
// The nextTo will be one window of the processingCatchUpWindowSize
assertThat(triggerUpdate.data()).isPresent().get().isEqualTo(EventProcessorExecutionJob.Data.builder().timerangeFrom(to.plus(processingHopSize).minus(processingWindowSize)).timerangeTo(to.plus(processingCatchUpWindowSize)).build());
} else {
// If no catchup is in effect, we fall back into the configured hopping window mode.
// With a hopping window the "to" is calculated by adding the hopSize and the "from" is based on the next "to"
// minus the windowSize + 1 millisecond.
assertThat(triggerUpdate.data()).isPresent().get().isEqualTo(EventProcessorExecutionJob.Data.builder().timerangeFrom(to.plus(processingHopSize).minus(processingWindowSize)).timerangeTo(to.plus(processingHopSize)).build());
}
assertThat(triggerUpdate.status()).isNotPresent();
}
use of org.joda.time.Duration in project graylog2-server by Graylog2.
the class LookupDataAdapterRefreshService method add.
/**
* Add the given {@link LookupDataAdapter} to the refresh service.
* <p>
* The {@link LookupDataAdapter#doRefresh(LookupCachePurge) refresh method} method will be called periodically
* according to the {@link LookupDataAdapter#refreshInterval() refresh interval} of the data adapter.
* @param dataAdapter the adapter to be added
*/
public void add(LookupDataAdapter dataAdapter) {
if (state() == State.STOPPING || state() == State.TERMINATED) {
LOG.debug("Service is in state <{}> - not adding new job for <{}/{}/@{}>", state(), dataAdapter.name(), dataAdapter.id(), objectId(dataAdapter));
return;
}
final Duration interval = dataAdapter.refreshInterval();
// No need to schedule the data adapter refresh if it doesn't implement a refresh
if (!interval.equals(Duration.ZERO)) {
// Using the adapter object ID here to make it possible to have multiple jobs for the same adapter
final String instanceId = objectId(dataAdapter);
// ConcurrentMap#computeIfAbsent() does not work here because scheduling a job is not idempotent.
synchronized (futures) {
if (!futures.containsKey(instanceId)) {
LOG.info("Adding job for <{}/{}/@{}> [interval={}ms]", dataAdapter.name(), dataAdapter.id(), instanceId, interval.getMillis());
futures.put(instanceId, schedule(dataAdapter, interval));
} else {
LOG.warn("Job for <{}/{}/@{}> already exists, not adding it again.", dataAdapter.name(), dataAdapter.id(), instanceId);
}
}
}
}
use of org.joda.time.Duration in project graylog2-server by Graylog2.
the class IndexSetValidatorTest method validate.
@Test
public void validate() throws Exception {
final String prefix = "graylog_index";
final Duration fieldTypeRefreshInterval = Duration.standardSeconds(1L);
final IndexSetConfig newConfig = mock(IndexSetConfig.class);
final IndexSet indexSet = mock(IndexSet.class);
when(indexSet.getIndexPrefix()).thenReturn("foo");
when(indexSetRegistry.iterator()).thenReturn(Collections.singleton(indexSet).iterator());
when(newConfig.indexPrefix()).thenReturn(prefix);
when(newConfig.fieldTypeRefreshInterval()).thenReturn(fieldTypeRefreshInterval);
final Optional<IndexSetValidator.Violation> violation = validator.validate(newConfig);
assertThat(violation).isNotPresent();
}
use of org.joda.time.Duration in project graylog2-server by Graylog2.
the class IndexSetValidatorTest method validateWithInvalidFieldTypeRefreshInterval.
@Test
public void validateWithInvalidFieldTypeRefreshInterval() throws Exception {
final Duration fieldTypeRefreshInterval = Duration.millis(999);
final IndexSetConfig newConfig = mock(IndexSetConfig.class);
final IndexSet indexSet = mock(IndexSet.class);
when(indexSetRegistry.iterator()).thenReturn(Collections.singleton(indexSet).iterator());
when(indexSet.getIndexPrefix()).thenReturn("foo");
when(newConfig.indexPrefix()).thenReturn("graylog_index");
when(newConfig.fieldTypeRefreshInterval()).thenReturn(fieldTypeRefreshInterval);
final Optional<IndexSetValidator.Violation> violation = validator.validate(newConfig);
assertThat(violation).isPresent();
}
Aggregations