Search in sources :

Example 31 with Script

use of org.ow2.proactive.scripting.Script in project scheduling by ow2-proactive.

the class TestTaskAttributes method testScripts.

@Test
public void testScripts() throws Exception {
    TaskFlowJob jobDef = new TaskFlowJob();
    JavaTask task1 = createDefaultTask("task1");
    task1.addSelectionScript(new SelectionScript("selection1", "js", new String[] { "param1", "param2" }, true));
    task1.addSelectionScript(new SelectionScript("selection2", "js", new String[] { "param3" }, false));
    task1.addSelectionScript(new SelectionScript("selection3", "js"));
    task1.setCleaningScript(new SimpleScript("cleanscript", "js", new String[] { "p1", "p2" }));
    task1.setPreScript(new SimpleScript("prescript", "js", new String[] { "p1", "p2" }));
    task1.setPostScript(new SimpleScript("postscript", "js", new String[] { "p1", "p2" }));
    task1.setFlowScript(FlowScript.createContinueFlowScript());
    jobDef.addTask(task1);
    InternalJob job = defaultSubmitJobAndLoadInternal(true, jobDef);
    InternalTask task = job.getTask("task1");
    Assert.assertEquals("cleanscript", task.getCleaningScript().getScript());
    Assert.assertArrayEquals(new String[] { "p1", "p2" }, task.getCleaningScript().getParameters());
    Assert.assertEquals("prescript", task.getPreScript().getScript());
    Assert.assertArrayEquals(new String[] { "p1", "p2" }, task.getPreScript().getParameters());
    Assert.assertEquals("postscript", task.getPostScript().getScript());
    Assert.assertArrayEquals(new String[] { "p1", "p2" }, task.getPostScript().getParameters());
    Assert.assertEquals(FlowActionType.CONTINUE.toString(), task.getFlowScript().getActionType());
    Assert.assertEquals(3, task.getSelectionScripts().size());
    Set<String> scripts = new HashSet<>();
    for (SelectionScript script : task.getSelectionScripts()) {
        scripts.add(script.getScript());
        if (script.getScript().equals("selection1")) {
            Assert.assertArrayEquals(new String[] { "param1", "param2" }, script.getParameters());
        }
        if (script.getScript().equals("selection2")) {
            Assert.assertArrayEquals(new String[] { "param3" }, script.getParameters());
        }
        if (script.getScript().equals("selection3")) {
            Assert.assertArrayEquals(new String[] {}, script.getParameters());
        }
    }
    Set<String> expected = new HashSet<>();
    expected.add("selection1");
    expected.add("selection2");
    expected.add("selection3");
    Assert.assertEquals(expected, scripts);
}
Also used : SelectionScript(org.ow2.proactive.scripting.SelectionScript) InternalJob(org.ow2.proactive.scheduler.job.InternalJob) InternalTask(org.ow2.proactive.scheduler.task.internal.InternalTask) TaskFlowJob(org.ow2.proactive.scheduler.common.job.TaskFlowJob) SimpleScript(org.ow2.proactive.scripting.SimpleScript) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 32 with Script

use of org.ow2.proactive.scripting.Script in project scheduling by ow2-proactive.

the class SchedulingMethodImpl method getNextcompatibleTasks.

/**
 * Extract the next compatible tasks from the eligible task list,
 *
 * Two tasks are compatible according to several criteria such as the same list of selection script,
 * the same node tokens, or node exclusions.
 * The check of compliance is currently done by the {@link SchedulingTaskComparator} class.<br>
 * This method has two side effects : extracted tasks are removed from the bagOfTasks and put in the toFill list
 *
 * @param bagOfTasks the list of tasks form which to extract tasks
 * @param toFill the list that will contains the task to schedule at the end. This list must not be null but must be empty.<br>
 * 		  this list will be filled with the first compatible tasks
 *
 * @return the number of nodes needed to start every task present in the 'toFill' argument at the end of the method.
 */
protected int getNextcompatibleTasks(Map<JobId, JobDescriptor> jobsMap, LinkedList<EligibleTaskDescriptor> bagOfTasks, LinkedList<EligibleTaskDescriptor> toFill) {
    if (toFill == null || bagOfTasks == null) {
        throw new IllegalArgumentException("The two given lists must not be null !");
    }
    int totalNeededNodes = 0;
    if (PASchedulerProperties.JETTY_STARTED.isSet() && !PASchedulerProperties.JETTY_STARTED.getValueAsBoolean()) {
        Iterator<EligibleTaskDescriptor> it = bagOfTasks.iterator();
        EligibleTaskDescriptor etd;
        while (it.hasNext()) {
            etd = it.next();
            if (checkEligibleTaskDescriptorScript.isTaskContainsAPIBinding(etd)) {
                // skip task here
                it.remove();
            }
        }
    }
    if (!bagOfTasks.isEmpty()) {
        EligibleTaskDescriptor etd = bagOfTasks.removeFirst();
        ((EligibleTaskDescriptorImpl) etd).addAttempt();
        InternalJob currentJob = ((JobDescriptorImpl) jobsMap.get(etd.getJobId())).getInternal();
        InternalTask internalTask = currentJob.getIHMTasks().get(etd.getTaskId());
        internalTask.updateVariables(schedulingService);
        int neededNodes = internalTask.getNumberOfNodesNeeded();
        SchedulingTaskComparator referent = new SchedulingTaskComparator(internalTask, currentJob);
        boolean firstLoop = true;
        do {
            if (!firstLoop) {
                // if bagOfTasks is not empty
                if (!bagOfTasks.isEmpty()) {
                    etd = bagOfTasks.removeFirst();
                    ((EligibleTaskDescriptorImpl) etd).addAttempt();
                    currentJob = ((JobDescriptorImpl) jobsMap.get(etd.getJobId())).getInternal();
                    internalTask = currentJob.getIHMTasks().get(etd.getTaskId());
                    internalTask.updateVariables(schedulingService);
                    neededNodes = internalTask.getNumberOfNodesNeeded();
                }
            } else {
                firstLoop = false;
            }
            // check if the task is compatible with the other previous one
            if (referent.equals(new SchedulingTaskComparator(internalTask, currentJob))) {
                tlogger.debug(internalTask.getId(), "scheduling");
                totalNeededNodes += neededNodes;
                toFill.add(etd);
            } else {
                bagOfTasks.addFirst(etd);
                break;
            }
        } while (!bagOfTasks.isEmpty());
    }
    return totalNeededNodes;
}
Also used : InternalJob(org.ow2.proactive.scheduler.job.InternalJob) EligibleTaskDescriptor(org.ow2.proactive.scheduler.descriptor.EligibleTaskDescriptor) InternalTask(org.ow2.proactive.scheduler.task.internal.InternalTask) EligibleTaskDescriptorImpl(org.ow2.proactive.scheduler.descriptor.EligibleTaskDescriptorImpl) JobDescriptorImpl(org.ow2.proactive.scheduler.descriptor.JobDescriptorImpl)

Example 33 with Script

use of org.ow2.proactive.scripting.Script in project scheduling by ow2-proactive.

the class SchedulingTaskComparator method computeHashForSelectionScripts.

private void computeHashForSelectionScripts(InternalTask task, InternalJob job) {
    List<SelectionScript> scriptList = SchedulingMethodImpl.resolveScriptVariables(task.getSelectionScripts(), task.getRuntimeVariables());
    for (SelectionScript script : scriptList) {
        SelectionScript modifiedScript = script;
        try {
            Map<String, Serializable> bindings = SchedulingMethodImpl.createBindingsForSelectionScripts(job, task, null);
            modifiedScript = SchedulingMethodImpl.replaceBindingsInsideScript(script, bindings);
        } catch (Exception e) {
            logger.error("Error while replacing selection script bindings for task " + task.getId(), e);
        }
        try {
            digests.add(new String(modifiedScript.digest()));
        } catch (NoSuchAlgorithmException e) {
            logger.error("Error while computing selection script digest for task " + task.getId(), e);
        }
    }
}
Also used : SelectionScript(org.ow2.proactive.scripting.SelectionScript) Serializable(java.io.Serializable) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 34 with Script

use of org.ow2.proactive.scripting.Script in project scheduling by ow2-proactive.

the class TerminationData method terminateRunningTask.

private void terminateRunningTask(SchedulingService service, TaskTerminationData taskToTerminate, RunningTaskData taskData) {
    Map<String, String> genericInformation = new HashMap<>();
    VariablesMap variables = null;
    if (taskToTerminate.internalJob != null) {
        genericInformation = taskData.getTask().getRuntimeGenericInformation();
    }
    try {
        variables = getStringSerializableMap(service, taskToTerminate);
    } catch (Exception e) {
        logger.error("Exception occurred, fail to get variables into the cleaning script: ", e);
    }
    try {
        if (taskToTerminate.terminationStatus == ABORTED) {
            taskData.getLauncher().kill();
        }
    } catch (Throwable t) {
        logger.info("Cannot terminate task launcher for task '" + taskData.getTask().getId() + "'", t);
        try {
            logger.info("Task launcher that cannot be terminated is identified by " + taskData.getLauncher().toString());
        } catch (Throwable ignore) {
            logger.info("Getting information about Task launcher failed (remote object not accessible?)");
        }
    }
    try {
        logger.debug("Releasing nodes for task '" + taskData.getTask().getId() + "'");
        RMProxiesManager proxiesManager = service.getInfrastructure().getRMProxiesManager();
        proxiesManager.getUserRMProxy(taskData.getUser(), taskData.getCredentials()).releaseNodes(taskData.getNodes(), getCleaningScript(taskToTerminate, taskData), variables, genericInformation, taskToTerminate.taskData.getTask().getId(), service.addThirdPartyCredentials(taskData.getCredentials()), new SynchronizationWrapper(taskToTerminate.internalJob.getOwner(), taskData.getTask().getId(), taskToTerminate.internalJob.getSynchronizationAPI()), new SignalApiImpl(taskToTerminate.internalJob.getOwner(), taskData.getTask().getId(), taskToTerminate.internalJob.getSynchronizationAPI()));
    } catch (Throwable t) {
        logger.info("Failed to release nodes for task '" + taskData.getTask().getId() + "'", t);
    }
}
Also used : HashMap(java.util.HashMap) SignalApiImpl(org.ow2.proactive.scheduler.signal.SignalApiImpl) VariablesMap(org.ow2.proactive.scheduler.task.utils.VariablesMap) IOException(java.io.IOException) RMProxiesManager(org.ow2.proactive.scheduler.core.rmproxies.RMProxiesManager) SynchronizationWrapper(org.ow2.proactive.scheduler.synchronization.SynchronizationWrapper)

Example 35 with Script

use of org.ow2.proactive.scripting.Script in project scheduling by ow2-proactive.

the class ForkedTaskVariablesManagerTest method testScriptParametersAreReplaced.

@Test
public void testScriptParametersAreReplaced() throws InvalidScriptException {
    ForkedTaskVariablesManager forkedTaskVariablesManager = new ForkedTaskVariablesManager();
    // Create a variable $[something] inside a python script
    Serializable[] parameters = new Serializable[] { "$" + testVariable1Key };
    Script script = new SimpleScript("print 'hello'", "python", parameters);
    // Create a hash map with key as variable name and value as variable value.
    Map<String, Serializable> variables = new HashMap<>();
    variables.put(testVariable1Key, testVariable1Value);
    VariablesMap variablesMap = new VariablesMap();
    variablesMap.setInheritedMap(variables);
    // Replace that variable inside the script parameters with its value in the hash map
    forkedTaskVariablesManager.replaceScriptParameters(script, new HashMap<String, String>(), variablesMap, System.out);
    assertThat((String) parameters[0], is(testVariable1Value));
}
Also used : TaskScript(org.ow2.proactive.scripting.TaskScript) Script(org.ow2.proactive.scripting.Script) SimpleScript(org.ow2.proactive.scripting.SimpleScript) Serializable(java.io.Serializable) HashMap(java.util.HashMap) SimpleScript(org.ow2.proactive.scripting.SimpleScript) VariablesMap(org.ow2.proactive.scheduler.task.utils.VariablesMap) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)46 SelectionScript (org.ow2.proactive.scripting.SelectionScript)40 File (java.io.File)38 SimpleScript (org.ow2.proactive.scripting.SimpleScript)33 TaskFlowJob (org.ow2.proactive.scheduler.common.job.TaskFlowJob)27 ScriptResult (org.ow2.proactive.scripting.ScriptResult)21 IOException (java.io.IOException)18 HashMap (java.util.HashMap)18 JavaTask (org.ow2.proactive.scheduler.common.task.JavaTask)16 Script (org.ow2.proactive.scripting.Script)16 JobId (org.ow2.proactive.scheduler.common.job.JobId)13 FlowScript (org.ow2.proactive.scheduler.common.task.flow.FlowScript)13 ArrayList (java.util.ArrayList)12 RMNode (org.ow2.proactive.resourcemanager.rmnode.RMNode)12 Serializable (java.io.Serializable)11 TaskScript (org.ow2.proactive.scripting.TaskScript)11 NodeSet (org.ow2.proactive.utils.NodeSet)11 RMFunctionalTest (functionaltests.utils.RMFunctionalTest)10 ResourceManager (org.ow2.proactive.resourcemanager.frontend.ResourceManager)10 ForkEnvironment (org.ow2.proactive.scheduler.common.task.ForkEnvironment)9