Search in sources :

Example 1 with ProgramSchedule

use of co.cask.cdap.internal.app.runtime.schedule.ProgramSchedule in project cdap by caskdata.

the class ProgramScheduleStoreDataset method addSchedules.

/**
   * Add one or more schedules to the store.
   *
   * @param schedules the schedules to add
   * @return the new schedules' last modified timestamp
   * @throws AlreadyExistsException if one of the schedules already exists
   */
public long addSchedules(Iterable<? extends ProgramSchedule> schedules) throws AlreadyExistsException {
    long currentTime = System.currentTimeMillis();
    for (ProgramSchedule schedule : schedules) {
        byte[] scheduleKey = rowKeyBytesForSchedule(schedule.getProgramId().getParent().schedule(schedule.getName()));
        if (!store.get(new Get(scheduleKey)).isEmpty()) {
            throw new AlreadyExistsException(schedule.getProgramId().getParent().schedule(schedule.getName()));
        }
        Put schedulePut = new Put(scheduleKey);
        schedulePut.add(SCHEDULE_COLUMN_BYTES, GSON.toJson(schedule));
        schedulePut.add(UPDATED_COLUMN_BYTES, currentTime);
        // initially suspended
        schedulePut.add(STATUS_COLUMN_BYTES, ProgramScheduleStatus.SUSPENDED.toString());
        store.put(schedulePut);
        int count = 0;
        for (String triggerKey : extractTriggerKeys(schedule)) {
            byte[] triggerRowKey = rowKeyBytesForTrigger(scheduleKey, count++);
            store.put(new Put(triggerRowKey, TRIGGER_KEY_COLUMN_BYTES, triggerKey));
        }
    }
    return currentTime;
}
Also used : AlreadyExistsException(co.cask.cdap.common.AlreadyExistsException) ProgramSchedule(co.cask.cdap.internal.app.runtime.schedule.ProgramSchedule) Get(co.cask.cdap.api.dataset.table.Get) Put(co.cask.cdap.api.dataset.table.Put) Constraint(co.cask.cdap.internal.schedule.constraint.Constraint)

Example 2 with ProgramSchedule

use of co.cask.cdap.internal.app.runtime.schedule.ProgramSchedule in project cdap by caskdata.

the class ProgramScheduleStoreDataset method migrateFromAppMetadataStore.

/**
   * Migrate schedules in a given namespace from app metadata store to ProgramScheduleStoreDataset
   *
   * @param namespaceId  the namespace with schedules to be migrated
   * @param appMetaStore app metadata store with schedules to be migrated
   * @return the lexicographically largest namespace id String with schedule migration completed
   */
public String migrateFromAppMetadataStore(NamespaceId namespaceId, Store appMetaStore) {
    String completedNamespace = getMigrationCompleteNamespace();
    if (completedNamespace != null && completedNamespace.compareTo(namespaceId.toString()) > 0) {
        return completedNamespace;
    }
    for (ApplicationSpecification appSpec : appMetaStore.getAllApplications(namespaceId)) {
        ApplicationId appId = namespaceId.app(appSpec.getName(), appSpec.getAppVersion());
        for (ScheduleSpecification scheduleSpec : appSpec.getSchedules().values()) {
            ProgramSchedule schedule = Schedulers.toProgramSchedule(appId, scheduleSpec);
            try {
                addSchedule(schedule);
            } catch (AlreadyExistsException e) {
                // This should never happen since no schedule with the same schedule key should exist before migration
                LOG.warn("Schedule {} already exists before schedule migration", schedule.getScheduleId(), e);
            }
        }
    }
    store.put(MIGRATION_COMPLETE_NAMESPACE_ROW_BYTES, MIGRATION_COMPLETE_NAMESPACE_COLUMN_BYTES, Bytes.toBytes(namespaceId.toString()));
    return namespaceId.toString();
}
Also used : ApplicationSpecification(co.cask.cdap.api.app.ApplicationSpecification) AlreadyExistsException(co.cask.cdap.common.AlreadyExistsException) ProgramSchedule(co.cask.cdap.internal.app.runtime.schedule.ProgramSchedule) ApplicationId(co.cask.cdap.proto.id.ApplicationId) ScheduleSpecification(co.cask.cdap.api.schedule.ScheduleSpecification)

Example 3 with ProgramSchedule

use of co.cask.cdap.internal.app.runtime.schedule.ProgramSchedule in project cdap by caskdata.

the class ProgramLifecycleService method suspendResumeSchedule.

/**
   * Performs an action (suspend/resume) on the given schedule
   *
   * @param scheduleId Id of the schedule
   * @param action the action to perform
   * @throws Exception if the given action is invalid or failed to perform a valid action on the schedule
   */
public void suspendResumeSchedule(ScheduleId scheduleId, String action) throws Exception {
    boolean doEnable;
    if (action.equals("disable") || action.equals("suspend")) {
        doEnable = false;
    } else if (action.equals("enable") || action.equals("resume")) {
        doEnable = true;
    } else {
        throw new BadRequestException("Action for schedules may only be 'enable', 'disable', 'suspend', or 'resume' but is'" + action + "'");
    }
    ProgramSchedule schedule = scheduler.getSchedule(scheduleId);
    authorizationEnforcer.enforce(schedule.getProgramId(), authenticationContext.getPrincipal(), Action.EXECUTE);
    if (doEnable) {
        scheduler.enableSchedule(scheduleId);
    } else {
        scheduler.disableSchedule(scheduleId);
    }
}
Also used : ProgramSchedule(co.cask.cdap.internal.app.runtime.schedule.ProgramSchedule) BadRequestException(co.cask.cdap.common.BadRequestException)

Example 4 with ProgramSchedule

use of co.cask.cdap.internal.app.runtime.schedule.ProgramSchedule in project cdap by caskdata.

the class ProgramLifecycleService method getScheduleStatus.

/**
   * Gets the state of the given schedule
   *
   * @return the status of the given schedule
   * @throws Exception if failed to get the state of the schedule
   */
public ProgramScheduleStatus getScheduleStatus(ScheduleId scheduleId) throws Exception {
    ApplicationId applicationId = scheduleId.getParent();
    ApplicationSpecification appSpec = store.getApplication(applicationId);
    if (appSpec == null) {
        throw new NotFoundException(applicationId);
    }
    ProgramSchedule schedule = scheduler.getSchedule(scheduleId);
    ensureAccess(schedule.getProgramId());
    return scheduler.getScheduleStatus(scheduleId);
}
Also used : ApplicationSpecification(co.cask.cdap.api.app.ApplicationSpecification) ProgramSchedule(co.cask.cdap.internal.app.runtime.schedule.ProgramSchedule) ProgramNotFoundException(co.cask.cdap.common.ProgramNotFoundException) ApplicationNotFoundException(co.cask.cdap.common.ApplicationNotFoundException) NotFoundException(co.cask.cdap.common.NotFoundException) ApplicationId(co.cask.cdap.proto.id.ApplicationId)

Example 5 with ProgramSchedule

use of co.cask.cdap.internal.app.runtime.schedule.ProgramSchedule in project cdap by caskdata.

the class TriggerCodecTest method testObjectContainingTrigger.

@Test
public void testObjectContainingTrigger() {
    ProgramSchedule proto1 = new ProgramSchedule("sched1", "one partition schedule", new NamespaceId("test").app("a").worker("ww"), ImmutableMap.of("prop3", "abc"), new ProtoTrigger.PartitionTrigger(new DatasetId("test1", "pdfs1"), 1), ImmutableList.<Constraint>of());
    ProgramSchedule sched1 = new ProgramSchedule("sched1", "one partition schedule", new NamespaceId("test").app("a").worker("ww"), ImmutableMap.of("prop3", "abc"), new PartitionTrigger(new DatasetId("test1", "pdfs1"), 1), ImmutableList.<Constraint>of());
    Assert.assertEquals(sched1, GSON.fromJson(GSON.toJson(proto1), ProgramSchedule.class));
    ProgramSchedule proto2 = new ProgramSchedule("schedone", "one time schedule", new NamespaceId("test3").app("abc").workflow("wf112"), ImmutableMap.of("prop", "all"), new ProtoTrigger.TimeTrigger("* * * 1 1"), ImmutableList.<Constraint>of());
    ProgramSchedule sched2 = new ProgramSchedule("schedone", "one time schedule", new NamespaceId("test3").app("abc").workflow("wf112"), ImmutableMap.of("prop", "all"), new TimeTrigger("* * * 1 1"), ImmutableList.<Constraint>of());
    Assert.assertEquals(sched2, GSON.fromJson(GSON.toJson(proto2), ProgramSchedule.class));
    ProgramSchedule proto3 = new ProgramSchedule("sched3", "one MB schedule", new NamespaceId("test3").app("abc").workflow("wf112"), ImmutableMap.of("prop", "all"), new ProtoTrigger.StreamSizeTrigger(new StreamId("x", "y"), 1), ImmutableList.<Constraint>of());
    ProgramSchedule sched3 = new ProgramSchedule("sched3", "one MB schedule", new NamespaceId("test3").app("abc").workflow("wf112"), ImmutableMap.of("prop", "all"), new StreamSizeTrigger(new StreamId("x", "y"), 1), ImmutableList.<Constraint>of());
    Assert.assertEquals(sched3, GSON.fromJson(GSON.toJson(proto3), ProgramSchedule.class));
}
Also used : StreamId(co.cask.cdap.proto.id.StreamId) ProtoTrigger(co.cask.cdap.proto.ProtoTrigger) ProgramSchedule(co.cask.cdap.internal.app.runtime.schedule.ProgramSchedule) NamespaceId(co.cask.cdap.proto.id.NamespaceId) DatasetId(co.cask.cdap.proto.id.DatasetId) Test(org.junit.Test)

Aggregations

ProgramSchedule (co.cask.cdap.internal.app.runtime.schedule.ProgramSchedule)35 NotFoundException (co.cask.cdap.common.NotFoundException)9 ApplicationId (co.cask.cdap.proto.id.ApplicationId)9 PartitionTrigger (co.cask.cdap.internal.app.runtime.schedule.trigger.PartitionTrigger)7 ProgramId (co.cask.cdap.proto.id.ProgramId)7 Test (org.junit.Test)7 BadRequestException (co.cask.cdap.common.BadRequestException)6 ScheduleId (co.cask.cdap.proto.id.ScheduleId)6 Row (co.cask.cdap.api.dataset.table.Row)5 AlreadyExistsException (co.cask.cdap.common.AlreadyExistsException)5 ProgramScheduleRecord (co.cask.cdap.internal.app.runtime.schedule.ProgramScheduleRecord)5 ScheduleDetail (co.cask.cdap.proto.ScheduleDetail)5 Scanner (co.cask.cdap.api.dataset.table.Scanner)4 TimeTrigger (co.cask.cdap.internal.app.runtime.schedule.trigger.TimeTrigger)4 ProgramType (co.cask.cdap.proto.ProgramType)4 ApplicationSpecification (co.cask.cdap.api.app.ApplicationSpecification)3 Get (co.cask.cdap.api.dataset.table.Get)3 Scan (co.cask.cdap.api.dataset.table.Scan)3 ScheduleSpecification (co.cask.cdap.api.schedule.ScheduleSpecification)3 Trigger (co.cask.cdap.api.schedule.Trigger)3