Search in sources :

Example 1 with AbstractSatisfiableCompositeTrigger

use of io.cdap.cdap.internal.app.runtime.schedule.trigger.AbstractSatisfiableCompositeTrigger in project cdap by caskdata.

the class TimeScheduler method getCronTriggerKeyMap.

/**
 * @return A Map with cron expression as keys and corresponding trigger key as values.
 * Trigger keys are created from program name, programType and scheduleName (and cron expression if the trigger
 * in the schedule is a composite trigger) and TimeScheuler#PAUSED_NEW_TRIGGERS_GROUP
 * if it exists in this group else returns the {@link TriggerKey} prepared with null which gets it with
 * {@link Key#DEFAULT_GROUP}
 * @throws org.quartz.SchedulerException
 */
private Map<String, TriggerKey> getCronTriggerKeyMap(ProgramSchedule schedule) throws org.quartz.SchedulerException {
    ProgramId program = schedule.getProgramId();
    SchedulableProgramType programType = program.getType().getSchedulableType();
    io.cdap.cdap.api.schedule.Trigger trigger = schedule.getTrigger();
    Map<String, TriggerKey> cronTriggerKeyMap = new HashMap<>();
    // Get a set of TimeTrigger if the schedule's trigger is a composite trigger
    if (trigger instanceof AbstractSatisfiableCompositeTrigger) {
        Set<SatisfiableTrigger> triggerSet = ((AbstractSatisfiableCompositeTrigger) trigger).getUnitTriggers().get(ProtoTrigger.Type.TIME);
        if (triggerSet == null) {
            return ImmutableMap.of();
        }
        for (SatisfiableTrigger timeTrigger : triggerSet) {
            String cron = ((TimeTrigger) timeTrigger).getCronExpression();
            String triggerName = AbstractTimeSchedulerService.getTriggerName(program, programType, schedule.getName(), cron);
            cronTriggerKeyMap.put(cron, triggerKeyForName(triggerName));
        }
        return cronTriggerKeyMap;
    }
    // No need to include cron expression in trigger key if the trigger is not composite trigger
    String triggerName = AbstractTimeSchedulerService.scheduleIdFor(program, programType, schedule.getName());
    cronTriggerKeyMap.put(((TimeTrigger) schedule.getTrigger()).getCronExpression(), triggerKeyForName(triggerName));
    return cronTriggerKeyMap;
}
Also used : TimeTrigger(io.cdap.cdap.internal.app.runtime.schedule.trigger.TimeTrigger) HashMap(java.util.HashMap) AbstractSatisfiableCompositeTrigger(io.cdap.cdap.internal.app.runtime.schedule.trigger.AbstractSatisfiableCompositeTrigger) ProgramId(io.cdap.cdap.proto.id.ProgramId) TriggerKey(org.quartz.TriggerKey) SatisfiableTrigger(io.cdap.cdap.internal.app.runtime.schedule.trigger.SatisfiableTrigger) SchedulableProgramType(io.cdap.cdap.api.schedule.SchedulableProgramType)

Example 2 with AbstractSatisfiableCompositeTrigger

use of io.cdap.cdap.internal.app.runtime.schedule.trigger.AbstractSatisfiableCompositeTrigger in project cdap by caskdata.

the class ProgramScheduleStoreDataset method modifySchedulesTriggeredByDeletedProgram.

/**
 * Update all schedules that can be triggered by the given deleted program. A schedule will be removed if
 * the only {@link ProgramStatusTrigger} in it is triggered by the deleted program. Schedules with composite triggers
 * will be updated if the composite trigger can still be satisfied after the program is deleted, otherwise the
 * schedules will be deleted.
 *
 * @param programId the program id for which to delete the schedules
 * @return the IDs of the schedules that were deleted
 */
public List<ProgramSchedule> modifySchedulesTriggeredByDeletedProgram(ProgramId programId) throws IOException {
    long deleteTime = System.currentTimeMillis();
    List<ProgramSchedule> deleted = new ArrayList<>();
    Set<ProgramScheduleRecord> scheduleRecords = new HashSet<>();
    for (ProgramStatus status : ProgramStatus.values()) {
        scheduleRecords.addAll(findSchedules(Schedulers.triggerKeyForProgramStatus(programId, status)));
    }
    for (ProgramScheduleRecord scheduleRecord : scheduleRecords) {
        ProgramSchedule schedule = scheduleRecord.getSchedule();
        markScheduleAsDeleted(schedule.getScheduleId(), deleteTime);
        triggerStore.deleteAll(Range.singleton(getScheduleKeys(schedule.getScheduleId())));
        if (schedule.getTrigger() instanceof AbstractSatisfiableCompositeTrigger) {
            // get the updated composite trigger by removing the program status trigger of the given program
            Trigger updatedTrigger = ((AbstractSatisfiableCompositeTrigger) schedule.getTrigger()).getTriggerWithDeletedProgram(programId);
            if (updatedTrigger == null) {
                deleted.add(schedule);
                continue;
            }
            // if the updated composite trigger is not null, add the schedule back with updated composite trigger
            try {
                addScheduleWithStatus(new ProgramSchedule(schedule.getName(), schedule.getDescription(), schedule.getProgramId(), schedule.getProperties(), updatedTrigger, schedule.getConstraints(), schedule.getTimeoutMillis()), scheduleRecord.getMeta().getStatus(), System.currentTimeMillis());
            } catch (AlreadyExistsException e) {
                // this should never happen
                LOG.warn("Failed to add the schedule '{}' triggered by '{}' with updated trigger '{}', " + "skip adding this schedule.", schedule.getScheduleId(), programId, updatedTrigger, e);
            }
        } else {
            deleted.add(schedule);
        }
    }
    return deleted;
}
Also used : ProgramStatusTrigger(io.cdap.cdap.internal.app.runtime.schedule.trigger.ProgramStatusTrigger) SatisfiableTrigger(io.cdap.cdap.internal.app.runtime.schedule.trigger.SatisfiableTrigger) Trigger(io.cdap.cdap.api.schedule.Trigger) AbstractSatisfiableCompositeTrigger(io.cdap.cdap.internal.app.runtime.schedule.trigger.AbstractSatisfiableCompositeTrigger) AlreadyExistsException(io.cdap.cdap.common.AlreadyExistsException) ProgramSchedule(io.cdap.cdap.internal.app.runtime.schedule.ProgramSchedule) ArrayList(java.util.ArrayList) AbstractSatisfiableCompositeTrigger(io.cdap.cdap.internal.app.runtime.schedule.trigger.AbstractSatisfiableCompositeTrigger) ProgramScheduleRecord(io.cdap.cdap.internal.app.runtime.schedule.ProgramScheduleRecord) HashSet(java.util.HashSet) ProgramStatus(io.cdap.cdap.api.ProgramStatus)

Aggregations

AbstractSatisfiableCompositeTrigger (io.cdap.cdap.internal.app.runtime.schedule.trigger.AbstractSatisfiableCompositeTrigger)2 SatisfiableTrigger (io.cdap.cdap.internal.app.runtime.schedule.trigger.SatisfiableTrigger)2 ProgramStatus (io.cdap.cdap.api.ProgramStatus)1 SchedulableProgramType (io.cdap.cdap.api.schedule.SchedulableProgramType)1 Trigger (io.cdap.cdap.api.schedule.Trigger)1 AlreadyExistsException (io.cdap.cdap.common.AlreadyExistsException)1 ProgramSchedule (io.cdap.cdap.internal.app.runtime.schedule.ProgramSchedule)1 ProgramScheduleRecord (io.cdap.cdap.internal.app.runtime.schedule.ProgramScheduleRecord)1 ProgramStatusTrigger (io.cdap.cdap.internal.app.runtime.schedule.trigger.ProgramStatusTrigger)1 TimeTrigger (io.cdap.cdap.internal.app.runtime.schedule.trigger.TimeTrigger)1 ProgramId (io.cdap.cdap.proto.id.ProgramId)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 TriggerKey (org.quartz.TriggerKey)1