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);
}
}
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);
}
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);
}
}
}
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;
}
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);
}
Aggregations