use of org.opensearch.ad.model.ADTask.INIT_PROGRESS_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);
}));
}
use of org.opensearch.ad.model.ADTask.INIT_PROGRESS_FIELD in project anomaly-detection by opensearch-project.
the class ADBatchTaskRunner method forwardOrExecuteADTask.
/**
* Forward AD task to work node.
* 1. For HC detector, return directly if no more pending entity. Otherwise check if
* there is AD task created for this entity. If yes, just forward the entity task
* to worker node; otherwise, create entity task first, then forward.
* 2. For single entity detector, set task as INIT state and forward task to worker
* node.
*
* @param adTask AD task
* @param transportService transport service
* @param listener action listener
*/
public void forwardOrExecuteADTask(ADTask adTask, TransportService transportService, ActionListener<ADBatchAnomalyResultResponse> listener) {
try {
checkIfADTaskCancelledAndCleanupCache(adTask);
String detectorId = adTask.getDetectorId();
AnomalyDetector detector = adTask.getDetector();
boolean isHCDetector = detector.isMultientityDetector();
if (isHCDetector) {
String entityString = adTaskCacheManager.pollEntity(detectorId);
logger.debug("Start to run entity: {} of detector {}", entityString, detectorId);
if (entityString == null) {
listener.onResponse(new ADBatchAnomalyResultResponse(clusterService.localNode().getId(), false));
return;
}
ActionListener<Object> wrappedListener = ActionListener.wrap(r -> logger.debug("Entity task created successfully"), e -> {
logger.error("Failed to start entity task for detector: {}, entity: {}", detectorId, entityString);
// If fail, move the entity into pending task queue
adTaskCacheManager.addPendingEntity(detectorId, entityString);
});
// This is to handle retry case. To retry entity, we need to get the old entity task created before.
Entity entity = adTaskManager.parseEntityFromString(entityString, adTask);
String parentTaskId = adTask.getTaskType().equals(ADTaskType.HISTORICAL_HC_ENTITY.name()) ? // For HISTORICAL_HC_ENTITY task, return its parent task id
adTask.getParentTaskId() : // For HISTORICAL_HC_DETECTOR task, its task id is parent task id
adTask.getTaskId();
adTaskManager.getAndExecuteOnLatestADTask(detectorId, parentTaskId, entity, ImmutableList.of(ADTaskType.HISTORICAL_HC_ENTITY), existingEntityTask -> {
if (existingEntityTask.isPresent()) {
// retry failed entity caused by limit exceed exception
// TODO: if task failed due to limit exceed exception in half way, resume from the break point or just clear
// the
// old AD tasks and rerun it? Currently we just support rerunning task failed due to limit exceed exception
// before starting.
ADTask adEntityTask = existingEntityTask.get();
logger.debug("Rerun entity task for task id: {}, error of last run: {}", adEntityTask.getTaskId(), adEntityTask.getError());
ActionListener<ADBatchAnomalyResultResponse> workerNodeResponseListener = workerNodeResponseListener(adEntityTask, transportService, listener);
forwardOrExecuteEntityTask(adEntityTask, transportService, workerNodeResponseListener);
} else {
logger.info("Create entity task for entity:{}", entityString);
Instant now = Instant.now();
ADTask adEntityTask = new ADTask.Builder().detectorId(adTask.getDetectorId()).detector(detector).isLatest(true).taskType(ADTaskType.HISTORICAL_HC_ENTITY.name()).executionStartTime(now).taskProgress(0.0f).initProgress(0.0f).state(ADTaskState.INIT.name()).initProgress(0.0f).lastUpdateTime(now).startedBy(adTask.getStartedBy()).coordinatingNode(clusterService.localNode().getId()).detectionDateRange(adTask.getDetectionDateRange()).user(adTask.getUser()).entity(entity).parentTaskId(parentTaskId).build();
adTaskManager.createADTaskDirectly(adEntityTask, r -> {
adEntityTask.setTaskId(r.getId());
ActionListener<ADBatchAnomalyResultResponse> workerNodeResponseListener = workerNodeResponseListener(adEntityTask, transportService, listener);
forwardOrExecuteEntityTask(adEntityTask, transportService, workerNodeResponseListener);
}, wrappedListener);
}
}, transportService, false, wrappedListener);
} else {
Map<String, Object> updatedFields = new HashMap<>();
updatedFields.put(STATE_FIELD, ADTaskState.INIT.name());
updatedFields.put(INIT_PROGRESS_FIELD, 0.0f);
ActionListener<ADBatchAnomalyResultResponse> workerNodeResponseListener = workerNodeResponseListener(adTask, transportService, listener);
adTaskManager.updateADTask(adTask.getTaskId(), updatedFields, ActionListener.wrap(r -> forwardOrExecuteEntityTask(adTask, transportService, workerNodeResponseListener), e -> {
workerNodeResponseListener.onFailure(e);
}));
}
} catch (Exception e) {
logger.error("Failed to forward or execute AD task " + adTask.getTaskId(), e);
listener.onFailure(e);
}
}
Aggregations