use of co.cask.cdap.internal.app.runtime.schedule.constraint.ConcurrencyConstraint in project cdap by caskdata.
the class ProgramLifecycleHttpHandlerTest method testUpdateSchedule.
private void testUpdateSchedule(ApplicationId appV2Id) throws Exception {
ScheduleDetail updateDetail = new ScheduleDetail(AppWithSchedule.SCHEDULE, "updatedDescription", null, ImmutableMap.of("twoKey", "twoValue", "someKey", "newValue"), new TimeTrigger("0 4 * * *"), ImmutableList.of(new ConcurrencyConstraint(5)), null);
// trying to update schedule for a non-existing app should fail
HttpResponse response = updateSchedule(TEST_NAMESPACE1, "nonExistingApp", null, AppWithSchedule.SCHEDULE, updateDetail);
Assert.assertEquals(HttpResponseStatus.NOT_FOUND.code(), response.getStatusLine().getStatusCode());
// trying to update a non-existing schedule should fail
ScheduleDetail nonExistingSchedule = new ScheduleDetail("NonExistingSchedule", "updatedDescription", null, ImmutableMap.of("twoKey", "twoValue"), new TimeTrigger("0 4 * * *"), ImmutableList.of(new ConcurrencyConstraint(5)), null);
response = updateSchedule(TEST_NAMESPACE1, AppWithSchedule.NAME, null, "NonExistingSchedule", nonExistingSchedule);
Assert.assertEquals(HttpResponseStatus.NOT_FOUND.code(), response.getStatusLine().getStatusCode());
// should be able to update an existing schedule with a valid new time schedule
response = updateSchedule(TEST_NAMESPACE1, AppWithSchedule.NAME, null, AppWithSchedule.SCHEDULE, updateDetail);
Assert.assertEquals(HttpResponseStatus.OK.code(), response.getStatusLine().getStatusCode());
// verify that the schedule information for updated
ScheduleDetail schedule = getSchedule(TEST_NAMESPACE1, AppWithSchedule.NAME, null, AppWithSchedule.SCHEDULE);
Assert.assertEquals("updatedDescription", schedule.getDescription());
Assert.assertEquals("0 4 * * *", ((TimeTrigger) schedule.getTrigger()).getCronExpression());
Assert.assertEquals(new ProtoConstraint.ConcurrencyConstraint(5), schedule.getConstraints().get(0));
// the properties should have been replaced
Assert.assertEquals(2, schedule.getProperties().size());
Assert.assertEquals("newValue", schedule.getProperties().get("someKey"));
Assert.assertEquals("twoValue", schedule.getProperties().get("twoKey"));
// the old property should not exist
Assert.assertNull(schedule.getProperties().get("oneKey"));
// the above update should not have affected the schedule for the other version of the app
schedule = getSchedule(TEST_NAMESPACE1, AppWithSchedule.NAME, appV2Id.getVersion(), AppWithSchedule.SCHEDULE);
Assert.assertNotEquals("updatedDescription", schedule.getDescription());
Assert.assertEquals("0/15 * * * * ?", ((TimeTrigger) schedule.getTrigger()).getCronExpression());
// try to update the schedule again but this time with property as null. It should retain the old properties
ScheduleDetail scheduleDetail = new ScheduleDetail(AppWithSchedule.SCHEDULE, "updatedDescription", null, null, new ProtoTrigger.TimeTrigger("0 4 * * *"), null, null);
response = updateSchedule(TEST_NAMESPACE1, AppWithSchedule.NAME, null, AppWithSchedule.SCHEDULE, scheduleDetail);
Assert.assertEquals(HttpResponseStatus.OK.code(), response.getStatusLine().getStatusCode());
schedule = getSchedule(TEST_NAMESPACE1, AppWithSchedule.NAME, null, AppWithSchedule.SCHEDULE);
Assert.assertEquals(2, schedule.getProperties().size());
Assert.assertEquals("newValue", schedule.getProperties().get("someKey"));
Assert.assertEquals("twoValue", schedule.getProperties().get("twoKey"));
Assert.assertEquals(new ProtoConstraint.ConcurrencyConstraint(5), schedule.getConstraints().get(0));
}
use of co.cask.cdap.internal.app.runtime.schedule.constraint.ConcurrencyConstraint in project cdap by caskdata.
the class Schedulers method toScheduleCreationSpec.
public static ScheduleCreationSpec toScheduleCreationSpec(NamespaceId deployNamespace, Schedule schedule, String programName, Map<String, String> properties) {
Trigger trigger;
if (schedule instanceof TimeSchedule) {
trigger = new TimeTrigger(((TimeSchedule) schedule).getCronEntry());
} else {
StreamSizeSchedule streamSizeSchedule = ((StreamSizeSchedule) schedule);
trigger = new StreamSizeTrigger(deployNamespace.stream(streamSizeSchedule.getStreamName()), streamSizeSchedule.getDataTriggerMB());
}
Integer maxConcurrentRuns = schedule.getRunConstraints().getMaxConcurrentRuns();
List<Constraint> constraints = maxConcurrentRuns == null ? ImmutableList.<Constraint>of() : ImmutableList.<Constraint>of(new ConcurrencyConstraint(maxConcurrentRuns));
return new ScheduleCreationSpec(schedule.getName(), schedule.getDescription(), programName, properties, trigger, constraints, Schedulers.JOB_QUEUE_TIMEOUT_MILLIS);
}
use of co.cask.cdap.internal.app.runtime.schedule.constraint.ConcurrencyConstraint in project cdap by caskdata.
the class Schedulers method toProgramSchedule.
public static ProgramSchedule toProgramSchedule(ApplicationId appId, ScheduleSpecification spec) {
Schedule schedule = spec.getSchedule();
ProgramType programType = ProgramType.valueOfSchedulableType(spec.getProgram().getProgramType());
ProgramId programId = appId.program(programType, spec.getProgram().getProgramName());
Trigger trigger;
if (schedule instanceof TimeSchedule) {
TimeSchedule timeSchedule = (TimeSchedule) schedule;
trigger = new TimeTrigger(timeSchedule.getCronEntry());
} else {
StreamSizeSchedule streamSchedule = (StreamSizeSchedule) schedule;
StreamId streamId = programId.getNamespaceId().stream(streamSchedule.getStreamName());
trigger = new StreamSizeTrigger(streamId, streamSchedule.getDataTriggerMB());
}
Integer maxConcurrentRuns = schedule.getRunConstraints().getMaxConcurrentRuns();
List<Constraint> constraints = maxConcurrentRuns == null ? ImmutableList.<Constraint>of() : ImmutableList.<Constraint>of(new ConcurrencyConstraint(maxConcurrentRuns));
return new ProgramSchedule(schedule.getName(), schedule.getDescription(), programId, spec.getProperties(), trigger, constraints);
}
Aggregations