Search in sources :

Example 1 with SimpleJob

use of io.cdap.cdap.internal.app.runtime.schedule.queue.SimpleJob in project cdap by caskdata.

the class ConcurrencyConstraintTest method testMaxConcurrentRuns.

@Test
public void testMaxConcurrentRuns() {
    Store store = AppFabricTestHelper.getInjector().getInstance(Store.class);
    try {
        long now = System.currentTimeMillis();
        ProgramSchedule schedule = new ProgramSchedule("SCHED1", "one partition schedule", WORKFLOW_ID, ImmutableMap.of("prop3", "abc"), new PartitionTrigger(DATASET_ID, 1), ImmutableList.of());
        SimpleJob job = new SimpleJob(schedule, 0, now, Collections.emptyList(), Job.State.PENDING_TRIGGER, 0L);
        ConcurrencyConstraint concurrencyConstraint = new ConcurrencyConstraint(2);
        ConstraintContext constraintContext = new ConstraintContext(job, now, store);
        assertSatisfied(true, concurrencyConstraint.check(schedule, constraintContext));
        ProgramRunId pid1 = WORKFLOW_ID.run(RunIds.generate().getId());
        ProgramRunId pid2 = WORKFLOW_ID.run(RunIds.generate().getId());
        ProgramRunId pid3 = WORKFLOW_ID.run(RunIds.generate().getId());
        // add a run for the schedule
        Map<String, String> systemArgs = ImmutableMap.of(ProgramOptionConstants.SCHEDULE_NAME, schedule.getName());
        setStartAndRunning(store, pid1, EMPTY_MAP, systemArgs);
        assertSatisfied(true, concurrencyConstraint.check(schedule, constraintContext));
        // add a run for the program from a different schedule. Since there are now 2 running instances of the
        // workflow (regardless of the schedule name), the constraint is not met
        systemArgs = ImmutableMap.of(ProgramOptionConstants.SCHEDULE_NAME, "not" + schedule.getName());
        setStartAndRunning(store, pid2, EMPTY_MAP, systemArgs);
        assertSatisfied(false, concurrencyConstraint.check(schedule, constraintContext));
        // add a run for the program that wasn't from a schedule
        // there are now three concurrent runs, so the constraint will not be met
        setStartAndRunning(store, pid3);
        assertSatisfied(false, concurrencyConstraint.check(schedule, constraintContext));
        // stop the first program; constraint will not be satisfied as there are still 2 running
        store.setStop(pid1, System.currentTimeMillis(), ProgramRunStatus.COMPLETED, AppFabricTestHelper.createSourceId(++sourceId));
        assertSatisfied(false, concurrencyConstraint.check(schedule, constraintContext));
        // suspending/resuming the workflow doesn't reduce its concurrency count
        store.setSuspend(pid3, AppFabricTestHelper.createSourceId(++sourceId), -1);
        assertSatisfied(false, concurrencyConstraint.check(schedule, constraintContext));
        store.setResume(pid3, AppFabricTestHelper.createSourceId(++sourceId), -1);
        assertSatisfied(false, concurrencyConstraint.check(schedule, constraintContext));
        // but the constraint will be satisfied with it completes, as there is only 1 remaining RUNNING
        store.setStop(pid3, System.currentTimeMillis(), ProgramRunStatus.KILLED, AppFabricTestHelper.createSourceId(++sourceId));
        assertSatisfied(true, concurrencyConstraint.check(schedule, constraintContext));
        // add a run in provisioning state, constraint will not be satisfied since active runs increased to 2
        ProgramRunId pid4 = WORKFLOW_ID.run(RunIds.generate().getId());
        setProvisioning(store, pid4, Collections.emptyMap(), Collections.emptyMap());
        assertSatisfied(false, concurrencyConstraint.check(schedule, constraintContext));
        // stop the provisioning run, constraint will be satisfied since active runs decreased to 1
        store.setStop(pid4, System.currentTimeMillis(), ProgramRunStatus.FAILED, AppFabricTestHelper.createSourceId(++sourceId));
        assertSatisfied(true, concurrencyConstraint.check(schedule, constraintContext));
        // stopping the last running workflow will also satisfy the constraint
        store.setStop(pid2, System.currentTimeMillis(), ProgramRunStatus.FAILED, AppFabricTestHelper.createSourceId(++sourceId));
        assertSatisfied(true, concurrencyConstraint.check(schedule, constraintContext));
    } finally {
        AppFabricTestHelper.shutdown();
    }
}
Also used : ProgramSchedule(io.cdap.cdap.internal.app.runtime.schedule.ProgramSchedule) Store(io.cdap.cdap.app.store.Store) SimpleJob(io.cdap.cdap.internal.app.runtime.schedule.queue.SimpleJob) ProgramRunId(io.cdap.cdap.proto.id.ProgramRunId) PartitionTrigger(io.cdap.cdap.internal.app.runtime.schedule.trigger.PartitionTrigger) Test(org.junit.Test)

Example 2 with SimpleJob

use of io.cdap.cdap.internal.app.runtime.schedule.queue.SimpleJob in project cdap by caskdata.

the class DelayConstraintTest method testDelayConstraint.

@Test
public void testDelayConstraint() {
    long now = System.currentTimeMillis();
    ProgramSchedule schedule = new ProgramSchedule("SCHED1", "one partition schedule", WORKFLOW_ID, ImmutableMap.of("prop3", "abc"), new PartitionTrigger(DATASET_ID, 1), ImmutableList.<Constraint>of());
    SimpleJob job = new SimpleJob(schedule, 0, now, Collections.<Notification>emptyList(), Job.State.PENDING_TRIGGER, 0L);
    // test with 10 minute delay
    DelayConstraint tenMinuteDelayConstraint = new DelayConstraint(10, TimeUnit.MINUTES);
    // a check against 12 minutes after 'now' will return SATISFIED
    ConstraintContext constraintContext = new ConstraintContext(job, now + TimeUnit.MINUTES.toMillis(12), null);
    ConstraintResult result = tenMinuteDelayConstraint.check(schedule, constraintContext);
    Assert.assertEquals(ConstraintResult.SATISFIED, result);
    // a check against 9 minutes after 'now' will return NOT_SATISFIED, with 1 minute to wait until next retry
    constraintContext = new ConstraintContext(job, now + TimeUnit.MINUTES.toMillis(9), null);
    result = tenMinuteDelayConstraint.check(schedule, constraintContext);
    Assert.assertEquals(ConstraintResult.SatisfiedState.NOT_SATISFIED, result.getSatisfiedState());
    Assert.assertEquals(constraintContext.getCheckTimeMillis() + TimeUnit.MINUTES.toMillis(1), (long) result.getNextCheckTime());
}
Also used : ProgramSchedule(io.cdap.cdap.internal.app.runtime.schedule.ProgramSchedule) SimpleJob(io.cdap.cdap.internal.app.runtime.schedule.queue.SimpleJob) PartitionTrigger(io.cdap.cdap.internal.app.runtime.schedule.trigger.PartitionTrigger) Test(org.junit.Test)

Example 3 with SimpleJob

use of io.cdap.cdap.internal.app.runtime.schedule.queue.SimpleJob in project cdap by caskdata.

the class TimeRangeConstraintTest method testReverseRange.

@Test
public void testReverseRange() {
    // 3:24PM PST
    long now = 1494368640000L;
    SimpleJob job = new SimpleJob(SCHEDULE, 0, now, Collections.<Notification>emptyList(), Job.State.PENDING_TRIGGER, 0L);
    // use a TimeRangeConstraint [10:00PM, 6:00AM)
    TimeRangeConstraint timeRangeConstraint = new TimeRangeConstraint("22:00", "06:00", TimeZone.getTimeZone("PST"));
    ConstraintContext constraintContext = createConstraintContext(job, now);
    ConstraintResult result = timeRangeConstraint.check(SCHEDULE, constraintContext);
    Assert.assertEquals(ConstraintResult.SatisfiedState.NOT_SATISFIED, result.getSatisfiedState());
    // 6 hours + 36 minutes till 4PM
    long sixHoursAnd36Minutes = TimeUnit.HOURS.toMillis(6) + TimeUnit.MINUTES.toMillis(36);
    Assert.assertEquals(constraintContext.getCheckTimeMillis() + sixHoursAnd36Minutes, (long) result.getNextCheckTime());
    constraintContext = createConstraintContext(job, result.getNextCheckTime() - 1);
    result = timeRangeConstraint.check(SCHEDULE, constraintContext);
    Assert.assertEquals(ConstraintResult.SatisfiedState.NOT_SATISFIED, result.getSatisfiedState());
    Assert.assertEquals(constraintContext.getCheckTimeMillis() + 1L, (long) result.getNextCheckTime());
    result = timeRangeConstraint.check(SCHEDULE, createConstraintContext(job, now + sixHoursAnd36Minutes));
    Assert.assertEquals(ConstraintResult.SATISFIED, result);
    // 5:00PM PST
    long fivePM = 1494374400000L;
    constraintContext = createConstraintContext(job, fivePM);
    result = timeRangeConstraint.check(SCHEDULE, constraintContext);
    Assert.assertEquals(ConstraintResult.SatisfiedState.NOT_SATISFIED, result.getSatisfiedState());
    // 5 hours until the next time its 10PM again
    Assert.assertEquals(constraintContext.getCheckTimeMillis() + TimeUnit.HOURS.toMillis(5), (long) result.getNextCheckTime());
    // 5:00AM PST
    long fiveAM = 1494331200000L;
    constraintContext = createConstraintContext(job, fiveAM);
    result = timeRangeConstraint.check(SCHEDULE, constraintContext);
    Assert.assertEquals(ConstraintResult.SatisfiedState.SATISFIED, result.getSatisfiedState());
    // 6:00AM PST - not satisfied, because the end range is exclusive
    long sixAM = 1494334800000L;
    constraintContext = createConstraintContext(job, sixAM);
    result = timeRangeConstraint.check(SCHEDULE, constraintContext);
    Assert.assertEquals(ConstraintResult.SatisfiedState.NOT_SATISFIED, result.getSatisfiedState());
    // 16 hours until the next time its 10PM
    Assert.assertEquals(constraintContext.getCheckTimeMillis() + TimeUnit.HOURS.toMillis(16), (long) result.getNextCheckTime());
    // 7:00AM PST
    long sevenAM = 1494338400000L;
    constraintContext = createConstraintContext(job, sevenAM);
    result = timeRangeConstraint.check(SCHEDULE, constraintContext);
    Assert.assertEquals(ConstraintResult.SatisfiedState.NOT_SATISFIED, result.getSatisfiedState());
    // 15 hours until the next time its 10PM
    Assert.assertEquals(constraintContext.getCheckTimeMillis() + TimeUnit.HOURS.toMillis(15), (long) result.getNextCheckTime());
}
Also used : SimpleJob(io.cdap.cdap.internal.app.runtime.schedule.queue.SimpleJob) Test(org.junit.Test)

Example 4 with SimpleJob

use of io.cdap.cdap.internal.app.runtime.schedule.queue.SimpleJob in project cdap by caskdata.

the class TimeRangeConstraintTest method testForwardRange.

@Test
public void testForwardRange() {
    // 3:24PM PST
    long now = 1494368640000L;
    SimpleJob job = new SimpleJob(SCHEDULE, 0, now, Collections.<Notification>emptyList(), Job.State.PENDING_TRIGGER, 0L);
    // use a TimeRangeConstraint [4:00PM, 5:00PM)
    TimeRangeConstraint timeRangeConstraint = new TimeRangeConstraint("16:00", "17:00", TimeZone.getTimeZone("PST"));
    ConstraintContext constraintContext = createConstraintContext(job, now);
    ConstraintResult result = timeRangeConstraint.check(SCHEDULE, constraintContext);
    Assert.assertEquals(ConstraintResult.SatisfiedState.NOT_SATISFIED, result.getSatisfiedState());
    // 36 minutes till 4PM
    Assert.assertEquals(constraintContext.getCheckTimeMillis() + TimeUnit.MINUTES.toMillis(36), (long) result.getNextCheckTime());
    constraintContext = createConstraintContext(job, result.getNextCheckTime() - 1);
    result = timeRangeConstraint.check(SCHEDULE, constraintContext);
    Assert.assertEquals(ConstraintResult.SatisfiedState.NOT_SATISFIED, result.getSatisfiedState());
    Assert.assertEquals(constraintContext.getCheckTimeMillis() + 1L, (long) result.getNextCheckTime());
    result = timeRangeConstraint.check(SCHEDULE, createConstraintContext(job, now + TimeUnit.MINUTES.toMillis(36)));
    Assert.assertEquals(ConstraintResult.SATISFIED, result);
    // 5:00PM PST
    long fivePM = 1494374400000L;
    constraintContext = createConstraintContext(job, fivePM);
    result = timeRangeConstraint.check(SCHEDULE, constraintContext);
    Assert.assertEquals(ConstraintResult.SatisfiedState.NOT_SATISFIED, result.getSatisfiedState());
    // 23 hours until the next time its 4PM again
    Assert.assertEquals(constraintContext.getCheckTimeMillis() + TimeUnit.HOURS.toMillis(23), (long) result.getNextCheckTime());
}
Also used : SimpleJob(io.cdap.cdap.internal.app.runtime.schedule.queue.SimpleJob) Test(org.junit.Test)

Example 5 with SimpleJob

use of io.cdap.cdap.internal.app.runtime.schedule.queue.SimpleJob in project cdap by caskdata.

the class LastRunConstraintTest method testLastRunConstraint.

@Test
public void testLastRunConstraint() {
    Store store = AppFabricTestHelper.getInjector().getInstance(Store.class);
    try {
        long now = System.currentTimeMillis();
        long nowSec = TimeUnit.MILLISECONDS.toSeconds(now);
        ProgramSchedule schedule = new ProgramSchedule("SCHED1", "one partition schedule", WORKFLOW_ID, ImmutableMap.of("prop3", "abc"), new PartitionTrigger(DATASET_ID, 1), ImmutableList.of());
        SimpleJob job = new SimpleJob(schedule, 0, now, Collections.emptyList(), Job.State.PENDING_TRIGGER, 0L);
        // require 1 hour since last run
        LastRunConstraint lastRunConstraint = new LastRunConstraint(1, TimeUnit.HOURS);
        ConstraintContext constraintContext = new ConstraintContext(job, now, store);
        // there's been no runs, so the constraint is satisfied by default
        assertSatisfied(true, lastRunConstraint.check(schedule, constraintContext));
        // a RUNNING workflow, started 3 hours ago will fail the constraint check
        ProgramRunId pid1 = WORKFLOW_ID.run(RunIds.generate(nowSec - TimeUnit.HOURS.toSeconds(3)).getId());
        Map<String, String> systemArgs = ImmutableMap.of(ProgramOptionConstants.SCHEDULE_NAME, schedule.getName());
        setStartAndRunning(store, pid1, EMPTY_MAP, systemArgs);
        assertSatisfied(false, lastRunConstraint.check(schedule, constraintContext));
        // a SUSPENDED workflow started 3 hours ago will also fail the constraint check
        store.setSuspend(pid1, AppFabricTestHelper.createSourceId(++sourceId), -1);
        assertSatisfied(false, lastRunConstraint.check(schedule, constraintContext));
        store.setResume(pid1, AppFabricTestHelper.createSourceId(++sourceId), -1);
        assertSatisfied(false, lastRunConstraint.check(schedule, constraintContext));
        // if that same workflow runs completes 2 hours ago, the constraint check will be satisfied
        store.setStop(pid1, nowSec - TimeUnit.HOURS.toSeconds(2), ProgramRunStatus.COMPLETED, AppFabricTestHelper.createSourceId(++sourceId));
        assertSatisfied(true, lastRunConstraint.check(schedule, constraintContext));
        // a RUNNING workflow, started 2 hours ago will fail the constraint check
        ProgramRunId pid2 = WORKFLOW_ID.run(RunIds.generate(nowSec - TimeUnit.HOURS.toSeconds(2)).getId());
        setStartAndRunning(store, pid2);
        assertSatisfied(false, lastRunConstraint.check(schedule, constraintContext));
        // if that same workflow run fails 1 minute ago, the constraint check will be satisfied
        store.setStop(pid2, nowSec - TimeUnit.MINUTES.toSeconds(1), ProgramRunStatus.FAILED, AppFabricTestHelper.createSourceId(++sourceId));
        assertSatisfied(true, lastRunConstraint.check(schedule, constraintContext));
        // similarly, a KILLED workflow, started 2 hours ago will also fail the constraint check
        ProgramRunId pid3 = WORKFLOW_ID.run(RunIds.generate(nowSec - TimeUnit.HOURS.toSeconds(2)).getId());
        setStartAndRunning(store, pid3);
        assertSatisfied(false, lastRunConstraint.check(schedule, constraintContext));
        store.setStop(pid3, nowSec - TimeUnit.MINUTES.toSeconds(1), ProgramRunStatus.KILLED, AppFabricTestHelper.createSourceId(++sourceId));
        assertSatisfied(true, lastRunConstraint.check(schedule, constraintContext));
        // a RUNNING workflow, started 2 hours ago will fail the constraint check
        ProgramRunId pid4 = WORKFLOW_ID.run(RunIds.generate(nowSec - TimeUnit.HOURS.toSeconds(2)).getId());
        setStartAndRunning(store, pid4);
        assertSatisfied(false, lastRunConstraint.check(schedule, constraintContext));
        // if that same workflow runs completes 1 minute ago, the constraint check will not be satisfied
        store.setStop(pid4, nowSec - TimeUnit.MINUTES.toSeconds(1), ProgramRunStatus.COMPLETED, AppFabricTestHelper.createSourceId(++sourceId));
        assertSatisfied(false, lastRunConstraint.check(schedule, constraintContext));
    } finally {
        AppFabricTestHelper.shutdown();
    }
}
Also used : ProgramSchedule(io.cdap.cdap.internal.app.runtime.schedule.ProgramSchedule) Store(io.cdap.cdap.app.store.Store) SimpleJob(io.cdap.cdap.internal.app.runtime.schedule.queue.SimpleJob) ProgramRunId(io.cdap.cdap.proto.id.ProgramRunId) PartitionTrigger(io.cdap.cdap.internal.app.runtime.schedule.trigger.PartitionTrigger) Test(org.junit.Test)

Aggregations

SimpleJob (io.cdap.cdap.internal.app.runtime.schedule.queue.SimpleJob)5 Test (org.junit.Test)5 ProgramSchedule (io.cdap.cdap.internal.app.runtime.schedule.ProgramSchedule)3 PartitionTrigger (io.cdap.cdap.internal.app.runtime.schedule.trigger.PartitionTrigger)3 Store (io.cdap.cdap.app.store.Store)2 ProgramRunId (io.cdap.cdap.proto.id.ProgramRunId)2