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 ProgramLifecycleService method suspendResumeSchedule.
/**
* Performs an action (suspend/resume) on the given schedule
*
* @param scheduleId Id of the schedule
* @param action the action to perform
* @throws Exception if the given action is invalid or failed to perform a valid action on the schedule
*/
public void suspendResumeSchedule(ScheduleId scheduleId, String action) throws Exception {
boolean doEnable;
if (action.equals("disable") || action.equals("suspend")) {
doEnable = false;
} else if (action.equals("enable") || action.equals("resume")) {
doEnable = true;
} else {
throw new BadRequestException("Action for schedules may only be 'enable', 'disable', 'suspend', or 'resume' but is'" + action + "'");
}
ProgramSchedule schedule = scheduler.getSchedule(scheduleId);
authorizationEnforcer.enforce(schedule.getProgramId(), authenticationContext.getPrincipal(), Action.EXECUTE);
if (doEnable) {
scheduler.enableSchedule(scheduleId);
} else {
scheduler.disableSchedule(scheduleId);
}
}
use of co.cask.cdap.internal.app.runtime.schedule.ProgramSchedule in project cdap by caskdata.
the class ProgramLifecycleService method getScheduleStatus.
/**
* Gets the state of the given schedule
*
* @return the status of the given schedule
* @throws Exception if failed to get the state of the schedule
*/
public ProgramScheduleStatus getScheduleStatus(ScheduleId scheduleId) throws Exception {
ApplicationId applicationId = scheduleId.getParent();
ApplicationSpecification appSpec = store.getApplication(applicationId);
if (appSpec == null) {
throw new NotFoundException(applicationId);
}
ProgramSchedule schedule = scheduler.getSchedule(scheduleId);
ensureAccess(schedule.getProgramId());
return scheduler.getScheduleStatus(scheduleId);
}
use of co.cask.cdap.internal.app.runtime.schedule.ProgramSchedule in project cdap by caskdata.
the class TriggerCodecTest method testObjectContainingTrigger.
@Test
public void testObjectContainingTrigger() {
ProgramSchedule proto1 = new ProgramSchedule("sched1", "one partition schedule", new NamespaceId("test").app("a").worker("ww"), ImmutableMap.of("prop3", "abc"), new ProtoTrigger.PartitionTrigger(new DatasetId("test1", "pdfs1"), 1), ImmutableList.<Constraint>of());
ProgramSchedule sched1 = new ProgramSchedule("sched1", "one partition schedule", new NamespaceId("test").app("a").worker("ww"), ImmutableMap.of("prop3", "abc"), new PartitionTrigger(new DatasetId("test1", "pdfs1"), 1), ImmutableList.<Constraint>of());
Assert.assertEquals(sched1, GSON.fromJson(GSON.toJson(proto1), ProgramSchedule.class));
ProgramSchedule proto2 = new ProgramSchedule("schedone", "one time schedule", new NamespaceId("test3").app("abc").workflow("wf112"), ImmutableMap.of("prop", "all"), new ProtoTrigger.TimeTrigger("* * * 1 1"), ImmutableList.<Constraint>of());
ProgramSchedule sched2 = new ProgramSchedule("schedone", "one time schedule", new NamespaceId("test3").app("abc").workflow("wf112"), ImmutableMap.of("prop", "all"), new TimeTrigger("* * * 1 1"), ImmutableList.<Constraint>of());
Assert.assertEquals(sched2, GSON.fromJson(GSON.toJson(proto2), ProgramSchedule.class));
ProgramSchedule proto3 = new ProgramSchedule("sched3", "one MB schedule", new NamespaceId("test3").app("abc").workflow("wf112"), ImmutableMap.of("prop", "all"), new ProtoTrigger.StreamSizeTrigger(new StreamId("x", "y"), 1), ImmutableList.<Constraint>of());
ProgramSchedule sched3 = new ProgramSchedule("sched3", "one MB schedule", new NamespaceId("test3").app("abc").workflow("wf112"), ImmutableMap.of("prop", "all"), new StreamSizeTrigger(new StreamId("x", "y"), 1), ImmutableList.<Constraint>of());
Assert.assertEquals(sched3, GSON.fromJson(GSON.toJson(proto3), ProgramSchedule.class));
}
Aggregations