use of co.cask.cdap.proto.ProgramRunStatus in project cdap by caskdata.
the class MapReduceProgramRunner method createRuntimeServiceListener.
/**
* Creates a service listener to reactor on state changes on {@link MapReduceRuntimeService}.
*/
private Service.Listener createRuntimeServiceListener(final ProgramId programId, final RunId runId, final Iterable<Closeable> closeables, final Arguments arguments, final Arguments userArgs) {
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) {
closeAllQuietly(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) {
closeAllQuietly(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));
}
};
}
use of co.cask.cdap.proto.ProgramRunStatus 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.proto.ProgramRunStatus in project cdap by caskdata.
the class ProgramLifecycleHttpHandler method getRuns.
private void getRuns(HttpResponder responder, ProgramId programId, String status, long start, long end, int limit) throws BadRequestException {
try {
ProgramRunStatus runStatus = (status == null) ? ProgramRunStatus.ALL : ProgramRunStatus.valueOf(status.toUpperCase());
Collection<RunRecord> records = Collections2.transform(store.getRuns(programId, runStatus, start, end, limit).values(), CONVERT_TO_RUN_RECORD);
responder.sendJson(HttpResponseStatus.OK, records);
} catch (IllegalArgumentException e) {
throw new BadRequestException(String.format("Invalid status %s. Supported options for status of runs are " + "running/completed/failed", status));
}
}
use of co.cask.cdap.proto.ProgramRunStatus 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));
}
};
}
use of co.cask.cdap.proto.ProgramRunStatus in project cdap by caskdata.
the class AppMetadataStore method recordProgramSuspendResume.
private void recordProgramSuspendResume(ProgramId programId, String pid, String action) {
String fromType = TYPE_RUN_RECORD_STARTED;
String toType = TYPE_RUN_RECORD_SUSPENDED;
ProgramRunStatus toStatus = ProgramRunStatus.SUSPENDED;
if (action.equals("resume")) {
fromType = TYPE_RUN_RECORD_SUSPENDED;
toType = TYPE_RUN_RECORD_STARTED;
toStatus = ProgramRunStatus.RUNNING;
}
MDSKey key = getProgramKeyBuilder(fromType, programId).add(pid).build();
RunRecordMeta record = get(key, RunRecordMeta.class);
// Check without the version string only for default version
if (!upgradeComplete.get() && record == null && (programId.getVersion().equals(ApplicationId.DEFAULT_VERSION))) {
key = getVersionLessProgramKeyBuilder(fromType, programId).add(pid).build();
record = get(key, RunRecordMeta.class);
}
if (record == null) {
String msg = String.format("No meta for %s run record for namespace %s app %s program type %s " + "program %s pid %s exists", action.equals("suspend") ? "started" : "suspended", programId.getNamespace(), programId.getApplication(), programId.getType().name(), programId.getProgram(), pid);
LOG.error(msg);
throw new IllegalArgumentException(msg);
}
// Since the key contains the RunId/PID in addition to the programId, it is ok to deleteAll.
deleteAll(key);
key = getProgramKeyBuilder(toType, programId).add(pid).build();
write(key, new RunRecordMeta(record, null, toStatus));
}
Aggregations