Search in sources :

Example 1 with RunConstraints

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));
}
Also used : ConcurrencyConstraint(co.cask.cdap.internal.app.runtime.schedule.constraint.ConcurrencyConstraint) ProtoConstraint(co.cask.cdap.proto.ProtoConstraint) TimeTrigger(co.cask.cdap.internal.app.runtime.schedule.trigger.TimeTrigger) StreamSizeTrigger(co.cask.cdap.internal.app.runtime.schedule.trigger.StreamSizeTrigger) HttpResponse(org.apache.http.HttpResponse) ScheduleDetail(co.cask.cdap.proto.ScheduleDetail) NamespaceId(co.cask.cdap.proto.id.NamespaceId) RunConstraints(co.cask.cdap.api.schedule.RunConstraints) ScheduleUpdateDetail(co.cask.cdap.proto.ScheduleUpdateDetail)

Example 2 with RunConstraints

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());
}
Also used : Constraint(co.cask.cdap.internal.schedule.constraint.Constraint) Schedule(co.cask.cdap.api.schedule.Schedule) StreamSizeSchedule(co.cask.cdap.internal.schedule.StreamSizeSchedule) TimeSchedule(co.cask.cdap.internal.schedule.TimeSchedule) TimeSchedule(co.cask.cdap.internal.schedule.TimeSchedule) StreamSizeSchedule(co.cask.cdap.internal.schedule.StreamSizeSchedule) RunConstraints(co.cask.cdap.api.schedule.RunConstraints) ScheduleSpecification(co.cask.cdap.api.schedule.ScheduleSpecification) Nullable(javax.annotation.Nullable)

Aggregations

RunConstraints (co.cask.cdap.api.schedule.RunConstraints)2 Schedule (co.cask.cdap.api.schedule.Schedule)1 ScheduleSpecification (co.cask.cdap.api.schedule.ScheduleSpecification)1 ConcurrencyConstraint (co.cask.cdap.internal.app.runtime.schedule.constraint.ConcurrencyConstraint)1 StreamSizeTrigger (co.cask.cdap.internal.app.runtime.schedule.trigger.StreamSizeTrigger)1 TimeTrigger (co.cask.cdap.internal.app.runtime.schedule.trigger.TimeTrigger)1 StreamSizeSchedule (co.cask.cdap.internal.schedule.StreamSizeSchedule)1 TimeSchedule (co.cask.cdap.internal.schedule.TimeSchedule)1 Constraint (co.cask.cdap.internal.schedule.constraint.Constraint)1 ProtoConstraint (co.cask.cdap.proto.ProtoConstraint)1 ScheduleDetail (co.cask.cdap.proto.ScheduleDetail)1 ScheduleUpdateDetail (co.cask.cdap.proto.ScheduleUpdateDetail)1 NamespaceId (co.cask.cdap.proto.id.NamespaceId)1 Nullable (javax.annotation.Nullable)1 HttpResponse (org.apache.http.HttpResponse)1