Search in sources :

Example 36 with ClusterService

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

the class NoPowermockSearchFeatureDaoTests method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    serviceField = "service";
    hostField = "host";
    detector = mock(AnomalyDetector.class);
    when(detector.isMultientityDetector()).thenReturn(true);
    when(detector.getCategoryField()).thenReturn(Arrays.asList(new String[] { serviceField, hostField }));
    detectorId = "123";
    when(detector.getDetectorId()).thenReturn(detectorId);
    when(detector.getTimeField()).thenReturn("testTimeField");
    when(detector.getIndices()).thenReturn(Arrays.asList("testIndices"));
    IntervalTimeConfiguration detectionInterval = new IntervalTimeConfiguration(1, ChronoUnit.MINUTES);
    when(detector.getDetectionInterval()).thenReturn(detectionInterval);
    when(detector.getFilterQuery()).thenReturn(QueryBuilders.matchAllQuery());
    client = mock(Client.class);
    interpolator = new LinearUniformInterpolator(new SingleFeatureLinearUniformInterpolator());
    clientUtil = mock(ClientUtil.class);
    settings = Settings.EMPTY;
    ClusterSettings clusterSettings = new ClusterSettings(Settings.EMPTY, Collections.unmodifiableSet(new HashSet<>(Arrays.asList(AnomalyDetectorSettings.MAX_ENTITIES_FOR_PREVIEW, AnomalyDetectorSettings.PAGE_SIZE))));
    clusterService = mock(ClusterService.class);
    when(clusterService.getClusterSettings()).thenReturn(clusterSettings);
    clock = mock(Clock.class);
    searchFeatureDao = new SearchFeatureDao(client, // Important. Without this, ParseUtils cannot parse anything
    xContentRegistry(), interpolator, clientUtil, settings, clusterService, AnomalyDetectorSettings.NUM_SAMPLES_PER_TREE, clock, 1, 1, 60_000L);
    String app0 = "app_0";
    String server1 = "server_1";
    attrs1 = new HashMap<>();
    attrs1.put(serviceField, app0);
    attrs1.put(hostField, server1);
    String server2 = "server_2";
    attrs1 = new HashMap<>();
    attrs1.put(serviceField, app0);
    attrs1.put(hostField, server2);
}
Also used : ClusterSettings(org.opensearch.common.settings.ClusterSettings) ClientUtil(org.opensearch.ad.util.ClientUtil) IntervalTimeConfiguration(org.opensearch.ad.model.IntervalTimeConfiguration) Clock(java.time.Clock) AnomalyDetector(org.opensearch.ad.model.AnomalyDetector) SingleFeatureLinearUniformInterpolator(org.opensearch.ad.dataprocessor.SingleFeatureLinearUniformInterpolator) ClusterService(org.opensearch.cluster.service.ClusterService) SingleFeatureLinearUniformInterpolator(org.opensearch.ad.dataprocessor.SingleFeatureLinearUniformInterpolator) LinearUniformInterpolator(org.opensearch.ad.dataprocessor.LinearUniformInterpolator) Client(org.opensearch.client.Client) HashSet(java.util.HashSet)

Example 37 with ClusterService

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

the class RolloverTests method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    Client client = mock(Client.class);
    indicesClient = mock(IndicesAdminClient.class);
    AdminClient adminClient = mock(AdminClient.class);
    clusterService = mock(ClusterService.class);
    ClusterSettings clusterSettings = new ClusterSettings(Settings.EMPTY, Collections.unmodifiableSet(new HashSet<>(Arrays.asList(AnomalyDetectorSettings.AD_RESULT_HISTORY_MAX_DOCS_PER_SHARD, AnomalyDetectorSettings.AD_RESULT_HISTORY_ROLLOVER_PERIOD, AnomalyDetectorSettings.AD_RESULT_HISTORY_RETENTION_PERIOD, AnomalyDetectorSettings.MAX_PRIMARY_SHARDS))));
    clusterName = new ClusterName("test");
    when(clusterService.getClusterSettings()).thenReturn(clusterSettings);
    ThreadPool threadPool = mock(ThreadPool.class);
    Settings settings = Settings.EMPTY;
    when(client.admin()).thenReturn(adminClient);
    when(adminClient.indices()).thenReturn(indicesClient);
    DiscoveryNodeFilterer nodeFilter = mock(DiscoveryNodeFilterer.class);
    numberOfNodes = 2;
    when(nodeFilter.getNumberOfEligibleDataNodes()).thenReturn(numberOfNodes);
    adIndices = new AnomalyDetectionIndices(client, clusterService, threadPool, settings, nodeFilter, AnomalyDetectorSettings.MAX_UPDATE_RETRY_TIMES);
    clusterAdminClient = mock(ClusterAdminClient.class);
    when(adminClient.cluster()).thenReturn(clusterAdminClient);
    doAnswer(invocation -> {
        ClusterStateRequest clusterStateRequest = invocation.getArgument(0);
        assertEquals(AnomalyDetectionIndices.ALL_AD_RESULTS_INDEX_PATTERN, clusterStateRequest.indices()[0]);
        @SuppressWarnings("unchecked") ActionListener<ClusterStateResponse> listener = (ActionListener<ClusterStateResponse>) invocation.getArgument(1);
        listener.onResponse(new ClusterStateResponse(clusterName, clusterState, true));
        return null;
    }).when(clusterAdminClient).state(any(), any());
    defaultMaxDocs = AnomalyDetectorSettings.AD_RESULT_HISTORY_MAX_DOCS_PER_SHARD.getDefault(Settings.EMPTY);
}
Also used : ClusterSettings(org.opensearch.common.settings.ClusterSettings) DiscoveryNodeFilterer(org.opensearch.ad.util.DiscoveryNodeFilterer) ClusterAdminClient(org.opensearch.client.ClusterAdminClient) ClusterStateResponse(org.opensearch.action.admin.cluster.state.ClusterStateResponse) ClusterStateRequest(org.opensearch.action.admin.cluster.state.ClusterStateRequest) ThreadPool(org.opensearch.threadpool.ThreadPool) IndicesAdminClient(org.opensearch.client.IndicesAdminClient) ClusterService(org.opensearch.cluster.service.ClusterService) ActionListener(org.opensearch.action.ActionListener) ClusterName(org.opensearch.cluster.ClusterName) Client(org.opensearch.client.Client) AdminClient(org.opensearch.client.AdminClient) ClusterAdminClient(org.opensearch.client.ClusterAdminClient) IndicesAdminClient(org.opensearch.client.IndicesAdminClient) AnomalyDetectorSettings(org.opensearch.ad.settings.AnomalyDetectorSettings) ClusterSettings(org.opensearch.common.settings.ClusterSettings) Settings(org.opensearch.common.settings.Settings) AdminClient(org.opensearch.client.AdminClient) ClusterAdminClient(org.opensearch.client.ClusterAdminClient) IndicesAdminClient(org.opensearch.client.IndicesAdminClient) HashSet(java.util.HashSet)

Example 38 with ClusterService

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

the class ADDataMigratorTests method testMigrateDataWithNormalJobResponse_ExistingDetector_ExistingInternalError.

public void testMigrateDataWithNormalJobResponse_ExistingDetector_ExistingInternalError() {
    when(detectionIndices.doesAnomalyDetectorJobIndexExist()).thenReturn(true);
    when(detectionIndices.doesDetectorStateIndexExist()).thenReturn(true);
    String detectorId = randomAlphaOfLength(10);
    doAnswer(invocation -> {
        // Return correct AD job when search job index
        ActionListener<SearchResponse> listener = invocation.getArgument(1);
        SearchHit job1 = SearchHit.fromXContent(TestHelpers.parser(jobContent));
        SearchHits searchHits = new SearchHits(new SearchHit[] { job1 }, new TotalHits(2, TotalHits.Relation.EQUAL_TO), Float.NaN);
        InternalSearchResponse response = new InternalSearchResponse(searchHits, InternalAggregations.EMPTY, null, null, false, null, 1);
        SearchResponse searchResponse = new SearchResponse(response, null, 1, 1, 0, 100, ShardSearchFailure.EMPTY_ARRAY, SearchResponse.Clusters.EMPTY);
        listener.onResponse(searchResponse);
        return null;
    }).doAnswer(invocation -> {
        // Return null when search realtime tasks
        ActionListener<SearchResponse> listener = invocation.getArgument(1);
        listener.onResponse(null);
        return null;
    }).when(client).search(any(), any());
    doAnswer(invocation -> {
        // Return null when get detector internal error from index.
        ActionListener<GetResponse> listener = invocation.getArgument(1);
        XContentParser parser = TestHelpers.parser(internalError, false);
        GetResponse getResponse = GetResponse.fromXContent(parser);
        listener.onResponse(getResponse);
        return null;
    }).doAnswer(invocation -> {
        // Return correct detector when get detector index.
        ActionListener<GetResponse> listener = invocation.getArgument(1);
        XContentParser parser = TestHelpers.parser(detectorContent, false);
        GetResponse getResponse = GetResponse.fromXContent(parser);
        listener.onResponse(getResponse);
        return null;
    }).when(client).get(any(), any());
    doAnswer(invocation -> {
        ActionListener<IndexResponse> listener = invocation.getArgument(1);
        String taskId = randomAlphaOfLength(5);
        IndexResponse indexResponse = IndexResponse.fromXContent(TestHelpers.parser(indexResponseContent, false));
        listener.onResponse(indexResponse);
        return null;
    }).when(client).index(any(), any());
    DiscoveryNode localNode = createNode("localNodeId");
    doReturn(localNode).when(clusterService).localNode();
    adDataMigrator.migrateData();
    verify(adDataMigrator, times(2)).backfillRealtimeTask(any(), anyBoolean());
    verify(client, times(1)).index(any(), any());
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) IndexResponse(org.opensearch.action.index.IndexResponse) SearchHits(org.opensearch.search.SearchHits) ArgumentMatchers.anyBoolean(org.mockito.ArgumentMatchers.anyBoolean) Mockito.spy(org.mockito.Mockito.spy) XContentParser(org.opensearch.common.xcontent.XContentParser) ADUnitTestCase(org.opensearch.ad.ADUnitTestCase) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) ANOMALY_DETECTOR_JOB_INDEX(org.opensearch.ad.model.AnomalyDetectorJob.ANOMALY_DETECTOR_JOB_INDEX) Mockito.doAnswer(org.mockito.Mockito.doAnswer) InternalAggregations(org.opensearch.search.aggregations.InternalAggregations) ActionListener(org.opensearch.action.ActionListener) SearchResponse(org.opensearch.action.search.SearchResponse) Mockito.doReturn(org.mockito.Mockito.doReturn) GetResponse(org.opensearch.action.get.GetResponse) Before(org.junit.Before) Client(org.opensearch.client.Client) SearchHit(org.opensearch.search.SearchHit) IndexNotFoundException(org.opensearch.index.IndexNotFoundException) Mockito.times(org.mockito.Mockito.times) AnomalyDetectionIndices(org.opensearch.ad.indices.AnomalyDetectionIndices) Mockito.when(org.mockito.Mockito.when) CreateIndexResponse(org.opensearch.action.admin.indices.create.CreateIndexResponse) DETECTION_STATE_INDEX(org.opensearch.ad.constant.CommonName.DETECTION_STATE_INDEX) InternalSearchResponse(org.opensearch.search.internal.InternalSearchResponse) Mockito.verify(org.mockito.Mockito.verify) TotalHits(org.apache.lucene.search.TotalHits) ShardId(org.opensearch.index.shard.ShardId) Mockito.never(org.mockito.Mockito.never) TestHelpers(org.opensearch.ad.TestHelpers) ShardSearchFailure(org.opensearch.action.search.ShardSearchFailure) NamedXContentRegistry(org.opensearch.common.xcontent.NamedXContentRegistry) ClusterService(org.opensearch.cluster.service.ClusterService) ResourceAlreadyExistsException(org.opensearch.ResourceAlreadyExistsException) NoShardAvailableActionException(org.opensearch.action.NoShardAvailableActionException) Mockito.mock(org.mockito.Mockito.mock) TotalHits(org.apache.lucene.search.TotalHits) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) SearchHit(org.opensearch.search.SearchHit) GetResponse(org.opensearch.action.get.GetResponse) SearchResponse(org.opensearch.action.search.SearchResponse) InternalSearchResponse(org.opensearch.search.internal.InternalSearchResponse) ActionListener(org.opensearch.action.ActionListener) IndexResponse(org.opensearch.action.index.IndexResponse) CreateIndexResponse(org.opensearch.action.admin.indices.create.CreateIndexResponse) SearchHits(org.opensearch.search.SearchHits) XContentParser(org.opensearch.common.xcontent.XContentParser) InternalSearchResponse(org.opensearch.search.internal.InternalSearchResponse)

Example 39 with ClusterService

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

the class HashRingTests method setUp.

@Override
@Before
public void setUp() throws Exception {
    super.setUp();
    localNodeId = "localNode";
    localNode = createNode(localNodeId, "127.0.0.1", 9200, emptyMap());
    newNodeId = "newNode";
    newNode = createNode(newNodeId, "127.0.0.2", 9201, emptyMap());
    warmNodeId = "warmNode";
    warmNode = createNode(warmNodeId, "127.0.0.3", 9202, ImmutableMap.of(CommonName.BOX_TYPE_KEY, CommonName.WARM_BOX_TYPE));
    settings = Settings.builder().put(COOLDOWN_MINUTES.getKey(), TimeValue.timeValueSeconds(5)).build();
    ClusterSettings clusterSettings = clusterSetting(settings, COOLDOWN_MINUTES);
    clusterService = spy(new ClusterService(settings, clusterSettings, null));
    nodeFilter = spy(new DiscoveryNodeFilterer(clusterService));
    client = mock(Client.class);
    dataMigrator = mock(ADDataMigrator.class);
    clock = mock(Clock.class);
    when(clock.millis()).thenReturn(700000L);
    delta = mock(DiscoveryNodes.Delta.class);
    adminClient = mock(AdminClient.class);
    when(client.admin()).thenReturn(adminClient);
    clusterAdminClient = mock(ClusterAdminClient.class);
    when(adminClient.cluster()).thenReturn(clusterAdminClient);
    String modelId = "123_model_threshold";
    modelManager = mock(ModelManager.class);
    doAnswer(invocation -> {
        Set<String> res = new HashSet<>();
        res.add(modelId);
        return res;
    }).when(modelManager).getAllModelIds();
    hashRing = spy(new HashRing(nodeFilter, clock, settings, client, clusterService, dataMigrator, modelManager));
}
Also used : ClusterSettings(org.opensearch.common.settings.ClusterSettings) DiscoveryNodeFilterer(org.opensearch.ad.util.DiscoveryNodeFilterer) ClusterAdminClient(org.opensearch.client.ClusterAdminClient) Clock(java.time.Clock) ModelManager(org.opensearch.ad.ml.ModelManager) ClusterService(org.opensearch.cluster.service.ClusterService) Client(org.opensearch.client.Client) AdminClient(org.opensearch.client.AdminClient) ClusterAdminClient(org.opensearch.client.ClusterAdminClient) AdminClient(org.opensearch.client.AdminClient) ClusterAdminClient(org.opensearch.client.ClusterAdminClient) HashSet(java.util.HashSet) Before(org.junit.Before)

Example 40 with ClusterService

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

the class InitAnomalyDetectionIndicesTests method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    client = mock(Client.class);
    indicesClient = mock(IndicesAdminClient.class);
    AdminClient adminClient = mock(AdminClient.class);
    when(client.admin()).thenReturn(adminClient);
    when(adminClient.indices()).thenReturn(indicesClient);
    clusterService = mock(ClusterService.class);
    threadPool = mock(ThreadPool.class);
    numberOfHotNodes = 4;
    nodeFilter = mock(DiscoveryNodeFilterer.class);
    when(nodeFilter.getNumberOfEligibleDataNodes()).thenReturn(numberOfHotNodes);
    Settings settings = Settings.EMPTY;
    ClusterSettings clusterSettings = new ClusterSettings(settings, Collections.unmodifiableSet(new HashSet<>(Arrays.asList(AnomalyDetectorSettings.AD_RESULT_HISTORY_MAX_DOCS_PER_SHARD, AnomalyDetectorSettings.AD_RESULT_HISTORY_ROLLOVER_PERIOD, AnomalyDetectorSettings.AD_RESULT_HISTORY_RETENTION_PERIOD, AnomalyDetectorSettings.MAX_PRIMARY_SHARDS))));
    clusterName = new ClusterName("test");
    when(clusterService.getClusterSettings()).thenReturn(clusterSettings);
    clusterState = ClusterState.builder(clusterName).metadata(Metadata.builder().build()).build();
    when(clusterService.state()).thenReturn(clusterState);
    adIndices = new AnomalyDetectionIndices(client, clusterService, threadPool, settings, nodeFilter, AnomalyDetectorSettings.MAX_UPDATE_RETRY_TIMES);
}
Also used : ClusterService(org.opensearch.cluster.service.ClusterService) DiscoveryNodeFilterer(org.opensearch.ad.util.DiscoveryNodeFilterer) ClusterSettings(org.opensearch.common.settings.ClusterSettings) ThreadPool(org.opensearch.threadpool.ThreadPool) ClusterName(org.opensearch.cluster.ClusterName) IndicesAdminClient(org.opensearch.client.IndicesAdminClient) Client(org.opensearch.client.Client) AdminClient(org.opensearch.client.AdminClient) IndicesAdminClient(org.opensearch.client.IndicesAdminClient) AnomalyDetectorSettings(org.opensearch.ad.settings.AnomalyDetectorSettings) ClusterSettings(org.opensearch.common.settings.ClusterSettings) Settings(org.opensearch.common.settings.Settings) AdminClient(org.opensearch.client.AdminClient) IndicesAdminClient(org.opensearch.client.IndicesAdminClient) HashSet(java.util.HashSet)

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