Search in sources :

Example 1 with Trigger

use of co.cask.cdap.api.schedule.Trigger in project cdap by caskdata.

the class ProtoTriggerCodecTest method testTriggerCodec.

private void testTriggerCodec(ProtoTrigger trigger) {
    String json = GSON.toJson(trigger);
    Trigger trigger1 = GSON.fromJson(json, Trigger.class);
    Assert.assertEquals(trigger, trigger1);
    json = GSON.toJson(trigger, Trigger.class);
    trigger1 = GSON.fromJson(json, Trigger.class);
    Assert.assertEquals(trigger, trigger1);
}
Also used : Trigger(co.cask.cdap.api.schedule.Trigger)

Example 2 with Trigger

use of co.cask.cdap.api.schedule.Trigger in project cdap by caskdata.

the class DeleteAndCreateSchedulesStage method toProgramSchedule.

private ProgramSchedule toProgramSchedule(ApplicationId appId, ScheduleCreationSpec scheduleCreationSpec) {
    ProgramId programId = appId.workflow(scheduleCreationSpec.getProgramName());
    Trigger trigger = scheduleCreationSpec.getTrigger();
    return new ProgramSchedule(scheduleCreationSpec.getName(), scheduleCreationSpec.getDescription(), programId, scheduleCreationSpec.getProperties(), trigger, scheduleCreationSpec.getConstraints(), scheduleCreationSpec.getTimeoutMillis());
}
Also used : Trigger(co.cask.cdap.api.schedule.Trigger) ProgramSchedule(co.cask.cdap.internal.app.runtime.schedule.ProgramSchedule) ProgramId(co.cask.cdap.proto.id.ProgramId)

Example 3 with Trigger

use of co.cask.cdap.api.schedule.Trigger 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<ScheduleId> modifySchedulesTriggeredByDeletedProgram(ProgramId programId) {
    List<ScheduleId> 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();
        try {
            deleteSchedule(schedule.getScheduleId());
        } catch (NotFoundException e) {
            // this should never happen
            LOG.warn("Failed to delete the schedule '{}' triggered by '{}', skip this schedule.", schedule.getScheduleId(), programId, e);
            continue;
        }
        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.getScheduleId());
                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.getScheduleId());
        }
    }
    return deleted;
}
Also used : SatisfiableTrigger(co.cask.cdap.internal.app.runtime.schedule.trigger.SatisfiableTrigger) Trigger(co.cask.cdap.api.schedule.Trigger) AbstractSatisfiableCompositeTrigger(co.cask.cdap.internal.app.runtime.schedule.trigger.AbstractSatisfiableCompositeTrigger) ProgramStatusTrigger(co.cask.cdap.internal.app.runtime.schedule.trigger.ProgramStatusTrigger) AlreadyExistsException(co.cask.cdap.common.AlreadyExistsException) ProgramSchedule(co.cask.cdap.internal.app.runtime.schedule.ProgramSchedule) ArrayList(java.util.ArrayList) AbstractSatisfiableCompositeTrigger(co.cask.cdap.internal.app.runtime.schedule.trigger.AbstractSatisfiableCompositeTrigger) NotFoundException(co.cask.cdap.common.NotFoundException) ProgramScheduleRecord(co.cask.cdap.internal.app.runtime.schedule.ProgramScheduleRecord) ScheduleId(co.cask.cdap.proto.id.ScheduleId) HashSet(java.util.HashSet) ProgramStatus(co.cask.cdap.api.ProgramStatus)

Example 4 with Trigger

use of co.cask.cdap.api.schedule.Trigger in project cdap by caskdata.

the class TriggerInfoCodec method deserialize.

@Override
public TriggerInfo deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
    if (json == null) {
        return null;
    }
    if (!(json instanceof JsonObject)) {
        throw new JsonParseException("Expected a JsonObject but found a " + json.getClass().getName());
    }
    JsonObject object = (JsonObject) json;
    JsonElement typeJson = object.get("type");
    Trigger.Type triggerType = context.deserialize(typeJson, Trigger.Type.class);
    Class<? extends TriggerInfo> subClass = typeClassMap.get(triggerType);
    if (subClass == null) {
        throw new JsonParseException("Unable to map trigger type " + triggerType + " to a TriggerInfo class");
    }
    return context.deserialize(json, subClass);
}
Also used : Trigger(co.cask.cdap.api.schedule.Trigger) JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject) JsonParseException(com.google.gson.JsonParseException)

Example 5 with Trigger

use of co.cask.cdap.api.schedule.Trigger in project cdap by caskdata.

the class AbstractSatisfiableCompositeTrigger method initializeUnitTriggers.

private void initializeUnitTriggers() {
    unitTriggers = new HashMap<>();
    for (Trigger trigger : getTriggers()) {
        // Add current non-composite trigger to the corresponding set in the map
        Type triggerType = trigger.getType();
        if (trigger instanceof AbstractSatisfiableCompositeTrigger) {
            // If the current trigger is a composite trigger, add each of its unit triggers to the set according to type
            for (Map.Entry<Type, Set<SatisfiableTrigger>> entry : ((AbstractSatisfiableCompositeTrigger) trigger).getUnitTriggers().entrySet()) {
                Set<SatisfiableTrigger> innerUnitTriggerSet = unitTriggers.get(entry.getKey());
                if (innerUnitTriggerSet == null) {
                    innerUnitTriggerSet = new HashSet<>();
                    unitTriggers.put(entry.getKey(), innerUnitTriggerSet);
                }
                innerUnitTriggerSet.addAll(entry.getValue());
            }
        } else {
            // If the current trigger is a non-composite trigger, add it to the set according to its type
            Set<SatisfiableTrigger> triggerSet = unitTriggers.get(triggerType);
            if (triggerSet == null) {
                triggerSet = new HashSet<>();
                unitTriggers.put(triggerType, triggerSet);
            }
            triggerSet.add((SatisfiableTrigger) trigger);
        }
    }
}
Also used : Trigger(co.cask.cdap.api.schedule.Trigger) ProtoTrigger(co.cask.cdap.proto.ProtoTrigger) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

Trigger (co.cask.cdap.api.schedule.Trigger)9 ProgramSchedule (co.cask.cdap.internal.app.runtime.schedule.ProgramSchedule)3 ProtoTrigger (co.cask.cdap.proto.ProtoTrigger)2 ProgramId (co.cask.cdap.proto.id.ProgramId)2 HashSet (java.util.HashSet)2 ProgramStatus (co.cask.cdap.api.ProgramStatus)1 AlreadyExistsException (co.cask.cdap.common.AlreadyExistsException)1 BadRequestException (co.cask.cdap.common.BadRequestException)1 NotFoundException (co.cask.cdap.common.NotFoundException)1 ProgramScheduleRecord (co.cask.cdap.internal.app.runtime.schedule.ProgramScheduleRecord)1 AbstractSatisfiableCompositeTrigger (co.cask.cdap.internal.app.runtime.schedule.trigger.AbstractSatisfiableCompositeTrigger)1 ProgramStatusTrigger (co.cask.cdap.internal.app.runtime.schedule.trigger.ProgramStatusTrigger)1 SatisfiableTrigger (co.cask.cdap.internal.app.runtime.schedule.trigger.SatisfiableTrigger)1 ScheduleId (co.cask.cdap.proto.id.ScheduleId)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 JsonParseException (com.google.gson.JsonParseException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1