Search in sources :

Example 41 with ProgramType

use of co.cask.cdap.proto.ProgramType in project cdap by caskdata.

the class ProgramLifecycleHttpHandler method programRunRecord.

/**
 * Returns run record for a particular run of a program of an app version.
 */
@GET
@Path("/apps/{app-name}/versions/{app-version}/{program-type}/{program-name}/runs/{run-id}")
public void programRunRecord(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @PathParam("app-name") String appName, @PathParam("app-version") String appVersion, @PathParam("program-type") String type, @PathParam("program-name") String programName, @PathParam("run-id") String runid) throws NotFoundException {
    ProgramType programType = getProgramType(type);
    if (programType == null || programType == ProgramType.WEBAPP) {
        throw new NotFoundException(String.format("Program run record is not supported for program type '%s'.", programType));
    }
    ProgramId progId = new ApplicationId(namespaceId, appName, appVersion).program(programType, programName);
    RunRecordMeta runRecordMeta = store.getRun(progId.run(runid));
    if (runRecordMeta != null) {
        RunRecord runRecord = RunRecord.builder(runRecordMeta).build();
        responder.sendJson(HttpResponseStatus.OK, GSON.toJson(runRecord));
        return;
    }
    throw new NotFoundException(progId.run(runid));
}
Also used : RunRecord(co.cask.cdap.proto.RunRecord) RunRecordMeta(co.cask.cdap.internal.app.store.RunRecordMeta) NamespaceNotFoundException(co.cask.cdap.common.NamespaceNotFoundException) NotFoundException(co.cask.cdap.common.NotFoundException) ProgramType(co.cask.cdap.proto.ProgramType) ProgramId(co.cask.cdap.proto.id.ProgramId) ApplicationId(co.cask.cdap.proto.id.ApplicationId) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 42 with ProgramType

use of co.cask.cdap.proto.ProgramType 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 = ProgramType.valueOfCategoryName(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 endpointStrategy = new RandomEndpointStrategy(discoveryServiceClient.discover(discoverableName));
    if (endpointStrategy.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(co.cask.cdap.common.discovery.RandomEndpointStrategy) EndpointStrategy(co.cask.cdap.common.discovery.EndpointStrategy) BadRequestException(co.cask.cdap.common.BadRequestException) ProgramType(co.cask.cdap.proto.ProgramType) ServiceUnavailableException(co.cask.cdap.common.ServiceUnavailableException) ProgramId(co.cask.cdap.proto.id.ProgramId) ApplicationId(co.cask.cdap.proto.id.ApplicationId) ProgramStatus(co.cask.cdap.proto.ProgramStatus) BatchProgramStatus(co.cask.cdap.proto.BatchProgramStatus) RandomEndpointStrategy(co.cask.cdap.common.discovery.RandomEndpointStrategy) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 43 with ProgramType

use of co.cask.cdap.proto.ProgramType in project cdap by caskdata.

the class Schedulers method toProgramSchedule.

public static ProgramSchedule toProgramSchedule(ApplicationId appId, ScheduleSpecification spec) {
    Schedule schedule = spec.getSchedule();
    ProgramType programType = ProgramType.valueOfSchedulableType(spec.getProgram().getProgramType());
    ProgramId programId = appId.program(programType, spec.getProgram().getProgramName());
    Trigger trigger;
    if (schedule instanceof TimeSchedule) {
        TimeSchedule timeSchedule = (TimeSchedule) schedule;
        trigger = new TimeTrigger(timeSchedule.getCronEntry());
    } else {
        StreamSizeSchedule streamSchedule = (StreamSizeSchedule) schedule;
        StreamId streamId = programId.getNamespaceId().stream(streamSchedule.getStreamName());
        trigger = new StreamSizeTrigger(streamId, streamSchedule.getDataTriggerMB());
    }
    Integer maxConcurrentRuns = schedule.getRunConstraints().getMaxConcurrentRuns();
    List<Constraint> constraints = maxConcurrentRuns == null ? ImmutableList.<Constraint>of() : ImmutableList.<Constraint>of(new ConcurrencyConstraint(maxConcurrentRuns));
    return new ProgramSchedule(schedule.getName(), schedule.getDescription(), programId, spec.getProperties(), trigger, constraints);
}
Also used : StreamId(co.cask.cdap.proto.id.StreamId) ConcurrencyConstraint(co.cask.cdap.internal.app.runtime.schedule.constraint.ConcurrencyConstraint) TimeTrigger(co.cask.cdap.internal.app.runtime.schedule.trigger.TimeTrigger) Constraint(co.cask.cdap.internal.schedule.constraint.Constraint) ConcurrencyConstraint(co.cask.cdap.internal.app.runtime.schedule.constraint.ConcurrencyConstraint) ProgramId(co.cask.cdap.proto.id.ProgramId) StreamSizeTrigger(co.cask.cdap.internal.app.runtime.schedule.trigger.StreamSizeTrigger) TimeTrigger(co.cask.cdap.internal.app.runtime.schedule.trigger.TimeTrigger) Trigger(co.cask.cdap.internal.schedule.trigger.Trigger) ProgramSchedule(co.cask.cdap.internal.app.runtime.schedule.ProgramSchedule) StreamSizeSchedule(co.cask.cdap.internal.schedule.StreamSizeSchedule) Schedule(co.cask.cdap.api.schedule.Schedule) ProgramSchedule(co.cask.cdap.internal.app.runtime.schedule.ProgramSchedule) TimeSchedule(co.cask.cdap.internal.schedule.TimeSchedule) StreamSizeTrigger(co.cask.cdap.internal.app.runtime.schedule.trigger.StreamSizeTrigger) TimeSchedule(co.cask.cdap.internal.schedule.TimeSchedule) ProgramType(co.cask.cdap.proto.ProgramType) StreamSizeSchedule(co.cask.cdap.internal.schedule.StreamSizeSchedule)

Example 44 with ProgramType

use of co.cask.cdap.proto.ProgramType in project cdap by caskdata.

the class ProgramLifecycleHttpHandler method saveProgramRuntimeArgs.

/**
   * Save program runtime args.
   */
@PUT
@Path("/apps/{app-name}/{program-type}/{program-name}/runtimeargs")
@AuditPolicy(AuditDetail.REQUEST_BODY)
public void saveProgramRuntimeArgs(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @PathParam("app-name") String appName, @PathParam("program-type") String type, @PathParam("program-name") String programName) throws Exception {
    ProgramType programType = getProgramType(type);
    ProgramId programId = new ProgramId(namespaceId, appName, programType, programName);
    saveProgramIdRuntimeArgs(programId, request, responder);
}
Also used : ProgramType(co.cask.cdap.proto.ProgramType) ProgramId(co.cask.cdap.proto.id.ProgramId) Path(javax.ws.rs.Path) AuditPolicy(co.cask.cdap.common.security.AuditPolicy) PUT(javax.ws.rs.PUT)

Example 45 with ProgramType

use of co.cask.cdap.proto.ProgramType in project cdap by caskdata.

the class ProgramLifecycleHttpHandler method resetLogLevels.

private void resetLogLevels(HttpRequest request, HttpResponder responder, String namespace, String appName, String appVersion, String type, String programName, @Nullable String component, String runId) throws Exception {
    ProgramType programType = getProgramType(type);
    if (programType == null) {
        throw new BadRequestException("Invalid program type provided");
    }
    try {
        Set<String> loggerNames = parseBody(request, SET_STRING_TYPE);
        lifecycleService.resetProgramLogLevels(new ApplicationId(namespace, appName, appVersion).program(programType, programName), loggerNames == null ? Collections.<String>emptySet() : loggerNames, component, runId);
        responder.sendStatus(HttpResponseStatus.OK);
    } catch (JsonSyntaxException e) {
        throw new BadRequestException("Invalid JSON in body");
    } catch (SecurityException e) {
        throw new UnauthorizedException("Unauthorized to reset the log levels");
    }
}
Also used : JsonSyntaxException(com.google.gson.JsonSyntaxException) UnauthorizedException(co.cask.cdap.security.spi.authorization.UnauthorizedException) BadRequestException(co.cask.cdap.common.BadRequestException) ProgramType(co.cask.cdap.proto.ProgramType) ApplicationId(co.cask.cdap.proto.id.ApplicationId)

Aggregations

ProgramType (co.cask.cdap.proto.ProgramType)71 ProgramId (co.cask.cdap.proto.id.ProgramId)33 ApplicationId (co.cask.cdap.proto.id.ApplicationId)22 ApplicationSpecification (co.cask.cdap.api.app.ApplicationSpecification)21 Path (javax.ws.rs.Path)16 BadRequestException (co.cask.cdap.common.BadRequestException)13 GET (javax.ws.rs.GET)13 NamespaceNotFoundException (co.cask.cdap.common.NamespaceNotFoundException)12 NotFoundException (co.cask.cdap.common.NotFoundException)12 RunId (org.apache.twill.api.RunId)11 ProgramController (co.cask.cdap.app.runtime.ProgramController)8 ArrayList (java.util.ArrayList)8 ProgramContextAware (co.cask.cdap.data.ProgramContextAware)6 BasicProgramContext (co.cask.cdap.internal.app.runtime.BasicProgramContext)6 FlowSpecification (co.cask.cdap.api.flow.FlowSpecification)5 UnauthorizedException (co.cask.cdap.security.spi.authorization.UnauthorizedException)5 JsonSyntaxException (com.google.gson.JsonSyntaxException)5 MetricsCollectionService (co.cask.cdap.api.metrics.MetricsCollectionService)4 WorkflowSpecification (co.cask.cdap.api.workflow.WorkflowSpecification)4 PluginInstantiator (co.cask.cdap.internal.app.runtime.plugin.PluginInstantiator)4