Search in sources :

Example 6 with WorkflowToken

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

the class TopNMapReduce method destroy.

@Override
public void destroy() {
    WorkflowToken workflowToken = getContext().getWorkflowToken();
    if (workflowToken != null) {
        boolean isSuccessful = getContext().getState().getStatus() == ProgramStatus.COMPLETED;
        workflowToken.put("result", Value.of(isSuccessful));
    }
}
Also used : WorkflowToken(co.cask.cdap.api.workflow.WorkflowToken)

Example 7 with WorkflowToken

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

the class TopNMapReduce method initialize.

@Override
public void initialize() throws Exception {
    MapReduceContext context = getContext();
    Map<String, String> runtimeArguments = context.getRuntimeArguments();
    Job job = context.getHadoopJob();
    WorkflowToken workflowToken = context.getWorkflowToken();
    int topNRank = 10;
    if (runtimeArguments.containsKey("topn.rank")) {
        topNRank = Integer.parseInt(runtimeArguments.get("topn.rank"));
    }
    if (workflowToken != null) {
        workflowToken.put("topn.rank", Value.of(topNRank));
    }
    int numReduceTasks = 1;
    if (runtimeArguments.containsKey("num.reduce.tasks")) {
        numReduceTasks = Integer.parseInt(runtimeArguments.get("num.reduce.tasks"));
    }
    job.setNumReduceTasks(numReduceTasks);
    job.setMapperClass(TokenizerMapper.class);
    job.setReducerClass(TopNReducer.class);
    String dataNamespace = runtimeArguments.get(WikipediaPipelineApp.NAMESPACE_ARG);
    dataNamespace = dataNamespace == null ? getContext().getNamespace() : dataNamespace;
    context.addInput(Input.ofDataset(WikipediaPipelineApp.NORMALIZED_WIKIPEDIA_DATASET).fromNamespace(dataNamespace));
    context.addOutput(Output.ofDataset(WikipediaPipelineApp.MAPREDUCE_TOPN_OUTPUT).fromNamespace(dataNamespace));
}
Also used : MapReduceContext(co.cask.cdap.api.mapreduce.MapReduceContext) WorkflowToken(co.cask.cdap.api.workflow.WorkflowToken) Job(org.apache.hadoop.mapreduce.Job)

Example 8 with WorkflowToken

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

the class WorkflowDriver method executeFork.

private void executeFork(final ApplicationSpecification appSpec, WorkflowForkNode fork, final InstantiatorFactory instantiator, final ClassLoader classLoader, final WorkflowToken token) throws Exception {
    CountDownLatch executorTerminateLatch = new CountDownLatch(1);
    ExecutorService executorService = createExecutor(fork.getBranches().size(), executorTerminateLatch, "fork-" + fork.getNodeId() + "-%d");
    CompletionService<Map.Entry<String, WorkflowToken>> completionService = new ExecutorCompletionService<>(executorService);
    try {
        for (final List<WorkflowNode> branch : fork.getBranches()) {
            completionService.submit(new Callable<Map.Entry<String, WorkflowToken>>() {

                @Override
                public Map.Entry<String, WorkflowToken> call() throws Exception {
                    WorkflowToken copiedToken = ((BasicWorkflowToken) token).deepCopy();
                    executeAll(branch.iterator(), appSpec, instantiator, classLoader, copiedToken);
                    return Maps.immutableEntry(branch.toString(), copiedToken);
                }
            });
        }
        for (int i = 0; i < fork.getBranches().size(); i++) {
            try {
                Future<Map.Entry<String, WorkflowToken>> forkBranchResult = completionService.take();
                Map.Entry<String, WorkflowToken> retValue = forkBranchResult.get();
                String branchInfo = retValue.getKey();
                WorkflowToken branchToken = retValue.getValue();
                ((BasicWorkflowToken) token).mergeToken(branchToken);
                LOG.trace("Execution of branch {} for fork {} completed.", branchInfo, fork);
            } catch (InterruptedException e) {
                // Due to workflow abortion, so just break the loop
                break;
            } catch (ExecutionException e) {
                // Unwrap the cause
                Throwables.propagateIfPossible(e.getCause(), Exception.class);
                throw Throwables.propagate(e.getCause());
            }
        }
    } finally {
        // Update the WorkflowToken after the execution of the FORK node completes.
        runtimeStore.updateWorkflowToken(workflowRunId, token);
        executorService.shutdownNow();
        // Wait for the executor termination
        executorTerminateLatch.await();
    }
}
Also used : WorkflowToken(co.cask.cdap.api.workflow.WorkflowToken) ExecutorCompletionService(java.util.concurrent.ExecutorCompletionService) CountDownLatch(java.util.concurrent.CountDownLatch) WorkflowNode(co.cask.cdap.api.workflow.WorkflowNode) DatasetManagementException(co.cask.cdap.api.dataset.DatasetManagementException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) ExecutorService(java.util.concurrent.ExecutorService) ExecutionException(java.util.concurrent.ExecutionException) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 9 with WorkflowToken

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

the class StreamToDataset method destroy.

@Override
public void destroy() {
    WorkflowToken workflowToken = getContext().getWorkflowToken();
    if (workflowToken != null) {
        boolean isSuccessful = getContext().getState().getStatus() == ProgramStatus.COMPLETED;
        workflowToken.put("result", Value.of(isSuccessful));
    }
}
Also used : WorkflowToken(co.cask.cdap.api.workflow.WorkflowToken)

Example 10 with WorkflowToken

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

the class WikiContentValidatorAndNormalizer method destroy.

@Override
public void destroy() {
    WorkflowToken workflowToken = getContext().getWorkflowToken();
    if (workflowToken != null) {
        boolean isSuccessful = getContext().getState().getStatus() == ProgramStatus.COMPLETED;
        workflowToken.put("result", Value.of(isSuccessful));
    }
}
Also used : WorkflowToken(co.cask.cdap.api.workflow.WorkflowToken)

Aggregations

WorkflowToken (co.cask.cdap.api.workflow.WorkflowToken)10 MapReduceContext (co.cask.cdap.api.mapreduce.MapReduceContext)2 Map (java.util.Map)2 Path (javax.ws.rs.Path)2 Job (org.apache.hadoop.mapreduce.Job)2 AbstractCustomAction (co.cask.cdap.api.customaction.AbstractCustomAction)1 CustomAction (co.cask.cdap.api.customaction.CustomAction)1 CustomActionContext (co.cask.cdap.api.customaction.CustomActionContext)1 DatasetManagementException (co.cask.cdap.api.dataset.DatasetManagementException)1 InstanceNotFoundException (co.cask.cdap.api.dataset.InstanceNotFoundException)1 PluginContext (co.cask.cdap.api.plugin.PluginContext)1 SchedulableProgramType (co.cask.cdap.api.schedule.SchedulableProgramType)1 NodeValue (co.cask.cdap.api.workflow.NodeValue)1 Value (co.cask.cdap.api.workflow.Value)1 WorkflowNode (co.cask.cdap.api.workflow.WorkflowNode)1 ApplicationNotFoundException (co.cask.cdap.common.ApplicationNotFoundException)1 NotFoundException (co.cask.cdap.common.NotFoundException)1 ProgramNotFoundException (co.cask.cdap.common.ProgramNotFoundException)1 MethodArgument (co.cask.cdap.common.internal.remote.MethodArgument)1 Action (co.cask.cdap.etl.api.action.Action)1