Search in sources :

Example 31 with ClusterSettings

use of org.opensearch.common.settings.ClusterSettings in project anomaly-detection by opensearch-project.

the class ResultWriteWorkerTests method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    clusterService = mock(ClusterService.class);
    ClusterSettings clusterSettings = new ClusterSettings(Settings.EMPTY, Collections.unmodifiableSet(new HashSet<>(Arrays.asList(AnomalyDetectorSettings.RESULT_WRITE_QUEUE_MAX_HEAP_PERCENT, AnomalyDetectorSettings.RESULT_WRITE_QUEUE_CONCURRENCY, AnomalyDetectorSettings.RESULT_WRITE_QUEUE_BATCH_SIZE))));
    when(clusterService.getClusterSettings()).thenReturn(clusterSettings);
    threadPool = mock(ThreadPool.class);
    setUpADThreadPool(threadPool);
    resultHandler = mock(MultiEntityResultHandler.class);
    resultWriteQueue = new ResultWriteWorker(Integer.MAX_VALUE, AnomalyDetectorSettings.RESULT_WRITE_QUEUE_SIZE_IN_BYTES, AnomalyDetectorSettings.RESULT_WRITE_QUEUE_MAX_HEAP_PERCENT, clusterService, new Random(42), mock(ADCircuitBreakerService.class), threadPool, Settings.EMPTY, AnomalyDetectorSettings.MAX_QUEUED_TASKS_RATIO, clock, AnomalyDetectorSettings.MEDIUM_SEGMENT_PRUNE_RATIO, AnomalyDetectorSettings.LOW_SEGMENT_PRUNE_RATIO, AnomalyDetectorSettings.MAINTENANCE_FREQ_CONSTANT, AnomalyDetectorSettings.QUEUE_MAINTENANCE, resultHandler, xContentRegistry(), nodeStateManager, AnomalyDetectorSettings.HOURLY_MAINTENANCE);
    detectResult = TestHelpers.randomHCADAnomalyDetectResult(0.8, Double.NaN, null);
}
Also used : ClusterService(org.opensearch.cluster.service.ClusterService) ClusterSettings(org.opensearch.common.settings.ClusterSettings) Random(java.util.Random) ThreadPool(org.opensearch.threadpool.ThreadPool) MultiEntityResultHandler(org.opensearch.ad.transport.handler.MultiEntityResultHandler) HashSet(java.util.HashSet)

Example 32 with ClusterSettings

use of org.opensearch.common.settings.ClusterSettings 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 33 with ClusterSettings

use of org.opensearch.common.settings.ClusterSettings in project anomaly-detection by opensearch-project.

the class PreviewAnomalyDetectorTransportActionTests method setUp.

@Override
@Before
public void setUp() throws Exception {
    super.setUp();
    task = mock(Task.class);
    clusterService = mock(ClusterService.class);
    ClusterSettings clusterSettings = new ClusterSettings(Settings.EMPTY, Collections.unmodifiableSet(new HashSet<>(Arrays.asList(AnomalyDetectorSettings.MAX_ANOMALY_FEATURES, AnomalyDetectorSettings.FILTER_BY_BACKEND_ROLES, AnomalyDetectorSettings.PAGE_SIZE, AnomalyDetectorSettings.MAX_CONCURRENT_PREVIEW))));
    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);
    featureManager = mock(FeatureManager.class);
    modelManager = mock(ModelManager.class);
    runner = new AnomalyDetectorRunner(modelManager, featureManager, AnomalyDetectorSettings.MAX_PREVIEW_RESULTS);
    circuitBreaker = mock(ADCircuitBreakerService.class);
    when(circuitBreaker.isOpen()).thenReturn(false);
    action = new PreviewAnomalyDetectorTransportAction(Settings.EMPTY, mock(TransportService.class), clusterService, mock(ActionFilters.class), client(), runner, xContentRegistry(), circuitBreaker);
}
Also used : ClusterState(org.opensearch.cluster.ClusterState) Task(org.opensearch.tasks.Task) ClusterSettings(org.opensearch.common.settings.ClusterSettings) ADCircuitBreakerService(org.opensearch.ad.breaker.ADCircuitBreakerService) AnomalyDetectorRunner(org.opensearch.ad.AnomalyDetectorRunner) ModelManager(org.opensearch.ad.ml.ModelManager) ClusterService(org.opensearch.cluster.service.ClusterService) ClusterName(org.opensearch.cluster.ClusterName) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) Settings(org.opensearch.common.settings.Settings) AnomalyDetectorSettings(org.opensearch.ad.settings.AnomalyDetectorSettings) ClusterSettings(org.opensearch.common.settings.ClusterSettings) FeatureManager(org.opensearch.ad.feature.FeatureManager) HashSet(java.util.HashSet) Before(org.junit.Before)

Example 34 with ClusterSettings

use of org.opensearch.common.settings.ClusterSettings 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)

Example 35 with ClusterSettings

use of org.opensearch.common.settings.ClusterSettings in project anomaly-detection by opensearch-project.

the class GetAnomalyDetectorTests method setUp.

@Override
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);
    transportService = new TransportService(Settings.EMPTY, mock(Transport.class), null, TransportService.NOOP_TRANSPORT_INTERCEPTOR, x -> null, null, Collections.emptySet());
    nodeFilter = mock(DiscoveryNodeFilterer.class);
    actionFilters = mock(ActionFilters.class);
    client = mock(Client.class);
    when(client.threadPool()).thenReturn(threadPool);
    adTaskManager = mock(ADTaskManager.class);
    action = new GetAnomalyDetectorTransportAction(transportService, nodeFilter, actionFilters, clusterService, client, Settings.EMPTY, xContentRegistry(), adTaskManager);
    entity = Entity.createSingleAttributeEntity(categoryField, entityValue);
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Arrays(java.util.Arrays) DiscoveryNodeFilterer(org.opensearch.ad.util.DiscoveryNodeFilterer) BeforeClass(org.junit.BeforeClass) OpenSearchStatusException(org.opensearch.OpenSearchStatusException) AbstractADTest(org.opensearch.ad.AbstractADTest) AnomalyDetectorSettings(org.opensearch.ad.settings.AnomalyDetectorSettings) HashSet(java.util.HashSet) Transport(org.opensearch.transport.Transport) PlainActionFuture(org.opensearch.action.support.PlainActionFuture) Mockito.doAnswer(org.mockito.Mockito.doAnswer) ActionListener(org.opensearch.action.ActionListener) ClusterSettings(org.opensearch.common.settings.ClusterSettings) GetResponse(org.opensearch.action.get.GetResponse) AfterClass(org.junit.AfterClass) Client(org.opensearch.client.Client) ANOMALY_DETECTORS_INDEX(org.opensearch.ad.model.AnomalyDetector.ANOMALY_DETECTORS_INDEX) GetRequest(org.opensearch.action.get.GetRequest) IOException(java.io.IOException) ADTaskManager(org.opensearch.ad.task.ADTaskManager) Settings(org.opensearch.common.settings.Settings) Mockito.when(org.mockito.Mockito.when) TransportService(org.opensearch.transport.TransportService) ActionFilters(org.opensearch.action.support.ActionFilters) CommonErrorMessages(org.opensearch.ad.constant.CommonErrorMessages) Entity(org.opensearch.ad.model.Entity) ClusterService(org.opensearch.cluster.service.ClusterService) Collections(java.util.Collections) Mockito.mock(org.mockito.Mockito.mock) ClusterService(org.opensearch.cluster.service.ClusterService) ClusterSettings(org.opensearch.common.settings.ClusterSettings) DiscoveryNodeFilterer(org.opensearch.ad.util.DiscoveryNodeFilterer) TransportService(org.opensearch.transport.TransportService) ADTaskManager(org.opensearch.ad.task.ADTaskManager) ActionFilters(org.opensearch.action.support.ActionFilters) Client(org.opensearch.client.Client) HashSet(java.util.HashSet)

Aggregations

ClusterSettings (org.opensearch.common.settings.ClusterSettings)218 Settings (org.opensearch.common.settings.Settings)136 ClusterState (org.opensearch.cluster.ClusterState)70 ClusterService (org.opensearch.cluster.service.ClusterService)67 HashSet (java.util.HashSet)46 Metadata (org.opensearch.cluster.metadata.Metadata)37 Matchers.containsString (org.hamcrest.Matchers.containsString)36 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)36 Before (org.junit.Before)33 ArrayList (java.util.ArrayList)31 IndexMetadata (org.opensearch.cluster.metadata.IndexMetadata)31 ClusterName (org.opensearch.cluster.ClusterName)30 ThreadPool (org.opensearch.threadpool.ThreadPool)29 RoutingTable (org.opensearch.cluster.routing.RoutingTable)26 TestThreadPool (org.opensearch.threadpool.TestThreadPool)25 AtomicReference (java.util.concurrent.atomic.AtomicReference)23 Setting (org.opensearch.common.settings.Setting)23 ThreadContext (org.opensearch.common.util.concurrent.ThreadContext)23 IOException (java.io.IOException)19 BalancedShardsAllocator (org.opensearch.cluster.routing.allocation.allocator.BalancedShardsAllocator)19