Search in sources :

Example 1 with TaskDTO

use of com.linkedin.thirdeye.datalayer.dto.TaskDTO in project pinot by linkedin.

the class MonitorJobRunner method createTasks.

public List<Long> createTasks() {
    List<Long> taskIds = new ArrayList<>();
    try {
        LOG.info("Creating monitor tasks");
        List<MonitorTaskInfo> monitorTasks = taskGenerator.createMonitorTasks(monitorJobContext);
        LOG.info("Monitor tasks {}", monitorTasks);
        for (MonitorTaskInfo taskInfo : monitorTasks) {
            String taskInfoJson = null;
            try {
                taskInfoJson = OBJECT_MAPPER.writeValueAsString(taskInfo);
            } catch (JsonProcessingException e) {
                LOG.error("Exception when converting MonitorTaskInfo {} to jsonString", taskInfo, e);
            }
            TaskDTO taskSpec = new TaskDTO();
            taskSpec.setTaskType(TaskType.MONITOR);
            taskSpec.setJobName(monitorJobContext.getJobName());
            taskSpec.setStatus(TaskStatus.WAITING);
            taskSpec.setStartTime(System.currentTimeMillis());
            taskSpec.setTaskInfo(taskInfoJson);
            taskSpec.setJobId(monitorJobContext.getJobExecutionId());
            long taskId = taskDAO.save(taskSpec);
            taskIds.add(taskId);
            LOG.info("Created monitorTask {} with taskId {}", taskSpec, taskId);
        }
    } catch (Exception e) {
        LOG.error("Exception in creating anomaly tasks", e);
    }
    return taskIds;
}
Also used : ArrayList(java.util.ArrayList) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) TaskDTO(com.linkedin.thirdeye.datalayer.dto.TaskDTO) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 2 with TaskDTO

use of com.linkedin.thirdeye.datalayer.dto.TaskDTO in project pinot by linkedin.

the class AlertJobRunner method createTasks.

private List<Long> createTasks(DateTime monitoringWindowStartTime, DateTime monitoringWindowEndTime) {
    List<Long> taskIds = new ArrayList<>();
    try {
        List<AlertTaskInfo> tasks = taskGenerator.createAlertTasks(alertJobContext, monitoringWindowStartTime, monitoringWindowEndTime);
        for (AlertTaskInfo taskInfo : tasks) {
            String taskInfoJson = null;
            try {
                taskInfoJson = OBJECT_MAPPER.writeValueAsString(taskInfo);
            } catch (JsonProcessingException e) {
                LOG.error("Exception when converting AlertTaskInfo {} to jsonString", taskInfo, e);
            }
            TaskDTO taskSpec = new TaskDTO();
            taskSpec.setTaskType(TaskType.ALERT);
            taskSpec.setJobName(alertJobContext.getJobName());
            taskSpec.setStatus(TaskStatus.WAITING);
            taskSpec.setStartTime(System.currentTimeMillis());
            taskSpec.setTaskInfo(taskInfoJson);
            taskSpec.setJobId(alertJobContext.getJobExecutionId());
            long taskId = taskDAO.save(taskSpec);
            taskIds.add(taskId);
            LOG.info("Created alert task {} with taskId {}", taskSpec, taskId);
        }
    } catch (Exception e) {
        LOG.error("Exception in creating alert tasks", e);
    }
    return taskIds;
}
Also used : ArrayList(java.util.ArrayList) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) TaskDTO(com.linkedin.thirdeye.datalayer.dto.TaskDTO) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) JobExecutionException(org.quartz.JobExecutionException)

Example 3 with TaskDTO

use of com.linkedin.thirdeye.datalayer.dto.TaskDTO in project pinot by linkedin.

the class DetectionJobScheduler method cleanUpJob.

/**
   * Sets unfinished (i.e., RUNNING, WAITING) tasks and job's status to FAILED
   * @param job
   */
private void cleanUpJob(JobDTO job) {
    if (!job.getStatus().equals(JobConstants.JobStatus.COMPLETED)) {
        List<TaskDTO> tasks = DAO_REGISTRY.getTaskDAO().findByJobIdStatusNotIn(job.getId(), TaskConstants.TaskStatus.COMPLETED);
        if (CollectionUtils.isNotEmpty(tasks)) {
            for (TaskDTO task : tasks) {
                task.setStatus(TaskConstants.TaskStatus.FAILED);
                DAO_REGISTRY.getTaskDAO().save(task);
            }
            job.setStatus(JobConstants.JobStatus.FAILED);
        } else {
            // This case happens when scheduler dies before it knows that all its tasks are actually finished
            job.setStatus(JobConstants.JobStatus.COMPLETED);
        }
        DAO_REGISTRY.getJobDAO().save(job);
    }
}
Also used : TaskDTO(com.linkedin.thirdeye.datalayer.dto.TaskDTO)

Example 4 with TaskDTO

use of com.linkedin.thirdeye.datalayer.dto.TaskDTO in project pinot by linkedin.

the class DetectionJobScheduler method synchronousBackFill.

/**
   * Different from asynchronous backfill in runBackfill, it will return after the backfill is done.
   * This function monitors the backfill task status, and return once the tasks are completed.
   * @param functionId
   * the function id to be backfilled
   * @param backfillStartTime
   * the monitor start time for backfill
   * @param backfillEndTime
   * the monitor end time for backfill
   * @param force
   * set to false to resume from previous backfill if there exists any
   */
public void synchronousBackFill(long functionId, DateTime backfillStartTime, DateTime backfillEndTime, boolean force) {
    Long jobExecutionId = runBackfill(functionId, backfillStartTime, backfillEndTime, force);
    if (jobExecutionId == null) {
        LOG.warn("Unable to perform backfill on function Id {} between {} and {}", functionId, backfillStartTime, backfillEndTime);
        return;
    }
    TaskManager taskDAO = DAO_REGISTRY.getTaskDAO();
    JobManager jobDAO = DAO_REGISTRY.getJobDAO();
    JobDTO jobDTO = null;
    List<TaskDTO> scheduledTaskDTO = taskDAO.findByJobIdStatusNotIn(jobExecutionId, TaskConstants.TaskStatus.COMPLETED);
    int retryCounter = 0;
    while (scheduledTaskDTO.size() > 0) {
        boolean hasFailedTask = false;
        for (TaskDTO taskDTO : scheduledTaskDTO) {
            if (taskDTO.getStatus() == TaskConstants.TaskStatus.FAILED) {
                if (retryCounter >= MAX_BACKFILL_RETRY) {
                    LOG.warn("The backfill of anomaly function {} (task id: {}) failed. Stop retry.", functionId, jobExecutionId);
                    // Set job to be failed
                    jobDTO = jobDAO.findById(jobExecutionId);
                    if (jobDTO.getStatus() != JobConstants.JobStatus.FAILED) {
                        jobDTO.setStatus(JobConstants.JobStatus.FAILED);
                        jobDAO.save(jobDTO);
                    }
                    return;
                }
                hasFailedTask = true;
                LOG.warn("The backfill of anomaly function {} (task id: {}) failed. Retry count: {}", functionId, jobExecutionId, retryCounter);
            }
        }
        if (hasFailedTask) {
            retryCounter++;
            jobExecutionId = runBackfill(functionId, backfillStartTime, backfillEndTime, force);
        }
        try {
            TimeUnit.SECONDS.sleep(SYNC_SLEEP_SECONDS);
        } catch (InterruptedException e) {
            LOG.warn("The monitoring thread for anomaly function {} (task id: {}) backfill is awakened.", functionId, jobExecutionId);
        }
        scheduledTaskDTO = taskDAO.findByJobIdStatusNotIn(jobExecutionId, TaskConstants.TaskStatus.COMPLETED);
    }
    // Set job to be completed
    jobDTO = jobDAO.findById(jobExecutionId);
    if (jobDTO.getStatus() != JobConstants.JobStatus.COMPLETED) {
        jobDTO.setStatus(JobConstants.JobStatus.COMPLETED);
        jobDAO.save(jobDTO);
    }
}
Also used : JobDTO(com.linkedin.thirdeye.datalayer.dto.JobDTO) TaskManager(com.linkedin.thirdeye.datalayer.bao.TaskManager) JobManager(com.linkedin.thirdeye.datalayer.bao.JobManager) TaskDTO(com.linkedin.thirdeye.datalayer.dto.TaskDTO)

Example 5 with TaskDTO

use of com.linkedin.thirdeye.datalayer.dto.TaskDTO in project pinot by linkedin.

the class TaskDriver method acquireTask.

private TaskDTO acquireTask() {
    LOG.info(Thread.currentThread().getId() + " : Starting selectAndUpdate {}", Thread.currentThread().getId());
    TaskDTO acquiredTask = null;
    LOG.info(Thread.currentThread().getId() + " : Trying to find a task to execute");
    do {
        List<TaskDTO> anomalyTasks = new ArrayList<>();
        try {
            boolean orderAscending = System.currentTimeMillis() % 2 == 0;
            anomalyTasks = anomalyTaskDAO.findByStatusOrderByCreateTime(TaskStatus.WAITING, TASK_FETCH_SIZE, orderAscending);
        } catch (Exception e) {
            LOG.error("Exception found in fetching new tasks, sleeping for few seconds", e);
            try {
                // TODO : Add better wait / clear call
                Thread.sleep(TASK_FAILURE_DELAY_MILLIS);
            } catch (Exception e1) {
                LOG.error(e1.getMessage(), e1);
            }
        }
        if (anomalyTasks.size() > 0) {
            LOG.info(Thread.currentThread().getId() + " : Found {} tasks in waiting state", anomalyTasks.size());
        } else {
            // sleep for few seconds if not tasks found - avoid cpu thrashing
            // also add some extra random number of milli seconds to allow threads to start at different times
            // TODO : Add better wait / clear call
            int delay = NO_TASK_IDLE_DELAY_MILLIS + RANDOM.nextInt(NO_TASK_IDLE_DELAY_MILLIS);
            LOG.debug("No tasks found to execute, sleeping for {} MS", delay);
            try {
                Thread.sleep(delay);
            } catch (Exception e1) {
                LOG.error(e1.getMessage(), e1);
            }
        }
        for (TaskDTO anomalyTaskSpec : anomalyTasks) {
            LOG.info(Thread.currentThread().getId() + " : Trying to acquire task : {}", anomalyTaskSpec.getId());
            boolean success = false;
            try {
                success = anomalyTaskDAO.updateStatusAndWorkerId(workerId, anomalyTaskSpec.getId(), allowedOldTaskStatus, TaskStatus.RUNNING, anomalyTaskSpec.getVersion());
                LOG.info("Thread - [{}] : trying to acquire task id [{}], success status: [{}] with version [{}]", Thread.currentThread().getId(), anomalyTaskSpec.getId(), success, anomalyTaskSpec.getVersion());
            } catch (Exception e) {
                LOG.warn("exception : [{}] in acquiring task by threadId {} and workerId {}", e.getClass().getSimpleName(), Thread.currentThread().getId(), workerId);
            }
            if (success) {
                acquiredTask = anomalyTaskSpec;
                break;
            }
        }
    } while (acquiredTask == null);
    LOG.info(Thread.currentThread().getId() + " : Acquired task ======" + acquiredTask);
    return acquiredTask;
}
Also used : ArrayList(java.util.ArrayList) TaskDTO(com.linkedin.thirdeye.datalayer.dto.TaskDTO)

Aggregations

TaskDTO (com.linkedin.thirdeye.datalayer.dto.TaskDTO)22 ArrayList (java.util.ArrayList)10 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)5 Test (org.testng.annotations.Test)4 TaskStatus (com.linkedin.thirdeye.anomaly.task.TaskConstants.TaskStatus)3 TaskBean (com.linkedin.thirdeye.datalayer.pojo.TaskBean)3 Predicate (com.linkedin.thirdeye.datalayer.util.Predicate)3 HashMap (java.util.HashMap)3 JobDTO (com.linkedin.thirdeye.datalayer.dto.JobDTO)2 JobExecutionException (org.quartz.JobExecutionException)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 BiMap (com.google.common.collect.BiMap)1 Transactional (com.google.inject.persist.Transactional)1 AlertTaskInfo (com.linkedin.thirdeye.anomaly.alert.AlertTaskInfo)1 TaskType (com.linkedin.thirdeye.anomaly.task.TaskConstants.TaskType)1 DataCompletenessTaskInfo (com.linkedin.thirdeye.completeness.checker.DataCompletenessTaskInfo)1 JobManager (com.linkedin.thirdeye.datalayer.bao.JobManager)1 TaskManager (com.linkedin.thirdeye.datalayer.bao.TaskManager)1 MergedAnomalyResultDTO (com.linkedin.thirdeye.datalayer.dto.MergedAnomalyResultDTO)1 RawAnomalyResultDTO (com.linkedin.thirdeye.datalayer.dto.RawAnomalyResultDTO)1