use of org.opensearch.ad.model.ADEntityTaskProfile in project anomaly-detection by opensearch-project.
the class ADTaskManager method getADTaskProfile.
/**
* Get AD task profile.
* @param adDetectorLevelTask detector level task
* @param listener action listener
*/
private void getADTaskProfile(ADTask adDetectorLevelTask, ActionListener<ADTaskProfile> listener) {
String detectorId = adDetectorLevelTask.getDetectorId();
hashRing.getAllEligibleDataNodesWithKnownAdVersion(dataNodes -> {
ADTaskProfileRequest adTaskProfileRequest = new ADTaskProfileRequest(detectorId, dataNodes);
client.execute(ADTaskProfileAction.INSTANCE, adTaskProfileRequest, ActionListener.wrap(response -> {
if (response.hasFailures()) {
listener.onFailure(response.failures().get(0));
return;
}
List<ADEntityTaskProfile> adEntityTaskProfiles = new ArrayList<>();
ADTaskProfile detectorTaskProfile = new ADTaskProfile(adDetectorLevelTask);
for (ADTaskProfileNodeResponse node : response.getNodes()) {
ADTaskProfile taskProfile = node.getAdTaskProfile();
if (taskProfile != null) {
if (taskProfile.getNodeId() != null) {
// HC detector: task profile from coordinating node
// Single entity detector: task profile from worker node
detectorTaskProfile.setTaskId(taskProfile.getTaskId());
detectorTaskProfile.setShingleSize(taskProfile.getShingleSize());
detectorTaskProfile.setRcfTotalUpdates(taskProfile.getRcfTotalUpdates());
detectorTaskProfile.setThresholdModelTrained(taskProfile.getThresholdModelTrained());
detectorTaskProfile.setThresholdModelTrainingDataSize(taskProfile.getThresholdModelTrainingDataSize());
detectorTaskProfile.setModelSizeInBytes(taskProfile.getModelSizeInBytes());
detectorTaskProfile.setNodeId(taskProfile.getNodeId());
detectorTaskProfile.setTotalEntitiesCount(taskProfile.getTotalEntitiesCount());
detectorTaskProfile.setDetectorTaskSlots(taskProfile.getDetectorTaskSlots());
detectorTaskProfile.setPendingEntitiesCount(taskProfile.getPendingEntitiesCount());
detectorTaskProfile.setRunningEntitiesCount(taskProfile.getRunningEntitiesCount());
detectorTaskProfile.setRunningEntities(taskProfile.getRunningEntities());
detectorTaskProfile.setAdTaskType(taskProfile.getAdTaskType());
}
if (taskProfile.getEntityTaskProfiles() != null) {
adEntityTaskProfiles.addAll(taskProfile.getEntityTaskProfiles());
}
}
}
if (adEntityTaskProfiles != null && adEntityTaskProfiles.size() > 0) {
detectorTaskProfile.setEntityTaskProfiles(adEntityTaskProfiles);
}
listener.onResponse(detectorTaskProfile);
}, e -> {
logger.error("Failed to get task profile for task " + adDetectorLevelTask.getTaskId(), e);
listener.onFailure(e);
}));
}, listener);
}
use of org.opensearch.ad.model.ADEntityTaskProfile in project anomaly-detection by opensearch-project.
the class ADTaskManager method getLocalADTaskProfilesByDetectorId.
/**
* Get local task profiles of detector.
* @param detectorId detector id
* @return list of AD task profile
*/
public ADTaskProfile getLocalADTaskProfilesByDetectorId(String detectorId) {
List<String> tasksOfDetector = adTaskCacheManager.getTasksOfDetector(detectorId);
ADTaskProfile detectorTaskProfile = null;
String localNodeId = clusterService.localNode().getId();
if (adTaskCacheManager.isHCTaskRunning(detectorId)) {
detectorTaskProfile = new ADTaskProfile();
if (adTaskCacheManager.isHCTaskCoordinatingNode(detectorId)) {
detectorTaskProfile.setNodeId(localNodeId);
detectorTaskProfile.setTaskId(adTaskCacheManager.getDetectorTaskId(detectorId));
detectorTaskProfile.setDetectorTaskSlots(adTaskCacheManager.getDetectorTaskSlots(detectorId));
detectorTaskProfile.setTotalEntitiesInited(adTaskCacheManager.topEntityInited(detectorId));
detectorTaskProfile.setTotalEntitiesCount(adTaskCacheManager.getTopEntityCount(detectorId));
detectorTaskProfile.setPendingEntitiesCount(adTaskCacheManager.getPendingEntityCount(detectorId));
detectorTaskProfile.setRunningEntitiesCount(adTaskCacheManager.getRunningEntityCount(detectorId));
detectorTaskProfile.setRunningEntities(adTaskCacheManager.getRunningEntities(detectorId));
detectorTaskProfile.setAdTaskType(ADTaskType.HISTORICAL_HC_DETECTOR.name());
Instant latestHCTaskRunTime = adTaskCacheManager.getLatestHCTaskRunTime(detectorId);
if (latestHCTaskRunTime != null) {
detectorTaskProfile.setLatestHCTaskRunTime(latestHCTaskRunTime.toEpochMilli());
}
}
if (tasksOfDetector.size() > 0) {
List<ADEntityTaskProfile> entityTaskProfiles = new ArrayList<>();
tasksOfDetector.forEach(taskId -> {
ADEntityTaskProfile entityTaskProfile = new ADEntityTaskProfile(adTaskCacheManager.getShingle(taskId).size(), adTaskCacheManager.getTRcfModel(taskId).getForest().getTotalUpdates(), adTaskCacheManager.isThresholdModelTrained(taskId), adTaskCacheManager.getThresholdModelTrainingDataSize(taskId), adTaskCacheManager.getModelSize(taskId), localNodeId, adTaskCacheManager.getEntity(taskId), taskId, ADTaskType.HISTORICAL_HC_ENTITY.name());
entityTaskProfiles.add(entityTaskProfile);
});
detectorTaskProfile.setEntityTaskProfiles(entityTaskProfiles);
}
} else {
if (tasksOfDetector.size() > 1) {
String error = "Multiple tasks are running for detector: " + detectorId + ". You can stop detector to kill all running tasks.";
logger.error(error);
throw new LimitExceededException(error);
}
if (tasksOfDetector.size() == 1) {
String taskId = tasksOfDetector.get(0);
detectorTaskProfile = new ADTaskProfile(adTaskCacheManager.getDetectorTaskId(detectorId), adTaskCacheManager.getShingle(taskId).size(), adTaskCacheManager.getTRcfModel(taskId).getForest().getTotalUpdates(), adTaskCacheManager.isThresholdModelTrained(taskId), adTaskCacheManager.getThresholdModelTrainingDataSize(taskId), adTaskCacheManager.getModelSize(taskId), localNodeId);
// Single-flow detector only has 1 task slot.
// Can't use adTaskCacheManager.getDetectorTaskSlots(detectorId) here as task may run on worker node.
// Detector task slots stored in coordinating node cache.
detectorTaskProfile.setDetectorTaskSlots(1);
}
}
threadPool.executor(AD_BATCH_TASK_THREAD_POOL_NAME).execute(() -> {
// Clean expired HC batch task run states as it may exists after HC historical analysis done if user cancel
// before querying top entities done. We will clean it in hourly cron, check "maintainRunningHistoricalTasks"
// method. Clean it up here when get task profile to release memory earlier.
adTaskCacheManager.cleanExpiredHCBatchTaskRunStates();
});
logger.debug("Local AD task profile of detector {}: {}", detectorId, detectorTaskProfile);
return detectorTaskProfile;
}
Aggregations