Search in sources :

Example 1 with AnomalyDetectionIndices

use of org.opensearch.ad.indices.AnomalyDetectionIndices in project anomaly-detection by opensearch-project.

the class IndexAnomalyDetectorActionHandlerTests method setUp.

@SuppressWarnings("unchecked")
@Override
@Before
public void setUp() throws Exception {
    super.setUp();
    settings = Settings.EMPTY;
    clusterService = mock(ClusterService.class);
    clientMock = spy(new NodeClient(settings, threadPool));
    transportService = mock(TransportService.class);
    channel = mock(ActionListener.class);
    anomalyDetectionIndices = mock(AnomalyDetectionIndices.class);
    when(anomalyDetectionIndices.doesAnomalyDetectorIndexExist()).thenReturn(true);
    detectorId = "123";
    seqNo = 0L;
    primaryTerm = 0L;
    WriteRequest.RefreshPolicy refreshPolicy = WriteRequest.RefreshPolicy.IMMEDIATE;
    String field = "a";
    detector = TestHelpers.randomAnomalyDetectorUsingCategoryFields(detectorId, Arrays.asList(field));
    requestTimeout = new TimeValue(1000L);
    maxSingleEntityAnomalyDetectors = 1000;
    maxMultiEntityAnomalyDetectors = 10;
    maxAnomalyFeatures = 5;
    method = RestRequest.Method.POST;
    adTaskManager = mock(ADTaskManager.class);
    searchFeatureDao = mock(SearchFeatureDao.class);
    handler = new IndexAnomalyDetectorActionHandler(clusterService, clientMock, transportService, channel, anomalyDetectionIndices, detectorId, seqNo, primaryTerm, refreshPolicy, detector, requestTimeout, maxSingleEntityAnomalyDetectors, maxMultiEntityAnomalyDetectors, maxAnomalyFeatures, method, xContentRegistry(), null, adTaskManager, searchFeatureDao);
}
Also used : NodeClient(org.opensearch.client.node.NodeClient) ClusterService(org.opensearch.cluster.service.ClusterService) ActionListener(org.opensearch.action.ActionListener) TransportService(org.opensearch.transport.TransportService) ADTaskManager(org.opensearch.ad.task.ADTaskManager) WriteRequest(org.opensearch.action.support.WriteRequest) AnomalyDetectionIndices(org.opensearch.ad.indices.AnomalyDetectionIndices) IndexAnomalyDetectorActionHandler(org.opensearch.ad.rest.handler.IndexAnomalyDetectorActionHandler) SearchFeatureDao(org.opensearch.ad.feature.SearchFeatureDao) TimeValue(org.opensearch.common.unit.TimeValue) Before(org.junit.Before)

Example 2 with AnomalyDetectionIndices

use of org.opensearch.ad.indices.AnomalyDetectionIndices in project anomaly-detection by opensearch-project.

the class CheckpointReadWorkerTests method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    clusterService = mock(ClusterService.class);
    clusterSettings = new ClusterSettings(Settings.EMPTY, Collections.unmodifiableSet(new HashSet<>(Arrays.asList(AnomalyDetectorSettings.CHECKPOINT_READ_QUEUE_MAX_HEAP_PERCENT, AnomalyDetectorSettings.CHECKPOINT_READ_QUEUE_CONCURRENCY, AnomalyDetectorSettings.CHECKPOINT_READ_QUEUE_BATCH_SIZE))));
    when(clusterService.getClusterSettings()).thenReturn(clusterSettings);
    state = MLUtil.randomModelState(new RandomModelStateConfig.Builder().fullModel(true).build());
    checkpoint = mock(CheckpointDao.class);
    Map.Entry<EntityModel, Instant> entry = new SimpleImmutableEntry<EntityModel, Instant>(state.getModel(), Instant.now());
    when(checkpoint.processGetResponse(any(), anyString())).thenReturn(Optional.of(entry));
    checkpointWriteQueue = mock(CheckpointWriteWorker.class);
    modelManager = mock(ModelManager.class);
    when(modelManager.processEntityCheckpoint(any(), any(), anyString(), anyString(), anyInt())).thenReturn(state);
    when(modelManager.score(any(), anyString(), any())).thenReturn(new ThresholdingResult(0, 1, 0.7));
    coldstartQueue = mock(EntityColdStartWorker.class);
    resultWriteQueue = mock(ResultWriteWorker.class);
    anomalyDetectionIndices = mock(AnomalyDetectionIndices.class);
    cacheProvider = mock(CacheProvider.class);
    entityCache = mock(EntityCache.class);
    when(cacheProvider.get()).thenReturn(entityCache);
    when(entityCache.hostIfPossible(any(), any())).thenReturn(true);
    // Integer.MAX_VALUE makes a huge heap
    worker = new CheckpointReadWorker(Integer.MAX_VALUE, AnomalyDetectorSettings.ENTITY_FEATURE_REQUEST_SIZE_IN_BYTES, AnomalyDetectorSettings.CHECKPOINT_READ_QUEUE_MAX_HEAP_PERCENT, clusterService, new Random(42), mock(ADCircuitBreakerService.class), threadPool, Settings.EMPTY, AnomalyDetectorSettings.MAX_QUEUED_TASKS_RATIO, clock, AnomalyDetectorSettings.MEDIUM_SEGMENT_PRUNE_RATIO, AnomalyDetectorSettings.LOW_SEGMENT_PRUNE_RATIO, AnomalyDetectorSettings.MAINTENANCE_FREQ_CONSTANT, AnomalyDetectorSettings.QUEUE_MAINTENANCE, modelManager, checkpoint, coldstartQueue, resultWriteQueue, nodeStateManager, anomalyDetectionIndices, cacheProvider, AnomalyDetectorSettings.HOURLY_MAINTENANCE, checkpointWriteQueue);
    request = new EntityFeatureRequest(Integer.MAX_VALUE, detectorId, RequestPriority.MEDIUM, entity, new double[] { 0 }, 0);
    request2 = new EntityFeatureRequest(Integer.MAX_VALUE, detectorId, RequestPriority.MEDIUM, entity2, new double[] { 0 }, 0);
    request3 = new EntityFeatureRequest(Integer.MAX_VALUE, detectorId, RequestPriority.MEDIUM, entity3, new double[] { 0 }, 0);
}
Also used : ClusterSettings(org.opensearch.common.settings.ClusterSettings) EntityCache(org.opensearch.ad.caching.EntityCache) Instant(java.time.Instant) EntityModel(org.opensearch.ad.ml.EntityModel) ModelManager(org.opensearch.ad.ml.ModelManager) CacheProvider(org.opensearch.ad.caching.CacheProvider) ThresholdingResult(org.opensearch.ad.ml.ThresholdingResult) ClusterService(org.opensearch.cluster.service.ClusterService) CheckpointDao(org.opensearch.ad.ml.CheckpointDao) Random(java.util.Random) SimpleImmutableEntry(java.util.AbstractMap.SimpleImmutableEntry) AnomalyDetectionIndices(org.opensearch.ad.indices.AnomalyDetectionIndices) Map(java.util.Map)

Example 3 with AnomalyDetectionIndices

use of org.opensearch.ad.indices.AnomalyDetectionIndices in project anomaly-detection by opensearch-project.

the class EntityResultTransportActionTests method setUp.

@SuppressWarnings("unchecked")
@Override
@Before
public void setUp() throws Exception {
    super.setUp();
    actionFilters = mock(ActionFilters.class);
    transportService = mock(TransportService.class);
    adCircuitBreakerService = mock(ADCircuitBreakerService.class);
    when(adCircuitBreakerService.isOpen()).thenReturn(false);
    checkpointDao = mock(CheckpointDao.class);
    detectorId = "123";
    entities = new HashMap<>();
    start = 10L;
    end = 20L;
    request = new EntityResultRequest(detectorId, entities, start, end);
    clock = mock(Clock.class);
    now = Instant.now();
    when(clock.instant()).thenReturn(now);
    manager = new ModelManager(null, clock, 0, 0, 0, 0, 0, 0, null, null, mock(EntityColdStarter.class), null, null);
    provider = mock(CacheProvider.class);
    entityCache = mock(EntityCache.class);
    when(provider.get()).thenReturn(entityCache);
    String field = "a";
    detector = TestHelpers.randomAnomalyDetectorUsingCategoryFields(detectorId, Arrays.asList(field));
    stateManager = mock(NodeStateManager.class);
    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));
    cacheMissEntity = "0.0.0.1";
    cacheMissData = new double[] { 0.1 };
    cacheHitEntity = "0.0.0.2";
    cacheHitData = new double[] { 0.2 };
    cacheMissEntityObj = Entity.createSingleAttributeEntity(detector.getCategoryField().get(0), cacheMissEntity);
    entities.put(cacheMissEntityObj, cacheMissData);
    cacheHitEntityObj = Entity.createSingleAttributeEntity(detector.getCategoryField().get(0), cacheHitEntity);
    entities.put(cacheHitEntityObj, cacheHitData);
    tooLongEntity = randomAlphaOfLength(AnomalyDetectorSettings.MAX_ENTITY_LENGTH + 1);
    tooLongData = new double[] { 0.3 };
    entities.put(Entity.createSingleAttributeEntity(detector.getCategoryField().get(0), tooLongEntity), tooLongData);
    ModelState<EntityModel> state = MLUtil.randomModelState(new RandomModelStateConfig.Builder().fullModel(true).build());
    when(entityCache.get(eq(cacheMissEntityObj.getModelId(detectorId).get()), any())).thenReturn(null);
    when(entityCache.get(eq(cacheHitEntityObj.getModelId(detectorId).get()), any())).thenReturn(state);
    List<Entity> coldEntities = new ArrayList<>();
    coldEntities.add(cacheMissEntityObj);
    when(entityCache.selectUpdateCandidate(any(), anyString(), any())).thenReturn(Pair.of(new ArrayList<>(), coldEntities));
    settings = Settings.builder().put(AnomalyDetectorSettings.COOLDOWN_MINUTES.getKey(), TimeValue.timeValueMinutes(5)).build();
    AnomalyDetectionIndices indexUtil = mock(AnomalyDetectionIndices.class);
    when(indexUtil.getSchemaVersion(any())).thenReturn(CommonValue.NO_SCHEMA_VERSION);
    resultWriteQueue = mock(ResultWriteWorker.class);
    checkpointReadQueue = mock(CheckpointReadWorker.class);
    minSamples = 1;
    coldStarter = mock(EntityColdStarter.class);
    doAnswer(invocation -> {
        ModelState<EntityModel> modelState = invocation.getArgument(0);
        modelState.getModel().clear();
        return null;
    }).when(coldStarter).trainModelFromExistingSamples(any(), anyInt());
    coldEntityQueue = mock(ColdEntityWorker.class);
    entityResult = new EntityResultTransportAction(actionFilters, transportService, manager, adCircuitBreakerService, provider, stateManager, indexUtil, resultWriteQueue, checkpointReadQueue, coldEntityQueue, threadPool);
    // timeout in 60 seconds
    timeoutMs = 60000L;
}
Also used : Entity(org.opensearch.ad.model.Entity) EntityCache(org.opensearch.ad.caching.EntityCache) ArrayList(java.util.ArrayList) CheckpointReadWorker(org.opensearch.ad.ratelimit.CheckpointReadWorker) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Clock(java.time.Clock) ResultWriteWorker(org.opensearch.ad.ratelimit.ResultWriteWorker) NodeStateManager(org.opensearch.ad.NodeStateManager) RandomModelStateConfig(test.org.opensearch.ad.util.RandomModelStateConfig) EntityColdStarter(org.opensearch.ad.ml.EntityColdStarter) ColdEntityWorker(org.opensearch.ad.ratelimit.ColdEntityWorker) ADCircuitBreakerService(org.opensearch.ad.breaker.ADCircuitBreakerService) Optional(java.util.Optional) EntityModel(org.opensearch.ad.ml.EntityModel) ActionFilters(org.opensearch.action.support.ActionFilters) ModelManager(org.opensearch.ad.ml.ModelManager) CacheProvider(org.opensearch.ad.caching.CacheProvider) CheckpointDao(org.opensearch.ad.ml.CheckpointDao) ActionListener(org.opensearch.action.ActionListener) TransportService(org.opensearch.transport.TransportService) AnomalyDetectionIndices(org.opensearch.ad.indices.AnomalyDetectionIndices) Before(org.junit.Before)

Example 4 with AnomalyDetectionIndices

use of org.opensearch.ad.indices.AnomalyDetectionIndices 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 AnomalyDetectionIndices

use of org.opensearch.ad.indices.AnomalyDetectionIndices in project anomaly-detection by opensearch-project.

the class ValidateAnomalyDetectorActionHandlerTests method setUp.

@SuppressWarnings("unchecked")
@Override
@Before
public void setUp() throws Exception {
    super.setUp();
    MockitoAnnotations.initMocks(this);
    settings = Settings.EMPTY;
    clusterService = mock(ClusterService.class);
    channel = mock(ActionListener.class);
    transportService = mock(TransportService.class);
    anomalyDetectionIndices = mock(AnomalyDetectionIndices.class);
    when(anomalyDetectionIndices.doesAnomalyDetectorIndexExist()).thenReturn(true);
    detectorId = "123";
    seqNo = 0L;
    primaryTerm = 0L;
    clock = mock(Clock.class);
    refreshPolicy = WriteRequest.RefreshPolicy.IMMEDIATE;
    String field = "a";
    detector = TestHelpers.randomAnomalyDetectorUsingCategoryFields(detectorId, "timestamp", ImmutableList.of("test-index"), Arrays.asList(field));
    requestTimeout = new TimeValue(1000L);
    maxSingleEntityAnomalyDetectors = 1000;
    maxMultiEntityAnomalyDetectors = 10;
    maxAnomalyFeatures = 5;
    method = RestRequest.Method.POST;
    adTaskManager = mock(ADTaskManager.class);
    searchFeatureDao = mock(SearchFeatureDao.class);
    threadContext = new ThreadContext(settings);
    Mockito.doReturn(threadPool).when(clientMock).threadPool();
    Mockito.doReturn(threadContext).when(threadPool).getThreadContext();
}
Also used : ClusterService(org.opensearch.cluster.service.ClusterService) ActionListener(org.opensearch.action.ActionListener) TransportService(org.opensearch.transport.TransportService) ADTaskManager(org.opensearch.ad.task.ADTaskManager) AnomalyDetectionIndices(org.opensearch.ad.indices.AnomalyDetectionIndices) ThreadContext(org.opensearch.common.util.concurrent.ThreadContext) SearchFeatureDao(org.opensearch.ad.feature.SearchFeatureDao) Clock(java.time.Clock) TimeValue(org.opensearch.common.unit.TimeValue) Before(org.junit.Before)

Aggregations

AnomalyDetectionIndices (org.opensearch.ad.indices.AnomalyDetectionIndices)7 ActionListener (org.opensearch.action.ActionListener)5 Before (org.junit.Before)4 ClusterService (org.opensearch.cluster.service.ClusterService)4 Clock (java.time.Clock)3 CacheProvider (org.opensearch.ad.caching.CacheProvider)3 EntityCache (org.opensearch.ad.caching.EntityCache)3 SearchFeatureDao (org.opensearch.ad.feature.SearchFeatureDao)3 ADTaskManager (org.opensearch.ad.task.ADTaskManager)3 IndexUtils (org.opensearch.ad.util.IndexUtils)3 Settings (org.opensearch.common.settings.Settings)3 Random (java.util.Random)2 ADCircuitBreakerService (org.opensearch.ad.breaker.ADCircuitBreakerService)2 CheckpointDao (org.opensearch.ad.ml.CheckpointDao)2 EntityModel (org.opensearch.ad.ml.EntityModel)2 ModelManager (org.opensearch.ad.ml.ModelManager)2 ClientUtil (org.opensearch.ad.util.ClientUtil)2 Throttler (org.opensearch.ad.util.Throttler)2 ThreadContext (org.opensearch.common.util.concurrent.ThreadContext)2 TransportService (org.opensearch.transport.TransportService)2