Search in sources :

Example 1 with ChangedTasksInfo

use of org.ow2.proactive.scheduler.job.ChangedTasksInfo in project scheduling by ow2-proactive.

the class InternalJob method finishInErrorTask.

public ChangedTasksInfo finishInErrorTask(TaskId taskId, TaskResultImpl result, SchedulerStateUpdate frontend) {
    FlowAction action = result.getAction();
    ChangedTasksInfo changedTasksInfo = terminateTask(false, taskId, frontend, action, result, true);
    setUnPause();
    return changedTasksInfo;
}
Also used : FlowAction(org.ow2.proactive.scheduler.common.task.flow.FlowAction)

Example 2 with ChangedTasksInfo

use of org.ow2.proactive.scheduler.job.ChangedTasksInfo in project scheduling by ow2-proactive.

the class InternalJob method replicateForNextLoopIteration.

public boolean replicateForNextLoopIteration(InternalTask initiator, InternalTask target, ChangedTasksInfo changesInfo, SchedulerStateUpdate frontend, FlowAction action) {
    LOGGER.info("LOOP (init:" + initiator.getId() + "; target:" + target.getId() + ")");
    // accumulates the tasks between the initiator and the target
    Map<TaskId, InternalTask> dup = new HashMap<>();
    // replicate the tasks between the initiator and the target
    try {
        initiator.replicateTree(dup, target.getId(), true, initiator.getReplicationIndex(), initiator.getIterationIndex());
    } catch (ExecutableCreationException e) {
        LOGGER.error("", e);
        return false;
    }
    ((JobInfoImpl) this.getJobInfo()).setNumberOfPendingTasks(this.getJobInfo().getNumberOfPendingTasks() + dup.size());
    // time-consuming but safe
    for (InternalTask nt : dup.values()) {
        boolean ok;
        do {
            ok = true;
            for (InternalTask task : tasks.values()) {
                if (nt.getName().equals(task.getName())) {
                    nt.setIterationIndex(nt.getIterationIndex() + 1);
                    ok = false;
                }
            }
        } while (!ok);
    }
    // configure the new tasks
    InternalTask newTarget = null;
    InternalTask newInit = null;
    for (Entry<TaskId, InternalTask> it : dup.entrySet()) {
        InternalTask nt = it.getValue();
        if (target.getId().equals(it.getKey())) {
            newTarget = nt;
        }
        if (initiator.getId().equals(it.getKey())) {
            newInit = nt;
        }
        nt.setJobInfo(getJobInfo());
        this.addTask(nt);
        assignReplicationTag(nt, initiator, true, action);
    }
    changesInfo.newTasksAdded(dup.values());
    // connect replicated tree
    newTarget.addDependence(initiator);
    changesInfo.taskUpdated(newTarget);
    // connect mergers
    List<InternalTask> mergers = new ArrayList<>();
    for (InternalTask t : this.tasks.values()) {
        if (t.getIDependences() != null) {
            for (InternalTask p : t.getIDependences()) {
                if (p.getId().equals(initiator.getId())) {
                    if (!t.equals(newTarget)) {
                        mergers.add(t);
                    }
                }
            }
        }
    }
    for (InternalTask t : mergers) {
        t.getIDependences().remove(initiator);
        t.addDependence(newInit);
        changesInfo.taskUpdated(t);
    }
    // propagate the changes in the job descriptor
    getJobDescriptor().doLoop(initiator.getId(), dup, newTarget, newInit);
    this.jobInfo.setTasksChanges(changesInfo, this);
    // notify frontend that tasks were added and modified
    frontend.jobStateUpdated(this.getOwner(), new NotificationData<JobInfo>(SchedulerEvent.TASK_REPLICATED, new JobInfoImpl(jobInfo)));
    frontend.jobUpdatedFullData(this);
    this.jobInfo.clearTasksChanges();
    return true;
}
Also used : ExecutableCreationException(org.ow2.proactive.scheduler.common.exception.ExecutableCreationException) TaskId(org.ow2.proactive.scheduler.common.task.TaskId) InternalTask(org.ow2.proactive.scheduler.task.internal.InternalTask) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) JobInfo(org.ow2.proactive.scheduler.common.job.JobInfo) ArrayList(java.util.ArrayList)

Example 3 with ChangedTasksInfo

use of org.ow2.proactive.scheduler.job.ChangedTasksInfo in project scheduling by ow2-proactive.

the class InternalJob method terminateTask.

/**
 * Terminate a task, change status, managing dependencies
 * <p>
 * Also, apply a Control Flow Action if provided. This may alter the number
 * of tasks in the job, events have to be sent accordingly.
 *
 * @param errorOccurred has an error occurred for this termination
 * @param taskId        the task to terminate.
 * @param frontend      Used to notify all listeners of the replication of tasks,
 *                      triggered by the FlowAction
 * @param action        a Control Flow Action that will potentially create new tasks
 *                      inside the job
 * @return the taskDescriptor that has just been terminated.
 */
public ChangedTasksInfo terminateTask(boolean errorOccurred, TaskId taskId, SchedulerStateUpdate frontend, FlowAction action, TaskResultImpl result, boolean taskIsPaused) {
    final InternalTask descriptor = tasks.get(taskId);
    if (!errorOccurred) {
        decreaseNumberOfFaultyTasks(taskId);
    }
    descriptor.setFinishedTime(System.currentTimeMillis());
    descriptor.setStatus(errorOccurred ? TaskStatus.FAULTY : TaskStatus.FINISHED);
    descriptor.setExecutionDuration(result.getTaskDuration());
    if (taskIsPaused) {
        setNumberOfInErrorTasks(getNumberOfInErrorTasks() - 1);
    } else {
        setNumberOfRunningTasks(getNumberOfRunningTasks() - 1);
    }
    setNumberOfFinishedTasks(getNumberOfFinishedTasks() + 1);
    if ((getStatus() == JobStatus.RUNNING) && (getNumberOfRunningTasks() == 0) && !taskIsPaused) {
        setStatus(JobStatus.STALLED);
    }
    ChangedTasksInfo changesInfo = new ChangedTasksInfo();
    changesInfo.taskUpdated(descriptor);
    boolean didAction = false;
    if (action != null) {
        InternalTask initiator = tasks.get(taskId);
        switch(action.getType()) {
            /*
                 * LOOP action
                 */
            case LOOP:
                {
                    didAction = terminateLoopHandler.terminateLoopTask(action, initiator, changesInfo, frontend);
                    break;
                }
            /*
                 * IF action
                 */
            case IF:
                {
                    didAction = terminateIfTaskHandler.terminateIfTask(action, initiator, changesInfo, frontend, descriptor, taskId);
                    break;
                }
            /*
                 * REPLICATE action
                 */
            case REPLICATE:
                {
                    didAction = terminateReplicateTaskHandler.terminateReplicateTask(action, initiator, changesInfo, frontend, taskId);
                    break;
                }
            /*
                 * CONTINUE action : - continue taskflow as if no action was provided
                 */
            case CONTINUE:
                LOGGER.debug("Task flow Action CONTINUE on task " + initiator.getId().getReadableName());
                break;
            default:
                LOGGER.warn("There are inconsistency between InternalJob and FlowActionType classes.");
                break;
        }
    /**
     * System.out.println("******** task dump ** " +
     * this.getJobInfo().getJobId() + " " + initiator.getName() +
     * " does " + action.getType() + " " + ((action.getTarget() == null)
     * ? "." : action.getTarget()) + " " + ((action.getTargetElse() ==
     * null) ? "." : action.getTargetElse()) + " " +
     * ((action.getTargetJoin() == null) ? "." :
     * action.getTargetJoin())); for (InternalTask it :
     * this.tasks.values()) { System.out.print(it.getName() + " "); if
     * (it.getIDependences() != null) { System.out.print("deps "); for
     * (InternalTask parent : it.getIDependences()) {
     * System.out.print(parent.getName() + " "); } } if
     * (it.getIfBranch() != null) { System.out.print("if " +
     * it.getIfBranch().getName() + " "); } if (it.getJoinedBranches()
     * != null && it.getJoinedBranches().size() == 2) {
     * System.out.print("join " +
     * it.getJoinedBranches().get(0).getName() + " " +
     * it.getJoinedBranches().get(1).getName()); } System.out.println();
     * } System.out.println("******** task dump ** " +
     * this.getJobInfo().getJobId()); System.out.println();
     */
    }
    // terminate this task
    if (!didAction) {
        getJobDescriptor().terminate(taskId, taskIsPaused);
    }
    return changesInfo;
}
Also used : InternalTask(org.ow2.proactive.scheduler.task.internal.InternalTask)

Example 4 with ChangedTasksInfo

use of org.ow2.proactive.scheduler.job.ChangedTasksInfo in project scheduling by ow2-proactive.

the class TerminateLoopHandler method terminateLoopTask.

public boolean terminateLoopTask(FlowAction action, InternalTask initiator, ChangedTasksInfo changesInfo, SchedulerStateUpdate frontend) {
    // find the target of the loop
    InternalTask target = null;
    if (action.getTarget().equals(initiator.getName())) {
        target = initiator;
    } else {
        target = internalJob.findTaskUp(action.getTarget(), initiator);
    }
    boolean replicateForNextLoopIteration = internalJob.replicateForNextLoopIteration(initiator, target, changesInfo, frontend, action);
    if (replicateForNextLoopIteration && action.getCronExpr() != null) {
        for (TaskId tid : changesInfo.getNewTasks()) {
            InternalTask newTask = internalJob.getIHMTasks().get(tid);
            try {
                Date startAt = (new Predictor(action.getCronExpr())).nextMatchingDate();
                newTask.addGenericInformation(InternalJob.GENERIC_INFO_START_AT_KEY, ISO8601DateUtil.parse(startAt));
                newTask.setScheduledTime(startAt.getTime());
            } catch (InvalidPatternException e) {
                // this will not happen as the cron expression is
                // already being validated in FlowScript class.
                LOGGER.debug(e.getMessage());
            }
        }
    }
    return replicateForNextLoopIteration;
}
Also used : TaskId(org.ow2.proactive.scheduler.common.task.TaskId) InvalidPatternException(it.sauronsoftware.cron4j.InvalidPatternException) InternalTask(org.ow2.proactive.scheduler.task.internal.InternalTask) Predictor(it.sauronsoftware.cron4j.Predictor) Date(java.util.Date)

Example 5 with ChangedTasksInfo

use of org.ow2.proactive.scheduler.job.ChangedTasksInfo in project scheduling by ow2-proactive.

the class TerminateReplicateTaskHandler method terminateReplicateTask.

public boolean terminateReplicateTask(FlowAction action, InternalTask initiator, ChangedTasksInfo changesInfo, SchedulerStateUpdate frontend, TaskId taskId) {
    int runs = action.getDupNumber();
    logger.info("Control Flow Action REPLICATE (runs:" + runs + ")");
    List<InternalTask> toReplicate = new ArrayList<>();
    // find the tasks that need to be replicated
    for (InternalTask internalTask : internalJob.getIHMTasks().values()) {
        List<InternalTask> internalTaskDependencies = internalTask.getIDependences() == null ? new ArrayList<InternalTask>() : internalTask.getIDependences();
        for (InternalTask internalTaskDependency : internalTaskDependencies) {
            if (isTheInitiatorTask(initiator, toReplicate, internalTask, internalTaskDependency)) {
                if (runs < 1) {
                    skipReplication(initiator, changesInfo, internalTask);
                    break;
                } else {
                    toReplicate.add(internalTask);
                }
            }
        }
    }
    // for each initial task to replicate
    for (InternalTask internalTaskToReplicate : toReplicate) {
        // determine the target of the replication whether it is a block or
        // a single task
        InternalTask target = null;
        // target is a task block start : replication of the block
        if (internalTaskToReplicate.getFlowBlock().equals(FlowBlock.START)) {
            String tg = internalTaskToReplicate.getMatchingBlock();
            for (InternalTask internalTask : internalJob.getIHMTasks().values()) {
                if (tg.equals(internalTask.getName()) && !(internalTask.getStatus().equals(TaskStatus.FINISHED) || internalTask.getStatus().equals(TaskStatus.SKIPPED)) && internalTask.dependsOn(internalTaskToReplicate)) {
                    target = internalTask;
                    break;
                }
            }
            if (target == null) {
                logger.error("REPLICATE: could not find matching block '" + tg + "'");
                continue;
            }
        } else // target is not a block : replication of the task
        {
            target = internalTaskToReplicate;
        }
        // for each number of parallel run
        for (int i = 1; i < runs; i++) {
            // accumulates the tasks between the initiator and the target
            Map<TaskId, InternalTask> tasksBetweenInitiatorAndTarget = new HashMap<>();
            // replicate the tasks between the initiator and the target
            try {
                target.replicateTree(tasksBetweenInitiatorAndTarget, internalTaskToReplicate.getId(), false, initiator.getReplicationIndex() * runs, 0);
            } catch (Exception e) {
                logger.error("REPLICATE: could not replicate tree", e);
                break;
            }
            ((JobInfoImpl) internalJob.getJobInfo()).setNumberOfPendingTasks(((JobInfoImpl) internalJob.getJobInfo()).getNumberOfPendingTasks() + tasksBetweenInitiatorAndTarget.size());
            // pointers to the new replicated tasks corresponding the begin
            // and
            // the end of the block ; can be the same
            InternalTask newTarget = null;
            InternalTask newEnd = null;
            // configure the new tasks
            for (InternalTask internalTask : tasksBetweenInitiatorAndTarget.values()) {
                internalTask.setJobInfo(((JobInfoImpl) internalJob.getJobInfo()));
                int dupIndex = getNextReplicationIndex(InternalTask.getInitialName(internalTask.getName()), internalTask.getIterationIndex());
                internalJob.addTask(internalTask);
                internalTask.setReplicationIndex(dupIndex);
                assignReplicationTag(internalTask, initiator, false, action);
            }
            changesInfo.newTasksAdded(tasksBetweenInitiatorAndTarget.values());
            // find the beginning and the ending of the replicated block
            for (Entry<TaskId, InternalTask> tasksBetweenInitiatorAndTargetEntry : tasksBetweenInitiatorAndTarget.entrySet()) {
                InternalTask internalBlockTask = tasksBetweenInitiatorAndTargetEntry.getValue();
                // initiator
                if (internalTaskToReplicate.getId().equals(tasksBetweenInitiatorAndTargetEntry.getKey())) {
                    newTarget = internalBlockTask;
                    newTarget.addDependence(initiator);
                // no need to add newTarget to modifiedTasks
                // because newTarget is among dup.values(), and we
                // have added them all
                }
                // connect the last task of the block with the merge task(s)
                if (target.getId().equals(tasksBetweenInitiatorAndTargetEntry.getKey())) {
                    newEnd = internalBlockTask;
                    List<InternalTask> toAdd = new ArrayList<>();
                    // find the merge tasks ; can be multiple
                    for (InternalTask internalTask : internalJob.getIHMTasks().values()) {
                        List<InternalTask> pdeps = internalTask.getIDependences();
                        if (pdeps != null) {
                            for (InternalTask parent : pdeps) {
                                if (parent.getId().equals(target.getId())) {
                                    toAdd.add(internalTask);
                                }
                            }
                        }
                    }
                    // connect the merge tasks
                    for (InternalTask internalTask : toAdd) {
                        internalTask.addDependence(newEnd);
                        changesInfo.taskUpdated(internalTask);
                    }
                }
            }
            // propagate the changes on the JobDescriptor
            internalJob.getJobDescriptor().doReplicate(taskId, tasksBetweenInitiatorAndTarget, newTarget, target.getId(), newEnd.getId());
        }
    }
    // notify frontend that tasks were added to the job
    ((JobInfoImpl) internalJob.getJobInfo()).setTasksChanges(changesInfo, internalJob);
    if (frontend != null) {
        frontend.jobStateUpdated(internalJob.getOwner(), new NotificationData<>(SchedulerEvent.TASK_REPLICATED, internalJob.getJobInfo()));
        frontend.jobStateUpdated(internalJob.getOwner(), new NotificationData<>(SchedulerEvent.TASK_SKIPPED, internalJob.getJobInfo()));
        frontend.jobUpdatedFullData(internalJob);
    }
    ((JobInfoImpl) internalJob.getJobInfo()).clearTasksChanges();
    // no jump is performed ; now that the tasks have been replicated and
    // configured, the flow can continue its normal operation
    internalJob.getJobDescriptor().terminate(taskId);
    return true;
}
Also used : TaskId(org.ow2.proactive.scheduler.common.task.TaskId) InternalTask(org.ow2.proactive.scheduler.task.internal.InternalTask) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) JobInfoImpl(org.ow2.proactive.scheduler.job.JobInfoImpl)

Aggregations

InternalTask (org.ow2.proactive.scheduler.task.internal.InternalTask)10 TaskId (org.ow2.proactive.scheduler.common.task.TaskId)7 ChangedTasksInfo (org.ow2.proactive.scheduler.job.ChangedTasksInfo)6 InternalJob (org.ow2.proactive.scheduler.job.InternalJob)6 TaskResultImpl (org.ow2.proactive.scheduler.task.TaskResultImpl)5 FlowAction (org.ow2.proactive.scheduler.common.task.flow.FlowAction)4 JobInfoImpl (org.ow2.proactive.scheduler.job.JobInfoImpl)4 ArrayList (java.util.ArrayList)3 Test (org.junit.Test)3 JobInfo (org.ow2.proactive.scheduler.common.job.JobInfo)3 SchedulerStateRecoverHelper (org.ow2.proactive.scheduler.core.db.SchedulerStateRecoverHelper)3 HashMap (java.util.HashMap)2 TaskFlowJob (org.ow2.proactive.scheduler.common.job.TaskFlowJob)2 TaskInfo (org.ow2.proactive.scheduler.common.task.TaskInfo)2 TaskInfoImpl (org.ow2.proactive.scheduler.task.TaskInfoImpl)2 InvalidPatternException (it.sauronsoftware.cron4j.InvalidPatternException)1 Predictor (it.sauronsoftware.cron4j.Predictor)1 Date (java.util.Date)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ExecutableCreationException (org.ow2.proactive.scheduler.common.exception.ExecutableCreationException)1