Search in sources :

Example 6 with ScriptResult

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

the class ProbabilisticSelectionManagerTest method testIncreasingProbabilityStatic.

@Test
public void testIncreasingProbabilityStatic() throws Exception {
    int nbNodes = 10;
    SelectionScript script = new SelectionScript("test", "groovy", false);
    ManagerObjects managerObjects = new ManagerObjects(nbNodes).invoke();
    SelectionManager selectionManager = managerObjects.getSelectionManager();
    ArrayList<RMNode> freeNodes = managerObjects.getFreeNodes();
    for (int i = 0; i < nbNodes; i++) {
        // we increase the probability for each node, but it should not change the result for static scripts
        for (int j = 0; j < i + 1; j++) {
            selectionManager.processScriptResult(script, Collections.EMPTY_MAP, new ScriptResult<>(true), freeNodes.get(i));
        }
    }
    List<RMNode> arrangedNodes = selectionManager.arrangeNodesForScriptExecution(freeNodes, Collections.singletonList(script), Collections.EMPTY_MAP);
    // nodes are expected to be sorted in initial order
    for (int i = 0; i < nbNodes; i++) {
        Assert.assertEquals("mocked-node-" + (i + 1), arrangedNodes.get(i).getNodeName());
        Assert.assertTrue(selectionManager.isPassed(script, Collections.EMPTY_MAP, arrangedNodes.get(i)));
    }
}
Also used : SelectionManager(org.ow2.proactive.resourcemanager.selection.SelectionManager) SelectionScript(org.ow2.proactive.scripting.SelectionScript) RMNode(org.ow2.proactive.resourcemanager.rmnode.RMNode) SelectionManagerTest(org.ow2.proactive.resourcemanager.selection.SelectionManagerTest) Test(org.junit.Test)

Example 7 with ScriptResult

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

the class ScriptExecutor method executeScripts.

/**
 * Runs selection scripts and process the results
 * returns node if it matches, null otherwise
 */
private Node executeScripts() {
    boolean selectionScriptSpecified = selectionScriptList != null && selectionScriptList.size() > 0;
    boolean nodeMatch = true;
    ScriptException exception = null;
    if (selectionScriptSpecified) {
        // initializing parallel script execution
        for (SelectionScript script : selectionScriptList) {
            if (manager.isPassed(script, criteria.getBindings(), rmnode)) {
                // already executed static script
                logger.debug(rmnode.getNodeURL() + " : " + script.hashCode() + " skipping script execution");
                continue;
            }
            logger.info(rmnode.getNodeURL() + " : " + script.hashCode() + " executing");
            try {
                ScriptResult<Boolean> scriptResult = rmnode.executeScript(script, criteria.getBindings());
                // processing the results
                if (!MOP.isReifiedObject(scriptResult) && scriptResult.getException() != null) {
                    // could not create script execution handler
                    // probably the node id down
                    logger.warn(rmnode.getNodeURL() + " : " + script.hashCode() + " exception", scriptResult.getException());
                    logger.warn(rmnode.getNodeURL() + " : pinging the node");
                    rmnode.getNodeSource().pingNode(rmnode.getNode());
                    nodeMatch = false;
                    break;
                } else {
                    try {
                        PAFuture.waitFor(scriptResult, PAResourceManagerProperties.RM_SELECT_SCRIPT_TIMEOUT.getValueAsLong());
                    } catch (ProActiveTimeoutException e) {
                        logger.warn("Timeout on " + rmnode.getNodeURL());
                        // do not produce an exception here
                        nodeMatch = false;
                        break;
                    }
                    // display the script result and output in the scheduler logs
                    if (scriptResult != null && logger.isInfoEnabled()) {
                        logger.info(rmnode.getNodeURL() + " : " + script.hashCode() + " result " + scriptResult.getResult());
                        if (scriptResult.getOutput() != null && scriptResult.getOutput().length() > 0) {
                            logger.info(rmnode.getNodeURL() + " : " + script.hashCode() + " output\n" + scriptResult.getOutput());
                        }
                    }
                    if (scriptResult != null && scriptResult.errorOccured()) {
                        nodeMatch = false;
                        exception = new ScriptException(scriptResult.getException());
                        logger.warn(rmnode.getNodeURL() + " : exception during the script execution", scriptResult.getException());
                    }
                    // selection manager at the same time. Returns whether node is selected.
                    if (!manager.processScriptResult(script, criteria.getBindings(), scriptResult, rmnode)) {
                        nodeMatch = false;
                        break;
                    }
                }
            } catch (Exception ex) {
                // proactive or network exception occurred when script was executed
                logger.warn(rmnode.getNodeURL() + " : " + script.hashCode() + " exception", ex);
                nodeMatch = false;
                exception = new ScriptException(ex);
                break;
            }
        }
    }
    manager.scriptExecutionFinished(rmnode.getNodeURL());
    if (selectionScriptSpecified && logger.isDebugEnabled()) {
        if (nodeMatch) {
            logger.debug(rmnode.getNodeURL() + " : selected");
        } else {
            logger.debug(rmnode.getNodeURL() + " : not selected");
        }
    }
    // cleaning the node
    try {
        rmnode.clean();
    } catch (Throwable t) {
        logger.warn(rmnode.getNodeURL() + " : exception in cleaning", t);
        logger.warn(rmnode.getNodeURL() + " : pinging the node");
        try {
            // 'pingNode' call can fail with exception if NodeSource was destroyed
            rmnode.getNodeSource().pingNode(rmnode.getNode());
        } catch (Throwable pingError) {
            logger.warn(rmnode.getNodeURL() + " : nodeSource " + rmnode.getNodeSourceName() + " seems to be removed ", pingError);
        }
        return null;
    }
    if (exception != null) {
        throw exception;
    }
    if (nodeMatch) {
        return rmnode.getNode();
    } else {
        return null;
    }
}
Also used : ProActiveTimeoutException(org.objectweb.proactive.core.ProActiveTimeoutException) ScriptException(org.ow2.proactive.scripting.ScriptException) SelectionScript(org.ow2.proactive.scripting.SelectionScript) ScriptException(org.ow2.proactive.scripting.ScriptException) ProActiveTimeoutException(org.objectweb.proactive.core.ProActiveTimeoutException)

Example 8 with ScriptResult

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

the class SelectionManager method executeScript.

public <T> List<ScriptResult<T>> executeScript(final Script<T> script, final Collection<RMNode> nodes, final Map<String, Serializable> bindings) {
    // TODO: add a specific timeout for script execution
    final long timeout = PAResourceManagerProperties.RM_EXECUTE_SCRIPT_TIMEOUT.getValueAsLong();
    final ArrayList<Callable<ScriptResult<T>>> scriptExecutors = new ArrayList<>(nodes.size());
    // Execute the script on each selected node
    for (final RMNode node : nodes) {
        scriptExecutors.add(new Callable<ScriptResult<T>>() {

            @Override
            public ScriptResult<T> call() throws Exception {
                // treated as ExecutionException
                try {
                    ScriptResult<T> res = node.executeScript(script, bindings);
                    PAFuture.waitFor(res, timeout);
                    return res;
                // return PAFuture.getFutureValue(res, timeout);
                } finally {
                    // cleaning the node
                    try {
                        node.clean();
                    } catch (Throwable ex) {
                        logger.error("Cannot clean the node " + node.getNodeURL(), ex);
                    }
                    SelectionManager.this.rmcore.unlockNodes(Collections.singleton(node.getNodeURL()));
                }
            }

            @Override
            public String toString() {
                return "executing script on " + node.getNodeURL();
            }
        });
    }
    // Invoke all Callables and get the list of futures
    List<Future<ScriptResult<T>>> futures = null;
    try {
        futures = this.scriptExecutorThreadPool.invokeAll(scriptExecutors, timeout, TimeUnit.MILLISECONDS);
    } catch (InterruptedException e) {
        logger.warn("Interrupted while waiting, unable to execute all scripts", e);
        Thread.currentThread().interrupt();
    }
    final List<ScriptResult<T>> results = new LinkedList<>();
    int index = 0;
    // waiting for the results
    for (final Future<ScriptResult<T>> future : futures) {
        final String description = scriptExecutors.get(index++).toString();
        ScriptResult<T> result = null;
        try {
            result = future.get();
        } catch (CancellationException e) {
            result = new ScriptResult<>(new ScriptException("Cancelled due to timeout expiration when " + description, e));
        } catch (InterruptedException e) {
            result = new ScriptResult<>(new ScriptException("Cancelled due to interruption when " + description));
        } catch (ExecutionException e) {
            // Unwrap the root exception
            Throwable rex = e.getCause();
            result = new ScriptResult<>(new ScriptException("Exception occured in script call when " + description, rex));
        }
        results.add(result);
    }
    return results;
}
Also used : ArrayList(java.util.ArrayList) Callable(java.util.concurrent.Callable) ScriptException(org.ow2.proactive.scripting.ScriptException) NotConnectedException(org.ow2.proactive.resourcemanager.exception.NotConnectedException) CancellationException(java.util.concurrent.CancellationException) ExecutionException(java.util.concurrent.ExecutionException) LinkedList(java.util.LinkedList) ScriptResult(org.ow2.proactive.scripting.ScriptResult) ScriptException(org.ow2.proactive.scripting.ScriptException) RMNode(org.ow2.proactive.resourcemanager.rmnode.RMNode) CancellationException(java.util.concurrent.CancellationException) PAFuture(org.objectweb.proactive.api.PAFuture) Future(java.util.concurrent.Future) ExecutionException(java.util.concurrent.ExecutionException)

Example 9 with ScriptResult

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

the class TestExecRemote method scriptOnNodeSource.

private void scriptOnNodeSource(String nsName, HashSet<String> nodesUrls) throws Exception {
    RMTHelper.log("Test 6 - Execute script on a specified nodesource name");
    SimpleScript script = new SimpleScript(TestExecRemote.simpleScriptContent, "javascript");
    HashSet<String> targets = new HashSet<>(1);
    targets.add(nsName);
    List<ScriptResult<Object>> results = rmHelper.getResourceManager().executeScript(script, TargetType.NODESOURCE_NAME.toString(), targets);
    assertEquals("The size of result list must equal to size of nodesource", nodesUrls.size(), results.size());
    for (ScriptResult<Object> res : results) {
        Throwable exception = res.getException();
        if (exception != null) {
            RMTHelper.log("An exception occured while executing the script remotely:");
            exception.printStackTrace(System.out);
        }
        assertNull("No exception must occur", exception);
    }
}
Also used : ScriptResult(org.ow2.proactive.scripting.ScriptResult) SimpleScript(org.ow2.proactive.scripting.SimpleScript) HashSet(java.util.HashSet)

Example 10 with ScriptResult

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

the class TestExecRemote method scriptOnHost.

private void scriptOnHost(String hostname) throws Exception {
    RMTHelper.log("Test 7 - Execute script with hostname as target");
    SimpleScript script = new SimpleScript(TestExecRemote.simpleScriptContent, "javascript");
    HashSet<String> targets = new HashSet<>(1);
    targets.add(hostname);
    List<ScriptResult<Object>> results = rmHelper.getResourceManager().executeScript(script, TargetType.HOSTNAME.toString(), targets);
    assertEquals("The size of result list must equal to 1, if a hostname is specified a single node must be " + "selected", results.size(), 1);
    for (ScriptResult<Object> res : results) {
        Throwable exception = res.getException();
        if (exception != null) {
            RMTHelper.log("An exception occured while executing the script remotely:");
            exception.printStackTrace(System.out);
        }
        assertNull("No exception must occur", exception);
    }
}
Also used : ScriptResult(org.ow2.proactive.scripting.ScriptResult) SimpleScript(org.ow2.proactive.scripting.SimpleScript) HashSet(java.util.HashSet)

Aggregations

ScriptResult (org.ow2.proactive.scripting.ScriptResult)18 SelectionScript (org.ow2.proactive.scripting.SelectionScript)10 RMNode (org.ow2.proactive.resourcemanager.rmnode.RMNode)8 Test (org.junit.Test)7 SimpleScript (org.ow2.proactive.scripting.SimpleScript)7 SelectionManager (org.ow2.proactive.resourcemanager.selection.SelectionManager)6 SelectionManagerTest (org.ow2.proactive.resourcemanager.selection.SelectionManagerTest)6 File (java.io.File)5 Serializable (java.io.Serializable)4 InvalidScriptException (org.ow2.proactive.scripting.InvalidScriptException)4 ScriptHandler (org.ow2.proactive.scripting.ScriptHandler)4 IOException (java.io.IOException)3 PrintStream (java.io.PrintStream)3 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 LoginException (javax.security.auth.login.LoginException)2 ForkEnvironment (org.ow2.proactive.scheduler.common.task.ForkEnvironment)2 RemoteSpace (org.ow2.proactive.scheduler.common.task.dataspaces.RemoteSpace)2