Search in sources :

Example 61 with ClusterService

use of org.opensearch.cluster.service.ClusterService in project anomaly-detection by opensearch-project.

the class ColdEntityWorkerTests method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    clusterService = mock(ClusterService.class);
    Settings settings = Settings.builder().put(AnomalyDetectorSettings.CHECKPOINT_READ_QUEUE_BATCH_SIZE.getKey(), 1).build();
    ClusterSettings clusterSettings = new ClusterSettings(settings, Collections.unmodifiableSet(new HashSet<>(Arrays.asList(AnomalyDetectorSettings.EXPECTED_COLD_ENTITY_EXECUTION_TIME_IN_SECS, AnomalyDetectorSettings.COLD_ENTITY_QUEUE_MAX_HEAP_PERCENT, AnomalyDetectorSettings.CHECKPOINT_READ_QUEUE_BATCH_SIZE))));
    when(clusterService.getClusterSettings()).thenReturn(clusterSettings);
    readWorker = mock(CheckpointReadWorker.class);
    // Integer.MAX_VALUE makes a huge heap
    coldWorker = new ColdEntityWorker(Integer.MAX_VALUE, AnomalyDetectorSettings.ENTITY_FEATURE_REQUEST_SIZE_IN_BYTES, AnomalyDetectorSettings.COLD_ENTITY_QUEUE_MAX_HEAP_PERCENT, clusterService, new Random(42), mock(ADCircuitBreakerService.class), threadPool, settings, AnomalyDetectorSettings.MAX_QUEUED_TASKS_RATIO, clock, AnomalyDetectorSettings.MEDIUM_SEGMENT_PRUNE_RATIO, AnomalyDetectorSettings.LOW_SEGMENT_PRUNE_RATIO, AnomalyDetectorSettings.MAINTENANCE_FREQ_CONSTANT, readWorker, AnomalyDetectorSettings.HOURLY_MAINTENANCE, nodeStateManager);
    request = new EntityFeatureRequest(Integer.MAX_VALUE, detectorId, RequestPriority.LOW, entity, new double[] { 0 }, 0);
    request2 = new EntityFeatureRequest(Integer.MAX_VALUE, detectorId, RequestPriority.LOW, entity2, new double[] { 0 }, 0);
    invalidRequest = new EntityFeatureRequest(Integer.MAX_VALUE, detectorId, RequestPriority.MEDIUM, entity2, new double[] { 0 }, 0);
    requests = new ArrayList<>();
    requests.add(request);
    requests.add(request2);
    requests.add(invalidRequest);
    doAnswer(invocation -> {
        Runnable runnable = invocation.getArgument(0);
        runnable.run();
        TimeValue value = invocation.getArgument(1);
        // since we have only 1 request each time
        long expectedExecutionPerRequestMilli = 1000 * AnomalyDetectorSettings.EXPECTED_COLD_ENTITY_EXECUTION_TIME_IN_SECS.getDefault(Settings.EMPTY);
        long delay = value.getMillis();
        assertTrue(delay >= expectedExecutionPerRequestMilli);
        assertTrue(delay <= expectedExecutionPerRequestMilli * 2);
        return null;
    }).when(threadPool).schedule(any(), any(), any());
}
Also used : ClusterService(org.opensearch.cluster.service.ClusterService) ClusterSettings(org.opensearch.common.settings.ClusterSettings) Random(java.util.Random) Settings(org.opensearch.common.settings.Settings) AnomalyDetectorSettings(org.opensearch.ad.settings.AnomalyDetectorSettings) ClusterSettings(org.opensearch.common.settings.ClusterSettings) TimeValue(org.opensearch.common.unit.TimeValue) HashSet(java.util.HashSet)

Example 62 with ClusterService

use of org.opensearch.cluster.service.ClusterService 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 63 with ClusterService

use of org.opensearch.cluster.service.ClusterService 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 64 with ClusterService

use of org.opensearch.cluster.service.ClusterService in project anomaly-detection by opensearch-project.

the class MultiEntityResultTests method setUpTransportInterceptor.

private <T extends TransportResponse> void setUpTransportInterceptor(Function<TransportResponseHandler<T>, TransportResponseHandler<T>> interceptor) {
    entityResultInterceptor = new TransportInterceptor() {

        @Override
        public AsyncSender interceptSender(AsyncSender sender) {
            return new AsyncSender() {

                @SuppressWarnings("unchecked")
                @Override
                public <T2 extends TransportResponse> void sendRequest(Transport.Connection connection, String action, TransportRequest request, TransportRequestOptions options, TransportResponseHandler<T2> handler) {
                    if (action.equals(EntityResultAction.NAME)) {
                        sender.sendRequest(connection, action, request, options, interceptor.apply((TransportResponseHandler<T>) handler));
                    } else {
                        sender.sendRequest(connection, action, request, options, handler);
                    }
                }
            };
        }
    };
    // we start support multi-category fields since 1.1
    // Set version to 1.1 will force the outbound/inbound message to use 1.1 version
    setupTestNodes(entityResultInterceptor, 5, settings, BwcTests.V_1_1_0, MAX_ENTITIES_PER_QUERY, PAGE_SIZE);
    TransportService realTransportService = testNodes[0].transportService;
    ClusterService realClusterService = testNodes[0].clusterService;
    action = new AnomalyResultTransportAction(new ActionFilters(Collections.emptySet()), realTransportService, settings, client, stateManager, featureQuery, normalModelManager, hashRing, realClusterService, indexNameResolver, adCircuitBreakerService, adStats, threadPool, xContentRegistry(), adTaskManager);
}
Also used : TransportInterceptor(org.opensearch.transport.TransportInterceptor) TransportRequest(org.opensearch.transport.TransportRequest) Matchers.containsString(org.hamcrest.Matchers.containsString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ActionFilters(org.opensearch.action.support.ActionFilters) ClusterService(org.opensearch.cluster.service.ClusterService) TransportService(org.opensearch.transport.TransportService) TransportRequestOptions(org.opensearch.transport.TransportRequestOptions) Transport(org.opensearch.transport.Transport)

Example 65 with ClusterService

use of org.opensearch.cluster.service.ClusterService 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)

Aggregations

ClusterService (org.opensearch.cluster.service.ClusterService)296 ThreadPool (org.opensearch.threadpool.ThreadPool)123 Settings (org.opensearch.common.settings.Settings)115 ClusterState (org.opensearch.cluster.ClusterState)106 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)103 TestThreadPool (org.opensearch.threadpool.TestThreadPool)93 TransportService (org.opensearch.transport.TransportService)86 ClusterSettings (org.opensearch.common.settings.ClusterSettings)76 ActionListener (org.opensearch.action.ActionListener)75 Before (org.junit.Before)66 ActionFilters (org.opensearch.action.support.ActionFilters)66 CountDownLatch (java.util.concurrent.CountDownLatch)65 HashSet (java.util.HashSet)63 TimeValue (org.opensearch.common.unit.TimeValue)61 IOException (java.io.IOException)56 IndexMetadata (org.opensearch.cluster.metadata.IndexMetadata)53 OpenSearchTestCase (org.opensearch.test.OpenSearchTestCase)48 Collections (java.util.Collections)47 ClusterName (org.opensearch.cluster.ClusterName)47 Metadata (org.opensearch.cluster.metadata.Metadata)47