use of co.cask.cdap.api.schedule.RunConstraints in project cdap by caskdata.
the class ProgramLifecycleHttpHandlerTest method testUpdateSchedule.
private void testUpdateSchedule(ApplicationId appV2Id) throws Exception {
// intentionally keep two ScheduleUpdateDetail's to test backward compatibility
ScheduleUpdateDetail scheduleUpdateDetail = new ScheduleUpdateDetail("updatedDescription", new RunConstraints(5), "0 4 * * *", null, null, ImmutableMap.of("twoKey", "twoValue", "someKey", "newValue"));
ScheduleUpdateDetail invalidUpdateDetail = new ScheduleUpdateDetail("updatedDescription", null, null, "streamName", null, ImmutableMap.<String, String>of());
ScheduleDetail validScheduleDetail = new ScheduleDetail(AppWithSchedule.SCHEDULE, "updatedDescription", null, ImmutableMap.<String, String>of(), new StreamSizeTrigger(new NamespaceId(TEST_NAMESPACE1).stream(AppWithSchedule.STREAM), 10), ImmutableList.<Constraint>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, scheduleUpdateDetail);
Assert.assertEquals(HttpResponseStatus.NOT_FOUND.getCode(), response.getStatusLine().getStatusCode());
// trying to update a non-existing schedule should fail
response = updateSchedule(TEST_NAMESPACE1, AppWithSchedule.NAME, null, "NonExistingSchedule", scheduleUpdateDetail);
Assert.assertEquals(HttpResponseStatus.NOT_FOUND.getCode(), response.getStatusLine().getStatusCode());
// trying to update a time schedule with stream schedule detail containing null dataTriggerMB should fail
response = updateSchedule(TEST_NAMESPACE1, AppWithSchedule.NAME, null, AppWithSchedule.SCHEDULE, invalidUpdateDetail);
Assert.assertEquals(HttpResponseStatus.BAD_REQUEST.getCode(), response.getStatusLine().getStatusCode());
// trying to update a time schedule with stream schedule detail containing both
// stream name and dataTriggerMB should succeed
response = updateSchedule(TEST_NAMESPACE1, AppWithSchedule.NAME, null, AppWithSchedule.SCHEDULE, validScheduleDetail);
Assert.assertEquals(HttpResponseStatus.OK.getCode(), response.getStatusLine().getStatusCode());
// should be able to update an existing stream size schedule with a valid new time schedule
response = updateSchedule(TEST_NAMESPACE1, AppWithSchedule.NAME, null, AppWithSchedule.SCHEDULE, scheduleUpdateDetail);
Assert.assertEquals(HttpResponseStatus.OK.getCode(), 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 TimeTrigger("0 4 * * *"), null, null);
response = updateSchedule(TEST_NAMESPACE1, AppWithSchedule.NAME, null, AppWithSchedule.SCHEDULE, scheduleDetail);
Assert.assertEquals(HttpResponseStatus.OK.getCode(), 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.api.schedule.RunConstraints in project cdap by caskdata.
the class ScheduleDetail method toScheduleSpec.
/**
* Return an equivalent schedule specification, or null if there is no equivalent one.
*/
@Deprecated
@Nullable
public ScheduleSpecification toScheduleSpec() {
RunConstraints constraints = RunConstraints.NONE;
if (getConstraints() != null) {
for (Constraint runConstraint : getConstraints()) {
if (runConstraint instanceof ProtoConstraint.ConcurrencyConstraint) {
constraints = new RunConstraints(((ProtoConstraint.ConcurrencyConstraint) runConstraint).getMaxConcurrency());
break;
}
}
}
Schedule schedule;
if (getTrigger() instanceof ProtoTrigger.TimeTrigger) {
ProtoTrigger.TimeTrigger trigger = ((ProtoTrigger.TimeTrigger) getTrigger());
schedule = new TimeSchedule(getName(), getDescription(), trigger.getCronExpression(), constraints);
} else if (getTrigger() instanceof ProtoTrigger.StreamSizeTrigger) {
ProtoTrigger.StreamSizeTrigger trigger = (ProtoTrigger.StreamSizeTrigger) getTrigger();
schedule = new StreamSizeSchedule(getName(), getDescription(), trigger.getStreamId().getStream(), trigger.getTriggerMB(), constraints);
} else {
return null;
}
return new ScheduleSpecification(schedule, getProgram(), getProperties());
}
Aggregations