use of org.opensearch.ad.model.ADTask.COORDINATING_NODE_FIELD in project anomaly-detection by opensearch-project.
the class ADTaskManager method updateLatestRealtimeTaskOnCoordinatingNode.
/**
* Update realtime task cache on realtime detector's coordinating node.
*
* @param detectorId detector id
* @param state new state
* @param rcfTotalUpdates rcf total updates
* @param detectorIntervalInMinutes detector interval in minutes
* @param error error
* @param listener action listener
*/
public void updateLatestRealtimeTaskOnCoordinatingNode(String detectorId, String state, Long rcfTotalUpdates, Long detectorIntervalInMinutes, String error, ActionListener<UpdateResponse> listener) {
Float initProgress = null;
String newState = null;
// calculate init progress and task state with RCF total updates
if (detectorIntervalInMinutes != null && rcfTotalUpdates != null) {
newState = ADTaskState.INIT.name();
if (rcfTotalUpdates < NUM_MIN_SAMPLES) {
initProgress = (float) rcfTotalUpdates / NUM_MIN_SAMPLES;
} else {
newState = ADTaskState.RUNNING.name();
initProgress = 1.0f;
}
}
// Check if new state is not null and override state calculated with rcf total updates
if (state != null) {
newState = state;
}
error = Optional.ofNullable(error).orElse("");
if (!adTaskCacheManager.isRealtimeTaskChanged(detectorId, newState, initProgress, error)) {
// If task not changed, no need to update, just return
listener.onResponse(null);
return;
}
Map<String, Object> updatedFields = new HashMap<>();
updatedFields.put(COORDINATING_NODE_FIELD, clusterService.localNode().getId());
if (initProgress != null) {
updatedFields.put(INIT_PROGRESS_FIELD, initProgress);
updatedFields.put(ESTIMATED_MINUTES_LEFT_FIELD, Math.max(0, NUM_MIN_SAMPLES - rcfTotalUpdates) * detectorIntervalInMinutes);
}
if (newState != null) {
updatedFields.put(STATE_FIELD, newState);
}
if (error != null) {
updatedFields.put(ERROR_FIELD, error);
}
Float finalInitProgress = initProgress;
// Variable used in lambda expression should be final or effectively final
String finalError = error;
String finalNewState = newState;
updateLatestADTask(detectorId, ADTaskType.REALTIME_TASK_TYPES, updatedFields, ActionListener.wrap(r -> {
logger.debug("Updated latest realtime AD task successfully for detector {}", detectorId);
adTaskCacheManager.updateRealtimeTaskCache(detectorId, finalNewState, finalInitProgress, finalError);
listener.onResponse(r);
}, e -> {
logger.error("Failed to update realtime task for detector " + detectorId, e);
listener.onFailure(e);
}));
}
Aggregations