use of org.opensearch.jobscheduler.spi.utils.LockService in project anomaly-detection by opensearch-project.
the class AnomalyDetectorJobRunner method runAnomalyDetectionJob.
private void runAnomalyDetectionJob(AnomalyDetectorJob jobParameter, LockService lockService, LockModel lock, Instant detectionStartTime, Instant executionStartTime, String detectorId, String user, List<String> roles) {
try (InjectSecurity injectSecurity = new InjectSecurity(detectorId, settings, client.threadPool().getThreadContext())) {
// Injecting user role to verify if the user has permissions for our API.
injectSecurity.inject(user, roles);
AnomalyResultRequest request = new AnomalyResultRequest(detectorId, detectionStartTime.toEpochMilli(), executionStartTime.toEpochMilli());
client.execute(AnomalyResultAction.INSTANCE, request, ActionListener.wrap(response -> {
indexAnomalyResult(jobParameter, lockService, lock, detectionStartTime, executionStartTime, response);
}, exception -> {
handleAdException(jobParameter, lockService, lock, detectionStartTime, executionStartTime, exception);
}));
} catch (Exception e) {
indexAnomalyResultException(jobParameter, lockService, lock, detectionStartTime, executionStartTime, e, true);
log.error("Failed to execute AD job " + detectorId, e);
}
}
use of org.opensearch.jobscheduler.spi.utils.LockService 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);
}
use of org.opensearch.jobscheduler.spi.utils.LockService in project job-scheduler by opensearch-project.
the class SampleJobRunner method runJob.
@Override
public void runJob(ScheduledJobParameter jobParameter, JobExecutionContext context) {
if (!(jobParameter instanceof SampleJobParameter)) {
throw new IllegalStateException("Job parameter is not instance of SampleJobParameter, type: " + jobParameter.getClass().getCanonicalName());
}
if (this.clusterService == null) {
throw new IllegalStateException("ClusterService is not initialized.");
}
if (this.threadPool == null) {
throw new IllegalStateException("ThreadPool is not initialized.");
}
final LockService lockService = context.getLockService();
Runnable runnable = () -> {
if (jobParameter.getLockDurationSeconds() != null) {
lockService.acquireLock(jobParameter, context, ActionListener.wrap(lock -> {
if (lock == null) {
return;
}
SampleJobParameter parameter = (SampleJobParameter) jobParameter;
StringBuilder msg = new StringBuilder();
msg.append("Watching index ").append(parameter.getIndexToWatch()).append("\n");
List<ShardRouting> shardRoutingList = this.clusterService.state().routingTable().allShards(parameter.getIndexToWatch());
for (ShardRouting shardRouting : shardRoutingList) {
msg.append(shardRouting.shardId().getId()).append("\t").append(shardRouting.currentNodeId()).append("\t").append(shardRouting.active() ? "active" : "inactive").append("\n");
}
log.info(msg.toString());
runTaskForIntegrationTests(parameter);
runTaskForLockIntegrationTests(parameter);
lockService.release(lock, ActionListener.wrap(released -> {
log.info("Released lock for job {}", jobParameter.getName());
}, exception -> {
throw new IllegalStateException("Failed to release lock.");
}));
}, exception -> {
throw new IllegalStateException("Failed to acquire lock.");
}));
}
};
threadPool.generic().submit(runnable);
}
use of org.opensearch.jobscheduler.spi.utils.LockService in project job-scheduler by opensearch-project.
the class JobSweeperTests method setup.
@Before
public void setup() throws IOException {
this.client = Mockito.mock(Client.class);
this.threadPool = Mockito.mock(ThreadPool.class);
this.scheduler = Mockito.mock(JobScheduler.class);
this.jobRunner = Mockito.mock(ScheduledJobRunner.class);
this.jobParser = Mockito.mock(ScheduledJobParser.class);
// NamedXContentRegistry.Entry xContentRegistryEntry = new NamedXContentRegistry.Entry(ScheduledJobParameter.class,
// new ParseField("JOB_TYPE"), this.jobParser);
List<NamedXContentRegistry.Entry> namedXContentRegistryEntries = new ArrayList<>();
// namedXContentRegistryEntries.add(xContentRegistryEntry);
this.xContentRegistry = new NamedXContentRegistry(namedXContentRegistryEntries);
this.settings = Settings.builder().build();
this.discoveryNode = new DiscoveryNode("node", OpenSearchTestCase.buildNewFakeTransportAddress(), Version.CURRENT);
Set<Setting<?>> settingSet = new HashSet<>();
settingSet.addAll(ClusterSettings.BUILT_IN_CLUSTER_SETTINGS);
settingSet.add(JobSchedulerSettings.REQUEST_TIMEOUT);
settingSet.add(JobSchedulerSettings.SWEEP_PERIOD);
settingSet.add(JobSchedulerSettings.SWEEP_BACKOFF_RETRY_COUNT);
settingSet.add(JobSchedulerSettings.SWEEP_BACKOFF_MILLIS);
settingSet.add(JobSchedulerSettings.SWEEP_PAGE_SIZE);
settingSet.add(JobSchedulerSettings.JITTER_LIMIT);
ClusterSettings clusterSettings = new ClusterSettings(this.settings, settingSet);
ClusterService originClusterService = ClusterServiceUtils.createClusterService(this.threadPool, discoveryNode, clusterSettings);
this.clusterService = Mockito.spy(originClusterService);
ScheduledJobProvider jobProvider = new ScheduledJobProvider("JOB_TYPE", "job-index-name", this.jobParser, this.jobRunner);
Map<String, ScheduledJobProvider> jobProviderMap = new HashMap<>();
jobProviderMap.put("index-name", jobProvider);
sweeper = new JobSweeper(settings, this.client, this.clusterService, this.threadPool, xContentRegistry, jobProviderMap, scheduler, new LockService(client, clusterService));
}
use of org.opensearch.jobscheduler.spi.utils.LockService in project job-scheduler by opensearch-project.
the class JobSchedulerPlugin method createComponents.
@Override
public Collection<Object> createComponents(Client client, ClusterService clusterService, ThreadPool threadPool, ResourceWatcherService resourceWatcherService, ScriptService scriptService, NamedXContentRegistry xContentRegistry, Environment environment, NodeEnvironment nodeEnvironment, NamedWriteableRegistry namedWriteableRegistry, IndexNameExpressionResolver indexNameExpressionResolver, Supplier<RepositoriesService> repositoriesServiceSupplier) {
this.lockService = new LockService(client, clusterService);
this.scheduler = new JobScheduler(threadPool, this.lockService);
this.sweeper = initSweeper(environment.settings(), client, clusterService, threadPool, xContentRegistry, this.scheduler, this.lockService);
clusterService.addListener(this.sweeper);
clusterService.addLifecycleListener(this.sweeper);
return Collections.emptyList();
}
Aggregations