use of io.cdap.cdap.api.annotation.TransactionControl in project cdap by caskdata.
the class WorkflowDriver method executeCondition.
@SuppressWarnings("unchecked")
private void executeCondition(ApplicationSpecification appSpec, final WorkflowConditionNode node, InstantiatorFactory instantiator, ClassLoader classLoader, WorkflowToken token) throws Exception {
final BasicWorkflowContext context = new BasicWorkflowContext(workflowSpec, token, program, programOptions, cConf, metricsCollectionService, datasetFramework, txClient, discoveryServiceClient, nodeStates, pluginInstantiator, secureStore, secureStoreManager, messagingService, node.getConditionSpecification(), metadataReader, metadataPublisher, namespaceQueryAdmin, fieldLineageWriter, remoteClientFactory);
final Iterator<WorkflowNode> iterator;
Class<?> clz = classLoader.loadClass(node.getPredicateClassName());
Predicate<WorkflowContext> predicate = instantiator.get(TypeToken.of((Class<? extends Predicate<WorkflowContext>>) clz)).create();
if (!(predicate instanceof Condition)) {
iterator = predicate.apply(context) ? node.getIfBranch().iterator() : node.getElseBranch().iterator();
} else {
final Condition workflowCondition = (Condition) predicate;
Reflections.visit(workflowCondition, workflowCondition.getClass(), new PropertyFieldSetter(node.getConditionSpecification().getProperties()), new DataSetFieldSetter(context), new MetricsFieldSetter(context.getMetrics()));
TransactionControl defaultTxControl = workflowContext.getDefaultTxControl();
try {
// AbstractCondition implements final initialize(context) and requires subclass to
// implement initialize(), whereas conditions that directly implement Condition can
// override initialize(context)
TransactionControl txControl = workflowCondition instanceof AbstractCondition ? Transactions.getTransactionControl(defaultTxControl, AbstractCondition.class, workflowCondition, "initialize") : Transactions.getTransactionControl(defaultTxControl, Condition.class, workflowCondition, "initialize", WorkflowContext.class);
context.initializeProgram(workflowCondition, txControl, false);
boolean result = context.execute(() -> workflowCondition.apply(context));
iterator = result ? node.getIfBranch().iterator() : node.getElseBranch().iterator();
} finally {
TransactionControl txControl = Transactions.getTransactionControl(defaultTxControl, Condition.class, workflowCondition, "destroy");
context.destroyProgram(workflowCondition, txControl, false);
}
}
// If a workflow updates its token at a condition node, it will be persisted after the execution of the next node.
// However, the call below ensures that even if the workflow fails/crashes after a condition node, updates from the
// condition node are also persisted.
workflowStateWriter.setWorkflowToken(workflowRunId, token);
executeAll(iterator, appSpec, instantiator, classLoader, token);
}
use of io.cdap.cdap.api.annotation.TransactionControl in project cdap by caskdata.
the class WorkerDriver method shutDown.
@Override
protected void shutDown() throws Exception {
if (worker == null) {
return;
}
// Worker is always using Explicit transaction
TransactionControl txControl = Transactions.getTransactionControl(TransactionControl.EXPLICIT, Worker.class, worker, "destroy");
context.destroyProgram(worker, txControl, false);
context.close();
}
use of io.cdap.cdap.api.annotation.TransactionControl in project cdap by caskdata.
the class MapReduceRuntimeService method destroy.
/**
* Calls the destroy method of {@link ProgramLifecycle}.
*/
private void destroy() {
TransactionControl defaultTxControl = context.getDefaultTxControl();
TransactionControl txControl = mapReduce instanceof ProgramLifecycle ? Transactions.getTransactionControl(defaultTxControl, MapReduce.class, mapReduce, "destroy") : defaultTxControl;
context.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(context.getFieldLineageOperations());
fieldLineageWriter.write(mapReduceRunId, info);
} catch (Throwable t) {
LOG.warn("Failed to emit the field lineage operations for MapReduce {}", mapReduceRunId, t);
}
}
}
use of io.cdap.cdap.api.annotation.TransactionControl in project cdap by caskdata.
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.annotation.TransactionControl 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);
}
}
Aggregations