Search in sources :

Example 6 with ProgramScheduleRecord

use of io.cdap.cdap.internal.app.runtime.schedule.ProgramScheduleRecord in project cdap by caskdata.

the class CoreSchedulerService method enableScheduleInternal.

private void enableScheduleInternal(ProgramScheduleStoreDataset store, ScheduleId scheduleId) throws IOException, NotFoundException, ConflictException, SchedulerException {
    ProgramScheduleRecord record = store.getScheduleRecord(scheduleId);
    if (ProgramScheduleStatus.SUSPENDED != record.getMeta().getStatus()) {
        throw new ConflictException("Schedule '" + scheduleId + "' is already enabled");
    }
    timeSchedulerService.resumeProgramSchedule(record.getSchedule());
    store.updateScheduleStatus(scheduleId, ProgramScheduleStatus.SCHEDULED);
}
Also used : ProfileConflictException(io.cdap.cdap.common.ProfileConflictException) ConflictException(io.cdap.cdap.common.ConflictException) ProgramScheduleRecord(io.cdap.cdap.internal.app.runtime.schedule.ProgramScheduleRecord)

Example 7 with ProgramScheduleRecord

use of io.cdap.cdap.internal.app.runtime.schedule.ProgramScheduleRecord in project cdap by caskdata.

the class ProgramScheduleService method getRecord.

/**
 * Get the schedule record for the given schedule ID
 *
 * @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 ProgramScheduleRecord getRecord(ScheduleId scheduleId) throws Exception {
    ProgramScheduleRecord record = scheduler.getScheduleRecord(scheduleId);
    accessEnforcer.enforce(record.getSchedule().getProgramId(), authenticationContext.getPrincipal(), StandardPermission.GET);
    return record;
}
Also used : ProgramScheduleRecord(io.cdap.cdap.internal.app.runtime.schedule.ProgramScheduleRecord)

Example 8 with ProgramScheduleRecord

use of io.cdap.cdap.internal.app.runtime.schedule.ProgramScheduleRecord in project cdap by caskdata.

the class ProgramScheduleStoreDataset method listSchedulesRecordsWithPrefix.

/**
 * List schedule records with the given key prefix and only returns the schedules that can pass the filter.
 *
 * @param prefixKeys the prefix of the schedule records to be listed
 * @param filter a filter that only returns true if the schedule record will be returned in the result
 * @return the schedule records with the given key prefix that can pass the filter
 */
private List<ProgramScheduleRecord> listSchedulesRecordsWithPrefix(Collection<Field<?>> prefixKeys, Predicate<ProgramSchedule> filter) throws IOException {
    List<ProgramScheduleRecord> result = new ArrayList<>();
    try (CloseableIterator<StructuredRow> iterator = scheduleStore.scan(Range.singleton(prefixKeys), Integer.MAX_VALUE)) {
        while (iterator.hasNext()) {
            StructuredRow row = iterator.next();
            String serializedSchedule = row.getString(StoreDefinition.ProgramScheduleStore.SCHEDULE);
            if (serializedSchedule != null) {
                ProgramSchedule schedule = GSON.fromJson(serializedSchedule, ProgramSchedule.class);
                if (schedule != null && filter.test(schedule)) {
                    result.add(new ProgramScheduleRecord(schedule, extractMetaFromRow(schedule.getScheduleId(), row)));
                }
            }
        }
    }
    return result;
}
Also used : ProgramSchedule(io.cdap.cdap.internal.app.runtime.schedule.ProgramSchedule) ArrayList(java.util.ArrayList) StructuredRow(io.cdap.cdap.spi.data.StructuredRow) ProgramScheduleRecord(io.cdap.cdap.internal.app.runtime.schedule.ProgramScheduleRecord)

Example 9 with ProgramScheduleRecord

use of io.cdap.cdap.internal.app.runtime.schedule.ProgramScheduleRecord in project cdap by caskdata.

the class ProgramScheduleStoreDataset method modifySchedulesTriggeredByDeletedProgram.

/**
 * Update all schedules that can be triggered by the given deleted program. A schedule will be removed if
 * the only {@link ProgramStatusTrigger} in it is triggered by the deleted program. Schedules with composite triggers
 * will be updated if the composite trigger can still be satisfied after the program is deleted, otherwise the
 * schedules will be deleted.
 *
 * @param programId the program id for which to delete the schedules
 * @return the IDs of the schedules that were deleted
 */
public List<ProgramSchedule> modifySchedulesTriggeredByDeletedProgram(ProgramId programId) throws IOException {
    long deleteTime = System.currentTimeMillis();
    List<ProgramSchedule> deleted = new ArrayList<>();
    Set<ProgramScheduleRecord> scheduleRecords = new HashSet<>();
    for (ProgramStatus status : ProgramStatus.values()) {
        scheduleRecords.addAll(findSchedules(Schedulers.triggerKeyForProgramStatus(programId, status)));
    }
    for (ProgramScheduleRecord scheduleRecord : scheduleRecords) {
        ProgramSchedule schedule = scheduleRecord.getSchedule();
        markScheduleAsDeleted(schedule.getScheduleId(), deleteTime);
        triggerStore.deleteAll(Range.singleton(getScheduleKeys(schedule.getScheduleId())));
        if (schedule.getTrigger() instanceof AbstractSatisfiableCompositeTrigger) {
            // get the updated composite trigger by removing the program status trigger of the given program
            Trigger updatedTrigger = ((AbstractSatisfiableCompositeTrigger) schedule.getTrigger()).getTriggerWithDeletedProgram(programId);
            if (updatedTrigger == null) {
                deleted.add(schedule);
                continue;
            }
            // if the updated composite trigger is not null, add the schedule back with updated composite trigger
            try {
                addScheduleWithStatus(new ProgramSchedule(schedule.getName(), schedule.getDescription(), schedule.getProgramId(), schedule.getProperties(), updatedTrigger, schedule.getConstraints(), schedule.getTimeoutMillis()), scheduleRecord.getMeta().getStatus(), System.currentTimeMillis());
            } catch (AlreadyExistsException e) {
                // this should never happen
                LOG.warn("Failed to add the schedule '{}' triggered by '{}' with updated trigger '{}', " + "skip adding this schedule.", schedule.getScheduleId(), programId, updatedTrigger, e);
            }
        } else {
            deleted.add(schedule);
        }
    }
    return deleted;
}
Also used : ProgramStatusTrigger(io.cdap.cdap.internal.app.runtime.schedule.trigger.ProgramStatusTrigger) SatisfiableTrigger(io.cdap.cdap.internal.app.runtime.schedule.trigger.SatisfiableTrigger) Trigger(io.cdap.cdap.api.schedule.Trigger) AbstractSatisfiableCompositeTrigger(io.cdap.cdap.internal.app.runtime.schedule.trigger.AbstractSatisfiableCompositeTrigger) AlreadyExistsException(io.cdap.cdap.common.AlreadyExistsException) ProgramSchedule(io.cdap.cdap.internal.app.runtime.schedule.ProgramSchedule) ArrayList(java.util.ArrayList) AbstractSatisfiableCompositeTrigger(io.cdap.cdap.internal.app.runtime.schedule.trigger.AbstractSatisfiableCompositeTrigger) ProgramScheduleRecord(io.cdap.cdap.internal.app.runtime.schedule.ProgramScheduleRecord) HashSet(java.util.HashSet) ProgramStatus(io.cdap.cdap.api.ProgramStatus)

Example 10 with ProgramScheduleRecord

use of io.cdap.cdap.internal.app.runtime.schedule.ProgramScheduleRecord in project cdap by caskdata.

the class ProgramLifecycleHttpHandler method doGetSchedules.

private void doGetSchedules(HttpResponder responder, ApplicationId applicationId, @Nullable ProgramId programId, @Nullable String triggerTypeStr, @Nullable String statusStr) throws Exception {
    ApplicationSpecification appSpec = store.getApplication(applicationId);
    if (appSpec == null) {
        throw new NotFoundException(applicationId);
    }
    ProgramScheduleStatus status;
    try {
        status = statusStr == null ? null : ProgramScheduleStatus.valueOf(statusStr);
    } catch (IllegalArgumentException e) {
        throw new BadRequestException(String.format("Invalid schedule status '%s'. Must be one of %s.", statusStr, Joiner.on(',').join(ProgramScheduleStatus.values())), e);
    }
    ProtoTrigger.Type triggerType;
    try {
        triggerType = triggerTypeStr == null ? null : ProtoTrigger.Type.valueOfCategoryName(triggerTypeStr);
    } catch (IllegalArgumentException e) {
        throw new BadRequestException(e.getMessage(), e);
    }
    Predicate<ProgramScheduleRecord> predicate = record -> true;
    if (status != null) {
        predicate = predicate.and(record -> record.getMeta().getStatus().equals(status));
    }
    if (triggerType != null) {
        predicate = predicate.and(record -> record.getSchedule().getTrigger().getType().equals(triggerType));
    }
    Collection<ProgramScheduleRecord> schedules;
    if (programId != null) {
        if (!appSpec.getProgramsByType(programId.getType().getApiProgramType()).contains(programId.getProgram())) {
            throw new NotFoundException(programId);
        }
        schedules = programScheduleService.list(programId, predicate);
    } else {
        schedules = programScheduleService.list(applicationId, predicate);
    }
    List<ScheduleDetail> details = schedules.stream().map(ProgramScheduleRecord::toScheduleDetail).collect(Collectors.toList());
    responder.sendJson(HttpResponseStatus.OK, GSON.toJson(details, Schedulers.SCHEDULE_DETAILS_TYPE));
}
Also used : BatchProgramSchedule(io.cdap.cdap.proto.BatchProgramSchedule) AuditDetail(io.cdap.cdap.common.security.AuditDetail) RunRecordDetail(io.cdap.cdap.internal.app.store.RunRecordDetail) BatchProgramResult(io.cdap.cdap.proto.BatchProgramResult) TypeToken(com.google.gson.reflect.TypeToken) MRJobInfoFetcher(io.cdap.cdap.app.mapreduce.MRJobInfoFetcher) MRJobInfo(io.cdap.cdap.proto.MRJobInfo) GsonBuilder(com.google.gson.GsonBuilder) ScheduledRuntime(io.cdap.cdap.proto.ScheduledRuntime) ProgramScheduleStatus(io.cdap.cdap.internal.app.runtime.schedule.ProgramScheduleStatus) ScheduleId(io.cdap.cdap.proto.id.ScheduleId) Map(java.util.Map) ProgramStatus(io.cdap.cdap.proto.ProgramStatus) ScheduleDetail(io.cdap.cdap.proto.ScheduleDetail) EnumSet(java.util.EnumSet) HttpRequest(io.netty.handler.codec.http.HttpRequest) Set(java.util.Set) Reader(java.io.Reader) Constraint(io.cdap.cdap.internal.schedule.constraint.Constraint) ProgramRunStatus(io.cdap.cdap.proto.ProgramRunStatus) ProgramScheduleRecord(io.cdap.cdap.internal.app.runtime.schedule.ProgramScheduleRecord) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) StandardCharsets(java.nio.charset.StandardCharsets) Id(io.cdap.cdap.common.id.Id) ApplicationSpecificationAdapter(io.cdap.cdap.internal.app.ApplicationSpecificationAdapter) TriggerCodec(io.cdap.cdap.internal.app.runtime.schedule.trigger.TriggerCodec) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) Joiner(com.google.common.base.Joiner) Singleton(com.google.inject.Singleton) RunRecord(io.cdap.cdap.proto.RunRecord) GET(javax.ws.rs.GET) SatisfiableTrigger(io.cdap.cdap.internal.app.runtime.schedule.trigger.SatisfiableTrigger) UnauthorizedException(io.cdap.cdap.security.spi.authorization.UnauthorizedException) ArrayList(java.util.ArrayList) NamespaceNotFoundException(io.cdap.cdap.common.NamespaceNotFoundException) ProgramRunId(io.cdap.cdap.proto.id.ProgramRunId) DiscoveryServiceClient(org.apache.twill.discovery.DiscoveryServiceClient) Nullable(javax.annotation.Nullable) Charsets(com.google.common.base.Charsets) AuditPolicy(io.cdap.cdap.common.security.AuditPolicy) BatchRunnableInstances(io.cdap.cdap.proto.BatchRunnableInstances) ProgramLiveInfo(io.cdap.cdap.proto.ProgramLiveInfo) ProgramLifecycleService(io.cdap.cdap.internal.app.services.ProgramLifecycleService) Throwables(com.google.common.base.Throwables) IOException(java.io.IOException) ConflictException(io.cdap.cdap.common.ConflictException) NotImplementedException(io.cdap.cdap.common.NotImplementedException) ServiceInstances(io.cdap.cdap.proto.ServiceInstances) InputStreamReader(java.io.InputStreamReader) ProgramRuntimeService(io.cdap.cdap.app.runtime.ProgramRuntimeService) Futures(com.google.common.util.concurrent.Futures) ProgramSpecification(io.cdap.cdap.api.ProgramSpecification) Schedulers(io.cdap.cdap.internal.app.runtime.schedule.store.Schedulers) RunCountResult(io.cdap.cdap.proto.RunCountResult) BatchProgramStatus(io.cdap.cdap.proto.BatchProgramStatus) JsonObject(com.google.gson.JsonObject) NamespaceQueryAdmin(io.cdap.cdap.common.namespace.NamespaceQueryAdmin) RandomEndpointStrategy(io.cdap.cdap.common.discovery.RandomEndpointStrategy) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) Inject(com.google.inject.Inject) ProgramScheduleService(io.cdap.cdap.scheduler.ProgramScheduleService) LoggerFactory(org.slf4j.LoggerFactory) Path(javax.ws.rs.Path) ServiceUnavailableException(io.cdap.cdap.common.ServiceUnavailableException) EndpointStrategy(io.cdap.cdap.common.discovery.EndpointStrategy) QueryParam(javax.ws.rs.QueryParam) Gson(com.google.gson.Gson) DefaultValue(javax.ws.rs.DefaultValue) Objects(com.google.common.base.Objects) ProgramHistory(io.cdap.cdap.proto.ProgramHistory) ConstraintCodec(io.cdap.cdap.internal.app.runtime.schedule.constraint.ConstraintCodec) DELETE(javax.ws.rs.DELETE) Containers(io.cdap.cdap.proto.Containers) Function(com.google.common.base.Function) ImmutableMap(com.google.common.collect.ImmutableMap) Predicate(java.util.function.Predicate) Collection(java.util.Collection) ApplicationSpecification(io.cdap.cdap.api.app.ApplicationSpecification) BatchProgramStart(io.cdap.cdap.proto.BatchProgramStart) BatchRunnable(io.cdap.cdap.proto.BatchRunnable) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) Collectors(java.util.stream.Collectors) ProgramStatusTrigger(io.cdap.cdap.internal.app.runtime.schedule.trigger.ProgramStatusTrigger) List(java.util.List) Type(java.lang.reflect.Type) CaseInsensitiveEnumTypeAdapterFactory(io.cdap.cdap.common.io.CaseInsensitiveEnumTypeAdapterFactory) Constants(io.cdap.cdap.common.conf.Constants) NotFoundException(io.cdap.cdap.common.NotFoundException) PathParam(javax.ws.rs.PathParam) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) BatchProgramHistory(io.cdap.cdap.proto.BatchProgramHistory) BatchProgramCount(io.cdap.cdap.proto.BatchProgramCount) HashMap(java.util.HashMap) ProgramType(io.cdap.cdap.proto.ProgramType) JsonElement(com.google.gson.JsonElement) NotRunningProgramLiveInfo(io.cdap.cdap.proto.NotRunningProgramLiveInfo) HashSet(java.util.HashSet) Trigger(io.cdap.cdap.api.schedule.Trigger) BatchProgram(io.cdap.cdap.proto.BatchProgram) Instances(io.cdap.cdap.proto.Instances) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) AbstractAppFabricHttpHandler(io.cdap.cdap.gateway.handlers.util.AbstractAppFabricHttpHandler) ProtoTrigger(io.cdap.cdap.proto.ProtoTrigger) Logger(org.slf4j.Logger) POST(javax.ws.rs.POST) HttpResponder(io.cdap.http.HttpResponder) JsonSyntaxException(com.google.gson.JsonSyntaxException) SchedulerException(io.cdap.cdap.internal.app.runtime.schedule.SchedulerException) ProgramId(io.cdap.cdap.proto.id.ProgramId) BadRequestException(io.cdap.cdap.common.BadRequestException) ProgramSchedule(io.cdap.cdap.internal.app.runtime.schedule.ProgramSchedule) Store(io.cdap.cdap.app.store.Store) TimeUnit(java.util.concurrent.TimeUnit) ServiceDiscoverable(io.cdap.cdap.common.service.ServiceDiscoverable) PUT(javax.ws.rs.PUT) Collections(java.util.Collections) ApplicationSpecification(io.cdap.cdap.api.app.ApplicationSpecification) NamespaceNotFoundException(io.cdap.cdap.common.NamespaceNotFoundException) NotFoundException(io.cdap.cdap.common.NotFoundException) ProgramScheduleStatus(io.cdap.cdap.internal.app.runtime.schedule.ProgramScheduleStatus) ProtoTrigger(io.cdap.cdap.proto.ProtoTrigger) BadRequestException(io.cdap.cdap.common.BadRequestException) ProgramScheduleRecord(io.cdap.cdap.internal.app.runtime.schedule.ProgramScheduleRecord) ScheduleDetail(io.cdap.cdap.proto.ScheduleDetail)

Aggregations

ProgramScheduleRecord (io.cdap.cdap.internal.app.runtime.schedule.ProgramScheduleRecord)12 ProgramSchedule (io.cdap.cdap.internal.app.runtime.schedule.ProgramSchedule)6 ProgramScheduleMeta (io.cdap.cdap.internal.app.runtime.schedule.ProgramScheduleMeta)5 NotFoundException (io.cdap.cdap.common.NotFoundException)4 ScheduleId (io.cdap.cdap.proto.id.ScheduleId)4 ArrayList (java.util.ArrayList)4 Notification (io.cdap.cdap.proto.Notification)3 ScheduleDetail (io.cdap.cdap.proto.ScheduleDetail)3 Throwables (com.google.common.base.Throwables)2 Inject (com.google.inject.Inject)2 Trigger (io.cdap.cdap.api.schedule.Trigger)2 ProgramStatusTrigger (io.cdap.cdap.internal.app.runtime.schedule.trigger.ProgramStatusTrigger)2 SatisfiableTrigger (io.cdap.cdap.internal.app.runtime.schedule.trigger.SatisfiableTrigger)2 ApplicationId (io.cdap.cdap.proto.id.ApplicationId)2 ProgramId (io.cdap.cdap.proto.id.ProgramId)2 ProgramScheduleService (io.cdap.cdap.scheduler.ProgramScheduleService)2 StructuredRow (io.cdap.cdap.spi.data.StructuredRow)2 IOException (java.io.IOException)2 Collection (java.util.Collection)2 HashSet (java.util.HashSet)2