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);
}
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);
}
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());
}
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));
}
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);
}
Aggregations