|
| 1 | +# 窗口函数 |
| 2 | + |
| 3 | +## WindowOperator |
| 4 | + |
| 5 | +1. 先为元素分配一组窗口 |
| 6 | +2. 针对每个窗口,检查trigger是否触发,并修改timer状态 |
| 7 | +3. timer触发时,同样是检查trigger是否触发 |
| 8 | + |
| 9 | +```javascript |
| 10 | +void processElement(StreamRecord<IN> element){ |
| 11 | + const elementWindows = windowAssigner.assignWindows( |
| 12 | + element.getValue(), element.getTimestamp(), windowAssignerContext); |
| 13 | + for (const window of elementWindows) { |
| 14 | + TriggerResult triggerResult = triggerContext.onElement(element); |
| 15 | + |
| 16 | + if (triggerResult.isFire()) { |
| 17 | + ACC contents = windowState.get(); |
| 18 | + if (contents != null) { |
| 19 | + emitWindowContents(actualWindow, contents); |
| 20 | + } |
| 21 | + } |
| 22 | + |
| 23 | + if (triggerResult.isPurge()) { |
| 24 | + windowState.clear(); |
| 25 | + } |
| 26 | + registerCleanupTimer(actualWindow); |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +void onEventTime(InternalTimer<K, W> timer) { |
| 31 | + triggerContext.key = timer.getKey(); |
| 32 | + triggerContext.window = timer.getNamespace(); |
| 33 | + |
| 34 | + MergingWindowSet<W> mergingWindows; |
| 35 | + |
| 36 | + if (windowAssigner instanceof MergingWindowAssigner) { |
| 37 | + mergingWindows = getMergingWindowSet(); |
| 38 | + W stateWindow = mergingWindows.getStateWindow(triggerContext.window); |
| 39 | + if (stateWindow == null) { |
| 40 | + // Timer firing for non-existent window, this can only happen if a |
| 41 | + // trigger did not clean up timers. We have already cleared the merging |
| 42 | + // window and therefore the Trigger state, however, so nothing to do. |
| 43 | + return; |
| 44 | + } else { |
| 45 | + windowState.setCurrentNamespace(stateWindow); |
| 46 | + } |
| 47 | + } else { |
| 48 | + windowState.setCurrentNamespace(triggerContext.window); |
| 49 | + mergingWindows = null; |
| 50 | + } |
| 51 | + |
| 52 | + TriggerResult triggerResult = triggerContext.onEventTime(timer.getTimestamp()); |
| 53 | + |
| 54 | + if (triggerResult.isFire()) { |
| 55 | + ACC contents = windowState.get(); |
| 56 | + if (contents != null) { |
| 57 | + emitWindowContents(triggerContext.window, contents); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + if (triggerResult.isPurge()) { |
| 62 | + windowState.clear(); |
| 63 | + } |
| 64 | + |
| 65 | + if (windowAssigner.isEventTime() |
| 66 | + && isCleanupTime(triggerContext.window, timer.getTimestamp())) { |
| 67 | + clearAllState(triggerContext.window, windowState, mergingWindows); |
| 68 | + } |
| 69 | + |
| 70 | + if (mergingWindows != null) { |
| 71 | + // need to make sure to update the merging state in state |
| 72 | + mergingWindows.persist(); |
| 73 | + } |
| 74 | +} |
| 75 | +``` |
| 76 | + |
| 77 | +EvictorWindowOperator和WindowOperator的区别仅在于emit的时候是否调用evictor来删除窗口。 |
0 commit comments