Search in sources :

Example 6 with ProgramLifecycle

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

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)

Example 7 with ProgramLifecycle

use of io.cdap.cdap.api.ProgramLifecycle 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 ProgramLifecycle

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

the class SparkRuntimeService method destroy.

/**
 * Calls the destroy or onFinish method of {@link ProgramLifecycle}.
 */
private void destroy(final ProgramState state) {
    context.setState(state);
    TransactionControl defaultTxControl = runtimeContext.getDefaultTxControl();
    TransactionControl txControl = spark instanceof ProgramLifecycle ? Transactions.getTransactionControl(defaultTxControl, Spark.class, spark, "destroy") : defaultTxControl;
    runtimeContext.destroyProgram(programLifecycle, txControl, false);
    if (emitFieldLineage()) {
        try {
            // here we cannot call context.flushRecord() since the WorkflowNodeState will need to record and store
            // the lineage information
            FieldLineageInfo info = new FieldLineageInfo(runtimeContext.getFieldLineageOperations());
            fieldLineageWriter.write(runtimeContext.getProgramRunId(), info);
        } catch (Throwable t) {
            LOG.warn("Failed to emit the field lineage operations for Spark {}", runtimeContext.getProgramRunId(), t);
        }
    }
}
Also used : ProgramLifecycle(io.cdap.cdap.api.ProgramLifecycle) TransactionControl(io.cdap.cdap.api.annotation.TransactionControl) Spark(io.cdap.cdap.api.spark.Spark) AbstractSpark(io.cdap.cdap.api.spark.AbstractSpark) FieldLineageInfo(io.cdap.cdap.data2.metadata.lineage.field.FieldLineageInfo)

Example 9 with ProgramLifecycle

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

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 10 with ProgramLifecycle

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

the class WorkflowDriver method destroyWorkflow.

@SuppressWarnings("unchecked")
private void destroyWorkflow() {
    if (!(workflow instanceof ProgramLifecycle)) {
        return;
    }
    final TransactionControl txControl = Transactions.getTransactionControl(workflowContext.getDefaultTxControl(), Workflow.class, workflow, "destroy");
    basicWorkflowToken.setCurrentNode(workflowSpec.getName());
    workflowContext.destroyProgram((ProgramLifecycle) workflow, txControl, false);
    try {
        workflowStateWriter.setWorkflowToken(workflowRunId, basicWorkflowToken);
    } catch (Throwable t) {
        LOG.error("Failed to store the final workflow token of Workflow {}", workflowRunId, t);
    }
    if (ProgramStatus.COMPLETED != workflowContext.getState().getStatus()) {
        return;
    }
    writeFieldLineage(workflowContext);
}
Also used : ProgramLifecycle(io.cdap.cdap.api.ProgramLifecycle) TransactionControl(io.cdap.cdap.api.annotation.TransactionControl) BasicThrowable(io.cdap.cdap.proto.BasicThrowable)

Aggregations

ProgramLifecycle (io.cdap.cdap.api.ProgramLifecycle)20 TransactionControl (io.cdap.cdap.api.annotation.TransactionControl)12 ProgramState (io.cdap.cdap.api.ProgramState)6 MetricsFieldSetter (io.cdap.cdap.internal.app.runtime.MetricsFieldSetter)6 RuntimeContext (io.cdap.cdap.api.RuntimeContext)4 AbstractMapReduce (io.cdap.cdap.api.mapreduce.AbstractMapReduce)4 AbstractSpark (io.cdap.cdap.api.spark.AbstractSpark)4 PropertyFieldSetter (io.cdap.cdap.common.lang.PropertyFieldSetter)4 WeakReferenceDelegatorClassLoader (io.cdap.cdap.common.lang.WeakReferenceDelegatorClassLoader)4 FieldLineageInfo (io.cdap.cdap.data2.metadata.lineage.field.FieldLineageInfo)4 DataSetFieldSetter (io.cdap.cdap.internal.app.runtime.DataSetFieldSetter)4 IOException (java.io.IOException)4 MapReduce (io.cdap.cdap.api.mapreduce.MapReduce)2 Spark (io.cdap.cdap.api.spark.Spark)2 Workflow (io.cdap.cdap.api.workflow.Workflow)2 InstantiatorFactory (io.cdap.cdap.common.lang.InstantiatorFactory)2 MultiInputTaggedSplit (io.cdap.cdap.internal.app.runtime.batch.dataset.input.MultiInputTaggedSplit)2 TaggedInputSplit (io.cdap.cdap.internal.app.runtime.batch.dataset.input.TaggedInputSplit)2 BasicThrowable (io.cdap.cdap.proto.BasicThrowable)2 InputSplit (org.apache.hadoop.mapreduce.InputSplit)2