Search in sources :

Example 1 with WorkflowContext

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

the class ETLWorkflow method destroy.

@TransactionPolicy(TransactionControl.EXPLICIT)
@Override
public void destroy() {
    WorkflowContext workflowContext = getContext();
    PipelineRuntime pipelineRuntime = new PipelineRuntime(workflowContext, workflowMetrics);
    if (workflowContext.getDataTracer(PostAction.PLUGIN_TYPE).isEnabled()) {
        return;
    }
    for (Map.Entry<String, PostAction> endingActionEntry : postActions.entrySet()) {
        String name = endingActionEntry.getKey();
        PostAction action = endingActionEntry.getValue();
        StageSpec stageSpec = postActionSpecs.get(name);
        BatchActionContext context = new WorkflowBackedActionContext(workflowContext, pipelineRuntime, stageSpec);
        try {
            action.run(context);
        } catch (Throwable t) {
            LOG.error("Error while running ending action {}.", name, t);
        }
    }
}
Also used : PipelineRuntime(co.cask.cdap.etl.common.PipelineRuntime) BatchActionContext(co.cask.cdap.etl.api.batch.BatchActionContext) WorkflowContext(co.cask.cdap.api.workflow.WorkflowContext) StageSpec(co.cask.cdap.etl.spec.StageSpec) PostAction(co.cask.cdap.etl.api.batch.PostAction) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) TransactionPolicy(co.cask.cdap.api.annotation.TransactionPolicy)

Example 2 with WorkflowContext

use of co.cask.cdap.api.workflow.WorkflowContext 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 3 with WorkflowContext

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

the class SmartWorkflow method destroy.

@Override
public void destroy() {
    WorkflowContext workflowContext = getContext();
    PipelineRuntime pipelineRuntime = new PipelineRuntime(workflowContext, workflowMetrics);
    // Execute the post actions only if pipeline is not running in preview mode.
    if (!workflowContext.getDataTracer(PostAction.PLUGIN_TYPE).isEnabled()) {
        for (Map.Entry<String, PostAction> endingActionEntry : postActions.entrySet()) {
            String name = endingActionEntry.getKey();
            PostAction action = endingActionEntry.getValue();
            StageSpec stageSpec = stageSpecs.get(name);
            BatchActionContext context = new WorkflowBackedActionContext(workflowContext, pipelineRuntime, stageSpec);
            try {
                action.run(context);
            } catch (Throwable t) {
                LOG.error("Error while running post action {}.", name, t);
            }
        }
    }
    // publish all alerts
    for (Map.Entry<String, AlertPublisher> alertPublisherEntry : alertPublishers.entrySet()) {
        String name = alertPublisherEntry.getKey();
        AlertPublisher alertPublisher = alertPublisherEntry.getValue();
        PartitionedFileSet alertConnector = workflowContext.getDataset(name);
        try (CloseableIterator<Alert> alerts = new AlertReader(alertConnector.getPartitions(PartitionFilter.ALWAYS_MATCH))) {
            if (!alerts.hasNext()) {
                continue;
            }
            StageMetrics stageMetrics = new DefaultStageMetrics(workflowMetrics, name);
            StageSpec stageSpec = stageSpecs.get(name);
            AlertPublisherContext alertContext = new DefaultAlertPublisherContext(pipelineRuntime, stageSpec, workflowContext, workflowContext.getAdmin());
            alertPublisher.initialize(alertContext);
            TrackedIterator<Alert> trackedIterator = new TrackedIterator<>(alerts, stageMetrics, Constants.Metrics.RECORDS_IN);
            alertPublisher.publish(trackedIterator);
        } catch (Exception e) {
            LOG.warn("Stage {} had errors publishing alerts. Alerts may not have been published.", name, e);
        } finally {
            try {
                alertPublisher.destroy();
            } catch (Exception e) {
                LOG.warn("Error destroying alert publisher for stage {}", name, e);
            }
        }
    }
    ProgramStatus status = getContext().getState().getStatus();
    if (status == ProgramStatus.FAILED) {
        WRAPPERLOGGER.error("Pipeline '{}' failed.", getContext().getApplicationSpecification().getName());
    } else {
        WRAPPERLOGGER.info("Pipeline '{}' {}.", getContext().getApplicationSpecification().getName(), status == ProgramStatus.COMPLETED ? "succeeded" : status.name().toLowerCase());
    }
    MacroEvaluator macroEvaluator = new DefaultMacroEvaluator(pipelineRuntime.getArguments(), workflowContext.getLogicalStartTime(), workflowContext, workflowContext.getNamespace());
    // Get resolved plugin properties
    Map<String, Map<String, String>> resolvedProperties = new HashMap<>();
    for (StageSpec spec : stageSpecs.values()) {
        String stageName = spec.getName();
        resolvedProperties.put(stageName, workflowContext.getPluginProperties(stageName, macroEvaluator).getProperties());
    }
    // Add resolved plugin properties to workflow token as a JSON String
    workflowContext.getToken().put(RESOLVED_PLUGIN_PROPERTIES_MAP, GSON.toJson(resolvedProperties));
}
Also used : PipelineRuntime(co.cask.cdap.etl.common.PipelineRuntime) DefaultMacroEvaluator(co.cask.cdap.etl.common.DefaultMacroEvaluator) MacroEvaluator(co.cask.cdap.api.macro.MacroEvaluator) BatchActionContext(co.cask.cdap.etl.api.batch.BatchActionContext) WorkflowBackedActionContext(co.cask.cdap.etl.batch.WorkflowBackedActionContext) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) AlertReader(co.cask.cdap.etl.batch.connector.AlertReader) StageSpec(co.cask.cdap.etl.spec.StageSpec) DefaultMacroEvaluator(co.cask.cdap.etl.common.DefaultMacroEvaluator) StageMetrics(co.cask.cdap.etl.api.StageMetrics) DefaultStageMetrics(co.cask.cdap.etl.common.DefaultStageMetrics) DefaultAlertPublisherContext(co.cask.cdap.etl.common.DefaultAlertPublisherContext) AlertPublisherContext(co.cask.cdap.etl.api.AlertPublisherContext) AlertPublisher(co.cask.cdap.etl.api.AlertPublisher) TrackedIterator(co.cask.cdap.etl.common.TrackedIterator) WorkflowContext(co.cask.cdap.api.workflow.WorkflowContext) PartitionedFileSet(co.cask.cdap.api.dataset.lib.PartitionedFileSet) DisjointConnectionsException(co.cask.cdap.etl.planner.DisjointConnectionsException) Alert(co.cask.cdap.etl.api.Alert) PostAction(co.cask.cdap.etl.api.batch.PostAction) DefaultAlertPublisherContext(co.cask.cdap.etl.common.DefaultAlertPublisherContext) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) DefaultStageMetrics(co.cask.cdap.etl.common.DefaultStageMetrics) ProgramStatus(co.cask.cdap.api.ProgramStatus)

Aggregations

WorkflowContext (co.cask.cdap.api.workflow.WorkflowContext)3 BatchActionContext (co.cask.cdap.etl.api.batch.BatchActionContext)2 PostAction (co.cask.cdap.etl.api.batch.PostAction)2 PipelineRuntime (co.cask.cdap.etl.common.PipelineRuntime)2 StageSpec (co.cask.cdap.etl.spec.StageSpec)2 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 ProgramStatus (co.cask.cdap.api.ProgramStatus)1 TransactionControl (co.cask.cdap.api.annotation.TransactionControl)1 TransactionPolicy (co.cask.cdap.api.annotation.TransactionPolicy)1 PartitionedFileSet (co.cask.cdap.api.dataset.lib.PartitionedFileSet)1 MacroEvaluator (co.cask.cdap.api.macro.MacroEvaluator)1 AbstractCondition (co.cask.cdap.api.workflow.AbstractCondition)1 Condition (co.cask.cdap.api.workflow.Condition)1 WorkflowNode (co.cask.cdap.api.workflow.WorkflowNode)1 PropertyFieldSetter (co.cask.cdap.common.lang.PropertyFieldSetter)1 Alert (co.cask.cdap.etl.api.Alert)1 AlertPublisher (co.cask.cdap.etl.api.AlertPublisher)1 AlertPublisherContext (co.cask.cdap.etl.api.AlertPublisherContext)1