use of co.cask.cdap.internal.app.runtime.schedule.trigger.StreamSizeTrigger in project cdap by caskdata.
the class ProgramLifecycleHttpHandler method toScheduleDetail.
private ScheduleDetail toScheduleDetail(ScheduleUpdateDetail updateDetail, ProgramSchedule existing) {
ScheduleUpdateDetail.Schedule scheduleUpdate = updateDetail.getSchedule();
if (scheduleUpdate == null) {
return new ScheduleDetail(null, null, null, updateDetail.getProperties(), null, null, null);
}
Trigger trigger = null;
if (scheduleUpdate.getCronExpression() != null && (scheduleUpdate.getStreamName() != null || scheduleUpdate.getDataTriggerMB() != null)) {
throw new IllegalArgumentException(String.format("Cannot define time trigger with cron expression and define stream size trigger with" + " stream name and data trigger configuration in the same schedule update details %s. " + "Schedule update detail must contain only one trigger.", updateDetail));
}
NamespaceId namespaceId = existing.getProgramId().getNamespaceId();
if (scheduleUpdate.getCronExpression() != null) {
trigger = new TimeTrigger(updateDetail.getSchedule().getCronExpression());
} else if (existing.getTrigger() instanceof StreamSizeTrigger) {
// if the existing trigger is StreamSizeTrigger, use the field in the existing trigger if the corresponding field
// in schedule update detail is null
StreamSizeTrigger existingTrigger = (StreamSizeTrigger) existing.getTrigger();
String streamName = Objects.firstNonNull(scheduleUpdate.getStreamName(), existingTrigger.getStreamId().getStream());
int dataTriggerMB = Objects.firstNonNull(scheduleUpdate.getDataTriggerMB(), existingTrigger.getTriggerMB());
trigger = new StreamSizeTrigger(namespaceId.stream(streamName), dataTriggerMB);
} else if (scheduleUpdate.getStreamName() != null && scheduleUpdate.getDataTriggerMB() != null) {
trigger = new StreamSizeTrigger(namespaceId.stream(scheduleUpdate.getStreamName()), scheduleUpdate.getDataTriggerMB());
} else if (scheduleUpdate.getStreamName() != null || scheduleUpdate.getDataTriggerMB() != null) {
throw new IllegalArgumentException(String.format("Only one of stream name and data trigger MB is defined in schedule update details %s. " + "Must provide both stream name and data trigger MB to update the existing schedule with " + "trigger of type %s to a schedule with stream size trigger.", updateDetail, existing.getTrigger().getClass()));
}
List<Constraint> constraints = toConstraints(scheduleUpdate.getRunConstraints());
return new ScheduleDetail(null, scheduleUpdate.getDescription(), null, updateDetail.getProperties(), trigger, constraints, null);
}
use of co.cask.cdap.internal.app.runtime.schedule.trigger.StreamSizeTrigger in project cdap by caskdata.
the class Schedulers method toScheduleCreationSpec.
public static ScheduleCreationSpec toScheduleCreationSpec(NamespaceId deployNamespace, Schedule schedule, String programName, Map<String, String> properties) {
Trigger trigger;
if (schedule instanceof TimeSchedule) {
trigger = new TimeTrigger(((TimeSchedule) schedule).getCronEntry());
} else {
StreamSizeSchedule streamSizeSchedule = ((StreamSizeSchedule) schedule);
trigger = new StreamSizeTrigger(deployNamespace.stream(streamSizeSchedule.getStreamName()), streamSizeSchedule.getDataTriggerMB());
}
Integer maxConcurrentRuns = schedule.getRunConstraints().getMaxConcurrentRuns();
List<Constraint> constraints = maxConcurrentRuns == null ? ImmutableList.<Constraint>of() : ImmutableList.<Constraint>of(new ConcurrencyConstraint(maxConcurrentRuns));
return new ScheduleCreationSpec(schedule.getName(), schedule.getDescription(), programName, properties, trigger, constraints, Schedulers.JOB_QUEUE_TIMEOUT_MILLIS);
}
use of co.cask.cdap.internal.app.runtime.schedule.trigger.StreamSizeTrigger in project cdap by caskdata.
the class Schedulers method toProgramSchedule.
public static ProgramSchedule toProgramSchedule(ApplicationId appId, ScheduleSpecification spec) {
Schedule schedule = spec.getSchedule();
ProgramType programType = ProgramType.valueOfSchedulableType(spec.getProgram().getProgramType());
ProgramId programId = appId.program(programType, spec.getProgram().getProgramName());
Trigger trigger;
if (schedule instanceof TimeSchedule) {
TimeSchedule timeSchedule = (TimeSchedule) schedule;
trigger = new TimeTrigger(timeSchedule.getCronEntry());
} else {
StreamSizeSchedule streamSchedule = (StreamSizeSchedule) schedule;
StreamId streamId = programId.getNamespaceId().stream(streamSchedule.getStreamName());
trigger = new StreamSizeTrigger(streamId, streamSchedule.getDataTriggerMB());
}
Integer maxConcurrentRuns = schedule.getRunConstraints().getMaxConcurrentRuns();
List<Constraint> constraints = maxConcurrentRuns == null ? ImmutableList.<Constraint>of() : ImmutableList.<Constraint>of(new ConcurrencyConstraint(maxConcurrentRuns));
return new ProgramSchedule(schedule.getName(), schedule.getDescription(), programId, spec.getProperties(), trigger, constraints);
}
use of co.cask.cdap.internal.app.runtime.schedule.trigger.StreamSizeTrigger in project cdap by caskdata.
the class JobQueueDataset method isTriggerSatisfied.
private boolean isTriggerSatisfied(Trigger trigger, List<Notification> notifications) {
if (trigger instanceof TimeTrigger || trigger instanceof StreamSizeTrigger) {
// is initially created
return true;
}
if (trigger instanceof PartitionTrigger) {
PartitionTrigger partitionTrigger = (PartitionTrigger) trigger;
int numPartitions = 0;
for (Notification notification : notifications) {
String numPartitionsString = notification.getProperties().get("numPartitions");
numPartitions += Integer.parseInt(numPartitionsString);
}
return numPartitions >= partitionTrigger.getNumPartitions();
}
throw new IllegalArgumentException("Unknown trigger class: " + trigger.getClass());
}
use of co.cask.cdap.internal.app.runtime.schedule.trigger.StreamSizeTrigger in project cdap by caskdata.
the class ProgramLifecycleHttpHandler method toScheduleDetail.
private ScheduleDetail toScheduleDetail(ApplicationId appId, ScheduleSpecification scheduleSpec) {
if (scheduleSpec.getSchedule() == null) {
throw new IllegalArgumentException("Schedule specification must contain schedule");
}
Trigger trigger;
if (scheduleSpec.getSchedule() instanceof TimeSchedule) {
trigger = new TimeTrigger(((TimeSchedule) scheduleSpec.getSchedule()).getCronEntry());
} else {
StreamSizeSchedule streamSchedule = (StreamSizeSchedule) scheduleSpec.getSchedule();
StreamId streamId = appId.getParent().stream(streamSchedule.getStreamName());
trigger = new StreamSizeTrigger(streamId, streamSchedule.getDataTriggerMB());
}
List<Constraint> runConstraints = toConstraints(scheduleSpec.getSchedule().getRunConstraints());
return new ScheduleDetail(scheduleSpec.getSchedule().getName(), scheduleSpec.getSchedule().getDescription(), scheduleSpec.getProgram(), scheduleSpec.getProperties(), trigger, runConstraints, null);
}
Aggregations