use of io.cdap.cdap.spi.data.StructuredRow in project cdap by caskdata.
the class ProgramScheduleStoreDataset method deleteSchedules.
/**
* Removes all schedules for a specific application from the store.
*
* @param appId the application id for which to delete the schedules
* @return the IDs of the schedules that were deleted
*/
// TODO: fix the bug that this method will return fake schedule id https://issues.cask.co/browse/CDAP-13626
public List<ScheduleId> deleteSchedules(ApplicationId appId, long deleteTime) throws IOException {
List<ScheduleId> deleted = new ArrayList<>();
Collection<Field<?>> scanKeys = getScheduleKeysForApplicationScan(appId);
Range range = Range.singleton(scanKeys);
// First collect all the schedules that are going to be deleted
try (CloseableIterator<StructuredRow> iterator = scheduleStore.scan(range, Integer.MAX_VALUE)) {
while (iterator.hasNext()) {
StructuredRow row = iterator.next();
if (row.getString(StoreDefinition.ProgramScheduleStore.SCHEDULE) != null) {
markScheduleAsDeleted(row, deleteTime);
deleted.add(rowToScheduleId(row));
}
}
}
// Then delete all triggers for the app
triggerStore.deleteAll(range);
return deleted;
}
use of io.cdap.cdap.spi.data.StructuredRow in project cdap by caskdata.
the class ProgramScheduleStoreDataset method deleteSchedules.
/**
* Removes all schedules for a specific program from the store.
*
* @param programId the program id for which to delete the schedules
* @return the IDs of the schedules that were deleted
*/
// TODO: fix the bug that this method will return fake schedule id https://issues.cask.co/browse/CDAP-13626
public List<ScheduleId> deleteSchedules(ProgramId programId, long deleteTime) throws IOException {
List<ScheduleId> deleted = new ArrayList<>();
Collection<Field<?>> scanKeys = getScheduleKeysForApplicationScan(programId.getParent());
Range range = Range.singleton(scanKeys);
// First collect all the schedules that are going to be deleted
try (CloseableIterator<StructuredRow> iterator = scheduleStore.scan(range, Integer.MAX_VALUE)) {
while (iterator.hasNext()) {
StructuredRow row = iterator.next();
String serializedSchedule = row.getString(StoreDefinition.ProgramScheduleStore.SCHEDULE);
if (serializedSchedule != null) {
ProgramSchedule schedule = GSON.fromJson(serializedSchedule, ProgramSchedule.class);
if (programId.equals(schedule.getProgramId())) {
markScheduleAsDeleted(row, deleteTime);
Collection<Field<?>> deleteKeys = getScheduleKeys(row);
triggerStore.deleteAll(Range.singleton(deleteKeys));
deleted.add(rowToScheduleId(row));
}
}
}
}
return deleted;
}
use of io.cdap.cdap.spi.data.StructuredRow in project cdap by caskdata.
the class ProgramScheduleStoreDataset method listSchedulesRecordsWithPrefix.
/**
* List schedule records with the given key prefix and only returns the schedules that can pass the filter.
*
* @param prefixKeys the prefix of the schedule records to be listed
* @param filter a filter that only returns true if the schedule record will be returned in the result
* @return the schedule records with the given key prefix that can pass the filter
*/
private List<ProgramScheduleRecord> listSchedulesRecordsWithPrefix(Collection<Field<?>> prefixKeys, Predicate<ProgramSchedule> filter) throws IOException {
List<ProgramScheduleRecord> result = new ArrayList<>();
try (CloseableIterator<StructuredRow> iterator = scheduleStore.scan(Range.singleton(prefixKeys), Integer.MAX_VALUE)) {
while (iterator.hasNext()) {
StructuredRow row = iterator.next();
String serializedSchedule = row.getString(StoreDefinition.ProgramScheduleStore.SCHEDULE);
if (serializedSchedule != null) {
ProgramSchedule schedule = GSON.fromJson(serializedSchedule, ProgramSchedule.class);
if (schedule != null && filter.test(schedule)) {
result.add(new ProgramScheduleRecord(schedule, extractMetaFromRow(schedule.getScheduleId(), row)));
}
}
}
}
return result;
}
use of io.cdap.cdap.spi.data.StructuredRow in project cdap by caskdata.
the class ProgramScheduleStoreDataset method getSchedule.
/**
* Read a schedule from the store.
*
* @param scheduleId the id of the schedule to read
* @return the schedule from the store
* @throws NotFoundException if the schedule does not exist in the store
*/
public ProgramSchedule getSchedule(ScheduleId scheduleId) throws NotFoundException, IOException {
StructuredRow row = readExistingScheduleRow(scheduleId);
String serializedSchedule = row.getString(StoreDefinition.ProgramScheduleStore.SCHEDULE);
if (serializedSchedule == null) {
throw new NotFoundException(scheduleId);
}
return GSON.fromJson(serializedSchedule, ProgramSchedule.class);
}
use of io.cdap.cdap.spi.data.StructuredRow in project cdap by caskdata.
the class DatasetBasedTimeScheduleStore method readSchedulesFromPersistentStore.
// Get schedule information from persistent store
private void readSchedulesFromPersistentStore() throws Exception {
final List<JobDetail> jobs = Lists.newArrayList();
final List<TriggerStatusV2> triggers = Lists.newArrayList();
TransactionRunners.run(transactionRunner, context -> {
StructuredTable table = getTimeScheduleStructuredTable(context);
try (CloseableIterator<StructuredRow> iterator = table.scan(Range.singleton(getScanPrefix(JOB_KEY)), Integer.MAX_VALUE)) {
while (iterator.hasNext()) {
JobDetail jobDetail = (JobDetail) SerializationUtils.deserialize(iterator.next().getBytes(StoreDefinition.TimeScheduleStore.VALUE_FIELD));
LOG.debug("Schedule: Job with key {} found", jobDetail.getKey());
jobs.add(jobDetail);
}
}
try (CloseableIterator<StructuredRow> iterator = table.scan(Range.singleton(getScanPrefix(TRIGGER_KEY)), Integer.MAX_VALUE)) {
while (iterator.hasNext()) {
TriggerStatusV2 trigger = (TriggerStatusV2) SerializationUtils.deserialize(iterator.next().getBytes(StoreDefinition.TimeScheduleStore.VALUE_FIELD));
if (trigger.state.equals(Trigger.TriggerState.NORMAL) || trigger.state.equals(Trigger.TriggerState.PAUSED)) {
triggers.add(trigger);
LOG.debug("Schedule: trigger with key {} added", trigger.trigger.getKey());
} else {
LOG.debug("Schedule: trigger with key {} and state {} skipped", trigger.trigger.getKey(), trigger.state);
}
}
}
});
Set<JobKey> jobKeys = new HashSet<>();
for (JobDetail job : jobs) {
super.storeJob(job, true);
jobKeys.add(job.getKey());
}
Set<TriggerKey> triggersWithNoJob = new HashSet<>();
for (TriggerStatusV2 trigger : triggers) {
if (!jobKeys.contains(trigger.trigger.getJobKey())) {
triggersWithNoJob.add(trigger.trigger.getKey());
continue;
}
super.storeTrigger(trigger.trigger, true);
// because the scheduler is actually not started at this point.
if (trigger.state == Trigger.TriggerState.PAUSED) {
super.pauseTrigger(trigger.trigger.getKey());
}
}
for (TriggerKey key : triggersWithNoJob) {
LOG.error(String.format("No Job was found for the Trigger key '%s'." + " Deleting the trigger entry from the store.", key));
executeDelete(key);
}
}
Aggregations