Search in sources :

Example 1 with SimpleJob

use of co.cask.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);
    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, now, Collections.<Notification>emptyList(), Job.State.PENDING_TRIGGER, 0L);
    ConcurrencyConstraint concurrencyConstraint = new ConcurrencyConstraint(2);
    ConstraintContext constraintContext = new ConstraintContext(job, now, store);
    assertSatisfied(true, concurrencyConstraint.check(schedule, constraintContext));
    String pid1 = RunIds.generate().getId();
    String pid2 = RunIds.generate().getId();
    String pid3 = RunIds.generate().getId();
    // add a run for the schedule
    Map<String, String> systemArgs = ImmutableMap.of(ProgramOptionConstants.SCHEDULE_NAME, schedule.getName());
    store.setStart(WORKFLOW_ID, pid1, System.currentTimeMillis(), null, 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());
    store.setStart(WORKFLOW_ID, pid2, System.currentTimeMillis(), null, 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
    store.setStart(WORKFLOW_ID, pid3, System.currentTimeMillis(), null, EMPTY_MAP, EMPTY_MAP);
    assertSatisfied(false, concurrencyConstraint.check(schedule, constraintContext));
    // stop the first program; constraint will not be satisfied as there are still 2 running
    store.setStop(WORKFLOW_ID, pid1, System.currentTimeMillis(), ProgramRunStatus.COMPLETED);
    assertSatisfied(false, concurrencyConstraint.check(schedule, constraintContext));
    // suspending/resuming the workflow doesn't reduce its concurrency count
    store.setSuspend(WORKFLOW_ID, pid3);
    assertSatisfied(false, concurrencyConstraint.check(schedule, constraintContext));
    store.setResume(WORKFLOW_ID, pid3);
    assertSatisfied(false, concurrencyConstraint.check(schedule, constraintContext));
    // but the constraint will be satisfied with it completes, as there is only 1 remaining RUNNING
    store.setStop(WORKFLOW_ID, pid3, System.currentTimeMillis(), ProgramRunStatus.KILLED);
    assertSatisfied(true, concurrencyConstraint.check(schedule, constraintContext));
    // stopping the last running workflow will also satisfy the constraint
    store.setStop(WORKFLOW_ID, pid2, System.currentTimeMillis(), ProgramRunStatus.FAILED);
    assertSatisfied(true, concurrencyConstraint.check(schedule, constraintContext));
}
Also used : ProgramSchedule(co.cask.cdap.internal.app.runtime.schedule.ProgramSchedule) Store(co.cask.cdap.app.store.Store) SimpleJob(co.cask.cdap.internal.app.runtime.schedule.queue.SimpleJob) PartitionTrigger(co.cask.cdap.internal.app.runtime.schedule.trigger.PartitionTrigger) Test(org.junit.Test)

Example 2 with SimpleJob

use of co.cask.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, 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(co.cask.cdap.internal.app.runtime.schedule.ProgramSchedule) SimpleJob(co.cask.cdap.internal.app.runtime.schedule.queue.SimpleJob) PartitionTrigger(co.cask.cdap.internal.app.runtime.schedule.trigger.PartitionTrigger) Test(org.junit.Test)

Example 3 with SimpleJob

use of co.cask.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, 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(co.cask.cdap.internal.app.runtime.schedule.queue.SimpleJob) Test(org.junit.Test)

Example 4 with SimpleJob

use of co.cask.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);
    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.<Constraint>of());
    SimpleJob job = new SimpleJob(schedule, now, Collections.<Notification>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));
    String pid1 = RunIds.generate().getId();
    String pid2 = RunIds.generate().getId();
    String pid3 = RunIds.generate().getId();
    String pid4 = RunIds.generate().getId();
    // a RUNNING workflow, started 3 hours ago will fail the constraint check
    Map<String, String> systemArgs = ImmutableMap.of(ProgramOptionConstants.SCHEDULE_NAME, schedule.getName());
    store.setStart(WORKFLOW_ID, pid1, nowSec - TimeUnit.HOURS.toSeconds(3), null, EMPTY_MAP, systemArgs);
    assertSatisfied(false, lastRunConstraint.check(schedule, constraintContext));
    // a SUSPENDED workflow started 3 hours ago will also fail the constraint check
    store.setSuspend(WORKFLOW_ID, pid1);
    assertSatisfied(false, lastRunConstraint.check(schedule, constraintContext));
    store.setResume(WORKFLOW_ID, pid1);
    assertSatisfied(false, lastRunConstraint.check(schedule, constraintContext));
    // if that same workflow runs completes 2 hours ago, the constraint check will be satisfied
    store.setStop(WORKFLOW_ID, pid1, nowSec - TimeUnit.HOURS.toSeconds(2), ProgramRunStatus.COMPLETED);
    assertSatisfied(true, lastRunConstraint.check(schedule, constraintContext));
    // a RUNNING workflow, started 2 hours ago will fail the constraint check
    store.setStart(WORKFLOW_ID, pid2, nowSec - TimeUnit.HOURS.toSeconds(2), null, EMPTY_MAP, EMPTY_MAP);
    assertSatisfied(false, lastRunConstraint.check(schedule, constraintContext));
    // if that same workflow run fails 1 minute ago, the constraint check will be satisfied
    store.setStop(WORKFLOW_ID, pid2, nowSec - TimeUnit.MINUTES.toSeconds(1), ProgramRunStatus.FAILED);
    assertSatisfied(true, lastRunConstraint.check(schedule, constraintContext));
    // similarly, a KILLED workflow, started 2 hours ago will also fail the constraint check
    store.setStart(WORKFLOW_ID, pid3, nowSec - TimeUnit.HOURS.toSeconds(2), null, EMPTY_MAP, EMPTY_MAP);
    assertSatisfied(false, lastRunConstraint.check(schedule, constraintContext));
    store.setStop(WORKFLOW_ID, pid3, nowSec - TimeUnit.MINUTES.toSeconds(1), ProgramRunStatus.KILLED);
    assertSatisfied(true, lastRunConstraint.check(schedule, constraintContext));
    // a RUNNING workflow, started 2 hours ago will fail the constraint check
    store.setStart(WORKFLOW_ID, pid4, nowSec - TimeUnit.HOURS.toSeconds(2), null, EMPTY_MAP, EMPTY_MAP);
    assertSatisfied(false, lastRunConstraint.check(schedule, constraintContext));
    // if that same workflow runs completes 1 minute ago, the constraint check will not be satisfied
    store.setStop(WORKFLOW_ID, pid4, nowSec - TimeUnit.MINUTES.toSeconds(1), ProgramRunStatus.COMPLETED);
    assertSatisfied(false, lastRunConstraint.check(schedule, constraintContext));
}
Also used : ProgramSchedule(co.cask.cdap.internal.app.runtime.schedule.ProgramSchedule) Store(co.cask.cdap.app.store.Store) SimpleJob(co.cask.cdap.internal.app.runtime.schedule.queue.SimpleJob) PartitionTrigger(co.cask.cdap.internal.app.runtime.schedule.trigger.PartitionTrigger) Test(org.junit.Test)

Example 5 with SimpleJob

use of co.cask.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, 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(co.cask.cdap.internal.app.runtime.schedule.queue.SimpleJob) Test(org.junit.Test)

Aggregations

SimpleJob (co.cask.cdap.internal.app.runtime.schedule.queue.SimpleJob)5 Test (org.junit.Test)5 ProgramSchedule (co.cask.cdap.internal.app.runtime.schedule.ProgramSchedule)3 PartitionTrigger (co.cask.cdap.internal.app.runtime.schedule.trigger.PartitionTrigger)3 Store (co.cask.cdap.app.store.Store)2