Search in sources :

Example 1 with ProgramStatus

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);
    }
}
Also used : ConflictException(io.cdap.cdap.common.ConflictException) NotFoundException(io.cdap.cdap.common.NotFoundException) ProgramStatus(io.cdap.cdap.proto.ProgramStatus)

Example 2 with ProgramStatus

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);
    }
}
Also used : ConflictException(io.cdap.cdap.common.ConflictException) NotFoundException(io.cdap.cdap.common.NotFoundException) ProgramId(io.cdap.cdap.proto.id.ProgramId) ProgramStatus(io.cdap.cdap.proto.ProgramStatus)

Example 3 with ProgramStatus

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);
}
Also used : ArtifactVersion(io.cdap.cdap.api.artifact.ArtifactVersion) ArtifactId(io.cdap.cdap.api.artifact.ArtifactId) RunRecordDetail(io.cdap.cdap.internal.app.store.RunRecordDetail) ProgramStatus(io.cdap.cdap.proto.ProgramStatus) Test(org.junit.Test)

Example 4 with ProgramStatus

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);
}
Also used : ArtifactVersion(io.cdap.cdap.api.artifact.ArtifactVersion) ArtifactId(io.cdap.cdap.api.artifact.ArtifactId) RunRecordDetail(io.cdap.cdap.internal.app.store.RunRecordDetail) ProgramId(io.cdap.cdap.proto.id.ProgramId) ProgramStatus(io.cdap.cdap.proto.ProgramStatus) Test(org.junit.Test)

Example 5 with ProgramStatus

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.");
}
Also used : RandomEndpointStrategy(io.cdap.cdap.common.discovery.RandomEndpointStrategy) EndpointStrategy(io.cdap.cdap.common.discovery.EndpointStrategy) BadRequestException(io.cdap.cdap.common.BadRequestException) ProgramType(io.cdap.cdap.proto.ProgramType) ServiceUnavailableException(io.cdap.cdap.common.ServiceUnavailableException) ProgramId(io.cdap.cdap.proto.id.ProgramId) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) ProgramStatus(io.cdap.cdap.proto.ProgramStatus) BatchProgramStatus(io.cdap.cdap.proto.BatchProgramStatus) RandomEndpointStrategy(io.cdap.cdap.common.discovery.RandomEndpointStrategy) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Aggregations

ProgramStatus (io.cdap.cdap.proto.ProgramStatus)8 ProgramId (io.cdap.cdap.proto.id.ProgramId)6 NotFoundException (io.cdap.cdap.common.NotFoundException)5 ConflictException (io.cdap.cdap.common.ConflictException)4 BatchProgramStatus (io.cdap.cdap.proto.BatchProgramStatus)4 ProgramType (io.cdap.cdap.proto.ProgramType)4 GET (javax.ws.rs.GET)4 Path (javax.ws.rs.Path)4 BadRequestException (io.cdap.cdap.common.BadRequestException)3 NamespaceNotFoundException (io.cdap.cdap.common.NamespaceNotFoundException)3 ServiceUnavailableException (io.cdap.cdap.common.ServiceUnavailableException)3 RunRecordDetail (io.cdap.cdap.internal.app.store.RunRecordDetail)3 ApplicationId (io.cdap.cdap.proto.id.ApplicationId)3 JsonObject (com.google.gson.JsonObject)2 JsonSyntaxException (com.google.gson.JsonSyntaxException)2 ArtifactId (io.cdap.cdap.api.artifact.ArtifactId)2 ArtifactVersion (io.cdap.cdap.api.artifact.ArtifactVersion)2 NotImplementedException (io.cdap.cdap.common.NotImplementedException)2 EndpointStrategy (io.cdap.cdap.common.discovery.EndpointStrategy)2 RandomEndpointStrategy (io.cdap.cdap.common.discovery.RandomEndpointStrategy)2