use of org.opensearch.cluster.service.ClusterService in project anomaly-detection by opensearch-project.
the class ColdEntityWorkerTests method setUp.
@Override
public void setUp() throws Exception {
super.setUp();
clusterService = mock(ClusterService.class);
Settings settings = Settings.builder().put(AnomalyDetectorSettings.CHECKPOINT_READ_QUEUE_BATCH_SIZE.getKey(), 1).build();
ClusterSettings clusterSettings = new ClusterSettings(settings, Collections.unmodifiableSet(new HashSet<>(Arrays.asList(AnomalyDetectorSettings.EXPECTED_COLD_ENTITY_EXECUTION_TIME_IN_SECS, AnomalyDetectorSettings.COLD_ENTITY_QUEUE_MAX_HEAP_PERCENT, AnomalyDetectorSettings.CHECKPOINT_READ_QUEUE_BATCH_SIZE))));
when(clusterService.getClusterSettings()).thenReturn(clusterSettings);
readWorker = mock(CheckpointReadWorker.class);
// Integer.MAX_VALUE makes a huge heap
coldWorker = new ColdEntityWorker(Integer.MAX_VALUE, AnomalyDetectorSettings.ENTITY_FEATURE_REQUEST_SIZE_IN_BYTES, AnomalyDetectorSettings.COLD_ENTITY_QUEUE_MAX_HEAP_PERCENT, clusterService, new Random(42), mock(ADCircuitBreakerService.class), threadPool, settings, AnomalyDetectorSettings.MAX_QUEUED_TASKS_RATIO, clock, AnomalyDetectorSettings.MEDIUM_SEGMENT_PRUNE_RATIO, AnomalyDetectorSettings.LOW_SEGMENT_PRUNE_RATIO, AnomalyDetectorSettings.MAINTENANCE_FREQ_CONSTANT, readWorker, AnomalyDetectorSettings.HOURLY_MAINTENANCE, nodeStateManager);
request = new EntityFeatureRequest(Integer.MAX_VALUE, detectorId, RequestPriority.LOW, entity, new double[] { 0 }, 0);
request2 = new EntityFeatureRequest(Integer.MAX_VALUE, detectorId, RequestPriority.LOW, entity2, new double[] { 0 }, 0);
invalidRequest = new EntityFeatureRequest(Integer.MAX_VALUE, detectorId, RequestPriority.MEDIUM, entity2, new double[] { 0 }, 0);
requests = new ArrayList<>();
requests.add(request);
requests.add(request2);
requests.add(invalidRequest);
doAnswer(invocation -> {
Runnable runnable = invocation.getArgument(0);
runnable.run();
TimeValue value = invocation.getArgument(1);
// since we have only 1 request each time
long expectedExecutionPerRequestMilli = 1000 * AnomalyDetectorSettings.EXPECTED_COLD_ENTITY_EXECUTION_TIME_IN_SECS.getDefault(Settings.EMPTY);
long delay = value.getMillis();
assertTrue(delay >= expectedExecutionPerRequestMilli);
assertTrue(delay <= expectedExecutionPerRequestMilli * 2);
return null;
}).when(threadPool).schedule(any(), any(), any());
}
use of org.opensearch.cluster.service.ClusterService in project anomaly-detection by opensearch-project.
the class ResultWriteWorkerTests method setUp.
@Override
public void setUp() throws Exception {
super.setUp();
clusterService = mock(ClusterService.class);
ClusterSettings clusterSettings = new ClusterSettings(Settings.EMPTY, Collections.unmodifiableSet(new HashSet<>(Arrays.asList(AnomalyDetectorSettings.RESULT_WRITE_QUEUE_MAX_HEAP_PERCENT, AnomalyDetectorSettings.RESULT_WRITE_QUEUE_CONCURRENCY, AnomalyDetectorSettings.RESULT_WRITE_QUEUE_BATCH_SIZE))));
when(clusterService.getClusterSettings()).thenReturn(clusterSettings);
threadPool = mock(ThreadPool.class);
setUpADThreadPool(threadPool);
resultHandler = mock(MultiEntityResultHandler.class);
resultWriteQueue = new ResultWriteWorker(Integer.MAX_VALUE, AnomalyDetectorSettings.RESULT_WRITE_QUEUE_SIZE_IN_BYTES, AnomalyDetectorSettings.RESULT_WRITE_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, resultHandler, xContentRegistry(), nodeStateManager, AnomalyDetectorSettings.HOURLY_MAINTENANCE);
detectResult = TestHelpers.randomHCADAnomalyDetectResult(0.8, Double.NaN, null);
}
use of org.opensearch.cluster.service.ClusterService in project anomaly-detection by opensearch-project.
the class IndexAnomalyDetectorTransportActionTests method setUp.
@SuppressWarnings("unchecked")
@Override
@Before
public void setUp() throws Exception {
super.setUp();
clusterService = mock(ClusterService.class);
clusterSettings = new ClusterSettings(Settings.EMPTY, Collections.unmodifiableSet(new HashSet<>(Arrays.asList(AnomalyDetectorSettings.FILTER_BY_BACKEND_ROLES))));
when(clusterService.getClusterSettings()).thenReturn(clusterSettings);
ClusterName clusterName = new ClusterName("test");
Settings indexSettings = Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0).put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT).build();
final Settings.Builder existingSettings = Settings.builder().put(indexSettings).put(IndexMetadata.SETTING_INDEX_UUID, "test2UUID");
IndexMetadata indexMetaData = IndexMetadata.builder(AnomalyDetector.ANOMALY_DETECTORS_INDEX).settings(existingSettings).build();
final ImmutableOpenMap<String, IndexMetadata> indices = ImmutableOpenMap.<String, IndexMetadata>builder().fPut(AnomalyDetector.ANOMALY_DETECTORS_INDEX, indexMetaData).build();
ClusterState clusterState = ClusterState.builder(clusterName).metadata(Metadata.builder().indices(indices).build()).build();
when(clusterService.state()).thenReturn(clusterState);
adTaskManager = mock(ADTaskManager.class);
searchFeatureDao = mock(SearchFeatureDao.class);
action = new IndexAnomalyDetectorTransportAction(mock(TransportService.class), mock(ActionFilters.class), client(), clusterService, indexSettings(), mock(AnomalyDetectionIndices.class), xContentRegistry(), adTaskManager, searchFeatureDao);
task = mock(Task.class);
AnomalyDetector detector = TestHelpers.randomAnomalyDetector(ImmutableMap.of("testKey", "testValue"), Instant.now());
GetResponse getDetectorResponse = TestHelpers.createGetResponse(detector, detector.getDetectorId(), AnomalyDetector.ANOMALY_DETECTORS_INDEX);
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);
assertTrue(args[0] instanceof GetRequest);
assertTrue(args[1] instanceof ActionListener);
ActionListener<GetResponse> listener = (ActionListener<GetResponse>) args[1];
listener.onResponse(getDetectorResponse);
return null;
}).when(client).get(any(GetRequest.class), any());
SearchHits hits = new SearchHits(new SearchHit[] {}, null, Float.NaN);
SearchResponseSections searchSections = new SearchResponseSections(hits, null, null, false, false, null, 1);
SearchResponse searchResponse = new SearchResponse(searchSections, null, 1, 1, 0, 30, ShardSearchFailure.EMPTY_ARRAY, SearchResponse.Clusters.EMPTY);
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);
assertTrue(args[0] instanceof SearchRequest);
assertTrue(args[1] instanceof ActionListener);
ActionListener<SearchResponse> listener = (ActionListener<SearchResponse>) args[1];
listener.onResponse(searchResponse);
return null;
}).when(client).search(any(SearchRequest.class), any());
request = new IndexAnomalyDetectorRequest("1234", 4567, 7890, WriteRequest.RefreshPolicy.IMMEDIATE, detector, RestRequest.Method.PUT, TimeValue.timeValueSeconds(60), 1000, 10, 5);
response = new ActionListener<IndexAnomalyDetectorResponse>() {
@Override
public void onResponse(IndexAnomalyDetectorResponse indexResponse) {
// onResponse will not be called as we do not have the AD index
Assert.assertTrue(false);
}
@Override
public void onFailure(Exception e) {
Assert.assertTrue(true);
}
};
}
use of org.opensearch.cluster.service.ClusterService in project anomaly-detection by opensearch-project.
the class MultiEntityResultTests method setUpTransportInterceptor.
private <T extends TransportResponse> void setUpTransportInterceptor(Function<TransportResponseHandler<T>, TransportResponseHandler<T>> interceptor) {
entityResultInterceptor = new TransportInterceptor() {
@Override
public AsyncSender interceptSender(AsyncSender sender) {
return new AsyncSender() {
@SuppressWarnings("unchecked")
@Override
public <T2 extends TransportResponse> void sendRequest(Transport.Connection connection, String action, TransportRequest request, TransportRequestOptions options, TransportResponseHandler<T2> handler) {
if (action.equals(EntityResultAction.NAME)) {
sender.sendRequest(connection, action, request, options, interceptor.apply((TransportResponseHandler<T>) handler));
} else {
sender.sendRequest(connection, action, request, options, handler);
}
}
};
}
};
// we start support multi-category fields since 1.1
// Set version to 1.1 will force the outbound/inbound message to use 1.1 version
setupTestNodes(entityResultInterceptor, 5, settings, BwcTests.V_1_1_0, MAX_ENTITIES_PER_QUERY, PAGE_SIZE);
TransportService realTransportService = testNodes[0].transportService;
ClusterService realClusterService = testNodes[0].clusterService;
action = new AnomalyResultTransportAction(new ActionFilters(Collections.emptySet()), realTransportService, settings, client, stateManager, featureQuery, normalModelManager, hashRing, realClusterService, indexNameResolver, adCircuitBreakerService, adStats, threadPool, xContentRegistry(), adTaskManager);
}
use of org.opensearch.cluster.service.ClusterService in project anomaly-detection by opensearch-project.
the class PreviewAnomalyDetectorTransportActionTests method setUp.
@Override
@Before
public void setUp() throws Exception {
super.setUp();
task = mock(Task.class);
clusterService = mock(ClusterService.class);
ClusterSettings clusterSettings = new ClusterSettings(Settings.EMPTY, Collections.unmodifiableSet(new HashSet<>(Arrays.asList(AnomalyDetectorSettings.MAX_ANOMALY_FEATURES, AnomalyDetectorSettings.FILTER_BY_BACKEND_ROLES, AnomalyDetectorSettings.PAGE_SIZE, AnomalyDetectorSettings.MAX_CONCURRENT_PREVIEW))));
when(clusterService.getClusterSettings()).thenReturn(clusterSettings);
ClusterName clusterName = new ClusterName("test");
Settings indexSettings = Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0).put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT).build();
final Settings.Builder existingSettings = Settings.builder().put(indexSettings).put(IndexMetadata.SETTING_INDEX_UUID, "test2UUID");
IndexMetadata indexMetaData = IndexMetadata.builder(AnomalyDetector.ANOMALY_DETECTORS_INDEX).settings(existingSettings).build();
final ImmutableOpenMap<String, IndexMetadata> indices = ImmutableOpenMap.<String, IndexMetadata>builder().fPut(AnomalyDetector.ANOMALY_DETECTORS_INDEX, indexMetaData).build();
ClusterState clusterState = ClusterState.builder(clusterName).metadata(Metadata.builder().indices(indices).build()).build();
when(clusterService.state()).thenReturn(clusterState);
featureManager = mock(FeatureManager.class);
modelManager = mock(ModelManager.class);
runner = new AnomalyDetectorRunner(modelManager, featureManager, AnomalyDetectorSettings.MAX_PREVIEW_RESULTS);
circuitBreaker = mock(ADCircuitBreakerService.class);
when(circuitBreaker.isOpen()).thenReturn(false);
action = new PreviewAnomalyDetectorTransportAction(Settings.EMPTY, mock(TransportService.class), clusterService, mock(ActionFilters.class), client(), runner, xContentRegistry(), circuitBreaker);
}
Aggregations