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);
}
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);
}
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;
}
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());
}
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();
}
Aggregations