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