Search in sources :

Example 1 with IndexUtils

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);
}
Also used : IndexUtils(org.opensearch.ad.util.IndexUtils) ClientUtil(org.opensearch.ad.util.ClientUtil)

Example 2 with IndexUtils

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);
}
Also used : ModelsOnNodeSupplier(org.opensearch.ad.stats.suppliers.ModelsOnNodeSupplier) ClusterSettings(org.opensearch.common.settings.ClusterSettings) EntityCache(org.opensearch.ad.caching.EntityCache) ArrayList(java.util.ArrayList) EntityModel(org.opensearch.ad.ml.EntityModel) HybridThresholdingModel(org.opensearch.ad.ml.HybridThresholdingModel) ModelState(org.opensearch.ad.ml.ModelState) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) RandomModelStateConfig(test.org.opensearch.ad.util.RandomModelStateConfig) CounterSupplier(org.opensearch.ad.stats.suppliers.CounterSupplier) IndexUtils(org.opensearch.ad.util.IndexUtils) ClusterService(org.opensearch.cluster.service.ClusterService) IndexStatusSupplier(org.opensearch.ad.stats.suppliers.IndexStatusSupplier) ClusterSettings(org.opensearch.common.settings.ClusterSettings) Settings(org.opensearch.common.settings.Settings) HashSet(java.util.HashSet) Before(org.junit.Before)

Example 3 with IndexUtils

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);
}
Also used : ModelsOnNodeSupplier(org.opensearch.ad.stats.suppliers.ModelsOnNodeSupplier) ClusterSettings(org.opensearch.common.settings.ClusterSettings) EntityCache(org.opensearch.ad.caching.EntityCache) ClientUtil(org.opensearch.ad.util.ClientUtil) ADStat(org.opensearch.ad.stats.ADStat) ThreadPool(org.opensearch.threadpool.ThreadPool) Clock(java.time.Clock) CounterSupplier(org.opensearch.ad.stats.suppliers.CounterSupplier) IndexUtils(org.opensearch.ad.util.IndexUtils) JvmService(org.opensearch.monitor.jvm.JvmService) Client(org.opensearch.client.Client) ClusterSettings(org.opensearch.common.settings.ClusterSettings) Settings(org.opensearch.common.settings.Settings) Throttler(org.opensearch.ad.util.Throttler) HashSet(java.util.HashSet) JvmStats(org.opensearch.monitor.jvm.JvmStats) ModelManager(org.opensearch.ad.ml.ModelManager) CacheProvider(org.opensearch.ad.caching.CacheProvider) SettableSupplier(org.opensearch.ad.stats.suppliers.SettableSupplier) ClusterService(org.opensearch.cluster.service.ClusterService) IndexStatusSupplier(org.opensearch.ad.stats.suppliers.IndexStatusSupplier) ADTaskManager(org.opensearch.ad.task.ADTaskManager) ADStats(org.opensearch.ad.stats.ADStats) IndexNameExpressionResolver(org.opensearch.cluster.metadata.IndexNameExpressionResolver) Before(org.junit.Before)

Example 4 with IndexUtils

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());
}
Also used : ThreadFactory(java.util.concurrent.ThreadFactory) LockService(org.opensearch.jobscheduler.spi.utils.LockService) ThreadContext(org.opensearch.common.util.concurrent.ThreadContext) Index(org.opensearch.index.Index) AnomalyDetectorJob(org.opensearch.ad.model.AnomalyDetectorJob) IndexRequest(org.opensearch.action.index.IndexRequest) GetResponse(org.opensearch.action.get.GetResponse) ShardId(org.opensearch.index.shard.ShardId) IndexUtils(org.opensearch.ad.util.IndexUtils) ActionListener(org.opensearch.action.ActionListener) IndexResponse(org.opensearch.action.index.IndexResponse) GetRequest(org.opensearch.action.get.GetRequest) AnomalyDetectionIndices(org.opensearch.ad.indices.AnomalyDetectionIndices) IndexNameExpressionResolver(org.opensearch.cluster.metadata.IndexNameExpressionResolver) Settings(org.opensearch.common.settings.Settings) Before(org.junit.Before)

Example 5 with IndexUtils

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) {
        }
    });
}
Also used : ClientUtil(org.opensearch.ad.util.ClientUtil) ThreadPool(org.opensearch.threadpool.ThreadPool) BulkResponse(org.opensearch.action.bulk.BulkResponse) Clock(java.time.Clock) VersionConflictEngineException(org.opensearch.index.engine.VersionConflictEngineException) IOException(java.io.IOException) IndexUtils(org.opensearch.ad.util.IndexUtils) ClusterService(org.opensearch.cluster.service.ClusterService) ActionListener(org.opensearch.action.ActionListener) AnomalyDetectionIndices(org.opensearch.ad.indices.AnomalyDetectionIndices) Client(org.opensearch.client.Client) Settings(org.opensearch.common.settings.Settings) Throttler(org.opensearch.ad.util.Throttler)

Aggregations

IndexUtils (org.opensearch.ad.util.IndexUtils)6 Settings (org.opensearch.common.settings.Settings)5 ClientUtil (org.opensearch.ad.util.ClientUtil)4 Before (org.junit.Before)3 EntityCache (org.opensearch.ad.caching.EntityCache)3 AnomalyDetectionIndices (org.opensearch.ad.indices.AnomalyDetectionIndices)3 Throttler (org.opensearch.ad.util.Throttler)3 Clock (java.time.Clock)2 HashSet (java.util.HashSet)2 ActionListener (org.opensearch.action.ActionListener)2 CacheProvider (org.opensearch.ad.caching.CacheProvider)2 CounterSupplier (org.opensearch.ad.stats.suppliers.CounterSupplier)2 IndexStatusSupplier (org.opensearch.ad.stats.suppliers.IndexStatusSupplier)2 ModelsOnNodeSupplier (org.opensearch.ad.stats.suppliers.ModelsOnNodeSupplier)2 ClusterService (org.opensearch.cluster.service.ClusterService)2 ClusterSettings (org.opensearch.common.settings.ClusterSettings)2 ThresholdedRandomCutForestMapper (com.amazon.randomcutforest.parkservices.state.ThresholdedRandomCutForestMapper)1 ThresholdedRandomCutForestState (com.amazon.randomcutforest.parkservices.state.ThresholdedRandomCutForestState)1 V1JsonToV2StateConverter (com.amazon.randomcutforest.serialize.json.v1.V1JsonToV2StateConverter)1 RandomCutForestMapper (com.amazon.randomcutforest.state.RandomCutForestMapper)1