use of org.opensearch.client.Client 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);
}
use of org.opensearch.client.Client in project anomaly-detection by opensearch-project.
the class NodeStateManagerTests method setUp.
@Override
@Before
public void setUp() throws Exception {
super.setUp();
client = mock(Client.class);
settings = Settings.builder().put("plugins.anomaly_detection.max_retry_for_unresponsive_node", 3).put("plugins.anomaly_detection.ad_mute_minutes", TimeValue.timeValueMinutes(10)).build();
clock = mock(Clock.class);
duration = Duration.ofHours(1);
context = TestHelpers.createThreadPool();
throttler = new Throttler(clock);
clientUtil = new ClientUtil(Settings.EMPTY, client, throttler, mock(ThreadPool.class));
Set<Setting<?>> nodestateSetting = new HashSet<>(ClusterSettings.BUILT_IN_CLUSTER_SETTINGS);
nodestateSetting.add(MAX_RETRY_FOR_UNRESPONSIVE_NODE);
nodestateSetting.add(BACKOFF_MINUTES);
clusterSettings = new ClusterSettings(Settings.EMPTY, nodestateSetting);
DiscoveryNode discoveryNode = new DiscoveryNode("node1", OpenSearchTestCase.buildNewFakeTransportAddress(), Collections.emptyMap(), DiscoveryNodeRole.BUILT_IN_ROLES, Version.CURRENT);
clusterService = ClusterServiceUtils.createClusterService(threadPool, discoveryNode, clusterSettings);
stateManager = new NodeStateManager(client, xContentRegistry(), settings, clientUtil, clock, duration, clusterService);
checkpointResponse = mock(GetResponse.class);
}
use of org.opensearch.client.Client in project anomaly-detection by opensearch-project.
the class NodeStateManagerTests method testRepeatedGetAnomalyDetector.
/**
* Test that we caches anomaly detector definition after the first call
* @throws IOException if client throws exception
* @throws InterruptedException if the current thread is interrupted while waiting
*/
@SuppressWarnings("unchecked")
public void testRepeatedGetAnomalyDetector() throws IOException, InterruptedException {
String detectorId = setupDetector();
final CountDownLatch inProgressLatch = new CountDownLatch(2);
stateManager.getAnomalyDetector(detectorId, ActionListener.wrap(asDetector -> {
assertEquals(detectorToCheck, asDetector.get());
inProgressLatch.countDown();
}, exception -> {
assertTrue(false);
inProgressLatch.countDown();
}));
stateManager.getAnomalyDetector(detectorId, ActionListener.wrap(asDetector -> {
assertEquals(detectorToCheck, asDetector.get());
inProgressLatch.countDown();
}, exception -> {
assertTrue(false);
inProgressLatch.countDown();
}));
assertTrue(inProgressLatch.await(100, TimeUnit.SECONDS));
verify(client, times(1)).get(any(), any(ActionListener.class));
}
use of org.opensearch.client.Client 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.client.Client in project anomaly-detection by opensearch-project.
the class ADDataMigratorTests method testMigrateDataWithNormalJobResponseButMissingDetector.
public void testMigrateDataWithNormalJobResponseButMissingDetector() {
when(detectionIndices.doesAnomalyDetectorJobIndexExist()).thenReturn(true);
when(detectionIndices.doesDetectorStateIndexExist()).thenReturn(true);
doAnswer(invocation -> {
// Return correct AD job when search job index
ActionListener<SearchResponse> listener = invocation.getArgument(1);
String detectorId = randomAlphaOfLength(10);
SearchHit job = SearchHit.fromXContent(TestHelpers.parser(jobContent));
SearchHits searchHits = new SearchHits(new SearchHit[] { job }, 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());
// Return null when get detector and internal error from index.
doAnswer(invocation -> {
ActionListener<SearchResponse> listener = invocation.getArgument(1);
listener.onResponse(null);
return null;
}).when(client).get(any(), any());
adDataMigrator.migrateData();
verify(adDataMigrator, times(2)).backfillRealtimeTask(any(), anyBoolean());
verify(client, never()).index(any(), any());
}
Aggregations