use of co.cask.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(basicWorkflowContext.getMetrics()));
if (!(workflow instanceof ProgramLifecycle)) {
return workflow;
}
final TransactionControl txControl = Transactions.getTransactionControl(TransactionControl.IMPLICIT, Workflow.class, workflow, "initialize", WorkflowContext.class);
basicWorkflowToken.setCurrentNode(workflowSpec.getName());
basicWorkflowContext.setState(new ProgramState(ProgramStatus.INITIALIZING, null));
basicWorkflowContext.initializeProgram((ProgramLifecycle) workflow, basicWorkflowContext, txControl, false);
runtimeStore.updateWorkflowToken(workflowRunId, basicWorkflowToken);
return workflow;
}
use of co.cask.cdap.api.ProgramState in project cdap by caskdata.
the class WorkflowDriver method executeAll.
private void executeAll(Iterator<WorkflowNode> iterator, ApplicationSpecification appSpec, InstantiatorFactory instantiator, ClassLoader classLoader, WorkflowToken token) {
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());
basicWorkflowContext.setState(new ProgramState(ProgramStatus.KILLED, rootCause.getMessage()));
break;
}
basicWorkflowContext.setState(new ProgramState(ProgramStatus.FAILED, rootCause.getMessage()));
throw Throwables.propagate(rootCause);
}
}
}
use of co.cask.cdap.api.ProgramState in project cdap by caskdata.
the class CustomActionExecutor method executeCustomAction.
private void executeCustomAction() throws Exception {
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(TransactionControl.IMPLICIT, AbstractCustomAction.class, customAction, "initialize") : Transactions.getTransactionControl(TransactionControl.IMPLICIT, CustomAction.class, customAction, "initialize", CustomActionContext.class);
customActionContext.initializeProgram(customAction, customActionContext, txControl, false);
customActionContext.setState(new ProgramState(ProgramStatus.RUNNING, null));
customActionContext.executeChecked(new AbstractContext.ThrowingRunnable() {
@Override
public void run() throws Exception {
customAction.run();
}
});
customActionContext.setState(new ProgramState(ProgramStatus.COMPLETED, null));
} catch (Throwable t) {
customActionContext.setState(new ProgramState(ProgramStatus.FAILED, Throwables.getRootCause(t).getMessage()));
Throwables.propagateIfPossible(t, Exception.class);
throw Throwables.propagate(t);
} finally {
TransactionControl txControl = Transactions.getTransactionControl(TransactionControl.IMPLICIT, CustomAction.class, customAction, "destroy");
customActionContext.destroyProgram(customAction, customActionContext, txControl, false);
}
}
use of co.cask.cdap.api.ProgramState in project cdap by caskdata.
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 {
// 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)
final TransactionControl txControl = spark instanceof AbstractSpark ? Transactions.getTransactionControl(TransactionControl.IMPLICIT, AbstractSpark.class, spark, "initialize") : spark instanceof ProgramLifecycle ? Transactions.getTransactionControl(TransactionControl.IMPLICIT, Spark.class, spark, "initialize", SparkClientContext.class) : TransactionControl.IMPLICIT;
TxRunnable runnable = new TxRunnable() {
@Override
public void run(DatasetContext ctxt) throws Exception {
Cancellable cancellable = SparkRuntimeUtils.setContextClassLoader(new SparkClassLoader(runtimeContext));
try {
context.setState(new ProgramState(ProgramStatus.INITIALIZING, null));
if (spark instanceof ProgramLifecycle) {
((ProgramLifecycle) spark).initialize(context);
} else {
spark.beforeSubmit(context);
}
} finally {
cancellable.cancel();
}
}
};
if (TransactionControl.IMPLICIT == txControl) {
context.execute(runnable);
} else {
runnable.run(context);
}
}
use of co.cask.cdap.api.ProgramState in project cdap by caskdata.
the class WorkflowDriver method run.
@Override
protected void run() throws Exception {
LOG.info("Starting workflow execution for '{}' with Run id '{}'", workflowSpec.getName(), workflowRunId.getRun());
LOG.trace("Workflow specification is {}", workflowSpec);
basicWorkflowContext.setState(new ProgramState(ProgramStatus.RUNNING, null));
executeAll(workflowSpec.getNodes().iterator(), program.getApplicationSpecification(), new InstantiatorFactory(false), program.getClassLoader(), basicWorkflowToken);
if (runningThread != null) {
basicWorkflowContext.setState(new ProgramState(ProgramStatus.COMPLETED, null));
}
LOG.info("Workflow '{}' with run id '{}' completed", workflowSpec.getName(), workflowRunId.getRun());
}
Aggregations