Search in sources :

Example 1 with ScheduleSpecification

use of co.cask.cdap.api.schedule.ScheduleSpecification in project cdap by caskdata.

the class ProgramScheduleStoreDataset method migrateFromAppMetadataStore.

/**
   * Migrate schedules in a given namespace from app metadata store to ProgramScheduleStoreDataset
   *
   * @param namespaceId  the namespace with schedules to be migrated
   * @param appMetaStore app metadata store with schedules to be migrated
   * @return the lexicographically largest namespace id String with schedule migration completed
   */
public String migrateFromAppMetadataStore(NamespaceId namespaceId, Store appMetaStore) {
    String completedNamespace = getMigrationCompleteNamespace();
    if (completedNamespace != null && completedNamespace.compareTo(namespaceId.toString()) > 0) {
        return completedNamespace;
    }
    for (ApplicationSpecification appSpec : appMetaStore.getAllApplications(namespaceId)) {
        ApplicationId appId = namespaceId.app(appSpec.getName(), appSpec.getAppVersion());
        for (ScheduleSpecification scheduleSpec : appSpec.getSchedules().values()) {
            ProgramSchedule schedule = Schedulers.toProgramSchedule(appId, scheduleSpec);
            try {
                addSchedule(schedule);
            } catch (AlreadyExistsException e) {
                // This should never happen since no schedule with the same schedule key should exist before migration
                LOG.warn("Schedule {} already exists before schedule migration", schedule.getScheduleId(), e);
            }
        }
    }
    store.put(MIGRATION_COMPLETE_NAMESPACE_ROW_BYTES, MIGRATION_COMPLETE_NAMESPACE_COLUMN_BYTES, Bytes.toBytes(namespaceId.toString()));
    return namespaceId.toString();
}
Also used : ApplicationSpecification(co.cask.cdap.api.app.ApplicationSpecification) AlreadyExistsException(co.cask.cdap.common.AlreadyExistsException) ProgramSchedule(co.cask.cdap.internal.app.runtime.schedule.ProgramSchedule) ApplicationId(co.cask.cdap.proto.id.ApplicationId) ScheduleSpecification(co.cask.cdap.api.schedule.ScheduleSpecification)

Example 2 with ScheduleSpecification

use of co.cask.cdap.api.schedule.ScheduleSpecification in project cdap by caskdata.

the class ScheduleSpecificationCodecTest method testBackwardsCompatibility.

@Test
public void testBackwardsCompatibility() throws Exception {
    // Before 2.8, the ScheduleSpecificationCodec used to have the same behavior as what Gson would do by
    // default, and only Schedules existed. We make sure that ScheduleSpecification persisted before
    // 2.8 can still be deserialized using the new codec.
    String cronEntry = "* * * * *";
    Schedule schedule = new OldSchedule("foo", "bar", cronEntry);
    ScheduleProgramInfo programInfo = new ScheduleProgramInfo(SchedulableProgramType.WORKFLOW, "testWorkflow");
    ImmutableMap<String, String> properties = ImmutableMap.of("a", "b", "c", "d");
    ScheduleSpecification specification = new ScheduleSpecification(schedule, programInfo, properties);
    // Use default Gson to serialize
    String jsonStr = new Gson().toJson(specification);
    ScheduleSpecification deserialized = GSON.fromJson(jsonStr, ScheduleSpecification.class);
    ScheduleSpecification expectedSpec = new ScheduleSpecification(Schedules.builder(schedule.getName()).setDescription(schedule.getDescription()).createTimeSchedule(cronEntry), programInfo, properties);
    Assert.assertEquals(expectedSpec, deserialized);
}
Also used : Schedule(co.cask.cdap.api.schedule.Schedule) StreamSizeSchedule(co.cask.cdap.internal.schedule.StreamSizeSchedule) TimeSchedule(co.cask.cdap.internal.schedule.TimeSchedule) Gson(com.google.gson.Gson) ScheduleProgramInfo(co.cask.cdap.api.workflow.ScheduleProgramInfo) ScheduleSpecification(co.cask.cdap.api.schedule.ScheduleSpecification) Test(org.junit.Test)

Example 3 with ScheduleSpecification

use of co.cask.cdap.api.schedule.ScheduleSpecification in project cdap by caskdata.

the class ScheduleSpecificationCodecTest method testTimeSchedule.

@Test
public void testTimeSchedule() throws Exception {
    TimeSchedule timeSchedule = (TimeSchedule) Schedules.builder("foo").setDescription("bar").createTimeSchedule("cronEntry");
    ScheduleProgramInfo programInfo = new ScheduleProgramInfo(SchedulableProgramType.WORKFLOW, "testWorkflow");
    ImmutableMap<String, String> properties = ImmutableMap.of("a", "b", "c", "d");
    ScheduleSpecification specification = new ScheduleSpecification(timeSchedule, programInfo, properties);
    String jsonStr = GSON.toJson(specification);
    ScheduleSpecification deserialized = GSON.fromJson(jsonStr, ScheduleSpecification.class);
    Assert.assertEquals(specification, deserialized);
}
Also used : TimeSchedule(co.cask.cdap.internal.schedule.TimeSchedule) ScheduleProgramInfo(co.cask.cdap.api.workflow.ScheduleProgramInfo) ScheduleSpecification(co.cask.cdap.api.schedule.ScheduleSpecification) Test(org.junit.Test)

Example 4 with ScheduleSpecification

use of co.cask.cdap.api.schedule.ScheduleSpecification in project cdap by caskdata.

the class ScheduleSpecificationCodecTest method testStreamSizeSchedule.

@Test
public void testStreamSizeSchedule() throws Exception {
    Schedule dataSchedule = Schedules.builder("foo").setDescription("bar").createDataSchedule(Schedules.Source.STREAM, "stream", 10);
    ScheduleProgramInfo programInfo = new ScheduleProgramInfo(SchedulableProgramType.WORKFLOW, "testWorkflow");
    ImmutableMap<String, String> properties = ImmutableMap.of("a", "b", "c", "d");
    ScheduleSpecification specification = new ScheduleSpecification(dataSchedule, programInfo, properties);
    String jsonStr = GSON.toJson(specification);
    ScheduleSpecification deserialized = GSON.fromJson(jsonStr, ScheduleSpecification.class);
    Assert.assertEquals(specification, deserialized);
}
Also used : Schedule(co.cask.cdap.api.schedule.Schedule) StreamSizeSchedule(co.cask.cdap.internal.schedule.StreamSizeSchedule) TimeSchedule(co.cask.cdap.internal.schedule.TimeSchedule) ScheduleProgramInfo(co.cask.cdap.api.workflow.ScheduleProgramInfo) ScheduleSpecification(co.cask.cdap.api.schedule.ScheduleSpecification) Test(org.junit.Test)

Example 5 with ScheduleSpecification

use of co.cask.cdap.api.schedule.ScheduleSpecification 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)

Aggregations

ScheduleSpecification (co.cask.cdap.api.schedule.ScheduleSpecification)11 StreamSizeSchedule (co.cask.cdap.internal.schedule.StreamSizeSchedule)6 TimeSchedule (co.cask.cdap.internal.schedule.TimeSchedule)6 Schedule (co.cask.cdap.api.schedule.Schedule)5 ScheduleProgramInfo (co.cask.cdap.api.workflow.ScheduleProgramInfo)5 ApplicationSpecification (co.cask.cdap.api.app.ApplicationSpecification)4 ProgramSchedule (co.cask.cdap.internal.app.runtime.schedule.ProgramSchedule)3 ApplicationId (co.cask.cdap.proto.id.ApplicationId)3 Test (org.junit.Test)3 NamespaceNotFoundException (co.cask.cdap.common.NamespaceNotFoundException)2 NotFoundException (co.cask.cdap.common.NotFoundException)2 ScheduleDetail (co.cask.cdap.proto.ScheduleDetail)2 JsonElement (com.google.gson.JsonElement)2 RunConstraints (co.cask.cdap.api.schedule.RunConstraints)1 AlreadyExistsException (co.cask.cdap.common.AlreadyExistsException)1 ApplicationNotFoundException (co.cask.cdap.common.ApplicationNotFoundException)1 BadRequestException (co.cask.cdap.common.BadRequestException)1 DefaultApplicationSpecification (co.cask.cdap.internal.app.DefaultApplicationSpecification)1 ScheduleCreationSpec (co.cask.cdap.internal.schedule.ScheduleCreationSpec)1 Constraint (co.cask.cdap.internal.schedule.constraint.Constraint)1