use of org.ow2.proactive_grid_cloud_portal.studio.Script in project scheduling by ow2-proactive.
the class Script method execute.
/**
* Execute the script and return the ScriptResult corresponding.
* This method can add an additional user bindings if needed.
*
* @param aBindings the additional user bindings to add if needed. Can be null or empty.
* @param outputSink where the script output is printed to.
* @param errorSink where the script error stream is printed to.
* @return a ScriptResult object.
*/
public ScriptResult<E> execute(Map<String, Object> aBindings, PrintStream outputSink, PrintStream errorSink) {
try {
fetchUrlIfNeeded();
} catch (Throwable t) {
String stack = Throwables.getStackTraceAsString(t);
if (t.getMessage() != null) {
stack = t.getMessage() + System.lineSeparator() + stack;
}
return new ScriptResult<>(new Exception(stack));
}
ScriptEngine engine = createScriptEngine();
if (engine == null)
return new ScriptResult<>(new Exception("No Script Engine Found for name or extension " + scriptEngineLookupName));
// SCHEDULING-1532: redirect script output to a buffer (keep the latest DEFAULT_OUTPUT_MAX_SIZE)
BoundedStringWriter outputBoundedWriter = new BoundedStringWriter(outputSink, DEFAULT_OUTPUT_MAX_SIZE);
BoundedStringWriter errorBoundedWriter = new BoundedStringWriter(errorSink, DEFAULT_OUTPUT_MAX_SIZE);
engine.getContext().setWriter(new PrintWriter(outputBoundedWriter));
engine.getContext().setErrorWriter(new PrintWriter(errorBoundedWriter));
Reader closedInput = new Reader() {
@Override
public int read(char[] cbuf, int off, int len) throws IOException {
throw new IOException("closed");
}
@Override
public void close() throws IOException {
}
};
engine.getContext().setReader(closedInput);
engine.getContext().setAttribute(ScriptEngine.FILENAME, scriptName, ScriptContext.ENGINE_SCOPE);
try {
Bindings bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE);
// add additional bindings
if (aBindings != null) {
for (Entry<String, Object> e : aBindings.entrySet()) {
bindings.put(e.getKey(), e.getValue());
}
}
prepareBindings(bindings);
Object evalResult = engine.eval(getReader());
engine.getContext().getErrorWriter().flush();
engine.getContext().getWriter().flush();
// Add output to the script result
ScriptResult<E> result = this.getResult(evalResult, bindings);
result.setOutput(outputBoundedWriter.toString());
return result;
} catch (javax.script.ScriptException e) {
// drop exception cause as it might not be serializable
ScriptException scriptException = new ScriptException(e.getMessage());
scriptException.setStackTrace(e.getStackTrace());
return new ScriptResult<>(scriptException);
} catch (Throwable t) {
String stack = Throwables.getStackTraceAsString(t);
if (t.getMessage() != null) {
stack = t.getMessage() + System.lineSeparator() + stack;
}
return new ScriptResult<>(new Exception(stack));
}
}
use of org.ow2.proactive_grid_cloud_portal.studio.Script in project scheduling by ow2-proactive.
the class SchedulingMethodImpl method schedule.
/**
* Scheduling process. For this implementation, steps are :<br>
* <ul>
* <li>Select running and pending jobs to be scheduled
* <li>Get an ordered list of the selected tasks to be scheduled
* <li>While returned tasks list is not empty :
* <ul>
* <li>Get n first compatible tasks (same selection script, same node exclusion)
* <li>Ask nodes to RM according to the previous specification
* <li>Try to start each tasks
* <li>Job started event if needed
* <li>Task started event
* </ul>
* <li>Manage exception while deploying tasks on nodes
* </ul>
*
* @return the number of tasks that have been started
*/
public int schedule() {
Policy currentPolicy = schedulingService.getPolicy();
// Number of time to retry an active object creation before leaving scheduling loop
activeObjectCreationRetryTimeNumber = ACTIVEOBJECT_CREATION_RETRY_TIME_NUMBER;
// get job Descriptor list with eligible jobs (running and pending)
Map<JobId, JobDescriptor> jobMap = schedulingService.lockJobsToSchedule();
Map<JobId, JobDescriptor> toUnlock = jobMap;
logSelectedJobs(jobMap);
// we wait for next scheduling loop and don't start any task
if (jobMap.isEmpty()) {
return 0;
}
return startTasks(currentPolicy, jobMap, toUnlock);
}
use of org.ow2.proactive_grid_cloud_portal.studio.Script in project scheduling by ow2-proactive.
the class SchedulingMethodImpl method getRMNodes.
/**
* Ask to the RM the given number of node resources.<br>
* If there is a problem with these task selection (such as bad selectionScript) this method
* will terminate the corresponding tasks and jobs. As the selection scripts contain errors, the task
* and its surrounding jobs must be stopped.
*
* @param neededResourcesNumber the number of resources to ask for (must be > 0).
* @param tasksToSchedule the task to be scheduled
* @return A nodeSet that contains at most 'neededResourcesNumber' available compatible resources.
* An empty nodeSet if no nodes could be found
* null if the their was an exception when asking for the nodes (ie : selection script has failed)
*/
protected NodeSet getRMNodes(Map<JobId, JobDescriptor> jobMap, int neededResourcesNumber, LinkedList<EligibleTaskDescriptor> tasksToSchedule, Set<String> freeResources) {
NodeSet nodeSet;
if (neededResourcesNumber <= 0) {
throw new IllegalArgumentException("'neededResourcesNumber' must be greater than 0");
}
EligibleTaskDescriptor etd = tasksToSchedule.getFirst();
InternalJob currentJob = ((JobDescriptorImpl) jobMap.get(etd.getJobId())).getInternal();
InternalTask internalTask0 = currentJob.getIHMTasks().get(etd.getTaskId());
try {
TopologyDescriptor descriptor = null;
boolean bestEffort = true;
if (internalTask0.isParallel()) {
descriptor = internalTask0.getParallelEnvironment().getTopologyDescriptor();
bestEffort = false;
if (descriptor == null) {
logger.debug("Topology is not defined for the task " + internalTask0.getName());
}
}
if (descriptor == null) {
// descriptor is not defined, use default
descriptor = TopologyDescriptor.ARBITRARY;
}
try {
Criteria criteria = new Criteria(neededResourcesNumber);
criteria.setTopology(descriptor);
// resolve script variables (if any) in the list of selection
// scripts and then set it as the selection criteria.
criteria.setScripts(resolveScriptVariables(internalTask0.getSelectionScripts(), internalTask0.getRuntimeVariables()));
criteria.setBlackList(internalTask0.getNodeExclusion());
criteria.setBestEffort(bestEffort);
criteria.setAcceptableNodesUrls(freeResources);
criteria.setBindings(createBindingsForSelectionScripts(currentJob, internalTask0));
if (internalTask0.getRuntimeGenericInformation().containsKey(SchedulerConstants.NODE_ACCESS_TOKEN)) {
criteria.setNodeAccessToken(internalTask0.getRuntimeGenericInformation().get(SchedulerConstants.NODE_ACCESS_TOKEN));
}
Collection<String> computationDescriptors = new ArrayList<>(tasksToSchedule.size());
for (EligibleTaskDescriptor task : tasksToSchedule) {
computationDescriptors.add(TaskLogger.getTaskLogRelativePath(task.getTaskId()));
}
criteria.setComputationDescriptors(computationDescriptors);
nodeSet = getRMProxiesManager().getUserRMProxy(currentJob.getOwner(), currentJob.getCredentials()).getNodes(criteria);
} catch (TopologyDisabledException tde) {
jlogger.warn(currentJob.getId(), "will be canceled as the topology is disabled");
schedulingService.simulateJobStartAndCancelIt(tasksToSchedule, "Topology is disabled");
return null;
}
// the following line is used to unwrap the future, warning when moving or removing
// it may also throw a ScriptException which is a RuntimeException
PAFuture.waitFor(nodeSet, true);
logger.debug("provided nodes " + nodeSet.size());
return nodeSet;
} catch (IOException | ClassNotFoundException e) {
logger.warn("Failed to deserialize previous task variables before selection for task " + internalTask0.getId().toString(), e);
schedulingService.simulateJobStartAndCancelIt(tasksToSchedule, "Failed to deserialize previous task variables before selection for task " + internalTask0.getId().toString());
return null;
} catch (RMProxyCreationException e) {
logger.warn("Failed to create User RM Proxy", e);
// simulate jobs starts and cancel it
schedulingService.simulateJobStartAndCancelIt(tasksToSchedule, "Failed to create User RM Proxy : Authentication Failed to Resource Manager for user '" + currentJob.getOwner() + "'");
// leave the method by ss failure
return null;
}
}
use of org.ow2.proactive_grid_cloud_portal.studio.Script in project scheduling by ow2-proactive.
the class SchedulingService method addThirdPartyCredentials.
/**
* Create a new Credential object containing users' 3rd Party Credentials.
*
* @param creds credentials for specific user
* @return in case of success new object containing the 3rd party credentials used to create bindings
* at clean script
*/
Credentials addThirdPartyCredentials(Credentials creds) throws KeyException, IllegalAccessException {
// retrieve scheduler key pair
String privateKeyPath = PASchedulerProperties.getAbsolutePath(PASchedulerProperties.SCHEDULER_AUTH_PRIVKEY_PATH.getValueAsString());
String publicKeyPath = PASchedulerProperties.getAbsolutePath(PASchedulerProperties.SCHEDULER_AUTH_PUBKEY_PATH.getValueAsString());
// get keys from task
PrivateKey privateKey = Credentials.getPrivateKey(privateKeyPath);
PublicKey publicKey = Credentials.getPublicKey(publicKeyPath);
// retrieve the current creData from task
CredData credData = creds.decrypt(privateKey);
// retrive database to get third party credentials from
SchedulerDBManager dbManager = getInfrastructure().getDBManager();
if (dbManager != null) {
Map<String, HybridEncryptedData> thirdPartyCredentials = dbManager.thirdPartyCredentialsMap(credData.getLogin());
if (thirdPartyCredentials == null) {
logger.error("Failed to retrieve Third Party Credentials!");
throw new KeyException("Failed to retrieve thirdPartyCredentials!");
} else {
// cycle third party credentials, add one-by-one to the decrypter
for (Map.Entry<String, HybridEncryptedData> thirdPartyCredential : thirdPartyCredentials.entrySet()) {
String decryptedValue = HybridEncryptionUtil.decryptString(thirdPartyCredential.getValue(), privateKey);
credData.addThirdPartyCredential(thirdPartyCredential.getKey(), decryptedValue);
}
}
}
return Credentials.createCredentials(credData, publicKey);
}
use of org.ow2.proactive_grid_cloud_portal.studio.Script in project scheduling by ow2-proactive.
the class SchedulerDBManager method toInternalTasks.
private Collection<InternalTask> toInternalTasks(boolean loadFullState, InternalJob internalJob, List<TaskData> taskRuntimeDataList) {
Map<DBTaskId, InternalTask> tasks = new HashMap<>(taskRuntimeDataList.size());
try {
for (TaskData taskData : taskRuntimeDataList) {
InternalTask internalTask = taskData.toInternalTask(internalJob, loadFullState);
if (loadFullState) {
internalTask.setParallelEnvironment(taskData.getParallelEnvironment());
internalTask.setGenericInformation(taskData.getGenericInformation());
for (SelectionScriptData scriptData : taskData.getSelectionScripts()) {
internalTask.addSelectionScript(scriptData.createSelectionScript());
}
if (taskData.getCleanScript() != null) {
internalTask.setCleaningScript(taskData.getCleanScript().createSimpleScript());
}
if (taskData.getPreScript() != null) {
internalTask.setPreScript(taskData.getPreScript().createSimpleScript());
}
if (taskData.getPostScript() != null) {
internalTask.setPostScript(taskData.getPostScript().createSimpleScript());
}
if (taskData.getFlowScript() != null) {
internalTask.setFlowScript(taskData.getFlowScript().createFlowScript());
}
for (SelectorData selectorData : taskData.getDataspaceSelectors()) {
if (selectorData.isInput()) {
InputSelector selector = selectorData.createInputSelector();
internalTask.addInputFiles(selector.getInputFiles(), selector.getMode());
} else {
OutputSelector selector = selectorData.createOutputSelector();
internalTask.addOutputFiles(selector.getOutputFiles(), selector.getMode());
}
}
}
tasks.put(taskData.getId(), internalTask);
}
} catch (InvalidScriptException e) {
throw new DatabaseManagerException("Failed to initialize loaded script", e);
}
for (TaskData taskData : taskRuntimeDataList) {
InternalTask internalTask = tasks.get(taskData.getId());
if (!taskData.getDependentTasks().isEmpty()) {
for (DBTaskId dependent : taskData.getDependentTasks()) {
internalTask.addDependence(tasks.get(dependent));
}
}
if (loadFullState) {
if (taskData.getIfBranch() != null) {
internalTask.setIfBranch(tasks.get(taskData.getIfBranch().getId()));
}
if (!taskData.getJoinedBranches().isEmpty()) {
List<InternalTask> branches = new ArrayList<>(taskData.getJoinedBranches().size());
for (DBTaskId joinedBranch : taskData.getJoinedBranches()) {
branches.add(tasks.get(joinedBranch));
}
internalTask.setJoinedBranches(branches);
}
internalTask.setName(internalTask.getName());
}
}
return tasks.values();
}
Aggregations