use of org.opensearch.ad.cluster.HashRing in project anomaly-detection by opensearch-project.
the class EntityProfileTests method setUp.
@Override
public void setUp() throws Exception {
super.setUp();
state = new HashSet<EntityProfileName>();
state.add(EntityProfileName.STATE);
all = new HashSet<EntityProfileName>();
all.add(EntityProfileName.INIT_PROGRESS);
all.add(EntityProfileName.ENTITY_INFO);
all.add(EntityProfileName.MODELS);
model = new HashSet<EntityProfileName>();
model.add(EntityProfileName.MODELS);
hashRing = mock(HashRing.class);
actionFilters = mock(ActionFilters.class);
transportService = new TransportService(Settings.EMPTY, mock(Transport.class), null, TransportService.NOOP_TRANSPORT_INTERCEPTOR, x -> null, null, Collections.emptySet());
settings = Settings.EMPTY;
modelId = "yecrdnUBqurvo9uKU_d8_entity_app_0";
clusterService = mock(ClusterService.class);
cacheProvider = mock(CacheProvider.class);
EntityCache cache = mock(EntityCache.class);
updates = 1L;
when(cache.getTotalUpdates(anyString(), anyString())).thenReturn(updates);
when(cache.isActive(anyString(), anyString())).thenReturn(isActive);
when(cache.getLastActiveMs(anyString(), anyString())).thenReturn(lastActiveTimestamp);
Map<String, Long> modelSizeMap = new HashMap<>();
modelSizeMap.put(modelId, modelSize);
when(cache.getModelSize(anyString())).thenReturn(modelSizeMap);
when(cacheProvider.get()).thenReturn(cache);
action = new EntityProfileTransportAction(actionFilters, transportService, settings, hashRing, clusterService, cacheProvider);
future = new PlainActionFuture<>();
transportAddress1 = new TransportAddress(new InetSocketAddress(InetAddress.getByName("1.2.3.4"), 9300));
entity = Entity.createSingleAttributeEntity(categoryName, entityValue);
request = new EntityProfileRequest(detectorId, entity, state);
normalTransportInterceptor = new TransportInterceptor() {
@Override
public AsyncSender interceptSender(AsyncSender sender) {
return new AsyncSender() {
@Override
public <T extends TransportResponse> void sendRequest(Transport.Connection connection, String action, TransportRequest request, TransportRequestOptions options, TransportResponseHandler<T> handler) {
if (EntityProfileAction.NAME.equals(action)) {
sender.sendRequest(connection, action, request, options, entityProfileHandler(handler));
} else {
sender.sendRequest(connection, action, request, options, handler);
}
}
};
}
};
failureTransportInterceptor = new TransportInterceptor() {
@Override
public AsyncSender interceptSender(AsyncSender sender) {
return new AsyncSender() {
@Override
public <T extends TransportResponse> void sendRequest(Transport.Connection connection, String action, TransportRequest request, TransportRequestOptions options, TransportResponseHandler<T> handler) {
if (EntityProfileAction.NAME.equals(action)) {
sender.sendRequest(connection, action, request, options, entityFailureProfileandler(handler));
} else {
sender.sendRequest(connection, action, request, options, handler);
}
}
};
}
};
}
use of org.opensearch.ad.cluster.HashRing in project anomaly-detection by opensearch-project.
the class ADTaskManagerTests method setUp.
@Override
public void setUp() throws Exception {
super.setUp();
Instant now = Instant.now();
Instant startTime = now.minus(10, ChronoUnit.DAYS);
Instant endTime = now.minus(1, ChronoUnit.DAYS);
detectionDateRange = new DetectionDateRange(startTime, endTime);
settings = Settings.builder().put(MAX_OLD_AD_TASK_DOCS_PER_DETECTOR.getKey(), 2).put(BATCH_TASK_PIECE_INTERVAL_SECONDS.getKey(), 1).put(REQUEST_TIMEOUT.getKey(), TimeValue.timeValueSeconds(10)).build();
clusterSettings = clusterSetting(settings, MAX_OLD_AD_TASK_DOCS_PER_DETECTOR, BATCH_TASK_PIECE_INTERVAL_SECONDS, REQUEST_TIMEOUT, DELETE_AD_RESULT_WHEN_DELETE_DETECTOR, MAX_BATCH_TASK_PER_NODE, MAX_RUNNING_ENTITIES_PER_DETECTOR_FOR_HISTORICAL_ANALYSIS);
maxBatchTaskPerNode = MAX_BATCH_TASK_PER_NODE.get(settings);
clusterService = spy(new ClusterService(settings, clusterSettings, null));
client = mock(Client.class);
nodeFilter = mock(DiscoveryNodeFilterer.class);
detectionIndices = mock(AnomalyDetectionIndices.class);
adTaskCacheManager = mock(ADTaskCacheManager.class);
hashRing = mock(HashRing.class);
transportService = mock(TransportService.class);
threadPool = mock(ThreadPool.class);
threadContext = new ThreadContext(settings);
when(threadPool.getThreadContext()).thenReturn(threadContext);
when(client.threadPool()).thenReturn(threadPool);
indexAnomalyDetectorJobActionHandler = mock(IndexAnomalyDetectorJobActionHandler.class);
adTaskManager = spy(new ADTaskManager(settings, clusterService, client, TestHelpers.xContentRegistry(), detectionIndices, nodeFilter, hashRing, adTaskCacheManager, threadPool));
listener = spy(new ActionListener<AnomalyDetectorJobResponse>() {
@Override
public void onResponse(AnomalyDetectorJobResponse bulkItemResponses) {
}
@Override
public void onFailure(Exception e) {
}
});
node1 = new DiscoveryNode("nodeName1", "node1", new TransportAddress(TransportAddress.META_ADDRESS, 9300), emptyMap(), emptySet(), Version.CURRENT);
node2 = new DiscoveryNode("nodeName2", "node2", new TransportAddress(TransportAddress.META_ADDRESS, 9300), emptyMap(), emptySet(), Version.CURRENT);
maxRunningEntities = MAX_RUNNING_ENTITIES_PER_DETECTOR_FOR_HISTORICAL_ANALYSIS.get(settings).intValue();
ThreadContext threadContext = new ThreadContext(settings);
context = threadContext.stashContext();
}
use of org.opensearch.ad.cluster.HashRing in project anomaly-detection by opensearch-project.
the class ADTaskManagerTests method setupGetAndExecuteOnLatestADTasks.
@SuppressWarnings("unchecked")
private void setupGetAndExecuteOnLatestADTasks(ADTaskProfile adTaskProfile) {
String runningRealtimeHCTaskContent = runningHistoricalHCTaskContent.replace(ADTaskType.HISTORICAL_HC_DETECTOR.name(), ADTaskType.REALTIME_HC_DETECTOR.name()).replace(historicalTaskId, realtimeTaskId);
doAnswer(invocation -> {
ActionListener<SearchResponse> listener = invocation.getArgument(1);
SearchHit historicalTask = SearchHit.fromXContent(TestHelpers.parser(runningHistoricalHCTaskContent));
SearchHit realtimeTask = SearchHit.fromXContent(TestHelpers.parser(runningRealtimeHCTaskContent));
SearchHits searchHits = new SearchHits(new SearchHit[] { historicalTask, realtimeTask }, new TotalHits(2, TotalHits.Relation.EQUAL_TO), Float.NaN);
InternalSearchResponse response = new InternalSearchResponse(searchHits, InternalAggregations.EMPTY, null, null, false, null, 1);
SearchResponse searchResponse = new SearchResponse(response, null, 1, 1, 0, 100, ShardSearchFailure.EMPTY_ARRAY, SearchResponse.Clusters.EMPTY);
listener.onResponse(searchResponse);
return null;
}).when(client).search(any(), any());
String detectorId = randomAlphaOfLength(5);
Consumer<List<ADTask>> function = mock(Consumer.class);
ActionListener<AnomalyDetectorJobResponse> listener = mock(ActionListener.class);
doAnswer(invocation -> {
Consumer<DiscoveryNode[]> getNodeFunction = invocation.getArgument(0);
getNodeFunction.accept(new DiscoveryNode[] { node1, node2 });
return null;
}).when(hashRing).getAllEligibleDataNodesWithKnownAdVersion(any(), any());
doAnswer(invocation -> {
ActionListener<ADTaskProfileResponse> taskProfileResponseListener = invocation.getArgument(2);
AnomalyDetector detector = TestHelpers.randomDetector(ImmutableList.of(randomFeature(true)), randomAlphaOfLength(5), randomIntBetween(1, 10), MockSimpleLog.TIME_FIELD, ImmutableList.of(randomAlphaOfLength(5)));
ADTaskProfileNodeResponse nodeResponse = new ADTaskProfileNodeResponse(node1, adTaskProfile, Version.CURRENT);
ImmutableList<ADTaskProfileNodeResponse> nodes = ImmutableList.of(nodeResponse);
ADTaskProfileResponse taskProfileResponse = new ADTaskProfileResponse(new ClusterName("test"), nodes, ImmutableList.of());
taskProfileResponseListener.onResponse(taskProfileResponse);
return null;
}).doAnswer(invocation -> {
ActionListener<BulkByScrollResponse> updateResponselistener = invocation.getArgument(2);
BulkByScrollResponse response = mock(BulkByScrollResponse.class);
when(response.getBulkFailures()).thenReturn(null);
updateResponselistener.onResponse(response);
return null;
}).when(client).execute(any(), any(), any());
when(nodeFilter.getEligibleDataNodes()).thenReturn(new DiscoveryNode[] { node1, node2 });
doAnswer(invocation -> {
ActionListener<UpdateResponse> updateResponselistener = invocation.getArgument(1);
UpdateResponse response = new UpdateResponse(ShardId.fromString("[test][1]"), CommonName.MAPPING_TYPE, "1", 0L, 1L, 1L, DocWriteResponse.Result.UPDATED);
updateResponselistener.onResponse(response);
return null;
}).when(client).update(any(), any());
doAnswer(invocation -> {
ActionListener<GetResponse> getResponselistener = invocation.getArgument(1);
GetResponse response = new GetResponse(new GetResult(AnomalyDetectorJob.ANOMALY_DETECTOR_JOB_INDEX, MapperService.SINGLE_MAPPING_NAME, detectorId, UNASSIGNED_SEQ_NO, 0, -1, true, BytesReference.bytes(new AnomalyDetectorJob(detectorId, randomIntervalSchedule(), randomIntervalTimeConfiguration(), false, Instant.now().minusSeconds(60), Instant.now(), Instant.now(), 60L, TestHelpers.randomUser(), null).toXContent(TestHelpers.builder(), ToXContent.EMPTY_PARAMS)), Collections.emptyMap(), Collections.emptyMap()));
getResponselistener.onResponse(response);
return null;
}).when(client).get(any(), any());
}
Aggregations