Search in sources :

Example 1 with Constraint

use of io.cdap.cdap.internal.schedule.constraint.Constraint in project cdap by caskdata.

the class ScheduledRunTimeTest method testGetNextRun.

@Test
public void testGetNextRun() throws Exception {
    ApplicationId appId = NamespaceId.DEFAULT.app("test");
    deploy(appId, new AppRequest<>(new ArtifactSummary(ARTIFACT_ID.getName(), ARTIFACT_ID.getVersion().getVersion())));
    String scheduleName = "schedule1";
    // Add a schedule. Use a constraint to make it not going to be executed
    ProgramId programId = appId.workflow(WorkflowApp.FunWorkflow.NAME);
    Constraint constraint = new DelayConstraint(1, TimeUnit.HOURS);
    ScheduleProgramInfo scheduleProgramInfo = new ScheduleProgramInfo(programId.getType().getSchedulableType(), programId.getProgram());
    addSchedule(appId.getNamespace(), appId.getApplication(), appId.getVersion(), scheduleName, new ScheduleDetail(scheduleName, null, scheduleProgramInfo, null, new TimeTrigger("0 0 * * * "), Collections.singletonList(constraint), null));
    long now = System.currentTimeMillis();
    HttpResponse response = enableSchedule(programId.getNamespace(), programId.getApplication(), programId.getVersion(), scheduleName);
    Assert.assertEquals(200, response.getResponseCode());
    // Get the next run time
    List<ScheduledRuntime> scheduledRunTimes = getScheduledRunTimes(programId, true);
    Assert.assertEquals(1, scheduledRunTimes.size());
    long nextTime = scheduledRunTimes.get(0).getTime();
    Assert.assertTrue(nextTime >= now);
}
Also used : TimeTrigger(io.cdap.cdap.internal.app.runtime.schedule.trigger.TimeTrigger) Constraint(io.cdap.cdap.internal.schedule.constraint.Constraint) DelayConstraint(io.cdap.cdap.internal.app.runtime.schedule.constraint.DelayConstraint) HttpResponse(io.cdap.common.http.HttpResponse) ProgramId(io.cdap.cdap.proto.id.ProgramId) DelayConstraint(io.cdap.cdap.internal.app.runtime.schedule.constraint.DelayConstraint) ArtifactSummary(io.cdap.cdap.api.artifact.ArtifactSummary) ScheduleDetail(io.cdap.cdap.proto.ScheduleDetail) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) ScheduleProgramInfo(io.cdap.cdap.api.workflow.ScheduleProgramInfo) ScheduledRuntime(io.cdap.cdap.proto.ScheduledRuntime) Test(org.junit.Test)

Example 2 with Constraint

use of io.cdap.cdap.internal.schedule.constraint.Constraint in project cdap by caskdata.

the class ScheduledRunTimeTest method testBatchGetNextRun.

@Test
public void testBatchGetNextRun() throws Exception {
    // deploys 5 apps and create schedules for each of them
    long now = System.currentTimeMillis();
    List<ProgramId> programIds = new ArrayList<>();
    // Use a constraint to make it not going to be executed
    Constraint constraint = new DelayConstraint(1, TimeUnit.HOURS);
    for (int i = 0; i < 5; i++) {
        ApplicationId appId = NamespaceId.DEFAULT.app("test" + i);
        deploy(appId, new AppRequest<>(new ArtifactSummary(ARTIFACT_ID.getName(), ARTIFACT_ID.getVersion().getVersion())));
        String scheduleName = "schedule" + i;
        // Add a schedule
        ProgramId programId = appId.workflow(WorkflowApp.FunWorkflow.NAME);
        programIds.add(programId);
        ScheduleProgramInfo scheduleProgramInfo = new ScheduleProgramInfo(programId.getType().getSchedulableType(), programId.getProgram());
        addSchedule(appId.getNamespace(), appId.getApplication(), appId.getVersion(), scheduleName, new ScheduleDetail(scheduleName, null, scheduleProgramInfo, null, new TimeTrigger("0 0 * * * "), Collections.singletonList(constraint), null));
        HttpResponse response = enableSchedule(programId.getNamespace(), programId.getApplication(), programId.getVersion(), scheduleName);
        Assert.assertEquals(200, response.getResponseCode());
    }
    // Add programs that the app or the program doesn't exist
    programIds.add(NamespaceId.DEFAULT.app("not-exist").workflow("not-exist"));
    programIds.add(NamespaceId.DEFAULT.app("test1").workflow("not-exist"));
    List<BatchProgramSchedule> schedules = getScheduledRunTimes(NamespaceId.DEFAULT.getNamespace(), programIds, true);
    Assert.assertEquals(programIds.size(), schedules.size());
    // For the first 5 programs, they should have a next run
    for (int i = 0; i < 5; i++) {
        BatchProgramSchedule schedule = schedules.get(i);
        Assert.assertEquals(200, schedule.getStatusCode());
        List<ScheduledRuntime> nextRuns = schedule.getSchedules();
        Assert.assertNotNull(nextRuns);
        Assert.assertEquals(1, nextRuns.size());
        long nextTime = nextRuns.get(0).getTime();
        Assert.assertTrue(nextTime >= now);
    }
    // The last two should be a not found
    Assert.assertEquals(404, schedules.get(5).getStatusCode());
    Assert.assertEquals(404, schedules.get(6).getStatusCode());
}
Also used : BatchProgramSchedule(io.cdap.cdap.proto.BatchProgramSchedule) TimeTrigger(io.cdap.cdap.internal.app.runtime.schedule.trigger.TimeTrigger) Constraint(io.cdap.cdap.internal.schedule.constraint.Constraint) DelayConstraint(io.cdap.cdap.internal.app.runtime.schedule.constraint.DelayConstraint) ArrayList(java.util.ArrayList) HttpResponse(io.cdap.common.http.HttpResponse) ProgramId(io.cdap.cdap.proto.id.ProgramId) Constraint(io.cdap.cdap.internal.schedule.constraint.Constraint) DelayConstraint(io.cdap.cdap.internal.app.runtime.schedule.constraint.DelayConstraint) DelayConstraint(io.cdap.cdap.internal.app.runtime.schedule.constraint.DelayConstraint) ArtifactSummary(io.cdap.cdap.api.artifact.ArtifactSummary) ScheduleDetail(io.cdap.cdap.proto.ScheduleDetail) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) ScheduleProgramInfo(io.cdap.cdap.api.workflow.ScheduleProgramInfo) ScheduledRuntime(io.cdap.cdap.proto.ScheduledRuntime) Test(org.junit.Test)

Example 3 with Constraint

use of io.cdap.cdap.internal.schedule.constraint.Constraint in project cdap by caskdata.

the class UpdateTimeScheduleCommand method perform.

@Override
public void perform(Arguments arguments, PrintStream printStream) throws Exception {
    String scheduleName = arguments.get(ArgumentName.SCHEDULE_NAME.toString());
    String[] programIdParts = arguments.get(ArgumentName.PROGRAM.toString()).split("\\.");
    String version = arguments.getOptional(ArgumentName.APP_VERSION.toString());
    String scheduleDescription = arguments.getOptional(ArgumentName.DESCRIPTION.toString(), "");
    String cronExpression = arguments.get(ArgumentName.CRON_EXPRESSION.toString());
    String schedulePropertiesString = arguments.getOptional(ArgumentName.SCHEDULE_PROPERTIES.toString(), "");
    String scheduleRunConcurrencyString = arguments.getOptional(ArgumentName.CONCURRENCY.toString(), null);
    if (programIdParts.length < 2) {
        throw new CommandInputError(this);
    }
    String appId = programIdParts[0];
    NamespaceId namespaceId = cliConfig.getCurrentNamespace();
    ApplicationId applicationId = (version == null) ? namespaceId.app(appId) : namespaceId.app(appId, version);
    ScheduleId scheduleId = applicationId.schedule(scheduleName);
    String description = scheduleDescription == null ? null : scheduleDescription;
    ScheduleProgramInfo programInfo = new ScheduleProgramInfo(SchedulableProgramType.WORKFLOW, programIdParts[1]);
    List<Constraint> constraints = scheduleRunConcurrencyString == null ? ImmutableList.of() : ImmutableList.of(new ProtoConstraint.ConcurrencyConstraint(Integer.valueOf(scheduleRunConcurrencyString)));
    Map<String, String> propertiesMap = ArgumentParser.parseMap(schedulePropertiesString, ArgumentName.SCHEDULE_PROPERTIES.toString());
    ScheduleDetail scheduleDetail = new ScheduleDetail(scheduleName, description, programInfo, propertiesMap, new ProtoTrigger.TimeTrigger(cronExpression), constraints, null);
    scheduleClient.update(scheduleId, scheduleDetail);
    printStream.printf("Successfully updated schedule '%s' in app '%s'\n", scheduleName, appId);
}
Also used : CommandInputError(io.cdap.cdap.cli.exception.CommandInputError) ProtoConstraint(io.cdap.cdap.proto.ProtoConstraint) Constraint(io.cdap.cdap.internal.schedule.constraint.Constraint) ScheduleId(io.cdap.cdap.proto.id.ScheduleId) ProtoTrigger(io.cdap.cdap.proto.ProtoTrigger) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) ScheduleDetail(io.cdap.cdap.proto.ScheduleDetail) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) ScheduleProgramInfo(io.cdap.cdap.api.workflow.ScheduleProgramInfo)

Example 4 with Constraint

use of io.cdap.cdap.internal.schedule.constraint.Constraint in project cdap by caskdata.

the class AddTimeScheduleCommand method perform.

@Override
public void perform(Arguments arguments, PrintStream printStream) throws Exception {
    String scheduleName = arguments.get(ArgumentName.SCHEDULE_NAME.toString());
    String[] programIdParts = arguments.get(ArgumentName.PROGRAM.toString()).split("\\.");
    String version = arguments.getOptional(ArgumentName.APP_VERSION.toString());
    String scheduleDescription = arguments.getOptional(ArgumentName.DESCRIPTION.toString(), "");
    String cronExpression = arguments.get(ArgumentName.CRON_EXPRESSION.toString());
    String schedulePropertiesString = arguments.getOptional(ArgumentName.SCHEDULE_PROPERTIES.toString(), "");
    String scheduleRunConcurrencyString = arguments.getOptional(ArgumentName.CONCURRENCY.toString(), null);
    if (programIdParts.length < 2) {
        throw new CommandInputError(this);
    }
    String appId = programIdParts[0];
    NamespaceId namespaceId = cliConfig.getCurrentNamespace();
    ApplicationId applicationId = (version == null) ? namespaceId.app(appId) : namespaceId.app(appId, version);
    ScheduleId scheduleId = applicationId.schedule(scheduleName);
    String description = scheduleDescription == null ? null : scheduleDescription;
    ScheduleProgramInfo programInfo = new ScheduleProgramInfo(SchedulableProgramType.WORKFLOW, programIdParts[1]);
    List<Constraint> constraints = scheduleRunConcurrencyString == null ? ImmutableList.of() : ImmutableList.of(new ProtoConstraint.ConcurrencyConstraint(Integer.valueOf(scheduleRunConcurrencyString)));
    Map<String, String> propertiesMap = ArgumentParser.parseMap(schedulePropertiesString, ArgumentName.SCHEDULE_PROPERTIES.toString());
    ScheduleDetail scheduleDetail = new ScheduleDetail(scheduleName, description, programInfo, propertiesMap, new ProtoTrigger.TimeTrigger(cronExpression), constraints, null);
    scheduleClient.add(scheduleId, scheduleDetail);
    printStream.printf("Successfully added schedule '%s' in app '%s'\n", scheduleName, appId);
}
Also used : CommandInputError(io.cdap.cdap.cli.exception.CommandInputError) ProtoConstraint(io.cdap.cdap.proto.ProtoConstraint) Constraint(io.cdap.cdap.internal.schedule.constraint.Constraint) ScheduleId(io.cdap.cdap.proto.id.ScheduleId) ProtoTrigger(io.cdap.cdap.proto.ProtoTrigger) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) ScheduleDetail(io.cdap.cdap.proto.ScheduleDetail) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) ScheduleProgramInfo(io.cdap.cdap.api.workflow.ScheduleProgramInfo)

Example 5 with Constraint

use of io.cdap.cdap.internal.schedule.constraint.Constraint in project cdap by caskdata.

the class ConstraintCheckerService method startUp.

@Override
protected void startUp() throws Exception {
    LOG.info("Starting ConstraintCheckerService.");
    taskExecutorService = MoreExecutors.listeningDecorator(Executors.newCachedThreadPool(new ThreadFactoryBuilder().setNameFormat("constraint-checker-task-%d").build()));
    taskRunner = new ScheduleTaskRunner(store, lifecycleService, propertiesResolver, namespaceQueryAdmin, cConf);
    int numPartitions = cConf.getInt(Constants.Scheduler.JOB_QUEUE_NUM_PARTITIONS);
    for (int partition = 0; partition < numPartitions; partition++) {
        taskExecutorService.submit(new ConstraintCheckerThread(partition));
    }
    LOG.info("Started ConstraintCheckerService. state: " + state());
}
Also used : ScheduleTaskRunner(io.cdap.cdap.internal.app.runtime.schedule.ScheduleTaskRunner) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) CheckableConstraint(io.cdap.cdap.internal.app.runtime.schedule.constraint.CheckableConstraint) Constraint(io.cdap.cdap.internal.schedule.constraint.Constraint)

Aggregations

Constraint (io.cdap.cdap.internal.schedule.constraint.Constraint)6 ScheduleProgramInfo (io.cdap.cdap.api.workflow.ScheduleProgramInfo)4 ScheduleDetail (io.cdap.cdap.proto.ScheduleDetail)4 ApplicationId (io.cdap.cdap.proto.id.ApplicationId)4 ProtoConstraint (io.cdap.cdap.proto.ProtoConstraint)3 ArtifactSummary (io.cdap.cdap.api.artifact.ArtifactSummary)2 CommandInputError (io.cdap.cdap.cli.exception.CommandInputError)2 DelayConstraint (io.cdap.cdap.internal.app.runtime.schedule.constraint.DelayConstraint)2 TimeTrigger (io.cdap.cdap.internal.app.runtime.schedule.trigger.TimeTrigger)2 ProtoTrigger (io.cdap.cdap.proto.ProtoTrigger)2 ScheduledRuntime (io.cdap.cdap.proto.ScheduledRuntime)2 NamespaceId (io.cdap.cdap.proto.id.NamespaceId)2 ProgramId (io.cdap.cdap.proto.id.ProgramId)2 ScheduleId (io.cdap.cdap.proto.id.ScheduleId)2 HttpResponse (io.cdap.common.http.HttpResponse)2 Test (org.junit.Test)2 ThreadFactoryBuilder (com.google.common.util.concurrent.ThreadFactoryBuilder)1 ScheduleTaskRunner (io.cdap.cdap.internal.app.runtime.schedule.ScheduleTaskRunner)1 CheckableConstraint (io.cdap.cdap.internal.app.runtime.schedule.constraint.CheckableConstraint)1 BatchProgramSchedule (io.cdap.cdap.proto.BatchProgramSchedule)1