Search in sources :

Example 1 with TaskException

use of org.ow2.proactive.scheduler.task.exceptions.TaskException in project scheduling by ow2-proactive.

the class TestLoadJobResult method testLoadJobResult.

@Test
public void testLoadJobResult() throws Throwable {
    TaskFlowJob job = new TaskFlowJob();
    ForkEnvironment forkEnvironment = new ForkEnvironment();
    forkEnvironment.addAdditionalClasspath("/path1/path1");
    forkEnvironment.addAdditionalClasspath("/path2/path2");
    JavaTask javaTask1 = createDefaultTask("task1");
    javaTask1.setPreciousResult(true);
    javaTask1.setForkEnvironment(forkEnvironment);
    job.addTask(javaTask1);
    for (int i = 2; i <= 4; i++) {
        JavaTask task = createDefaultTask("task" + i);
        task.setForkEnvironment(forkEnvironment);
        job.addTask(task);
    }
    JavaTask javaTask5 = createDefaultTask("task5");
    javaTask5.setPreciousResult(true);
    javaTask5.setForkEnvironment(forkEnvironment);
    job.addTask(javaTask5);
    InternalJob internalJob = defaultSubmitJobAndLoadInternal(true, job);
    InternalTask task1 = internalJob.getTask("task1");
    InternalTask task2 = internalJob.getTask("task2");
    InternalTask task3 = internalJob.getTask("task3");
    InternalTask task4 = internalJob.getTask("task4");
    InternalTask task5 = internalJob.getTask("task5");
    dbManager.updateAfterTaskFinished(internalJob, task1, new TaskResultImpl(null, new TestResult(0, "1_1"), null, 0));
    dbManager.updateAfterTaskFinished(internalJob, task1, new TaskResultImpl(null, new TestResult(0, "1_2"), null, 0));
    dbManager.updateAfterTaskFinished(internalJob, task2, new TaskResultImpl(null, new TestResult(0, "2_1"), null, 0));
    dbManager.updateAfterTaskFinished(internalJob, task2, new TaskResultImpl(null, new TestResult(0, "2_2"), null, 0));
    dbManager.updateAfterTaskFinished(internalJob, task3, new TaskResultImpl(null, new TestResult(0, "3_1"), null, 0));
    dbManager.updateAfterTaskFinished(internalJob, task4, new TaskResultImpl(null, new TestException("message4_1", "data4_1"), null, 0));
    dbManager.updateAfterTaskFinished(internalJob, task4, new TaskResultImpl(null, new TestException("message4_2", "data4_2"), null, 0));
    dbManager.updateAfterTaskFinished(internalJob, task5, new TaskResultImpl(null, new TestException("message5_1", "data5_1"), null, 0));
    TaskFlowJob job2 = new TaskFlowJob();
    job2.addTask(createDefaultTask("job2 task1"));
    InternalJob internalJob2 = defaultSubmitJobAndLoadInternal(true, job2);
    InternalTask task2_1 = internalJob2.getTask("job2 task1");
    dbManager.updateAfterTaskFinished(internalJob2, task2_1, new TaskResultImpl(null, new TestResult(0, "job2_task1"), null, 0));
    System.out.println("Load job result1");
    JobResult result = dbManager.loadJobResult(internalJob.getId());
    Assert.assertEquals(5, result.getAllResults().size());
    Assert.assertEquals(2, result.getExceptionResults().size());
    Assert.assertEquals(2, result.getPreciousResults().size());
    Assert.assertNotNull(result.getJobInfo());
    Assert.assertEquals(internalJob.getId(), result.getJobId());
    Assert.assertEquals(5, result.getJobInfo().getTotalNumberOfTasks());
    TestResult taskResultValue;
    taskResultValue = (TestResult) result.getResult("task1").value();
    Assert.assertEquals("1_2", taskResultValue.getB());
    taskResultValue = (TestResult) result.getResult("task2").value();
    Assert.assertEquals("2_2", taskResultValue.getB());
    taskResultValue = (TestResult) result.getResult("task3").value();
    Assert.assertEquals("3_1", taskResultValue.getB());
    TestException taskException;
    taskException = (TestException) result.getResult("task4").getException();
    Assert.assertEquals("message4_2", taskException.getMessage());
    taskException = (TestException) result.getResult("task5").getException();
    Assert.assertEquals("message5_1", taskException.getMessage());
    System.out.println("Load job result2");
    result = dbManager.loadJobResult(internalJob2.getId());
    Assert.assertEquals(1, result.getAllResults().size());
    Assert.assertEquals(0, result.getExceptionResults().size());
    Assert.assertEquals(0, result.getPreciousResults().size());
}
Also used : InternalJob(org.ow2.proactive.scheduler.job.InternalJob) TaskResultImpl(org.ow2.proactive.scheduler.task.TaskResultImpl) InternalTask(org.ow2.proactive.scheduler.task.internal.InternalTask) JobResult(org.ow2.proactive.scheduler.common.job.JobResult) TaskFlowJob(org.ow2.proactive.scheduler.common.job.TaskFlowJob) JavaTask(org.ow2.proactive.scheduler.common.task.JavaTask) ForkEnvironment(org.ow2.proactive.scheduler.common.task.ForkEnvironment) Test(org.junit.Test)

Example 2 with TaskException

use of org.ow2.proactive.scheduler.task.exceptions.TaskException in project scheduling by ow2-proactive.

the class SchedulerClientTest method testJobResult.

@Test(timeout = MAX_WAIT_TIME)
public void testJobResult() throws Throwable {
    ISchedulerClient client = clientInstance();
    Job job = createJobManyTasks("JobResult", SimpleJob.class, ErrorTask.class, LogTask.class, VariableTask.class, MetadataTask.class, RawTask.class);
    JobId jobId = submitJob(job, client);
    JobResult result = client.waitForJob(jobId, TimeUnit.MINUTES.toMillis(3));
    // job result
    Assert.assertNotNull(result.getJobId());
    Assert.assertNotNull(result.getJobInfo());
    Assert.assertEquals(JobStatus.FINISHED, result.getJobInfo().getStatus());
    // the following check cannot work because of the way the job id is created on the client side.
    // Assert.assertEquals(job.getName(), result.getName());
    Assert.assertTrue(result.hadException());
    Assert.assertEquals(1, result.getExceptionResults().size());
    // job info
    checkJobInfo(result.getJobInfo());
    checkJobInfo(client.getJobInfo(jobId.value()));
    JobState jobState = client.getJobState(jobId.value());
    JobStatus status = jobState.getStatus();
    assertFalse(status.isJobAlive());
    Assert.assertEquals(JobStatus.FINISHED, status);
    checkJobInfo(jobState.getJobInfo());
    TaskState errorTaskState = findTask(getTaskNameForClass(ErrorTask.class), jobState.getHMTasks());
    Assert.assertNotNull(errorTaskState);
    TaskState simpleTaskState = findTask(getTaskNameForClass(SimpleJob.class), jobState.getHMTasks());
    Assert.assertNotNull(simpleTaskState);
    Assert.assertEquals(TaskStatus.FAULTY, errorTaskState.getStatus());
    Assert.assertEquals(TaskStatus.FINISHED, simpleTaskState.getStatus());
    // task result simple
    TaskResult tResSimple = result.getResult(getTaskNameForClass(SimpleJob.class));
    Assert.assertNotNull(tResSimple.value());
    Assert.assertNotNull(tResSimple.getSerializedValue());
    Assert.assertEquals(new StringWrapper(TEST_JOB), tResSimple.value());
    Assert.assertEquals(new StringWrapper(TEST_JOB), ObjectByteConverter.byteArrayToObject(tResSimple.getSerializedValue()));
    // task result with error
    TaskResult tResError = result.getResult(getTaskNameForClass(ErrorTask.class));
    Assert.assertNotNull(tResError);
    Assert.assertTrue(tResError.hadException());
    Assert.assertNotNull(tResError.getException());
    Assert.assertTrue(tResError.getException() instanceof TaskException);
    // task result with logs
    TaskResult tResLog = result.getResult(getTaskNameForClass(LogTask.class));
    Assert.assertNotNull(tResLog);
    Assert.assertNotNull(tResLog.getOutput());
    System.err.println(tResLog.getOutput().getStdoutLogs(false));
    Assert.assertTrue(tResLog.getOutput().getStdoutLogs(false).contains(LogTask.HELLO_WORLD));
    // task result with variables
    TaskResult tResVar = result.getResult(getTaskNameForClass(VariableTask.class));
    Assert.assertNotNull(tResVar.getPropagatedVariables());
    Map<String, Serializable> vars = tResVar.getVariables();
    System.out.println(vars);
    Assert.assertTrue(tResVar.getPropagatedVariables().containsKey(VariableTask.MYVAR));
    Assert.assertEquals("myvalue", vars.get(VariableTask.MYVAR));
    // task result with metadata
    TaskResult tMetaVar = result.getResult(getTaskNameForClass(MetadataTask.class));
    Assert.assertNotNull(tMetaVar.getMetadata());
    Assert.assertTrue(tMetaVar.getMetadata().containsKey(MetadataTask.MYVAR));
    // task result with raw result
    TaskResult tResRaw = result.getResult(getTaskNameForClass(RawTask.class));
    Assert.assertNotNull(tResRaw.value());
    Assert.assertNotNull(tResRaw.getSerializedValue());
    Assert.assertArrayEquals(TEST_JOB.getBytes(), (byte[]) tResRaw.value());
    Assert.assertArrayEquals(TEST_JOB.getBytes(), tResRaw.getSerializedValue());
}
Also used : StringWrapper(org.objectweb.proactive.core.util.wrapper.StringWrapper) Serializable(java.io.Serializable) JobResult(org.ow2.proactive.scheduler.common.job.JobResult) JobStatus(org.ow2.proactive.scheduler.common.job.JobStatus) VariableTask(functionaltests.jobs.VariableTask) TaskException(org.ow2.proactive.scheduler.task.exceptions.TaskException) RawTask(functionaltests.jobs.RawTask) MetadataTask(functionaltests.jobs.MetadataTask) ISchedulerClient(org.ow2.proactive.scheduler.rest.ISchedulerClient) JobState(org.ow2.proactive.scheduler.common.job.JobState) SimpleJob(functionaltests.jobs.SimpleJob) TaskResult(org.ow2.proactive.scheduler.common.task.TaskResult) ErrorTask(functionaltests.jobs.ErrorTask) TaskFlowJob(org.ow2.proactive.scheduler.common.job.TaskFlowJob) SimpleJob(functionaltests.jobs.SimpleJob) Job(org.ow2.proactive.scheduler.common.job.Job) NonTerminatingJob(functionaltests.jobs.NonTerminatingJob) TaskState(org.ow2.proactive.scheduler.common.task.TaskState) JobId(org.ow2.proactive.scheduler.common.job.JobId) LogTask(functionaltests.jobs.LogTask) Test(org.junit.Test)

Example 3 with TaskException

use of org.ow2.proactive.scheduler.task.exceptions.TaskException in project scheduling by ow2-proactive.

the class InProcessTaskExecutor method saveScriptAsFile.

private void saveScriptAsFile(String path, Script<?> script, TaskContext taskContext) throws Throwable {
    // If the path doesn't contain an extension, add an extension to it
    if (!path.contains(".")) {
        ScriptEngineFactory factory = new ScriptEngineManager().getEngineByName(script.getEngineName()).getFactory();
        String extension = factory.getExtensions().get(0);
        path = path + "." + extension;
    }
    String fetchedScript = null;
    try {
        fetchedScript = script.fetchScriptWithExceptionHandling();
    } catch (IOException e) {
        throw new TaskException("Error fetching script from url " + script.getScriptUrl(), e);
    }
    String code = fetchedScript.trim();
    Path p = Paths.get(path);
    // Check if the path is an absolute path or a relative path, if it is a relative path store the file in the local space
    boolean isAbsolute = p.isAbsolute();
    if (isAbsolute) {
        // Check if the parent path exists, if not create it
        Path hasParent = p.getParent();
        if (!Files.exists(hasParent)) {
            FileUtils.forceMkdirParent(new File(path));
        }
        try (FileWriter fw = new FileWriter(new File(path))) {
            fw.write(code);
        } catch (IOException e) {
            throw new TaskException("Error writing script as file: " + path, e);
        }
    } else {
        // Needs to be stored in the local space
        String uri = taskContext.getNodeDataSpaceURIs().getScratchURI();
        if (isDockerWindows2Linux) {
            uri = ForkEnvironment.convertToLinuxPath(uri);
        }
        p = Paths.get(uri, path);
        Path hasParent = p.getParent();
        if (!Files.exists(hasParent)) {
            FileUtils.forceMkdirParent(new File(uri, path));
        }
        try (FileWriter fw = new FileWriter(new File(uri, path))) {
            fw.write(code);
        } catch (IOException e) {
            throw new TaskException("Error writing script as file: " + path, e);
        }
    }
}
Also used : Path(java.nio.file.Path) TaskException(org.ow2.proactive.scheduler.task.exceptions.TaskException) ScriptEngineFactory(javax.script.ScriptEngineFactory) FileWriter(java.io.FileWriter) ScriptEngineManager(javax.script.ScriptEngineManager) IOException(java.io.IOException) File(java.io.File)

Example 4 with TaskException

use of org.ow2.proactive.scheduler.task.exceptions.TaskException in project scheduling by ow2-proactive.

the class InProcessTaskExecutor method execute.

/**
 * Executes a task.
 *
 * @param taskContext           Task context to execute
 * @param output                Standard output sink.
 * @param error                 Error sink.
 * @param scriptHandler
 * @param thirdPartyCredentials
 * @param variables             Environment variables.
 * @return The result of the executed script.
 * @throws Throwable
 */
private Serializable execute(TaskContext taskContext, PrintStream output, PrintStream error, ScriptHandler scriptHandler, Map<String, String> thirdPartyCredentials, VariablesMap variables) throws Throwable {
    if (taskContext.getPreScript() != null) {
        Script<?> script = taskContext.getPreScript();
        forkedTaskVariablesManager.replaceScriptParameters(script, thirdPartyCredentials, variables, error);
        Map<String, String> genericInfo = taskContext.getInitializer().getGenericInformation();
        if (genericInfo != null && genericInfo.containsKey("PRE_SCRIPT_AS_FILE")) {
            String path = genericInfo.get("PRE_SCRIPT_AS_FILE");
            saveScriptAsFile(path, script, taskContext);
        } else {
            ScriptResult preScriptResult = scriptHandler.handle(script, output, error);
            if (preScriptResult.errorOccured()) {
                throw new TaskException("Failed to execute pre script: " + preScriptResult.getException().getMessage(), preScriptResult.getException());
            }
        }
    }
    Script<Serializable> script = ((ScriptExecutableContainer) taskContext.getExecutableContainer()).getScript();
    forkedTaskVariablesManager.replaceScriptParameters(script, thirdPartyCredentials, variables, error);
    ScriptResult<Serializable> scriptResult = scriptHandler.handle(script, output, error);
    if (scriptResult.errorOccured()) {
        throw new TaskException("Failed to execute task: " + scriptResult.getException().getMessage(), scriptResult.getException());
    }
    if (taskContext.getPostScript() != null) {
        forkedTaskVariablesManager.replaceScriptParameters(taskContext.getPostScript(), thirdPartyCredentials, variables, error);
        try {
            scriptHandler.addBinding(TaskScript.RESULT_VARIABLE, scriptResult.getResult());
        } catch (Throwable throwable) {
            scriptHandler.addBinding(TaskScript.RESULT_VARIABLE, throwable);
        }
        ScriptResult postScriptResult = scriptHandler.handle(taskContext.getPostScript(), output, error);
        if (postScriptResult.errorOccured()) {
            throw new TaskException("Failed to execute post script: " + postScriptResult.getException().getMessage(), postScriptResult.getException());
        }
    }
    return scriptResult.getResult();
}
Also used : ScriptResult(org.ow2.proactive.scripting.ScriptResult) Serializable(java.io.Serializable) TaskException(org.ow2.proactive.scheduler.task.exceptions.TaskException) ScriptExecutableContainer(org.ow2.proactive.scheduler.task.containers.ScriptExecutableContainer)

Example 5 with TaskException

use of org.ow2.proactive.scheduler.task.exceptions.TaskException in project scheduling by ow2-proactive.

the class JavaClassScriptEngine method eval.

@Override
public Object eval(String userExecutableClassName, ScriptContext context) throws ScriptException {
    try {
        JavaExecutable javaExecutable = getExecutable(userExecutableClassName);
        JavaStandaloneExecutableInitializer execInitializer = new JavaStandaloneExecutableInitializer();
        PrintStream output = new PrintStream(new WriterOutputStream(context.getWriter()), true);
        execInitializer.setOutputSink(output);
        PrintStream error = new PrintStream(new WriterOutputStream(context.getErrorWriter()), true);
        execInitializer.setErrorSink(error);
        Map<String, byte[]> propagatedVariables = null;
        if (context.getAttribute(SchedulerConstants.VARIABLES_BINDING_NAME) != null) {
            propagatedVariables = SerializationUtil.serializeVariableMap(((VariablesMap) context.getAttribute(SchedulerConstants.VARIABLES_BINDING_NAME)).getPropagatedVariables());
            execInitializer.setPropagatedVariables(propagatedVariables);
        } else {
            execInitializer.setPropagatedVariables(Collections.<String, byte[]>emptyMap());
        }
        if (context.getAttribute(Script.ARGUMENTS_NAME) != null) {
            execInitializer.setSerializedArguments((Map<String, byte[]>) ((Serializable[]) context.getAttribute(Script.ARGUMENTS_NAME))[0]);
        } else {
            execInitializer.setSerializedArguments(Collections.<String, byte[]>emptyMap());
        }
        if (context.getAttribute(SchedulerConstants.CREDENTIALS_VARIABLE) != null) {
            execInitializer.setThirdPartyCredentials((Map<String, String>) context.getAttribute(SchedulerConstants.CREDENTIALS_VARIABLE));
        } else {
            execInitializer.setThirdPartyCredentials(Collections.<String, String>emptyMap());
        }
        if (context.getAttribute(SchedulerConstants.MULTI_NODE_TASK_NODESURL_BINDING_NAME) != null) {
            List<String> nodesURLs = (List<String>) context.getAttribute(SchedulerConstants.MULTI_NODE_TASK_NODESURL_BINDING_NAME);
            execInitializer.setNodesURL(nodesURLs);
        } else {
            execInitializer.setNodesURL(Collections.<String>emptyList());
        }
        if (context.getAttribute(SchedulerConstants.MULTI_NODE_TASK_ALL_NODESURL_BINDING_NAME) != null) {
            Set<String> allNodesURLs = (Set<String>) context.getAttribute(SchedulerConstants.MULTI_NODE_TASK_ALL_NODESURL_BINDING_NAME);
            execInitializer.setAllNodesURL(allNodesURLs);
        } else {
            execInitializer.setAllNodesURL(Collections.<String>emptySet());
        }
        javaExecutable.internalInit(execInitializer, context);
        Serializable execute = javaExecutable.execute((TaskResult[]) context.getAttribute(SchedulerConstants.RESULTS_VARIABLE));
        if (propagatedVariables != null) {
            ((Map<String, Serializable>) context.getAttribute(SchedulerConstants.VARIABLES_BINDING_NAME)).putAll(javaExecutable.getVariables());
        }
        output.close();
        error.close();
        return execute;
    } catch (Throwable e) {
        throw new ScriptException(new TaskException(getStackTraceAsString(e), e));
    }
}
Also used : PrintStream(java.io.PrintStream) Serializable(java.io.Serializable) Set(java.util.Set) Throwables.getStackTraceAsString(com.google.common.base.Throwables.getStackTraceAsString) WriterOutputStream(org.apache.commons.io.output.WriterOutputStream) JavaExecutable(org.ow2.proactive.scheduler.common.task.executable.JavaExecutable) ScriptException(javax.script.ScriptException) TaskException(org.ow2.proactive.scheduler.task.exceptions.TaskException) TaskResult(org.ow2.proactive.scheduler.common.task.TaskResult) VariablesMap(org.ow2.proactive.scheduler.task.utils.VariablesMap) List(java.util.List) JavaStandaloneExecutableInitializer(org.ow2.proactive.scheduler.common.task.executable.internal.JavaStandaloneExecutableInitializer) Map(java.util.Map) VariablesMap(org.ow2.proactive.scheduler.task.utils.VariablesMap)

Aggregations

TaskException (org.ow2.proactive.scheduler.task.exceptions.TaskException)4 Serializable (java.io.Serializable)3 Test (org.junit.Test)2 JobResult (org.ow2.proactive.scheduler.common.job.JobResult)2 TaskFlowJob (org.ow2.proactive.scheduler.common.job.TaskFlowJob)2 TaskResult (org.ow2.proactive.scheduler.common.task.TaskResult)2 Throwables.getStackTraceAsString (com.google.common.base.Throwables.getStackTraceAsString)1 ErrorTask (functionaltests.jobs.ErrorTask)1 LogTask (functionaltests.jobs.LogTask)1 MetadataTask (functionaltests.jobs.MetadataTask)1 NonTerminatingJob (functionaltests.jobs.NonTerminatingJob)1 RawTask (functionaltests.jobs.RawTask)1 SimpleJob (functionaltests.jobs.SimpleJob)1 VariableTask (functionaltests.jobs.VariableTask)1 File (java.io.File)1 FileWriter (java.io.FileWriter)1 IOException (java.io.IOException)1 PrintStream (java.io.PrintStream)1 Path (java.nio.file.Path)1 List (java.util.List)1