Search in sources :

Example 6 with LockService

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

the class AnomalyDetectorJobRunnerTests method setup.

@SuppressWarnings("unchecked")
@Before
public void setup() throws Exception {
    super.setUp();
    super.setUpLog4jForJUnit(AnomalyDetectorJobRunner.class);
    MockitoAnnotations.initMocks(this);
    ThreadFactory threadFactory = OpenSearchExecutors.daemonThreadFactory(OpenSearchExecutors.threadName("node1", "test-ad"));
    ThreadContext threadContext = new ThreadContext(Settings.EMPTY);
    executorService = OpenSearchExecutors.newFixed("test-ad", 4, 100, threadFactory, threadContext);
    Mockito.doReturn(executorService).when(mockedThreadPool).executor(anyString());
    Mockito.doReturn(mockedThreadPool).when(client).threadPool();
    Mockito.doReturn(threadContext).when(mockedThreadPool).getThreadContext();
    runner.setThreadPool(mockedThreadPool);
    runner.setClient(client);
    runner.setAnomalyResultHandler(anomalyResultHandler);
    runner.setAdTaskManager(adTaskManager);
    Settings settings = Settings.builder().put("plugins.anomaly_detection.max_retry_for_backoff", 2).put("plugins.anomaly_detection.backoff_initial_delay", TimeValue.timeValueMillis(1)).put("plugins.anomaly_detection.max_retry_for_end_run_exception", 3).build();
    setUpJobParameter();
    runner.setSettings(settings);
    AnomalyDetectionIndices anomalyDetectionIndices = mock(AnomalyDetectionIndices.class);
    IndexNameExpressionResolver indexNameResolver = mock(IndexNameExpressionResolver.class);
    IndexUtils indexUtils = new IndexUtils(client, clientUtil, clusterService, indexNameResolver);
    NodeStateManager stateManager = mock(NodeStateManager.class);
    runner.setAnomalyDetectionIndices(indexUtil);
    lockService = new LockService(client, clusterService);
    doReturn(lockService).when(context).getLockService();
    doAnswer(invocation -> {
        Object[] args = invocation.getArguments();
        GetRequest request = (GetRequest) args[0];
        ActionListener<GetResponse> listener = (ActionListener<GetResponse>) args[1];
        if (request.index().equals(ANOMALY_DETECTOR_JOB_INDEX)) {
            AnomalyDetectorJob job = TestHelpers.randomAnomalyDetectorJob(true);
            listener.onResponse(TestHelpers.createGetResponse(job, randomAlphaOfLength(5), ANOMALY_DETECTOR_JOB_INDEX));
        }
        return null;
    }).when(client).get(any(), any());
    doAnswer(invocation -> {
        Object[] args = invocation.getArguments();
        assertTrue(String.format("The size of args is %d.  Its content is %s", args.length, Arrays.toString(args)), args.length >= 2);
        IndexRequest request = null;
        ActionListener<IndexResponse> listener = null;
        if (args[0] instanceof IndexRequest) {
            request = (IndexRequest) args[0];
        }
        if (args[1] instanceof ActionListener) {
            listener = (ActionListener<IndexResponse>) args[1];
        }
        assertTrue(request != null && listener != null);
        ShardId shardId = new ShardId(new Index(ANOMALY_DETECTOR_JOB_INDEX, randomAlphaOfLength(10)), 0);
        listener.onResponse(new IndexResponse(shardId, randomAlphaOfLength(10), request.id(), 1, 1, 1, true));
        return null;
    }).when(client).index(any(), any());
}
Also used : ThreadFactory(java.util.concurrent.ThreadFactory) LockService(org.opensearch.jobscheduler.spi.utils.LockService) ThreadContext(org.opensearch.common.util.concurrent.ThreadContext) Index(org.opensearch.index.Index) AnomalyDetectorJob(org.opensearch.ad.model.AnomalyDetectorJob) IndexRequest(org.opensearch.action.index.IndexRequest) GetResponse(org.opensearch.action.get.GetResponse) ShardId(org.opensearch.index.shard.ShardId) IndexUtils(org.opensearch.ad.util.IndexUtils) ActionListener(org.opensearch.action.ActionListener) IndexResponse(org.opensearch.action.index.IndexResponse) GetRequest(org.opensearch.action.get.GetRequest) AnomalyDetectionIndices(org.opensearch.ad.indices.AnomalyDetectionIndices) IndexNameExpressionResolver(org.opensearch.cluster.metadata.IndexNameExpressionResolver) Settings(org.opensearch.common.settings.Settings) Before(org.junit.Before)

Aggregations

LockService (org.opensearch.jobscheduler.spi.utils.LockService)6 ActionListener (org.opensearch.action.ActionListener)3 IndexRequest (org.opensearch.action.index.IndexRequest)3 AnomalyDetectorJob (org.opensearch.ad.model.AnomalyDetectorJob)3 Client (org.opensearch.client.Client)3 ScheduledJobRunner (org.opensearch.jobscheduler.spi.ScheduledJobRunner)3 ThreadPool (org.opensearch.threadpool.ThreadPool)3 Instant (java.time.Instant)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 List (java.util.List)2 ExecutorService (java.util.concurrent.ExecutorService)2 LogManager (org.apache.logging.log4j.LogManager)2 Logger (org.apache.logging.log4j.Logger)2 Before (org.junit.Before)2 GetRequest (org.opensearch.action.get.GetRequest)2 GetResponse (org.opensearch.action.get.GetResponse)2 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)2 ClusterService (org.opensearch.cluster.service.ClusterService)2 NamedXContentRegistry (org.opensearch.common.xcontent.NamedXContentRegistry)2