use of co.cask.cdap.internal.app.runtime.schedule.ProgramSchedule in project cdap by caskdata.
the class ProgramScheduleStoreDataset method addSchedules.
/**
* Add one or more schedules to the store.
*
* @param schedules the schedules to add
* @return the new schedules' last modified timestamp
* @throws AlreadyExistsException if one of the schedules already exists
*/
public long addSchedules(Iterable<? extends ProgramSchedule> schedules) throws AlreadyExistsException {
long currentTime = System.currentTimeMillis();
for (ProgramSchedule schedule : schedules) {
byte[] scheduleKey = rowKeyBytesForSchedule(schedule.getProgramId().getParent().schedule(schedule.getName()));
if (!store.get(new Get(scheduleKey)).isEmpty()) {
throw new AlreadyExistsException(schedule.getProgramId().getParent().schedule(schedule.getName()));
}
Put schedulePut = new Put(scheduleKey);
schedulePut.add(SCHEDULE_COLUMN_BYTES, GSON.toJson(schedule));
schedulePut.add(UPDATED_COLUMN_BYTES, currentTime);
// initially suspended
schedulePut.add(STATUS_COLUMN_BYTES, ProgramScheduleStatus.SUSPENDED.toString());
store.put(schedulePut);
int count = 0;
for (String triggerKey : extractTriggerKeys(schedule)) {
byte[] triggerRowKey = rowKeyBytesForTrigger(scheduleKey, count++);
store.put(new Put(triggerRowKey, TRIGGER_KEY_COLUMN_BYTES, triggerKey));
}
}
return currentTime;
}
use of co.cask.cdap.internal.app.runtime.schedule.ProgramSchedule 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.internal.app.runtime.schedule.ProgramSchedule in project cdap by caskdata.
the class ProgramScheduleStoreDataset method getScheduleRecord.
/**
* Read all information about a schedule from the store.
*
* @param scheduleId the id of the schedule to read
* @return the schedule record from the store
* @throws NotFoundException if the schedule does not exist in the store
*/
public ProgramScheduleRecord getScheduleRecord(ScheduleId scheduleId) throws NotFoundException {
Row row = store.get(new Get(rowKeyForSchedule(scheduleId)));
byte[] serialized = row.get(SCHEDULE_COLUMN_BYTES);
if (serialized == null) {
throw new NotFoundException(scheduleId);
}
ProgramSchedule schedule = GSON.fromJson(Bytes.toString(serialized), ProgramSchedule.class);
ProgramScheduleMeta meta = extractMetaFromRow(scheduleId, row);
return new ProgramScheduleRecord(schedule, meta);
}
use of co.cask.cdap.internal.app.runtime.schedule.ProgramSchedule in project cdap by caskdata.
the class ProgramLifecycleHttpHandler method doGetSchedule.
private void doGetSchedule(HttpResponder responder, String namespace, String app, String version, String scheduleName, String format) throws NotFoundException, BadRequestException {
boolean asScheduleSpec = returnScheduleAsSpec(format);
ScheduleId scheduleId = new ApplicationId(namespace, app, version).schedule(scheduleName);
ProgramSchedule schedule = programScheduler.getSchedule(scheduleId);
ScheduleDetail detail = schedule.toScheduleDetail();
if (asScheduleSpec) {
ScheduleSpecification spec = detail.toScheduleSpec();
if (spec == null) {
// then this application must be using new APIs and the schedule can't be returned in old form.
throw new NotFoundException(scheduleId);
}
responder.sendJson(HttpResponseStatus.OK, spec, ScheduleSpecification.class, GSON);
} else {
responder.sendJson(HttpResponseStatus.OK, detail, ScheduleDetail.class, GSON);
}
}
use of co.cask.cdap.internal.app.runtime.schedule.ProgramSchedule in project cdap by caskdata.
the class ProgramLifecycleHttpHandler method doUpdateSchedule.
private void doUpdateSchedule(HttpRequest request, HttpResponder responder, String namespaceId, String appId, String appVersion, String scheduleName) throws BadRequestException, IOException, NotFoundException {
ScheduleId scheduleId = new ApplicationId(namespaceId, appId, appVersion).schedule(scheduleName);
final ProgramSchedule existingSchedule = programScheduler.getSchedule(scheduleId);
ScheduleDetail scheduleDetail = readScheduleDetailBody(request, scheduleName, true, new Function<JsonElement, ScheduleDetail>() {
@Override
public ScheduleDetail apply(@Nullable JsonElement input) {
ScheduleUpdateDetail updateDetail = GSON.fromJson(input, ScheduleUpdateDetail.class);
return toScheduleDetail(updateDetail, existingSchedule);
}
});
ProgramSchedule updatedSchedule = combineForUpdate(scheduleDetail, existingSchedule);
programScheduler.updateSchedule(updatedSchedule);
responder.sendStatus(HttpResponseStatus.OK);
}
Aggregations