Search in sources :

Example 6 with ScheduledRuntime

use of io.cdap.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);
    long startTime = System.currentTimeMillis();
    scheduleClient.suspend(schedule);
    status = scheduleClient.getStatus(schedule);
    Assert.assertEquals("SUSPENDED", status);
    long endTime = System.currentTimeMillis() + 1;
    scheduleClient.reEnableSuspendedSchedules(NamespaceId.DEFAULT, startTime, endTime);
    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(io.cdap.cdap.proto.ProtoTrigger) NotFoundException(io.cdap.cdap.common.NotFoundException) ScheduleDetail(io.cdap.cdap.proto.ScheduleDetail) ScheduledRuntime(io.cdap.cdap.proto.ScheduledRuntime) Test(org.junit.Test)

Example 7 with ScheduledRuntime

use of io.cdap.cdap.proto.ScheduledRuntime in project cdap by caskdata.

the class AppFabricTestBase method getScheduledRunTimes.

/**
 * Returns a list of {@link ScheduledRuntime}.
 *
 * @param programId the program id
 * @param next if true, fetch the list of future run times. If false, fetch the list of past run times.
 */
protected List<ScheduledRuntime> getScheduledRunTimes(ProgramId programId, boolean next) throws Exception {
    String nextRunTimeUrl = String.format("apps/%s/workflows/%s/%sruntime", programId.getApplication(), programId.getProgram(), next ? "next" : "previous");
    String versionedUrl = getVersionedAPIPath(nextRunTimeUrl, Constants.Gateway.API_VERSION_3_TOKEN, programId.getNamespace());
    HttpResponse response = doGet(versionedUrl);
    assertResponseCode(200, response);
    return readResponse(response, new TypeToken<List<ScheduledRuntime>>() {
    }.getType());
}
Also used : TypeToken(com.google.gson.reflect.TypeToken) HttpResponse(io.cdap.common.http.HttpResponse) ScheduledRuntime(io.cdap.cdap.proto.ScheduledRuntime)

Example 8 with ScheduledRuntime

use of io.cdap.cdap.proto.ScheduledRuntime in project cdap by cdapio.

the class TimeScheduler method getScheduledRuntime.

private List<ScheduledRuntime> getScheduledRuntime(ProgramId program, boolean previousRuntimeRequested) throws SchedulerException {
    List<ScheduledRuntime> scheduledRuntimes = new ArrayList<>();
    SchedulableProgramType schedulableType = program.getType().getSchedulableType();
    if (schedulableType == null) {
        throw new IllegalArgumentException("Program " + program + " cannot be scheduled");
    }
    try {
        for (Trigger trigger : scheduler.getTriggersOfJob(jobKeyFor(program, schedulableType))) {
            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 {
                // skip the trigger that is not enabled, since it cannot launch program as scheduled
                if (scheduler.getTriggerState(trigger.getKey()) == Trigger.TriggerState.PAUSED) {
                    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(io.cdap.cdap.internal.app.runtime.schedule.trigger.SatisfiableTrigger) Trigger(org.quartz.Trigger) ProtoTrigger(io.cdap.cdap.proto.ProtoTrigger) AbstractSatisfiableCompositeTrigger(io.cdap.cdap.internal.app.runtime.schedule.trigger.AbstractSatisfiableCompositeTrigger) TimeTrigger(io.cdap.cdap.internal.app.runtime.schedule.trigger.TimeTrigger) ArrayList(java.util.ArrayList) SchedulableProgramType(io.cdap.cdap.api.schedule.SchedulableProgramType) ScheduledRuntime(io.cdap.cdap.proto.ScheduledRuntime)

Example 9 with ScheduledRuntime

use of io.cdap.cdap.proto.ScheduledRuntime in project cdap by cdapio.

the class TimeScheduler method getAllScheduledRunTimes.

/**
 * Get all the scheduled run time of the program within the given time range in the future.
 * A program may contain multiple schedules. This method returns the scheduled runtimes for all the schedules
 * within the given time range. This method only takes
 *   + into account schedules based on time. For schedules based on data, an empty list will
 *   + be returned.
 *
 * @param program program to fetch the next runtime.
 * @param programType type of program.
 * @param startTimeSecs the start of the time range in seconds (inclusive, i.e. scheduled time larger or
 *                      equal to the start will be returned)
 * @param endTimeSecs the end of the time range in seconds (exclusive, i.e. scheduled time smaller than the end
 *                    will be returned)
 * @return list of scheduled runtimes for the program. Empty list if there are no schedules
 *         or if the program is not found
 * @throws SchedulerException on unforeseen error.
 */
public List<ScheduledRuntime> getAllScheduledRunTimes(ProgramId program, SchedulableProgramType programType, long startTimeSecs, long endTimeSecs) throws SchedulerException {
    // decrease the start time by one second to include the next fire time that is equal to the start time
    Date startTime = new Date(TimeUnit.SECONDS.toMillis(startTimeSecs - 1));
    long endTimeMillis = TimeUnit.SECONDS.toMillis(endTimeSecs);
    List<ScheduledRuntime> scheduledRuntimes = new ArrayList<>();
    try {
        for (Trigger trigger : scheduler.getTriggersOfJob(jobKeyFor(program, programType))) {
            // skip the trigger that is not enabled, since it cannot launch program as scheduled
            if (Trigger.TriggerState.PAUSED.equals(scheduler.getTriggerState(trigger.getKey()))) {
                continue;
            }
            Date nextFireTime = trigger.getFireTimeAfter(startTime);
            String triggerKeyString = trigger.getKey().toString();
            while (nextFireTime != null && nextFireTime.getTime() < endTimeMillis) {
                ScheduledRuntime runtime = new ScheduledRuntime(triggerKeyString, nextFireTime.getTime());
                scheduledRuntimes.add(runtime);
                nextFireTime = trigger.getFireTimeAfter(nextFireTime);
            }
        }
    } catch (org.quartz.SchedulerException e) {
        throw new SchedulerException(e);
    }
    return scheduledRuntimes;
}
Also used : SatisfiableTrigger(io.cdap.cdap.internal.app.runtime.schedule.trigger.SatisfiableTrigger) Trigger(org.quartz.Trigger) ProtoTrigger(io.cdap.cdap.proto.ProtoTrigger) AbstractSatisfiableCompositeTrigger(io.cdap.cdap.internal.app.runtime.schedule.trigger.AbstractSatisfiableCompositeTrigger) TimeTrigger(io.cdap.cdap.internal.app.runtime.schedule.trigger.TimeTrigger) ArrayList(java.util.ArrayList) ScheduledRuntime(io.cdap.cdap.proto.ScheduledRuntime) Date(java.util.Date)

Example 10 with ScheduledRuntime

use of io.cdap.cdap.proto.ScheduledRuntime in project cdap by caskdata.

the class OperationsDashboardHttpHandler method getScheduledDashboardRecords.

/**
 * For a given time schedule, gets all the scheduled program run in the given time range
 *
 * @param schedule      the schedule to get scheduled program run with
 * @param startTimeSecs the start of the time range in seconds (inclusive, i.e. scheduled time larger or equal to the
 *                      start will be returned)
 * @param endTimeSecs   the end of the time range in seconds (exclusive, i.e. scheduled time smaller than the end will
 *                      be returned)
 * @return a list of dashboard program run records with scheduled time as start time
 * @throws Exception
 */
private List<DashboardProgramRunRecord> getScheduledDashboardRecords(ProgramSchedule schedule, long startTimeSecs, long endTimeSecs) throws Exception {
    ProgramId programId = schedule.getProgramId();
    // get all the scheduled run times within the given time range of the given program
    List<ScheduledRuntime> scheduledRuntimes = timeSchedulerService.getAllScheduledRunTimes(programId, programId.getType().getSchedulableType(), startTimeSecs, endTimeSecs);
    String userId = schedule.getProperties().get(ProgramOptionConstants.USER_ID);
    String artifactId = schedule.getProperties().get(ProgramOptionConstants.ARTIFACT_ID);
    ArtifactSummary artifactSummary = artifactId == null ? null : ArtifactSummary.from(GSON.fromJson(artifactId, ArtifactId.class));
    // for each scheduled runtime, construct a dashboard record for it with the scheduled time as start time
    return scheduledRuntimes.stream().map(scheduledRuntime -> new DashboardProgramRunRecord(programId.getNamespace(), artifactSummary, new DashboardProgramRunRecord.ApplicationNameVersion(programId.getApplication(), programId.getVersion()), programId.getType().name(), programId.getProgram(), null, userId, SCHEDULED, // convert the scheduled time from millis to seconds as start time
    TimeUnit.MILLISECONDS.toSeconds(scheduledRuntime.getTime()), null, null, null, null, null)).collect(Collectors.toList());
}
Also used : TriggerInfo(io.cdap.cdap.api.schedule.TriggerInfo) RunRecordDetail(io.cdap.cdap.internal.app.store.RunRecordDetail) DashboardProgramRunRecord(io.cdap.cdap.proto.ops.DashboardProgramRunRecord) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) GET(javax.ws.rs.GET) Inject(com.google.inject.Inject) LoggerFactory(org.slf4j.LoggerFactory) Path(javax.ws.rs.Path) ProgramHeartbeatService(io.cdap.cdap.reporting.ProgramHeartbeatService) GsonBuilder(com.google.gson.GsonBuilder) ScheduledRuntime(io.cdap.cdap.proto.ScheduledRuntime) ArrayList(java.util.ArrayList) ProgramRunId(io.cdap.cdap.proto.id.ProgramRunId) Trigger(io.cdap.cdap.api.schedule.Trigger) QueryParam(javax.ws.rs.QueryParam) Gson(com.google.gson.Gson) ArtifactSummary(io.cdap.cdap.api.artifact.ArtifactSummary) TimeSchedulerService(io.cdap.cdap.internal.app.runtime.schedule.TimeSchedulerService) KerberosName(org.apache.hadoop.security.authentication.util.KerberosName) TriggeringScheduleInfoAdapter(io.cdap.cdap.internal.app.runtime.schedule.TriggeringScheduleInfoAdapter) TriggeringScheduleInfo(io.cdap.cdap.api.schedule.TriggeringScheduleInfo) AbstractAppFabricHttpHandler(io.cdap.cdap.gateway.handlers.util.AbstractAppFabricHttpHandler) Logger(org.slf4j.Logger) HttpResponder(io.cdap.http.HttpResponder) Scheduler(io.cdap.cdap.scheduler.Scheduler) Collection(java.util.Collection) ProgramId(io.cdap.cdap.proto.id.ProgramId) Set(java.util.Set) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) IOException(java.io.IOException) BadRequestException(io.cdap.cdap.common.BadRequestException) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) Collectors(java.util.stream.Collectors) ProgramSchedule(io.cdap.cdap.internal.app.runtime.schedule.ProgramSchedule) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Constants(io.cdap.cdap.common.conf.Constants) VisibleForTesting(com.google.common.annotations.VisibleForTesting) ProgramOptionConstants(io.cdap.cdap.internal.app.runtime.ProgramOptionConstants) Comparator(java.util.Comparator) ArtifactId(io.cdap.cdap.api.artifact.ArtifactId) Singleton(com.google.inject.Singleton) ArtifactSummary(io.cdap.cdap.api.artifact.ArtifactSummary) DashboardProgramRunRecord(io.cdap.cdap.proto.ops.DashboardProgramRunRecord) ProgramId(io.cdap.cdap.proto.id.ProgramId) ScheduledRuntime(io.cdap.cdap.proto.ScheduledRuntime)

Aggregations

ScheduledRuntime (io.cdap.cdap.proto.ScheduledRuntime)18 TimeTrigger (io.cdap.cdap.internal.app.runtime.schedule.trigger.TimeTrigger)10 HttpResponse (io.cdap.common.http.HttpResponse)8 ArrayList (java.util.ArrayList)8 Test (org.junit.Test)8 ArtifactSummary (io.cdap.cdap.api.artifact.ArtifactSummary)6 ScheduleDetail (io.cdap.cdap.proto.ScheduleDetail)6 ProgramId (io.cdap.cdap.proto.id.ProgramId)6 ProtoTrigger (io.cdap.cdap.proto.ProtoTrigger)5 ScheduleProgramInfo (io.cdap.cdap.api.workflow.ScheduleProgramInfo)4 NotFoundException (io.cdap.cdap.common.NotFoundException)4 DelayConstraint (io.cdap.cdap.internal.app.runtime.schedule.constraint.DelayConstraint)4 AbstractSatisfiableCompositeTrigger (io.cdap.cdap.internal.app.runtime.schedule.trigger.AbstractSatisfiableCompositeTrigger)4 SatisfiableTrigger (io.cdap.cdap.internal.app.runtime.schedule.trigger.SatisfiableTrigger)4 Constraint (io.cdap.cdap.internal.schedule.constraint.Constraint)4 ApplicationId (io.cdap.cdap.proto.id.ApplicationId)4 List (java.util.List)4 Trigger (org.quartz.Trigger)4 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 TypeToken (com.google.common.reflect.TypeToken)2