Search in sources :

Example 1 with IntervalSchedule

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

the class AnomalyDetectorJobRunner method runJob.

@Override
public void runJob(ScheduledJobParameter scheduledJobParameter, JobExecutionContext context) {
    String detectorId = scheduledJobParameter.getName();
    log.info("Start to run AD job {}", detectorId);
    adTaskManager.refreshRealtimeJobRunTime(detectorId);
    if (!(scheduledJobParameter instanceof AnomalyDetectorJob)) {
        throw new IllegalArgumentException("Job parameter is not instance of AnomalyDetectorJob, type: " + scheduledJobParameter.getClass().getCanonicalName());
    }
    AnomalyDetectorJob jobParameter = (AnomalyDetectorJob) scheduledJobParameter;
    Instant executionStartTime = Instant.now();
    IntervalSchedule schedule = (IntervalSchedule) jobParameter.getSchedule();
    Instant detectionStartTime = executionStartTime.minus(schedule.getInterval(), schedule.getUnit());
    final LockService lockService = context.getLockService();
    Runnable runnable = () -> {
        if (jobParameter.getLockDurationSeconds() != null) {
            lockService.acquireLock(jobParameter, context, ActionListener.wrap(lock -> runAdJob(jobParameter, lockService, lock, detectionStartTime, executionStartTime), exception -> {
                indexAnomalyResultException(jobParameter, lockService, null, detectionStartTime, executionStartTime, exception, false);
                throw new IllegalStateException("Failed to acquire lock for AD job: " + detectorId);
            }));
        } else {
            log.warn("Can't get lock for AD job: " + detectorId);
        }
    };
    ExecutorService executor = threadPool.executor(AD_THREAD_POOL_NAME);
    executor.submit(runnable);
}
Also used : LockService(org.opensearch.jobscheduler.spi.utils.LockService) IntervalSchedule(org.opensearch.jobscheduler.spi.schedule.IntervalSchedule) Instant(java.time.Instant) ExecutorService(java.util.concurrent.ExecutorService) AnomalyDetectorJob(org.opensearch.ad.model.AnomalyDetectorJob)

Example 2 with IntervalSchedule

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

the class AnomalyDetectorJobRunnerTests method setUpJobParameter.

private void setUpJobParameter() {
    when(jobParameter.getName()).thenReturn(randomAlphaOfLength(10));
    IntervalSchedule schedule = new IntervalSchedule(Instant.now(), 1, ChronoUnit.MINUTES);
    when(jobParameter.getSchedule()).thenReturn(schedule);
    when(jobParameter.getWindowDelay()).thenReturn(new IntervalTimeConfiguration(10, ChronoUnit.SECONDS));
}
Also used : IntervalSchedule(org.opensearch.jobscheduler.spi.schedule.IntervalSchedule) IntervalTimeConfiguration(org.opensearch.ad.model.IntervalTimeConfiguration)

Example 3 with IntervalSchedule

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

the class AnomalyDetectorJobRunnerTests method testRunJobWithNullLockDuration.

@Test
public void testRunJobWithNullLockDuration() throws InterruptedException {
    when(jobParameter.getLockDurationSeconds()).thenReturn(null);
    when(jobParameter.getSchedule()).thenReturn(new IntervalSchedule(Instant.now(), 1, ChronoUnit.MINUTES));
    runner.runJob(jobParameter, context);
    Thread.sleep(1000);
    assertTrue(testAppender.containsMessage("Can't get lock for AD job"));
}
Also used : IntervalSchedule(org.opensearch.jobscheduler.spi.schedule.IntervalSchedule) Test(org.junit.Test)

Example 4 with IntervalSchedule

use of org.opensearch.jobscheduler.spi.schedule.IntervalSchedule 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 5 with IntervalSchedule

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

the class AnomalyDetectorJobRunnerTests method testRunJobWithLockDuration.

@Test
public void testRunJobWithLockDuration() throws InterruptedException {
    when(jobParameter.getLockDurationSeconds()).thenReturn(100L);
    when(jobParameter.getSchedule()).thenReturn(new IntervalSchedule(Instant.now(), 1, ChronoUnit.MINUTES));
    runner.runJob(jobParameter, context);
    Thread.sleep(1000);
    assertFalse(testAppender.containsMessage("Can't get lock for AD job"));
    verify(context, times(1)).getLockService();
}
Also used : IntervalSchedule(org.opensearch.jobscheduler.spi.schedule.IntervalSchedule) Test(org.junit.Test)

Aggregations

IntervalSchedule (org.opensearch.jobscheduler.spi.schedule.IntervalSchedule)6 IOException (java.io.IOException)2 Instant (java.time.Instant)2 Test (org.junit.Test)2 AnomalyDetectorJob (org.opensearch.ad.model.AnomalyDetectorJob)2 IntervalTimeConfiguration (org.opensearch.ad.model.IntervalTimeConfiguration)2 Duration (java.time.Duration)1 ChronoUnit (java.time.temporal.ChronoUnit)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 List (java.util.List)1 ExecutorService (java.util.concurrent.ExecutorService)1 OpenSearchStatusException (org.opensearch.OpenSearchStatusException)1 ActionListener (org.opensearch.action.ActionListener)1 DeleteRequest (org.opensearch.action.delete.DeleteRequest)1 DeleteResponse (org.opensearch.action.delete.DeleteResponse)1 IndexRequest (org.opensearch.action.index.IndexRequest)1 IndexResponse (org.opensearch.action.index.IndexResponse)1 NodeClient (org.opensearch.client.node.NodeClient)1 JsonXContent (org.opensearch.common.xcontent.json.JsonXContent)1