use of org.opensearch.client.Client in project anomaly-detection by opensearch-project.
the class ParseUtils method getDetector.
/**
* If filterByEnabled is true, get detector and check if the user has permissions to access the detector,
* then execute function; otherwise, get detector and execute function
* @param requestUser user from request
* @param detectorId detector id
* @param listener action listener
* @param function consumer function
* @param client client
* @param clusterService cluster service
* @param xContentRegistry XContent registry
* @param filterByBackendRole filter by backend role or not
*/
public static void getDetector(User requestUser, String detectorId, ActionListener listener, Consumer<AnomalyDetector> function, Client client, ClusterService clusterService, NamedXContentRegistry xContentRegistry, boolean filterByBackendRole) {
if (clusterService.state().metadata().indices().containsKey(AnomalyDetector.ANOMALY_DETECTORS_INDEX)) {
GetRequest request = new GetRequest(AnomalyDetector.ANOMALY_DETECTORS_INDEX).id(detectorId);
client.get(request, ActionListener.wrap(response -> onGetAdResponse(response, requestUser, detectorId, listener, function, xContentRegistry, filterByBackendRole), exception -> {
logger.error("Failed to get anomaly detector: " + detectorId, exception);
listener.onFailure(exception);
}));
} else {
listener.onFailure(new IndexNotFoundException(AnomalyDetector.ANOMALY_DETECTORS_INDEX));
}
}
use of org.opensearch.client.Client in project anomaly-detection by opensearch-project.
the class CheckpointDeleteTests method setUp.
@SuppressWarnings("unchecked")
@Override
@Before
public void setUp() throws Exception {
super.setUp();
super.setUpLog4jForJUnit(CheckpointDao.class);
client = mock(Client.class);
clientUtil = mock(ClientUtil.class);
gson = null;
indexUtil = mock(AnomalyDetectionIndices.class);
detectorId = "123";
maxCheckpointBytes = 1_000_000;
RandomCutForestMapper mapper = mock(RandomCutForestMapper.class);
V1JsonToV2StateConverter converter = mock(V1JsonToV2StateConverter.class);
objectPool = mock(GenericObjectPool.class);
int deserializeRCFBufferSize = 512;
anomalyRate = 0.005;
checkpointDao = new CheckpointDao(client, clientUtil, CommonName.CHECKPOINT_INDEX_NAME, gson, mapper, converter, ercfMapper, ercfSchema, HybridThresholdingModel.class, indexUtil, maxCheckpointBytes, objectPool, deserializeRCFBufferSize, anomalyRate);
}
use of org.opensearch.client.Client in project anomaly-detection by opensearch-project.
the class EntityColdStarterTests method setUp.
@SuppressWarnings("unchecked")
@Override
public void setUp() throws Exception {
super.setUp();
numMinSamples = AnomalyDetectorSettings.NUM_MIN_SAMPLES;
clock = mock(Clock.class);
when(clock.instant()).thenReturn(Instant.now());
threadPool = mock(ThreadPool.class);
setUpADThreadPool(threadPool);
settings = Settings.EMPTY;
Client client = mock(Client.class);
clientUtil = mock(ClientUtil.class);
detector = TestHelpers.AnomalyDetectorBuilder.newInstance().setDetectionInterval(new IntervalTimeConfiguration(1, ChronoUnit.MINUTES)).setCategoryFields(ImmutableList.of(randomAlphaOfLength(5))).build();
job = TestHelpers.randomAnomalyDetectorJob(true, Instant.ofEpochMilli(1602401500000L), null);
doAnswer(invocation -> {
GetRequest request = invocation.getArgument(0);
ActionListener<GetResponse> listener = invocation.getArgument(2);
if (request.index().equals(AnomalyDetectorJob.ANOMALY_DETECTOR_JOB_INDEX)) {
listener.onResponse(TestHelpers.createGetResponse(job, detectorId, AnomalyDetectorJob.ANOMALY_DETECTOR_JOB_INDEX));
} else {
listener.onResponse(TestHelpers.createGetResponse(detector, detectorId, AnomalyDetector.ANOMALY_DETECTORS_INDEX));
}
return null;
}).when(clientUtil).asyncRequest(any(GetRequest.class), any(), any(ActionListener.class));
Set<Setting<?>> nodestateSetting = new HashSet<>(ClusterSettings.BUILT_IN_CLUSTER_SETTINGS);
nodestateSetting.add(MAX_RETRY_FOR_UNRESPONSIVE_NODE);
nodestateSetting.add(BACKOFF_MINUTES);
ClusterSettings clusterSettings = new ClusterSettings(Settings.EMPTY, nodestateSetting);
DiscoveryNode discoveryNode = new DiscoveryNode("node1", OpenSearchTestCase.buildNewFakeTransportAddress(), Collections.emptyMap(), DiscoveryNodeRole.BUILT_IN_ROLES, Version.CURRENT);
ClusterService clusterService = ClusterServiceUtils.createClusterService(threadPool, discoveryNode, clusterSettings);
stateManager = new NodeStateManager(client, xContentRegistry(), settings, clientUtil, clock, AnomalyDetectorSettings.HOURLY_MAINTENANCE, clusterService);
SingleFeatureLinearUniformInterpolator singleFeatureLinearUniformInterpolator = new IntegerSensitiveSingleFeatureLinearUniformInterpolator();
interpolator = new LinearUniformInterpolator(singleFeatureLinearUniformInterpolator);
searchFeatureDao = mock(SearchFeatureDao.class);
checkpoint = mock(CheckpointDao.class);
featureManager = new FeatureManager(searchFeatureDao, interpolator, clock, AnomalyDetectorSettings.MAX_TRAIN_SAMPLE, AnomalyDetectorSettings.MAX_SAMPLE_STRIDE, AnomalyDetectorSettings.TRAIN_SAMPLE_TIME_RANGE_IN_HOURS, AnomalyDetectorSettings.MIN_TRAIN_SAMPLES, AnomalyDetectorSettings.MAX_SHINGLE_PROPORTION_MISSING, AnomalyDetectorSettings.MAX_IMPUTATION_NEIGHBOR_DISTANCE, AnomalyDetectorSettings.PREVIEW_SAMPLE_RATE, AnomalyDetectorSettings.MAX_PREVIEW_SAMPLES, AnomalyDetectorSettings.HOURLY_MAINTENANCE, threadPool, AnomalyDetectorPlugin.AD_THREAD_POOL_NAME);
checkpointWriteQueue = mock(CheckpointWriteWorker.class);
rcfSeed = 2051L;
entityColdStarter = new EntityColdStarter(clock, threadPool, stateManager, AnomalyDetectorSettings.NUM_SAMPLES_PER_TREE, AnomalyDetectorSettings.NUM_TREES, AnomalyDetectorSettings.TIME_DECAY, numMinSamples, AnomalyDetectorSettings.MAX_SAMPLE_STRIDE, AnomalyDetectorSettings.MAX_TRAIN_SAMPLE, interpolator, searchFeatureDao, AnomalyDetectorSettings.THRESHOLD_MIN_PVALUE, featureManager, settings, AnomalyDetectorSettings.HOURLY_MAINTENANCE, checkpointWriteQueue, rcfSeed, AnomalyDetectorSettings.MAX_COLD_START_ROUNDS);
detectorId = "123";
modelId = "123_entity_abc";
entityName = "abc";
priority = 0.3f;
entity = Entity.createSingleAttributeEntity("field", entityName);
released = new AtomicBoolean();
inProgressLatch = new CountDownLatch(1);
releaseSemaphore = () -> {
released.set(true);
inProgressLatch.countDown();
};
listener = ActionListener.wrap(releaseSemaphore);
modelManager = new ModelManager(mock(CheckpointDao.class), mock(Clock.class), AnomalyDetectorSettings.NUM_TREES, AnomalyDetectorSettings.NUM_SAMPLES_PER_TREE, AnomalyDetectorSettings.TIME_DECAY, AnomalyDetectorSettings.NUM_MIN_SAMPLES, AnomalyDetectorSettings.THRESHOLD_MIN_PVALUE, AnomalyDetectorSettings.MIN_PREVIEW_SIZE, AnomalyDetectorSettings.HOURLY_MAINTENANCE, AnomalyDetectorSettings.HOURLY_MAINTENANCE, entityColdStarter, mock(FeatureManager.class), mock(MemoryTracker.class));
}
use of org.opensearch.client.Client in project anomaly-detection by opensearch-project.
the class MultiEntityResultTests method testNullFeatures.
public void testNullFeatures() throws InterruptedException {
final CountDownLatch inProgressLatch = new CountDownLatch(1);
CompositeAggregation emptyComposite = mock(CompositeAggregation.class);
when(emptyComposite.getName()).thenReturn(null);
when(emptyComposite.afterKey()).thenReturn(null);
// empty bucket
when(emptyComposite.getBuckets()).thenAnswer((Answer<List<CompositeAggregation.Bucket>>) invocation -> {
return new ArrayList<CompositeAggregation.Bucket>();
});
Aggregations emptyAggs = new Aggregations(Collections.singletonList(emptyComposite));
SearchResponseSections emptySections = new SearchResponseSections(SearchHits.empty(), emptyAggs, null, false, null, null, 1);
SearchResponse nullResponse = new SearchResponse(emptySections, null, 1, 1, 0, 0, ShardSearchFailure.EMPTY_ARRAY, Clusters.EMPTY);
doAnswer(invocation -> {
ActionListener<SearchResponse> listener = invocation.getArgument(1);
listener.onResponse(nullResponse);
inProgressLatch.countDown();
return null;
}).when(client).search(any(), any());
PlainActionFuture<AnomalyResultResponse> listener = new PlainActionFuture<>();
action.doExecute(null, request, listener);
AnomalyResultResponse response = listener.actionGet(10000L);
assertEquals(Double.NaN, response.getAnomalyGrade(), 0.01);
assertTrue(inProgressLatch.await(10000L, TimeUnit.MILLISECONDS));
PlainActionFuture<AnomalyResultResponse> listener2 = new PlainActionFuture<>();
action.doExecute(null, request, listener2);
AnomalyResultResponse response2 = listener2.actionGet(10000L);
assertEquals(Double.NaN, response2.getAnomalyGrade(), 0.01);
}
use of org.opensearch.client.Client in project anomaly-detection by opensearch-project.
the class DeleteTests method setUp.
@Override
@Before
public void setUp() throws Exception {
super.setUp();
node1 = "node1";
node2 = "node2";
nodename1 = "nodename1";
nodename2 = "nodename2";
DiscoveryNode discoveryNode1 = new DiscoveryNode(nodename1, node1, new TransportAddress(TransportAddress.META_ADDRESS, 9300), emptyMap(), emptySet(), Version.CURRENT);
DiscoveryNode discoveryNode2 = new DiscoveryNode(nodename2, node2, new TransportAddress(TransportAddress.META_ADDRESS, 9301), emptyMap(), emptySet(), Version.CURRENT);
List<DiscoveryNode> discoveryNodes = new ArrayList<DiscoveryNode>(2);
discoveryNodes.add(discoveryNode1);
discoveryNodes.add(discoveryNode2);
DeleteModelNodeResponse nodeResponse1 = new DeleteModelNodeResponse(discoveryNode1);
DeleteModelNodeResponse nodeResponse2 = new DeleteModelNodeResponse(discoveryNode2);
deleteModelResponse = new ArrayList<>();
deleteModelResponse.add(nodeResponse1);
deleteModelResponse.add(nodeResponse2);
failures = new ArrayList<>();
failures.add(new FailedNodeException("node3", "blah", new OpenSearchException("foo")));
response = new DeleteModelResponse(new ClusterName("Cluster"), deleteModelResponse, failures);
clusterService = mock(ClusterService.class);
when(clusterService.localNode()).thenReturn(discoveryNode1);
when(clusterService.state()).thenReturn(ClusterCreation.state(new ClusterName("test"), discoveryNode2, discoveryNode1, discoveryNodes));
transportService = mock(TransportService.class);
threadPool = mock(ThreadPool.class);
actionFilters = mock(ActionFilters.class);
Settings settings = Settings.builder().put("plugins.anomaly_detection.request_timeout", TimeValue.timeValueSeconds(10)).build();
task = mock(Task.class);
when(task.getId()).thenReturn(1000L);
client = mock(Client.class);
when(client.settings()).thenReturn(settings);
when(client.threadPool()).thenReturn(threadPool);
}
Aggregations