Search in sources :

Example 6 with ClientUtil

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);
}
Also used : DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) ClusterSettings(org.opensearch.common.settings.ClusterSettings) ClientUtil(org.opensearch.ad.util.ClientUtil) Setting(org.opensearch.common.settings.Setting) Client(org.opensearch.client.Client) Clock(java.time.Clock) GetResponse(org.opensearch.action.get.GetResponse) Throttler(org.opensearch.ad.util.Throttler) HashSet(java.util.HashSet) Before(org.junit.Before)

Example 7 with ClientUtil

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);
}
Also used : ClientUtil(org.opensearch.ad.util.ClientUtil) ThresholdedRandomCutForestMapper(com.amazon.randomcutforest.parkservices.state.ThresholdedRandomCutForestMapper) RandomCutForestMapper(com.amazon.randomcutforest.state.RandomCutForestMapper) AnomalyDetectionIndices(org.opensearch.ad.indices.AnomalyDetectionIndices) V1JsonToV2StateConverter(com.amazon.randomcutforest.serialize.json.v1.V1JsonToV2StateConverter) Client(org.opensearch.client.Client) GenericObjectPool(org.apache.commons.pool2.impl.GenericObjectPool) Before(org.junit.Before)

Example 8 with ClientUtil

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));
}
Also used : DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) ClusterSettings(org.opensearch.common.settings.ClusterSettings) ClientUtil(org.opensearch.ad.util.ClientUtil) ThreadPool(org.opensearch.threadpool.ThreadPool) IntervalTimeConfiguration(org.opensearch.ad.model.IntervalTimeConfiguration) SearchFeatureDao(org.opensearch.ad.feature.SearchFeatureDao) Clock(java.time.Clock) NodeStateManager(org.opensearch.ad.NodeStateManager) IntegerSensitiveSingleFeatureLinearUniformInterpolator(org.opensearch.ad.dataprocessor.IntegerSensitiveSingleFeatureLinearUniformInterpolator) GetRequest(org.opensearch.action.get.GetRequest) SingleFeatureLinearUniformInterpolator(org.opensearch.ad.dataprocessor.SingleFeatureLinearUniformInterpolator) IntegerSensitiveSingleFeatureLinearUniformInterpolator(org.opensearch.ad.dataprocessor.IntegerSensitiveSingleFeatureLinearUniformInterpolator) LinearUniformInterpolator(org.opensearch.ad.dataprocessor.LinearUniformInterpolator) Client(org.opensearch.client.Client) FeatureManager(org.opensearch.ad.feature.FeatureManager) HashSet(java.util.HashSet) Setting(org.opensearch.common.settings.Setting) CountDownLatch(java.util.concurrent.CountDownLatch) GetResponse(org.opensearch.action.get.GetResponse) SingleFeatureLinearUniformInterpolator(org.opensearch.ad.dataprocessor.SingleFeatureLinearUniformInterpolator) IntegerSensitiveSingleFeatureLinearUniformInterpolator(org.opensearch.ad.dataprocessor.IntegerSensitiveSingleFeatureLinearUniformInterpolator) CheckpointWriteWorker(org.opensearch.ad.ratelimit.CheckpointWriteWorker) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ClusterService(org.opensearch.cluster.service.ClusterService) ActionListener(org.opensearch.action.ActionListener)

Example 9 with ClientUtil

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));
}
Also used : SearchRequest(org.opensearch.action.search.SearchRequest) ClientUtil(org.opensearch.ad.util.ClientUtil) AnomalyDetector(org.opensearch.ad.model.AnomalyDetector)

Example 10 with ClientUtil

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();
}
Also used : ActionListener(org.opensearch.action.ActionListener) ClientUtil(org.opensearch.ad.util.ClientUtil) IndexNotFoundException(org.opensearch.index.IndexNotFoundException) OpenSearchException(org.opensearch.OpenSearchException) Clock(java.time.Clock) BulkByScrollResponse(org.opensearch.index.reindex.BulkByScrollResponse)

Aggregations

ClientUtil (org.opensearch.ad.util.ClientUtil)20 Client (org.opensearch.client.Client)14 Clock (java.time.Clock)13 ActionListener (org.opensearch.action.ActionListener)12 GetResponse (org.opensearch.action.get.GetResponse)9 IOException (java.io.IOException)8 HashMap (java.util.HashMap)8 Before (org.junit.Before)8 GetRequest (org.opensearch.action.get.GetRequest)8 AnomalyDetectionIndices (org.opensearch.ad.indices.AnomalyDetectionIndices)8 ThresholdedRandomCutForestMapper (com.amazon.randomcutforest.parkservices.state.ThresholdedRandomCutForestMapper)7 V1JsonToV2StateConverter (com.amazon.randomcutforest.serialize.json.v1.V1JsonToV2StateConverter)7 RandomCutForestMapper (com.amazon.randomcutforest.state.RandomCutForestMapper)7 Arrays (java.util.Arrays)7 Map (java.util.Map)7 Entry (java.util.Map.Entry)7 Optional (java.util.Optional)7 CountDownLatch (java.util.concurrent.CountDownLatch)7 GenericObjectPool (org.apache.commons.pool2.impl.GenericObjectPool)7 LogManager (org.apache.logging.log4j.LogManager)7