use of io.cdap.cdap.spi.data.StructuredRow in project cdap by cdapio.
the class JobQueueTable method toJob.
/**
* Construct a Job object from the rows in iterator.
* A job may need up to three rows from the iterator for its construction -
* <ul>
* <li>Row JOB - the row containing the job data, required</li>
* <li>Row DELETE - the row containing the time when the job was marked for deletion, optional</li>
* <li>Row OBSOLETE - the row containing the time when the job was marked as obsolete, optional</li>
* </ul>
* The above three rows will always be next to each other due to sorting.
*
* @param peekingIterator should have at least one element
* @return a Job object for the first schedule in the iterator
*/
private Job toJob(PeekingIterator<StructuredRow> peekingIterator) {
SimpleJob job = null;
Long toBeDeletedTime = null;
Long isObsoleteTime = null;
// Get the schedule id for the job from the first element
String scheduleId = getScheduleId(peekingIterator.peek());
// Also get the generationId to only read the rows for the current job
int generationId = getGenerationId(peekingIterator.peek());
// Get all the rows for the current job from the iterator
while (peekingIterator.hasNext() && generationId == getGenerationId(peekingIterator.peek()) && scheduleId.equals(getScheduleId(peekingIterator.peek()))) {
StructuredRow row = peekingIterator.next();
StoreDefinition.JobQueueStore.RowType rowType = getRowType(row);
switch(rowType) {
case JOB:
job = fromStructuredRow(row);
break;
case DELETE:
toBeDeletedTime = row.getLong(StoreDefinition.JobQueueStore.DELETE_TIME);
break;
case OBSOLETE:
isObsoleteTime = row.getLong(StoreDefinition.JobQueueStore.OBSOLETE_TIME);
break;
default:
// Should not happen unless a new value is added to the RowType enum
throw new IllegalStateException(String.format("Unknown row type encountered in job queue: %s", rowType));
}
}
if (job == null) {
// Should not happen since we always write delete time or obsolete time only after reading the job from store
throw new IllegalStateException(String.format("Cannot find job for schedule id: %s", scheduleId));
}
Long timeToSet = toBeDeletedTime == null ? isObsoleteTime : isObsoleteTime == null ? toBeDeletedTime : new Long(Math.min(isObsoleteTime, toBeDeletedTime));
if (timeToSet != null) {
job.setToBeDeleted(timeToSet);
}
return job;
}
use of io.cdap.cdap.spi.data.StructuredRow in project cdap by cdapio.
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 cdapio.
the class ProgramScheduleStoreDataset method addScheduleWithStatus.
/**
* Add a schedule to the store.
*
* @param schedule the schedule to add
* @param status the status of the schedule to add
* @param currentTime the current time in milliseconds when adding the schedule
* @throws AlreadyExistsException if the schedule already exists
*/
private void addScheduleWithStatus(ProgramSchedule schedule, ProgramScheduleStatus status, long currentTime) throws AlreadyExistsException, IOException {
Collection<Field<?>> scheduleKeys = getScheduleKeys(schedule.getScheduleId());
Optional<StructuredRow> existing = scheduleStore.read(scheduleKeys);
if (existing.isPresent() && existing.get().getString(StoreDefinition.ProgramScheduleStore.SCHEDULE) != null) {
throw new AlreadyExistsException(schedule.getScheduleId());
}
Collection<Field<?>> scheduleFields = new ArrayList<>(scheduleKeys);
scheduleFields.add(Fields.stringField(StoreDefinition.ProgramScheduleStore.SCHEDULE, GSON.toJson(schedule)));
scheduleFields.add(Fields.longField(StoreDefinition.ProgramScheduleStore.UPDATE_TIME, currentTime));
scheduleFields.add(Fields.stringField(StoreDefinition.ProgramScheduleStore.STATUS, status.toString()));
scheduleStore.upsert(scheduleFields);
int count = 0;
for (String triggerKey : extractTriggerKeys(schedule)) {
Collection<Field<?>> triggerFields = getTriggerKeys(scheduleKeys, count++);
triggerFields.add(Fields.stringField(StoreDefinition.ProgramScheduleStore.TRIGGER_KEY, triggerKey));
triggerStore.upsert(triggerFields);
}
}
use of io.cdap.cdap.spi.data.StructuredRow in project cdap by cdapio.
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 cdapio.
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;
}
Aggregations