Search in sources :

Example 1 with TransactionControl

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);
}
Also used : AbstractCondition(io.cdap.cdap.api.workflow.AbstractCondition) Condition(io.cdap.cdap.api.workflow.Condition) WorkflowContext(io.cdap.cdap.api.workflow.WorkflowContext) WorkflowNode(io.cdap.cdap.api.workflow.WorkflowNode) DataSetFieldSetter(io.cdap.cdap.internal.app.runtime.DataSetFieldSetter) PropertyFieldSetter(io.cdap.cdap.common.lang.PropertyFieldSetter) MetricsFieldSetter(io.cdap.cdap.internal.app.runtime.MetricsFieldSetter) TransactionControl(io.cdap.cdap.api.annotation.TransactionControl) AbstractCondition(io.cdap.cdap.api.workflow.AbstractCondition)

Example 2 with TransactionControl

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();
}
Also used : TransactionControl(io.cdap.cdap.api.annotation.TransactionControl)

Example 3 with TransactionControl

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);
        }
    }
}
Also used : ProgramLifecycle(io.cdap.cdap.api.ProgramLifecycle) TransactionControl(io.cdap.cdap.api.annotation.TransactionControl) FieldLineageInfo(io.cdap.cdap.data2.metadata.lineage.field.FieldLineageInfo) AbstractMapReduce(io.cdap.cdap.api.mapreduce.AbstractMapReduce) MapReduce(io.cdap.cdap.api.mapreduce.MapReduce)

Example 4 with TransactionControl

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);
        }
    }
}
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 5 with TransactionControl

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

Aggregations

TransactionControl (io.cdap.cdap.api.annotation.TransactionControl)14 ProgramLifecycle (io.cdap.cdap.api.ProgramLifecycle)6 ProgramState (io.cdap.cdap.api.ProgramState)4 InstantiatorFactory (io.cdap.cdap.common.lang.InstantiatorFactory)3 MetricsFieldSetter (io.cdap.cdap.internal.app.runtime.MetricsFieldSetter)3 TypeToken (com.google.common.reflect.TypeToken)2 AbstractMapReduce (io.cdap.cdap.api.mapreduce.AbstractMapReduce)2 AbstractSpark (io.cdap.cdap.api.spark.AbstractSpark)2 PropertyFieldSetter (io.cdap.cdap.common.lang.PropertyFieldSetter)2 FieldLineageInfo (io.cdap.cdap.data2.metadata.lineage.field.FieldLineageInfo)2 Preconditions (com.google.common.base.Preconditions)1 AbstractCustomAction (io.cdap.cdap.api.customaction.AbstractCustomAction)1 CustomAction (io.cdap.cdap.api.customaction.CustomAction)1 CustomActionContext (io.cdap.cdap.api.customaction.CustomActionContext)1 MapReduce (io.cdap.cdap.api.mapreduce.MapReduce)1 MetricsCollectionService (io.cdap.cdap.api.metrics.MetricsCollectionService)1 MetricsContext (io.cdap.cdap.api.metrics.MetricsContext)1 AbstractSystemService (io.cdap.cdap.api.service.AbstractSystemService)1 Service (io.cdap.cdap.api.service.Service)1 HttpContentConsumer (io.cdap.cdap.api.service.http.HttpContentConsumer)1