Search in sources :

Example 6 with ProgramState

use of io.cdap.cdap.api.ProgramState in project cdap by cdapio.

the class WorkflowDriver method executeAll.

private void executeAll(Iterator<WorkflowNode> iterator, ApplicationSpecification appSpec, InstantiatorFactory instantiator, ClassLoader classLoader, WorkflowToken token) throws Exception {
    while (iterator.hasNext() && runningThread != null) {
        try {
            blockIfSuspended();
            WorkflowNode node = iterator.next();
            executeNode(appSpec, node, instantiator, classLoader, token);
        } catch (Throwable t) {
            Throwable rootCause = Throwables.getRootCause(t);
            if (rootCause instanceof InterruptedException) {
                LOG.debug("Workflow '{}' with run id '{}' aborted", workflowSpec.getName(), workflowRunId.getRun());
                workflowContext.setState(new ProgramState(ProgramStatus.KILLED, rootCause.getMessage()));
                break;
            }
            workflowContext.setState(new ProgramState(ProgramStatus.FAILED, Exceptions.condenseThrowableMessage(t)));
            throw t;
        }
    }
}
Also used : BasicThrowable(io.cdap.cdap.proto.BasicThrowable) ProgramState(io.cdap.cdap.api.ProgramState) WorkflowNode(io.cdap.cdap.api.workflow.WorkflowNode)

Example 7 with ProgramState

use of io.cdap.cdap.api.ProgramState in project cdap by cdapio.

the class SparkRuntimeService method initialize.

/**
 * Calls the {@link Spark#beforeSubmit(SparkClientContext)} for the pre 3.5 Spark programs, calls
 * the {@link ProgramLifecycle#initialize} otherwise.
 */
@SuppressWarnings("unchecked")
private void initialize() throws Exception {
    context.setState(new ProgramState(ProgramStatus.INITIALIZING, null));
    // AbstractSpark implements final initialize(context) and requires subclass to
    // implement initialize(), whereas programs that directly implement Spark have
    // the option to override initialize(context) (if they implement ProgramLifeCycle)
    TransactionControl defaultTxControl = runtimeContext.getDefaultTxControl();
    final TransactionControl txControl = spark instanceof AbstractSpark ? Transactions.getTransactionControl(defaultTxControl, AbstractSpark.class, spark, "initialize") : spark instanceof ProgramLifecycle ? Transactions.getTransactionControl(defaultTxControl, Spark.class, spark, "initialize", SparkClientContext.class) : defaultTxControl;
    runtimeContext.initializeProgram(programLifecycle, txControl, false);
}
Also used : ProgramLifecycle(io.cdap.cdap.api.ProgramLifecycle) TransactionControl(io.cdap.cdap.api.annotation.TransactionControl) ProgramState(io.cdap.cdap.api.ProgramState) AbstractSpark(io.cdap.cdap.api.spark.AbstractSpark)

Example 8 with ProgramState

use of io.cdap.cdap.api.ProgramState in project cdap by caskdata.

the class WorkflowDriver method initializeWorkflow.

@SuppressWarnings("unchecked")
private Workflow initializeWorkflow() throws Exception {
    Class<?> clz = Class.forName(workflowSpec.getClassName(), true, program.getClassLoader());
    if (!Workflow.class.isAssignableFrom(clz)) {
        throw new IllegalStateException(String.format("%s is not Workflow.", clz));
    }
    Class<? extends Workflow> workflowClass = (Class<? extends Workflow>) clz;
    final Workflow workflow = new InstantiatorFactory(false).get(TypeToken.of(workflowClass)).create();
    // set metrics
    Reflections.visit(workflow, workflow.getClass(), new MetricsFieldSetter(workflowContext.getMetrics()));
    if (!(workflow instanceof ProgramLifecycle)) {
        return workflow;
    }
    final TransactionControl txControl = Transactions.getTransactionControl(workflowContext.getDefaultTxControl(), Workflow.class, workflow, "initialize", WorkflowContext.class);
    basicWorkflowToken.setCurrentNode(workflowSpec.getName());
    workflowContext.setState(new ProgramState(ProgramStatus.INITIALIZING, null));
    workflowContext.initializeProgram((ProgramLifecycle) workflow, txControl, false);
    workflowStateWriter.setWorkflowToken(workflowRunId, basicWorkflowToken);
    return workflow;
}
Also used : InstantiatorFactory(io.cdap.cdap.common.lang.InstantiatorFactory) MetricsFieldSetter(io.cdap.cdap.internal.app.runtime.MetricsFieldSetter) ProgramLifecycle(io.cdap.cdap.api.ProgramLifecycle) TransactionControl(io.cdap.cdap.api.annotation.TransactionControl) Workflow(io.cdap.cdap.api.workflow.Workflow) ProgramState(io.cdap.cdap.api.ProgramState)

Example 9 with ProgramState

use of io.cdap.cdap.api.ProgramState in project cdap by caskdata.

the class CustomActionExecutor method execute.

void execute() throws Exception {
    TransactionControl defaultTxControl = customActionContext.getDefaultTxControl();
    try {
        customActionContext.setState(new ProgramState(ProgramStatus.INITIALIZING, null));
        // AbstractCustomAction implements final initialize(context) and requires subclass to
        // implement initialize(), whereas programs that directly implement CustomAction can
        // override initialize(context)
        TransactionControl txControl = customAction instanceof AbstractCustomAction ? Transactions.getTransactionControl(defaultTxControl, AbstractCustomAction.class, customAction, "initialize") : Transactions.getTransactionControl(defaultTxControl, CustomAction.class, customAction, "initialize", CustomActionContext.class);
        customActionContext.initializeProgram(customAction, txControl, false);
        customActionContext.setState(new ProgramState(ProgramStatus.RUNNING, null));
        customActionContext.execute(customAction::run);
        customActionContext.setState(new ProgramState(ProgramStatus.COMPLETED, null));
    } catch (Throwable t) {
        customActionContext.setState(new ProgramState(ProgramStatus.FAILED, Exceptions.condenseThrowableMessage(t)));
        Throwables.propagateIfPossible(t, Exception.class);
        throw Throwables.propagate(t);
    } finally {
        TransactionControl txControl = Transactions.getTransactionControl(defaultTxControl, CustomAction.class, customAction, "destroy");
        customActionContext.destroyProgram(customAction, txControl, false);
    }
}
Also used : AbstractCustomAction(io.cdap.cdap.api.customaction.AbstractCustomAction) CustomAction(io.cdap.cdap.api.customaction.CustomAction) TransactionControl(io.cdap.cdap.api.annotation.TransactionControl) AbstractCustomAction(io.cdap.cdap.api.customaction.AbstractCustomAction) CustomActionContext(io.cdap.cdap.api.customaction.CustomActionContext) BasicCustomActionContext(io.cdap.cdap.internal.app.runtime.customaction.BasicCustomActionContext) ProgramState(io.cdap.cdap.api.ProgramState)

Example 10 with ProgramState

use of io.cdap.cdap.api.ProgramState in project cdap by caskdata.

the class MapReduceRuntimeService method beforeSubmit.

/**
 * For MapReduce programs created after 3.5, calls the initialize method of the {@link ProgramLifecycle}.
 * This method also sets up the Input/Output within the same transaction.
 */
private void beforeSubmit(final Job job) throws Exception {
    context.setState(new ProgramState(ProgramStatus.INITIALIZING, null));
    // AbstractMapReduce implements final initialize(context) and requires subclass to
    // implement initialize(), whereas programs that directly implement MapReduce have
    // the option to override initialize(context) (if they implement ProgramLifeCycle)
    TransactionControl defaultTxControl = context.getDefaultTxControl();
    TransactionControl txControl = mapReduce instanceof AbstractMapReduce ? Transactions.getTransactionControl(defaultTxControl, AbstractMapReduce.class, mapReduce, "initialize") : mapReduce instanceof ProgramLifecycle ? Transactions.getTransactionControl(defaultTxControl, MapReduce.class, mapReduce, "initialize", MapReduceContext.class) : defaultTxControl;
    context.initializeProgram(programLifecycle, txControl, false);
    // once the initialize method is executed, set the status of the MapReduce to RUNNING
    context.setState(new ProgramState(ProgramStatus.RUNNING, null));
    ClassLoader oldClassLoader = ClassLoaders.setContextClassLoader(context.getProgramInvocationClassLoader());
    try {
        // set input/outputs info, and get one of the configured mapper's TypeToken
        TypeToken<?> mapperTypeToken = setInputsIfNeeded(job);
        setOutputsIfNeeded(job);
        setOutputClassesIfNeeded(job, mapperTypeToken);
        setMapOutputClassesIfNeeded(job, mapperTypeToken);
    } finally {
        ClassLoaders.setContextClassLoader(oldClassLoader);
    }
}
Also used : ProgramLifecycle(io.cdap.cdap.api.ProgramLifecycle) AbstractMapReduce(io.cdap.cdap.api.mapreduce.AbstractMapReduce) TransactionControl(io.cdap.cdap.api.annotation.TransactionControl) ProgramState(io.cdap.cdap.api.ProgramState)

Aggregations

ProgramState (io.cdap.cdap.api.ProgramState)14 TransactionControl (io.cdap.cdap.api.annotation.TransactionControl)8 ProgramLifecycle (io.cdap.cdap.api.ProgramLifecycle)6 InstantiatorFactory (io.cdap.cdap.common.lang.InstantiatorFactory)4 AbstractCustomAction (io.cdap.cdap.api.customaction.AbstractCustomAction)2 CustomAction (io.cdap.cdap.api.customaction.CustomAction)2 CustomActionContext (io.cdap.cdap.api.customaction.CustomActionContext)2 AbstractMapReduce (io.cdap.cdap.api.mapreduce.AbstractMapReduce)2 AbstractSpark (io.cdap.cdap.api.spark.AbstractSpark)2 Workflow (io.cdap.cdap.api.workflow.Workflow)2 WorkflowNode (io.cdap.cdap.api.workflow.WorkflowNode)2 MetricsFieldSetter (io.cdap.cdap.internal.app.runtime.MetricsFieldSetter)2 BasicCustomActionContext (io.cdap.cdap.internal.app.runtime.customaction.BasicCustomActionContext)2 BasicThrowable (io.cdap.cdap.proto.BasicThrowable)2 ProgramRunId (io.cdap.cdap.proto.id.ProgramRunId)2 IOException (java.io.IOException)2 URISyntaxException (java.net.URISyntaxException)2 RunId (org.apache.twill.api.RunId)2