public abstract class Trigger implements Serializable { /** * Called for every element that gets added to a pane. The result of this will determine * whether the pane is evaluated to emit results. * * @param element The element that arrived. * @param timestamp The timestamp of the element that arrived. * @param window The window to which the element is being added. * @param ctx A context object that can be used to register timer callbacks. */ public abstract TriggerResult onElement(T element, long timestamp, W window, TriggerContext ctx) throws Exception; /** * Called when a processing-time timer that was set using the trigger context fires. * * @param time The timestamp at which the timer fired. * @param window The window for which the timer fired. * @param ctx A context object that can be used to register timer callbacks. */ public abstract TriggerResult onProcessingTime(long time, W window, TriggerContext ctx) throws Exception; /** * Called when an event-time timer that was set using the trigger context fires. * * @param time The timestamp at which the timer fired. * @param window The window for which the timer fired. * @param ctx A context object that can be used to register timer callbacks. */ public abstract TriggerResult onEventTime(long time, W window, TriggerContext ctx) throws Exception; /** * Called when several windows have been merged into one window by the * { @link org.apache.flink.streaming.api.windowing.assigners.WindowAssigner}. * * @param window The new window that results from the merge. * @param ctx A context object that can be used to register timer callbacks and access state. */ public TriggerResult onMerge(W window, OnMergeContext ctx) throws Exception { throw new RuntimeException("This trigger does not support merging."); }
/** * A { @link Trigger} that fires once the current system time passes the end of the window * to which a pane belongs. */public class ProcessingTimeTrigger implements Trigger
enum TriggerResult { CONTINUE(false, false), FIRE_AND_PURGE(true, true), FIRE(true, false), PURGE(false, true); private final boolean fire; private final boolean purge;}
两个选项,fire,purge,2×2,所以4种可能性
两个Result可以merge,
/** * Merges two { @code TriggerResults}. This specifies what should happen if we have * two results from a Trigger, for example as a result from * { @link Trigger#onElement(Object, long, Window, Trigger.TriggerContext)} and * { @link Trigger#onEventTime(long, Window, Trigger.TriggerContext)}. * *
* For example, if one result says {
@code CONTINUE} while the other says { @code FIRE} * then { @code FIRE} is the combined result; */public static TriggerResult merge(TriggerResult a, TriggerResult b) { if (a.purge || b.purge) { if (a.fire || b.fire) { return FIRE_AND_PURGE; } else { return PURGE; } } else if (a.fire || b.fire) { return FIRE; } else { return CONTINUE; }}
/** * A context object that is given to { @link Trigger} methods to allow them to register timer * callbacks and deal with state. */ public interface TriggerContext { long getCurrentProcessingTime(); long getCurrentWatermark(); /** * Register a system time callback. When the current system time passes the specified * time { @link Trigger#onProcessingTime(long, Window, TriggerContext)} is called with the time specified here. * * @param time The time at which to invoke { @link Trigger#onProcessingTime(long, Window, TriggerContext)} */ void registerProcessingTimeTimer(long time); void registerEventTimeTimer(long time); void deleteProcessingTimeTimer(long time); void deleteEventTimeTimer(long time); S getPartitionedState(StateDescriptor stateDescriptor); }
OnMergeContext 仅仅是多了一个接口,
public interface OnMergeContext extends TriggerContext { > void mergePartitionedState(StateDescriptor stateDescriptor);}
WindowOperator.Context作为TriggerContext的一个实现,
/** * { @code Context} is a utility for handling { @code Trigger} invocations. It can be reused * by setting the { @code key} and { @code window} fields. No internal state must be kept in * the { @code Context} */public class Context implements Trigger.OnMergeContext { protected K key; //Context对应的window上下文 protected W window; protected Collection mergedWindows; //onMerge中被赋值 @SuppressWarnings("unchecked") public S getPartitionedState(StateDescriptor stateDescriptor) { try { return WindowOperator.this.getPartitionedState(window, windowSerializer, stateDescriptor); //从backend里面读出改window的状态,即window buffer } catch (Exception e) { throw new RuntimeException("Could not retrieve state", e); } } @Override public > void mergePartitionedState(StateDescriptor stateDescriptor) { if (mergedWindows != null && mergedWindows.size() > 0) { try { WindowOperator.this.getStateBackend().mergePartitionedStates(window, //在backend层面把mergedWindows merge到window中 mergedWindows, windowSerializer, stateDescriptor); } catch (Exception e) { throw new RuntimeException("Error while merging state.", e); } } } @Override public void registerProcessingTimeTimer(long time) { Timer timer = new Timer<>(time, key, window); // make sure we only put one timer per key into the queue if (processingTimeTimers.add(timer)) { processingTimeTimersQueue.add(timer); //If this is the first timer added for this timestamp register a TriggerTask if (processingTimeTimerTimestamps.add(time, 1) == 0) { //如果这个window是第一次注册的话 ScheduledFuture scheduledFuture = WindowOperator.this.registerTimer(time, WindowOperator.this); //对于processTime必须注册定时器主动触发 processingTimeTimerFutures.put(time, scheduledFuture); } } } @Override public void registerEventTimeTimer(long time) { Timer timer = new Timer<>(time, key, window); if (watermarkTimers.add(timer)) { watermarkTimersQueue.add(timer); } } //封装一遍trigger的接口,并把self作为context传入trigger的接口中 public TriggerResult onElement(StreamRecord element) throws Exception { return trigger.onElement(element.getValue(), element.getTimestamp(), window, this); } public TriggerResult onProcessingTime(long time) throws Exception { return trigger.onProcessingTime(time, window, this); } public TriggerResult onEventTime(long time) throws Exception { return trigger.onEventTime(time, window, this); } public TriggerResult onMerge(Collection mergedWindows) throws Exception { this.mergedWindows = mergedWindows; return trigger.onMerge(window, this); }}
Evictor
/** * An { @code Evictor} can remove elements from a pane before it is being processed and after * window evaluation was triggered by a * { @link org.apache.flink.streaming.api.windowing.triggers.Trigger}. * *
* A pane is the bucket of elements that have the same key (assigned by the * {
@link org.apache.flink.api.java.functions.KeySelector}) and same { @link Window}. An element can * be in multiple panes of it was assigned to multiple windows by the * { @link org.apache.flink.streaming.api.windowing.assigners.WindowAssigner}. These panes all * have their own instance of the { @code Evictor}. * * @param The type of elements that this { @code Evictor} can evict. * @param The type of { @link Window Windows} on which this { @code Evictor} can operate. */public interface Evictor extends Serializable { /** * Computes how many elements should be removed from the pane. The result specifies how * many elements should be removed from the beginning. * * @param elements The elements currently in the pane. * @param size The current number of elements in the pane. * @param window The { @link Window} */ int evict(Iterable > elements, int size, W window);}
/** * An { @link Evictor} that keeps only a certain amount of elements. * * @param The type of { @link Window Windows} on which this { @code Evictor} can operate. */public class CountEvictor implements Evictor
/** * An { @link Evictor} that keeps elements for a certain amount of time. Elements older * than { @code current_time - keep_time} are evicted. * * @param The type of { @link Window Windows} on which this { @code Evictor} can operate. */public class TimeEvictor implements Evictor { private static final long serialVersionUID = 1L; private final long windowSize; public TimeEvictor(long windowSize) { this.windowSize = windowSize; } @Override public int evict(Iterable > elements, int size, W window) { int toEvict = 0; long currentTime = Iterables.getLast(elements).getTimestamp(); long evictCutoff = currentTime - windowSize; for (StreamRecord record: elements) { if (record.getTimestamp() > evictCutoff) { break; } toEvict++; } return toEvict; }}