Search in sources :

Example 51 with StructuredRow

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;
}
Also used : StructuredRow(io.cdap.cdap.spi.data.StructuredRow) Constraint(io.cdap.cdap.internal.schedule.constraint.Constraint)

Example 52 with StructuredRow

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);
}
Also used : StructuredRow(io.cdap.cdap.spi.data.StructuredRow) NotFoundException(io.cdap.cdap.common.NotFoundException)

Example 53 with StructuredRow

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);
    }
}
Also used : Field(io.cdap.cdap.spi.data.table.field.Field) AlreadyExistsException(io.cdap.cdap.common.AlreadyExistsException) StructuredRow(io.cdap.cdap.spi.data.StructuredRow) ArrayList(java.util.ArrayList) Constraint(io.cdap.cdap.internal.schedule.constraint.Constraint)

Example 54 with StructuredRow

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;
}
Also used : Field(io.cdap.cdap.spi.data.table.field.Field) ArrayList(java.util.ArrayList) StructuredRow(io.cdap.cdap.spi.data.StructuredRow) ScheduleId(io.cdap.cdap.proto.id.ScheduleId) Range(io.cdap.cdap.spi.data.table.field.Range)

Example 55 with StructuredRow

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;
}
Also used : Field(io.cdap.cdap.spi.data.table.field.Field) ProgramSchedule(io.cdap.cdap.internal.app.runtime.schedule.ProgramSchedule) ArrayList(java.util.ArrayList) StructuredRow(io.cdap.cdap.spi.data.StructuredRow) ScheduleId(io.cdap.cdap.proto.id.ScheduleId) Range(io.cdap.cdap.spi.data.table.field.Range)

Aggregations

StructuredRow (io.cdap.cdap.spi.data.StructuredRow)142 StructuredTable (io.cdap.cdap.spi.data.StructuredTable)68 Field (io.cdap.cdap.spi.data.table.field.Field)66 ArrayList (java.util.ArrayList)54 Range (io.cdap.cdap.spi.data.table.field.Range)36 HashSet (java.util.HashSet)28 IOException (java.io.IOException)22 HashMap (java.util.HashMap)22 LinkedHashSet (java.util.LinkedHashSet)22 List (java.util.List)20 LinkedHashMap (java.util.LinkedHashMap)18 Map (java.util.Map)18 ArtifactNotFoundException (io.cdap.cdap.common.ArtifactNotFoundException)16 Collection (java.util.Collection)16 Set (java.util.Set)16 Nullable (javax.annotation.Nullable)16 ImmutableList (com.google.common.collect.ImmutableList)14 CloseableIterator (io.cdap.cdap.api.dataset.lib.CloseableIterator)14 TableNotFoundException (io.cdap.cdap.spi.data.TableNotFoundException)14 Fields (io.cdap.cdap.spi.data.table.field.Fields)14