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)));
}
}
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;
}
}
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;
}
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);
}
}
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);
}
}
Aggregations