Search in sources :

Example 1 with JavaExecutable

use of org.ow2.proactive.scheduler.common.task.executable.JavaExecutable in project scheduling by ow2-proactive.

the class TestProcessTreeKiller method testProcessTreeKiller.

@Test
public void testProcessTreeKiller() throws Throwable {
    schedulerHelper.addExtraNodes(2);
    Logger.getLogger(ProcessTree.class).setLevel(Level.DEBUG);
    Logger.getLogger(TaskLauncher.class).setLevel(Level.DEBUG);
    for (int i = 0; i < TestProcessTreeKillerUtil.NB_ITERATIONS; i++) {
        log("***************************************************");
        log("************** Iteration " + i + " *************************");
        log("***************************************************");
        log("Creating job...");
        // create job 1 NativeExecutable
        TaskFlowJob job1 = new TaskFlowJob();
        job1.setName(this.getClass().getSimpleName() + "_1");
        job1.setDescription("a command that spawn processes");
        NativeTask task1 = new NativeTask();
        String task1Name = "TestPTK1";
        task1.setName(task1Name);
        String workingDir = new File(TestProcessTreeKillerUtil.launchersDir.toURI()).getParentFile().getCanonicalPath();
        task1.setForkEnvironment(new ForkEnvironment(workingDir));
        JavaSpawnExecutable executable = new JavaSpawnExecutable();
        executable.home = PASchedulerProperties.SCHEDULER_HOME.getValueAsString();
        task1.setCommandLine(executable.getNativeExecLauncher(false));
        job1.addTask(task1);
        String task2Name = "TestTK2";
        TaskFlowJob job2 = TestProcessTreeKillerUtil.createJavaExecutableJob(task2Name, true);
        log("************** Test with Job Killing *************");
        // submit three jobs
        JobId id1 = schedulerHelper.submitJob(job1);
        JobId id2 = schedulerHelper.submitJob(job2);
        schedulerHelper.waitForEventTaskRunning(id1, task1Name);
        schedulerHelper.waitForEventTaskRunning(id2, task2Name);
        log("************** All 2 tasks running *************");
        TestProcessTreeKillerUtil.waitUntilForkedProcessesAreRunning(TestProcessTreeKillerUtil.detachedProcNumber * 2);
        // we should have 2 times (2 jobs) number of detached processes
        // kill the first job
        log("************** Waiting for the first job (NativeExecutable) to be killed *************");
        schedulerHelper.getSchedulerInterface().killJob(id1);
        schedulerHelper.waitForEventJobFinished(id1);
        log("************** First job killed *************");
        TestProcessTreeKillerUtil.waitUntilForkedProcessesAreRunning(TestProcessTreeKillerUtil.detachedProcNumber);
        // kill the second job
        log("************** Waiting for the second job (JavaExecutable) to be killed *************");
        schedulerHelper.getSchedulerInterface().killJob(id2);
        schedulerHelper.waitForEventJobFinished(id2);
        log("************** Second job killed *************");
        TestProcessTreeKillerUtil.waitUntilForkedProcessesAreRunning(0);
        JobResult res = schedulerHelper.getJobResult(id1);
        assertEquals(JobStatus.KILLED, res.getJobInfo().getStatus());
        res = schedulerHelper.getJobResult(id2);
        assertEquals(JobStatus.KILLED, res.getJobInfo().getStatus());
        log("************** Test with Normal Job termination *************");
        // The test for normal termination in case of NativeExecutable is slightly different
        // we don't spawn here a native zombie process that will wait forever, because that would mean that the
        // NativeExecutable task will also wait forever !
        // This is related to the current implementation of NativeExecutable, as long as there are still some IO streamed
        // from the subprocesses of the main process, the task will wait
        // TODO improve the test by finding a way to run a detached process without IO redirection
        TaskFlowJob job4 = new TaskFlowJob();
        job4.setName(this.getClass().getSimpleName() + "_4");
        job4.setDescription("a command that spawn processes");
        NativeTask task4 = new NativeTask();
        String task4Name = "TestPTK4";
        task4.setName(task4Name);
        task4.setForkEnvironment(new ForkEnvironment(workingDir));
        task4.setCommandLine(executable.getNativeExecLauncher(true));
        job4.addTask(task4);
        // submit three jobs
        id1 = schedulerHelper.submitJob(job4);
        id2 = schedulerHelper.submitJob(job2);
        schedulerHelper.waitForEventTaskRunning(id1, task4Name);
        schedulerHelper.waitForEventTaskRunning(id2, task2Name);
        log("************** All 2 tasks running *************");
        TestProcessTreeKillerUtil.waitUntilForkedProcessesAreRunning(TestProcessTreeKillerUtil.detachedProcNumber);
        // we should have 1 time (2 jobs) number of detached processes as the first job won't spawn any process
        log("************** Waiting for both jobs to finish *************");
        // wait for the first job to finish normally
        schedulerHelper.waitForEventJobFinished(id1);
        schedulerHelper.waitForEventJobFinished(id2);
        log("************** Both jobs finished *************");
        int runningDetachedProcNumber = TestProcessTreeKillerUtil.countProcesses();
        log("************** number of processes : " + runningDetachedProcNumber);
        assertEquals(0, runningDetachedProcNumber);
        res = schedulerHelper.getJobResult(id1);
        assertEquals(JobStatus.FINISHED, res.getJobInfo().getStatus());
        res = schedulerHelper.getJobResult(id2);
        assertEquals(JobStatus.FINISHED, res.getJobInfo().getStatus());
    }
}
Also used : JobResult(org.ow2.proactive.scheduler.common.job.JobResult) ProcessTree(org.ow2.proactive.process_tree_killer.ProcessTree) TaskLauncher(org.ow2.proactive.scheduler.task.TaskLauncher) TaskFlowJob(org.ow2.proactive.scheduler.common.job.TaskFlowJob) NativeTask(org.ow2.proactive.scheduler.common.task.NativeTask) ForkEnvironment(org.ow2.proactive.scheduler.common.task.ForkEnvironment) File(java.io.File) JobId(org.ow2.proactive.scheduler.common.job.JobId) Test(org.junit.Test)

Example 2 with JavaExecutable

use of org.ow2.proactive.scheduler.common.task.executable.JavaExecutable in project scheduling by ow2-proactive.

the class TestProcessTreeKillerNonForked method testProcessTreeKiller.

@Test
public void testProcessTreeKiller() throws Throwable {
    schedulerHelper.addExtraNodes(2);
    Logger.getLogger(ProcessTree.class).setLevel(Level.DEBUG);
    Logger.getLogger(TaskLauncher.class).setLevel(Level.DEBUG);
    for (int i = 0; i < TestProcessTreeKillerUtil.NB_ITERATIONS; i++) {
        log("***************************************************");
        log("************** Iteration " + i + " *************************");
        log("***************************************************");
        log("Creating job...");
        // create job 1 NativeExecutable
        TaskFlowJob job1 = new TaskFlowJob();
        job1.setName(this.getClass().getSimpleName() + "_1");
        job1.setDescription("a command that spawn processes");
        NativeTask task1 = new NativeTask();
        String task1Name = "TestPTK1";
        task1.setName(task1Name);
        // In non-forked mode, the java executable which spawns processes manages its own process tree killer
        JavaSpawnExecutable executable = new JavaSpawnExecutable();
        executable.home = PASchedulerProperties.SCHEDULER_HOME.getValueAsString();
        task1.setCommandLine(executable.getNativeExecLauncher(false));
        job1.addTask(task1);
        String task2Name = "TestTK2";
        TaskFlowJob job2 = TestProcessTreeKillerUtil.createJavaExecutableJob(task2Name, false);
        log("************** Test with Job Killing *************");
        // submit three jobs
        JobId id1 = schedulerHelper.submitJob(job1);
        JobId id2 = schedulerHelper.submitJob(job2);
        schedulerHelper.waitForEventTaskRunning(id1, task1Name);
        schedulerHelper.waitForEventTaskRunning(id2, task2Name);
        log("************** All 2 tasks running *************");
        TestProcessTreeKillerUtil.waitUntilForkedProcessesAreRunning(TestProcessTreeKillerUtil.detachedProcNumber * 2);
        // we should have 2 times (2 jobs) number of detached processes
        // kill the first job
        log("************** Waiting for the first job (NativeExecutable) to be killed *************");
        schedulerHelper.getSchedulerInterface().killJob(id1);
        schedulerHelper.waitForEventJobFinished(id1);
        log("************** First job killed *************");
        TestProcessTreeKillerUtil.waitUntilForkedProcessesAreRunning(TestProcessTreeKillerUtil.detachedProcNumber);
        // kill the second job
        log("************** Waiting for the second job (JavaExecutable) to be killed *************");
        schedulerHelper.getSchedulerInterface().killJob(id2);
        schedulerHelper.waitForEventJobFinished(id2);
        log("************** Second job killed *************");
        TestProcessTreeKillerUtil.waitUntilForkedProcessesAreRunning(0);
        JobResult res = schedulerHelper.getJobResult(id1);
        assertEquals(JobStatus.KILLED, res.getJobInfo().getStatus());
        res = schedulerHelper.getJobResult(id2);
        assertEquals(JobStatus.KILLED, res.getJobInfo().getStatus());
        log("************** Test with Normal Job termination *************");
        // The test for normal termination in case of NativeExecutable is slightly different
        // we don't spawn here a native zombie process that will wait forever, because that would mean that the
        // NativeExecutable task will also wait forever !
        // This is related to the current implementation of NativeExecutable, as long as there are still some IO streamed
        // from the subprocesses of the main process, the task will wait
        // TODO improve the test by finding a way to run a detached process without IO redirection
        TaskFlowJob job4 = new TaskFlowJob();
        job4.setName(this.getClass().getSimpleName() + "_4");
        job4.setDescription("a command that spawn processes");
        NativeTask task4 = new NativeTask();
        String task4Name = "TestPTK4";
        task4.setName(task4Name);
        task4.setCommandLine(executable.getNativeExecLauncher(true));
        task4.setPreciousLogs(true);
        job4.addTask(task4);
        // submit three jobs
        id1 = schedulerHelper.submitJob(job4);
        id2 = schedulerHelper.submitJob(job2);
        schedulerHelper.waitForEventTaskRunning(id1, task4Name);
        schedulerHelper.waitForEventTaskRunning(id2, task2Name);
        log("************** All 2 tasks running *************");
        TestProcessTreeKillerUtil.waitUntilForkedProcessesAreRunning(TestProcessTreeKillerUtil.detachedProcNumber);
        // we should have 1 time (2 jobs) number of detached processes as the first job won't spawn any process
        log("************** Waiting for both jobs to finish *************");
        schedulerHelper.waitForEventJobFinished(id1);
        schedulerHelper.waitForEventJobFinished(id2);
        schedulerHelper.waitForEventJobFinished(id1);
        log("************** Both job finished *************");
        int runningDetachedProcNumber = TestProcessTreeKillerUtil.countProcesses();
        log("************** number of processes : " + runningDetachedProcNumber);
        assertEquals(0, runningDetachedProcNumber);
        res = schedulerHelper.getJobResult(id1);
        assertEquals(JobStatus.FINISHED, res.getJobInfo().getStatus());
        res = schedulerHelper.getJobResult(id2);
        assertEquals(JobStatus.FINISHED, res.getJobInfo().getStatus());
    }
}
Also used : JobResult(org.ow2.proactive.scheduler.common.job.JobResult) ProcessTree(org.ow2.proactive.process_tree_killer.ProcessTree) TaskLauncher(org.ow2.proactive.scheduler.task.TaskLauncher) TaskFlowJob(org.ow2.proactive.scheduler.common.job.TaskFlowJob) NativeTask(org.ow2.proactive.scheduler.common.task.NativeTask) JobId(org.ow2.proactive.scheduler.common.job.JobId) Test(org.junit.Test)

Example 3 with JavaExecutable

use of org.ow2.proactive.scheduler.common.task.executable.JavaExecutable in project scheduling by ow2-proactive.

the class MonteCarloAverage method execute.

/**
 * @see JavaExecutable#execute(org.ow2.proactive.scheduler.common.task.TaskResult[])
 */
@Override
public Serializable execute(TaskResult... results) throws Throwable {
    double avrg = 0;
    int count = 0;
    getOut().print("Parameters are: ");
    for (TaskResult res : results) {
        if (!res.hadException()) {
            getOut().print(res.value() + " ");
            avrg += ((Double) (res.value())).doubleValue();
            count++;
        }
    }
    Double result = new Double(avrg / count);
    getOut().println("Average is: " + result);
    return result;
}
Also used : TaskResult(org.ow2.proactive.scheduler.common.task.TaskResult)

Example 4 with JavaExecutable

use of org.ow2.proactive.scheduler.common.task.executable.JavaExecutable in project scheduling by ow2-proactive.

the class JavaClassScriptEngine method getExecutable.

public JavaExecutable getExecutable(String userExecutableClassName) throws ExecutableCreationException {
    try {
        ClassLoader tcl = Thread.currentThread().getContextClassLoader();
        Class<?> userExecutableClass = tcl.loadClass(userExecutableClassName);
        return (JavaExecutable) userExecutableClass.newInstance();
    } catch (ClassNotFoundException e) {
        throw new ExecutableCreationException("Unable to instantiate JavaExecutable. " + userExecutableClassName + " class cannot be found", e);
    } catch (InstantiationException e) {
        throw new ExecutableCreationException("Unable to instantiate JavaExecutable. " + userExecutableClassName + " might not define no-args constructor", e);
    } catch (ClassCastException e) {
        throw new ExecutableCreationException("Unable to instantiate JavaExecutable. " + userExecutableClassName + " might not inherit from org.ow2.proactive.scheduler.common.task.executable.JavaExecutable", e);
    } catch (Throwable e) {
        throw new ExecutableCreationException("Unable to instantiate JavaExecutable", e);
    }
}
Also used : ExecutableCreationException(org.ow2.proactive.scheduler.common.exception.ExecutableCreationException) JavaExecutable(org.ow2.proactive.scheduler.common.task.executable.JavaExecutable)

Example 5 with JavaExecutable

use of org.ow2.proactive.scheduler.common.task.executable.JavaExecutable 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

Test (org.junit.Test)2 ProcessTree (org.ow2.proactive.process_tree_killer.ProcessTree)2 JobId (org.ow2.proactive.scheduler.common.job.JobId)2 JobResult (org.ow2.proactive.scheduler.common.job.JobResult)2 TaskFlowJob (org.ow2.proactive.scheduler.common.job.TaskFlowJob)2 NativeTask (org.ow2.proactive.scheduler.common.task.NativeTask)2 TaskResult (org.ow2.proactive.scheduler.common.task.TaskResult)2 JavaExecutable (org.ow2.proactive.scheduler.common.task.executable.JavaExecutable)2 TaskLauncher (org.ow2.proactive.scheduler.task.TaskLauncher)2 Throwables.getStackTraceAsString (com.google.common.base.Throwables.getStackTraceAsString)1 File (java.io.File)1 PrintStream (java.io.PrintStream)1 Serializable (java.io.Serializable)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 ScriptException (javax.script.ScriptException)1 WriterOutputStream (org.apache.commons.io.output.WriterOutputStream)1 ExecutableCreationException (org.ow2.proactive.scheduler.common.exception.ExecutableCreationException)1 ForkEnvironment (org.ow2.proactive.scheduler.common.task.ForkEnvironment)1