Search in sources :

Example 1 with WorkflowNodeState

use of io.cdap.cdap.api.workflow.WorkflowNodeState in project cdap by caskdata.

the class DefaultProgramWorkflowRunner method blockForCompletion.

/**
 * Adds a listener to the {@link ProgramController} and blocks for completion.
 *
 * @param closeable a {@link Closeable} to call when the program execution completed
 * @param controller the {@link ProgramController} for the program
 * @throws Exception if the execution failed
 */
private void blockForCompletion(final Closeable closeable, final ProgramController controller) throws Exception {
    // Execute the program.
    final SettableFuture<Void> completion = SettableFuture.create();
    controller.addListener(new AbstractListener() {

        @Override
        public void init(ProgramController.State currentState, @Nullable Throwable cause) {
            switch(currentState) {
                case COMPLETED:
                    completed();
                    break;
                case KILLED:
                    killed();
                    break;
                case ERROR:
                    error(cause);
                    break;
            }
        }

        @Override
        public void completed() {
            Closeables.closeQuietly(closeable);
            Set<Operation> fieldLineageOperations = new HashSet<>();
            if (controller instanceof WorkflowDataProvider) {
                fieldLineageOperations.addAll(((WorkflowDataProvider) controller).getFieldLineageOperations());
            }
            nodeStates.put(nodeId, new WorkflowNodeState(nodeId, NodeStatus.COMPLETED, fieldLineageOperations, controller.getRunId().getId(), null));
            completion.set(null);
        }

        @Override
        public void killed() {
            Closeables.closeQuietly(closeable);
            nodeStates.put(nodeId, new WorkflowNodeState(nodeId, NodeStatus.KILLED, controller.getRunId().getId(), null));
            completion.set(null);
        }

        @Override
        public void error(Throwable cause) {
            Closeables.closeQuietly(closeable);
            nodeStates.put(nodeId, new WorkflowNodeState(nodeId, NodeStatus.FAILED, controller.getRunId().getId(), cause));
            completion.setException(cause);
        }
    }, Threads.SAME_THREAD_EXECUTOR);
    // Block for completion.
    try {
        completion.get();
    } catch (ExecutionException e) {
        Throwable cause = e.getCause();
        if (cause instanceof Exception) {
            throw (Exception) cause;
        }
        throw Throwables.propagate(cause);
    } catch (InterruptedException e) {
        try {
            Futures.getUnchecked(controller.stop());
        } catch (Throwable t) {
        // no-op
        }
        // reset the interrupt
        Thread.currentThread().interrupt();
    }
}
Also used : ProgramController(io.cdap.cdap.app.runtime.ProgramController) HashSet(java.util.HashSet) Set(java.util.Set) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) WorkflowNodeState(io.cdap.cdap.api.workflow.WorkflowNodeState) WorkflowDataProvider(io.cdap.cdap.app.runtime.WorkflowDataProvider) AbstractListener(io.cdap.cdap.internal.app.runtime.AbstractListener) ExecutionException(java.util.concurrent.ExecutionException)

Example 2 with WorkflowNodeState

use of io.cdap.cdap.api.workflow.WorkflowNodeState in project cdap by caskdata.

the class NodeStatesAction method run.

@Override
public void run(BatchActionContext context) throws Exception {
    Table table = context.getDataset(conf.tableName);
    for (Map.Entry<String, WorkflowNodeState> entry : context.getNodeStates().entrySet()) {
        Put put = new Put(entry.getKey());
        WorkflowNodeState nodeState = entry.getValue();
        put.add("runid", nodeState.getRunId());
        put.add("nodeid", nodeState.getNodeId());
        put.add("status", nodeState.getNodeStatus().name());
        table.put(put);
    }
}
Also used : WorkflowNodeState(io.cdap.cdap.api.workflow.WorkflowNodeState) Table(io.cdap.cdap.api.dataset.table.Table) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) Map(java.util.Map) Put(io.cdap.cdap.api.dataset.table.Put)

Example 3 with WorkflowNodeState

use of io.cdap.cdap.api.workflow.WorkflowNodeState in project cdap by caskdata.

the class WorkflowDriver method executeCustomAction.

private void executeCustomAction(final WorkflowActionNode node, InstantiatorFactory instantiator, final ClassLoader classLoader, WorkflowToken token) throws Exception {
    CustomActionExecutor customActionExecutor;
    // Node has CustomActionSpecification, so it must represent the CustomAction added in 3.5.0
    // Create instance of the CustomActionExecutor using CustomActionContext
    WorkflowProgramInfo info = new WorkflowProgramInfo(workflowSpec.getName(), node.getNodeId(), workflowRunId.getRun(), node.getNodeId(), (BasicWorkflowToken) token, workflowContext.fieldLineageConsolidationEnabled());
    ProgramOptions actionOptions = new SimpleProgramOptions(programOptions.getProgramId(), programOptions.getArguments(), new BasicArguments(RuntimeArguments.extractScope(ACTION_SCOPE, node.getNodeId(), programOptions.getUserArguments().asMap())));
    BasicCustomActionContext context = new BasicCustomActionContext(program, actionOptions, cConf, node.getCustomActionSpecification(), info, metricsCollectionService, datasetFramework, txClient, discoveryServiceClient, pluginInstantiator, secureStore, secureStoreManager, messagingService, metadataReader, metadataPublisher, namespaceQueryAdmin, fieldLineageWriter, remoteClientFactory);
    customActionExecutor = new CustomActionExecutor(context, instantiator, classLoader);
    status.put(node.getNodeId(), node);
    workflowStateWriter.addWorkflowNodeState(workflowRunId, new WorkflowNodeStateDetail(node.getNodeId(), NodeStatus.RUNNING));
    Throwable failureCause = null;
    try {
        customActionExecutor.execute();
    } catch (Throwable t) {
        failureCause = t;
        throw t;
    } finally {
        status.remove(node.getNodeId());
        workflowStateWriter.setWorkflowToken(workflowRunId, token);
        NodeStatus status = failureCause == null ? NodeStatus.COMPLETED : NodeStatus.FAILED;
        if (failureCause == null) {
            writeFieldLineage(context);
        }
        nodeStates.put(node.getNodeId(), new WorkflowNodeState(node.getNodeId(), status, null, failureCause));
        BasicThrowable defaultThrowable = failureCause == null ? null : new BasicThrowable(failureCause);
        workflowStateWriter.addWorkflowNodeState(workflowRunId, new WorkflowNodeStateDetail(node.getNodeId(), status, null, defaultThrowable));
    }
}
Also used : WorkflowNodeState(io.cdap.cdap.api.workflow.WorkflowNodeState) BasicCustomActionContext(io.cdap.cdap.internal.app.runtime.customaction.BasicCustomActionContext) BasicThrowable(io.cdap.cdap.proto.BasicThrowable) SimpleProgramOptions(io.cdap.cdap.internal.app.runtime.SimpleProgramOptions) BasicArguments(io.cdap.cdap.internal.app.runtime.BasicArguments) BasicThrowable(io.cdap.cdap.proto.BasicThrowable) SimpleProgramOptions(io.cdap.cdap.internal.app.runtime.SimpleProgramOptions) ProgramOptions(io.cdap.cdap.app.runtime.ProgramOptions) NodeStatus(io.cdap.cdap.api.workflow.NodeStatus) WorkflowNodeStateDetail(io.cdap.cdap.proto.WorkflowNodeStateDetail)

Aggregations

WorkflowNodeState (io.cdap.cdap.api.workflow.WorkflowNodeState)3 ImmutableMap (com.google.common.collect.ImmutableMap)1 Put (io.cdap.cdap.api.dataset.table.Put)1 Table (io.cdap.cdap.api.dataset.table.Table)1 NodeStatus (io.cdap.cdap.api.workflow.NodeStatus)1 ProgramController (io.cdap.cdap.app.runtime.ProgramController)1 ProgramOptions (io.cdap.cdap.app.runtime.ProgramOptions)1 WorkflowDataProvider (io.cdap.cdap.app.runtime.WorkflowDataProvider)1 AbstractListener (io.cdap.cdap.internal.app.runtime.AbstractListener)1 BasicArguments (io.cdap.cdap.internal.app.runtime.BasicArguments)1 SimpleProgramOptions (io.cdap.cdap.internal.app.runtime.SimpleProgramOptions)1 BasicCustomActionContext (io.cdap.cdap.internal.app.runtime.customaction.BasicCustomActionContext)1 BasicThrowable (io.cdap.cdap.proto.BasicThrowable)1 WorkflowNodeStateDetail (io.cdap.cdap.proto.WorkflowNodeStateDetail)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Set (java.util.Set)1 ExecutionException (java.util.concurrent.ExecutionException)1