use of org.opensearch.ad.constant.CommonErrorMessages.NO_ELIGIBLE_NODE_TO_RUN_DETECTOR in project anomaly-detection by opensearch-project.
the class ADBatchTaskRunner method dispatchTask.
private void dispatchTask(ADTask adTask, ActionListener<DiscoveryNode> listener) {
hashRing.getNodesWithSameLocalAdVersion(dataNodes -> {
ADStatsRequest adStatsRequest = new ADStatsRequest(dataNodes);
adStatsRequest.addAll(ImmutableSet.of(AD_EXECUTING_BATCH_TASK_COUNT.getName(), JVM_HEAP_USAGE.getName()));
client.execute(ADStatsNodesAction.INSTANCE, adStatsRequest, ActionListener.wrap(adStatsResponse -> {
List<ADStatsNodeResponse> candidateNodeResponse = adStatsResponse.getNodes().stream().filter(stat -> (long) stat.getStatsMap().get(JVM_HEAP_USAGE.getName()) < DEFAULT_JVM_HEAP_USAGE_THRESHOLD).collect(Collectors.toList());
if (candidateNodeResponse.size() == 0) {
StringBuilder errorMessageBuilder = new StringBuilder("All nodes' memory usage exceeds limitation ").append(DEFAULT_JVM_HEAP_USAGE_THRESHOLD).append("%. ").append(NO_ELIGIBLE_NODE_TO_RUN_DETECTOR).append(adTask.getDetectorId());
String errorMessage = errorMessageBuilder.toString();
logger.warn(errorMessage + ", task id " + adTask.getTaskId() + ", " + adTask.getTaskType());
listener.onFailure(new LimitExceededException(adTask.getDetectorId(), errorMessage));
return;
}
candidateNodeResponse = candidateNodeResponse.stream().filter(stat -> (Long) stat.getStatsMap().get(AD_EXECUTING_BATCH_TASK_COUNT.getName()) < maxAdBatchTaskPerNode).collect(Collectors.toList());
if (candidateNodeResponse.size() == 0) {
StringBuilder errorMessageBuilder = new StringBuilder("All nodes' executing batch tasks exceeds limitation ").append(NO_ELIGIBLE_NODE_TO_RUN_DETECTOR).append(adTask.getDetectorId());
String errorMessage = errorMessageBuilder.toString();
logger.warn(errorMessage + ", task id " + adTask.getTaskId() + ", " + adTask.getTaskType());
listener.onFailure(new LimitExceededException(adTask.getDetectorId(), errorMessage));
return;
}
Optional<ADStatsNodeResponse> targetNode = candidateNodeResponse.stream().sorted((ADStatsNodeResponse r1, ADStatsNodeResponse r2) -> {
int result = ((Long) r1.getStatsMap().get(AD_EXECUTING_BATCH_TASK_COUNT.getName())).compareTo((Long) r2.getStatsMap().get(AD_EXECUTING_BATCH_TASK_COUNT.getName()));
if (result == 0) {
// JVM heap usage.
return ((Long) r1.getStatsMap().get(JVM_HEAP_USAGE.getName())).compareTo((Long) r2.getStatsMap().get(JVM_HEAP_USAGE.getName()));
}
return result;
}).findFirst();
listener.onResponse(targetNode.get().getNode());
}, exception -> {
logger.error("Failed to get node's task stats", exception);
listener.onFailure(exception);
}));
}, listener);
}
Aggregations