Search in sources :

Example 86 with StructuredRow

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;
}
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 87 with StructuredRow

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;
}
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)

Example 88 with StructuredRow

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;
}
Also used : ProgramSchedule(io.cdap.cdap.internal.app.runtime.schedule.ProgramSchedule) ArrayList(java.util.ArrayList) StructuredRow(io.cdap.cdap.spi.data.StructuredRow) ProgramScheduleRecord(io.cdap.cdap.internal.app.runtime.schedule.ProgramScheduleRecord)

Example 89 with StructuredRow

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

Example 90 with StructuredRow

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);
    }
}
Also used : TriggerKey(org.quartz.TriggerKey) JobDetail(org.quartz.JobDetail) JobKey(org.quartz.JobKey) StructuredTable(io.cdap.cdap.spi.data.StructuredTable) StructuredRow(io.cdap.cdap.spi.data.StructuredRow) HashSet(java.util.HashSet)

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