Search in sources :

Example 1 with ScheduledJobParameter

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

the class AnomalyDetectorJobRunnerTests method testRunJobWithWrongParameterType.

@Test
public void testRunJobWithWrongParameterType() {
    expectedEx.expect(IllegalArgumentException.class);
    expectedEx.expectMessage("Job parameter is not instance of AnomalyDetectorJob, type: ");
    ScheduledJobParameter parameter = mock(ScheduledJobParameter.class);
    when(jobParameter.getLockDurationSeconds()).thenReturn(null);
    runner.runJob(parameter, context);
}
Also used : ScheduledJobParameter(org.opensearch.jobscheduler.spi.ScheduledJobParameter) Test(org.junit.Test)

Example 2 with ScheduledJobParameter

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

the class LockService method acquireLock.

/**
 * Attempts to acquire lock the job. If the lock does not exists it attempts to create the lock document.
 * If the Lock document exists, it will try to update and acquire lock.
 *
 * @param jobParameter a {@code ScheduledJobParameter} containing the lock duration.
 * @param context a {@code JobExecutionContext} containing job index name and job id.
 * @param listener an {@code ActionListener} that has onResponse and onFailure that is used to return the lock if it was acquired
 *                 or else null. Passes {@code IllegalArgumentException} to onFailure if the {@code ScheduledJobParameter} does not
 *                 have {@code LockDurationSeconds}.
 */
public void acquireLock(final ScheduledJobParameter jobParameter, final JobExecutionContext context, ActionListener<LockModel> listener) {
    final String jobIndexName = context.getJobIndexName();
    final String jobId = context.getJobId();
    if (jobParameter.getLockDurationSeconds() == null) {
        listener.onFailure(new IllegalArgumentException("Job LockDuration should not be null"));
    } else {
        final long lockDurationSecond = jobParameter.getLockDurationSeconds();
        createLockIndex(ActionListener.wrap(created -> {
            if (created) {
                try {
                    findLock(LockModel.generateLockId(jobIndexName, jobId), ActionListener.wrap(existingLock -> {
                        if (existingLock != null) {
                            if (isLockReleasedOrExpired(existingLock)) {
                                // Lock is expired. Attempt to acquire lock.
                                logger.debug("lock is released or expired: " + existingLock);
                                LockModel updateLock = new LockModel(existingLock, getNow(), lockDurationSecond, false);
                                updateLock(updateLock, listener);
                            } else {
                                logger.debug("Lock is NOT released or expired. " + existingLock);
                                // Lock is still not expired. Return null as we cannot acquire lock.
                                listener.onResponse(null);
                            }
                        } else {
                            // There is no lock object and it is first time. Create new lock.
                            LockModel tempLock = new LockModel(jobIndexName, jobId, getNow(), lockDurationSecond, false);
                            logger.debug("Lock does not exist. Creating new lock" + tempLock);
                            createLock(tempLock, listener);
                        }
                    }, listener::onFailure));
                } catch (VersionConflictEngineException e) {
                    logger.debug("could not acquire lock {}", e.getMessage());
                    listener.onResponse(null);
                }
            } else {
                listener.onResponse(null);
            }
        }, listener::onFailure));
    }
}
Also used : SequenceNumbers(org.opensearch.index.seqno.SequenceNumbers) ToXContent(org.opensearch.common.xcontent.ToXContent) XContentParser(org.opensearch.common.xcontent.XContentParser) XContentFactory(org.opensearch.common.xcontent.XContentFactory) ActionListener(org.opensearch.action.ActionListener) LockModel(org.opensearch.jobscheduler.spi.LockModel) CreateIndexRequest(org.opensearch.action.admin.indices.create.CreateIndexRequest) DeleteRequest(org.opensearch.action.delete.DeleteRequest) Client(org.opensearch.client.Client) VersionConflictEngineException(org.opensearch.index.engine.VersionConflictEngineException) IndexNotFoundException(org.opensearch.index.IndexNotFoundException) GetRequest(org.opensearch.action.get.GetRequest) LoggingDeprecationHandler(org.opensearch.common.xcontent.LoggingDeprecationHandler) IOException(java.io.IOException) DocumentMissingException(org.opensearch.index.engine.DocumentMissingException) Instant(java.time.Instant) ScheduledJobParameter(org.opensearch.jobscheduler.spi.ScheduledJobParameter) InputStreamReader(java.io.InputStreamReader) StandardCharsets(java.nio.charset.StandardCharsets) Logger(org.apache.logging.log4j.Logger) NamedXContentRegistry(org.opensearch.common.xcontent.NamedXContentRegistry) ClusterService(org.opensearch.cluster.service.ClusterService) VisibleForTesting(com.cronutils.utils.VisibleForTesting) ResourceAlreadyExistsException(org.opensearch.ResourceAlreadyExistsException) DocWriteResponse(org.opensearch.action.DocWriteResponse) UpdateRequest(org.opensearch.action.update.UpdateRequest) XContentType(org.opensearch.common.xcontent.XContentType) JobExecutionContext(org.opensearch.jobscheduler.spi.JobExecutionContext) BufferedReader(java.io.BufferedReader) IndexRequest(org.opensearch.action.index.IndexRequest) LogManager(org.apache.logging.log4j.LogManager) InputStream(java.io.InputStream) VersionConflictEngineException(org.opensearch.index.engine.VersionConflictEngineException) LockModel(org.opensearch.jobscheduler.spi.LockModel)

Example 3 with ScheduledJobParameter

use of org.opensearch.jobscheduler.spi.ScheduledJobParameter 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 4 with ScheduledJobParameter

use of org.opensearch.jobscheduler.spi.ScheduledJobParameter 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)

Example 5 with ScheduledJobParameter

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

the class LockService method acquireLockWithId.

/**
 * Attempts to acquire a lock with a specific lock Id. If the lock does not exist it attempts to create the lock document.
 * If the Lock document exists, it will try to update and acquire the lock.
 *
 * @param jobIndexName a non-null job index name.
 * @param lockDurationSeconds the amount of time in seconds that the lock should exist
 * @param lockId the unique Id for the lock. This should represent the resource that the lock is on, whether it be
 *               a job, or some other arbitrary resource. If the lockID matches a jobID, then the lock will be deleted
 *               when the job is deleted.
 * @param listener an {@code ActionListener} that has onResponse and onFailure that is used to return the lock if it was acquired
 *                 or else null. Passes {@code IllegalArgumentException} to onFailure if the {@code ScheduledJobParameter} does not
 *                 have {@code LockDurationSeconds}.
 */
public void acquireLockWithId(final String jobIndexName, final Long lockDurationSeconds, final String lockId, ActionListener<LockModel> listener) {
    if (lockDurationSeconds == null) {
        listener.onFailure(new IllegalArgumentException("Job LockDuration should not be null"));
    } else if (jobIndexName == null) {
        listener.onFailure(new IllegalArgumentException("Job index name should not be null"));
    } else if (lockId == null) {
        listener.onFailure(new IllegalArgumentException("Lock ID should not be null"));
    } else {
        createLockIndex(ActionListener.wrap(created -> {
            if (created) {
                try {
                    findLock(LockModel.generateLockId(jobIndexName, lockId), ActionListener.wrap(existingLock -> {
                        if (existingLock != null) {
                            if (isLockReleasedOrExpired(existingLock)) {
                                // Lock is expired. Attempt to acquire lock.
                                logger.debug("lock is released or expired: " + existingLock);
                                LockModel updateLock = new LockModel(existingLock, getNow(), lockDurationSeconds, false);
                                updateLock(updateLock, listener);
                            } else {
                                logger.debug("Lock is NOT released or expired. " + existingLock);
                                // Lock is still not expired. Return null as we cannot acquire lock.
                                listener.onResponse(null);
                            }
                        } else {
                            // There is no lock object and it is first time. Create new lock.
                            // Note that the lockID will be set to {jobIndexName}-{lockId}
                            LockModel tempLock = new LockModel(jobIndexName, lockId, getNow(), lockDurationSeconds, false);
                            logger.debug("Lock does not exist. Creating new lock" + tempLock);
                            createLock(tempLock, listener);
                        }
                    }, listener::onFailure));
                } catch (VersionConflictEngineException e) {
                    logger.debug("could not acquire lock {}", e.getMessage());
                    listener.onResponse(null);
                }
            } else {
                listener.onResponse(null);
            }
        }, listener::onFailure));
    }
}
Also used : SequenceNumbers(org.opensearch.index.seqno.SequenceNumbers) ToXContent(org.opensearch.common.xcontent.ToXContent) XContentParser(org.opensearch.common.xcontent.XContentParser) XContentFactory(org.opensearch.common.xcontent.XContentFactory) ActionListener(org.opensearch.action.ActionListener) LockModel(org.opensearch.jobscheduler.spi.LockModel) CreateIndexRequest(org.opensearch.action.admin.indices.create.CreateIndexRequest) DeleteRequest(org.opensearch.action.delete.DeleteRequest) Client(org.opensearch.client.Client) VersionConflictEngineException(org.opensearch.index.engine.VersionConflictEngineException) IndexNotFoundException(org.opensearch.index.IndexNotFoundException) GetRequest(org.opensearch.action.get.GetRequest) LoggingDeprecationHandler(org.opensearch.common.xcontent.LoggingDeprecationHandler) IOException(java.io.IOException) DocumentMissingException(org.opensearch.index.engine.DocumentMissingException) Instant(java.time.Instant) ScheduledJobParameter(org.opensearch.jobscheduler.spi.ScheduledJobParameter) InputStreamReader(java.io.InputStreamReader) StandardCharsets(java.nio.charset.StandardCharsets) Logger(org.apache.logging.log4j.Logger) NamedXContentRegistry(org.opensearch.common.xcontent.NamedXContentRegistry) ClusterService(org.opensearch.cluster.service.ClusterService) VisibleForTesting(com.cronutils.utils.VisibleForTesting) ResourceAlreadyExistsException(org.opensearch.ResourceAlreadyExistsException) DocWriteResponse(org.opensearch.action.DocWriteResponse) UpdateRequest(org.opensearch.action.update.UpdateRequest) XContentType(org.opensearch.common.xcontent.XContentType) JobExecutionContext(org.opensearch.jobscheduler.spi.JobExecutionContext) BufferedReader(java.io.BufferedReader) IndexRequest(org.opensearch.action.index.IndexRequest) LogManager(org.apache.logging.log4j.LogManager) InputStream(java.io.InputStream) VersionConflictEngineException(org.opensearch.index.engine.VersionConflictEngineException) LockModel(org.opensearch.jobscheduler.spi.LockModel)

Aggregations

ScheduledJobParameter (org.opensearch.jobscheduler.spi.ScheduledJobParameter)11 Instant (java.time.Instant)4 CronSchedule (org.opensearch.jobscheduler.spi.schedule.CronSchedule)4 LogManager (org.apache.logging.log4j.LogManager)3 Logger (org.apache.logging.log4j.Logger)3 ActionListener (org.opensearch.action.ActionListener)3 IndexRequest (org.opensearch.action.index.IndexRequest)3 Client (org.opensearch.client.Client)3 ClusterService (org.opensearch.cluster.service.ClusterService)3 XContentParser (org.opensearch.common.xcontent.XContentParser)3 XContentType (org.opensearch.common.xcontent.XContentType)3 JobExecutionContext (org.opensearch.jobscheduler.spi.JobExecutionContext)3 ScheduledJobRunner (org.opensearch.jobscheduler.spi.ScheduledJobRunner)3 Schedule (org.opensearch.jobscheduler.spi.schedule.Schedule)3 VisibleForTesting (com.cronutils.utils.VisibleForTesting)2 BufferedReader (java.io.BufferedReader)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 InputStreamReader (java.io.InputStreamReader)2 StandardCharsets (java.nio.charset.StandardCharsets)2