use of org.ow2.proactive.resourcemanager.task.client.RMNodeClient in project scheduling by ow2-proactive.
the class InProcessTaskExecutor method execute.
/**
* Executes a task inside a task context.
*
* @param taskContext Task context to execute.
* @param output Standard output sink.
* @param error Error sink.
* @return Returns the task result.
*/
@Override
public TaskResultImpl execute(TaskContext taskContext, PrintStream output, PrintStream error) {
ScriptHandler scriptHandler = ScriptLoader.createLocalHandler();
String nodesFile = null;
SchedulerNodeClient schedulerNodeClient = null;
RMNodeClient rmNodeClient = null;
RemoteSpace userSpaceClient = null;
RemoteSpace globalSpaceClient = null;
try {
nodesFile = writeNodesFile(taskContext);
VariablesMap variables = new VariablesMap();
variables.setInheritedMap(taskContextVariableExtractor.getAllNonTaskVariablesInjectNodesFile(taskContext, nodesFile));
variables.setScopeMap(taskContextVariableExtractor.getScopeVariables(taskContext));
Map<String, String> resultMetadata = new HashMap<>();
Map<String, Serializable> resultMap = new HashMap<>();
Map<String, String> thirdPartyCredentials = forkedTaskVariablesManager.extractThirdPartyCredentials(taskContext);
schedulerNodeClient = forkedTaskVariablesManager.createSchedulerNodeClient(taskContext);
rmNodeClient = forkedTaskVariablesManager.createRMNodeClient(taskContext);
userSpaceClient = forkedTaskVariablesManager.createDataSpaceNodeClient(taskContext, schedulerNodeClient, IDataSpaceClient.Dataspace.USER);
globalSpaceClient = forkedTaskVariablesManager.createDataSpaceNodeClient(taskContext, schedulerNodeClient, IDataSpaceClient.Dataspace.GLOBAL);
forkedTaskVariablesManager.addBindingsToScriptHandler(scriptHandler, taskContext, variables, resultMap, thirdPartyCredentials, schedulerNodeClient, rmNodeClient, userSpaceClient, globalSpaceClient, resultMetadata, output, error);
Stopwatch stopwatch = Stopwatch.createUnstarted();
TaskResultImpl taskResult;
try {
stopwatch.start();
Serializable result = execute(taskContext, output, error, scriptHandler, thirdPartyCredentials, variables);
stopwatch.stop();
taskResult = new TaskResultImpl(taskContext.getTaskId(), result, null, stopwatch.elapsed(TimeUnit.MILLISECONDS));
} catch (Throwable e) {
stopwatch.stop();
e.printStackTrace(error);
taskResult = new TaskResultImpl(taskContext.getTaskId(), e, null, stopwatch.elapsed(TimeUnit.MILLISECONDS));
}
executeFlowScript(taskContext.getControlFlowScript(), scriptHandler, output, error, taskResult);
taskResult.setPropagatedVariables(SerializationUtil.serializeVariableMap(variables.getPropagatedVariables()));
taskResult.setResultMap(resultMap);
taskResult.setMetadata(resultMetadata);
return taskResult;
} catch (Throwable e) {
e.printStackTrace(error);
TaskResultImpl result = new TaskResultImpl(taskContext.getTaskId(), e);
result.setPropagatedVariables(taskContextVariableExtractor.extractPropagatedVariables(taskContext));
return result;
} finally {
if (nodesFile != null && !nodesFile.isEmpty()) {
FileUtils.deleteQuietly(new File(nodesFile));
}
if (schedulerNodeClient != null && schedulerNodeClient.isConnected()) {
try {
schedulerNodeClient.disconnect();
} catch (Exception ignored) {
}
}
if (rmNodeClient != null && rmNodeClient.getSession() != null) {
try {
rmNodeClient.disconnect();
} catch (Exception ignored) {
}
}
}
}
use of org.ow2.proactive.resourcemanager.task.client.RMNodeClient in project scheduling by ow2-proactive.
the class ForkedTaskVariablesManagerTest method initializeForkedTaskVariableManager.
private Map<String, Object> initializeForkedTaskVariableManager(ScriptHandler scriptHandler, TaskContext taskContext, VariablesMap variables, Map<String, Serializable> resultMap, Map<String, String> credentials, Map<String, String> resultMetadata) throws IllegalAccessException, NoSuchFieldException {
// Create class
ForkedTaskVariablesManager forkedTaskVariablesManager = new ForkedTaskVariablesManager();
// Replace additionalBindings to hold a reference to it
Map<String, Object> scriptHandlerBindings = new HashMap<>();
setPrivateField(ScriptHandler.class.getDeclaredField("additionalBindings"), scriptHandler, scriptHandlerBindings);
SchedulerNodeClient schedulerNodeClient = forkedTaskVariablesManager.createSchedulerNodeClient(taskContext);
RMNodeClient rmNodeClient = forkedTaskVariablesManager.createRMNodeClient(taskContext);
// Execute method which adds bindings
forkedTaskVariablesManager.addBindingsToScriptHandler(scriptHandler, taskContext, variables, resultMap, credentials, schedulerNodeClient, rmNodeClient, forkedTaskVariablesManager.createDataSpaceNodeClient(taskContext, schedulerNodeClient, IDataSpaceClient.Dataspace.USER), forkedTaskVariablesManager.createDataSpaceNodeClient(taskContext, schedulerNodeClient, IDataSpaceClient.Dataspace.GLOBAL), resultMetadata, System.out, System.err);
return scriptHandlerBindings;
}
use of org.ow2.proactive.resourcemanager.task.client.RMNodeClient in project scheduling by ow2-proactive.
the class RMProxyActiveObject method handleCleaningScript.
/**
* Execute the given script on the given node.
* Also register a callback on {@link #cleanCallBack(Future, NodeSet)} method when script has returned.
* @param nodes the nodeset on which to start the script
* @param cleaningScript the script to be executed
* @param variables
* @param genericInformation
* @param taskId
* @param creds credentials with CredData containing third party credentials
*/
private void handleCleaningScript(NodeSet nodes, Script<?> cleaningScript, VariablesMap variables, Map<String, String> genericInformation, TaskId taskId, Credentials creds, Synchronization store, SignalApi signalAPI) {
TaskLogger instance = TaskLogger.getInstance();
try {
this.nodesTaskId.put(nodes, taskId);
// create a decrypter to access scheduler and retrieve Third Party User Credentials
String privateKeyPath = PASchedulerProperties.getAbsolutePath(PASchedulerProperties.SCHEDULER_AUTH_PRIVKEY_PATH.getValueAsString());
Decrypter decrypter = new Decrypter(Credentials.getPrivateKey(privateKeyPath));
decrypter.setCredentials(creds);
Node node = nodes.get(0);
String nodeUrl = node.getNodeInformation().getURL();
String nodeName = node.getNodeInformation().getName();
String hostName = node.getVMInformation().getHostName();
HashMap<String, Serializable> dictionary = new HashMap<>();
dictionary.putAll(variables.getScriptMap());
dictionary.putAll(variables.getInheritedMap());
dictionary.putAll(variables.getPropagatedVariables());
dictionary.putAll(variables.getScopeMap());
dictionary.put(SchedulerVars.PA_NODE_URL.toString(), nodeUrl);
dictionary.put(SchedulerVars.PA_NODE_NAME.toString(), nodeName);
dictionary.put(SchedulerVars.PA_NODE_HOST.toString(), hostName);
// start handler for binding
ScriptHandler handler = ScriptLoader.createHandler(node);
VariablesMap resolvedMap = new VariablesMap();
resolvedMap.setInheritedMap(VariableSubstitutor.resolveVariables(variables.getInheritedMap(), dictionary));
resolvedMap.setScopeMap(VariableSubstitutor.resolveVariables(variables.getScopeMap(), dictionary));
resolvedMap.put(SchedulerVars.PA_NODE_URL.toString(), nodeUrl);
resolvedMap.put(SchedulerVars.PA_NODE_NAME.toString(), nodeName);
resolvedMap.put(SchedulerVars.PA_NODE_HOST.toString(), hostName);
handler.addBinding(SchedulerConstants.VARIABLES_BINDING_NAME, (Serializable) resolvedMap);
handler.addBinding(SchedulerConstants.GENERIC_INFO_BINDING_NAME, (Serializable) genericInformation);
handler.addBinding(SchedulerConstants.SYNCHRONIZATION_API_BINDING_NAME, store);
handler.addBinding(SchedulerConstants.SIGNAL_API_BINDING_NAME, signalAPI);
// retrieve scheduler URL to bind with schedulerapi, globalspaceapi, and userspaceapi
String schedulerUrl = PASchedulerProperties.SCHEDULER_REST_URL.getValueAsString();
logger.debug("Binding schedulerapi...");
SchedulerNodeClient client = new SchedulerNodeClient(decrypter, schedulerUrl, taskId.getJobId(), Collections.emptyMap(), Collections.emptyMap());
handler.addBinding(SchedulerConstants.SCHEDULER_CLIENT_BINDING_NAME, client);
logger.debug("Binging rmapi...");
RMNodeClient rmNodeClient = new RMNodeClient(decrypter.decrypt(), schedulerUrl);
handler.addBinding(SchedulerConstants.RM_CLIENT_BINDING_NAME, rmNodeClient);
logger.debug("Binding globalspaceapi...");
RemoteSpace globalSpaceClient = new DataSpaceNodeClient(client, IDataSpaceClient.Dataspace.GLOBAL, schedulerUrl);
handler.addBinding(SchedulerConstants.DS_GLOBAL_API_BINDING_NAME, (Serializable) globalSpaceClient);
logger.debug("Binding userspaceapi...");
RemoteSpace userSpaceClient = new DataSpaceNodeClient(client, IDataSpaceClient.Dataspace.USER, schedulerUrl);
handler.addBinding(SchedulerConstants.DS_USER_API_BINDING_NAME, (Serializable) userSpaceClient);
logger.debug("Binding credentials...");
Map<String, String> resolvedThirdPartyCredentials = VariableSubstitutor.filterAndUpdate(decrypter.decrypt().getThirdPartyCredentials(), dictionary);
handler.addBinding(SchedulerConstants.CREDENTIALS_VARIABLE, (Serializable) resolvedThirdPartyCredentials);
ScriptResult<?> future = handler.handle(cleaningScript);
try {
PAEventProgramming.addActionOnFuture(future, "cleanCallBack", nodes);
} catch (IllegalArgumentException e) {
// TODO - linked to PROACTIVE-936 -> IllegalArgumentException is raised if method name is unknown
// should be replaced by checked exception
instance.error(taskId, "ERROR : Callback method won't be executed, node won't be released. This is a critical state, check the callback method name", e);
instance.close(taskId);
}
instance.info(taskId, "Cleaning Script started on node " + nodes.get(0).getNodeInformation().getURL());
} catch (Exception e) {
// if active object cannot be created or script has failed
instance.error(taskId, "Error while starting cleaning script for task " + taskId + " on " + nodes.get(0), e);
instance.close(taskId);
releaseNodes(nodes).booleanValue();
}
}
use of org.ow2.proactive.resourcemanager.task.client.RMNodeClient in project scheduling by ow2-proactive.
the class ForkEnvironmentScriptExecutor method executeForkEnvironmentScript.
/**
* Executes a fork environment script.
*
* @param context The task context to execute the script in.
* @param outputSink Standard output sink.
* @param errorSink Error output sink.
* @return Returns a ScriptResult.
* @throws Exception
*/
public ScriptResult executeForkEnvironmentScript(TaskContext context, PrintStream outputSink, PrintStream errorSink) throws Exception {
VariablesMap variables = new VariablesMap();
variables.setInheritedMap(taskContextVariableExtractor.getAllNonTaskVariables(context));
variables.setScopeMap(taskContextVariableExtractor.getScopeVariables(context));
Map<String, String> thirdPartyCredentials = forkedTaskVariablesManager.extractThirdPartyCredentials(context);
ScriptHandler scriptHandler = ScriptLoader.createLocalHandler();
Script<?> script = context.getInitializer().getForkEnvironment().getEnvScript();
SchedulerNodeClient schedulerNodeClient = forkedTaskVariablesManager.createSchedulerNodeClient(context);
RMNodeClient rmNodeClient = forkedTaskVariablesManager.createRMNodeClient(context);
RemoteSpace userSpaceClient = forkedTaskVariablesManager.createDataSpaceNodeClient(context, schedulerNodeClient, IDataSpaceClient.Dataspace.USER);
RemoteSpace globalSpaceClient = forkedTaskVariablesManager.createDataSpaceNodeClient(context, schedulerNodeClient, IDataSpaceClient.Dataspace.GLOBAL);
forkedTaskVariablesManager.addBindingsToScriptHandler(scriptHandler, context, variables, Collections.<String, Serializable>emptyMap(), thirdPartyCredentials, schedulerNodeClient, rmNodeClient, userSpaceClient, globalSpaceClient, Collections.<String, String>emptyMap(), outputSink, errorSink);
forkedTaskVariablesManager.replaceScriptParameters(script, thirdPartyCredentials, variables, errorSink);
ScriptResult scriptResult = scriptHandler.handle(script, outputSink, errorSink);
if (schedulerNodeClient != null && schedulerNodeClient.isConnected()) {
try {
schedulerNodeClient.disconnect();
} catch (Exception ignored) {
}
}
if (rmNodeClient != null && rmNodeClient.getSession() != null) {
try {
rmNodeClient.disconnect();
} catch (Exception ignored) {
}
}
if (scriptResult.errorOccured()) {
throw new Exception("Failed to execute fork environment script", scriptResult.getException());
}
return scriptResult;
}
Aggregations