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();
}
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);
}
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);
}
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);
}
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);
}
Aggregations