use of io.cdap.cdap.proto.ProgramStatus in project cdap by caskdata.
the class SystemAppEnableExecutor method startProgram.
private void startProgram(ProgramId programId) throws Exception {
try {
ProgramStatus currentStatus = programLifecycleService.getProgramStatus(programId);
// checking if artifact version has changed would be a good start.
try {
if (currentStatus == ProgramStatus.RUNNING) {
programLifecycleService.stop(programId);
}
} catch (ConflictException e) {
// Will reach here if the program is already stopped, which means it tried to stop after the status check above.
// ignore this, as it means the program is stopped as we wanted.
}
programLifecycleService.run(programId, Collections.emptyMap(), false);
} catch (ConflictException e) {
// thrown if the program is already running, which means it was started after the status check above.
// ignore this, as it means the program is running as expected
} catch (NotFoundException e) {
// use a nicer error message
throw new IllegalArgumentException(String.format("Cannot start %s because it does not exist.", programId), e);
}
}
use of io.cdap.cdap.proto.ProgramStatus in project cdap by caskdata.
the class ProgramStarter method execute.
@Override
public void execute(Arguments arguments) throws Exception {
ProgramId programId = arguments.getId();
Preconditions.checkArgument(programLifecycleService.getProgramSpecification(programId) != null, "Cannot start %s because it does not exist.", programId);
try {
// do nothing if the program is already running
ProgramStatus currentStatus = programLifecycleService.getProgramStatus(programId);
if (currentStatus != ProgramStatus.STOPPED) {
LOG.info("Program {} is in the {} state, skipping start program bootstrap step.", programId, currentStatus);
return;
}
programLifecycleService.run(programId, Collections.emptyMap(), false);
} catch (ConflictException e) {
// thrown if the program is already running, which means it was started after the status check above.
// ignore this, as it means the program is running as expected
} catch (NotFoundException e) {
// use a nicer error message
throw new IllegalArgumentException(String.format("Cannot start %s because it does not exist.", programId), e);
}
}
use of io.cdap.cdap.proto.ProgramStatus in project cdap by caskdata.
the class ProgramLifecycleServiceTest method testProgramStatusFromSingleRun.
@Test
public void testProgramStatusFromSingleRun() {
RunRecordDetail record = RunRecordDetail.builder().setProgramRunId(NamespaceId.DEFAULT.app("app").mr("mr").run(RunIds.generate())).setStartTime(System.currentTimeMillis()).setArtifactId(new ArtifactId("r", new ArtifactVersion("1.0"), ArtifactScope.USER)).setStatus(ProgramRunStatus.PENDING).setSourceId(new byte[] { 0 }).build();
// pending or starting -> starting
ProgramStatus status = ProgramLifecycleService.getProgramStatus(Collections.singleton(record));
Assert.assertEquals(ProgramStatus.STARTING, status);
record = RunRecordDetail.builder(record).setStatus(ProgramRunStatus.STARTING).build();
status = ProgramLifecycleService.getProgramStatus(Collections.singleton(record));
Assert.assertEquals(ProgramStatus.STARTING, status);
// running, suspended, resuming -> running
record = RunRecordDetail.builder(record).setStatus(ProgramRunStatus.RUNNING).build();
status = ProgramLifecycleService.getProgramStatus(Collections.singleton(record));
Assert.assertEquals(ProgramStatus.RUNNING, status);
record = RunRecordDetail.builder(record).setStatus(ProgramRunStatus.SUSPENDED).build();
status = ProgramLifecycleService.getProgramStatus(Collections.singleton(record));
Assert.assertEquals(ProgramStatus.RUNNING, status);
// failed, killed, completed -> stopped
record = RunRecordDetail.builder(record).setStatus(ProgramRunStatus.FAILED).build();
status = ProgramLifecycleService.getProgramStatus(Collections.singleton(record));
Assert.assertEquals(ProgramStatus.STOPPED, status);
record = RunRecordDetail.builder(record).setStatus(ProgramRunStatus.KILLED).build();
status = ProgramLifecycleService.getProgramStatus(Collections.singleton(record));
Assert.assertEquals(ProgramStatus.STOPPED, status);
record = RunRecordDetail.builder(record).setStatus(ProgramRunStatus.COMPLETED).build();
status = ProgramLifecycleService.getProgramStatus(Collections.singleton(record));
Assert.assertEquals(ProgramStatus.STOPPED, status);
}
use of io.cdap.cdap.proto.ProgramStatus in project cdap by caskdata.
the class ProgramLifecycleServiceTest method testProgramStatusFromMultipleRuns.
@Test
public void testProgramStatusFromMultipleRuns() {
ProgramId programId = NamespaceId.DEFAULT.app("app").mr("mr");
RunRecordDetail pending = RunRecordDetail.builder().setProgramRunId(programId.run(RunIds.generate())).setStartTime(System.currentTimeMillis()).setArtifactId(new ArtifactId("r", new ArtifactVersion("1.0"), ArtifactScope.USER)).setStatus(ProgramRunStatus.PENDING).setSourceId(new byte[] { 0 }).build();
RunRecordDetail starting = RunRecordDetail.builder(pending).setProgramRunId(programId.run(RunIds.generate())).setStatus(ProgramRunStatus.STARTING).build();
RunRecordDetail running = RunRecordDetail.builder(pending).setProgramRunId(programId.run(RunIds.generate())).setStatus(ProgramRunStatus.RUNNING).build();
RunRecordDetail killed = RunRecordDetail.builder(pending).setProgramRunId(programId.run(RunIds.generate())).setStatus(ProgramRunStatus.KILLED).build();
RunRecordDetail failed = RunRecordDetail.builder(pending).setProgramRunId(programId.run(RunIds.generate())).setStatus(ProgramRunStatus.FAILED).build();
RunRecordDetail completed = RunRecordDetail.builder(pending).setProgramRunId(programId.run(RunIds.generate())).setStatus(ProgramRunStatus.COMPLETED).build();
// running takes precedence over others
ProgramStatus status = ProgramLifecycleService.getProgramStatus(Arrays.asList(pending, starting, running, killed, failed, completed));
Assert.assertEquals(ProgramStatus.RUNNING, status);
// starting takes precedence over stopped
status = ProgramLifecycleService.getProgramStatus(Arrays.asList(pending, killed, failed, completed));
Assert.assertEquals(ProgramStatus.STARTING, status);
status = ProgramLifecycleService.getProgramStatus(Arrays.asList(starting, killed, failed, completed));
Assert.assertEquals(ProgramStatus.STARTING, status);
// end states are stopped
status = ProgramLifecycleService.getProgramStatus(Arrays.asList(killed, failed, completed));
Assert.assertEquals(ProgramStatus.STOPPED, status);
}
use of io.cdap.cdap.proto.ProgramStatus in project cdap by caskdata.
the class ProgramLifecycleHttpHandler method getServiceAvailability.
/**
* Return the availability (i.e. discoverable registration) status of a service.
*/
@GET
@Path("/apps/{app-name}/versions/{app-version}/{service-type}/{program-name}/available")
public void getServiceAvailability(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @PathParam("app-name") String appName, @PathParam("app-version") String appVersion, @PathParam("service-type") String serviceType, @PathParam("program-name") String programName) throws Exception {
// Currently we only support services and sparks as the service-type
ProgramType programType = getProgramType(serviceType);
if (!ServiceDiscoverable.getUserServiceTypes().contains(programType)) {
throw new BadRequestException("Only service or spark is support for service availability check");
}
ProgramId programId = new ProgramId(new ApplicationId(namespaceId, appName, appVersion), programType, programName);
ProgramStatus status = lifecycleService.getProgramStatus(programId);
if (status == ProgramStatus.STOPPED) {
throw new ServiceUnavailableException(programId.toString(), "Service is stopped. Please start it.");
}
// Construct discoverable name and return 200 OK if discoverable is present. If not return 503.
String discoverableName = ServiceDiscoverable.getName(programId);
// TODO: CDAP-12959 - Should use the UserServiceEndpointStrategy and discover based on the version
// and have appVersion nullable for the non versioned endpoint
EndpointStrategy strategy = new RandomEndpointStrategy(() -> discoveryServiceClient.discover(discoverableName));
if (strategy.pick(300L, TimeUnit.MILLISECONDS) == null) {
LOG.trace("Discoverable endpoint {} not found", discoverableName);
throw new ServiceUnavailableException(programId.toString(), "Service is running but not accepting requests at this time.");
}
responder.sendString(HttpResponseStatus.OK, "Service is available to accept requests.");
}
Aggregations