Search in sources :

Example 11 with JobTriggerDto

use of org.graylog.scheduler.JobTriggerDto in project graylog2-server by Graylog2.

the class EventProcessorExecutionJobTest method executeWithOnceSchedule.

@Test
public void executeWithOnceSchedule() throws Exception {
    final DateTime now = clock.nowUTC();
    final long processingWindowSize = Duration.standardSeconds(60).getMillis();
    final long processingHopSize = Duration.standardSeconds(60).getMillis();
    final DateTime from = now.minus(processingWindowSize);
    final DateTime to = now;
    final DateTime triggerNextTime = now;
    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(// This job should only be triggered ONCE
    OnceJobSchedule.create()).build();
    final JobExecutionContext jobExecutionContext = JobExecutionContext.builder().definition(jobDefinition).trigger(trigger).isRunning(new AtomicBoolean(true)).jobTriggerUpdates(new JobTriggerUpdates(clock, jobScheduleStrategies, trigger)).build();
    final JobTriggerUpdate triggerUpdate = job.execute(jobExecutionContext);
    verify(eventProcessorEngine, times(1)).execute("processor-1", eventProcessorParameters);
    // No nextTime because this job was triggered ONCE
    assertThat(triggerUpdate.nextTime()).isNotPresent();
    assertThat(triggerUpdate.data()).isNotPresent();
    assertThat(triggerUpdate.status()).isNotPresent();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) JobDefinitionDto(org.graylog.scheduler.JobDefinitionDto) JobTriggerUpdates(org.graylog.scheduler.JobTriggerUpdates) JobTriggerUpdate(org.graylog.scheduler.JobTriggerUpdate) JobExecutionContext(org.graylog.scheduler.JobExecutionContext) DateTime(org.joda.time.DateTime) TestEventProcessorParameters(org.graylog.events.TestEventProcessorParameters) EventProcessorExecutionJob(org.graylog.events.processor.EventProcessorExecutionJob) JobTriggerDto(org.graylog.scheduler.JobTriggerDto) Test(org.junit.Test)

Example 12 with JobTriggerDto

use of org.graylog.scheduler.JobTriggerDto in project graylog2-server by Graylog2.

the class EventProcessorExecutionJobTest method executeWithHoppingWindow.

@Test
public void executeWithHoppingWindow() throws Exception {
    clock.plus(60, TimeUnit.SECONDS);
    final DateTime now = clock.nowUTC();
    // When using a hopping window, the window size is not the same as the hop size
    final long processingWindowSize = Duration.standardSeconds(60).getMillis();
    final long processingHopSize = Duration.standardSeconds(5).getMillis();
    final int scheduleIntervalSeconds = 5;
    final DateTime from = now.minus(processingWindowSize);
    final DateTime to = now;
    final DateTime triggerNextTime = now;
    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(7, TimeUnit.SECONDS);
        return null;
    }).when(eventProcessorEngine).execute(any(), any());
    final JobTriggerUpdate triggerUpdate = job.execute(jobExecutionContext);
    verify(eventProcessorEngine, times(1)).execute("processor-1", eventProcessorParameters);
    // The next time should be based on the previous nextTime + the schedule. The 7 second event processor
    // runtime should not be added to the new nextTime.
    assertThat(triggerUpdate.nextTime()).isPresent().get().isEqualTo(triggerNextTime.plusSeconds(scheduleIntervalSeconds));
    // 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();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) JobDefinitionDto(org.graylog.scheduler.JobDefinitionDto) JobTriggerUpdates(org.graylog.scheduler.JobTriggerUpdates) JobTriggerUpdate(org.graylog.scheduler.JobTriggerUpdate) JobExecutionContext(org.graylog.scheduler.JobExecutionContext) DateTime(org.joda.time.DateTime) TestEventProcessorParameters(org.graylog.events.TestEventProcessorParameters) EventProcessorExecutionJob(org.graylog.events.processor.EventProcessorExecutionJob) JobTriggerDto(org.graylog.scheduler.JobTriggerDto) Test(org.junit.Test)

Example 13 with JobTriggerDto

use of org.graylog.scheduler.JobTriggerDto in project graylog2-server by Graylog2.

the class EventProcessorExecutionJobTest method execute.

@Test
public void execute() throws Exception {
    final DateTime now = clock.nowUTC();
    final long processingWindowSize = Duration.standardSeconds(60).getMillis();
    final long processingHopSize = Duration.standardSeconds(60).getMillis();
    final int scheduleIntervalSeconds = 1;
    final DateTime from = now.minus(processingWindowSize);
    final DateTime to = now;
    final DateTime triggerNextTime = now;
    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();
    final JobTriggerUpdate triggerUpdate = job.execute(jobExecutionContext);
    verify(eventProcessorEngine, times(1)).execute("processor-1", eventProcessorParameters);
    assertThat(triggerUpdate.nextTime()).isPresent().get().isEqualTo(triggerNextTime.plusSeconds(scheduleIntervalSeconds));
    assertThat(triggerUpdate.data()).isPresent().get().isEqualTo(EventProcessorExecutionJob.Data.builder().timerangeFrom(to).timerangeTo(to.plus(processingWindowSize)).build());
    assertThat(triggerUpdate.status()).isNotPresent();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) JobDefinitionDto(org.graylog.scheduler.JobDefinitionDto) JobTriggerUpdates(org.graylog.scheduler.JobTriggerUpdates) JobTriggerUpdate(org.graylog.scheduler.JobTriggerUpdate) JobExecutionContext(org.graylog.scheduler.JobExecutionContext) DateTime(org.joda.time.DateTime) TestEventProcessorParameters(org.graylog.events.TestEventProcessorParameters) EventProcessorExecutionJob(org.graylog.events.processor.EventProcessorExecutionJob) JobTriggerDto(org.graylog.scheduler.JobTriggerDto) Test(org.junit.Test)

Example 14 with JobTriggerDto

use of org.graylog.scheduler.JobTriggerDto in project graylog2-server by Graylog2.

the class EventDefinitionHandlerTest method create.

@Test
public void create() {
    final EventDefinitionDto newDto = EventDefinitionDto.builder().title("Test").description("A test event definition").config(TestEventProcessorConfig.builder().message("This is a test event processor").searchWithinMs(300000).executeEveryMs(60001).build()).priority(3).alert(false).notificationSettings(EventNotificationSettings.withGracePeriod(60000)).keySpec(ImmutableList.of("a", "b")).notifications(ImmutableList.of()).build();
    final EventDefinitionDto dto = handler.create(newDto, Optional.empty());
    // Handler should create the event definition
    assertThat(eventDefinitionService.get(dto.id())).isPresent();
    final Optional<JobDefinitionDto> jobDefinition = jobDefinitionService.getByConfigField("event_definition_id", dto.id());
    // Handler also should create the job definition for the event definition/processor
    assertThat(jobDefinition).isPresent().get().satisfies(definition -> {
        assertThat(definition.title()).isEqualTo("Test");
        assertThat(definition.description()).isEqualTo("A test event definition");
        assertThat(definition.config()).isInstanceOf(EventProcessorExecutionJob.Config.class);
        final EventProcessorExecutionJob.Config config = (EventProcessorExecutionJob.Config) definition.config();
        assertThat(config.processingWindowSize()).isEqualTo(300000);
        assertThat(config.processingHopSize()).isEqualTo(60001);
    });
    // And the handler should also create a job trigger for the created job definition
    final Optional<JobTriggerDto> jobTrigger = jobTriggerService.nextRunnableTrigger();
    assertThat(jobTrigger).isPresent().get().satisfies(trigger -> {
        assertThat(trigger.jobDefinitionId()).isEqualTo(jobDefinition.get().id());
        assertThat(trigger.schedule()).isInstanceOf(IntervalJobSchedule.class);
        final IntervalJobSchedule schedule = (IntervalJobSchedule) trigger.schedule();
        assertThat(schedule.interval()).isEqualTo(60001);
        assertThat(schedule.unit()).isEqualTo(TimeUnit.MILLISECONDS);
    });
}
Also used : JobDefinitionDto(org.graylog.scheduler.JobDefinitionDto) TestEventProcessorConfig(org.graylog.events.TestEventProcessorConfig) IntervalJobSchedule(org.graylog.scheduler.schedule.IntervalJobSchedule) JobTriggerDto(org.graylog.scheduler.JobTriggerDto) Test(org.junit.Test)

Example 15 with JobTriggerDto

use of org.graylog.scheduler.JobTriggerDto in project graylog2-server by Graylog2.

the class EventDefinitionHandlerTest method update.

@Test
@MongoDBFixtures("event-processors.json")
public void update() {
    final String newTitle = "A NEW TITLE " + DateTime.now(DateTimeZone.UTC).toString();
    final String newDescription = "A NEW DESCRIPTION " + DateTime.now(DateTimeZone.UTC).toString();
    final EventDefinitionDto existingDto = eventDefinitionService.get("54e3deadbeefdeadbeef0000").orElse(null);
    final JobDefinitionDto existingJobDefinition = jobDefinitionService.get("54e3deadbeefdeadbeef0001").orElse(null);
    final JobTriggerDto existingTrigger = jobTriggerService.get("54e3deadbeefdeadbeef0002").orElse(null);
    final TestEventProcessorConfig existingConfig = (TestEventProcessorConfig) existingDto.config();
    final TestEventProcessorConfig newConfig = existingConfig.toBuilder().executeEveryMs(550000).searchWithinMs(800000).build();
    final EventProcessorExecutionJob.Data existingTriggerData = (EventProcessorExecutionJob.Data) existingTrigger.data().orElseThrow(AssertionError::new);
    assertThat(existingDto).isNotNull();
    assertThat(existingJobDefinition).isNotNull();
    assertThat(existingTrigger).isNotNull();
    final EventDefinitionDto updatedDto = existingDto.toBuilder().title(newTitle).description(newDescription).config(newConfig).build();
    assertThat(handler.update(updatedDto, true)).isNotEqualTo(existingDto);
    assertThat(eventDefinitionService.get(existingDto.id())).isPresent().get().satisfies(dto -> {
        assertThat(dto.id()).isEqualTo(existingDto.id());
        assertThat(dto.title()).isEqualTo(newTitle);
        assertThat(dto.description()).isEqualTo(newDescription);
    });
    // Test that the schedule is updated to the new config
    final JobDefinitionDto newJobDefinition = jobDefinitionService.get("54e3deadbeefdeadbeef0001").orElseThrow(AssertionError::new);
    assertThat(newJobDefinition.title()).isEqualTo(newTitle);
    assertThat(newJobDefinition.description()).isEqualTo(newDescription);
    assertThat(((EventProcessorExecutionJob.Config) newJobDefinition.config()).processingHopSize()).isEqualTo(550000);
    assertThat(((EventProcessorExecutionJob.Config) newJobDefinition.config()).processingWindowSize()).isEqualTo(800000);
    // Test if the EventDefinition update removed the old trigger data
    // and reset the job definition timerange to the new parameters
    final EventProcessorExecutionJob.Config newJobConfig = (EventProcessorExecutionJob.Config) newJobDefinition.config();
    final TimeRange newTimeRange = newJobConfig.parameters().timerange();
    assertThat(newTimeRange.getFrom()).isEqualTo(clock.nowUTC().minus(newConfig.searchWithinMs()));
    assertThat(newTimeRange.getTo()).isEqualTo(clock.nowUTC());
    assertThat(jobTriggerService.get("54e3deadbeefdeadbeef0002")).isPresent().get().satisfies(trigger -> {
        assertThat(trigger.data()).isEmpty();
        assertThat(trigger.nextTime()).isEqualTo(clock.nowUTC());
    });
}
Also used : TestEventProcessorConfig(org.graylog.events.TestEventProcessorConfig) TimeRange(org.graylog2.plugin.indexer.searches.timeranges.TimeRange) JobDefinitionDto(org.graylog.scheduler.JobDefinitionDto) TestEventProcessorConfig(org.graylog.events.TestEventProcessorConfig) JobTriggerDto(org.graylog.scheduler.JobTriggerDto) MongoDBFixtures(org.graylog.testing.mongodb.MongoDBFixtures) Test(org.junit.Test)

Aggregations

JobTriggerDto (org.graylog.scheduler.JobTriggerDto)19 JobDefinitionDto (org.graylog.scheduler.JobDefinitionDto)15 Test (org.junit.Test)12 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)8 TestEventProcessorParameters (org.graylog.events.TestEventProcessorParameters)8 EventProcessorExecutionJob (org.graylog.events.processor.EventProcessorExecutionJob)8 JobExecutionContext (org.graylog.scheduler.JobExecutionContext)8 JobTriggerUpdates (org.graylog.scheduler.JobTriggerUpdates)8 DateTime (org.joda.time.DateTime)8 JobTriggerUpdate (org.graylog.scheduler.JobTriggerUpdate)7 TestEventProcessorConfig (org.graylog.events.TestEventProcessorConfig)3 EventDefinitionDto (org.graylog.events.processor.EventDefinitionDto)3 MongoDBFixtures (org.graylog.testing.mongodb.MongoDBFixtures)3 JobExecutionException (org.graylog.scheduler.JobExecutionException)2 NotFoundException (org.graylog2.database.NotFoundException)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 List (java.util.List)1 Event (org.graylog.events.event.Event)1 EventDto (org.graylog.events.event.EventDto)1 EventWithContext (org.graylog.events.event.EventWithContext)1