use of org.ow2.proactive_grid_cloud_portal.studio.Script in project scheduling by ow2-proactive.
the class StaxJobFactory method createSelectionScript.
/**
* Get the selection script defined at the specified cursor.
* Leave the method with cursor at the end of the 'ELEMENT_SCRIPT_SELECTION' script.
*
* @param cursorScript the streamReader with the cursor on the 'ELEMENT_SCRIPT_SELECTION' tag.
* @return the script defined at the specified cursor.
*/
private List<SelectionScript> createSelectionScript(XMLStreamReader cursorScript, Map<String, String> variables) throws JobCreationException {
List<SelectionScript> scripts = new ArrayList<>(0);
String selectionTag = cursorScript.getLocalName();
String current = null;
try {
SelectionScript newOne;
int eventType;
while (cursorScript.hasNext()) {
eventType = cursorScript.next();
switch(eventType) {
case XMLEvent.START_ELEMENT:
current = cursorScript.getLocalName();
if (XMLTags.SCRIPT_SCRIPT.matches(current)) {
newOne = (SelectionScript) createScript(cursorScript, ScriptType.SELECTION, variables);
scripts.add(newOne);
}
break;
case XMLEvent.END_ELEMENT:
current = cursorScript.getLocalName();
if (current.equals(selectionTag)) {
if (scripts.size() == 0) {
return null;
} else {
return scripts;
}
}
break;
}
}
} catch (JobCreationException jce) {
jce.pushTag(current);
throw jce;
} catch (Exception e) {
throw new JobCreationException(current, null, e);
}
return scripts;
}
use of org.ow2.proactive_grid_cloud_portal.studio.Script in project scheduling by ow2-proactive.
the class StaxJobFactory method displayJobInfo.
private void displayJobInfo(Job job) {
if (logger.isDebugEnabled()) {
logger.debug("type: " + job.getType());
logger.debug("name: " + job.getName());
logger.debug("description: " + job.getDescription());
logger.debug("projectName: " + job.getProjectName());
logger.debug("variables: " + job.getVariables());
logger.debug("priority: " + job.getPriority());
logger.debug("onTaskError: " + job.getOnTaskErrorProperty().getValue().toString());
logger.debug("restartTaskOnError: " + job.getRestartTaskOnError());
logger.debug("maxNumberOfExecution: " + job.getMaxNumberOfExecution());
logger.debug("inputSpace: " + job.getInputSpace());
logger.debug("outputSpace: " + job.getOutputSpace());
logger.debug("genericInformation: " + job.getGenericInformation());
logger.debug("TASKS ------------------------------------------------");
ArrayList<Task> tasks = new ArrayList<>();
switch(job.getType()) {
case TASKSFLOW:
tasks.addAll(((TaskFlowJob) job).getTasks());
break;
default:
break;
}
for (Task t : tasks) {
logger.debug("name: " + t.getName());
logger.debug("description: " + t.getDescription());
logger.debug("parallel: " + t.isParallel());
logger.debug("nbNodes: " + (t.getParallelEnvironment() == null ? "1" : t.getParallelEnvironment().getNodesNumber()));
logger.debug("onTaskError: " + t.getOnTaskErrorProperty().getValue().toString());
logger.debug("preciousResult: " + t.isPreciousResult());
logger.debug("preciousLogs: " + t.isPreciousLogs());
logger.debug("restartTaskOnError: " + t.getRestartTaskOnError());
logger.debug("maxNumberOfExecution: " + t.getMaxNumberOfExecution());
logger.debug("walltime: " + t.getWallTime());
logger.debug("selectionScripts: " + t.getSelectionScripts());
logger.debug("preScript: " + t.getPreScript());
logger.debug("postScript: " + t.getPostScript());
logger.debug("cleaningScript: " + t.getCleaningScript());
try {
logger.debug("inputFileList: length=" + t.getInputFilesList().size());
} catch (NullPointerException ignored) {
}
try {
logger.debug("outputFileList: length=" + t.getOutputFilesList().size());
} catch (NullPointerException ignored) {
}
if (t.getDependencesList() != null) {
String dep = "dependence: ";
for (Task tdep : t.getDependencesList()) {
dep += tdep.getName() + " ";
}
logger.debug(dep);
} else {
logger.debug("dependence: null");
}
logger.debug("genericInformation: " + t.getGenericInformation());
logger.debug("variables: " + t.getVariables());
if (t instanceof JavaTask) {
logger.debug("class: " + ((JavaTask) t).getExecutableClassName());
try {
logger.debug("args: " + ((JavaTask) t).getArguments());
} catch (Exception e) {
logger.debug("Cannot get args: " + e.getMessage(), e);
}
logger.debug("fork: " + ((JavaTask) t).isFork());
} else if (t instanceof NativeTask) {
logger.debug("commandLine: " + Arrays.toString(((NativeTask) t).getCommandLine()));
} else if (t instanceof ScriptTask) {
logger.debug("script: " + ((ScriptTask) t).getScript());
}
ForkEnvironment forkEnvironment = t.getForkEnvironment();
if (forkEnvironment != null) {
logger.debug("javaHome: " + forkEnvironment.getJavaHome());
logger.debug("systemEnvironment: " + forkEnvironment.getSystemEnvironment());
logger.debug("jvmArguments: " + forkEnvironment.getJVMArguments());
logger.debug("classpath: " + forkEnvironment.getAdditionalClasspath());
logger.debug("envScript: " + forkEnvironment.getEnvScript());
}
logger.debug("--------------------------------------------------");
}
}
}
use of org.ow2.proactive_grid_cloud_portal.studio.Script in project scheduling by ow2-proactive.
the class StaxJobFactory method createScript.
/**
* Get the script defined at the specified cursor.
* Leave the method with cursor at the end of the corresponding script.
*
* @param cursorScript the streamReader with the cursor on the corresponding script tag (pre, post, cleaning, selection, generation).
* @param type nature of the script
* @return the script defined at the specified cursor.
*/
private Script<?> createScript(XMLStreamReader cursorScript, ScriptType type, Map<String, String> variables) throws JobCreationException {
String attrtmp = null;
String currentScriptTag = cursorScript.getLocalName();
String current = null;
try {
boolean isDynamic = true;
Script<?> toReturn = null;
int eventType = -1;
while (cursorScript.hasNext()) {
if (type == ScriptType.SELECTION && eventType == -1) {
eventType = cursorScript.getEventType();
} else {
eventType = cursorScript.next();
}
switch(eventType) {
case XMLEvent.START_ELEMENT:
current = cursorScript.getLocalName();
if (XMLTags.SCRIPT_CODE.matches(current)) {
String language = null;
String content = "";
if (cursorScript.getAttributeCount() > 0) {
language = cursorScript.getAttributeValue(0);
attrtmp = cursorScript.getAttributeLocalName(0);
}
// goto script content
if (cursorScript.next() == XMLEvent.CHARACTERS) {
content = cursorScript.getText();
}
toReturn = new SimpleScript(content, language);
// fast forward to the end of tag
while (true) {
int ev = cursorScript.next();
if (XMLTags.SCRIPT_CODE.matches(current) && ev == XMLEvent.END_ELEMENT) {
break;
}
}
} else if (XMLTags.SCRIPT_FILE.matches(current)) {
String path = null;
String url = null;
String language = null;
for (int i = 0; i < cursorScript.getAttributeCount(); i++) {
attrtmp = cursorScript.getAttributeLocalName(i);
if (XMLAttributes.SCRIPT_URL.matches(attrtmp)) {
url = replace(cursorScript.getAttributeValue(i), variables);
} else if (XMLAttributes.LANGUAGE.matches(attrtmp)) {
language = replace(cursorScript.getAttributeValue(i), variables);
} else if (XMLAttributes.PATH.matches(attrtmp)) {
path = checkPath(cursorScript.getAttributeValue(i), variables);
} else {
throw new JobCreationException("Unrecognized attribute : " + attrtmp);
}
}
// go to the next 'arguments' start element or the 'file' end element
while (true) {
int ev = cursorScript.next();
if (((ev == XMLEvent.START_ELEMENT) && XMLTags.SCRIPT_ARGUMENTS.matches(cursorScript.getLocalName())) || (ev == XMLEvent.END_ELEMENT)) {
break;
}
}
if (url != null) {
if (language != null) {
toReturn = new SimpleScript(new URL(url), language, getArguments(cursorScript));
} else {
toReturn = new SimpleScript(new URL(url), getArguments(cursorScript));
}
} else if (path != null) {
// language is ignored if a File is provided, the script language will be determined based on the file extension
toReturn = new SimpleScript(new File(path), getArguments(cursorScript));
} else {
attrtmp = null;
throw new JobCreationException("Invalid script file definition, one of path/url attributes must be declared");
}
} else if (XMLTags.SCRIPT_ARGUMENTS.matches(current)) {
toReturn = new SimpleScript(toReturn.getScript(), toReturn.getEngineName(), getArguments(cursorScript));
} else if (XMLTags.SCRIPT_SCRIPT.matches(current)) {
if (cursorScript.getAttributeCount() > 0) {
isDynamic = !"static".equals(cursorScript.getAttributeValue(0));
}
}
break;
case XMLEvent.END_ELEMENT:
if (cursorScript.getLocalName().equals(currentScriptTag)) {
if (type == ScriptType.SELECTION) {
return new SelectionScript(toReturn, isDynamic);
} else {
return toReturn;
}
}
break;
}
}
return toReturn;
} catch (JobCreationException jce) {
jce.pushTag(current);
throw jce;
} catch (Exception e) {
throw new JobCreationException(current, attrtmp, e);
}
}
use of org.ow2.proactive_grid_cloud_portal.studio.Script in project scheduling by ow2-proactive.
the class StaxJobFactory method createControlFlowScript.
private FlowScript createControlFlowScript(XMLStreamReader cursorTask, Task tmpTask, Map<String, String> variables) throws JobCreationException {
String type = null;
String target = null;
String targetElse = null;
String targetJoin = null;
int event = -1;
for (int i = 0; i < cursorTask.getAttributeCount(); i++) {
String attrName = cursorTask.getAttributeLocalName(i);
if (XMLAttributes.FLOW_BLOCK.matches(attrName)) {
tmpTask.setFlowBlock(FlowBlock.parse(replace(cursorTask.getAttributeValue(i), variables)));
}
}
// <control> => <if> | <loop> | <replicate>
try {
while (cursorTask.hasNext()) {
event = cursorTask.next();
if (event == XMLEvent.START_ELEMENT) {
break;
} else if (event == XMLEvent.END_ELEMENT && XMLTags.FLOW.matches(cursorTask.getLocalName())) {
return null;
}
}
} catch (Exception e) {
throw new JobCreationException(XMLTags.FLOW.getXMLName(), null, e);
}
if (event != XMLEvent.START_ELEMENT) {
throw new JobCreationException(XMLTags.FLOW.getXMLName(), null, null);
}
String tag = null;
// REPLICATE: no attribute
if (XMLTags.FLOW_REPLICATE.matches(cursorTask.getLocalName())) {
type = FlowActionType.REPLICATE.toString();
tag = XMLTags.FLOW_REPLICATE.getXMLName();
} else // IF: attributes TARGET_IF and TARGET_ELSE and TARGET_JOIN
if (XMLTags.FLOW_IF.matches(cursorTask.getLocalName())) {
type = FlowActionType.IF.toString();
tag = XMLTags.FLOW_IF.getXMLName();
for (int i = 0; i < cursorTask.getAttributeCount(); i++) {
String attrName = cursorTask.getAttributeLocalName(i);
if (XMLAttributes.FLOW_TARGET.matches(attrName)) {
target = cursorTask.getAttributeValue(i);
} else if (XMLAttributes.FLOW_ELSE.matches(attrName)) {
targetElse = cursorTask.getAttributeValue(i);
} else if (XMLAttributes.FLOW_CONTINUATION.matches(attrName)) {
targetJoin = cursorTask.getAttributeValue(i);
}
}
} else // LOOP: attribute TARGET
if (XMLTags.FLOW_LOOP.matches(cursorTask.getLocalName())) {
type = FlowActionType.LOOP.toString();
tag = XMLTags.FLOW_LOOP.getXMLName();
for (int i = 0; i < cursorTask.getAttributeCount(); i++) {
String attrName = cursorTask.getAttributeLocalName(i);
if (XMLAttributes.FLOW_TARGET.matches(attrName)) {
target = cursorTask.getAttributeValue(i);
}
}
}
FlowScript sc = null;
Script<?> internalScript;
try {
internalScript = createScript(cursorTask, ScriptType.FLOW, variables);
switch(FlowActionType.parse(type)) {
case IF:
sc = FlowScript.createIfFlowScript(internalScript, target, targetElse, targetJoin);
break;
case REPLICATE:
sc = FlowScript.createReplicateFlowScript(internalScript);
break;
case LOOP:
sc = FlowScript.createLoopFlowScript(internalScript, target);
break;
default:
break;
}
} catch (Exception e) {
throw new JobCreationException(tag, null, e);
}
// </script> --> </if> | </replicate> | </loop>
try {
while (cursorTask.hasNext()) {
event = cursorTask.next();
if (event == XMLEvent.END_ELEMENT) {
break;
}
}
} catch (XMLStreamException e) {
throw new JobCreationException(tag, null, e);
}
if (event != XMLEvent.END_ELEMENT) {
throw new JobCreationException(tag, null, null);
}
return sc;
}
use of org.ow2.proactive_grid_cloud_portal.studio.Script in project scheduling by ow2-proactive.
the class SelectionManager method doSelectNodes.
private NodeSet doSelectNodes(Criteria criteria, Client client) {
boolean hasScripts = criteria.getScripts() != null && criteria.getScripts().size() > 0;
boolean loggerIsDebugEnabled = logger.isDebugEnabled();
if (loggerIsDebugEnabled) {
logger.debug(client + " requested " + criteria.getSize() + " nodes with " + criteria.getTopology());
if (hasScripts) {
logger.debug("Selection scripts:");
for (SelectionScript s : criteria.getScripts()) {
logger.debug(s);
}
}
if (criteria.getBlackList() != null && criteria.getBlackList().size() > 0) {
logger.debug("Black list nodes:");
for (Node n : criteria.getBlackList()) {
logger.debug(n);
}
}
}
// can throw Exception if topology is disabled
TopologyHandler handler = RMCore.topologyManager.getHandler(criteria.getTopology());
int totalNumberOfAliveNodesRightNow = rmcore.getTotalAliveNodesNumber();
List<RMNode> freeNodes = rmcore.getFreeNodes();
// filtering out the "free node list"
// removing exclusion and checking permissions
List<RMNode> filteredNodes = filterOut(freeNodes, criteria, client);
if (filteredNodes.size() == 0) {
if (loggerIsDebugEnabled) {
logger.debug(client + " will get 0 nodes");
}
return new NodeSet();
}
// arranging nodes according to the selection policy
// if could be shuffling or node source priorities
List<RMNode> afterPolicyNodes = selectionPolicy.arrangeNodes(criteria.getSize(), filteredNodes, client);
List<Node> matchedNodes;
if (hasScripts) {
// checking if all scripts are authorized
checkAuthorizedScripts(criteria.getScripts());
// arranging nodes for script execution
List<RMNode> arrangedNodes = arrangeNodesForScriptExecution(afterPolicyNodes, criteria.getScripts(), criteria.getBindings());
List<RMNode> arrangedFilteredNodes = arrangedNodes;
if (criteria.getTopology().isTopologyBased()) {
arrangedFilteredNodes = topologyNodesFilter.filterNodes(criteria, arrangedNodes);
}
if (arrangedFilteredNodes.isEmpty()) {
matchedNodes = new LinkedList<>();
} else if (electedToRunOnAllNodes(criteria)) {
// run scripts on all available nodes
matchedNodes = runScripts(arrangedFilteredNodes, criteria);
} else {
// run scripts not on all nodes, but always on missing number of
// nodes
// until required node set is found
matchedNodes = new LinkedList<>();
while (matchedNodes.size() < criteria.getSize()) {
int numberOfNodesForScriptExecution = criteria.getSize() - matchedNodes.size();
if (numberOfNodesForScriptExecution < PAResourceManagerProperties.RM_SELECTION_MAX_THREAD_NUMBER.getValueAsInt()) {
// we can run
// "PAResourceManagerProperties.RM_SELECTION_MAX_THREAD_NUMBER.getValueAsInt()"
// scripts in parallel
// in case when we need less nodes it still useful to
// the full capacity of the thread pool to find nodes
// quicker
// it is not important if we find more nodes than needed
// subset will be selected later (topology handlers)
numberOfNodesForScriptExecution = PAResourceManagerProperties.RM_SELECTION_MAX_THREAD_NUMBER.getValueAsInt();
}
List<RMNode> subset = arrangedFilteredNodes.subList(0, Math.min(numberOfNodesForScriptExecution, arrangedFilteredNodes.size()));
matchedNodes.addAll(runScripts(subset, criteria));
// removing subset of arrangedNodes
subset.clear();
if (arrangedFilteredNodes.size() == 0) {
break;
}
}
if (loggerIsDebugEnabled) {
logger.debug(matchedNodes.size() + " nodes found after scripts execution for " + client);
}
}
} else {
matchedNodes = new LinkedList<>();
for (RMNode node : afterPolicyNodes) {
matchedNodes.add(node.getNode());
}
}
if (criteria.getTopology().isTopologyBased() && loggerIsDebugEnabled) {
logger.debug("Filtering nodes with topology " + criteria.getTopology());
}
NodeSet selectedNodes = handler.select(criteria.getSize(), matchedNodes);
if (selectedNodes.size() < criteria.getSize() && !criteria.isBestEffort()) {
selectedNodes.clear();
if (selectedNodes.getExtraNodes() != null) {
selectedNodes.getExtraNodes().clear();
}
}
// the nodes are selected, now mark them as busy.
for (Node node : selectedNodes) {
try {
// Synchronous call
rmcore.setBusyNode(node.getNodeInformation().getURL(), client);
} catch (NotConnectedException e) {
// client has disconnected during getNodes request
logger.warn(e.getMessage(), e);
return null;
}
}
// marking extra selected nodes as busy
if (selectedNodes.size() > 0 && selectedNodes.getExtraNodes() != null) {
for (Node node : new LinkedList<>(selectedNodes.getExtraNodes())) {
try {
// synchronous call
rmcore.setBusyNode(node.getNodeInformation().getURL(), client);
} catch (NotConnectedException e) {
// client has disconnected during getNodes request
logger.warn(e.getMessage(), e);
return null;
}
}
}
if (logger.isInfoEnabled()) {
String extraNodes = selectedNodes.getExtraNodes() != null && selectedNodes.getExtraNodes().size() > 0 ? " and " + selectedNodes.getExtraNodes().size() + " extra nodes" : "";
logger.info(client + " requested " + criteria.getSize() + " nodes with " + criteria.getTopology() + " and will get " + selectedNodes.size() + " nodes " + extraNodes + " [totalNumberOfAliveNodesRightNow:" + totalNumberOfAliveNodesRightNow + ";freeNodes:" + freeNodes.size() + ";filteredNodes:" + filteredNodes.size() + ";reordered after policy:" + afterPolicyNodes.size() + ";selection script present:" + hasScripts + ";nodes filtered by selection script:" + matchedNodes.size() + ";selectedNodes:" + selectedNodes.size() + "]");
}
if (loggerIsDebugEnabled) {
for (Node n : selectedNodes) {
logger.debug(n.getNodeInformation().getURL());
}
}
return selectedNodes;
}
Aggregations