Search in sources :

Example 1 with WorkflowDataProvider

use of io.cdap.cdap.app.runtime.WorkflowDataProvider 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 WorkflowDataProvider

use of io.cdap.cdap.app.runtime.WorkflowDataProvider in project cdap by caskdata.

the class DefaultProgramWorkflowRunner method runAndWait.

private void runAndWait(ProgramRunner programRunner, Program program, ProgramOptions options) throws Exception {
    Closeable closeable = createCloseable(programRunner, program);
    // Publish the program's starting state
    RunId runId = ProgramRunners.getRunId(options);
    String twillRunId = options.getArguments().getOption(ProgramOptionConstants.TWILL_RUN_ID);
    ProgramDescriptor programDescriptor = new ProgramDescriptor(program.getId(), program.getApplicationSpecification());
    programStateWriter.start(program.getId().run(runId), options, twillRunId, programDescriptor);
    ProgramController controller;
    try {
        controller = programRunner.run(program, options);
    } catch (Throwable t) {
        // If there is any exception when running the program, close the program to release resources.
        // Otherwise it will be released when the execution completed.
        programStateWriter.error(program.getId().run(runId), t);
        Closeables.closeQuietly(closeable);
        throw t;
    }
    blockForCompletion(closeable, controller);
    if (controller instanceof WorkflowDataProvider) {
        updateWorkflowToken(((WorkflowDataProvider) controller).getWorkflowToken());
    } else {
        // This shouldn't happen
        throw new IllegalStateException("No WorkflowToken available after program completed: " + program.getId());
    }
}
Also used : ProgramController(io.cdap.cdap.app.runtime.ProgramController) Closeable(java.io.Closeable) WorkflowDataProvider(io.cdap.cdap.app.runtime.WorkflowDataProvider) ProgramDescriptor(io.cdap.cdap.app.program.ProgramDescriptor) RunId(org.apache.twill.api.RunId)

Aggregations

ProgramController (io.cdap.cdap.app.runtime.ProgramController)2 WorkflowDataProvider (io.cdap.cdap.app.runtime.WorkflowDataProvider)2 WorkflowNodeState (io.cdap.cdap.api.workflow.WorkflowNodeState)1 ProgramDescriptor (io.cdap.cdap.app.program.ProgramDescriptor)1 AbstractListener (io.cdap.cdap.internal.app.runtime.AbstractListener)1 Closeable (java.io.Closeable)1 IOException (java.io.IOException)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 ExecutionException (java.util.concurrent.ExecutionException)1 RunId (org.apache.twill.api.RunId)1