Search in sources :

Example 1 with CounterSupplier

use of org.opensearch.ad.stats.suppliers.CounterSupplier in project anomaly-detection by opensearch-project.

the class ADStatTests method testGetValue.

@Test
public void testGetValue() {
    ADStat<Long> stat1 = new ADStat<>(false, new CounterSupplier());
    assertEquals("GetValue returns the incorrect value", 0L, (long) (stat1.getValue()));
    ADStat<String> stat2 = new ADStat<>(false, new TestSupplier());
    assertEquals("GetValue returns the incorrect value", "test", stat2.getValue());
}
Also used : CounterSupplier(org.opensearch.ad.stats.suppliers.CounterSupplier) Test(org.junit.Test)

Example 2 with CounterSupplier

use of org.opensearch.ad.stats.suppliers.CounterSupplier 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 CounterSupplier

use of org.opensearch.ad.stats.suppliers.CounterSupplier 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 CounterSupplier

use of org.opensearch.ad.stats.suppliers.CounterSupplier in project anomaly-detection by opensearch-project.

the class AnomalyDetectorPlugin method createComponents.

@Override
public Collection<Object> createComponents(Client client, ClusterService clusterService, ThreadPool threadPool, ResourceWatcherService resourceWatcherService, ScriptService scriptService, NamedXContentRegistry xContentRegistry, Environment environment, NodeEnvironment nodeEnvironment, NamedWriteableRegistry namedWriteableRegistry, IndexNameExpressionResolver indexNameExpressionResolver, Supplier<RepositoriesService> repositoriesServiceSupplier) {
    EnabledSetting.getInstance().init(clusterService);
    NumericSetting.getInstance().init(clusterService);
    this.client = client;
    this.threadPool = threadPool;
    Settings settings = environment.settings();
    Throttler throttler = new Throttler(getClock());
    this.clientUtil = new ClientUtil(settings, client, throttler, threadPool);
    this.indexUtils = new IndexUtils(client, clientUtil, clusterService, indexNameExpressionResolver);
    this.nodeFilter = new DiscoveryNodeFilterer(clusterService);
    this.anomalyDetectionIndices = new AnomalyDetectionIndices(client, clusterService, threadPool, settings, nodeFilter, AnomalyDetectorSettings.MAX_UPDATE_RETRY_TIMES);
    this.clusterService = clusterService;
    SingleFeatureLinearUniformInterpolator singleFeatureLinearUniformInterpolator = new IntegerSensitiveSingleFeatureLinearUniformInterpolator();
    Interpolator interpolator = new LinearUniformInterpolator(singleFeatureLinearUniformInterpolator);
    SearchFeatureDao searchFeatureDao = new SearchFeatureDao(client, xContentRegistry, interpolator, clientUtil, settings, clusterService, AnomalyDetectorSettings.NUM_SAMPLES_PER_TREE);
    JvmService jvmService = new JvmService(environment.settings());
    RandomCutForestMapper mapper = new RandomCutForestMapper();
    mapper.setSaveExecutorContextEnabled(true);
    mapper.setSaveTreeStateEnabled(true);
    mapper.setPartialTreeStateEnabled(true);
    V1JsonToV2StateConverter converter = new V1JsonToV2StateConverter();
    double modelMaxSizePercent = AnomalyDetectorSettings.MODEL_MAX_SIZE_PERCENTAGE.get(settings);
    ADCircuitBreakerService adCircuitBreakerService = new ADCircuitBreakerService(jvmService).init();
    MemoryTracker memoryTracker = new MemoryTracker(jvmService, modelMaxSizePercent, AnomalyDetectorSettings.DESIRED_MODEL_SIZE_PERCENTAGE, clusterService, adCircuitBreakerService);
    NodeStateManager stateManager = new NodeStateManager(client, xContentRegistry, settings, clientUtil, getClock(), AnomalyDetectorSettings.HOURLY_MAINTENANCE, clusterService);
    FeatureManager featureManager = new FeatureManager(searchFeatureDao, interpolator, getClock(), 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, AD_THREAD_POOL_NAME);
    long heapSizeBytes = JvmInfo.jvmInfo().getMem().getHeapMax().getBytes();
    serializeRCFBufferPool = AccessController.doPrivileged(new PrivilegedAction<GenericObjectPool<LinkedBuffer>>() {

        @Override
        public GenericObjectPool<LinkedBuffer> run() {
            return new GenericObjectPool<>(new BasePooledObjectFactory<LinkedBuffer>() {

                @Override
                public LinkedBuffer create() throws Exception {
                    return LinkedBuffer.allocate(AnomalyDetectorSettings.SERIALIZATION_BUFFER_BYTES);
                }

                @Override
                public PooledObject<LinkedBuffer> wrap(LinkedBuffer obj) {
                    return new DefaultPooledObject<>(obj);
                }
            });
        }
    });
    serializeRCFBufferPool.setMaxTotal(AnomalyDetectorSettings.MAX_TOTAL_RCF_SERIALIZATION_BUFFERS);
    serializeRCFBufferPool.setMaxIdle(AnomalyDetectorSettings.MAX_TOTAL_RCF_SERIALIZATION_BUFFERS);
    serializeRCFBufferPool.setMinIdle(0);
    serializeRCFBufferPool.setBlockWhenExhausted(false);
    serializeRCFBufferPool.setTimeBetweenEvictionRuns(AnomalyDetectorSettings.HOURLY_MAINTENANCE);
    CheckpointDao checkpoint = new CheckpointDao(client, clientUtil, CommonName.CHECKPOINT_INDEX_NAME, gson, mapper, converter, new ThresholdedRandomCutForestMapper(), AccessController.doPrivileged((PrivilegedAction<Schema<ThresholdedRandomCutForestState>>) () -> RuntimeSchema.getSchema(ThresholdedRandomCutForestState.class)), HybridThresholdingModel.class, anomalyDetectionIndices, AnomalyDetectorSettings.MAX_CHECKPOINT_BYTES, serializeRCFBufferPool, AnomalyDetectorSettings.SERIALIZATION_BUFFER_BYTES, 1 - AnomalyDetectorSettings.THRESHOLD_MIN_PVALUE);
    Random random = new Random(42);
    CheckpointWriteWorker checkpointWriteQueue = new CheckpointWriteWorker(heapSizeBytes, AnomalyDetectorSettings.CHECKPOINT_WRITE_QUEUE_SIZE_IN_BYTES, AnomalyDetectorSettings.CHECKPOINT_WRITE_QUEUE_MAX_HEAP_PERCENT, clusterService, random, adCircuitBreakerService, threadPool, settings, AnomalyDetectorSettings.MAX_QUEUED_TASKS_RATIO, getClock(), AnomalyDetectorSettings.MEDIUM_SEGMENT_PRUNE_RATIO, AnomalyDetectorSettings.LOW_SEGMENT_PRUNE_RATIO, AnomalyDetectorSettings.MAINTENANCE_FREQ_CONSTANT, AnomalyDetectorSettings.QUEUE_MAINTENANCE, checkpoint, CommonName.CHECKPOINT_INDEX_NAME, AnomalyDetectorSettings.HOURLY_MAINTENANCE, stateManager, AnomalyDetectorSettings.HOURLY_MAINTENANCE);
    EntityCache cache = new PriorityCache(checkpoint, AnomalyDetectorSettings.DEDICATED_CACHE_SIZE.get(settings), AnomalyDetectorSettings.CHECKPOINT_TTL, AnomalyDetectorSettings.MAX_INACTIVE_ENTITIES, memoryTracker, AnomalyDetectorSettings.NUM_TREES, getClock(), clusterService, AnomalyDetectorSettings.HOURLY_MAINTENANCE, threadPool, checkpointWriteQueue, AnomalyDetectorSettings.MAINTENANCE_FREQ_CONSTANT);
    CacheProvider cacheProvider = new CacheProvider(cache);
    EntityColdStarter entityColdStarter = new EntityColdStarter(getClock(), threadPool, stateManager, AnomalyDetectorSettings.NUM_SAMPLES_PER_TREE, AnomalyDetectorSettings.NUM_TREES, AnomalyDetectorSettings.TIME_DECAY, AnomalyDetectorSettings.NUM_MIN_SAMPLES, AnomalyDetectorSettings.MAX_SAMPLE_STRIDE, AnomalyDetectorSettings.MAX_TRAIN_SAMPLE, interpolator, searchFeatureDao, AnomalyDetectorSettings.THRESHOLD_MIN_PVALUE, featureManager, settings, AnomalyDetectorSettings.HOURLY_MAINTENANCE, checkpointWriteQueue, AnomalyDetectorSettings.MAX_COLD_START_ROUNDS);
    EntityColdStartWorker coldstartQueue = new EntityColdStartWorker(heapSizeBytes, AnomalyDetectorSettings.ENTITY_REQUEST_SIZE_IN_BYTES, AnomalyDetectorSettings.ENTITY_COLD_START_QUEUE_MAX_HEAP_PERCENT, clusterService, random, adCircuitBreakerService, threadPool, settings, AnomalyDetectorSettings.MAX_QUEUED_TASKS_RATIO, getClock(), AnomalyDetectorSettings.MEDIUM_SEGMENT_PRUNE_RATIO, AnomalyDetectorSettings.LOW_SEGMENT_PRUNE_RATIO, AnomalyDetectorSettings.MAINTENANCE_FREQ_CONSTANT, AnomalyDetectorSettings.QUEUE_MAINTENANCE, entityColdStarter, AnomalyDetectorSettings.HOURLY_MAINTENANCE, stateManager);
    ModelManager modelManager = new ModelManager(checkpoint, getClock(), 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, featureManager, memoryTracker);
    MultiEntityResultHandler multiEntityResultHandler = new MultiEntityResultHandler(client, settings, threadPool, anomalyDetectionIndices, this.clientUtil, this.indexUtils, clusterService);
    ResultWriteWorker resultWriteQueue = new ResultWriteWorker(heapSizeBytes, AnomalyDetectorSettings.RESULT_WRITE_QUEUE_SIZE_IN_BYTES, AnomalyDetectorSettings.RESULT_WRITE_QUEUE_MAX_HEAP_PERCENT, clusterService, random, adCircuitBreakerService, threadPool, settings, AnomalyDetectorSettings.MAX_QUEUED_TASKS_RATIO, getClock(), AnomalyDetectorSettings.MEDIUM_SEGMENT_PRUNE_RATIO, AnomalyDetectorSettings.LOW_SEGMENT_PRUNE_RATIO, AnomalyDetectorSettings.MAINTENANCE_FREQ_CONSTANT, AnomalyDetectorSettings.QUEUE_MAINTENANCE, multiEntityResultHandler, xContentRegistry, stateManager, AnomalyDetectorSettings.HOURLY_MAINTENANCE);
    CheckpointReadWorker checkpointReadQueue = new CheckpointReadWorker(heapSizeBytes, AnomalyDetectorSettings.ENTITY_FEATURE_REQUEST_SIZE_IN_BYTES, AnomalyDetectorSettings.CHECKPOINT_READ_QUEUE_MAX_HEAP_PERCENT, clusterService, random, adCircuitBreakerService, threadPool, settings, AnomalyDetectorSettings.MAX_QUEUED_TASKS_RATIO, getClock(), AnomalyDetectorSettings.MEDIUM_SEGMENT_PRUNE_RATIO, AnomalyDetectorSettings.LOW_SEGMENT_PRUNE_RATIO, AnomalyDetectorSettings.MAINTENANCE_FREQ_CONSTANT, AnomalyDetectorSettings.QUEUE_MAINTENANCE, modelManager, checkpoint, coldstartQueue, resultWriteQueue, stateManager, anomalyDetectionIndices, cacheProvider, AnomalyDetectorSettings.HOURLY_MAINTENANCE, checkpointWriteQueue);
    ColdEntityWorker coldEntityQueue = new ColdEntityWorker(heapSizeBytes, AnomalyDetectorSettings.ENTITY_FEATURE_REQUEST_SIZE_IN_BYTES, AnomalyDetectorSettings.COLD_ENTITY_QUEUE_MAX_HEAP_PERCENT, clusterService, random, adCircuitBreakerService, threadPool, settings, AnomalyDetectorSettings.MAX_QUEUED_TASKS_RATIO, getClock(), AnomalyDetectorSettings.MEDIUM_SEGMENT_PRUNE_RATIO, AnomalyDetectorSettings.LOW_SEGMENT_PRUNE_RATIO, AnomalyDetectorSettings.MAINTENANCE_FREQ_CONSTANT, checkpointReadQueue, AnomalyDetectorSettings.HOURLY_MAINTENANCE, stateManager);
    ADDataMigrator dataMigrator = new ADDataMigrator(client, clusterService, xContentRegistry, anomalyDetectionIndices);
    HashRing hashRing = new HashRing(nodeFilter, getClock(), settings, client, clusterService, dataMigrator, modelManager);
    anomalyDetectorRunner = new AnomalyDetectorRunner(modelManager, featureManager, AnomalyDetectorSettings.MAX_PREVIEW_RESULTS);
    Map<String, ADStat<?>> stats = ImmutableMap.<String, ADStat<?>>builder().put(StatNames.AD_EXECUTE_REQUEST_COUNT.getName(), new ADStat<>(false, new CounterSupplier())).put(StatNames.AD_EXECUTE_FAIL_COUNT.getName(), new ADStat<>(false, new CounterSupplier())).put(StatNames.AD_HC_EXECUTE_REQUEST_COUNT.getName(), new ADStat<>(false, new CounterSupplier())).put(StatNames.AD_HC_EXECUTE_FAIL_COUNT.getName(), new ADStat<>(false, new CounterSupplier())).put(StatNames.MODEL_INFORMATION.getName(), new ADStat<>(false, new ModelsOnNodeSupplier(modelManager, cacheProvider, settings, clusterService))).put(StatNames.ANOMALY_DETECTORS_INDEX_STATUS.getName(), new ADStat<>(true, new IndexStatusSupplier(indexUtils, AnomalyDetector.ANOMALY_DETECTORS_INDEX))).put(StatNames.ANOMALY_RESULTS_INDEX_STATUS.getName(), new ADStat<>(true, new IndexStatusSupplier(indexUtils, CommonName.ANOMALY_RESULT_INDEX_ALIAS))).put(StatNames.MODELS_CHECKPOINT_INDEX_STATUS.getName(), new ADStat<>(true, new IndexStatusSupplier(indexUtils, CommonName.CHECKPOINT_INDEX_NAME))).put(StatNames.ANOMALY_DETECTION_JOB_INDEX_STATUS.getName(), new ADStat<>(true, new IndexStatusSupplier(indexUtils, AnomalyDetectorJob.ANOMALY_DETECTOR_JOB_INDEX))).put(StatNames.ANOMALY_DETECTION_STATE_STATUS.getName(), new ADStat<>(true, new IndexStatusSupplier(indexUtils, CommonName.DETECTION_STATE_INDEX))).put(StatNames.DETECTOR_COUNT.getName(), new ADStat<>(true, new SettableSupplier())).put(StatNames.SINGLE_ENTITY_DETECTOR_COUNT.getName(), new ADStat<>(true, new SettableSupplier())).put(StatNames.MULTI_ENTITY_DETECTOR_COUNT.getName(), new ADStat<>(true, new SettableSupplier())).put(StatNames.AD_EXECUTING_BATCH_TASK_COUNT.getName(), new ADStat<>(false, new CounterSupplier())).put(StatNames.AD_CANCELED_BATCH_TASK_COUNT.getName(), new ADStat<>(false, new CounterSupplier())).put(StatNames.AD_TOTAL_BATCH_TASK_EXECUTION_COUNT.getName(), new ADStat<>(false, new CounterSupplier())).put(StatNames.AD_BATCH_TASK_FAILURE_COUNT.getName(), new ADStat<>(false, new CounterSupplier())).put(StatNames.MODEL_COUNT.getName(), new ADStat<>(false, new ModelsOnNodeCountSupplier(modelManager, cacheProvider))).build();
    adStats = new ADStats(stats);
    adTaskCacheManager = new ADTaskCacheManager(settings, clusterService, memoryTracker);
    adTaskManager = new ADTaskManager(settings, clusterService, client, xContentRegistry, anomalyDetectionIndices, nodeFilter, hashRing, adTaskCacheManager, threadPool);
    AnomalyResultBulkIndexHandler anomalyResultBulkIndexHandler = new AnomalyResultBulkIndexHandler(client, settings, threadPool, this.clientUtil, this.indexUtils, clusterService, anomalyDetectionIndices);
    adBatchTaskRunner = new ADBatchTaskRunner(settings, threadPool, clusterService, client, adCircuitBreakerService, featureManager, adTaskManager, anomalyDetectionIndices, adStats, anomalyResultBulkIndexHandler, adTaskCacheManager, searchFeatureDao, hashRing, modelManager);
    ADSearchHandler adSearchHandler = new ADSearchHandler(settings, clusterService, client);
    // transport action handler constructors
    return ImmutableList.of(anomalyDetectionIndices, anomalyDetectorRunner, searchFeatureDao, singleFeatureLinearUniformInterpolator, interpolator, gson, jvmService, hashRing, featureManager, modelManager, stateManager, new ADClusterEventListener(clusterService, hashRing), adCircuitBreakerService, adStats, new MasterEventListener(clusterService, threadPool, client, getClock(), clientUtil, nodeFilter), nodeFilter, multiEntityResultHandler, checkpoint, cacheProvider, adTaskManager, adBatchTaskRunner, adSearchHandler, coldstartQueue, resultWriteQueue, checkpointReadQueue, checkpointWriteQueue, coldEntityQueue, entityColdStarter, adTaskCacheManager);
}
Also used : ModelsOnNodeSupplier(org.opensearch.ad.stats.suppliers.ModelsOnNodeSupplier) DiscoveryNodeFilterer(org.opensearch.ad.util.DiscoveryNodeFilterer) EntityCache(org.opensearch.ad.caching.EntityCache) ClientUtil(org.opensearch.ad.util.ClientUtil) SearchFeatureDao(org.opensearch.ad.feature.SearchFeatureDao) ResultWriteWorker(org.opensearch.ad.ratelimit.ResultWriteWorker) AnomalyResultBulkIndexHandler(org.opensearch.ad.transport.handler.AnomalyResultBulkIndexHandler) Random(java.util.Random) IntegerSensitiveSingleFeatureLinearUniformInterpolator(org.opensearch.ad.dataprocessor.IntegerSensitiveSingleFeatureLinearUniformInterpolator) ADClusterEventListener(org.opensearch.ad.cluster.ADClusterEventListener) LinearUniformInterpolator(org.opensearch.ad.dataprocessor.LinearUniformInterpolator) SingleFeatureLinearUniformInterpolator(org.opensearch.ad.dataprocessor.SingleFeatureLinearUniformInterpolator) IntegerSensitiveSingleFeatureLinearUniformInterpolator(org.opensearch.ad.dataprocessor.IntegerSensitiveSingleFeatureLinearUniformInterpolator) ThresholdedRandomCutForestState(com.amazon.randomcutforest.parkservices.state.ThresholdedRandomCutForestState) ADSearchHandler(org.opensearch.ad.transport.handler.ADSearchHandler) IndexScopedSettings(org.opensearch.common.settings.IndexScopedSettings) Settings(org.opensearch.common.settings.Settings) AnomalyDetectorSettings(org.opensearch.ad.settings.AnomalyDetectorSettings) LegacyOpenDistroAnomalyDetectorSettings(org.opensearch.ad.settings.LegacyOpenDistroAnomalyDetectorSettings) ClusterSettings(org.opensearch.common.settings.ClusterSettings) Throttler(org.opensearch.ad.util.Throttler) ThresholdedRandomCutForestMapper(com.amazon.randomcutforest.parkservices.state.ThresholdedRandomCutForestMapper) PriorityCache(org.opensearch.ad.caching.PriorityCache) CacheProvider(org.opensearch.ad.caching.CacheProvider) ModelManager(org.opensearch.ad.ml.ModelManager) CheckpointWriteWorker(org.opensearch.ad.ratelimit.CheckpointWriteWorker) CheckpointDao(org.opensearch.ad.ml.CheckpointDao) V1JsonToV2StateConverter(com.amazon.randomcutforest.serialize.json.v1.V1JsonToV2StateConverter) ModelsOnNodeCountSupplier(org.opensearch.ad.stats.suppliers.ModelsOnNodeCountSupplier) ADBatchTaskRunner(org.opensearch.ad.task.ADBatchTaskRunner) ADStat(org.opensearch.ad.stats.ADStat) CheckpointReadWorker(org.opensearch.ad.ratelimit.CheckpointReadWorker) EntityColdStartWorker(org.opensearch.ad.ratelimit.EntityColdStartWorker) HashRing(org.opensearch.ad.cluster.HashRing) CounterSupplier(org.opensearch.ad.stats.suppliers.CounterSupplier) IndexUtils(org.opensearch.ad.util.IndexUtils) EntityColdStarter(org.opensearch.ad.ml.EntityColdStarter) PrivilegedAction(java.security.PrivilegedAction) ColdEntityWorker(org.opensearch.ad.ratelimit.ColdEntityWorker) JvmService(org.opensearch.monitor.jvm.JvmService) Interpolator(org.opensearch.ad.dataprocessor.Interpolator) LinearUniformInterpolator(org.opensearch.ad.dataprocessor.LinearUniformInterpolator) SingleFeatureLinearUniformInterpolator(org.opensearch.ad.dataprocessor.SingleFeatureLinearUniformInterpolator) IntegerSensitiveSingleFeatureLinearUniformInterpolator(org.opensearch.ad.dataprocessor.IntegerSensitiveSingleFeatureLinearUniformInterpolator) MasterEventListener(org.opensearch.ad.cluster.MasterEventListener) MultiEntityResultHandler(org.opensearch.ad.transport.handler.MultiEntityResultHandler) FeatureManager(org.opensearch.ad.feature.FeatureManager) LinkedBuffer(io.protostuff.LinkedBuffer) ADDataMigrator(org.opensearch.ad.cluster.ADDataMigrator) ADCircuitBreakerService(org.opensearch.ad.breaker.ADCircuitBreakerService) GenericObjectPool(org.apache.commons.pool2.impl.GenericObjectPool) SingleFeatureLinearUniformInterpolator(org.opensearch.ad.dataprocessor.SingleFeatureLinearUniformInterpolator) IntegerSensitiveSingleFeatureLinearUniformInterpolator(org.opensearch.ad.dataprocessor.IntegerSensitiveSingleFeatureLinearUniformInterpolator) SettableSupplier(org.opensearch.ad.stats.suppliers.SettableSupplier) IndexStatusSupplier(org.opensearch.ad.stats.suppliers.IndexStatusSupplier) ADTaskCacheManager(org.opensearch.ad.task.ADTaskCacheManager) DefaultPooledObject(org.apache.commons.pool2.impl.DefaultPooledObject) PooledObject(org.apache.commons.pool2.PooledObject) ADTaskManager(org.opensearch.ad.task.ADTaskManager) ThresholdedRandomCutForestMapper(com.amazon.randomcutforest.parkservices.state.ThresholdedRandomCutForestMapper) RandomCutForestMapper(com.amazon.randomcutforest.state.RandomCutForestMapper) AnomalyDetectionIndices(org.opensearch.ad.indices.AnomalyDetectionIndices) ADStats(org.opensearch.ad.stats.ADStats)

Example 5 with CounterSupplier

use of org.opensearch.ad.stats.suppliers.CounterSupplier in project anomaly-detection by opensearch-project.

the class AnomalyResultTests method setUp.

@SuppressWarnings("unchecked")
@Override
@Before
public void setUp() throws Exception {
    super.setUp();
    super.setUpLog4jForJUnit(AnomalyResultTransportAction.class);
    setupTestNodes(AnomalyDetectorSettings.MAX_ENTITIES_PER_QUERY, AnomalyDetectorSettings.PAGE_SIZE);
    transportService = testNodes[0].transportService;
    clusterService = testNodes[0].clusterService;
    settings = clusterService.getSettings();
    stateManager = mock(NodeStateManager.class);
    when(stateManager.isMuted(any(String.class), any(String.class))).thenReturn(false);
    when(stateManager.markColdStartRunning(anyString())).thenReturn(() -> {
    });
    detector = mock(AnomalyDetector.class);
    featureId = "xyz";
    // we have one feature
    when(detector.getEnabledFeatureIds()).thenReturn(Collections.singletonList(featureId));
    featureName = "abc";
    when(detector.getEnabledFeatureNames()).thenReturn(Collections.singletonList(featureName));
    List<String> userIndex = new ArrayList<>();
    userIndex.add("test*");
    when(detector.getIndices()).thenReturn(userIndex);
    adID = "123";
    when(detector.getDetectorId()).thenReturn(adID);
    when(detector.getCategoryField()).thenReturn(null);
    doAnswer(invocation -> {
        ActionListener<Optional<AnomalyDetector>> listener = invocation.getArgument(1);
        listener.onResponse(Optional.of(detector));
        return null;
    }).when(stateManager).getAnomalyDetector(any(String.class), any(ActionListener.class));
    when(detector.getDetectorIntervalInMinutes()).thenReturn(1L);
    hashRing = mock(HashRing.class);
    Optional<DiscoveryNode> localNode = Optional.of(clusterService.state().nodes().getLocalNode());
    when(hashRing.getOwningNodeWithSameLocalAdVersionForRealtimeAD(any(String.class))).thenReturn(localNode);
    doReturn(localNode).when(hashRing).getNodeByAddress(any());
    featureQuery = mock(FeatureManager.class);
    doAnswer(invocation -> {
        ActionListener<SinglePointFeatures> listener = invocation.getArgument(3);
        listener.onResponse(new SinglePointFeatures(Optional.of(new double[] { 0.0d }), Optional.of(new double[] { 0 })));
        return null;
    }).when(featureQuery).getCurrentFeatures(any(AnomalyDetector.class), anyLong(), anyLong(), any(ActionListener.class));
    double rcfScore = 0.2;
    confidence = 0.91;
    anomalyGrade = 0.5;
    normalModelManager = mock(ModelManager.class);
    long totalUpdates = 1440;
    int relativeIndex = 0;
    double[] currentTimeAttribution = new double[] { 0.5, 0.5 };
    double[] pastValues = new double[] { 123, 456 };
    double[][] expectedValuesList = new double[][] { new double[] { 789, 12 } };
    double[] likelihood = new double[] { 1 };
    double threshold = 1.1d;
    doAnswer(invocation -> {
        ActionListener<ThresholdingResult> listener = invocation.getArgument(3);
        listener.onResponse(new ThresholdingResult(anomalyGrade, confidence, rcfScore, totalUpdates, relativeIndex, currentTimeAttribution, pastValues, expectedValuesList, likelihood, threshold, 30));
        return null;
    }).when(normalModelManager).getTRcfResult(any(String.class), any(String.class), any(double[].class), any(ActionListener.class));
    doAnswer(invocation -> {
        ActionListener<ThresholdingResult> listener = invocation.getArgument(3);
        listener.onResponse(new ThresholdingResult(0, 1.0d, rcfScore));
        return null;
    }).when(normalModelManager).getThresholdingResult(any(String.class), any(String.class), anyDouble(), any(ActionListener.class));
    // "123-threshold";
    thresholdModelID = SingleStreamModelIdMapper.getThresholdModelId(adID);
    // when(normalModelPartitioner.getThresholdModelId(any(String.class))).thenReturn(thresholdModelID);
    adCircuitBreakerService = mock(ADCircuitBreakerService.class);
    when(adCircuitBreakerService.isOpen()).thenReturn(false);
    ThreadPool threadPool = mock(ThreadPool.class);
    client = mock(Client.class);
    ThreadContext threadContext = new ThreadContext(Settings.EMPTY);
    when(client.threadPool()).thenReturn(threadPool);
    when(client.threadPool().getThreadContext()).thenReturn(threadContext);
    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(CommonName.ANOMALY_RESULT_INDEX_ALIAS, randomAlphaOfLength(10)), 0);
        listener.onResponse(new IndexResponse(shardId, randomAlphaOfLength(10), request.id(), 1, 1, 1, true));
        return null;
    }).when(client).index(any(), any());
    indexNameResolver = new IndexNameExpressionResolver(new ThreadContext(Settings.EMPTY));
    Map<String, ADStat<?>> statsMap = new HashMap<String, ADStat<?>>() {

        {
            put(StatNames.AD_EXECUTE_REQUEST_COUNT.getName(), new ADStat<>(false, new CounterSupplier()));
            put(StatNames.AD_EXECUTE_FAIL_COUNT.getName(), new ADStat<>(false, new CounterSupplier()));
            put(StatNames.AD_HC_EXECUTE_REQUEST_COUNT.getName(), new ADStat<>(false, new CounterSupplier()));
            put(StatNames.AD_HC_EXECUTE_FAIL_COUNT.getName(), new ADStat<>(false, new CounterSupplier()));
        }
    };
    adStats = new ADStats(statsMap);
    doAnswer(invocation -> {
        Object[] args = invocation.getArguments();
        GetRequest request = (GetRequest) args[0];
        ActionListener<GetResponse> listener = (ActionListener<GetResponse>) args[1];
        if (request.index().equals(CommonName.DETECTION_STATE_INDEX)) {
            DetectorInternalState.Builder result = new DetectorInternalState.Builder().lastUpdateTime(Instant.now());
            listener.onResponse(TestHelpers.createGetResponse(result.build(), detector.getDetectorId(), CommonName.DETECTION_STATE_INDEX));
        }
        return null;
    }).when(client).get(any(), any());
    adTaskManager = mock(ADTaskManager.class);
    doAnswer(invocation -> {
        ActionListener<Boolean> listener = invocation.getArgument(3);
        listener.onResponse(true);
        return null;
    }).when(adTaskManager).initRealtimeTaskCacheAndCleanupStaleCache(anyString(), any(AnomalyDetector.class), any(TransportService.class), any(ActionListener.class));
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Index(org.opensearch.index.Index) Matchers.containsString(org.hamcrest.Matchers.containsString) Mockito.anyString(org.mockito.Mockito.anyString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) AnomalyDetector(org.opensearch.ad.model.AnomalyDetector) GetRequest(org.opensearch.action.get.GetRequest) Client(org.opensearch.client.Client) OpenSearchTestCase.randomBoolean(org.opensearch.test.OpenSearchTestCase.randomBoolean) Optional(java.util.Optional) ModelManager(org.opensearch.ad.ml.ModelManager) ThresholdingResult(org.opensearch.ad.ml.ThresholdingResult) ActionListener(org.opensearch.action.ActionListener) IndexResponse(org.opensearch.action.index.IndexResponse) IndexNameExpressionResolver(org.opensearch.cluster.metadata.IndexNameExpressionResolver) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) ADStat(org.opensearch.ad.stats.ADStat) ThreadPool(org.opensearch.threadpool.ThreadPool) DetectorInternalState(org.opensearch.ad.model.DetectorInternalState) IndexRequest(org.opensearch.action.index.IndexRequest) NodeStateManager(org.opensearch.ad.NodeStateManager) HashRing(org.opensearch.ad.cluster.HashRing) ShardId(org.opensearch.index.shard.ShardId) CounterSupplier(org.opensearch.ad.stats.suppliers.CounterSupplier) SinglePointFeatures(org.opensearch.ad.feature.SinglePointFeatures) FeatureManager(org.opensearch.ad.feature.FeatureManager) ADCircuitBreakerService(org.opensearch.ad.breaker.ADCircuitBreakerService) ThreadContext(org.opensearch.common.util.concurrent.ThreadContext) GetResponse(org.opensearch.action.get.GetResponse) ADTaskManager(org.opensearch.ad.task.ADTaskManager) TransportService(org.opensearch.transport.TransportService) ADStats(org.opensearch.ad.stats.ADStats) Before(org.junit.Before)

Aggregations

CounterSupplier (org.opensearch.ad.stats.suppliers.CounterSupplier)7 Before (org.junit.Before)4 EntityCache (org.opensearch.ad.caching.EntityCache)4 ModelManager (org.opensearch.ad.ml.ModelManager)4 ADStat (org.opensearch.ad.stats.ADStat)4 ADStats (org.opensearch.ad.stats.ADStats)4 ADTaskManager (org.opensearch.ad.task.ADTaskManager)4 ClusterSettings (org.opensearch.common.settings.ClusterSettings)4 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)3 ADCircuitBreakerService (org.opensearch.ad.breaker.ADCircuitBreakerService)3 CacheProvider (org.opensearch.ad.caching.CacheProvider)3 HashRing (org.opensearch.ad.cluster.HashRing)3 FeatureManager (org.opensearch.ad.feature.FeatureManager)3 Client (org.opensearch.client.Client)3 IndexNameExpressionResolver (org.opensearch.cluster.metadata.IndexNameExpressionResolver)3 ThreadPool (org.opensearch.threadpool.ThreadPool)3 Clock (java.time.Clock)2 HashMap (java.util.HashMap)2