Search in sources :

Example 1 with AndTrigger

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

the class ProgramScheduleStoreDatasetTest method testDeleteScheduleByTriggeringProgram.

@Test
public void testDeleteScheduleByTriggeringProgram() {
    TransactionRunner transactionRunner = getTransactionRunner();
    SatisfiableTrigger prog1Trigger = new ProgramStatusTrigger(PROG1_ID, ProgramStatus.COMPLETED, ProgramStatus.FAILED, ProgramStatus.KILLED);
    SatisfiableTrigger prog2Trigger = new ProgramStatusTrigger(PROG2_ID, ProgramStatus.COMPLETED, ProgramStatus.FAILED, ProgramStatus.KILLED);
    final ProgramSchedule sched1 = new ProgramSchedule("sched1", "a program status trigger", PROG3_ID, ImmutableMap.of("propper", "popper"), prog1Trigger, ImmutableList.<Constraint>of());
    final ProgramSchedule sched2 = new ProgramSchedule("sched2", "a program status trigger", PROG3_ID, ImmutableMap.of("propper", "popper"), prog2Trigger, ImmutableList.<Constraint>of());
    final ProgramSchedule schedOr = new ProgramSchedule("schedOr", "an OR trigger", PROG3_ID, ImmutableMap.of("propper", "popper"), new OrTrigger(new PartitionTrigger(DS1_ID, 1), prog1Trigger, new AndTrigger(new OrTrigger(prog1Trigger, prog2Trigger), new PartitionTrigger(DS2_ID, 1)), new OrTrigger(prog2Trigger)), ImmutableList.<Constraint>of());
    final ProgramSchedule schedAnd = new ProgramSchedule("schedAnd", "an AND trigger", PROG3_ID, ImmutableMap.of("propper", "popper"), new AndTrigger(new PartitionTrigger(DS1_ID, 1), prog2Trigger, new AndTrigger(prog1Trigger, new PartitionTrigger(DS2_ID, 1))), ImmutableList.<Constraint>of());
    TransactionRunners.run(transactionRunner, context -> {
        ProgramScheduleStoreDataset store = Schedulers.getScheduleStore(context);
        store.addSchedules(ImmutableList.of(sched1, sched2, schedOr, schedAnd));
    });
    TransactionRunners.run(transactionRunner, context -> {
        ProgramScheduleStoreDataset store = Schedulers.getScheduleStore(context);
        // ProgramStatus event for PROG1_ID should trigger only sched1, schedOr, schedAnd
        Assert.assertEquals(ImmutableSet.of(sched1, schedOr, schedAnd), toScheduleSet(store.findSchedules(Schedulers.triggerKeyForProgramStatus(PROG1_ID, ProgramStatus.COMPLETED))));
        // ProgramStatus event for PROG2_ID should trigger only sched2, schedOr, schedAnd
        Assert.assertEquals(ImmutableSet.of(sched2, schedOr, schedAnd), toScheduleSet(store.findSchedules(Schedulers.triggerKeyForProgramStatus(PROG2_ID, ProgramStatus.FAILED))));
    });
    // update or delete all schedules triggered by PROG1_ID
    TransactionRunners.run(transactionRunner, context -> {
        ProgramScheduleStoreDataset store = Schedulers.getScheduleStore(context);
        store.modifySchedulesTriggeredByDeletedProgram(PROG1_ID);
    });
    final ProgramSchedule schedOrNew = new ProgramSchedule("schedOr", "an OR trigger", PROG3_ID, ImmutableMap.of("propper", "popper"), new OrTrigger(new PartitionTrigger(DS1_ID, 1), new AndTrigger(prog2Trigger, new PartitionTrigger(DS2_ID, 1)), prog2Trigger), ImmutableList.of());
    TransactionRunners.run(transactionRunner, context -> {
        ProgramScheduleStoreDataset store = Schedulers.getScheduleStore(context);
        // ProgramStatus event for PROG1_ID should trigger no schedules after modifying schedules triggered by it
        Assert.assertEquals(Collections.emptySet(), toScheduleSet(store.findSchedules(Schedulers.triggerKeyForProgramStatus(PROG1_ID, ProgramStatus.COMPLETED))));
        Assert.assertEquals(Collections.emptySet(), toScheduleSet(store.findSchedules(Schedulers.triggerKeyForProgramStatus(PROG1_ID, ProgramStatus.FAILED))));
        Assert.assertEquals(Collections.emptySet(), toScheduleSet(store.findSchedules(Schedulers.triggerKeyForProgramStatus(PROG1_ID, ProgramStatus.KILLED))));
        // ProgramStatus event for PROG2_ID should trigger only sched2 and schedOrNew
        Assert.assertEquals(ImmutableSet.of(sched2, schedOrNew), toScheduleSet(store.findSchedules(Schedulers.triggerKeyForProgramStatus(PROG2_ID, ProgramStatus.FAILED))));
    });
    // update or delete all schedules triggered by PROG2_ID
    TransactionRunners.run(transactionRunner, context -> {
        ProgramScheduleStoreDataset store = Schedulers.getScheduleStore(context);
        store.modifySchedulesTriggeredByDeletedProgram(PROG2_ID);
    });
    final ProgramSchedule schedOrNew1 = new ProgramSchedule("schedOr", "an OR trigger", PROG3_ID, ImmutableMap.of("propper", "popper"), new PartitionTrigger(DS1_ID, 1), ImmutableList.of());
    final Set<ProgramSchedule> ds1Schedules = new HashSet<>();
    TransactionRunners.run(transactionRunner, context -> {
        ProgramScheduleStoreDataset store = Schedulers.getScheduleStore(context);
        // ProgramStatus event for PROG2_ID should trigger no schedules after modifying schedules triggered by it
        Assert.assertEquals(Collections.emptySet(), toScheduleSet(store.findSchedules(Schedulers.triggerKeyForProgramStatus(PROG2_ID, ProgramStatus.COMPLETED))));
        Assert.assertEquals(Collections.emptySet(), toScheduleSet(store.findSchedules(Schedulers.triggerKeyForProgramStatus(PROG2_ID, ProgramStatus.FAILED))));
        Assert.assertEquals(Collections.emptySet(), toScheduleSet(store.findSchedules(Schedulers.triggerKeyForProgramStatus(PROG2_ID, ProgramStatus.KILLED))));
        // event for DS1 should trigger only schedOrNew1 since all other schedules are deleted
        ds1Schedules.addAll(toScheduleSet(store.findSchedules(Schedulers.triggerKeyForPartition(DS1_ID))));
    });
    Assert.assertEquals(ImmutableSet.of(schedOrNew1), ds1Schedules);
}
Also used : SatisfiableTrigger(io.cdap.cdap.internal.app.runtime.schedule.trigger.SatisfiableTrigger) ProgramSchedule(io.cdap.cdap.internal.app.runtime.schedule.ProgramSchedule) OrTrigger(io.cdap.cdap.internal.app.runtime.schedule.trigger.OrTrigger) TransactionRunner(io.cdap.cdap.spi.data.transaction.TransactionRunner) ProgramStatusTrigger(io.cdap.cdap.internal.app.runtime.schedule.trigger.ProgramStatusTrigger) PartitionTrigger(io.cdap.cdap.internal.app.runtime.schedule.trigger.PartitionTrigger) AndTrigger(io.cdap.cdap.internal.app.runtime.schedule.trigger.AndTrigger) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

ProgramSchedule (io.cdap.cdap.internal.app.runtime.schedule.ProgramSchedule)1 AndTrigger (io.cdap.cdap.internal.app.runtime.schedule.trigger.AndTrigger)1 OrTrigger (io.cdap.cdap.internal.app.runtime.schedule.trigger.OrTrigger)1 PartitionTrigger (io.cdap.cdap.internal.app.runtime.schedule.trigger.PartitionTrigger)1 ProgramStatusTrigger (io.cdap.cdap.internal.app.runtime.schedule.trigger.ProgramStatusTrigger)1 SatisfiableTrigger (io.cdap.cdap.internal.app.runtime.schedule.trigger.SatisfiableTrigger)1 TransactionRunner (io.cdap.cdap.spi.data.transaction.TransactionRunner)1 HashSet (java.util.HashSet)1 Test (org.junit.Test)1