Search in sources :

Example 6 with WorkflowNode

use of co.cask.cdap.api.workflow.WorkflowNode 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());
    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()));
        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(TransactionControl.IMPLICIT, AbstractCondition.class, workflowCondition, "initialize") : Transactions.getTransactionControl(TransactionControl.IMPLICIT, 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(TransactionControl.IMPLICIT, 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.
    runtimeStore.updateWorkflowToken(workflowRunId, token);
    executeAll(iterator, appSpec, instantiator, classLoader, token);
}
Also used : Condition(co.cask.cdap.api.workflow.Condition) AbstractCondition(co.cask.cdap.api.workflow.AbstractCondition) WorkflowContext(co.cask.cdap.api.workflow.WorkflowContext) WorkflowNode(co.cask.cdap.api.workflow.WorkflowNode) DataSetFieldSetter(co.cask.cdap.internal.app.runtime.DataSetFieldSetter) PropertyFieldSetter(co.cask.cdap.common.lang.PropertyFieldSetter) MetricsFieldSetter(co.cask.cdap.internal.app.runtime.MetricsFieldSetter) TransactionControl(co.cask.cdap.api.annotation.TransactionControl) AbstractCondition(co.cask.cdap.api.workflow.AbstractCondition)

Example 7 with WorkflowNode

use of co.cask.cdap.api.workflow.WorkflowNode in project cdap by caskdata.

the class DefaultWorkflowConfigurer method createForkNodeWithId.

private WorkflowNode createForkNodeWithId(WorkflowNode node) {
    String forkNodeId = Integer.toString(nodeIdentifier++);
    List<List<WorkflowNode>> branches = Lists.newArrayList();
    WorkflowForkNode forkNode = (WorkflowForkNode) node;
    for (List<WorkflowNode> branch : forkNode.getBranches()) {
        branches.add(createNodesWithId(branch));
    }
    return new WorkflowForkNode(forkNodeId, branches);
}
Also used : List(java.util.List) WorkflowForkNode(co.cask.cdap.api.workflow.WorkflowForkNode) WorkflowNode(co.cask.cdap.api.workflow.WorkflowNode)

Example 8 with WorkflowNode

use of co.cask.cdap.api.workflow.WorkflowNode in project cdap by caskdata.

the class DefaultWorkflowConfigurer method createConditionNodeWithId.

private WorkflowNode createConditionNodeWithId(WorkflowNode node) {
    WorkflowConditionNode conditionNode = (WorkflowConditionNode) node;
    List<WorkflowNode> ifbranch = Lists.newArrayList();
    List<WorkflowNode> elsebranch = Lists.newArrayList();
    ifbranch.addAll(createNodesWithId(conditionNode.getIfBranch()));
    elsebranch.addAll(createNodesWithId(conditionNode.getElseBranch()));
    ConditionSpecification spec = conditionNode.getConditionSpecification();
    return new WorkflowConditionNode(spec.getName(), spec, ifbranch, elsebranch);
}
Also used : WorkflowNode(co.cask.cdap.api.workflow.WorkflowNode) WorkflowConditionNode(co.cask.cdap.api.workflow.WorkflowConditionNode) ConditionSpecification(co.cask.cdap.api.workflow.ConditionSpecification) DefaultConditionSpecification(co.cask.cdap.internal.workflow.condition.DefaultConditionSpecification)

Example 9 with WorkflowNode

use of co.cask.cdap.api.workflow.WorkflowNode in project cdap by caskdata.

the class ApplicationVerificationStage method verifyWorkflowFork.

private void verifyWorkflowFork(ApplicationSpecification appSpec, WorkflowSpecification workflowSpec, WorkflowNode node, Set<String> existingNodeNames) {
    WorkflowForkNode forkNode = (WorkflowForkNode) node;
    Preconditions.checkNotNull(forkNode.getBranches(), String.format("Fork is added in the Workflow '%s' without" + " any branches", workflowSpec.getName()));
    for (List<WorkflowNode> branch : forkNode.getBranches()) {
        verifyWorkflowNodeList(appSpec, workflowSpec, branch, existingNodeNames);
    }
}
Also used : WorkflowForkNode(co.cask.cdap.api.workflow.WorkflowForkNode) WorkflowNode(co.cask.cdap.api.workflow.WorkflowNode)

Example 10 with WorkflowNode

use of co.cask.cdap.api.workflow.WorkflowNode in project cdap by caskdata.

the class DistributedWorkflowProgramRunner method validateOptions.

@Override
protected void validateOptions(Program program, ProgramOptions options) {
    super.validateOptions(program, options);
    // Extract and verify parameters
    ApplicationSpecification appSpec = program.getApplicationSpecification();
    Preconditions.checkNotNull(appSpec, "Missing application specification.");
    ProgramType processorType = program.getType();
    Preconditions.checkNotNull(processorType, "Missing processor type.");
    Preconditions.checkArgument(processorType == ProgramType.WORKFLOW, "Only WORKFLOW process type is supported.");
    WorkflowSpecification spec = appSpec.getWorkflows().get(program.getName());
    Preconditions.checkNotNull(spec, "Missing WorkflowSpecification for %s", program.getName());
    for (WorkflowNode node : spec.getNodes()) {
        if (node.getType().equals(WorkflowNodeType.ACTION)) {
            SystemArguments.validateTransactionTimeout(options.getUserArguments().asMap(), cConf, "action", node.getNodeId());
        }
    }
}
Also used : ApplicationSpecification(co.cask.cdap.api.app.ApplicationSpecification) WorkflowSpecification(co.cask.cdap.api.workflow.WorkflowSpecification) ProgramType(co.cask.cdap.proto.ProgramType) SchedulableProgramType(co.cask.cdap.api.schedule.SchedulableProgramType) WorkflowNode(co.cask.cdap.api.workflow.WorkflowNode)

Aggregations

WorkflowNode (co.cask.cdap.api.workflow.WorkflowNode)15 WorkflowSpecification (co.cask.cdap.api.workflow.WorkflowSpecification)7 WorkflowActionNode (co.cask.cdap.api.workflow.WorkflowActionNode)5 ScheduleProgramInfo (co.cask.cdap.api.workflow.ScheduleProgramInfo)4 WorkflowForkNode (co.cask.cdap.api.workflow.WorkflowForkNode)4 Map (java.util.Map)3 ApplicationSpecification (co.cask.cdap.api.app.ApplicationSpecification)2 SchedulableProgramType (co.cask.cdap.api.schedule.SchedulableProgramType)2 WorkflowConditionNode (co.cask.cdap.api.workflow.WorkflowConditionNode)2 DatasetCreationSpec (co.cask.cdap.internal.dataset.DatasetCreationSpec)2 ProgramType (co.cask.cdap.proto.ProgramType)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 ProgramState (co.cask.cdap.api.ProgramState)1 Resources (co.cask.cdap.api.Resources)1 TransactionControl (co.cask.cdap.api.annotation.TransactionControl)1 DatasetManagementException (co.cask.cdap.api.dataset.DatasetManagementException)1 FileSet (co.cask.cdap.api.dataset.lib.FileSet)1 Table (co.cask.cdap.api.dataset.table.Table)1 AbstractCondition (co.cask.cdap.api.workflow.AbstractCondition)1 Condition (co.cask.cdap.api.workflow.Condition)1