Search in sources :

Example 1 with JobType

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

the class StaxJobFactory method createAndFillJob.

/**
 * Create the real job and fill it with its property. Leave the method at
 * the first tag that define the real type of job.
 *
 * @param cursorJob          the streamReader with the cursor on the job element.
 * @param replacementVariables map of variables which has precedence over those that defined
 *                           in Job descriptor
 * @throws JobCreationException if an exception occurs during job creation.
 */
private Job createAndFillJob(XMLStreamReader cursorJob, Map<String, String> replacementVariables) throws JobCreationException {
    // create a job that will just temporary store the common properties of the job
    Job commonPropertiesHolder = new Job() {

        @Override
        public JobId getId() {
            throw new RuntimeException("Not Available!");
        }

        @Override
        public JobType getType() {
            throw new RuntimeException("Not Available!");
        }
    };
    // parse job attributes and fill the temporary one
    // all attributes in the job element are saved and will be handled after the job variables are parsed.
    // This is to allow variable replacements on these attributes
    Map<String, String> delayedJobAttributes = new HashMap<>();
    int attrLen = cursorJob.getAttributeCount();
    int i = 0;
    for (; i < attrLen; i++) {
        String attributeName = cursorJob.getAttributeLocalName(i);
        String attributeValue = cursorJob.getAttributeValue(i);
        delayedJobAttributes.put(attributeName, attributeValue);
    }
    // parse job elements and fill the temporary one
    Job job = commonPropertiesHolder;
    try {
        int eventType;
        boolean shouldContinue = true;
        while (shouldContinue && cursorJob.hasNext()) {
            eventType = cursorJob.next();
            switch(eventType) {
                case XMLEvent.START_ELEMENT:
                    String current = cursorJob.getLocalName();
                    if (XMLTags.VARIABLES.matches(current)) {
                        // create job variables using the replacement map provided at job submission
                        // the final value of the variable can either be overwritten by a value of the replacement map or
                        // use in a pattern such value
                        commonPropertiesHolder.getVariables().putAll(createJobVariables(cursorJob, replacementVariables));
                    } else if (XMLTags.COMMON_GENERIC_INFORMATION.matches(current)) {
                        commonPropertiesHolder.setGenericInformation(getGenericInformation(cursorJob, commonPropertiesHolder.getVariablesAsReplacementMap()));
                    } else if (XMLTags.JOB_CLASSPATHES.matches(current)) {
                        logger.warn("Element " + XMLTags.JOB_CLASSPATHES.getXMLName() + " is no longer supported. Please define a " + XMLTags.FORK_ENVIRONMENT.getXMLName() + " per task if needed.");
                    } else if (XMLTags.COMMON_DESCRIPTION.matches(current)) {
                        commonPropertiesHolder.setDescription(getDescription(cursorJob, commonPropertiesHolder.getVariablesAsReplacementMap()));
                    } else if (XMLTags.DS_INPUT_SPACE.matches(current)) {
                        commonPropertiesHolder.setInputSpace(getIOSpace(cursorJob, commonPropertiesHolder.getVariablesAsReplacementMap()));
                    } else if (XMLTags.DS_OUTPUT_SPACE.matches(current)) {
                        commonPropertiesHolder.setOutputSpace(getIOSpace(cursorJob, commonPropertiesHolder.getVariablesAsReplacementMap()));
                    } else if (XMLTags.DS_GLOBAL_SPACE.matches(current)) {
                        commonPropertiesHolder.setGlobalSpace(getIOSpace(cursorJob, commonPropertiesHolder.getVariablesAsReplacementMap()));
                    } else if (XMLTags.DS_USER_SPACE.matches(current)) {
                        commonPropertiesHolder.setUserSpace(getIOSpace(cursorJob, commonPropertiesHolder.getVariablesAsReplacementMap()));
                    } else if (XMLTags.TASK_FLOW.matches(current)) {
                        job = new TaskFlowJob();
                        shouldContinue = false;
                    }
                    break;
            }
        }
        handleJobAttributes(commonPropertiesHolder, delayedJobAttributes);
        // if this point is reached, fill the real job using the temporary one
        if (job != commonPropertiesHolder) {
            job.setDescription(commonPropertiesHolder.getDescription());
            job.setName(commonPropertiesHolder.getName());
            job.setPriority(commonPropertiesHolder.getPriority());
            job.setProjectName(commonPropertiesHolder.getProjectName());
            job.setOnTaskError(commonPropertiesHolder.getOnTaskErrorProperty().getValue());
            job.setRestartTaskOnError(commonPropertiesHolder.getRestartTaskOnError());
            job.setMaxNumberOfExecution(commonPropertiesHolder.getMaxNumberOfExecution());
            job.setGenericInformation(commonPropertiesHolder.getGenericInformation());
            job.setInputSpace(commonPropertiesHolder.getInputSpace());
            job.setOutputSpace(commonPropertiesHolder.getOutputSpace());
            job.setGlobalSpace(commonPropertiesHolder.getGlobalSpace());
            job.setUserSpace(commonPropertiesHolder.getUserSpace());
            job.setVariables(commonPropertiesHolder.getVariables());
        }
        return job;
    } catch (JobCreationException jce) {
        jce.pushTag(cursorJob.getLocalName());
        throw jce;
    } catch (Exception e) {
        String temporaryAttribute = null;
        if (cursorJob.isStartElement() && cursorJob.getAttributeCount() > i) {
            temporaryAttribute = cursorJob.getAttributeLocalName(i);
        }
        throw new JobCreationException(cursorJob.getLocalName(), temporaryAttribute, e);
    }
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) TaskFlowJob(org.ow2.proactive.scheduler.common.job.TaskFlowJob) JobCreationException(org.ow2.proactive.scheduler.common.exception.JobCreationException) TaskFlowJob(org.ow2.proactive.scheduler.common.job.TaskFlowJob) Job(org.ow2.proactive.scheduler.common.job.Job) XMLStreamException(javax.xml.stream.XMLStreamException) JobValidationException(org.ow2.proactive.scheduler.common.exception.JobValidationException) FileNotFoundException(java.io.FileNotFoundException) JobCreationException(org.ow2.proactive.scheduler.common.exception.JobCreationException) VerifierConfigurationException(org.iso_relax.verifier.VerifierConfigurationException)

Example 2 with JobType

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

the class StaxJobFactory method createAndFillJob.

/**
 * Create the real job and fill it with its property. Leave the method at
 * the first tag that define the real type of job.
 *
 * @param cursorJob          the streamReader with the cursor on the job element.
 * @param submittedJobVariables job submission variables map taking priority on those defined in the xml
 * @param submittedGenericInfos job submission generic infos map taking priority on those defined in the xml
 * @param jobContent contains xml representation of this job
 * @throws JobCreationException if an exception occurs during job creation.
 */
private Job createAndFillJob(XMLStreamReader cursorJob, Map<String, String> submittedJobVariables, Map<String, String> submittedGenericInfos, String jobContent, GlobalVariablesData globalVariablesData) throws JobCreationException {
    // A temporary job
    Job commonPropertiesHolder = new Job() {

        @Override
        public JobId getId() {
            throw new RuntimeException("Not Available!");
        }

        @Override
        public JobType getType() {
            throw new RuntimeException("Not Available!");
        }
    };
    // To allow variable replacements on the job attributes, store them until job variables are parsed
    Map<String, String> delayedJobAttributes = new LinkedHashMap<>();
    int attrLen = cursorJob.getAttributeCount();
    int i = 0;
    for (; i < attrLen; i++) {
        String attributeName = cursorJob.getAttributeLocalName(i);
        String attributeValue = cursorJob.getAttributeValue(i);
        delayedJobAttributes.put(attributeName, attributeValue);
    }
    Job job = commonPropertiesHolder;
    try {
        int eventType;
        // Start by adding to the temporary job, the global variables (with internal references enabled)
        commonPropertiesHolder.getVariables().putAll(replaceVariablesInJobVariablesMap(globalVariablesData.getVariables(), convertToReplacementMap(globalVariablesData.getVariables())));
        commonPropertiesHolder.addGenericInformations(getResolvedGenericInformations(globalVariablesData.getGenericInformation(), commonPropertiesHolder.getVariablesAsReplacementMap()));
        // Then add job submission variables, which will override eventually global variables
        if (submittedJobVariables != null) {
            for (String variableName : submittedJobVariables.keySet()) {
                if (commonPropertiesHolder.getVariables().containsKey(variableName)) {
                    commonPropertiesHolder.getVariables().get(variableName).setValue(submittedJobVariables.get(variableName));
                } else {
                    commonPropertiesHolder.getVariables().put(variableName, new JobVariable(variableName, submittedJobVariables.get(variableName), null));
                }
            }
            // enable referencing of global variables by submitted variables
            commonPropertiesHolder.getVariables().putAll(replaceVariablesInJobVariablesMap(commonPropertiesHolder.getVariables(), commonPropertiesHolder.getVariablesAsReplacementMap()));
        }
        // Then add job submission generic information, resolved using job submission and global variables
        if (submittedGenericInfos != null) {
            commonPropertiesHolder.addGenericInformations(getResolvedGenericInformations(submittedGenericInfos, commonPropertiesHolder.getVariablesAsReplacementMap()));
        }
        // Continue to fill the temporary job with xml elements
        while (cursorJob.hasNext()) {
            eventType = cursorJob.next();
            if (eventType == XMLEvent.START_ELEMENT) {
                String current = cursorJob.getLocalName();
                if (XMLTags.VARIABLES.matches(current)) {
                    // Add resolved job variables using the job submission variables
                    // the final value of the variable can either be overwritten by a value of the job submission variables map or
                    // use in a pattern such value
                    Map<String, JobVariable> unresolvedJobVariablesMap = createUnresolvedJobVariables(cursorJob, globalVariablesData);
                    commonPropertiesHolder.getUnresolvedVariables().putAll(unresolvedJobVariablesMap);
                    Map<String, JobVariable> jobVariablesMap = replaceVariablesInJobVariablesMap(unresolvedJobVariablesMap, submittedJobVariables);
                    // this is to ensure preserving the order of variables defined in the workflow
                    for (String key : jobVariablesMap.keySet()) {
                        commonPropertiesHolder.getVariables().remove(key);
                    }
                    commonPropertiesHolder.getVariables().putAll(jobVariablesMap);
                } else if (XMLTags.COMMON_GENERIC_INFORMATION.matches(current)) {
                    // Resolve the generic infos in the xml with the resolved variables
                    Map<String, String> resolvedJobVariables = commonPropertiesHolder.getVariablesAsReplacementMap();
                    Map<String, String> unresolvedGenericInformationsDefinedInWorkflow = getUnresolvedGenericInformations(cursorJob, false, globalVariablesData);
                    Map<String, String> resolvedGenericInformationsDefinedInWorkflow = getResolvedGenericInformations(unresolvedGenericInformationsDefinedInWorkflow, resolvedJobVariables);
                    // Then add/replace the resolved generic infos in the xml with the ones specified at job submission
                    if (submittedGenericInfos != null) {
                        Map<String, String> submittedGenericInformations = getResolvedGenericInformations(submittedGenericInfos, commonPropertiesHolder.getVariablesAsReplacementMap());
                        resolvedGenericInformationsDefinedInWorkflow.putAll(submittedGenericInformations);
                    }
                    // Update the temporary job
                    commonPropertiesHolder.setGenericInformation(resolvedGenericInformationsDefinedInWorkflow);
                    commonPropertiesHolder.setUnresolvedGenericInformation(unresolvedGenericInformationsDefinedInWorkflow);
                } else if (XMLTags.JOB_CLASSPATHES.matches(current)) {
                    logger.warn("Element " + XMLTags.JOB_CLASSPATHES.getXMLName() + " is no longer supported. Please define a " + XMLTags.FORK_ENVIRONMENT.getXMLName() + " per task if needed.");
                } else if (XMLTags.COMMON_DESCRIPTION.matches(current)) {
                    commonPropertiesHolder.setDescription(getDescription(cursorJob, commonPropertiesHolder.getVariablesAsReplacementMap()));
                } else if (XMLTags.DS_INPUT_SPACE.matches(current)) {
                    commonPropertiesHolder.setInputSpace(getIOSpace(cursorJob, commonPropertiesHolder.getVariablesAsReplacementMap()));
                } else if (XMLTags.DS_OUTPUT_SPACE.matches(current)) {
                    commonPropertiesHolder.setOutputSpace(getIOSpace(cursorJob, commonPropertiesHolder.getVariablesAsReplacementMap()));
                } else if (XMLTags.DS_GLOBAL_SPACE.matches(current)) {
                    commonPropertiesHolder.setGlobalSpace(getIOSpace(cursorJob, commonPropertiesHolder.getVariablesAsReplacementMap()));
                } else if (XMLTags.DS_USER_SPACE.matches(current)) {
                    commonPropertiesHolder.setUserSpace(getIOSpace(cursorJob, commonPropertiesHolder.getVariablesAsReplacementMap()));
                } else if (XMLTags.TASK_FLOW.matches(current)) {
                    job = new TaskFlowJob();
                    // Stop cursor at the beginning of 'taskflow' tag, at this level all job properties are extracted except metadata
                    break;
                }
            }
        }
        handleJobAttributes(commonPropertiesHolder, delayedJobAttributes);
        // if this point is reached, fill the real job using the temporary one
        if (job != commonPropertiesHolder) {
            job.setDescription(commonPropertiesHolder.getDescription());
            job.setName(commonPropertiesHolder.getName());
            job.setPriority(commonPropertiesHolder.getPriority());
            job.setProjectName(commonPropertiesHolder.getProjectName());
            job.setOnTaskError(commonPropertiesHolder.getOnTaskErrorProperty().getValue());
            job.setRestartTaskOnError(commonPropertiesHolder.getRestartTaskOnError());
            if (commonPropertiesHolder.getTaskRetryDelayProperty().isSet()) {
                job.setTaskRetryDelay(commonPropertiesHolder.getTaskRetryDelay());
            }
            job.setMaxNumberOfExecution(commonPropertiesHolder.getMaxNumberOfExecution());
            job.setGenericInformation(commonPropertiesHolder.getGenericInformation());
            job.setUnresolvedGenericInformation(commonPropertiesHolder.getUnresolvedGenericInformation());
            job.setInputSpace(commonPropertiesHolder.getInputSpace());
            job.setOutputSpace(commonPropertiesHolder.getOutputSpace());
            job.setGlobalSpace(commonPropertiesHolder.getGlobalSpace());
            job.setUserSpace(commonPropertiesHolder.getUserSpace());
            job.setVariables(commonPropertiesHolder.getVariables());
            job.setUnresolvedVariables(commonPropertiesHolder.getUnresolvedVariables());
            job.setVisualization(commonPropertiesHolder.getVisualization());
            String updatedJobContent = getJobContentFactory.replaceVarsAndGenericInfo(jobContent, commonPropertiesHolder.getVariables(), commonPropertiesHolder.getGenericInformation());
            job.setJobContent(updatedJobContent);
        }
        return job;
    } catch (JobCreationException jce) {
        jce.pushTag(cursorJob.getLocalName());
        throw jce;
    } catch (Exception e) {
        String temporaryAttribute = null;
        if (cursorJob.isStartElement() && cursorJob.getAttributeCount() > i) {
            temporaryAttribute = cursorJob.getAttributeLocalName(i);
        }
        throw new JobCreationException(cursorJob.getLocalName(), temporaryAttribute, e);
    }
}
Also used : TaskFlowJob(org.ow2.proactive.scheduler.common.job.TaskFlowJob) JobCreationException(org.ow2.proactive.scheduler.common.exception.JobCreationException) TaskFlowJob(org.ow2.proactive.scheduler.common.job.TaskFlowJob) Job(org.ow2.proactive.scheduler.common.job.Job) JobVariable(org.ow2.proactive.scheduler.common.job.JobVariable) LRUMap(org.apache.commons.collections4.map.LRUMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) XMLStreamException(javax.xml.stream.XMLStreamException) JobValidationException(org.ow2.proactive.scheduler.common.exception.JobValidationException) FileNotFoundException(java.io.FileNotFoundException) JobCreationException(org.ow2.proactive.scheduler.common.exception.JobCreationException) IOException(java.io.IOException) LinkedHashMap(java.util.LinkedHashMap)

Example 3 with JobType

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

the class DozerMappingTest method createJobState.

private JobState createJobState() {
    return new ClientJobState(new JobState() {

        @Override
        public void update(TaskInfo taskInfo) {
        }

        @Override
        public void update(JobInfo jobInfo) {
        }

        @Override
        public JobInfo getJobInfo() {
            return new JobInfoImpl();
        }

        @Override
        public ArrayList<TaskState> getTasks() {
            return new ArrayList<>(getHMTasks().values());
        }

        @Override
        public Map<TaskId, TaskState> getHMTasks() {
            TaskId taskId = TaskIdImpl.createTaskId(new JobIdImpl(42, "job"), "remoteVisuTask", 1);
            TaskState value = new ClientTaskState(new TaskState() {

                @Override
                public void update(TaskInfo taskInfo) {
                }

                @Override
                public List<TaskState> getDependences() {
                    return null;
                }

                @Override
                public TaskInfo getTaskInfo() {
                    TaskInfoImpl taskInfo = new TaskInfoImpl();
                    taskInfo.setTaskId(TaskIdImpl.createTaskId(new JobIdImpl(42, "job"), "remoteVisuTask", 1));
                    return taskInfo;
                }

                @Override
                public int getMaxNumberOfExecutionOnFailure() {
                    return 0;
                }

                @Override
                public TaskState replicate() throws Exception {
                    return null;
                }

                @Override
                public int getIterationIndex() {
                    return 0;
                }

                @Override
                public int getReplicationIndex() {
                    return 0;
                }
            });
            return Collections.singletonMap(taskId, value);
        }

        @Override
        public String getOwner() {
            return null;
        }

        @Override
        public JobType getType() {
            return null;
        }
    });
}
Also used : TaskId(org.ow2.proactive.scheduler.common.task.TaskId) ArrayList(java.util.ArrayList) TaskInfoImpl(org.ow2.proactive.scheduler.task.TaskInfoImpl) ClientTaskState(org.ow2.proactive.scheduler.task.ClientTaskState) TaskInfo(org.ow2.proactive.scheduler.common.task.TaskInfo) JobType(org.ow2.proactive.scheduler.common.job.JobType) JobInfo(org.ow2.proactive.scheduler.common.job.JobInfo) ClientJobState(org.ow2.proactive.scheduler.job.ClientJobState) JobState(org.ow2.proactive.scheduler.common.job.JobState) ClientJobState(org.ow2.proactive.scheduler.job.ClientJobState) JobIdImpl(org.ow2.proactive.scheduler.job.JobIdImpl) JobInfoImpl(org.ow2.proactive.scheduler.job.JobInfoImpl) Map(java.util.Map) TaskState(org.ow2.proactive.scheduler.common.task.TaskState) ClientTaskState(org.ow2.proactive.scheduler.task.ClientTaskState)

Example 4 with JobType

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

the class ClientJobStateTest method createJobState.

private JobState createJobState(final JobInfoImpl jobInfo) {
    return new JobState() {

        @Override
        public void update(TaskInfo info) {
        }

        @Override
        public void update(JobInfo jobInfo) {
        }

        @Override
        public JobInfo getJobInfo() {
            return jobInfo;
        }

        @Override
        public List<TaskState> getTasks() {
            List<TaskState> tasks = new ArrayList<>(0);
            tasks.add(new TaskState() {

                @Override
                public void update(TaskInfo taskInfo) {
                }

                @Override
                public List<TaskState> getDependences() {
                    return null;
                }

                @Override
                public TaskInfo getTaskInfo() {
                    TaskInfoImpl taskInfo = new TaskInfoImpl();
                    taskInfo.setJobInfo(jobInfo);
                    taskInfo.setTaskId(TaskIdImpl.createTaskId(jobInfo.getJobId(), "task", 1));
                    return taskInfo;
                }

                @Override
                public int getMaxNumberOfExecutionOnFailure() {
                    return 0;
                }

                @Override
                public TaskState replicate() throws Exception {
                    return null;
                }

                @Override
                public int getIterationIndex() {
                    return 0;
                }

                @Override
                public int getReplicationIndex() {
                    return 0;
                }
            });
            return tasks;
        }

        @Override
        public Map<TaskId, TaskState> getHMTasks() {
            return null;
        }

        @Override
        public String getOwner() {
            return null;
        }

        @Override
        public JobType getType() {
            return null;
        }
    };
}
Also used : TaskInfo(org.ow2.proactive.scheduler.common.task.TaskInfo) TaskId(org.ow2.proactive.scheduler.common.task.TaskId) JobInfo(org.ow2.proactive.scheduler.common.job.JobInfo) ArrayList(java.util.ArrayList) JobState(org.ow2.proactive.scheduler.common.job.JobState) TaskInfoImpl(org.ow2.proactive.scheduler.task.TaskInfoImpl) ArrayList(java.util.ArrayList) List(java.util.List) TaskState(org.ow2.proactive.scheduler.common.task.TaskState) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 XMLStreamException (javax.xml.stream.XMLStreamException)2 JobCreationException (org.ow2.proactive.scheduler.common.exception.JobCreationException)2 JobValidationException (org.ow2.proactive.scheduler.common.exception.JobValidationException)2 Job (org.ow2.proactive.scheduler.common.job.Job)2 JobInfo (org.ow2.proactive.scheduler.common.job.JobInfo)2 JobState (org.ow2.proactive.scheduler.common.job.JobState)2 TaskFlowJob (org.ow2.proactive.scheduler.common.job.TaskFlowJob)2 TaskId (org.ow2.proactive.scheduler.common.task.TaskId)2 TaskInfo (org.ow2.proactive.scheduler.common.task.TaskInfo)2 TaskState (org.ow2.proactive.scheduler.common.task.TaskState)2 TaskInfoImpl (org.ow2.proactive.scheduler.task.TaskInfoImpl)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 List (java.util.List)1