use of org.opensearch.client.Client 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.client.Client 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);
}
use of org.opensearch.client.Client in project anomaly-detection by opensearch-project.
the class HourlyCronTests method templateHourlyCron.
@SuppressWarnings("unchecked")
public void templateHourlyCron(HourlyCronTestExecutionMode mode) {
super.setUpLog4jForJUnit(HourlyCron.class);
ClusterService clusterService = mock(ClusterService.class);
ClusterState state = ClusterCreation.state(1);
when(clusterService.state()).thenReturn(state);
HashMap<String, String> ignoredAttributes = new HashMap<String, String>();
ignoredAttributes.put(CommonName.BOX_TYPE_KEY, CommonName.WARM_BOX_TYPE);
DiscoveryNodeFilterer nodeFilter = new DiscoveryNodeFilterer(clusterService);
Client client = mock(Client.class);
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 == 3);
assertTrue(args[2] instanceof ActionListener);
ActionListener<CronResponse> listener = (ActionListener<CronResponse>) args[2];
if (mode == HourlyCronTestExecutionMode.NODE_FAIL) {
listener.onResponse(new CronResponse(new ClusterName("test"), Collections.singletonList(new CronNodeResponse(state.nodes().getLocalNode())), Collections.singletonList(new FailedNodeException("foo0", "blah", new OpenSearchException("bar")))));
} else if (mode == HourlyCronTestExecutionMode.ALL_FAIL) {
listener.onFailure(new OpenSearchException("bar"));
} else {
CronNodeResponse nodeResponse = new CronNodeResponse(state.nodes().getLocalNode());
BytesStreamOutput nodeResponseOut = new BytesStreamOutput();
nodeResponseOut.setVersion(Version.CURRENT);
nodeResponse.writeTo(nodeResponseOut);
StreamInput siNode = nodeResponseOut.bytes().streamInput();
CronNodeResponse nodeResponseRead = new CronNodeResponse(siNode);
CronResponse response = new CronResponse(new ClusterName("test"), Collections.singletonList(nodeResponseRead), Collections.EMPTY_LIST);
BytesStreamOutput out = new BytesStreamOutput();
out.setVersion(Version.CURRENT);
response.writeTo(out);
StreamInput si = out.bytes().streamInput();
CronResponse responseRead = new CronResponse(si);
listener.onResponse(responseRead);
}
return null;
}).when(client).execute(eq(CronAction.INSTANCE), any(), any());
HourlyCron cron = new HourlyCron(client, nodeFilter);
cron.run();
Logger LOG = LogManager.getLogger(HourlyCron.class);
LOG.info(testAppender.messages);
if (mode == HourlyCronTestExecutionMode.NODE_FAIL) {
assertTrue(testAppender.containsMessage(HourlyCron.NODE_EXCEPTION_LOG_MSG));
} else if (mode == HourlyCronTestExecutionMode.ALL_FAIL) {
assertTrue(testAppender.containsMessage(HourlyCron.EXCEPTION_LOG_MSG));
} else {
assertTrue(testAppender.containsMessage(HourlyCron.SUCCEEDS_LOG_MSG));
}
super.tearDownLog4jForJUnit();
}
use of org.opensearch.client.Client in project anomaly-detection by opensearch-project.
the class EntityProfileRunnerTests method testNotMultiEntityDetector.
@SuppressWarnings("unchecked")
public void testNotMultiEntityDetector() throws IOException, InterruptedException {
detector = TestHelpers.randomAnomalyDetectorWithInterval(new IntervalTimeConfiguration(detectorIntervalMin, ChronoUnit.MINUTES));
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));
}
return null;
}).when(client).get(any(), any());
final CountDownLatch inProgressLatch = new CountDownLatch(1);
runner.profile(detectorId, entity, state, ActionListener.wrap(response -> {
assertTrue("Should not reach here", false);
inProgressLatch.countDown();
}, exception -> {
assertTrue(exception.getMessage().contains(EntityProfileRunner.NOT_HC_DETECTOR_ERR_MSG));
inProgressLatch.countDown();
}));
assertTrue(inProgressLatch.await(100, TimeUnit.SECONDS));
}
use of org.opensearch.client.Client in project anomaly-detection by opensearch-project.
the class EntityProfileRunnerTests method testJobIndexNotFound.
@SuppressWarnings("unchecked")
public void testJobIndexNotFound() throws InterruptedException {
setUpExecuteEntityProfileAction(InittedEverResultStatus.INITTED);
final CountDownLatch inProgressLatch = new CountDownLatch(1);
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(ANOMALY_DETECTOR_JOB_INDEX)) {
listener.onFailure(new IndexNotFoundException(ANOMALY_DETECTOR_JOB_INDEX));
}
return null;
}).when(client).get(any(), any());
EntityProfile expectedProfile = new EntityProfile.Builder().build();
runner.profile(detectorId, entity, initNInfo, ActionListener.wrap(response -> {
assertEquals(expectedProfile, response);
inProgressLatch.countDown();
}, exception -> {
LOG.error("Unexpected error", exception);
assertTrue("Should not reach here", false);
inProgressLatch.countDown();
}));
assertTrue(inProgressLatch.await(100, TimeUnit.SECONDS));
}
Aggregations