use of org.opensearch.client.Client in project anomaly-detection by opensearch-project.
the class StatsAnomalyDetectorTransportAction method getClusterStats.
/**
* Make async request to get the number of detectors in AnomalyDetector.ANOMALY_DETECTORS_INDEX if necessary
* and, onResponse, gather the cluster statistics
*
* @param client Client
* @param listener MultiResponsesDelegateActionListener to be used once both requests complete
* @param adStatsRequest Request containing stats to be retrieved
*/
private void getClusterStats(Client client, MultiResponsesDelegateActionListener<ADStatsResponse> listener, ADStatsRequest adStatsRequest) {
ADStatsResponse adStatsResponse = new ADStatsResponse();
if ((adStatsRequest.getStatsToBeRetrieved().contains(StatNames.DETECTOR_COUNT.getName()) || adStatsRequest.getStatsToBeRetrieved().contains(StatNames.SINGLE_ENTITY_DETECTOR_COUNT.getName()) || adStatsRequest.getStatsToBeRetrieved().contains(StatNames.MULTI_ENTITY_DETECTOR_COUNT.getName())) && clusterService.state().getRoutingTable().hasIndex(AnomalyDetector.ANOMALY_DETECTORS_INDEX)) {
TermsAggregationBuilder termsAgg = AggregationBuilders.terms(DETECTOR_TYPE_AGG).field(AnomalyDetector.DETECTOR_TYPE_FIELD);
SearchRequest request = new SearchRequest().indices(AnomalyDetector.ANOMALY_DETECTORS_INDEX).source(new SearchSourceBuilder().aggregation(termsAgg).size(0).trackTotalHits(true));
client.search(request, ActionListener.wrap(r -> {
StringTerms aggregation = r.getAggregations().get(DETECTOR_TYPE_AGG);
List<StringTerms.Bucket> buckets = aggregation.getBuckets();
long totalDetectors = r.getHits().getTotalHits().value;
long totalSingleEntityDetectors = 0;
long totalMultiEntityDetectors = 0;
for (StringTerms.Bucket b : buckets) {
if (AnomalyDetectorType.SINGLE_ENTITY.name().equals(b.getKeyAsString()) || AnomalyDetectorType.REALTIME_SINGLE_ENTITY.name().equals(b.getKeyAsString()) || AnomalyDetectorType.HISTORICAL_SINGLE_ENTITY.name().equals(b.getKeyAsString())) {
totalSingleEntityDetectors += b.getDocCount();
}
if (AnomalyDetectorType.MULTI_ENTITY.name().equals(b.getKeyAsString()) || AnomalyDetectorType.REALTIME_MULTI_ENTITY.name().equals(b.getKeyAsString()) || AnomalyDetectorType.HISTORICAL_MULTI_ENTITY.name().equals(b.getKeyAsString())) {
totalMultiEntityDetectors += b.getDocCount();
}
}
if (adStatsRequest.getStatsToBeRetrieved().contains(StatNames.DETECTOR_COUNT.getName())) {
adStats.getStat(StatNames.DETECTOR_COUNT.getName()).setValue(totalDetectors);
}
if (adStatsRequest.getStatsToBeRetrieved().contains(StatNames.SINGLE_ENTITY_DETECTOR_COUNT.getName())) {
adStats.getStat(StatNames.SINGLE_ENTITY_DETECTOR_COUNT.getName()).setValue(totalSingleEntityDetectors);
}
if (adStatsRequest.getStatsToBeRetrieved().contains(StatNames.MULTI_ENTITY_DETECTOR_COUNT.getName())) {
adStats.getStat(StatNames.MULTI_ENTITY_DETECTOR_COUNT.getName()).setValue(totalMultiEntityDetectors);
}
adStatsResponse.setClusterStats(getClusterStatsMap(adStatsRequest));
listener.onResponse(adStatsResponse);
}, e -> listener.onFailure(e)));
} else {
adStatsResponse.setClusterStats(getClusterStatsMap(adStatsRequest));
listener.onResponse(adStatsResponse);
}
}
use of org.opensearch.client.Client in project anomaly-detection by opensearch-project.
the class DeleteAnomalyDetectorTransportAction method doExecute.
@Override
protected void doExecute(Task task, DeleteAnomalyDetectorRequest request, ActionListener<DeleteResponse> actionListener) {
String detectorId = request.getDetectorID();
LOG.info("Delete anomaly detector job {}", detectorId);
User user = getUserContext(client);
ActionListener<DeleteResponse> listener = wrapRestActionListener(actionListener, FAIL_TO_DELETE_DETECTOR);
// By the time request reaches here, the user permissions are validated by Security plugin.
try (ThreadContext.StoredContext context = client.threadPool().getThreadContext().stashContext()) {
resolveUserAndExecute(user, detectorId, filterByEnabled, listener, (anomalyDetector) -> adTaskManager.getDetector(detectorId, detector -> {
if (!detector.isPresent()) {
// In a mixed cluster, if delete detector request routes to node running AD1.0, then it will
// not delete detector tasks. User can re-delete these deleted detector after cluster upgraded,
// in that case, the detector is not present.
LOG.info("Can't find anomaly detector {}", detectorId);
adTaskManager.deleteADTasks(detectorId, () -> deleteAnomalyDetectorJobDoc(detectorId, listener), listener);
return;
}
// Check if there is realtime job or historical analysis task running. If none of these running, we
// can delete the detector.
getDetectorJob(detectorId, listener, () -> {
adTaskManager.getAndExecuteOnLatestDetectorLevelTask(detectorId, HISTORICAL_DETECTOR_TASK_TYPES, adTask -> {
if (adTask.isPresent() && !adTask.get().isDone()) {
listener.onFailure(new OpenSearchStatusException("Detector is running", RestStatus.INTERNAL_SERVER_ERROR));
} else {
adTaskManager.deleteADTasks(detectorId, () -> deleteAnomalyDetectorJobDoc(detectorId, listener), listener);
}
}, transportService, true, listener);
});
}, listener), client, clusterService, xContentRegistry);
} catch (Exception e) {
LOG.error(e);
listener.onFailure(e);
}
}
use of org.opensearch.client.Client in project k-NN by opensearch-project.
the class TrainingJobRouterTransportActionTests method testMultiNode_withCapacity_withoutPreferredAvailable.
public void testMultiNode_withCapacity_withoutPreferredAvailable() {
// Mock datanodes in the cluster through mocking the cluster service
List<String> nodeIds = ImmutableList.of("node-1", "node-2", "node-3");
String preferredNode = nodeIds.get(2);
ImmutableOpenMap<String, DiscoveryNode> discoveryNodesMap = generateDiscoveryNodes(nodeIds);
ClusterService clusterService = generateMockedClusterService(discoveryNodesMap);
// Create a response to be returned with job route decision info
List<TrainingJobRouteDecisionInfoNodeResponse> responseList = new ArrayList<>();
// First node has capacity
responseList.add(new TrainingJobRouteDecisionInfoNodeResponse(discoveryNodesMap.get(nodeIds.get(0)), 0));
// Second node has capacity
responseList.add(new TrainingJobRouteDecisionInfoNodeResponse(discoveryNodesMap.get(nodeIds.get(1)), 0));
// Third node with no capacity (preferred node)
responseList.add(new TrainingJobRouteDecisionInfoNodeResponse(discoveryNodesMap.get(nodeIds.get(1)), 1));
TrainingJobRouteDecisionInfoResponse infoResponse = new TrainingJobRouteDecisionInfoResponse(ClusterName.DEFAULT, responseList, Collections.emptyList());
TransportService transportService = mock(TransportService.class);
Client client = mock(Client.class);
// Setup the action
TrainingJobRouterTransportAction transportAction = new TrainingJobRouterTransportAction(transportService, new ActionFilters(Collections.emptySet()), clusterService, client);
// Select the node
DiscoveryNode selectedNode = transportAction.selectNode(preferredNode, infoResponse);
assertNotNull(selectedNode);
assertNotEquals(preferredNode, selectedNode.getId());
}
use of org.opensearch.client.Client in project k-NN by opensearch-project.
the class TrainingJobRouterTransportActionTests method testSingleNode_withoutCapacity.
public void testSingleNode_withoutCapacity() {
// Mock datanodes in the cluster through mocking the cluster service
List<String> nodeIds = ImmutableList.of("node-1");
ImmutableOpenMap<String, DiscoveryNode> discoveryNodesMap = generateDiscoveryNodes(nodeIds);
ClusterService clusterService = generateMockedClusterService(discoveryNodesMap);
// Create a response to be returned with job route decision info
List<TrainingJobRouteDecisionInfoNodeResponse> responseList = new ArrayList<>();
nodeIds.forEach(id -> responseList.add(new TrainingJobRouteDecisionInfoNodeResponse(discoveryNodesMap.get(id), // node has no capacity
1)));
TrainingJobRouteDecisionInfoResponse infoResponse = new TrainingJobRouteDecisionInfoResponse(ClusterName.DEFAULT, responseList, Collections.emptyList());
TransportService transportService = mock(TransportService.class);
Client client = mock(Client.class);
// Setup the action
TrainingJobRouterTransportAction transportAction = new TrainingJobRouterTransportAction(transportService, new ActionFilters(Collections.emptySet()), clusterService, client);
// Select the node
DiscoveryNode selectedNode = transportAction.selectNode(null, infoResponse);
assertNull(selectedNode);
}
use of org.opensearch.client.Client in project k-NN by opensearch-project.
the class TrainingJobRouterTransportActionTests method testMultiNode_withCapacity_withPreferredAvailable.
public void testMultiNode_withCapacity_withPreferredAvailable() {
// Mock datanodes in the cluster through mocking the cluster service
List<String> nodeIds = ImmutableList.of("node-1", "node-2", "node-3");
String preferredNode = nodeIds.get(2);
ImmutableOpenMap<String, DiscoveryNode> discoveryNodesMap = generateDiscoveryNodes(nodeIds);
ClusterService clusterService = generateMockedClusterService(discoveryNodesMap);
// Create a response to be returned with job route decision info
List<TrainingJobRouteDecisionInfoNodeResponse> responseList = new ArrayList<>();
// First node has capacity
responseList.add(new TrainingJobRouteDecisionInfoNodeResponse(discoveryNodesMap.get(nodeIds.get(0)), 0));
// Second node has capacity
responseList.add(new TrainingJobRouteDecisionInfoNodeResponse(discoveryNodesMap.get(nodeIds.get(1)), 0));
// Third node with capacity
responseList.add(new TrainingJobRouteDecisionInfoNodeResponse(discoveryNodesMap.get(nodeIds.get(2)), 0));
TrainingJobRouteDecisionInfoResponse infoResponse = new TrainingJobRouteDecisionInfoResponse(ClusterName.DEFAULT, responseList, Collections.emptyList());
TransportService transportService = mock(TransportService.class);
Client client = mock(Client.class);
// Setup the action
TrainingJobRouterTransportAction transportAction = new TrainingJobRouterTransportAction(transportService, new ActionFilters(Collections.emptySet()), clusterService, client);
// Select the node
DiscoveryNode selectedNode = transportAction.selectNode(preferredNode, infoResponse);
assertEquals(preferredNode, selectedNode.getId());
}
Aggregations