Search in sources :

Example 26 with ProgramSchedule

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

the class JobQueueDataset method addNotification.

@Override
public void addNotification(ProgramScheduleRecord record, Notification notification) {
    boolean jobExists = false;
    ProgramSchedule schedule = record.getSchedule();
    // Only add notifications for enabled schedules
    if (record.getMeta().getStatus() != ProgramScheduleStatus.SCHEDULED) {
        return;
    }
    // for the same schedule.
    if (schedule.getTrigger() instanceof AbstractSatisfiableCompositeTrigger) {
        scheduleIds.add(getRowKeyPrefix(schedule.getScheduleId()));
    }
    try (CloseableIterator<Job> jobs = getJobsForSchedule(schedule.getScheduleId())) {
        while (jobs.hasNext()) {
            Job job = jobs.next();
            if (job.getState() == Job.State.PENDING_TRIGGER) {
                // ConstraintCheckerService
                if (job.isToBeDeleted()) {
                    // ignore, it will be deleted by ConstraintCheckerService
                    continue;
                }
                long scheduleLastUpdated = record.getMeta().getLastUpdated();
                if (job.getScheduleLastUpdatedTime() != scheduleLastUpdated) {
                    // schedule has changed: this job is obsolete
                    table.put(getRowKey(job.getJobKey().getScheduleId(), job.getJobKey().getCreationTime()), IS_OBSOLETE_COL, Bytes.toBytes(System.currentTimeMillis()));
                } else if (System.currentTimeMillis() - job.getCreationTime() > job.getSchedule().getTimeoutMillis()) {
                    // job has timed out; mark it obsolete
                    table.put(getRowKey(job.getJobKey().getScheduleId(), job.getJobKey().getCreationTime()), IS_OBSOLETE_COL, Bytes.toBytes(System.currentTimeMillis()));
                } else {
                    jobExists = true;
                    addNotification(job, notification);
                    break;
                }
            }
        }
    }
    // if no job exists for the scheduleId, add a new job with the first notification
    if (!jobExists) {
        List<Notification> notifications = Collections.singletonList(notification);
        Job.State jobState = isTriggerSatisfied(schedule, notifications) ? Job.State.PENDING_CONSTRAINT : Job.State.PENDING_TRIGGER;
        put(new SimpleJob(schedule, System.currentTimeMillis(), notifications, jobState, record.getMeta().getLastUpdated()));
    }
}
Also used : ProgramSchedule(co.cask.cdap.internal.app.runtime.schedule.ProgramSchedule) AbstractSatisfiableCompositeTrigger(co.cask.cdap.internal.app.runtime.schedule.trigger.AbstractSatisfiableCompositeTrigger) Notification(co.cask.cdap.proto.Notification)

Example 27 with ProgramSchedule

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

the class CoreSchedulerService method addSchedules.

@Override
public void addSchedules(final Iterable<? extends ProgramSchedule> schedules) throws AlreadyExistsException, BadRequestException {
    checkStarted();
    for (ProgramSchedule schedule : schedules) {
        if (!schedule.getProgramId().getType().equals(ProgramType.WORKFLOW)) {
            throw new BadRequestException(String.format("Cannot schedule program %s of type %s: Only workflows can be scheduled", schedule.getProgramId().getProgram(), schedule.getProgramId().getType()));
        }
    }
    execute((StoreTxRunnable<Void, AlreadyExistsException>) store -> {
        store.addSchedules(schedules);
        for (ProgramSchedule schedule : schedules) {
            try {
                timeSchedulerService.addProgramSchedule(schedule);
            } catch (SchedulerException e) {
                LOG.error("Exception occurs when adding schedule {}", schedule, e);
                throw new RuntimeException(e);
            }
        }
        return null;
    }, AlreadyExistsException.class);
}
Also used : TransactionFailureException(org.apache.tephra.TransactionFailureException) MultiThreadDatasetCache(co.cask.cdap.data2.dataset2.MultiThreadDatasetCache) RetryStrategies(org.apache.tephra.RetryStrategies) Job(co.cask.cdap.internal.app.runtime.schedule.queue.Job) Inject(com.google.inject.Inject) LoggerFactory(org.slf4j.LoggerFactory) TimeoutException(java.util.concurrent.TimeoutException) ProgramScheduleStatus(co.cask.cdap.internal.app.runtime.schedule.ProgramScheduleStatus) ProgramType(co.cask.cdap.proto.ProgramType) ServiceUnavailableException(co.cask.cdap.common.ServiceUnavailableException) ScheduleId(co.cask.cdap.proto.id.ScheduleId) AbstractIdleService(com.google.common.util.concurrent.AbstractIdleService) RetryOnStartFailureService(co.cask.cdap.common.service.RetryOnStartFailureService) DatasetFramework(co.cask.cdap.data2.dataset2.DatasetFramework) SchedulerException(co.cask.cdap.internal.app.runtime.schedule.SchedulerException) ProgramId(co.cask.cdap.proto.id.ProgramId) SystemDatasetInstantiator(co.cask.cdap.data.dataset.SystemDatasetInstantiator) Schedulers(co.cask.cdap.internal.app.runtime.schedule.store.Schedulers) TransactionSystemClient(org.apache.tephra.TransactionSystemClient) AlreadyExistsException(co.cask.cdap.common.AlreadyExistsException) ProgramScheduleRecord(co.cask.cdap.internal.app.runtime.schedule.ProgramScheduleRecord) Uninterruptibles(com.google.common.util.concurrent.Uninterruptibles) Logger(org.slf4j.Logger) ApplicationId(co.cask.cdap.proto.id.ApplicationId) Collection(java.util.Collection) JobQueueDataset(co.cask.cdap.internal.app.runtime.schedule.queue.JobQueueDataset) Throwables(com.google.common.base.Throwables) Transactionals(co.cask.cdap.api.Transactionals) ConflictException(co.cask.cdap.common.ConflictException) Service(com.google.common.util.concurrent.Service) DynamicDatasetCache(co.cask.cdap.data2.dataset2.DynamicDatasetCache) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Transactions(co.cask.cdap.data2.transaction.Transactions) ProgramSchedule(co.cask.cdap.internal.app.runtime.schedule.ProgramSchedule) NotFoundException(co.cask.cdap.common.NotFoundException) ProgramScheduleStoreDataset(co.cask.cdap.internal.app.runtime.schedule.store.ProgramScheduleStoreDataset) Transactional(co.cask.cdap.api.Transactional) DatasetProperties(co.cask.cdap.api.dataset.DatasetProperties) VisibleForTesting(com.google.common.annotations.VisibleForTesting) BadRequestException(co.cask.cdap.common.BadRequestException) CloseableIterator(co.cask.cdap.api.dataset.lib.CloseableIterator) Collections(java.util.Collections) TimeSchedulerService(co.cask.cdap.internal.app.runtime.schedule.TimeSchedulerService) SchedulerException(co.cask.cdap.internal.app.runtime.schedule.SchedulerException) AlreadyExistsException(co.cask.cdap.common.AlreadyExistsException) ProgramSchedule(co.cask.cdap.internal.app.runtime.schedule.ProgramSchedule) BadRequestException(co.cask.cdap.common.BadRequestException)

Example 28 with ProgramSchedule

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

the class ProgramScheduleService method getStatus.

/**
 * Get the state of the given schedule
 *
 * @return the status of the given schedule
 * @throws NotFoundException if the schedule could not be found
 * @throws UnauthorizedException if the principal is not authorized to access the schedule program
 * @throws Exception if any other errors occurred while performing the authorization enforcement check
 */
public ProgramScheduleStatus getStatus(ScheduleId scheduleId) throws Exception {
    ProgramSchedule schedule = scheduler.getSchedule(scheduleId);
    AuthorizationUtil.ensureAccess(schedule.getProgramId(), authorizationEnforcer, authenticationContext.getPrincipal());
    return scheduler.getScheduleStatus(scheduleId);
}
Also used : ProgramSchedule(co.cask.cdap.internal.app.runtime.schedule.ProgramSchedule)

Example 29 with ProgramSchedule

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

the class ProgramScheduleService method resume.

/**
 * Resume the given schedule
 *
 * @param scheduleId the schedule to suspend
 * @throws NotFoundException if the schedule could not be found
 * @throws UnauthorizedException if the principal is not authorized to suspend the schedule
 * @throws Exception if any other errors occurred while performing the authorization enforcement check
 */
public void resume(ScheduleId scheduleId) throws Exception {
    ProgramSchedule schedule = scheduler.getSchedule(scheduleId);
    authorizationEnforcer.enforce(schedule.getProgramId(), authenticationContext.getPrincipal(), Action.EXECUTE);
    scheduler.enableSchedule(scheduleId);
}
Also used : ProgramSchedule(co.cask.cdap.internal.app.runtime.schedule.ProgramSchedule)

Example 30 with ProgramSchedule

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

the class ProgramScheduleService method get.

/**
 * Get the given schedule
 *
 * @return the schedule
 * @throws NotFoundException if the schedule could not be found
 * @throws UnauthorizedException if the principal is not authorized to access the schedule program
 * @throws Exception if any other errors occurred while performing the authorization enforcement check
 */
public ProgramSchedule get(ScheduleId scheduleId) throws Exception {
    ProgramSchedule schedule = scheduler.getSchedule(scheduleId);
    AuthorizationUtil.ensureAccess(schedule.getProgramId(), authorizationEnforcer, authenticationContext.getPrincipal());
    return schedule;
}
Also used : ProgramSchedule(co.cask.cdap.internal.app.runtime.schedule.ProgramSchedule)

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