Search in sources :

Example 16 with ScheduleSpecification

use of co.cask.cdap.api.schedule.ScheduleSpecification in project cdap by caskdata.

the class WorkflowHttpHandlerTest method testWorkflowSchedules.

@Ignore
@Test
public void testWorkflowSchedules() throws Exception {
    // Steps for the test:
    // 1. Deploy the app
    // 2. Verify the schedules
    // 3. Verify the history after waiting a while
    // 4. Suspend the schedule
    // 5. Verify there are no runs after the suspend by looking at the history
    // 6. Resume the schedule
    // 7. Verify there are runs after the resume by looking at the history
    String appName = AppWithSchedule.NAME;
    String workflowName = AppWithSchedule.WORKFLOW_NAME;
    String sampleSchedule = AppWithSchedule.SCHEDULE;
    // deploy app with schedule in namespace 2
    HttpResponse response = deploy(AppWithSchedule.class, Constants.Gateway.API_VERSION_3_TOKEN, TEST_NAMESPACE2);
    Assert.assertEquals(200, response.getStatusLine().getStatusCode());
    Id.Program programId = Id.Program.from(TEST_NAMESPACE2, appName, ProgramType.WORKFLOW, workflowName);
    Map<String, String> runtimeArguments = ImmutableMap.of("someKey", "someWorkflowValue", "workflowKey", "workflowValue");
    setAndTestRuntimeArgs(programId, runtimeArguments);
    // get schedules
    List<ScheduleDetail> schedules = getSchedules(TEST_NAMESPACE2, appName, workflowName);
    Assert.assertEquals(1, schedules.size());
    String scheduleName = schedules.get(0).getName();
    Assert.assertFalse(scheduleName.isEmpty());
    // get schedules in backward-compatible ScheduleSpecification form
    List<ScheduleSpecification> specs = getScheduleSpecs(TEST_NAMESPACE2, appName, workflowName);
    Assert.assertEquals(1, specs.size());
    String specName = specs.get(0).getSchedule().getName();
    Assert.assertEquals(scheduleName, specName);
    // TODO [CDAP-2327] Sagar Investigate why following check fails sometimes. Mostly test case issue.
    // List<ScheduledRuntime> previousRuntimes = getScheduledRunTime(programId, scheduleName, "previousruntime");
    // Assert.assertTrue(previousRuntimes.size() == 0);
    long current = System.currentTimeMillis();
    // Resume the schedule
    Assert.assertEquals(200, resumeSchedule(TEST_NAMESPACE2, appName, sampleSchedule));
    // Check schedule status
    assertSchedule(programId, scheduleName, true, 30, TimeUnit.SECONDS);
    List<ScheduledRuntime> runtimes = getScheduledRunTime(programId, true);
    String id = runtimes.get(0).getId();
    Assert.assertTrue(String.format("Expected schedule id '%s' to contain schedule name '%s'", id, scheduleName), id.contains(scheduleName));
    Long nextRunTime = runtimes.get(0).getTime();
    Assert.assertTrue(String.format("Expected nextRuntime '%s' to be greater than current runtime '%s'", nextRunTime, current), nextRunTime > current);
    // Verify that at least one program is completed
    verifyProgramRuns(programId, "completed");
    // Suspend the schedule
    Assert.assertEquals(200, suspendSchedule(TEST_NAMESPACE2, appName, scheduleName));
    // check paused state
    assertSchedule(programId, scheduleName, false, 30, TimeUnit.SECONDS);
    // check that there were at least 1 previous runs
    List<ScheduledRuntime> previousRuntimes = getScheduledRunTime(programId, false);
    int numRuns = previousRuntimes.size();
    Assert.assertTrue(String.format("After sleeping for two seconds, the schedule should have at least triggered " + "once, but found %s previous runs", numRuns), numRuns >= 1);
    // Verify no program running
    verifyNoRunWithStatus(programId, "running");
    // get number of completed runs after schedule is suspended
    int workflowRuns = getProgramRuns(programId, "completed").size();
    // verify that resuming the suspended schedule again has expected behavior (spawns new runs)
    Assert.assertEquals(200, resumeSchedule(TEST_NAMESPACE2, appName, scheduleName));
    //check scheduled state
    assertSchedule(programId, scheduleName, true, 30, TimeUnit.SECONDS);
    // Verify that the program ran after the schedule was resumed
    verifyProgramRuns(programId, "completed", workflowRuns);
    // Suspend the schedule
    Assert.assertEquals(200, suspendSchedule(TEST_NAMESPACE2, appName, scheduleName));
    //check paused state
    assertSchedule(programId, scheduleName, false, 30, TimeUnit.SECONDS);
    //Check status of a non existing schedule
    try {
        assertSchedule(programId, "invalid", true, 2, TimeUnit.SECONDS);
        Assert.fail();
    } catch (Exception e) {
    // expected
    }
    //Schedule operations using invalid namespace
    try {
        assertSchedule(Id.Program.from(TEST_NAMESPACE1, appName, ProgramType.WORKFLOW, workflowName), scheduleName, true, 2, TimeUnit.SECONDS);
        Assert.fail();
    } catch (Exception e) {
    // expected
    }
    Assert.assertEquals(404, suspendSchedule(TEST_NAMESPACE1, appName, scheduleName));
    Assert.assertEquals(404, resumeSchedule(TEST_NAMESPACE1, appName, scheduleName));
    verifyNoRunWithStatus(programId, "running");
    deleteApp(Id.Application.from(TEST_NAMESPACE2, AppWithSchedule.class.getSimpleName()), 200);
}
Also used : HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) Id(co.cask.cdap.proto.Id) ProgramId(co.cask.cdap.proto.id.ProgramId) ScheduleDetail(co.cask.cdap.proto.ScheduleDetail) ScheduledRuntime(co.cask.cdap.proto.ScheduledRuntime) ScheduleSpecification(co.cask.cdap.api.schedule.ScheduleSpecification) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 17 with ScheduleSpecification

use of co.cask.cdap.api.schedule.ScheduleSpecification 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)

Example 18 with ScheduleSpecification

use of co.cask.cdap.api.schedule.ScheduleSpecification in project cdap by caskdata.

the class ScheduleSpecificationCodec method deserialize.

@Override
public ScheduleSpecification deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
    JsonObject jsonObj = json.getAsJsonObject();
    JsonElement scheduleTypeJson = jsonObj.get("scheduleType");
    ScheduleType scheduleType;
    if (scheduleTypeJson == null) {
        // For backwards compatibility with spec persisted with older versions than 2.8, we need these lines
        scheduleType = null;
    } else {
        scheduleType = context.deserialize(jsonObj.get("scheduleType"), ScheduleType.class);
    }
    Schedule schedule = null;
    if (scheduleType == null) {
        JsonObject scheduleObj = jsonObj.get("schedule").getAsJsonObject();
        String name = context.deserialize(scheduleObj.get("name"), String.class);
        String description = context.deserialize(scheduleObj.get("description"), String.class);
        String cronEntry = context.deserialize(scheduleObj.get("cronEntry"), String.class);
        schedule = Schedules.builder(name).setDescription(description).createTimeSchedule(cronEntry);
    } else {
        switch(scheduleType) {
            case TIME:
                schedule = context.deserialize(jsonObj.get("schedule"), TimeSchedule.class);
                break;
            case STREAM:
                schedule = context.deserialize(jsonObj.get("schedule"), StreamSizeSchedule.class);
                break;
        }
    }
    ScheduleProgramInfo program = context.deserialize(jsonObj.get("program"), ScheduleProgramInfo.class);
    Map<String, String> properties = deserializeMap(jsonObj.get("properties"), context, String.class);
    return new ScheduleSpecification(schedule, program, properties);
}
Also used : ScheduleType(co.cask.cdap.proto.ScheduleType) JsonElement(com.google.gson.JsonElement) Schedule(co.cask.cdap.api.schedule.Schedule) StreamSizeSchedule(co.cask.cdap.internal.schedule.StreamSizeSchedule) TimeSchedule(co.cask.cdap.internal.schedule.TimeSchedule) JsonObject(com.google.gson.JsonObject) TimeSchedule(co.cask.cdap.internal.schedule.TimeSchedule) StreamSizeSchedule(co.cask.cdap.internal.schedule.StreamSizeSchedule) ScheduleProgramInfo(co.cask.cdap.api.workflow.ScheduleProgramInfo) ScheduleSpecification(co.cask.cdap.api.schedule.ScheduleSpecification)

Aggregations

ScheduleSpecification (co.cask.cdap.api.schedule.ScheduleSpecification)18 TimeSchedule (co.cask.cdap.internal.schedule.TimeSchedule)8 ScheduleDetail (co.cask.cdap.proto.ScheduleDetail)8 Test (org.junit.Test)7 ScheduleProgramInfo (co.cask.cdap.api.workflow.ScheduleProgramInfo)6 StreamSizeSchedule (co.cask.cdap.internal.schedule.StreamSizeSchedule)6 ApplicationId (co.cask.cdap.proto.id.ApplicationId)6 Schedule (co.cask.cdap.api.schedule.Schedule)5 ApplicationSpecification (co.cask.cdap.api.app.ApplicationSpecification)4 NotFoundException (co.cask.cdap.common.NotFoundException)4 ProgramSchedule (co.cask.cdap.internal.app.runtime.schedule.ProgramSchedule)4 ProgramId (co.cask.cdap.proto.id.ProgramId)4 NamespaceNotFoundException (co.cask.cdap.common.NamespaceNotFoundException)3 Id (co.cask.cdap.proto.Id)3 ArtifactSummary (co.cask.cdap.api.artifact.ArtifactSummary)2 TimeTrigger (co.cask.cdap.internal.app.runtime.schedule.trigger.TimeTrigger)2 ScheduleCreationSpec (co.cask.cdap.internal.schedule.ScheduleCreationSpec)2 ScheduledRuntime (co.cask.cdap.proto.ScheduledRuntime)2 AppRequest (co.cask.cdap.proto.artifact.AppRequest)2 NamespaceId (co.cask.cdap.proto.id.NamespaceId)2