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