Search in sources :

Example 46 with Task

use of org.ow2.proactive.scheduler.common.task.Task in project scheduling by ow2-proactive.

the class DefaultModelJobValidatorServiceProviderTest method createJobWithTaskModelVariable.

private TaskFlowJob createJobWithTaskModelVariable(String value, String model) throws UserException {
    TaskFlowJob job = new TaskFlowJob();
    TaskVariable variable = new TaskVariable("VAR", value, model, false);
    Task task = new ScriptTask();
    task.setName("ModelTask");
    task.setVariables(Collections.singletonMap(variable.getName(), variable));
    job.addTask(task);
    return job;
}
Also used : Task(org.ow2.proactive.scheduler.common.task.Task) ScriptTask(org.ow2.proactive.scheduler.common.task.ScriptTask) ScriptTask(org.ow2.proactive.scheduler.common.task.ScriptTask) TaskFlowJob(org.ow2.proactive.scheduler.common.job.TaskFlowJob) TaskVariable(org.ow2.proactive.scheduler.common.task.TaskVariable)

Example 47 with Task

use of org.ow2.proactive.scheduler.common.task.Task in project scheduling by ow2-proactive.

the class TaskLauncher method doTask.

public void doTask(ExecutableContainer executableContainer, TaskResult[] previousTasksResults, TaskTerminateNotification terminateNotification, String terminateNotificationNodeURL, boolean taskRecoverable) {
    TaskResultImpl taskResult;
    WallTimer wallTimer = null;
    TaskContext context = null;
    Stopwatch taskStopwatchForFailures = null;
    TaskDataspaces dataspaces = null;
    try {
        taskStarted.set(true);
        logger.info("Task started " + taskId.getJobId().getReadableName() + " : " + taskId.getReadableName());
        this.taskKiller = this.replaceTaskKillerWithDoubleTimeoutValueIfRunAsMe(executableContainer.isRunAsUser());
        wallTimer = new WallTimer(initializer.getWalltime(), taskKiller);
        taskStopwatchForFailures = Stopwatch.createUnstarted();
        taskLauncherRebinder = new TaskLauncherRebinder(taskId, terminateNotificationNodeURL, taskRecoverable);
        addShutdownHook();
        // lock the cache space cleaning mechanism
        DataSpaceNodeConfigurationAgent.lockCacheSpaceCleaning();
        dataspaces = factory.createTaskDataspaces(taskId, initializer.getNamingService(), executableContainer.isRunAsUser());
        File taskLogFile = taskLogger.createFileAppender(dataspaces.getScratchFolder());
        progressFileReader.start(dataspaces.getScratchFolder(), taskId);
        context = new TaskContext(executableContainer, initializer, previousTasksResults, new NodeDataSpacesURIs(dataspaces.getScratchURI(), dataspaces.getCacheURI(), dataspaces.getInputURI(), dataspaces.getOutputURI(), dataspaces.getUserURI(), dataspaces.getGlobalURI()), progressFileReader.getProgressFile().toString(), getHostname(), decrypter);
        File workingDir = getTaskWorkingDir(context, dataspaces);
        logger.info("Task working dir: " + workingDir);
        logger.info("Cache space: " + context.getNodeDataSpaceURIs().getCacheURI());
        logger.info("Input space: " + context.getNodeDataSpaceURIs().getInputURI());
        logger.info("Output space: " + context.getNodeDataSpaceURIs().getOutputURI());
        logger.info("User space: " + context.getNodeDataSpaceURIs().getUserURI());
        logger.info("Global space: " + context.getNodeDataSpaceURIs().getGlobalURI());
        logger.info("Scheduler rest url: " + context.getSchedulerRestUrl());
        wallTimer.start();
        // should handle interrupt
        dataspaces.copyInputDataToScratch(initializer.getFilteredInputFiles(fileSelectorsFilters(context)));
        if (decrypter != null) {
            decrypter.setCredentials(executableContainer.getCredentials());
        }
        TaskExecutor taskExecutor = factory.createTaskExecutor(workingDir);
        taskStopwatchForFailures.start();
        taskResult = taskExecutor.execute(context, taskLogger.getOutputSink(), taskLogger.getErrorSink());
        taskStopwatchForFailures.stop();
        // by the time the task finishes, the scheduler might have had a
        // transient failure, so we need to make sure that the placeholder
        // for the task's result still exists, or get the new place for
        // the result if it does not exist anymore.
        TaskTerminateNotification rebindedTerminateNotification = taskLauncherRebinder.makeSureSchedulerIsConnected(terminateNotification);
        switch(taskKiller.getStatus()) {
            case WALLTIME_REACHED:
                taskResult = getWalltimedTaskResult(context, taskStopwatchForFailures);
                sendResultToScheduler(rebindedTerminateNotification, taskResult);
                return;
            case KILLED_MANUALLY:
                // killed by Scheduler, no need to send results back
                return;
        }
        dataspaces.copyScratchDataToOutput(initializer.getFilteredOutputFiles(fileSelectorsFilters(context, taskResult)));
        wallTimer.stop();
        copyTaskLogsToUserSpace(taskLogFile, dataspaces);
        taskResult.setLogs(taskLogger.getLogs());
        sendResultToScheduler(rebindedTerminateNotification, taskResult);
    } catch (Throwable taskFailure) {
        if (wallTimer != null) {
            wallTimer.stop();
        }
        switch(taskKiller.getStatus()) {
            case WALLTIME_REACHED:
                taskResult = getWalltimedTaskResult(context, taskStopwatchForFailures);
                sendResultToScheduler(terminateNotification, taskResult);
                break;
            case KILLED_MANUALLY:
                // killed by Scheduler, no need to send results back
                return;
            default:
                logger.info("Failed to execute task", taskFailure);
                long elapsedTime = 0;
                if (taskStopwatchForFailures != null) {
                    elapsedTime = taskStopwatchForFailures.elapsed(TimeUnit.MILLISECONDS);
                }
                taskFailure.printStackTrace(taskLogger.getErrorSink());
                Map<String, byte[]> serializedVariables = extractVariablesFromContext(context);
                taskResult = new TaskResultImpl(taskId, taskFailure, taskLogger.getLogs(), elapsedTime);
                taskResult.setPropagatedVariables(serializedVariables);
                sendResultToScheduler(terminateNotification, taskResult);
        }
    } finally {
        try {
            progressFileReader.stop();
            taskLogger.close();
            if (dataspaces != null) {
                dataspaces.close();
            }
            // unlocks the cache space cleaning thread
            DataSpaceNodeConfigurationAgent.unlockCacheSpaceCleaning();
            removeShutdownHook();
        } finally {
            terminate();
        }
    }
}
Also used : TaskTerminateNotification(org.ow2.proactive.scheduler.common.TaskTerminateNotification) TaskExecutor(org.ow2.proactive.scheduler.task.executors.TaskExecutor) TaskContext(org.ow2.proactive.scheduler.task.context.TaskContext) TaskDataspaces(org.ow2.proactive.scheduler.task.data.TaskDataspaces) Stopwatch(com.google.common.base.Stopwatch) WallTimer(org.ow2.proactive.scheduler.task.utils.WallTimer) NodeDataSpacesURIs(org.ow2.proactive.scheduler.task.context.NodeDataSpacesURIs) File(java.io.File) Map(java.util.Map)

Example 48 with Task

use of org.ow2.proactive.scheduler.common.task.Task in project scheduling by ow2-proactive.

the class TaskLauncher method sendResultToScheduler.

private void sendResultToScheduler(TaskTerminateNotification terminateNotification, TaskResultImpl taskResult) {
    if (isNodeShuttingDown()) {
        return;
    }
    int pingAttempts = initializer.getPingAttempts();
    int pingPeriodMs = initializer.getPingPeriod() * 1000;
    // We are going to contact the recipient of the task result. This
    // recipient is the TaskTerminateNotification, an active object on the
    // scheduler side. If the scheduler experienced a transient failure
    // while the task was computing, then the reference to this
    // TaskTerminateNotification is obsolete and we need to update it. This
    // is what the following code does.
    TaskTerminateNotification currentTerminateNotification = terminateNotification;
    for (int i = 0; i < pingAttempts; i++) {
        try {
            currentTerminateNotification.terminate(taskId, taskResult);
            logger.debug("Successfully notified task termination " + taskId);
            // termination has succeeded, exit the method
            return;
        } catch (Throwable t) {
            logger.warn("Cannot notify task termination, trying to rebind to the task termination handler");
            TaskTerminateNotification rebindedTerminateNotification = taskLauncherRebinder.getReboundTaskTerminateNotificationHandler(t);
            if (rebindedTerminateNotification != null) {
                currentTerminateNotification = rebindedTerminateNotification;
            } else {
                decreasePingAttemptsAndWait(pingAttempts, pingPeriodMs, i, t);
            }
        }
    }
    logger.error("Cannot notify task termination " + taskId + " after " + pingAttempts + " attempts, terminating task launcher now");
}
Also used : TaskTerminateNotification(org.ow2.proactive.scheduler.common.TaskTerminateNotification)

Example 49 with Task

use of org.ow2.proactive.scheduler.common.task.Task in project scheduling by ow2-proactive.

the class TaskLogger method activateLogs.

public void activateLogs(AppenderProvider logSink) {
    logger.info("Activating logs for task " + this.taskId + " (" + taskId.getReadableName() + ")");
    if (this.loggersActivated.get()) {
        logger.info("Logs for task " + this.taskId + " are already activated");
        return;
    }
    this.loggersActivated.set(true);
    // create appender
    Appender appender;
    try {
        appender = logSink.getAppender();
    } catch (LogForwardingException e) {
        logger.error("Cannot create log appender.", e);
        return;
    }
    // fill appender
    if (!this.loggersFinalized.get()) {
        taskLogAppender.addAppender(appender);
    } else {
        logger.info("Logs for task " + this.taskId + " are closed. Flushing buffer...");
        // Everything is closed: reopen and close...
        for (LoggingEvent e : taskLogAppender.getStorage()) {
            appender.doAppend(e);
        }
        appender.close();
        this.loggersActivated.set(false);
        return;
    }
    logger.info("Activated logs for task " + this.taskId);
}
Also used : Appender(org.apache.log4j.Appender) FileAppender(org.apache.log4j.FileAppender) LoggingEvent(org.apache.log4j.spi.LoggingEvent) LogForwardingException(org.ow2.proactive.scheduler.common.util.logforwarder.LogForwardingException)

Example 50 with Task

use of org.ow2.proactive.scheduler.common.task.Task in project scheduling by ow2-proactive.

the class ForkedTaskExecutor method execute.

@Override
public TaskResultImpl execute(TaskContext context, PrintStream outputSink, PrintStream errorSink) {
    CookieBasedProcessTreeKiller taskProcessTreeKiller = null;
    Process process = null;
    ProcessStreamsReader processStreamsReader = null;
    File serializedContext = null;
    try {
        if (!workingDir.exists()) {
            FileUtils.forceMkdir(workingDir);
        }
        serializedContext = taskContextSerializer.serializeContext(context, workingDir);
        OSProcessBuilder processBuilder = forkedJvmProcessBuilderCreator.createForkedProcessBuilder(context, serializedContext, outputSink, errorSink, workingDir);
        TaskId taskId = context.getTaskId();
        String cookieNameSuffix = "Job" + taskId.getJobId().value() + "Task" + taskId.value();
        taskProcessTreeKiller = CookieBasedProcessTreeKiller.createProcessChildrenKiller(cookieNameSuffix, processBuilder.environment());
        process = processBuilder.start();
        processStreamsReader = new ProcessStreamsReader(taskId.toString(), process, outputSink, errorSink);
        int exitCode = process.waitFor();
        if (exitCode != 0) {
            try {
                Object error = deserializeTaskResult(serializedContext);
                if (error instanceof TaskContext) {
                    return createTaskResult(context, new IOException("Forked JVM process returned with exit code " + exitCode + ", see task logs for more information"));
                } else {
                    Throwable exception = (Throwable) error;
                    return createTaskResult(context, exception);
                }
            } catch (Throwable cannotDeserializeResult) {
                return createTaskResult(context, cannotDeserializeResult);
            }
        }
        return (TaskResultImpl) deserializeTaskResult(serializedContext);
    } catch (Throwable throwable) {
        return createTaskResult(context, throwable);
    } finally {
        FileUtils.deleteQuietly(serializedContext);
        if (process != null) {
            process.destroy();
        }
        if (taskProcessTreeKiller != null) {
            taskProcessTreeKiller.kill();
        }
        if (processStreamsReader != null) {
            processStreamsReader.close();
        }
    }
}
Also used : TaskId(org.ow2.proactive.scheduler.common.task.TaskId) TaskContext(org.ow2.proactive.scheduler.task.context.TaskContext) TaskResultImpl(org.ow2.proactive.scheduler.task.TaskResultImpl) IOException(java.io.IOException) OSProcessBuilder(org.objectweb.proactive.extensions.processbuilder.OSProcessBuilder) ProcessStreamsReader(org.ow2.proactive.scheduler.task.utils.ProcessStreamsReader) CookieBasedProcessTreeKiller(org.ow2.proactive.utils.CookieBasedProcessTreeKiller) File(java.io.File)

Aggregations

Test (org.junit.Test)152 InternalTask (org.ow2.proactive.scheduler.task.internal.InternalTask)112 TaskFlowJob (org.ow2.proactive.scheduler.common.job.TaskFlowJob)100 JobId (org.ow2.proactive.scheduler.common.job.JobId)73 InternalJob (org.ow2.proactive.scheduler.job.InternalJob)69 JavaTask (org.ow2.proactive.scheduler.common.task.JavaTask)68 TaskResult (org.ow2.proactive.scheduler.common.task.TaskResult)60 ArrayList (java.util.ArrayList)49 TaskId (org.ow2.proactive.scheduler.common.task.TaskId)48 TaskResultImpl (org.ow2.proactive.scheduler.task.TaskResultImpl)43 Task (org.ow2.proactive.scheduler.common.task.Task)41 JobIdImpl (org.ow2.proactive.scheduler.job.JobIdImpl)41 File (java.io.File)37 Scheduler (org.ow2.proactive.scheduler.common.Scheduler)34 HashMap (java.util.HashMap)33 UnknownJobException (org.ow2.proactive.scheduler.common.exception.UnknownJobException)33 SimpleScript (org.ow2.proactive.scripting.SimpleScript)33 InternalScriptTask (org.ow2.proactive.scheduler.task.internal.InternalScriptTask)27 TaskScript (org.ow2.proactive.scripting.TaskScript)27 UnknownTaskException (org.ow2.proactive.scheduler.common.exception.UnknownTaskException)26