use of org.opensearch.ad.model.ADTaskType.REALTIME_TASK_TYPES in project anomaly-detection by opensearch-project.
the class ADTaskManager method initRealtimeTaskCacheAndCleanupStaleCache.
/**
* Init realtime task cache and clean up realtime task cache on old coordinating node. Realtime AD
* depends on job scheduler to choose node (job coordinating node) to run AD job. Nodes have primary
* or replica shard of AD job index are candidate to run AD job. Job scheduler will build hash ring
* on these candidate nodes and choose one to run AD job. If AD job index shard relocated, for example
* new node added into cluster, then job scheduler will rebuild hash ring and may choose different
* node to run AD job. So we need to init realtime task cache on new AD job coordinating node and
* clean up cache on old coordinating node.
*
* If realtime task cache inited for the first time on this node, listener will return true; otherwise
* listener will return false.
*
* @param detectorId detector id
* @param detector anomaly detector
* @param transportService transport service
* @param listener listener
*/
public void initRealtimeTaskCacheAndCleanupStaleCache(String detectorId, AnomalyDetector detector, TransportService transportService, ActionListener<Boolean> listener) {
try {
if (adTaskCacheManager.getRealtimeTaskCache(detectorId) != null) {
listener.onResponse(false);
return;
}
getAndExecuteOnLatestDetectorLevelTask(detectorId, REALTIME_TASK_TYPES, (adTaskOptional) -> {
if (!adTaskOptional.isPresent()) {
logger.debug("Can't find realtime task for detector {}, init realtime task cache directly", detectorId);
AnomalyDetectorFunction function = () -> createNewADTask(detector, null, detector.getUser(), clusterService.localNode().getId(), ActionListener.wrap(r -> {
logger.info("Recreate realtime task successfully for detector {}", detectorId);
adTaskCacheManager.initRealtimeTaskCache(detectorId, detector.getDetectorIntervalInMilliseconds());
listener.onResponse(true);
}, e -> {
logger.error("Failed to recreate realtime task for detector " + detectorId, e);
listener.onFailure(e);
}));
recreateRealtimeTask(function, listener);
return;
}
ADTask adTask = adTaskOptional.get();
String localNodeId = clusterService.localNode().getId();
String oldCoordinatingNode = adTask.getCoordinatingNode();
if (oldCoordinatingNode != null && !localNodeId.equals(oldCoordinatingNode)) {
logger.warn("AD realtime job coordinating node changed from {} to this node {} for detector {}", oldCoordinatingNode, localNodeId, detectorId);
cleanDetectorCache(adTask, transportService, () -> {
logger.info("Realtime task cache cleaned on old coordinating node {} for detector {}", oldCoordinatingNode, detectorId);
adTaskCacheManager.initRealtimeTaskCache(detectorId, detector.getDetectorIntervalInMilliseconds());
listener.onResponse(true);
}, listener);
} else {
logger.info("Init realtime task cache for detector {}", detectorId);
adTaskCacheManager.initRealtimeTaskCache(detectorId, detector.getDetectorIntervalInMilliseconds());
listener.onResponse(true);
}
}, transportService, false, listener);
} catch (Exception e) {
logger.error("Failed to init realtime task cache for " + detectorId, e);
listener.onFailure(e);
}
}
use of org.opensearch.ad.model.ADTaskType.REALTIME_TASK_TYPES in project anomaly-detection by opensearch-project.
the class ADTaskManager method stopLatestRealtimeTask.
/**
* Update latest realtime task.
*
* @param detectorId detector id
* @param state task state
* @param error error
* @param transportService transport service
* @param listener action listener
*/
public void stopLatestRealtimeTask(String detectorId, ADTaskState state, Exception error, TransportService transportService, ActionListener<AnomalyDetectorJobResponse> listener) {
getAndExecuteOnLatestDetectorLevelTask(detectorId, REALTIME_TASK_TYPES, (adTask) -> {
if (adTask.isPresent() && !adTask.get().isDone()) {
Map<String, Object> updatedFields = new HashMap<>();
updatedFields.put(ADTask.STATE_FIELD, state.name());
if (error != null) {
updatedFields.put(ADTask.ERROR_FIELD, error.getMessage());
}
AnomalyDetectorFunction function = () -> updateADTask(adTask.get().getTaskId(), updatedFields, ActionListener.wrap(r -> {
if (error == null) {
listener.onResponse(new AnomalyDetectorJobResponse(detectorId, 0, 0, 0, RestStatus.OK));
} else {
listener.onFailure(error);
}
}, e -> {
listener.onFailure(e);
}));
String coordinatingNode = adTask.get().getCoordinatingNode();
if (coordinatingNode != null && transportService != null) {
cleanDetectorCache(adTask.get(), transportService, function, listener);
} else {
function.execute();
}
} else {
listener.onFailure(new OpenSearchStatusException("Anomaly detector job is already stopped: " + detectorId, RestStatus.OK));
}
}, null, false, listener);
}
Aggregations