Search in sources :

Example 1 with ADTaskManager

use of org.opensearch.ad.task.ADTaskManager in project anomaly-detection by opensearch-project.

the class ADStatsNodesTransportActionTests method setUp.

@Override
@Before
public void setUp() throws Exception {
    super.setUp();
    Client client = client();
    Clock clock = mock(Clock.class);
    Throttler throttler = new Throttler(clock);
    ThreadPool threadPool = mock(ThreadPool.class);
    IndexNameExpressionResolver indexNameResolver = mock(IndexNameExpressionResolver.class);
    IndexUtils indexUtils = new IndexUtils(client, new ClientUtil(Settings.EMPTY, client, throttler, threadPool), clusterService(), indexNameResolver);
    ModelManager modelManager = mock(ModelManager.class);
    CacheProvider cacheProvider = mock(CacheProvider.class);
    EntityCache cache = mock(EntityCache.class);
    when(cacheProvider.get()).thenReturn(cache);
    clusterStatName1 = "clusterStat1";
    clusterStatName2 = "clusterStat2";
    nodeStatName1 = "nodeStat1";
    nodeStatName2 = "nodeStat2";
    Settings settings = Settings.builder().put(MAX_MODEL_SIZE_PER_NODE.getKey(), 10).build();
    ClusterService clusterService = mock(ClusterService.class);
    ClusterSettings clusterSettings = new ClusterSettings(Settings.EMPTY, Collections.unmodifiableSet(new HashSet<>(Arrays.asList(MAX_MODEL_SIZE_PER_NODE))));
    when(clusterService.getClusterSettings()).thenReturn(clusterSettings);
    statsMap = new HashMap<String, ADStat<?>>() {

        {
            put(nodeStatName1, new ADStat<>(false, new CounterSupplier()));
            put(nodeStatName2, new ADStat<>(false, new ModelsOnNodeSupplier(modelManager, cacheProvider, settings, clusterService)));
            put(clusterStatName1, new ADStat<>(true, new IndexStatusSupplier(indexUtils, "index1")));
            put(clusterStatName2, new ADStat<>(true, new IndexStatusSupplier(indexUtils, "index2")));
            put(InternalStatNames.JVM_HEAP_USAGE.getName(), new ADStat<>(true, new SettableSupplier()));
        }
    };
    adStats = new ADStats(statsMap);
    JvmService jvmService = mock(JvmService.class);
    JvmStats jvmStats = mock(JvmStats.class);
    JvmStats.Mem mem = mock(JvmStats.Mem.class);
    when(jvmService.stats()).thenReturn(jvmStats);
    when(jvmStats.getMem()).thenReturn(mem);
    when(mem.getHeapUsedPercent()).thenReturn(randomShort());
    adTaskManager = mock(ADTaskManager.class);
    action = new ADStatsNodesTransportAction(client().threadPool(), clusterService(), mock(TransportService.class), mock(ActionFilters.class), adStats, jvmService, adTaskManager);
}
Also used : ModelsOnNodeSupplier(org.opensearch.ad.stats.suppliers.ModelsOnNodeSupplier) ClusterSettings(org.opensearch.common.settings.ClusterSettings) EntityCache(org.opensearch.ad.caching.EntityCache) ClientUtil(org.opensearch.ad.util.ClientUtil) ADStat(org.opensearch.ad.stats.ADStat) ThreadPool(org.opensearch.threadpool.ThreadPool) Clock(java.time.Clock) CounterSupplier(org.opensearch.ad.stats.suppliers.CounterSupplier) IndexUtils(org.opensearch.ad.util.IndexUtils) JvmService(org.opensearch.monitor.jvm.JvmService) Client(org.opensearch.client.Client) ClusterSettings(org.opensearch.common.settings.ClusterSettings) Settings(org.opensearch.common.settings.Settings) Throttler(org.opensearch.ad.util.Throttler) HashSet(java.util.HashSet) JvmStats(org.opensearch.monitor.jvm.JvmStats) ModelManager(org.opensearch.ad.ml.ModelManager) CacheProvider(org.opensearch.ad.caching.CacheProvider) SettableSupplier(org.opensearch.ad.stats.suppliers.SettableSupplier) ClusterService(org.opensearch.cluster.service.ClusterService) IndexStatusSupplier(org.opensearch.ad.stats.suppliers.IndexStatusSupplier) ADTaskManager(org.opensearch.ad.task.ADTaskManager) ADStats(org.opensearch.ad.stats.ADStats) IndexNameExpressionResolver(org.opensearch.cluster.metadata.IndexNameExpressionResolver) Before(org.junit.Before)

Example 2 with ADTaskManager

use of org.opensearch.ad.task.ADTaskManager in project anomaly-detection by opensearch-project.

the class MultiEntityProfileRunnerTests method setUp.

@SuppressWarnings("unchecked")
@Before
@Override
public void setUp() throws Exception {
    super.setUp();
    client = mock(Client.class);
    nodeFilter = mock(DiscoveryNodeFilterer.class);
    requiredSamples = 128;
    detectorId = "A69pa3UBHuCbh-emo9oR";
    detector = TestHelpers.randomAnomalyDetectorUsingCategoryFields(detectorId, Arrays.asList("a"));
    result = new DetectorInternalState.Builder().lastUpdateTime(Instant.now());
    job = TestHelpers.randomAnomalyDetectorJob(true);
    adTaskManager = mock(ADTaskManager.class);
    transportService = mock(TransportService.class);
    doAnswer(invocation -> {
        Object[] args = invocation.getArguments();
        Consumer<Optional<ADTask>> function = (Consumer<Optional<ADTask>>) args[2];
        function.accept(Optional.of(TestHelpers.randomAdTask()));
        return null;
    }).when(adTaskManager).getAndExecuteOnLatestDetectorLevelTask(any(), any(), any(), any(), anyBoolean(), any());
    runner = new AnomalyDetectorProfileRunner(client, xContentRegistry(), nodeFilter, requiredSamples, transportService, adTaskManager);
    doAnswer(invocation -> {
        Object[] args = invocation.getArguments();
        GetRequest request = (GetRequest) args[0];
        ActionListener<GetResponse> listener = (ActionListener<GetResponse>) args[1];
        String indexName = request.index();
        if (indexName.equals(ANOMALY_DETECTORS_INDEX)) {
            listener.onResponse(TestHelpers.createGetResponse(detector, detector.getDetectorId(), AnomalyDetector.ANOMALY_DETECTORS_INDEX));
        } else if (indexName.equals(CommonName.DETECTION_STATE_INDEX)) {
            listener.onResponse(TestHelpers.createGetResponse(result.build(), detector.getDetectorId(), CommonName.DETECTION_STATE_INDEX));
        } else if (indexName.equals(ANOMALY_DETECTOR_JOB_INDEX)) {
            listener.onResponse(TestHelpers.createGetResponse(job, detector.getDetectorId(), AnomalyDetectorJob.ANOMALY_DETECTOR_JOB_INDEX));
        }
        return null;
    }).when(client).get(any(), any());
    stateNError = new HashSet<DetectorProfileName>();
    stateNError.add(DetectorProfileName.ERROR);
    stateNError.add(DetectorProfileName.STATE);
}
Also used : DiscoveryNodeFilterer(org.opensearch.ad.util.DiscoveryNodeFilterer) Optional(java.util.Optional) GetResponse(org.opensearch.action.get.GetResponse) Consumer(java.util.function.Consumer) ActionListener(org.opensearch.action.ActionListener) ADTaskManager(org.opensearch.ad.task.ADTaskManager) TransportService(org.opensearch.transport.TransportService) DetectorProfileName(org.opensearch.ad.model.DetectorProfileName) GetRequest(org.opensearch.action.get.GetRequest) ADTask(org.opensearch.ad.model.ADTask) Client(org.opensearch.client.Client) Before(org.junit.Before)

Example 3 with ADTaskManager

use of org.opensearch.ad.task.ADTaskManager in project anomaly-detection by opensearch-project.

the class IndexAnomalyDetectorActionHandlerTests method setUp.

@SuppressWarnings("unchecked")
@Override
@Before
public void setUp() throws Exception {
    super.setUp();
    settings = Settings.EMPTY;
    clusterService = mock(ClusterService.class);
    clientMock = spy(new NodeClient(settings, threadPool));
    transportService = mock(TransportService.class);
    channel = mock(ActionListener.class);
    anomalyDetectionIndices = mock(AnomalyDetectionIndices.class);
    when(anomalyDetectionIndices.doesAnomalyDetectorIndexExist()).thenReturn(true);
    detectorId = "123";
    seqNo = 0L;
    primaryTerm = 0L;
    WriteRequest.RefreshPolicy refreshPolicy = WriteRequest.RefreshPolicy.IMMEDIATE;
    String field = "a";
    detector = TestHelpers.randomAnomalyDetectorUsingCategoryFields(detectorId, Arrays.asList(field));
    requestTimeout = new TimeValue(1000L);
    maxSingleEntityAnomalyDetectors = 1000;
    maxMultiEntityAnomalyDetectors = 10;
    maxAnomalyFeatures = 5;
    method = RestRequest.Method.POST;
    adTaskManager = mock(ADTaskManager.class);
    searchFeatureDao = mock(SearchFeatureDao.class);
    handler = new IndexAnomalyDetectorActionHandler(clusterService, clientMock, transportService, channel, anomalyDetectionIndices, detectorId, seqNo, primaryTerm, refreshPolicy, detector, requestTimeout, maxSingleEntityAnomalyDetectors, maxMultiEntityAnomalyDetectors, maxAnomalyFeatures, method, xContentRegistry(), null, adTaskManager, searchFeatureDao);
}
Also used : NodeClient(org.opensearch.client.node.NodeClient) ClusterService(org.opensearch.cluster.service.ClusterService) ActionListener(org.opensearch.action.ActionListener) TransportService(org.opensearch.transport.TransportService) ADTaskManager(org.opensearch.ad.task.ADTaskManager) WriteRequest(org.opensearch.action.support.WriteRequest) AnomalyDetectionIndices(org.opensearch.ad.indices.AnomalyDetectionIndices) IndexAnomalyDetectorActionHandler(org.opensearch.ad.rest.handler.IndexAnomalyDetectorActionHandler) SearchFeatureDao(org.opensearch.ad.feature.SearchFeatureDao) TimeValue(org.opensearch.common.unit.TimeValue) Before(org.junit.Before)

Example 4 with ADTaskManager

use of org.opensearch.ad.task.ADTaskManager in project anomaly-detection by opensearch-project.

the class IndexAnomalyDetectorTransportActionTests method setUp.

@SuppressWarnings("unchecked")
@Override
@Before
public void setUp() throws Exception {
    super.setUp();
    clusterService = mock(ClusterService.class);
    clusterSettings = new ClusterSettings(Settings.EMPTY, Collections.unmodifiableSet(new HashSet<>(Arrays.asList(AnomalyDetectorSettings.FILTER_BY_BACKEND_ROLES))));
    when(clusterService.getClusterSettings()).thenReturn(clusterSettings);
    ClusterName clusterName = new ClusterName("test");
    Settings indexSettings = Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0).put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT).build();
    final Settings.Builder existingSettings = Settings.builder().put(indexSettings).put(IndexMetadata.SETTING_INDEX_UUID, "test2UUID");
    IndexMetadata indexMetaData = IndexMetadata.builder(AnomalyDetector.ANOMALY_DETECTORS_INDEX).settings(existingSettings).build();
    final ImmutableOpenMap<String, IndexMetadata> indices = ImmutableOpenMap.<String, IndexMetadata>builder().fPut(AnomalyDetector.ANOMALY_DETECTORS_INDEX, indexMetaData).build();
    ClusterState clusterState = ClusterState.builder(clusterName).metadata(Metadata.builder().indices(indices).build()).build();
    when(clusterService.state()).thenReturn(clusterState);
    adTaskManager = mock(ADTaskManager.class);
    searchFeatureDao = mock(SearchFeatureDao.class);
    action = new IndexAnomalyDetectorTransportAction(mock(TransportService.class), mock(ActionFilters.class), client(), clusterService, indexSettings(), mock(AnomalyDetectionIndices.class), xContentRegistry(), adTaskManager, searchFeatureDao);
    task = mock(Task.class);
    AnomalyDetector detector = TestHelpers.randomAnomalyDetector(ImmutableMap.of("testKey", "testValue"), Instant.now());
    GetResponse getDetectorResponse = TestHelpers.createGetResponse(detector, detector.getDetectorId(), AnomalyDetector.ANOMALY_DETECTORS_INDEX);
    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);
        assertTrue(args[0] instanceof GetRequest);
        assertTrue(args[1] instanceof ActionListener);
        ActionListener<GetResponse> listener = (ActionListener<GetResponse>) args[1];
        listener.onResponse(getDetectorResponse);
        return null;
    }).when(client).get(any(GetRequest.class), any());
    SearchHits hits = new SearchHits(new SearchHit[] {}, null, Float.NaN);
    SearchResponseSections searchSections = new SearchResponseSections(hits, null, null, false, false, null, 1);
    SearchResponse searchResponse = new SearchResponse(searchSections, null, 1, 1, 0, 30, ShardSearchFailure.EMPTY_ARRAY, SearchResponse.Clusters.EMPTY);
    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);
        assertTrue(args[0] instanceof SearchRequest);
        assertTrue(args[1] instanceof ActionListener);
        ActionListener<SearchResponse> listener = (ActionListener<SearchResponse>) args[1];
        listener.onResponse(searchResponse);
        return null;
    }).when(client).search(any(SearchRequest.class), any());
    request = new IndexAnomalyDetectorRequest("1234", 4567, 7890, WriteRequest.RefreshPolicy.IMMEDIATE, detector, RestRequest.Method.PUT, TimeValue.timeValueSeconds(60), 1000, 10, 5);
    response = new ActionListener<IndexAnomalyDetectorResponse>() {

        @Override
        public void onResponse(IndexAnomalyDetectorResponse indexResponse) {
            // onResponse will not be called as we do not have the AD index
            Assert.assertTrue(false);
        }

        @Override
        public void onFailure(Exception e) {
            Assert.assertTrue(true);
        }
    };
}
Also used : SearchRequest(org.opensearch.action.search.SearchRequest) Task(org.opensearch.tasks.Task) SearchResponseSections(org.opensearch.action.search.SearchResponseSections) ClusterSettings(org.opensearch.common.settings.ClusterSettings) SearchFeatureDao(org.opensearch.ad.feature.SearchFeatureDao) AnomalyDetector(org.opensearch.ad.model.AnomalyDetector) GetRequest(org.opensearch.action.get.GetRequest) ClusterName(org.opensearch.cluster.ClusterName) SearchHits(org.opensearch.search.SearchHits) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) AnomalyDetectorSettings(org.opensearch.ad.settings.AnomalyDetectorSettings) ClusterSettings(org.opensearch.common.settings.ClusterSettings) Settings(org.opensearch.common.settings.Settings) ClusterState(org.opensearch.cluster.ClusterState) GetResponse(org.opensearch.action.get.GetResponse) SearchResponse(org.opensearch.action.search.SearchResponse) ClusterService(org.opensearch.cluster.service.ClusterService) ActionListener(org.opensearch.action.ActionListener) ADTaskManager(org.opensearch.ad.task.ADTaskManager) Before(org.junit.Before)

Example 5 with ADTaskManager

use of org.opensearch.ad.task.ADTaskManager in project anomaly-detection by opensearch-project.

the class DeleteAnomalyDetectorActionTests method setUp.

@Override
@Before
public void setUp() throws Exception {
    super.setUp();
    ClusterService clusterService = mock(ClusterService.class);
    ClusterSettings clusterSettings = new ClusterSettings(Settings.EMPTY, Collections.unmodifiableSet(new HashSet<>(Arrays.asList(AnomalyDetectorSettings.FILTER_BY_BACKEND_ROLES))));
    when(clusterService.getClusterSettings()).thenReturn(clusterSettings);
    adTaskManager = mock(ADTaskManager.class);
    action = new DeleteAnomalyDetectorTransportAction(mock(TransportService.class), mock(ActionFilters.class), client(), clusterService, Settings.EMPTY, xContentRegistry(), adTaskManager);
    response = new ActionListener<DeleteResponse>() {

        @Override
        public void onResponse(DeleteResponse deleteResponse) {
            Assert.assertTrue(true);
        }

        @Override
        public void onFailure(Exception e) {
            Assert.assertTrue(true);
        }
    };
}
Also used : ClusterService(org.opensearch.cluster.service.ClusterService) ClusterSettings(org.opensearch.common.settings.ClusterSettings) DeleteResponse(org.opensearch.action.delete.DeleteResponse) ADTaskManager(org.opensearch.ad.task.ADTaskManager) IOException(java.io.IOException) ActionRequestValidationException(org.opensearch.action.ActionRequestValidationException) HashSet(java.util.HashSet) Before(org.junit.Before)

Aggregations

ADTaskManager (org.opensearch.ad.task.ADTaskManager)15 Before (org.junit.Before)11 ClusterService (org.opensearch.cluster.service.ClusterService)10 ActionListener (org.opensearch.action.ActionListener)9 TransportService (org.opensearch.transport.TransportService)9 Client (org.opensearch.client.Client)8 HashSet (java.util.HashSet)6 GetResponse (org.opensearch.action.get.GetResponse)6 Optional (java.util.Optional)5 GetRequest (org.opensearch.action.get.GetRequest)5 DiscoveryNodeFilterer (org.opensearch.ad.util.DiscoveryNodeFilterer)5 ClusterSettings (org.opensearch.common.settings.ClusterSettings)5 ActionFilters (org.opensearch.action.support.ActionFilters)4 CacheProvider (org.opensearch.ad.caching.CacheProvider)4 EntityCache (org.opensearch.ad.caching.EntityCache)4 FeatureManager (org.opensearch.ad.feature.FeatureManager)4 SearchFeatureDao (org.opensearch.ad.feature.SearchFeatureDao)4 AnomalyDetectionIndices (org.opensearch.ad.indices.AnomalyDetectionIndices)4 ModelManager (org.opensearch.ad.ml.ModelManager)4 ADTask (org.opensearch.ad.model.ADTask)4