Search in sources :

Example 1 with AbstractCondition

use of io.cdap.cdap.api.workflow.AbstractCondition 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)

Aggregations

TransactionControl (io.cdap.cdap.api.annotation.TransactionControl)1 AbstractCondition (io.cdap.cdap.api.workflow.AbstractCondition)1 Condition (io.cdap.cdap.api.workflow.Condition)1 WorkflowContext (io.cdap.cdap.api.workflow.WorkflowContext)1 WorkflowNode (io.cdap.cdap.api.workflow.WorkflowNode)1 PropertyFieldSetter (io.cdap.cdap.common.lang.PropertyFieldSetter)1 DataSetFieldSetter (io.cdap.cdap.internal.app.runtime.DataSetFieldSetter)1 MetricsFieldSetter (io.cdap.cdap.internal.app.runtime.MetricsFieldSetter)1