/** * List schedule records with the given key prefix and only returns the schedules that can pass the filter. * * @param prefix 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(byte[] prefix, Predicate<ProgramSchedule> filter) { List<ProgramScheduleRecord> result = new ArrayList<>(); try (Scanner scanner = store.scan(new Scan(prefix, Bytes.stopKeyForPrefix(prefix)))) { Row row; while ((row = scanner.next()) != null) { byte[] serialized = row.get(SCHEDULE_COLUMN_BYTES); if (serialized != null) { ProgramSchedule schedule = GSON.fromJson(Bytes.toString(serialized), ProgramSchedule.class); if (schedule != null && filter.test(schedule)) { result.add(new ProgramScheduleRecord(schedule, extractMetaFromRow(schedule.getScheduleId(), row))); } } } } return result; }
@Override public void enableSchedule(ScheduleId scheduleId) throws NotFoundException, ConflictException { checkStarted(); try { execute((StoreTxRunnable<Void, Exception>) store -> { ProgramScheduleRecord record = store.getScheduleRecord(scheduleId); if (ProgramScheduleStatus.SUSPENDED != record.getMeta().getStatus()) { throw new ConflictException("Schedule '" + scheduleId + "' is already enabled"); } timeSchedulerService.resumeProgramSchedule(record.getSchedule()); store.updateScheduleStatus(scheduleId, ProgramScheduleStatus.SCHEDULED); return null; }, Exception.class); } catch (NotFoundException | ConflictException e) { throw e; } catch (SchedulerException e) { // TODO: [CDAP-11574] temporarily catch the SchedulerException and throw RuntimeException. throw new RuntimeException("Exception occurs when enabling schedule " + scheduleId, e); } catch (Exception e) { throw Throwables.propagate(e); } }
@Override public ProgramScheduleStatus getScheduleStatus(ScheduleId scheduleId) throws NotFoundException { checkStarted(); return execute(store -> store.getScheduleRecord(scheduleId).getMeta().getStatus(), NotFoundException.class); }
@Override public ProgramScheduleStatus getScheduleStatus(ScheduleId scheduleId) throws NotFoundException { checkStarted(); return execute(store -> store.getScheduleRecord(scheduleId).getMeta().getStatus(), NotFoundException.class); }
@Override public void enableSchedule(ScheduleId scheduleId) throws NotFoundException, ConflictException { checkStarted(); try { execute((StoreTxRunnable<Void, Exception>) store -> { ProgramScheduleRecord record = store.getScheduleRecord(scheduleId); if (ProgramScheduleStatus.SUSPENDED != record.getMeta().getStatus()) { throw new ConflictException("Schedule '" + scheduleId + "' is already enabled"); } timeSchedulerService.resumeProgramSchedule(record.getSchedule()); store.updateScheduleStatus(scheduleId, ProgramScheduleStatus.SCHEDULED); return null; }, Exception.class); } catch (NotFoundException | ConflictException e) { throw e; } catch (SchedulerException e) { // TODO: [CDAP-11574] temporarily catch the SchedulerException and throw RuntimeException. throw new RuntimeException("Exception occurs when enabling schedule " + scheduleId, e); } catch (Exception e) { throw Throwables.propagate(e); } }
/** * List schedule records with the given key prefix and only returns the schedules that can pass the filter. * * @param prefix 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(byte[] prefix, Predicate<ProgramSchedule> filter) { List<ProgramScheduleRecord> result = new ArrayList<>(); try (Scanner scanner = store.scan(new Scan(prefix, Bytes.stopKeyForPrefix(prefix)))) { Row row; while ((row = scanner.next()) != null) { byte[] serialized = row.get(SCHEDULE_COLUMN_BYTES); if (serialized != null) { ProgramSchedule schedule = GSON.fromJson(Bytes.toString(serialized), ProgramSchedule.class); if (schedule != null && filter.test(schedule)) { result.add(new ProgramScheduleRecord(schedule, extractMetaFromRow(schedule.getScheduleId(), row))); } } } } return result; }
.filter(record -> programScheduleStatus == null || record.getMeta().getStatus().equals(programScheduleStatus)) .map(ProgramScheduleRecord::toScheduleDetail) .collect(Collectors.toList());
@Override public void disableSchedule(ScheduleId scheduleId) throws NotFoundException, ConflictException { checkStarted(); try { execute((StoreAndQueueTxRunnable<Void, Exception>) (store, queue) -> { ProgramScheduleRecord record = store.getScheduleRecord(scheduleId); if (ProgramScheduleStatus.SCHEDULED != record.getMeta().getStatus()) { throw new ConflictException("Schedule '" + scheduleId + "' is already disabled"); } timeSchedulerService.suspendProgramSchedule(record.getSchedule()); store.updateScheduleStatus(scheduleId, ProgramScheduleStatus.SUSPENDED); queue.markJobsForDeletion(scheduleId, System.currentTimeMillis()); return null; }, Exception.class); } catch (NotFoundException | ConflictException e) { throw e; } catch (SchedulerException e) { // TODO: [CDAP-11574] temporarily catch the SchedulerException and throw RuntimeException. throw new RuntimeException("Exception occurs when enabling schedule " + scheduleId, e); } catch (Exception e) { throw Throwables.propagate(e); } }
ProgramScheduleRecord record = new ProgramScheduleRecord(schedule, meta); schedulesFound.put(scheduleId, record); } catch (IllegalArgumentException | NotFoundException e) {
.filter(record -> programScheduleStatus == null || record.getMeta().getStatus().equals(programScheduleStatus)) .map(ProgramScheduleRecord::toScheduleDetail) .collect(Collectors.toList());
@Override public void disableSchedule(ScheduleId scheduleId) throws NotFoundException, ConflictException { checkStarted(); try { execute((StoreAndQueueTxRunnable<Void, Exception>) (store, queue) -> { ProgramScheduleRecord record = store.getScheduleRecord(scheduleId); if (ProgramScheduleStatus.SCHEDULED != record.getMeta().getStatus()) { throw new ConflictException("Schedule '" + scheduleId + "' is already disabled"); } timeSchedulerService.suspendProgramSchedule(record.getSchedule()); store.updateScheduleStatus(scheduleId, ProgramScheduleStatus.SUSPENDED); queue.markJobsForDeletion(scheduleId, System.currentTimeMillis()); return null; }, Exception.class); } catch (NotFoundException | ConflictException e) { throw e; } catch (SchedulerException e) { // TODO: [CDAP-11574] temporarily catch the SchedulerException and throw RuntimeException. throw new RuntimeException("Exception occurs when enabling schedule " + scheduleId, e); } catch (Exception e) { throw Throwables.propagate(e); } }
ProgramScheduleRecord record = new ProgramScheduleRecord(schedule, meta); schedulesFound.put(scheduleId, record); } catch (IllegalArgumentException | NotFoundException e) {
predicate = predicate.and(record -> record.getMeta().getStatus().equals(status)); predicate = predicate.and(record -> record.getSchedule().getTrigger().getType().equals(triggerType));
/** * Read all information about a schedule from the store. * * @param scheduleId the id of the schedule to read * @return the schedule record from the store * @throws NotFoundException if the schedule does not exist in the store */ public ProgramScheduleRecord getScheduleRecord(ScheduleId scheduleId) throws NotFoundException { Row row = store.get(new Get(rowKeyForSchedule(scheduleId))); byte[] serialized = row.get(SCHEDULE_COLUMN_BYTES); if (serialized == null) { throw new NotFoundException(scheduleId); } ProgramSchedule schedule = GSON.fromJson(Bytes.toString(serialized), ProgramSchedule.class); ProgramScheduleMeta meta = extractMetaFromRow(scheduleId, row); return new ProgramScheduleRecord(schedule, meta); }
predicate = predicate.and(record -> record.getMeta().getStatus().equals(status)); predicate = predicate.and(record -> record.getSchedule().getTrigger().getType().equals(triggerType));
/** * Read all information about a schedule from the store. * * @param scheduleId the id of the schedule to read * @return the schedule record from the store * @throws NotFoundException if the schedule does not exist in the store */ public ProgramScheduleRecord getScheduleRecord(ScheduleId scheduleId) throws NotFoundException { Row row = store.get(new Get(rowKeyForSchedule(scheduleId))); byte[] serialized = row.get(SCHEDULE_COLUMN_BYTES); if (serialized == null) { throw new NotFoundException(scheduleId); } ProgramSchedule schedule = GSON.fromJson(Bytes.toString(serialized), ProgramSchedule.class); ProgramScheduleMeta meta = extractMetaFromRow(scheduleId, row); return new ProgramScheduleRecord(schedule, meta); }
ProgramSchedule schedule = scheduleRecord.getSchedule(); try { deleteSchedule(schedule.getScheduleId()); schedule.getProgramId(), schedule.getProperties(), updatedTrigger, schedule.getConstraints(), schedule.getTimeoutMillis()), scheduleRecord.getMeta().getStatus(), System.currentTimeMillis()); } catch (AlreadyExistsException e) {
@Override public void apply() throws Exception { // should be 0 jobs in the JobQueue to begin with Assert.assertEquals(0, getAllJobs(jobQueue, false).size()); // Construct a partition notification with DATASET_ID Notification notification = Notification.forPartitions(DATASET_ID, ImmutableList.<PartitionKey>of()); Assert.assertNull(jobQueue.getJob(SCHED1_JOB.getJobKey())); jobQueue.put(SCHED1_JOB); Assert.assertEquals(SCHED1_JOB, jobQueue.getJob(SCHED1_JOB.getJobKey())); // Since notification and SCHED1 have the same dataset id DATASET_ID, notification will be added to // SCHED1_JOB, which is a job in SCHED1 jobQueue.addNotification( new ProgramScheduleRecord(SCHED1, new ProgramScheduleMeta(ProgramScheduleStatus.SCHEDULED, 0L)), notification); Assert.assertEquals(ImmutableList.of(notification), jobQueue.getJob(SCHED1_JOB.getJobKey()).getNotifications()); } });
ProgramSchedule schedule = scheduleRecord.getSchedule(); try { deleteSchedule(schedule.getScheduleId()); schedule.getProgramId(), schedule.getProperties(), updatedTrigger, schedule.getConstraints(), schedule.getTimeoutMillis()), scheduleRecord.getMeta().getStatus(), System.currentTimeMillis()); } catch (AlreadyExistsException e) {
new ProgramScheduleRecord(SCHED1, new ProgramScheduleMeta(ProgramScheduleStatus.SCHEDULED, 0L)), notification);