use of org.opensearch.ad.util.IndexUtils in project anomaly-detection by opensearch-project.
the class AbstractIndexHandlerTest method setUp.
@Override
public void setUp() throws Exception {
super.setUp();
MockitoAnnotations.initMocks(this);
setWriteBlockAdResultIndex(false);
context = TestHelpers.createThreadPool();
clientUtil = new ClientUtil(settings, client, throttler, context);
indexUtil = new IndexUtils(client, clientUtil, clusterService, indexNameResolver);
}
use of org.opensearch.ad.util.IndexUtils in project anomaly-detection by opensearch-project.
the class ADStatsTests method setup.
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
// sampleSize * numberOfTrees has to be larger than 1. Otherwise, RCF reports errors.
rcf = RandomCutForest.builder().dimensions(1).sampleSize(2).numberOfTrees(1).build();
thresholdingModel = new HybridThresholdingModel(1e-8, 1e-5, 200, 10_000, 2, 5_000_000);
List<ModelState<?>> modelsInformation = new ArrayList<>(Arrays.asList(new ModelState<>(rcf, "rcf-model-1", "detector-1", ModelManager.ModelType.RCF.getName(), clock, 0f), new ModelState<>(thresholdingModel, "thr-model-1", "detector-1", ModelManager.ModelType.RCF.getName(), clock, 0f), new ModelState<>(rcf, "rcf-model-2", "detector-2", ModelManager.ModelType.THRESHOLD.getName(), clock, 0f), new ModelState<>(thresholdingModel, "thr-model-2", "detector-2", ModelManager.ModelType.THRESHOLD.getName(), clock, 0f)));
when(modelManager.getAllModels()).thenReturn(modelsInformation);
ModelState<EntityModel> entityModel1 = MLUtil.randomModelState(new RandomModelStateConfig.Builder().fullModel(true).build());
ModelState<EntityModel> entityModel2 = MLUtil.randomModelState(new RandomModelStateConfig.Builder().fullModel(true).build());
List<ModelState<?>> entityModelsInformation = new ArrayList<>(Arrays.asList(entityModel1, entityModel2));
EntityCache cache = mock(EntityCache.class);
when(cacheProvider.get()).thenReturn(cache);
when(cache.getAllModels()).thenReturn(entityModelsInformation);
IndexUtils indexUtils = mock(IndexUtils.class);
when(indexUtils.getIndexHealthStatus(anyString())).thenReturn("yellow");
when(indexUtils.getNumberOfDocumentsInIndex(anyString())).thenReturn(100L);
clusterStatName1 = "clusterStat1";
clusterStatName2 = "clusterStat2";
nodeStatName1 = "nodeStat1";
nodeStatName2 = "nodeStat2";
Settings settings = Settings.builder().put(MAX_MODEL_SIZE_PER_NODE.getKey(), 10).build();
ClusterService clusterService = mock(ClusterService.class);
ClusterSettings clusterSettings = new ClusterSettings(Settings.EMPTY, Collections.unmodifiableSet(new HashSet<>(Arrays.asList(MAX_MODEL_SIZE_PER_NODE))));
when(clusterService.getClusterSettings()).thenReturn(clusterSettings);
statsMap = new HashMap<String, ADStat<?>>() {
{
put(nodeStatName1, new ADStat<>(false, new CounterSupplier()));
put(nodeStatName2, new ADStat<>(false, new ModelsOnNodeSupplier(modelManager, cacheProvider, settings, clusterService)));
put(clusterStatName1, new ADStat<>(true, new IndexStatusSupplier(indexUtils, "index1")));
put(clusterStatName2, new ADStat<>(true, new IndexStatusSupplier(indexUtils, "index2")));
}
};
adStats = new ADStats(statsMap);
}
use of org.opensearch.ad.util.IndexUtils in project anomaly-detection by opensearch-project.
the class ADStatsNodesTransportActionTests method setUp.
@Override
@Before
public void setUp() throws Exception {
super.setUp();
Client client = client();
Clock clock = mock(Clock.class);
Throttler throttler = new Throttler(clock);
ThreadPool threadPool = mock(ThreadPool.class);
IndexNameExpressionResolver indexNameResolver = mock(IndexNameExpressionResolver.class);
IndexUtils indexUtils = new IndexUtils(client, new ClientUtil(Settings.EMPTY, client, throttler, threadPool), clusterService(), indexNameResolver);
ModelManager modelManager = mock(ModelManager.class);
CacheProvider cacheProvider = mock(CacheProvider.class);
EntityCache cache = mock(EntityCache.class);
when(cacheProvider.get()).thenReturn(cache);
clusterStatName1 = "clusterStat1";
clusterStatName2 = "clusterStat2";
nodeStatName1 = "nodeStat1";
nodeStatName2 = "nodeStat2";
Settings settings = Settings.builder().put(MAX_MODEL_SIZE_PER_NODE.getKey(), 10).build();
ClusterService clusterService = mock(ClusterService.class);
ClusterSettings clusterSettings = new ClusterSettings(Settings.EMPTY, Collections.unmodifiableSet(new HashSet<>(Arrays.asList(MAX_MODEL_SIZE_PER_NODE))));
when(clusterService.getClusterSettings()).thenReturn(clusterSettings);
statsMap = new HashMap<String, ADStat<?>>() {
{
put(nodeStatName1, new ADStat<>(false, new CounterSupplier()));
put(nodeStatName2, new ADStat<>(false, new ModelsOnNodeSupplier(modelManager, cacheProvider, settings, clusterService)));
put(clusterStatName1, new ADStat<>(true, new IndexStatusSupplier(indexUtils, "index1")));
put(clusterStatName2, new ADStat<>(true, new IndexStatusSupplier(indexUtils, "index2")));
put(InternalStatNames.JVM_HEAP_USAGE.getName(), new ADStat<>(true, new SettableSupplier()));
}
};
adStats = new ADStats(statsMap);
JvmService jvmService = mock(JvmService.class);
JvmStats jvmStats = mock(JvmStats.class);
JvmStats.Mem mem = mock(JvmStats.Mem.class);
when(jvmService.stats()).thenReturn(jvmStats);
when(jvmStats.getMem()).thenReturn(mem);
when(mem.getHeapUsedPercent()).thenReturn(randomShort());
adTaskManager = mock(ADTaskManager.class);
action = new ADStatsNodesTransportAction(client().threadPool(), clusterService(), mock(TransportService.class), mock(ActionFilters.class), adStats, jvmService, adTaskManager);
}
use of org.opensearch.ad.util.IndexUtils in project anomaly-detection by opensearch-project.
the class AnomalyDetectorJobRunnerTests method setup.
@SuppressWarnings("unchecked")
@Before
public void setup() throws Exception {
super.setUp();
super.setUpLog4jForJUnit(AnomalyDetectorJobRunner.class);
MockitoAnnotations.initMocks(this);
ThreadFactory threadFactory = OpenSearchExecutors.daemonThreadFactory(OpenSearchExecutors.threadName("node1", "test-ad"));
ThreadContext threadContext = new ThreadContext(Settings.EMPTY);
executorService = OpenSearchExecutors.newFixed("test-ad", 4, 100, threadFactory, threadContext);
Mockito.doReturn(executorService).when(mockedThreadPool).executor(anyString());
Mockito.doReturn(mockedThreadPool).when(client).threadPool();
Mockito.doReturn(threadContext).when(mockedThreadPool).getThreadContext();
runner.setThreadPool(mockedThreadPool);
runner.setClient(client);
runner.setAnomalyResultHandler(anomalyResultHandler);
runner.setAdTaskManager(adTaskManager);
Settings settings = Settings.builder().put("plugins.anomaly_detection.max_retry_for_backoff", 2).put("plugins.anomaly_detection.backoff_initial_delay", TimeValue.timeValueMillis(1)).put("plugins.anomaly_detection.max_retry_for_end_run_exception", 3).build();
setUpJobParameter();
runner.setSettings(settings);
AnomalyDetectionIndices anomalyDetectionIndices = mock(AnomalyDetectionIndices.class);
IndexNameExpressionResolver indexNameResolver = mock(IndexNameExpressionResolver.class);
IndexUtils indexUtils = new IndexUtils(client, clientUtil, clusterService, indexNameResolver);
NodeStateManager stateManager = mock(NodeStateManager.class);
runner.setAnomalyDetectionIndices(indexUtil);
lockService = new LockService(client, clusterService);
doReturn(lockService).when(context).getLockService();
doAnswer(invocation -> {
Object[] args = invocation.getArguments();
GetRequest request = (GetRequest) args[0];
ActionListener<GetResponse> listener = (ActionListener<GetResponse>) args[1];
if (request.index().equals(ANOMALY_DETECTOR_JOB_INDEX)) {
AnomalyDetectorJob job = TestHelpers.randomAnomalyDetectorJob(true);
listener.onResponse(TestHelpers.createGetResponse(job, randomAlphaOfLength(5), ANOMALY_DETECTOR_JOB_INDEX));
}
return null;
}).when(client).get(any(), any());
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 >= 2);
IndexRequest request = null;
ActionListener<IndexResponse> listener = null;
if (args[0] instanceof IndexRequest) {
request = (IndexRequest) args[0];
}
if (args[1] instanceof ActionListener) {
listener = (ActionListener<IndexResponse>) args[1];
}
assertTrue(request != null && listener != null);
ShardId shardId = new ShardId(new Index(ANOMALY_DETECTOR_JOB_INDEX, randomAlphaOfLength(10)), 0);
listener.onResponse(new IndexResponse(shardId, randomAlphaOfLength(10), request.id(), 1, 1, 1, true));
return null;
}).when(client).index(any(), any());
}
use of org.opensearch.ad.util.IndexUtils in project anomaly-detection by opensearch-project.
the class AnomalyResultBulkIndexHandlerTests method setUp.
@Override
public void setUp() throws Exception {
super.setUp();
anomalyDetectionIndices = mock(AnomalyDetectionIndices.class);
client = mock(Client.class);
Settings settings = Settings.EMPTY;
Clock clock = mock(Clock.class);
Throttler throttler = new Throttler(clock);
ThreadPool threadpool = mock(ThreadPool.class);
ClientUtil clientUtil = new ClientUtil(Settings.EMPTY, client, throttler, threadpool);
indexUtils = mock(IndexUtils.class);
ClusterService clusterService = mock(ClusterService.class);
ThreadPool threadPool = mock(ThreadPool.class);
bulkIndexHandler = new AnomalyResultBulkIndexHandler(client, settings, threadPool, clientUtil, indexUtils, clusterService, anomalyDetectionIndices);
listener = spy(new ActionListener<BulkResponse>() {
@Override
public void onResponse(BulkResponse bulkItemResponses) {
}
@Override
public void onFailure(Exception e) {
}
});
}
Aggregations