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);
}
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"));
}
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));
}
}
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());
}
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));
}
Aggregations