Search in sources :

Example 1 with ScheduledRuntime

use of co.cask.cdap.proto.ScheduledRuntime in project cdap by caskdata.

the class ScheduleClient method nextRuntimes.

/**
 * Get the next scheduled run time of the program. A program may contain multiple schedules.
 * This method returns the next scheduled runtimes for all the schedules. This method only takes
 *   + into account schedules based on time. Schedules based on data are ignored.
 *
 * @param workflow Id of the Workflow for which to fetch next run times.
 * @return list of Scheduled runtimes for the Workflow. Empty list if there are no schedules.
 */
public List<ScheduledRuntime> nextRuntimes(WorkflowId workflow) throws IOException, UnauthenticatedException, NotFoundException, UnauthorizedException {
    String path = String.format("apps/%s/workflows/%s/nextruntime", workflow.getApplication(), workflow.getProgram());
    URL url = config.resolveNamespacedURLV3(workflow.getNamespaceId(), path);
    HttpResponse response = restClient.execute(HttpMethod.GET, url, config.getAccessToken(), HttpURLConnection.HTTP_NOT_FOUND);
    if (HttpURLConnection.HTTP_NOT_FOUND == response.getResponseCode()) {
        throw new NotFoundException(workflow);
    }
    ObjectResponse<List<ScheduledRuntime>> objectResponse = ObjectResponse.fromJsonBody(response, new TypeToken<List<ScheduledRuntime>>() {
    }.getType(), GSON);
    return objectResponse.getResponseObject();
}
Also used : TypeToken(com.google.common.reflect.TypeToken) HttpResponse(co.cask.common.http.HttpResponse) NotFoundException(co.cask.cdap.common.NotFoundException) List(java.util.List) ScheduledRuntime(co.cask.cdap.proto.ScheduledRuntime) URL(java.net.URL)

Example 2 with ScheduledRuntime

use of co.cask.cdap.proto.ScheduledRuntime in project cdap by caskdata.

the class TimeScheduler method getScheduledRuntime.

private List<ScheduledRuntime> getScheduledRuntime(ProgramId program, SchedulableProgramType programType, boolean previousRuntimeRequested) throws SchedulerException {
    List<ScheduledRuntime> scheduledRuntimes = new ArrayList<>();
    try {
        for (Trigger trigger : scheduler.getTriggersOfJob(jobKeyFor(program, programType))) {
            long time;
            if (previousRuntimeRequested) {
                if (trigger.getPreviousFireTime() == null) {
                    // previous fire time can be null for the triggers which are not yet fired
                    continue;
                }
                time = trigger.getPreviousFireTime().getTime();
            } else {
                if (scheduler.getTriggerState(trigger.getKey()) == Trigger.TriggerState.PAUSED) {
                    // if the trigger is paused, then skip getting the next fire time
                    continue;
                }
                time = trigger.getNextFireTime().getTime();
            }
            ScheduledRuntime runtime = new ScheduledRuntime(trigger.getKey().toString(), time);
            scheduledRuntimes.add(runtime);
        }
    } catch (org.quartz.SchedulerException e) {
        throw new SchedulerException(e);
    }
    return scheduledRuntimes;
}
Also used : SatisfiableTrigger(co.cask.cdap.internal.app.runtime.schedule.trigger.SatisfiableTrigger) ProtoTrigger(co.cask.cdap.proto.ProtoTrigger) Trigger(org.quartz.Trigger) AbstractSatisfiableCompositeTrigger(co.cask.cdap.internal.app.runtime.schedule.trigger.AbstractSatisfiableCompositeTrigger) TimeTrigger(co.cask.cdap.internal.app.runtime.schedule.trigger.TimeTrigger) ArrayList(java.util.ArrayList) ScheduledRuntime(co.cask.cdap.proto.ScheduledRuntime)

Example 3 with ScheduledRuntime

use of co.cask.cdap.proto.ScheduledRuntime in project cdap by caskdata.

the class ScheduleClientTestRun method testAll.

@Test
public void testAll() throws Exception {
    List<ScheduleDetail> list = scheduleClient.listSchedules(workflow);
    Assert.assertEquals(1, list.size());
    ScheduleDetail timeSchedule = list.get(0);
    ProtoTrigger.TimeTrigger timeTrigger = (ProtoTrigger.TimeTrigger) timeSchedule.getTrigger();
    Assert.assertEquals(FakeApp.TIME_SCHEDULE_NAME, timeSchedule.getName());
    Assert.assertEquals(FakeApp.SCHEDULE_CRON, timeTrigger.getCronExpression());
    String status = scheduleClient.getStatus(schedule);
    Assert.assertEquals("SUSPENDED", status);
    status = scheduleClient.getStatus(schedule);
    Assert.assertEquals("SUSPENDED", status);
    scheduleClient.resume(schedule);
    status = scheduleClient.getStatus(schedule);
    Assert.assertEquals("SCHEDULED", status);
    scheduleClient.suspend(schedule);
    status = scheduleClient.getStatus(schedule);
    Assert.assertEquals("SUSPENDED", status);
    scheduleClient.resume(schedule);
    status = scheduleClient.getStatus(schedule);
    Assert.assertEquals("SCHEDULED", status);
    scheduleClient.suspend(schedule);
    status = scheduleClient.getStatus(schedule);
    Assert.assertEquals("SUSPENDED", status);
    scheduleClient.resume(schedule);
    List<ScheduledRuntime> scheduledRuntimes = scheduleClient.nextRuntimes(workflow);
    scheduleClient.suspend(schedule);
    Assert.assertEquals(1, scheduledRuntimes.size());
    // simply assert that its scheduled for some time in the future (or scheduled for now, but hasn't quite
    // executed yet
    Assert.assertTrue(scheduledRuntimes.get(0).getTime() >= System.currentTimeMillis() - TimeUnit.MINUTES.toMillis(1));
    try {
        scheduleClient.nextRuntimes(app.workflow("nonexistentWorkflow"));
        Assert.fail("Expected not to be able to retrieve next run times for a nonexistent workflow.");
    } catch (NotFoundException expected) {
    // expected
    }
}
Also used : ProtoTrigger(co.cask.cdap.proto.ProtoTrigger) NotFoundException(co.cask.cdap.common.NotFoundException) ScheduleDetail(co.cask.cdap.proto.ScheduleDetail) ScheduledRuntime(co.cask.cdap.proto.ScheduledRuntime) Test(org.junit.Test)

Example 4 with ScheduledRuntime

use of co.cask.cdap.proto.ScheduledRuntime in project cdap by caskdata.

the class WorkflowHttpHandlerTest method getScheduledRunTime.

/**
 * Returns a list of {@link ScheduledRuntime}.
 *
 * @param program the program id
 * @param next if true, fetch the list of future run times. If false, fetch the list of past run times.
 */
private List<ScheduledRuntime> getScheduledRunTime(Id.Program program, boolean next) throws Exception {
    String nextRunTimeUrl = String.format("apps/%s/workflows/%s/%sruntime", program.getApplicationId(), program.getId(), next ? "next" : "previous");
    String versionedUrl = getVersionedAPIPath(nextRunTimeUrl, Constants.Gateway.API_VERSION_3_TOKEN, program.getNamespaceId());
    HttpResponse response = doGet(versionedUrl);
    return readResponse(response, new TypeToken<List<ScheduledRuntime>>() {
    }.getType());
}
Also used : TypeToken(com.google.gson.reflect.TypeToken) HttpResponse(org.apache.http.HttpResponse) ScheduledRuntime(co.cask.cdap.proto.ScheduledRuntime)

Example 5 with ScheduledRuntime

use of co.cask.cdap.proto.ScheduledRuntime 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());
    // 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();
    // sampleSchedule is initially  suspended, so listing schedules with SCHEDULED status will get 0 schedule
    schedules = getSchedules(TEST_NAMESPACE2, appName, ApplicationId.DEFAULT_VERSION, sampleSchedule, ProgramScheduleStatus.SCHEDULED);
    Assert.assertEquals(0, schedules.size());
    // Resume the schedule
    Assert.assertEquals(200, resumeSchedule(TEST_NAMESPACE2, appName, sampleSchedule));
    // Check schedule status
    assertSchedule(programId, scheduleName, true, 30, TimeUnit.SECONDS);
    // sampleSchedule is now resumed in SCHEDULED, so listing schedules with SCHEDULED status
    // should return sampleSchedule
    schedules = getSchedules(TEST_NAMESPACE2, appName, ApplicationId.DEFAULT_VERSION, sampleSchedule, ProgramScheduleStatus.SCHEDULED);
    Assert.assertEquals(1, schedules.size());
    Assert.assertEquals(TEST_NAMESPACE2, schedules.get(0).getNamespace());
    Assert.assertEquals(appName, schedules.get(0).getApplication());
    Assert.assertEquals(ApplicationId.DEFAULT_VERSION, schedules.get(0).getApplicationVersion());
    Assert.assertEquals(sampleSchedule, schedules.get(0).getName());
    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, ProgramRunStatus.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, ProgramRunStatus.RUNNING);
    // get number of completed runs after schedule is suspended
    int workflowRuns = getProgramRuns(programId, ProgramRunStatus.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, ProgramRunStatus.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, ProgramRunStatus.RUNNING);
    deleteApp(Id.Application.from(TEST_NAMESPACE2, AppWithSchedule.class.getSimpleName()), 200);
}
Also used : HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) WorkflowId(co.cask.cdap.proto.id.WorkflowId) ProgramId(co.cask.cdap.proto.id.ProgramId) Id(co.cask.cdap.common.id.Id) ApplicationId(co.cask.cdap.proto.id.ApplicationId) ScheduleDetail(co.cask.cdap.proto.ScheduleDetail) ScheduledRuntime(co.cask.cdap.proto.ScheduledRuntime) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

ScheduledRuntime (co.cask.cdap.proto.ScheduledRuntime)6 NotFoundException (co.cask.cdap.common.NotFoundException)2 ProtoTrigger (co.cask.cdap.proto.ProtoTrigger)2 ScheduleDetail (co.cask.cdap.proto.ScheduleDetail)2 ApplicationId (co.cask.cdap.proto.id.ApplicationId)2 WorkflowId (co.cask.cdap.proto.id.WorkflowId)2 HttpResponse (org.apache.http.HttpResponse)2 Test (org.junit.Test)2 ApplicationSpecification (co.cask.cdap.api.app.ApplicationSpecification)1 ApplicationNotFoundException (co.cask.cdap.common.ApplicationNotFoundException)1 ProgramNotFoundException (co.cask.cdap.common.ProgramNotFoundException)1 Id (co.cask.cdap.common.id.Id)1 AbstractSatisfiableCompositeTrigger (co.cask.cdap.internal.app.runtime.schedule.trigger.AbstractSatisfiableCompositeTrigger)1 SatisfiableTrigger (co.cask.cdap.internal.app.runtime.schedule.trigger.SatisfiableTrigger)1 TimeTrigger (co.cask.cdap.internal.app.runtime.schedule.trigger.TimeTrigger)1 ProgramId (co.cask.cdap.proto.id.ProgramId)1 HttpResponse (co.cask.common.http.HttpResponse)1 TypeToken (com.google.common.reflect.TypeToken)1 TypeToken (com.google.gson.reflect.TypeToken)1 IOException (java.io.IOException)1