Search in sources :

Example 1 with Trigger

use of io.cdap.cdap.api.schedule.Trigger in project cdap by caskdata.

the class AbstractCompositeTriggerBuilder method getBuiltTriggers.

protected SatisfiableTrigger[] getBuiltTriggers(String namespace, String applicationName, String applicationVersion) {
    int numTriggers = triggers.length;
    SatisfiableTrigger[] builtTriggers = new SatisfiableTrigger[numTriggers];
    for (int i = 0; i < numTriggers; i++) {
        Trigger trigger = triggers[i];
        builtTriggers[i] = trigger instanceof TriggerBuilder ? ((TriggerBuilder) trigger).build(namespace, applicationName, applicationVersion) : (SatisfiableTrigger) trigger;
    }
    return builtTriggers;
}
Also used : Trigger(io.cdap.cdap.api.schedule.Trigger)

Example 2 with Trigger

use of io.cdap.cdap.api.schedule.Trigger in project cdap by caskdata.

the class AppWithFrequentScheduledWorkflows method configure.

@Override
public void configure() {
    setName(NAME);
    setDescription("Sample application with multiple Workflows");
    addWorkflow(new DummyWorkflow(SOME_WORKFLOW));
    addWorkflow(new DummyWorkflow(ANOTHER_WORKFLOW));
    addWorkflow(new DummyWorkflow(SCHEDULED_WORKFLOW_1));
    addWorkflow(new DummyWorkflow(SCHEDULED_WORKFLOW_2));
    addWorkflow(new DummyWorkflow(COMPOSITE_WORKFLOW));
    schedule(buildSchedule(DATASET_PARTITION_SCHEDULE_1, ProgramType.WORKFLOW, SOME_WORKFLOW).triggerOnPartitions(DATASET_NAME1, 1));
    schedule(buildSchedule(DATASET_PARTITION_SCHEDULE_2, ProgramType.WORKFLOW, ANOTHER_WORKFLOW).triggerOnPartitions(DATASET_NAME2, 2));
    // Schedule the workflow to run in every ten seconds
    schedule(buildSchedule(TEN_SECOND_SCHEDULE_1, ProgramType.WORKFLOW, SCHEDULED_WORKFLOW_1).triggerByTime("*/10 * * * * ?"));
    // Schedule the workflow to run in every ten seconds
    schedule(buildSchedule(TEN_SECOND_SCHEDULE_2, ProgramType.WORKFLOW, SCHEDULED_WORKFLOW_2).triggerByTime("*/10 * * * * ?"));
    // OrTrigger with only PartitionTrigger to be triggered
    Trigger orTrigger1 = getTriggerFactory().or(getTriggerFactory().onPartitions(DATASET_NAME2, 3), getTriggerFactory().onProgramStatus(ProgramType.WORKFLOW, SCHEDULED_WORKFLOW_1, ProgramStatus.KILLED));
    // OrTrigger with only TimeTrigger to be triggered
    Trigger orTrigger2 = getTriggerFactory().or(getTriggerFactory().byTime("*/5 * * * * ?"), getTriggerFactory().onProgramStatus(ProgramType.WORKFLOW, SCHEDULED_WORKFLOW_1, ProgramStatus.KILLED));
    schedule(buildSchedule(COMPOSITE_SCHEDULE, ProgramType.WORKFLOW, COMPOSITE_WORKFLOW).triggerOn(getTriggerFactory().and(orTrigger1, orTrigger2)));
}
Also used : Trigger(io.cdap.cdap.api.schedule.Trigger)

Example 3 with Trigger

use of io.cdap.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(io.cdap.cdap.api.schedule.Trigger)

Example 4 with Trigger

use of io.cdap.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<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)

Example 5 with Trigger

use of io.cdap.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(io.cdap.cdap.api.schedule.Trigger) JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject) JsonParseException(com.google.gson.JsonParseException)

Aggregations

Trigger (io.cdap.cdap.api.schedule.Trigger)9 ProgramSchedule (io.cdap.cdap.internal.app.runtime.schedule.ProgramSchedule)3 ProtoTrigger (io.cdap.cdap.proto.ProtoTrigger)2 ProgramId (io.cdap.cdap.proto.id.ProgramId)2 HashSet (java.util.HashSet)2 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 ProgramStatus (io.cdap.cdap.api.ProgramStatus)1 AlreadyExistsException (io.cdap.cdap.common.AlreadyExistsException)1 BadRequestException (io.cdap.cdap.common.BadRequestException)1 ProgramScheduleRecord (io.cdap.cdap.internal.app.runtime.schedule.ProgramScheduleRecord)1 AbstractSatisfiableCompositeTrigger (io.cdap.cdap.internal.app.runtime.schedule.trigger.AbstractSatisfiableCompositeTrigger)1 ProgramStatusTrigger (io.cdap.cdap.internal.app.runtime.schedule.trigger.ProgramStatusTrigger)1 SatisfiableTrigger (io.cdap.cdap.internal.app.runtime.schedule.trigger.SatisfiableTrigger)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Set (java.util.Set)1