Search in sources :

Example 1 with Schedule

use of org.opensearch.jobscheduler.spi.schedule.Schedule in project anomaly-detection by opensearch-project.

the class AnomalyDetectorJob method parse.

public static AnomalyDetectorJob parse(XContentParser parser) throws IOException {
    String name = null;
    Schedule schedule = null;
    TimeConfiguration windowDelay = null;
    // we cannot set it to null as isEnabled() would do the unboxing and results in null pointer exception
    Boolean isEnabled = Boolean.FALSE;
    Instant enabledTime = null;
    Instant disabledTime = null;
    Instant lastUpdateTime = null;
    Long lockDurationSeconds = DEFAULT_AD_JOB_LOC_DURATION_SECONDS;
    User user = null;
    String resultIndex = null;
    ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.currentToken(), parser);
    while (parser.nextToken() != XContentParser.Token.END_OBJECT) {
        String fieldName = parser.currentName();
        parser.nextToken();
        switch(fieldName) {
            case NAME_FIELD:
                name = parser.text();
                break;
            case SCHEDULE_FIELD:
                schedule = ScheduleParser.parse(parser);
                break;
            case WINDOW_DELAY_FIELD:
                windowDelay = TimeConfiguration.parse(parser);
                break;
            case IS_ENABLED_FIELD:
                isEnabled = parser.booleanValue();
                break;
            case ENABLED_TIME_FIELD:
                enabledTime = ParseUtils.toInstant(parser);
                break;
            case DISABLED_TIME_FIELD:
                disabledTime = ParseUtils.toInstant(parser);
                break;
            case LAST_UPDATE_TIME_FIELD:
                lastUpdateTime = ParseUtils.toInstant(parser);
                break;
            case LOCK_DURATION_SECONDS:
                lockDurationSeconds = parser.longValue();
                break;
            case USER_FIELD:
                user = User.parse(parser);
                break;
            case RESULT_INDEX_FIELD:
                resultIndex = parser.text();
                break;
            default:
                parser.skipChildren();
                break;
        }
    }
    return new AnomalyDetectorJob(name, schedule, windowDelay, isEnabled, enabledTime, disabledTime, lastUpdateTime, lockDurationSeconds, user, resultIndex);
}
Also used : User(org.opensearch.commons.authuser.User) Schedule(org.opensearch.jobscheduler.spi.schedule.Schedule) CronSchedule(org.opensearch.jobscheduler.spi.schedule.CronSchedule) IntervalSchedule(org.opensearch.jobscheduler.spi.schedule.IntervalSchedule) Instant(java.time.Instant)

Example 2 with Schedule

use of org.opensearch.jobscheduler.spi.schedule.Schedule in project anomaly-detection by opensearch-project.

the class AnomalyDetectorJobRunnerTests method testRunAdJobWithEndRunExceptionNotNowAndRetryUntilStop.

@Test
public void testRunAdJobWithEndRunExceptionNotNowAndRetryUntilStop() throws InterruptedException {
    LockModel lock = new LockModel(AnomalyDetectorJob.ANOMALY_DETECTOR_JOB_INDEX, jobParameter.getName(), Instant.now(), 10, false);
    Instant executionStartTime = Instant.now();
    Schedule schedule = mock(IntervalSchedule.class);
    when(jobParameter.getSchedule()).thenReturn(schedule);
    when(schedule.getNextExecutionTime(executionStartTime)).thenReturn(executionStartTime.plusSeconds(5));
    doAnswer(invocation -> {
        Exception exception = new EndRunException(jobParameter.getName(), randomAlphaOfLength(5), false);
        ActionListener<?> listener = invocation.getArgument(2);
        listener.onFailure(exception);
        return null;
    }).when(client).execute(any(), any(), any());
    for (int i = 0; i < 3; i++) {
        runner.runAdJob(jobParameter, lockService, lock, Instant.now().minusSeconds(60), executionStartTime);
        assertEquals(i + 1, testAppender.countMessage("EndRunException happened for"));
    }
    runner.runAdJob(jobParameter, lockService, lock, Instant.now().minusSeconds(60), executionStartTime);
    assertEquals(1, testAppender.countMessage("JobRunner will stop AD job due to EndRunException retry exceeds upper limit"));
}
Also used : EndRunException(org.opensearch.ad.common.exception.EndRunException) Instant(java.time.Instant) IntervalSchedule(org.opensearch.jobscheduler.spi.schedule.IntervalSchedule) Schedule(org.opensearch.jobscheduler.spi.schedule.Schedule) LockModel(org.opensearch.jobscheduler.spi.LockModel) ExpectedException(org.junit.rules.ExpectedException) EndRunException(org.opensearch.ad.common.exception.EndRunException) Test(org.junit.Test)

Example 3 with Schedule

use of org.opensearch.jobscheduler.spi.schedule.Schedule in project anomaly-detection by opensearch-project.

the class IndexAnomalyDetectorJobActionHandler method createJob.

private void createJob(AnomalyDetector detector) {
    try {
        IntervalTimeConfiguration interval = (IntervalTimeConfiguration) detector.getDetectionInterval();
        Schedule schedule = new IntervalSchedule(Instant.now(), (int) interval.getInterval(), interval.getUnit());
        Duration duration = Duration.of(interval.getInterval(), interval.getUnit());
        AnomalyDetectorJob job = new AnomalyDetectorJob(detector.getDetectorId(), schedule, detector.getWindowDelay(), true, Instant.now(), null, Instant.now(), duration.getSeconds(), detector.getUser(), detector.getResultIndex());
        getAnomalyDetectorJobForWrite(detector, job);
    } catch (Exception e) {
        String message = "Failed to parse anomaly detector job " + detectorId;
        logger.error(message, e);
        listener.onFailure(new OpenSearchStatusException(message, RestStatus.INTERNAL_SERVER_ERROR));
    }
}
Also used : IntervalSchedule(org.opensearch.jobscheduler.spi.schedule.IntervalSchedule) Schedule(org.opensearch.jobscheduler.spi.schedule.Schedule) IntervalSchedule(org.opensearch.jobscheduler.spi.schedule.IntervalSchedule) IntervalTimeConfiguration(org.opensearch.ad.model.IntervalTimeConfiguration) Duration(java.time.Duration) AnomalyDetectorJob(org.opensearch.ad.model.AnomalyDetectorJob) OpenSearchStatusException(org.opensearch.OpenSearchStatusException) IOException(java.io.IOException) OpenSearchStatusException(org.opensearch.OpenSearchStatusException)

Example 4 with Schedule

use of org.opensearch.jobscheduler.spi.schedule.Schedule in project job-scheduler by opensearch-project.

the class JobSchedulerTests method testSchedule.

public void testSchedule() {
    Schedule schedule = Mockito.mock(Schedule.class);
    ScheduledJobRunner runner = Mockito.mock(ScheduledJobRunner.class);
    ScheduledJobParameter jobParameter = buildScheduledJobParameter("job-id", "dummy job name", Instant.now().minus(1, ChronoUnit.HOURS), Instant.now(), schedule, true);
    Mockito.when(schedule.getNextExecutionTime(Mockito.any())).thenReturn(Instant.now().plus(1, ChronoUnit.MINUTES));
    Scheduler.ScheduledCancellable cancellable = Mockito.mock(Scheduler.ScheduledCancellable.class);
    Mockito.when(this.threadPool.schedule(Mockito.any(), Mockito.any(), Mockito.anyString())).thenReturn(cancellable);
    boolean scheduled = this.scheduler.schedule("index", "job-id", jobParameter, runner, dummyVersion, jitterLimit);
    Assert.assertTrue(scheduled);
    Mockito.verify(this.threadPool, Mockito.times(1)).schedule(Mockito.any(), Mockito.any(), Mockito.anyString());
    scheduled = this.scheduler.schedule("index", "job-id", jobParameter, runner, dummyVersion, jitterLimit);
    Assert.assertTrue(scheduled);
    // already scheduled, no extra threadpool call
    Mockito.verify(this.threadPool, Mockito.times(1)).schedule(Mockito.any(), Mockito.any(), Mockito.anyString());
}
Also used : ScheduledJobRunner(org.opensearch.jobscheduler.spi.ScheduledJobRunner) ScheduledJobParameter(org.opensearch.jobscheduler.spi.ScheduledJobParameter) Scheduler(org.opensearch.threadpool.Scheduler) Schedule(org.opensearch.jobscheduler.spi.schedule.Schedule) CronSchedule(org.opensearch.jobscheduler.spi.schedule.CronSchedule)

Example 5 with Schedule

use of org.opensearch.jobscheduler.spi.schedule.Schedule in project job-scheduler by opensearch-project.

the class JobSchedulerTests method testReschedule_jobDescheduled.

public void testReschedule_jobDescheduled() {
    Schedule schedule = Mockito.mock(Schedule.class);
    ScheduledJobParameter jobParameter = buildScheduledJobParameter("job-id", "dummy job name", Instant.now().minus(1, ChronoUnit.HOURS), Instant.now(), schedule, false, 0.6);
    JobSchedulingInfo jobSchedulingInfo = new JobSchedulingInfo("job-index", "job-id", jobParameter);
    Instant now = Instant.now();
    jobSchedulingInfo.setDescheduled(true);
    Mockito.when(schedule.getNextExecutionTime(Mockito.any())).thenReturn(now.plus(1, ChronoUnit.MINUTES)).thenReturn(now.plus(2, ChronoUnit.MINUTES));
    Assert.assertFalse(this.scheduler.reschedule(jobParameter, jobSchedulingInfo, null, dummyVersion, jitterLimit));
}
Also used : ScheduledJobParameter(org.opensearch.jobscheduler.spi.ScheduledJobParameter) Schedule(org.opensearch.jobscheduler.spi.schedule.Schedule) CronSchedule(org.opensearch.jobscheduler.spi.schedule.CronSchedule) Instant(java.time.Instant)

Aggregations

Schedule (org.opensearch.jobscheduler.spi.schedule.Schedule)6 Instant (java.time.Instant)4 CronSchedule (org.opensearch.jobscheduler.spi.schedule.CronSchedule)4 ScheduledJobParameter (org.opensearch.jobscheduler.spi.ScheduledJobParameter)3 IntervalSchedule (org.opensearch.jobscheduler.spi.schedule.IntervalSchedule)3 Scheduler (org.opensearch.threadpool.Scheduler)2 IOException (java.io.IOException)1 Duration (java.time.Duration)1 Test (org.junit.Test)1 ExpectedException (org.junit.rules.ExpectedException)1 OpenSearchStatusException (org.opensearch.OpenSearchStatusException)1 EndRunException (org.opensearch.ad.common.exception.EndRunException)1 AnomalyDetectorJob (org.opensearch.ad.model.AnomalyDetectorJob)1 IntervalTimeConfiguration (org.opensearch.ad.model.IntervalTimeConfiguration)1 User (org.opensearch.commons.authuser.User)1 LockModel (org.opensearch.jobscheduler.spi.LockModel)1 ScheduledJobRunner (org.opensearch.jobscheduler.spi.ScheduledJobRunner)1