Search in sources :

Example 6 with BasicThrowable

use of co.cask.cdap.proto.BasicThrowable in project cdap by caskdata.

the class DefaultStoreTest method testWorkflowNodeState.

@Test
public void testWorkflowNodeState() throws Exception {
    String namespaceName = "namespace1";
    String appName = "app1";
    String workflowName = "workflow1";
    String mapReduceName = "mapReduce1";
    String sparkName = "spark1";
    ApplicationId appId = Ids.namespace(namespaceName).app(appName);
    ProgramId mapReduceProgram = appId.mr(mapReduceName);
    ProgramId sparkProgram = appId.spark(sparkName);
    long currentTime = System.currentTimeMillis();
    String workflowRunId = RunIds.generate(currentTime).getId();
    ProgramRunId workflowRun = appId.workflow(workflowName).run(workflowRunId);
    // start Workflow
    store.setStart(workflowRun.getParent(), workflowRun.getRun(), currentTime);
    // start MapReduce as a part of Workflow
    Map<String, String> systemArgs = ImmutableMap.of(ProgramOptionConstants.WORKFLOW_NODE_ID, mapReduceName, ProgramOptionConstants.WORKFLOW_NAME, workflowName, ProgramOptionConstants.WORKFLOW_RUN_ID, workflowRunId);
    RunId mapReduceRunId = RunIds.generate(currentTime + 10);
    store.setStart(mapReduceProgram, mapReduceRunId.getId(), currentTime + 10, null, ImmutableMap.<String, String>of(), systemArgs);
    // stop the MapReduce program
    store.setStop(mapReduceProgram, mapReduceRunId.getId(), currentTime + 50, ProgramRunStatus.COMPLETED);
    // start Spark program as a part of Workflow
    systemArgs = ImmutableMap.of(ProgramOptionConstants.WORKFLOW_NODE_ID, sparkName, ProgramOptionConstants.WORKFLOW_NAME, workflowName, ProgramOptionConstants.WORKFLOW_RUN_ID, workflowRunId);
    RunId sparkRunId = RunIds.generate(currentTime + 60);
    store.setStart(sparkProgram, sparkRunId.getId(), currentTime + 60, null, ImmutableMap.<String, String>of(), systemArgs);
    // stop the Spark program with failure
    NullPointerException npe = new NullPointerException("dataset not found");
    IllegalArgumentException iae = new IllegalArgumentException("illegal argument", npe);
    store.setStop(sparkProgram, sparkRunId.getId(), currentTime + 100, ProgramRunStatus.FAILED, new BasicThrowable(iae));
    // stop Workflow
    store.setStop(workflowRun.getParent(), workflowRun.getRun(), currentTime + 110, ProgramRunStatus.FAILED);
    List<WorkflowNodeStateDetail> nodeStateDetails = store.getWorkflowNodeStates(workflowRun);
    Map<String, WorkflowNodeStateDetail> workflowNodeStates = new HashMap<>();
    for (WorkflowNodeStateDetail nodeStateDetail : nodeStateDetails) {
        workflowNodeStates.put(nodeStateDetail.getNodeId(), nodeStateDetail);
    }
    Assert.assertEquals(2, workflowNodeStates.size());
    WorkflowNodeStateDetail nodeStateDetail = workflowNodeStates.get(mapReduceName);
    Assert.assertEquals(mapReduceName, nodeStateDetail.getNodeId());
    Assert.assertEquals(NodeStatus.COMPLETED, nodeStateDetail.getNodeStatus());
    Assert.assertEquals(mapReduceRunId.getId(), nodeStateDetail.getRunId());
    Assert.assertNull(nodeStateDetail.getFailureCause());
    nodeStateDetail = workflowNodeStates.get(sparkName);
    Assert.assertEquals(sparkName, nodeStateDetail.getNodeId());
    Assert.assertEquals(NodeStatus.FAILED, nodeStateDetail.getNodeStatus());
    Assert.assertEquals(sparkRunId.getId(), nodeStateDetail.getRunId());
    BasicThrowable failureCause = nodeStateDetail.getFailureCause();
    Assert.assertNotNull(failureCause);
    Assert.assertTrue("illegal argument".equals(failureCause.getMessage()));
    Assert.assertTrue("java.lang.IllegalArgumentException".equals(failureCause.getClassName()));
    failureCause = failureCause.getCause();
    Assert.assertNotNull(failureCause);
    Assert.assertTrue("dataset not found".equals(failureCause.getMessage()));
    Assert.assertTrue("java.lang.NullPointerException".equals(failureCause.getClassName()));
    Assert.assertNull(failureCause.getCause());
}
Also used : HashMap(java.util.HashMap) ProgramId(co.cask.cdap.proto.id.ProgramId) WorkflowNodeStateDetail(co.cask.cdap.proto.WorkflowNodeStateDetail) ProgramRunId(co.cask.cdap.proto.id.ProgramRunId) ApplicationId(co.cask.cdap.proto.id.ApplicationId) RunId(org.apache.twill.api.RunId) ProgramRunId(co.cask.cdap.proto.id.ProgramRunId) BasicThrowable(co.cask.cdap.proto.BasicThrowable) Test(org.junit.Test)

Example 7 with BasicThrowable

use of co.cask.cdap.proto.BasicThrowable in project cdap by caskdata.

the class RemoteRuntimeStoreTest method testWorkflowMethods.

@Test
public void testWorkflowMethods() {
    ProgramId workflowId = new ProgramId(Id.Namespace.DEFAULT.getId(), "test_app", ProgramType.WORKFLOW, "test_workflow");
    long stopTime = System.currentTimeMillis() / 1000;
    long startTime = stopTime - 20;
    String pid = RunIds.generate(startTime * 1000).getId();
    String twillRunId = "twill_run_id";
    Map<String, String> runtimeArgs = ImmutableMap.of();
    Map<String, String> properties = ImmutableMap.of("runtimeArgs", GSON.toJson(runtimeArgs));
    Map<String, String> systemArgs = ImmutableMap.of();
    RunRecordMeta initialRunRecord = new RunRecordMeta(pid, startTime, null, ProgramRunStatus.RUNNING, properties, systemArgs, twillRunId);
    runtimeStore.setStart(workflowId, pid, startTime, twillRunId, runtimeArgs, systemArgs);
    Assert.assertEquals(initialRunRecord, store.getRun(workflowId, pid));
    ProgramId mapreduceId = new ProgramId(workflowId.getNamespace(), workflowId.getApplication(), ProgramType.MAPREDUCE, "test_mr");
    String mapreducePid = RunIds.generate(startTime * 1000).getId();
    // these system properties just have to be set on the system arguments of the program, in order for it to be
    // understood as a program in a workflow node
    Map<String, String> mrSystemArgs = ImmutableMap.of(ProgramOptionConstants.WORKFLOW_NODE_ID, "test_node_id", ProgramOptionConstants.WORKFLOW_NAME, workflowId.getProgram(), ProgramOptionConstants.WORKFLOW_RUN_ID, pid);
    runtimeStore.setStart(mapreduceId, mapreducePid, startTime, twillRunId, runtimeArgs, mrSystemArgs);
    BasicThrowable failureCause = new BasicThrowable(new IllegalArgumentException("failure", new RuntimeException("oops")));
    runtimeStore.setStop(mapreduceId, mapreducePid, stopTime, ProgramRunStatus.FAILED, failureCause);
    runtimeStore.setStop(workflowId, pid, stopTime, ProgramRunStatus.FAILED);
    RunRecordMeta completedWorkflowRecord = store.getRun(workflowId, pid);
    // we're not comparing properties, since runtime (such as starting/stopping inner programs) modifies it
    Assert.assertEquals(pid, completedWorkflowRecord.getPid());
    Assert.assertEquals(initialRunRecord.getStartTs(), completedWorkflowRecord.getStartTs());
    Assert.assertEquals((Long) stopTime, completedWorkflowRecord.getStopTs());
    Assert.assertEquals(ProgramRunStatus.FAILED, completedWorkflowRecord.getStatus());
    Assert.assertEquals(twillRunId, completedWorkflowRecord.getTwillRunId());
    Assert.assertEquals(systemArgs, completedWorkflowRecord.getSystemArgs());
    // test that the BasicThrowable was serialized properly by RemoteRuntimeStore
    ProgramRunId workflowRunId = workflowId.run(pid);
    List<WorkflowNodeStateDetail> workflowNodeStates = store.getWorkflowNodeStates(workflowRunId);
    Assert.assertEquals(1, workflowNodeStates.size());
    WorkflowNodeStateDetail workflowNodeStateDetail = workflowNodeStates.get(0);
    Assert.assertEquals("test_node_id", workflowNodeStateDetail.getNodeId());
    Assert.assertEquals(mapreducePid, workflowNodeStateDetail.getRunId());
    Assert.assertEquals(NodeStatus.FAILED, workflowNodeStateDetail.getNodeStatus());
    Assert.assertEquals(failureCause, workflowNodeStateDetail.getFailureCause());
}
Also used : RunRecordMeta(co.cask.cdap.internal.app.store.RunRecordMeta) ProgramRunId(co.cask.cdap.proto.id.ProgramRunId) ProgramId(co.cask.cdap.proto.id.ProgramId) BasicThrowable(co.cask.cdap.proto.BasicThrowable) WorkflowNodeStateDetail(co.cask.cdap.proto.WorkflowNodeStateDetail) Test(org.junit.Test)

Example 8 with BasicThrowable

use of co.cask.cdap.proto.BasicThrowable in project cdap by caskdata.

the class BasicThrowableCodecTest method testCodec.

private void testCodec(Throwable t) {
    BasicThrowable bt = new BasicThrowable(t);
    String json = GSON.toJson(bt);
    BasicThrowable bt1 = GSON.fromJson(json, BasicThrowable.class);
    Assert.assertEquals(bt, bt1);
}
Also used : BasicThrowable(co.cask.cdap.proto.BasicThrowable)

Example 9 with BasicThrowable

use of co.cask.cdap.proto.BasicThrowable in project cdap by caskdata.

the class SparkProgramRunner method createRuntimeServiceListener.

/**
   * Creates a service listener to reactor on state changes on {@link SparkRuntimeService}.
   */
private Service.Listener createRuntimeServiceListener(final ProgramId programId, final RunId runId, final Arguments arguments, final Arguments userArgs, final Iterable<Closeable> closeables, final RuntimeStore runtimeStore) {
    final String twillRunId = arguments.getOption(ProgramOptionConstants.TWILL_RUN_ID);
    return new ServiceListenerAdapter() {

        @Override
        public void starting() {
            //Get start time from RunId
            long startTimeInSeconds = RunIds.getTime(runId, TimeUnit.SECONDS);
            if (startTimeInSeconds == -1) {
                // If RunId is not time-based, use current time as start time
                startTimeInSeconds = TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis());
            }
            final long finalStartTimeInSeconds = startTimeInSeconds;
            Retries.supplyWithRetries(new Supplier<Void>() {

                @Override
                public Void get() {
                    runtimeStore.setStart(programId, runId.getId(), finalStartTimeInSeconds, twillRunId, userArgs.asMap(), arguments.asMap());
                    return null;
                }
            }, RetryStrategies.fixDelay(Constants.Retry.RUN_RECORD_UPDATE_RETRY_DELAY_SECS, TimeUnit.SECONDS));
        }

        @Override
        public void terminated(Service.State from) {
            closeAll(closeables);
            ProgramRunStatus runStatus = ProgramController.State.COMPLETED.getRunStatus();
            if (from == Service.State.STOPPING) {
                // Service was killed
                runStatus = ProgramController.State.KILLED.getRunStatus();
            }
            final ProgramRunStatus finalRunStatus = runStatus;
            Retries.supplyWithRetries(new Supplier<Void>() {

                @Override
                public Void get() {
                    runtimeStore.setStop(programId, runId.getId(), TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()), finalRunStatus);
                    return null;
                }
            }, RetryStrategies.fixDelay(Constants.Retry.RUN_RECORD_UPDATE_RETRY_DELAY_SECS, TimeUnit.SECONDS));
        }

        @Override
        public void failed(Service.State from, @Nullable final Throwable failure) {
            closeAll(closeables);
            Retries.supplyWithRetries(new Supplier<Void>() {

                @Override
                public Void get() {
                    runtimeStore.setStop(programId, runId.getId(), TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()), ProgramController.State.ERROR.getRunStatus(), new BasicThrowable(failure));
                    return null;
                }
            }, RetryStrategies.fixDelay(Constants.Retry.RUN_RECORD_UPDATE_RETRY_DELAY_SECS, TimeUnit.SECONDS));
        }
    };
}
Also used : ProgramRunStatus(co.cask.cdap.proto.ProgramRunStatus) BasicThrowable(co.cask.cdap.proto.BasicThrowable) ServiceListenerAdapter(org.apache.twill.internal.ServiceListenerAdapter) BasicThrowable(co.cask.cdap.proto.BasicThrowable) Nullable(javax.annotation.Nullable)

Example 10 with BasicThrowable

use of co.cask.cdap.proto.BasicThrowable in project cdap by caskdata.

the class DefaultPreviewRunner method startPreview.

@Override
public void startPreview(PreviewRequest<?> previewRequest) throws Exception {
    namespaceAdmin.create(new NamespaceMeta.Builder().setName(previewRequest.getProgram().getNamespaceId()).build());
    programId = previewRequest.getProgram();
    AppRequest<?> request = previewRequest.getAppRequest();
    ArtifactSummary artifactSummary = request.getArtifact();
    ApplicationId preview = programId.getParent();
    DataTracerFactoryProvider.setDataTracerFactory(preview, dataTracerFactory);
    String config = request.getConfig() == null ? null : GSON.toJson(request.getConfig());
    try {
        applicationLifecycleService.deployApp(preview.getParent(), preview.getApplication(), preview.getVersion(), artifactSummary, config, NOOP_PROGRAM_TERMINATOR, null, request.canUpdateSchedules());
    } catch (Exception e) {
        this.status = new PreviewStatus(PreviewStatus.Status.DEPLOY_FAILED, new BasicThrowable(e), null, null);
        throw e;
    }
    final PreviewConfig previewConfig = previewRequest.getAppRequest().getPreview();
    ProgramController controller = programLifecycleService.start(programId, previewConfig == null ? Collections.<String, String>emptyMap() : previewConfig.getRuntimeArgs(), false);
    controller.addListener(new AbstractListener() {

        @Override
        public void init(ProgramController.State currentState, @Nullable Throwable cause) {
            setStatus(new PreviewStatus(PreviewStatus.Status.RUNNING, null, System.currentTimeMillis(), null));
            // Only have timer if there is a timeout setting.
            if (previewConfig.getTimeout() != null) {
                timer = new Timer();
                final int timeOutMinutes = previewConfig.getTimeout();
                timer.schedule(new TimerTask() {

                    @Override
                    public void run() {
                        try {
                            LOG.info("Stopping the preview since it has reached running time: {} mins.", timeOutMinutes);
                            stopPreview();
                            killedByTimer = true;
                        } catch (Exception e) {
                            LOG.debug("Error shutting down the preview run with id: {}", programId);
                        }
                    }
                }, timeOutMinutes * 60 * 1000);
            }
        }

        @Override
        public void completed() {
            setStatus(new PreviewStatus(PreviewStatus.Status.COMPLETED, null, status.getStartTime(), System.currentTimeMillis()));
            shutDownUnrequiredServices();
        }

        @Override
        public void killed() {
            if (!killedByTimer) {
                setStatus(new PreviewStatus(PreviewStatus.Status.KILLED, null, status.getStartTime(), System.currentTimeMillis()));
            } else {
                setStatus(new PreviewStatus(PreviewStatus.Status.KILLED_BY_TIMER, null, status.getStartTime(), System.currentTimeMillis()));
            }
            shutDownUnrequiredServices();
        }

        @Override
        public void error(Throwable cause) {
            setStatus(new PreviewStatus(PreviewStatus.Status.RUN_FAILED, new BasicThrowable(cause), status.getStartTime(), System.currentTimeMillis()));
            shutDownUnrequiredServices();
        }
    }, Threads.SAME_THREAD_EXECUTOR);
    runId = controller.getProgramRunId();
}
Also used : ProgramController(co.cask.cdap.app.runtime.ProgramController) ArtifactSummary(co.cask.cdap.api.artifact.ArtifactSummary) Timer(java.util.Timer) TimerTask(java.util.TimerTask) NamespaceMeta(co.cask.cdap.proto.NamespaceMeta) PreviewStatus(co.cask.cdap.app.preview.PreviewStatus) BasicThrowable(co.cask.cdap.proto.BasicThrowable) AbstractListener(co.cask.cdap.internal.app.runtime.AbstractListener) ApplicationId(co.cask.cdap.proto.id.ApplicationId) BasicThrowable(co.cask.cdap.proto.BasicThrowable) PreviewConfig(co.cask.cdap.proto.artifact.preview.PreviewConfig)

Aggregations

BasicThrowable (co.cask.cdap.proto.BasicThrowable)13 ProgramId (co.cask.cdap.proto.id.ProgramId)4 ProgramController (co.cask.cdap.app.runtime.ProgramController)3 AbstractListener (co.cask.cdap.internal.app.runtime.AbstractListener)3 ProgramRunStatus (co.cask.cdap.proto.ProgramRunStatus)3 WorkflowNodeStateDetail (co.cask.cdap.proto.WorkflowNodeStateDetail)3 BasicArguments (co.cask.cdap.internal.app.runtime.BasicArguments)2 SimpleProgramOptions (co.cask.cdap.internal.app.runtime.SimpleProgramOptions)2 ApplicationId (co.cask.cdap.proto.id.ApplicationId)2 ProgramRunId (co.cask.cdap.proto.id.ProgramRunId)2 Supplier (com.google.common.base.Supplier)2 JsonObject (com.google.gson.JsonObject)2 Nullable (javax.annotation.Nullable)2 RunId (org.apache.twill.api.RunId)2 ServiceListenerAdapter (org.apache.twill.internal.ServiceListenerAdapter)2 Test (org.junit.Test)2 ApplicationSpecification (co.cask.cdap.api.app.ApplicationSpecification)1 ArtifactSummary (co.cask.cdap.api.artifact.ArtifactSummary)1 DatasetContext (co.cask.cdap.api.data.DatasetContext)1 DatasetManagementException (co.cask.cdap.api.dataset.DatasetManagementException)1