Search in sources :

Example 6 with FlowError

use of org.ow2.proactive.scheduler.common.job.factories.FlowError in project scheduling by ow2-proactive.

the class FlowChecker method createTaskTree.

/**
 * Created a double linked dependency tree of a job
 *
 * @param job a job
 * @return a double linked tree representation of the parameter, as the list of roots
 * @throws FlowError
 */
private void createTaskTree(TaskFlowJob job) throws FlowError {
    // list of roots
    List<TaskTree> roots = new ArrayList<>();
    // all tree nodes
    Map<String, TaskTree> tasks = new HashMap<>();
    for (Task t : job.getTasks()) {
        TaskTree tt = new TaskTree(t);
        tasks.put(t.getName(), tt);
    }
    for (TaskTree treeDown : tasks.values()) {
        List<Task> deps = treeDown.element.getDependencesList();
        if (deps == null) {
            roots.add(treeDown);
        } else {
            for (Task dep : deps) {
                TaskTree treeUp = tasks.get(dep.getName());
                treeUp.children.add(treeDown);
                treeDown.parents.add(treeUp);
            }
        }
        if (treeDown.element.getFlowScript() != null && treeDown.element.getFlowScript().getActionType().equals(FlowActionType.IF.toString())) {
            String tT = treeDown.element.getFlowScript().getActionTarget();
            String tE = treeDown.element.getFlowScript().getActionTargetElse();
            String tJ = treeDown.element.getFlowScript().getActionContinuation();
            if (tT != null) {
                TaskTree tt = tasks.get(tT);
                if (tt == null) {
                    throw new FlowError("IF target is null", FlowErrorType.IF, treeDown.element.getName());
                }
                if (tt.targetOf != null) {
                    throw new FlowError("Task is target of multiple IF actions", FlowErrorType.IF, tT);
                } else {
                    tt.targetOf = treeDown;
                    treeDown.targets.add(tt);
                }
            }
            if (tE != null) {
                TaskTree tt = tasks.get(tE);
                if (tt == null) {
                    throw new FlowError("ELSE target is null", FlowErrorType.IF, treeDown.element.getName());
                }
                if (tt.targetOf != null) {
                    throw new FlowError("Task is target of multiple IF actions", FlowErrorType.IF, tE);
                } else {
                    tt.targetOf = treeDown;
                    treeDown.targets.add(tt);
                }
            }
            if (tJ != null) {
                treeDown.targetJoin = tasks.get(tJ);
            }
        }
    }
    for (TaskTree tree : tasks.values()) {
        if (tree.element.getFlowScript() != null && tree.element.getFlowScript().getActionType().equals(FlowActionType.IF.toString())) {
            String tJ = tree.element.getFlowScript().getActionContinuation();
            if (tJ != null && tJ.length() > 0) {
                TaskTree ifT = tasks.get(tree.element.getFlowScript().getActionTarget());
                TaskTree elseT = tasks.get(tree.element.getFlowScript().getActionTargetElse());
                List<TaskTree> tgs = new ArrayList<>(2);
                tgs.add(ifT);
                tgs.add(elseT);
                for (TaskTree tree2 : tgs) {
                    TaskTree target = tree2, target2 = null;
                    Stack<String> jOpen = new Stack<>();
                    TaskTree joinTask = tasks.get(tJ);
                    do {
                        target2 = target;
                        if (target.element.getFlowScript() != null && target.element.getFlowScript().getActionContinuation() != null) {
                            String jT = target.element.getFlowScript().getActionContinuation();
                            if (jT != null && jT.length() > 0) {
                                jOpen.push(jT);
                            }
                        }
                        target = null;
                        if (target2.children.size() > 0) {
                            target = target2.children.get(0);
                        }
                        if (target == null && target2.element.getFlowScript() != null) {
                            target = tasks.get(target2.element.getFlowScript().getActionTargetElse());
                        }
                        if (target == null && jOpen.size() > 0) {
                            target = tasks.get(jOpen.pop());
                        }
                    } while (target != null);
                    if (joinTask != null) {
                        joinTask.joins.add(target2);
                        target2.joinedBy = joinTask;
                    }
                }
            }
        }
    }
    Collection<TaskTree> values = tasks.values();
    this.tasksFlat = new ArrayList<>(values.size());
    for (TaskTree t : values) {
        tasksFlat.add(t);
    }
    this.roots = roots;
}
Also used : Task(org.ow2.proactive.scheduler.common.task.Task) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Stack(java.util.Stack)

Example 7 with FlowError

use of org.ow2.proactive.scheduler.common.job.factories.FlowError in project scheduling by ow2-proactive.

the class ValidationUtil method validateJob.

private void validateJob(TaskFlowJob job, JobValidationData data) {
    ArrayList<Task> tasks = job.getTasks();
    if (tasks.isEmpty()) {
        data.setErrorMessage(String.format("%s must contain at least one task.", job.getName()));
        return;
    }
    FlowError error = FlowChecker.validate(job);
    if (error != null) {
        data.setTaskName(error.getTask());
        data.setErrorMessage(error.getMessage());
        return;
    }
    data.setValid(true);
}
Also used : FlowError(org.ow2.proactive.scheduler.common.job.factories.FlowError) Task(org.ow2.proactive.scheduler.common.task.Task)

Example 8 with FlowError

use of org.ow2.proactive.scheduler.common.job.factories.FlowError in project scheduling by ow2-proactive.

the class InternalJobFactory method createJob.

/**
 * Create an internalTaskFlow job with the given task flow job (user)
 *
 * @param userJob the user job that will be used to create the internal job.
 * @return the created internal job.
 * @throws JobCreationException an exception if the factory cannot create the given job.
 */
private static InternalJob createJob(TaskFlowJob userJob) throws JobCreationException {
    if (userJob.getTasks().size() == 0) {
        logger.info("Job '" + userJob.getName() + "' must contain tasks !");
        throw new JobCreationException("This job must contains tasks !");
    }
    // validate taskflow
    List<FlowChecker.Block> blocks = new ArrayList<>();
    FlowError err = FlowChecker.validate(userJob, blocks);
    if (err != null) {
        String e = "";
        e += "Invalid taskflow: " + err.getMessage() + "; context: " + err.getTask();
        logger.error(e);
        throw new JobCreationException(e, err);
    }
    InternalJob job = new InternalTaskFlowJob();
    // keep an initial job content
    job.setTaskFlowJob(userJob);
    Map<Task, InternalTask> tasksList = new LinkedHashMap<>();
    boolean hasPreciousResult = false;
    for (Task t : userJob.getTasks()) {
        tasksList.put(t, createTask(userJob, job, t));
        if (!hasPreciousResult) {
            hasPreciousResult = t.isPreciousResult();
        }
    }
    for (Entry<Task, InternalTask> entry : tasksList.entrySet()) {
        if (entry.getKey().getDependencesList() != null) {
            for (Task t : entry.getKey().getDependencesList()) {
                entry.getValue().addDependence(tasksList.get(t));
            }
        }
        job.addTask(entry.getValue());
    }
    // tag matching blocks in InternalTasks
    for (InternalTask it : tasksList.values()) {
        for (FlowChecker.Block block : blocks) {
            if (it.getName().equals(block.start.element.getName())) {
                it.setMatchingBlock(block.end.element.getName());
            }
            if (it.getName().equals(block.end.element.getName())) {
                it.setMatchingBlock(block.start.element.getName());
            }
        }
    }
    // create if/else/join weak dependencies
    for (InternalTask it : tasksList.values()) {
        // it performs an IF action
        if (it.getFlowScript() != null && it.getFlowScript().getActionType().equals(FlowActionType.IF.toString())) {
            String ifBranch = it.getFlowScript().getActionTarget();
            String elseBranch = it.getFlowScript().getActionTargetElse();
            String join = it.getFlowScript().getActionContinuation();
            List<InternalTask> joinedBranches = new ArrayList<>();
            // find the ifBranch task
            for (InternalTask it2 : tasksList.values()) {
                if (it2.getName().equals(ifBranch)) {
                    it2.setIfBranch(it);
                    String match = it2.getMatchingBlock();
                    // find its matching block task
                    if (match == null) {
                        // no match: single task
                        joinedBranches.add(it2);
                    } else {
                        for (InternalTask it3 : tasksList.values()) {
                            if (it3.getName().equals(match)) {
                                joinedBranches.add(it3);
                                break;
                            }
                        }
                    }
                    break;
                }
            }
            // find the elseBranch task
            for (InternalTask it2 : tasksList.values()) {
                if (it2.getName().equals(elseBranch)) {
                    it2.setIfBranch(it);
                    String match = it2.getMatchingBlock();
                    // find its matching block task
                    if (match == null) {
                        // no match: single task
                        joinedBranches.add(it2);
                    } else {
                        for (InternalTask it3 : tasksList.values()) {
                            if (it3.getName().equals(match)) {
                                joinedBranches.add(it3);
                                break;
                            }
                        }
                    }
                    break;
                }
            }
            // find the joinBranch task
            for (InternalTask it2 : tasksList.values()) {
                if (it2.getName().equals(join)) {
                    it2.setJoinedBranches(joinedBranches);
                }
            }
        }
    }
    return job;
}
Also used : FlowError(org.ow2.proactive.scheduler.common.job.factories.FlowError) Task(org.ow2.proactive.scheduler.common.task.Task) JavaTask(org.ow2.proactive.scheduler.common.task.JavaTask) InternalScriptTask(org.ow2.proactive.scheduler.task.internal.InternalScriptTask) InternalForkedScriptTask(org.ow2.proactive.scheduler.task.internal.InternalForkedScriptTask) InternalTask(org.ow2.proactive.scheduler.task.internal.InternalTask) NativeTask(org.ow2.proactive.scheduler.common.task.NativeTask) ScriptTask(org.ow2.proactive.scheduler.common.task.ScriptTask) InternalTask(org.ow2.proactive.scheduler.task.internal.InternalTask) ArrayList(java.util.ArrayList) JobCreationException(org.ow2.proactive.scheduler.common.exception.JobCreationException) LinkedHashMap(java.util.LinkedHashMap) FlowChecker(org.ow2.proactive.scheduler.common.job.factories.FlowChecker)

Aggregations

ArrayList (java.util.ArrayList)4 Task (org.ow2.proactive.scheduler.common.task.Task)4 FlowBlock (org.ow2.proactive.scheduler.common.task.flow.FlowBlock)4 Stack (java.util.Stack)3 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 FlowError (org.ow2.proactive.scheduler.common.job.factories.FlowError)2 EmptyStackException (java.util.EmptyStackException)1 LinkedHashMap (java.util.LinkedHashMap)1 JobCreationException (org.ow2.proactive.scheduler.common.exception.JobCreationException)1 FlowChecker (org.ow2.proactive.scheduler.common.job.factories.FlowChecker)1 JavaTask (org.ow2.proactive.scheduler.common.task.JavaTask)1 NativeTask (org.ow2.proactive.scheduler.common.task.NativeTask)1 ScriptTask (org.ow2.proactive.scheduler.common.task.ScriptTask)1 InternalForkedScriptTask (org.ow2.proactive.scheduler.task.internal.InternalForkedScriptTask)1 InternalScriptTask (org.ow2.proactive.scheduler.task.internal.InternalScriptTask)1 InternalTask (org.ow2.proactive.scheduler.task.internal.InternalTask)1