Search in sources :

Example 66 with Script

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

the class SelectionManager method runScripts.

/**
 * Runs scripts on given set of nodes and returns matched nodes. It blocks
 * until all results are obtained.
 *
 * @param candidates
 *            nodes to execute scripts on
 * @param criteria
 *            contains a set of scripts to execute on each node
 * @return nodes matched to all scripts
 */
private List<Node> runScripts(List<RMNode> candidates, Criteria criteria) {
    List<Node> matched = new LinkedList<>();
    if (candidates.size() == 0) {
        return matched;
    }
    // creating script executors object to be run in dedicated thread pool
    List<Callable<Node>> scriptExecutors = new LinkedList<>();
    synchronized (inProgress) {
        if (inProgress.size() > 0) {
            logger.warn(inProgress.size() + " nodes are in process of script execution");
            for (String nodeName : inProgress) {
                logger.warn(nodeName);
            }
            logger.warn("Something is wrong on these nodes");
        }
        for (RMNode node : candidates) {
            if (!inProgress.contains(node.getNodeURL())) {
                inProgress.add(node.getNodeURL());
                scriptExecutors.add(new ScriptExecutor(node, criteria, this));
            }
        }
    }
    try {
        // launching
        Collection<Future<Node>> matchedNodes = scriptExecutorThreadPool.invokeAll(scriptExecutors);
        // waiting for the results
        for (Future<Node> futureNode : matchedNodes) {
            Node node;
            try {
                node = futureNode.get();
                if (node != null) {
                    matched.add(node);
                }
            } catch (InterruptedException e) {
                logger.warn("Interrupting the selection manager");
                return matched;
            } catch (ExecutionException e) {
                logger.warn("Ignoring exception in selection script: " + e.getMessage());
            }
        }
    } catch (InterruptedException e1) {
        logger.warn("Interrupting the selection manager");
    }
    return matched;
}
Also used : RMNode(org.ow2.proactive.resourcemanager.rmnode.RMNode) Node(org.objectweb.proactive.core.node.Node) LinkedList(java.util.LinkedList) Callable(java.util.concurrent.Callable) RMNode(org.ow2.proactive.resourcemanager.rmnode.RMNode) PAFuture(org.objectweb.proactive.api.PAFuture) Future(java.util.concurrent.Future) ExecutionException(java.util.concurrent.ExecutionException)

Example 67 with Script

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

the class ProbablisticSelectionManager method processScriptResult.

/**
 * Processes script result and updates knowledge base of
 * selection manager at the same time.
 *
 * @param script - executed script
 * @param scriptResult - obtained script result
 * @param rmnode - node on which script has been executed
 * @return whether node is selected
 */
@Override
public synchronized boolean processScriptResult(SelectionScript script, Map<String, Serializable> bindings, ScriptResult<Boolean> scriptResult, RMNode rmnode) {
    boolean result = false;
    SelectionScript scriptWithReplacedBindings = replaceBindings(script, bindings);
    try {
        String digest = new String(scriptWithReplacedBindings.digest());
        Probability probability = new Probability(Probability.defaultValue());
        if (probabilities.containsKey(digest) && probabilities.get(digest).containsKey(rmnode.getNodeURL())) {
            probability = probabilities.get(digest).get(rmnode.getNodeURL());
            assert (probability.value() >= 0 && probability.value() <= 1);
        }
        if (scriptResult == null || scriptResult.errorOccured() || !scriptResult.getResult()) {
            // error during script execution or script returned false
            if (scriptWithReplacedBindings.isDynamic()) {
                probability.decrease();
            } else {
                probability = Probability.ZERO;
            }
        } else {
            // script passed
            result = true;
            if (scriptWithReplacedBindings.isDynamic()) {
                probability.increase();
            } else {
                probability = Probability.ONE;
            }
        }
        if (!probabilities.containsKey(digest)) {
            // checking if the number of selection script does not exceeded the maximum
            if (probabilities.size() >= PAResourceManagerProperties.RM_SELECT_SCRIPT_CACHE_SIZE.getValueAsInt()) {
                String oldest = digestQueue.poll();
                probabilities.remove(oldest);
                if (logger.isDebugEnabled()) {
                    logger.debug("Removing the script: " + scriptWithReplacedBindings.hashCode() + " from the data base because the limit is reached");
                }
            }
            // adding a new script record
            probabilities.put(digest, new HashMap<String, Probability>());
            logger.debug("Scripts cache size " + probabilities.size());
            digestQueue.offer(digest);
        }
        if (logger.isDebugEnabled()) {
            logger.debug(rmnode.getNodeURL() + " : script " + scriptWithReplacedBindings.hashCode() + ", probability " + probability);
        }
        probabilities.get(digest).put(rmnode.getNodeURL().intern(), probability);
    } catch (NoSuchAlgorithmException e) {
        logger.error(e.getMessage(), e);
    }
    return result;
}
Also used : SelectionScript(org.ow2.proactive.scripting.SelectionScript) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 68 with Script

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

the class ProbablisticSelectionManager method arrangeNodesForScriptExecution.

/**
 * Find appropriate candidates nodes for script execution, taking into
 * account "free" and "exclusion" nodes lists.
 *
 * @param scripts set of scripts to execute
 * @param nodes free nodes list provided by resource manager
 * @return candidates node list for script execution
 */
@Override
public List<RMNode> arrangeNodesForScriptExecution(final List<RMNode> nodes, List<SelectionScript> scripts, Map<String, Serializable> bindings) {
    long startTime = System.currentTimeMillis();
    boolean scriptSpecified = scripts != null && scripts.size() > 0;
    // if no scripts are specified return filtered free nodes
    if (!scriptSpecified) {
        return nodes;
    }
    try {
        // finding intersection
        HashMap<RMNode, Probability> intersectionMap = new LinkedHashMap<>();
        for (RMNode rmnode : nodes) {
            boolean intersection = true;
            double intersectionProbability = 1;
            for (SelectionScript script : scripts) {
                SelectionScript scriptWithReplacedBindings = replaceBindings(script, bindings);
                String digest = new String(scriptWithReplacedBindings.digest());
                if (probabilities.containsKey(digest) && probabilities.get(digest).containsKey(rmnode.getNodeURL())) {
                    double probability = probabilities.get(digest).get(rmnode.getNodeURL()).value();
                    if (probability == 0) {
                        intersection = false;
                        break;
                    } else {
                        intersectionProbability *= probability;
                    }
                } else {
                    intersectionProbability *= Probability.defaultValue();
                }
            }
            if (intersection) {
                intersectionMap.put(rmnode, new Probability(intersectionProbability));
            }
        }
        // sorting results based on calculated probability
        Set<RMNode> nodeSet = intersectionMap.keySet();
        List<RMNode> res = new ArrayList<>(nodeSet.size());
        res.addAll(nodeSet);
        Collections.sort(res, new NodeProbabilityComparator(intersectionMap));
        if (logger.isDebugEnabled()) {
            logger.debug("The following nodes are selected for scripts execution (time is " + (System.currentTimeMillis() - startTime) + " ms) :");
            if (res.size() > 0) {
                for (RMNode rmnode : res) {
                    logger.debug(rmnode.getNodeURL() + " : probability " + intersectionMap.get(rmnode));
                }
            } else {
                logger.debug("None");
            }
        }
        return res;
    } catch (NoSuchAlgorithmException e) {
        logger.error(e.getMessage(), e);
        return new ArrayList<>(0);
    }
}
Also used : ArrayList(java.util.ArrayList) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) LinkedHashMap(java.util.LinkedHashMap) RMNode(org.ow2.proactive.resourcemanager.rmnode.RMNode) SelectionScript(org.ow2.proactive.scripting.SelectionScript)

Example 69 with Script

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

the class TestExecRemote method processBuilderScript.

private void processBuilderScript(String miscDir, boolean isLinux, String valueToEcho, HashSet<String> nodesUrls) throws Exception {
    File sFile = new File(miscDir + "processBuilder.groovy");
    RMTHelper.log("Test 4 - Test " + sFile);
    String[] cmd = (isLinux) ? new String[] { "/bin/bash", "-c", "echo " + valueToEcho } : new String[] { "cmd.exe", "/c", "@(echo " + valueToEcho + ")" };
    SimpleScript script = new SimpleScript(sFile, cmd);
    List<ScriptResult<Object>> results = rmHelper.getResourceManager().executeScript(script, TargetType.NODE_URL.toString(), nodesUrls);
    assertFalse("The results must not be empty", results.size() == 0);
    for (ScriptResult<Object> res : results) {
        String output = res.getOutput();
        assertTrue("The script output must contains " + valueToEcho, output.contains(valueToEcho));
    }
}
Also used : ScriptResult(org.ow2.proactive.scripting.ScriptResult) SimpleScript(org.ow2.proactive.scripting.SimpleScript) File(java.io.File)

Example 70 with Script

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

the class NonBlockingCoreTest method action.

@Test
public void action() throws Exception {
    String rmconf = new File(PAResourceManagerProperties.getAbsolutePath(getClass().getResource("/functionaltests/config/functionalTRMProperties-long-selection-script-timeout.ini").getFile())).getAbsolutePath();
    rmHelper.startRM(rmconf);
    ResourceManager resourceManager = rmHelper.getResourceManager();
    int initialNodeNumber = 2;
    rmHelper.createNodeSource("NonBlockingCoreTest1", initialNodeNumber);
    long coreScriptExecutionTimeout = PAResourceManagerProperties.RM_SELECT_SCRIPT_TIMEOUT.getValueAsLong();
    long scriptSleepingTime = coreScriptExecutionTimeout * 2;
    log("Selecting node with timeout script");
    // create the static selection script object
    final SelectionScript sScript = new SelectionScript(new File(selectionScriptWithtimeOutPath.toURI()), new String[] { Long.toString(scriptSleepingTime) }, false);
    // mandatory to use RMUser AO, otherwise, getAtMostNode we be send in
    // RMAdmin request queue
    final RMAuthentication auth = rmHelper.getRMAuth();
    final Credentials cred = Credentials.createCredentials(new CredData(TestUsers.TEST.username, TestUsers.TEST.password), auth.getPublicKey());
    // cannot connect twice from the same active object
    // so creating another thread
    Thread t = new Thread() {

        public void run() {
            ResourceManager rm2;
            try {
                rm2 = auth.login(cred);
                nodes = rm2.getAtMostNodes(2, sScript);
            } catch (LoginException e) {
            }
        }
    };
    t.start();
    String nodeName = "node_non_blocking_test";
    testNode = RMTHelper.createNode(nodeName);
    String nodeUrl = testNode.getNode().getNodeInformation().getURL();
    log("Adding node " + nodeUrl);
    resourceManager.addNode(nodeUrl);
    rmHelper.waitForNodeEvent(RMEventType.NODE_ADDED, nodeUrl);
    // waiting for node to be in free state, it is in configuring state when
    // added...
    rmHelper.waitForAnyNodeEvent(RMEventType.NODE_STATE_CHANGED);
    assertEquals(resourceManager.getState().getTotalNodesNumber(), initialNodeNumber + 1);
    assertEquals(resourceManager.getState().getFreeNodesNumber(), initialNodeNumber + 1);
    // preemptive removal is useless for this case, because node is free
    log("Removing node " + nodeUrl);
    resourceManager.removeNode(nodeUrl, false);
    rmHelper.waitForNodeEvent(RMEventType.NODE_REMOVED, nodeUrl);
    assertEquals(resourceManager.getState().getTotalNodesNumber(), initialNodeNumber);
    assertEquals(resourceManager.getState().getFreeNodesNumber(), initialNodeNumber);
    String nsName = "NonBlockingCoreTest";
    log("Creating a node source " + nsName);
    int nsNodesNumber = 1;
    rmHelper.createNodeSource(nsName, nsNodesNumber);
    assertEquals(resourceManager.getState().getTotalNodesNumber(), nsNodesNumber + initialNodeNumber);
    assertEquals(resourceManager.getState().getFreeNodesNumber(), nsNodesNumber + initialNodeNumber);
    log("Removing node source " + nsName);
    resourceManager.removeNodeSource(nsName, true);
    boolean selectionInProgress = PAFuture.isAwaited(nodes);
    if (!selectionInProgress) {
        // normally we are looking for 2 nodes with timeout script,
        // so the selection procedure has to finish not earlier than
        // coreScriptExecutionTimeout * nodesNumber
        // it should be sufficient to perform all admin operations
        // concurrently
        // 
        // if the core is blocked admin operations will be performed after
        // selection
        fail("Blocked inside RMCore");
    } else {
        log("No blocking inside RMCore");
    }
    t.interrupt();
}
Also used : SelectionScript(org.ow2.proactive.scripting.SelectionScript) RMAuthentication(org.ow2.proactive.resourcemanager.authentication.RMAuthentication) CredData(org.ow2.proactive.authentication.crypto.CredData) LoginException(javax.security.auth.login.LoginException) ResourceManager(org.ow2.proactive.resourcemanager.frontend.ResourceManager) File(java.io.File) Credentials(org.ow2.proactive.authentication.crypto.Credentials) Test(org.junit.Test) RMFunctionalTest(functionaltests.utils.RMFunctionalTest) SelectionScriptTimeOutTest(functionaltests.selectionscript.SelectionScriptTimeOutTest)

Aggregations

Test (org.junit.Test)38 SelectionScript (org.ow2.proactive.scripting.SelectionScript)32 File (java.io.File)31 SimpleScript (org.ow2.proactive.scripting.SimpleScript)28 TaskFlowJob (org.ow2.proactive.scheduler.common.job.TaskFlowJob)25 ArrayList (java.util.ArrayList)16 JavaTask (org.ow2.proactive.scheduler.common.task.JavaTask)15 Script (org.ow2.proactive.scripting.Script)15 ScriptResult (org.ow2.proactive.scripting.ScriptResult)15 FlowScript (org.ow2.proactive.scheduler.common.task.flow.FlowScript)13 HashMap (java.util.HashMap)12 RMNode (org.ow2.proactive.resourcemanager.rmnode.RMNode)11 IOException (java.io.IOException)10 JobId (org.ow2.proactive.scheduler.common.job.JobId)10 NodeSet (org.ow2.proactive.utils.NodeSet)10 RMFunctionalTest (functionaltests.utils.RMFunctionalTest)9 ResourceManager (org.ow2.proactive.resourcemanager.frontend.ResourceManager)9 Job (org.ow2.proactive.scheduler.common.job.Job)8 ForkEnvironment (org.ow2.proactive.scheduler.common.task.ForkEnvironment)8 Serializable (java.io.Serializable)7