Search in sources :

Example 11 with ProgramId

use of co.cask.cdap.proto.id.ProgramId in project cdap by caskdata.

the class ProgramLifecycleHttpHandler method doAddSchedule.

private void doAddSchedule(HttpRequest request, HttpResponder responder, String namespace, String appName, String appVersion, String scheduleName) throws Exception {
    final ApplicationId applicationId = new ApplicationId(namespace, appName, appVersion);
    ScheduleDetail scheduleFromRequest = readScheduleDetailBody(request, scheduleName, false, new Function<JsonElement, ScheduleDetail>() {

        @Override
        public ScheduleDetail apply(@Nullable JsonElement input) {
            ScheduleSpecification scheduleSpec = GSON.fromJson(input, ScheduleSpecification.class);
            return toScheduleDetail(applicationId, scheduleSpec);
        }
    });
    if (scheduleFromRequest.getProgram() == null) {
        throw new BadRequestException("No program was specified for the schedule");
    }
    if (scheduleFromRequest.getProgram().getProgramType() == null) {
        throw new BadRequestException("No program type was specified for the schedule");
    }
    if (scheduleFromRequest.getProgram().getProgramName() == null) {
        throw new BadRequestException("No program name was specified for the schedule");
    }
    if (scheduleFromRequest.getTrigger() == null) {
        throw new BadRequestException("No trigger was specified for the schedule");
    }
    ProgramType programType = ProgramType.valueOfSchedulableType(scheduleFromRequest.getProgram().getProgramType());
    String programName = scheduleFromRequest.getProgram().getProgramName();
    ProgramId programId = applicationId.program(programType, programName);
    if (lifecycleService.getProgramSpecification(programId) == null) {
        throw new NotFoundException(programId);
    }
    String description = Objects.firstNonNull(scheduleFromRequest.getDescription(), "");
    Map<String, String> properties = Objects.firstNonNull(scheduleFromRequest.getProperties(), EMPTY_PROPERTIES);
    List<? extends Constraint> constraints = Objects.firstNonNull(scheduleFromRequest.getConstraints(), NO_CONSTRAINTS);
    long timeoutMillis = Objects.firstNonNull(scheduleFromRequest.getTimeoutMillis(), Schedulers.JOB_QUEUE_TIMEOUT_MILLIS);
    ProgramSchedule schedule = new ProgramSchedule(scheduleName, description, programId, properties, scheduleFromRequest.getTrigger(), constraints, timeoutMillis);
    programScheduler.addSchedule(schedule);
    responder.sendStatus(HttpResponseStatus.OK);
}
Also used : NamespaceNotFoundException(co.cask.cdap.common.NamespaceNotFoundException) NotFoundException(co.cask.cdap.common.NotFoundException) ProgramId(co.cask.cdap.proto.id.ProgramId) ProgramSchedule(co.cask.cdap.internal.app.runtime.schedule.ProgramSchedule) JsonElement(com.google.gson.JsonElement) BadRequestException(co.cask.cdap.common.BadRequestException) ScheduleDetail(co.cask.cdap.proto.ScheduleDetail) ProgramType(co.cask.cdap.proto.ProgramType) ApplicationId(co.cask.cdap.proto.id.ApplicationId) ScheduleSpecification(co.cask.cdap.api.schedule.ScheduleSpecification)

Example 12 with ProgramId

use of co.cask.cdap.proto.id.ProgramId in project cdap by caskdata.

the class ProgramLifecycleHttpHandler method combineForUpdate.

private ProgramSchedule combineForUpdate(ScheduleDetail scheduleDetail, ProgramSchedule existing) throws BadRequestException {
    String description = Objects.firstNonNull(scheduleDetail.getDescription(), existing.getDescription());
    ProgramId programId = scheduleDetail.getProgram() == null ? existing.getProgramId() : existing.getProgramId().getParent().program(scheduleDetail.getProgram().getProgramType() == null ? existing.getProgramId().getType() : ProgramType.valueOfSchedulableType(scheduleDetail.getProgram().getProgramType()), Objects.firstNonNull(scheduleDetail.getProgram().getProgramName(), existing.getProgramId().getProgram()));
    if (!programId.equals(existing.getProgramId())) {
        throw new BadRequestException(String.format("Must update the schedule '%s' with the same program as '%s'. " + "To change the program in a schedule, please delete the schedule and create a new one.", existing.getName(), existing.getProgramId().toString()));
    }
    Map<String, String> properties = Objects.firstNonNull(scheduleDetail.getProperties(), existing.getProperties());
    Trigger trigger = Objects.firstNonNull(scheduleDetail.getTrigger(), existing.getTrigger());
    List<? extends Constraint> constraints = Objects.firstNonNull(scheduleDetail.getConstraints(), existing.getConstraints());
    Long timeoutMillis = Objects.firstNonNull(scheduleDetail.getTimeoutMillis(), existing.getTimeoutMillis());
    return new ProgramSchedule(existing.getName(), description, programId, properties, trigger, constraints, timeoutMillis);
}
Also used : 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) BadRequestException(co.cask.cdap.common.BadRequestException) ProgramId(co.cask.cdap.proto.id.ProgramId)

Example 13 with ProgramId

use of co.cask.cdap.proto.id.ProgramId in project cdap by caskdata.

the class RouteConfigHttpHandler method storeRouteConfig.

@PUT
@Path("/routeconfig")
@AuditPolicy(AuditDetail.REQUEST_BODY)
public void storeRouteConfig(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @PathParam("app-id") String appId, @PathParam("service-id") String serviceId) throws Exception {
    NamespaceId namespace = new NamespaceId(namespaceId);
    ProgramId programId = namespace.app(appId).service(serviceId);
    Map<String, Integer> routes = parseBody(request, ROUTE_CONFIG_TYPE);
    if (routes == null || routes.isEmpty()) {
        throw new BadRequestException("Route config contains invalid format or empty content.");
    }
    List<ProgramId> nonExistingServices = new ArrayList<>();
    for (String version : routes.keySet()) {
        ProgramId routeProgram = namespace.app(appId, version).service(serviceId);
        if (lifecycleService.getProgramSpecification(routeProgram) == null) {
            nonExistingServices.add(routeProgram);
        }
    }
    if (nonExistingServices.size() > 0) {
        throw new BadRequestException("The following versions of the application/service could not be found : " + nonExistingServices);
    }
    RouteConfig routeConfig = new RouteConfig(routes);
    if (!routeConfig.isValid()) {
        throw new BadRequestException("Route Percentage needs to add up to 100.");
    }
    routeStore.store(programId, routeConfig);
    responder.sendStatus(HttpResponseStatus.OK);
}
Also used : ArrayList(java.util.ArrayList) BadRequestException(co.cask.cdap.common.BadRequestException) RouteConfig(co.cask.cdap.route.store.RouteConfig) NamespaceId(co.cask.cdap.proto.id.NamespaceId) 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 14 with ProgramId

use of co.cask.cdap.proto.id.ProgramId in project cdap by caskdata.

the class DefaultStoreTest method writeStartRecord.

private void writeStartRecord(Id.Run run) {
    ProgramId programId = run.getProgram().toEntityId();
    store.setStart(programId, run.getId(), RunIds.getTime(RunIds.fromString(run.getId()), TimeUnit.SECONDS));
    Assert.assertNotNull(store.getRun(programId, run.getId()));
}
Also used : ProgramId(co.cask.cdap.proto.id.ProgramId)

Example 15 with ProgramId

use of co.cask.cdap.proto.id.ProgramId in project cdap by caskdata.

the class DefaultStoreTest method writeSuspendedRecord.

private void writeSuspendedRecord(Id.Run run) {
    ProgramId programId = run.getProgram().toEntityId();
    store.setSuspend(programId, run.getId());
    Assert.assertNotNull(store.getRun(programId, run.getId()));
}
Also used : ProgramId(co.cask.cdap.proto.id.ProgramId)

Aggregations

ProgramId (co.cask.cdap.proto.id.ProgramId)259 Test (org.junit.Test)104 ApplicationId (co.cask.cdap.proto.id.ApplicationId)96 Path (javax.ws.rs.Path)62 ProgramRunId (co.cask.cdap.proto.id.ProgramRunId)45 DatasetId (co.cask.cdap.proto.id.DatasetId)40 RunId (org.apache.twill.api.RunId)40 NotFoundException (co.cask.cdap.common.NotFoundException)35 ProgramType (co.cask.cdap.proto.ProgramType)35 NamespaceId (co.cask.cdap.proto.id.NamespaceId)35 StreamId (co.cask.cdap.proto.id.StreamId)32 ApplicationSpecification (co.cask.cdap.api.app.ApplicationSpecification)28 IOException (java.io.IOException)26 ArrayList (java.util.ArrayList)26 POST (javax.ws.rs.POST)24 AuditPolicy (co.cask.cdap.common.security.AuditPolicy)23 GET (javax.ws.rs.GET)22 NamespaceNotFoundException (co.cask.cdap.common.NamespaceNotFoundException)21 HttpResponse (org.apache.http.HttpResponse)20 HashMap (java.util.HashMap)19