Search in sources :

Example 6 with RuntimeInfo

use of co.cask.cdap.app.runtime.ProgramRuntimeService.RuntimeInfo in project cdap by caskdata.

the class ProgramLifecycleServiceTest method testInvalidFlowRunRecord.

@Test
public void testInvalidFlowRunRecord() throws Exception {
    // Create App with Flow and the deploy
    HttpResponse response = deploy(WordCountApp.class, Constants.Gateway.API_VERSION_3_TOKEN, TEST_NAMESPACE1);
    Assert.assertEquals(200, response.getStatusLine().getStatusCode());
    final Id.Program wordcountFlow1 = Id.Program.from(TEST_NAMESPACE1, "WordCountApp", ProgramType.FLOW, "WordCountFlow");
    // flow is stopped initially
    Assert.assertEquals("STOPPED", getProgramStatus(wordcountFlow1));
    // start a flow and check the status
    startProgram(wordcountFlow1);
    waitState(wordcountFlow1, ProgramRunStatus.RUNNING.toString());
    // Wait until we have a run record
    Tasks.waitFor(1, new Callable<Integer>() {

        @Override
        public Integer call() throws Exception {
            return getProgramRuns(wordcountFlow1, ProgramRunStatus.RUNNING.toString()).size();
        }
    }, 5, TimeUnit.SECONDS);
    // Get the RunRecord
    List<RunRecord> runRecords = getProgramRuns(wordcountFlow1, ProgramRunStatus.RUNNING.toString());
    Assert.assertEquals(1, runRecords.size());
    final RunRecord rr = runRecords.get(0);
    // Check the RunRecords status
    Assert.assertEquals(ProgramRunStatus.RUNNING, rr.getStatus());
    // Lets set the runtime info to off
    RuntimeInfo runtimeInfo = runtimeService.lookup(wordcountFlow1.toEntityId(), RunIds.fromString(rr.getPid()));
    ProgramController programController = runtimeInfo.getController();
    programController.stop();
    // Verify that the status of that run is KILLED
    Tasks.waitFor(ProgramRunStatus.KILLED, new Callable<ProgramRunStatus>() {

        @Override
        public ProgramRunStatus call() throws Exception {
            RunRecordMeta runRecord = store.getRun(wordcountFlow1.toEntityId(), rr.getPid());
            return runRecord == null ? null : runRecord.getStatus();
        }
    }, 5, TimeUnit.SECONDS, 100, TimeUnit.MILLISECONDS);
    // Use the store manipulate state to be RUNNING
    long now = System.currentTimeMillis();
    long nowSecs = TimeUnit.MILLISECONDS.toSeconds(now);
    store.setStart(wordcountFlow1.toEntityId(), rr.getPid(), nowSecs);
    // Now check again via Store to assume data store is wrong.
    RunRecord runRecordMeta = store.getRun(wordcountFlow1.toEntityId(), rr.getPid());
    Assert.assertNotNull(runRecordMeta);
    Assert.assertEquals(ProgramRunStatus.RUNNING, runRecordMeta.getStatus());
    // Verify there is NO FAILED run record for the application
    runRecords = getProgramRuns(wordcountFlow1, ProgramRunStatus.FAILED.toString());
    Assert.assertEquals(0, runRecords.size());
    // Lets fix it
    Set<String> processedInvalidRunRecordIds = Sets.newHashSet();
    programLifecycleService.validateAndCorrectRunningRunRecords(ProgramType.FLOW, processedInvalidRunRecordIds);
    // Verify there is one FAILED run record for the application
    runRecords = getProgramRuns(wordcountFlow1, ProgramRunStatus.FAILED.toString());
    Assert.assertEquals(1, runRecords.size());
    Assert.assertEquals(ProgramRunStatus.FAILED, runRecords.get(0).getStatus());
}
Also used : ProgramController(co.cask.cdap.app.runtime.ProgramController) RuntimeInfo(co.cask.cdap.app.runtime.ProgramRuntimeService.RuntimeInfo) RunRecordMeta(co.cask.cdap.internal.app.store.RunRecordMeta) HttpResponse(org.apache.http.HttpResponse) RunRecord(co.cask.cdap.proto.RunRecord) ProgramRunStatus(co.cask.cdap.proto.ProgramRunStatus) Id(co.cask.cdap.proto.Id) Test(org.junit.Test)

Example 7 with RuntimeInfo

use of co.cask.cdap.app.runtime.ProgramRuntimeService.RuntimeInfo in project cdap by caskdata.

the class ProgramLifecycleService method updateLogLevels.

/**
   * Helper method to update log levels for Worker, Flow, or Service.
   */
private void updateLogLevels(ProgramId programId, Map<String, LogEntry.Level> logLevels, @Nullable String component, @Nullable String runId) throws Exception {
    List<ProgramRuntimeService.RuntimeInfo> runtimeInfos = findRuntimeInfo(programId, runId);
    ProgramRuntimeService.RuntimeInfo runtimeInfo = runtimeInfos.isEmpty() ? null : runtimeInfos.get(0);
    if (runtimeInfo != null) {
        LogLevelUpdater logLevelUpdater = getLogLevelUpdater(runtimeInfo);
        logLevelUpdater.updateLogLevels(logLevels, component);
    }
}
Also used : RuntimeInfo(co.cask.cdap.app.runtime.ProgramRuntimeService.RuntimeInfo) RuntimeInfo(co.cask.cdap.app.runtime.ProgramRuntimeService.RuntimeInfo) LogLevelUpdater(co.cask.cdap.app.runtime.LogLevelUpdater) ProgramRuntimeService(co.cask.cdap.app.runtime.ProgramRuntimeService)

Example 8 with RuntimeInfo

use of co.cask.cdap.app.runtime.ProgramRuntimeService.RuntimeInfo in project cdap by caskdata.

the class ProgramLifecycleService method shouldCorrectForWorkflowChildren.

/**
   * Helper method to check if the run record is a child program of a Workflow
   *
   * @param runRecordMeta The target {@link RunRecordMeta} to check
   * @param processedInvalidRunRecordIds the {@link Set} of processed invalid run record ids.
   * @return {@code true} of we should check and {@code false} otherwise
   */
private boolean shouldCorrectForWorkflowChildren(RunRecordMeta runRecordMeta, Set<String> processedInvalidRunRecordIds) {
    // check if it is part of workflow because it may not have actual runtime info
    if (runRecordMeta.getProperties() != null && runRecordMeta.getProperties().get("workflowrunid") != null) {
        // Get the parent Workflow info
        String workflowRunId = runRecordMeta.getProperties().get("workflowrunid");
        if (!processedInvalidRunRecordIds.contains(workflowRunId)) {
            // If the parent workflow has not been processed, then check if it still valid
            ProgramId workflowProgramId = retrieveProgramIdForRunRecord(ProgramType.WORKFLOW, workflowRunId);
            if (workflowProgramId != null) {
                // lets see if the parent workflow run records state is still running
                RunRecordMeta wfRunRecord = store.getRun(workflowProgramId, workflowRunId);
                RuntimeInfo wfRuntimeInfo = runtimeService.lookup(workflowProgramId, RunIds.fromString(workflowRunId));
                // then do not update it
                if (wfRunRecord != null && wfRunRecord.getStatus() == ProgramRunStatus.RUNNING && wfRuntimeInfo != null) {
                    return false;
                }
            }
        }
    }
    return true;
}
Also used : RuntimeInfo(co.cask.cdap.app.runtime.ProgramRuntimeService.RuntimeInfo) RunRecordMeta(co.cask.cdap.internal.app.store.RunRecordMeta) ProgramId(co.cask.cdap.proto.id.ProgramId)

Example 9 with RuntimeInfo

use of co.cask.cdap.app.runtime.ProgramRuntimeService.RuntimeInfo in project cdap by caskdata.

the class ProgramLifecycleService method setServiceInstances.

private void setServiceInstances(ProgramId programId, int instances) throws ExecutionException, InterruptedException, BadRequestException {
    int oldInstances = store.getServiceInstances(programId);
    if (oldInstances != instances) {
        store.setServiceInstances(programId, instances);
        ProgramRuntimeService.RuntimeInfo runtimeInfo = findRuntimeInfo(programId);
        if (runtimeInfo != null) {
            runtimeInfo.getController().command(ProgramOptionConstants.INSTANCES, ImmutableMap.of("runnable", programId.getProgram(), "newInstances", String.valueOf(instances), "oldInstances", String.valueOf(oldInstances))).get();
        }
    }
}
Also used : RuntimeInfo(co.cask.cdap.app.runtime.ProgramRuntimeService.RuntimeInfo) ProgramRuntimeService(co.cask.cdap.app.runtime.ProgramRuntimeService)

Example 10 with RuntimeInfo

use of co.cask.cdap.app.runtime.ProgramRuntimeService.RuntimeInfo in project cdap by caskdata.

the class ProgramLifecycleService method findRuntimeInfo.

private List<ProgramRuntimeService.RuntimeInfo> findRuntimeInfo(ProgramId programId, @Nullable String runId) throws BadRequestException {
    if (runId != null) {
        RunId run;
        try {
            run = RunIds.fromString(runId);
        } catch (IllegalArgumentException e) {
            throw new BadRequestException("Error parsing run-id.", e);
        }
        ProgramRuntimeService.RuntimeInfo runtimeInfo = runtimeService.lookup(programId, run);
        return runtimeInfo == null ? Collections.<RuntimeInfo>emptyList() : Collections.singletonList(runtimeInfo);
    }
    return new ArrayList<>(runtimeService.list(programId).values());
}
Also used : ArrayList(java.util.ArrayList) BadRequestException(co.cask.cdap.common.BadRequestException) RuntimeInfo(co.cask.cdap.app.runtime.ProgramRuntimeService.RuntimeInfo) RunId(org.apache.twill.api.RunId) ProgramRunId(co.cask.cdap.proto.id.ProgramRunId) ProgramRuntimeService(co.cask.cdap.app.runtime.ProgramRuntimeService)

Aggregations

RuntimeInfo (co.cask.cdap.app.runtime.ProgramRuntimeService.RuntimeInfo)12 ProgramRuntimeService (co.cask.cdap.app.runtime.ProgramRuntimeService)9 RunRecordMeta (co.cask.cdap.internal.app.store.RunRecordMeta)4 ProgramRunId (co.cask.cdap.proto.id.ProgramRunId)3 LogLevelUpdater (co.cask.cdap.app.runtime.LogLevelUpdater)2 ProgramController (co.cask.cdap.app.runtime.ProgramController)2 ApplicationNotFoundException (co.cask.cdap.common.ApplicationNotFoundException)2 BadRequestException (co.cask.cdap.common.BadRequestException)2 NotFoundException (co.cask.cdap.common.NotFoundException)2 ProgramNotFoundException (co.cask.cdap.common.ProgramNotFoundException)2 ProgramId (co.cask.cdap.proto.id.ProgramId)2 ArrayList (java.util.ArrayList)2 RunId (org.apache.twill.api.RunId)2 Predicate (co.cask.cdap.api.Predicate)1 ProgramSpecification (co.cask.cdap.api.ProgramSpecification)1 FlowSpecification (co.cask.cdap.api.flow.FlowSpecification)1 ProgramDescriptor (co.cask.cdap.app.program.ProgramDescriptor)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