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