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());
}
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);
}
}
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;
}
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();
}
}
}
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());
}
Aggregations