Search in sources :

Example 1 with WorkflowNodeState

use of co.cask.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);
    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);
    customActionExecutor = new CustomActionExecutor(context, instantiator, classLoader);
    status.put(node.getNodeId(), node);
    runtimeStore.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());
        runtimeStore.updateWorkflowToken(workflowRunId, token);
        NodeStatus status = failureCause == null ? NodeStatus.COMPLETED : NodeStatus.FAILED;
        nodeStates.put(node.getNodeId(), new WorkflowNodeState(node.getNodeId(), status, null, failureCause));
        BasicThrowable defaultThrowable = failureCause == null ? null : new BasicThrowable(failureCause);
        runtimeStore.addWorkflowNodeState(workflowRunId, new WorkflowNodeStateDetail(node.getNodeId(), status, null, defaultThrowable));
    }
}
Also used : WorkflowNodeState(co.cask.cdap.api.workflow.WorkflowNodeState) BasicCustomActionContext(co.cask.cdap.internal.app.runtime.customaction.BasicCustomActionContext) BasicThrowable(co.cask.cdap.proto.BasicThrowable) SimpleProgramOptions(co.cask.cdap.internal.app.runtime.SimpleProgramOptions) BasicArguments(co.cask.cdap.internal.app.runtime.BasicArguments) BasicThrowable(co.cask.cdap.proto.BasicThrowable) SimpleProgramOptions(co.cask.cdap.internal.app.runtime.SimpleProgramOptions) ProgramOptions(co.cask.cdap.app.runtime.ProgramOptions) NodeStatus(co.cask.cdap.api.workflow.NodeStatus) WorkflowNodeStateDetail(co.cask.cdap.proto.WorkflowNodeStateDetail)

Example 2 with WorkflowNodeState

use of co.cask.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(co.cask.cdap.api.workflow.WorkflowNodeState) Table(co.cask.cdap.api.dataset.table.Table) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map) Put(co.cask.cdap.api.dataset.table.Put)

Example 3 with WorkflowNodeState

use of co.cask.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);
            nodeStates.put(nodeId, new WorkflowNodeState(nodeId, NodeStatus.COMPLETED, 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(co.cask.cdap.app.runtime.ProgramController) WorkflowNodeState(co.cask.cdap.api.workflow.WorkflowNodeState) AbstractListener(co.cask.cdap.internal.app.runtime.AbstractListener) ExecutionException(java.util.concurrent.ExecutionException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

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