use of org.opensearch.ad.util.ClientUtil 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.ad.util.ClientUtil 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.ad.util.ClientUtil 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.ad.util.ClientUtil in project anomaly-detection by opensearch-project.
the class NodeStateManagerTests method testHasRunningQuery.
public void testHasRunningQuery() throws IOException {
stateManager = new NodeStateManager(client, xContentRegistry(), settings, new ClientUtil(settings, client, throttler, context), clock, duration, clusterService);
AnomalyDetector detector = TestHelpers.randomAnomalyDetector(ImmutableMap.of(), null);
SearchRequest dummySearchRequest = new SearchRequest();
assertFalse(stateManager.hasRunningQuery(detector));
throttler.insertFilteredQuery(detector.getDetectorId(), dummySearchRequest);
assertTrue(stateManager.hasRunningQuery(detector));
}
use of org.opensearch.ad.util.ClientUtil in project anomaly-detection by opensearch-project.
the class DailyCronTests method templateDailyCron.
@SuppressWarnings("unchecked")
private void templateDailyCron(DailyCronTestExecutionMode mode) {
Clock clock = mock(Clock.class);
ClientUtil clientUtil = mock(ClientUtil.class);
DailyCron cron = new DailyCron(clock, Duration.ofHours(24), clientUtil);
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<BulkByScrollResponse> listener = (ActionListener<BulkByScrollResponse>) args[2];
if (mode == DailyCronTestExecutionMode.INDEX_NOT_EXIST) {
listener.onFailure(new IndexNotFoundException("foo", "bar"));
} else if (mode == DailyCronTestExecutionMode.FAIL) {
listener.onFailure(new OpenSearchException("bar"));
} else {
BulkByScrollResponse deleteByQueryResponse = mock(BulkByScrollResponse.class);
when(deleteByQueryResponse.getDeleted()).thenReturn(10L);
listener.onResponse(deleteByQueryResponse);
}
return null;
}).when(clientUtil).execute(eq(DeleteByQueryAction.INSTANCE), any(), any());
cron.run();
}
Aggregations