• 从ContinuousEventTimeTrigger/ContinuousProcessingTimeTrigger代码看如何实现一个自定义的触发器


    背景

    当我们想要实现提前触发计算的触发器时,我们可以使用ContinuousEventTimeTrigger/ContinuousProcessingTimeTrigger作为触发器达到比如几分钟触发一次计算并发送计算结果的类,我们本文就从代码角度解析下实现自定义触发器的一些注意事项

    ContinuousEventTimeTrigger源码解析

    @PublicEvolving
    public class ContinuousEventTimeTrigger<W extends Window> extends Trigger<Object, W> {
        private static final long serialVersionUID = 1L;
    
        private final long interval;
    
        /** When merging we take the lowest of all fire timestamps as the new fire timestamp. */
        private final ReducingStateDescriptor<Long> stateDesc =
                new ReducingStateDescriptor<>("fire-time", new Min(), LongSerializer.INSTANCE);
    
        private ContinuousEventTimeTrigger(long interval) {
            this.interval = interval;
        }
    
        @Override
        public TriggerResult onElement(Object element, long timestamp, W window, TriggerContext ctx)
                throws Exception {
    
            if (window.maxTimestamp() <= ctx.getCurrentWatermark()) {
                // if the watermark is already past the window fire immediately
                // 这里只需要fire触发计算,为什么不是FIRE_AND_PURGE?
                return TriggerResult.FIRE;
            } else {
                // 这里注册一个结束窗口的计时器是否必要?
                ctx.registerEventTimeTimer(window.maxTimestamp());
            }
    
            ReducingState<Long> fireTimestamp = ctx.getPartitionedState(stateDesc);
            if (fireTimestamp.get() == null) {
                long start = timestamp - (timestamp % interval);
                long nextFireTimestamp = start + interval;
                ctx.registerEventTimeTimer(nextFireTimestamp);
                fireTimestamp.add(nextFireTimestamp);
            }
    
            return TriggerResult.CONTINUE;
        }
    
        @Override
        public TriggerResult onEventTime(long time, W window, TriggerContext ctx) throws Exception {
    
    		//为什么是FIRE而不是FIRE_AND_PURGE
            if (time == window.maxTimestamp()) {
                return TriggerResult.FIRE;
            }
    
            ReducingState<Long> fireTimestampState = ctx.getPartitionedState(stateDesc);
    
            Long fireTimestamp = fireTimestampState.get();
    
            if (fireTimestamp != null && fireTimestamp == time) {
                fireTimestampState.clear();
                fireTimestampState.add(time + interval);
                ctx.registerEventTimeTimer(time + interval);
                return TriggerResult.FIRE;
            }
    
            return TriggerResult.CONTINUE;
        }
    
        @Override
        public TriggerResult onProcessingTime(long time, W window, TriggerContext ctx)
                throws Exception {
            return TriggerResult.CONTINUE;
        }
    
        @Override
        public void clear(W window, TriggerContext ctx) throws Exception {
            // 清除计时器
            ReducingState<Long> fireTimestamp = ctx.getPartitionedState(stateDesc);
            Long timestamp = fireTimestamp.get();
            if (timestamp != null) {
                ctx.deleteEventTimeTimer(timestamp);
                fireTimestamp.clear();
            }
        }
    
        @Override
        public boolean canMerge() {
            return true;
        }
    
        @Override
        public void onMerge(W window, OnMergeContext ctx) throws Exception {
            ctx.mergePartitionedState(stateDesc);
            Long nextFireTimestamp = ctx.getPartitionedState(stateDesc).get();
            if (nextFireTimestamp != null) {
                ctx.registerEventTimeTimer(nextFireTimestamp);
            }
        }
    
        @Override
        public String toString() {
            return "ContinuousEventTimeTrigger(" + interval + ")";
        }
    
        @VisibleForTesting
        public long getInterval() {
            return interval;
        }
    
        /**
         * Creates a trigger that continuously fires based on the given interval.
         *
         * @param interval The time interval at which to fire.
         * @param  The type of {@link Window Windows} on which this trigger can operate.
         */
        public static <W extends Window> ContinuousEventTimeTrigger<W> of(Time interval) {
            return new ContinuousEventTimeTrigger<>(interval.toMilliseconds());
        }
    
        private static class Min implements ReduceFunction<Long> {
            private static final long serialVersionUID = 1L;
    
            @Override
            public Long reduce(Long value1, Long value2) throws Exception {
                return Math.min(value1, value2);
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121

    ContinuousProcessingTimeTrigger源码

    @PublicEvolving
    public class ContinuousProcessingTimeTrigger<W extends Window> extends Trigger<Object, W> {
        private static final long serialVersionUID = 1L;
    
        private final long interval;
    
        /** When merging we take the lowest of all fire timestamps as the new fire timestamp. */
        private final ReducingStateDescriptor<Long> stateDesc =
                new ReducingStateDescriptor<>("fire-time", new Min(), LongSerializer.INSTANCE);
    
        private ContinuousProcessingTimeTrigger(long interval) {
            this.interval = interval;
        }
    
        @Override
        public TriggerResult onElement(Object element, long timestamp, W window, TriggerContext ctx)
                throws Exception {
            ReducingState<Long> fireTimestamp = ctx.getPartitionedState(stateDesc);
    
            timestamp = ctx.getCurrentProcessingTime();
    		// 注册计时器,为什么这里不需要类似ContinuousEventTimeTrigger一样注册一个窗口结束时间的计时器?
            if (fireTimestamp.get() == null) {
                long start = timestamp - (timestamp % interval);
                long nextFireTimestamp = start + interval;
    
                ctx.registerProcessingTimeTimer(nextFireTimestamp);
    
                fireTimestamp.add(nextFireTimestamp);
                return TriggerResult.CONTINUE;
            }
            return TriggerResult.CONTINUE;
        }
    
        @Override
        public TriggerResult onEventTime(long time, W window, TriggerContext ctx) throws Exception {
            return TriggerResult.CONTINUE;
        }
    
        @Override
        public TriggerResult onProcessingTime(long time, W window, TriggerContext ctx)
                throws Exception {
            ReducingState<Long> fireTimestamp = ctx.getPartitionedState(stateDesc);
    
            if (fireTimestamp.get().equals(time)) {
                fireTimestamp.clear();
                fireTimestamp.add(time + interval);
                ctx.registerProcessingTimeTimer(time + interval);
                return TriggerResult.FIRE;
            }
            //为什么这里没有FIRE_AND_PURGE?状态是何时清理的?
            return TriggerResult.CONTINUE;
        }
    
        @Override
        public void clear(W window, TriggerContext ctx) throws Exception {
            //清除计时器
            // State could be merged into new window.
            ReducingState<Long> fireTimestamp = ctx.getPartitionedState(stateDesc);
            Long timestamp = fireTimestamp.get();
            if (timestamp != null) {
                ctx.deleteProcessingTimeTimer(timestamp);
                fireTimestamp.clear();
            }
        }
    
        @Override
        public boolean canMerge() {
            return true;
        }
    
        @Override
        public void onMerge(W window, OnMergeContext ctx) throws Exception {
            // States for old windows will lose after the call.
            ctx.mergePartitionedState(stateDesc);
    
            // Register timer for this new window.
            Long nextFireTimestamp = ctx.getPartitionedState(stateDesc).get();
            if (nextFireTimestamp != null) {
                ctx.registerProcessingTimeTimer(nextFireTimestamp);
            }
        }
    
        @VisibleForTesting
        public long getInterval() {
            return interval;
        }
    
        @Override
        public String toString() {
            return "ContinuousProcessingTimeTrigger(" + interval + ")";
        }
    
        /**
         * Creates a trigger that continuously fires based on the given interval.
         *
         * @param interval The time interval at which to fire.
         * @param  The type of {@link Window Windows} on which this trigger can operate.
         */
        public static <W extends Window> ContinuousProcessingTimeTrigger<W> of(Time interval) {
            return new ContinuousProcessingTimeTrigger<>(interval.toMilliseconds());
        }
    
        private static class Min implements ReduceFunction<Long> {
            private static final long serialVersionUID = 1L;
    
            @Override
            public Long reduce(Long value1, Long value2) throws Exception {
                return Math.min(value1, value2);
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111

    疑问:

    1.为什么ContinuousEventTimeTrigger需要注册一个窗口结束时间的计时器而ContinuousProcessingTimeTrigger不注册?
    答案: 其实我们需要看下它注册后的目的作用是什么,ContinuousEventTimeTrigger的ontimer在处理窗口结束的触发器时会返回FIRE触发计算,那问题就来了,如果只是触发计算,那么如果没有,那么仅仅只是窗口结束的时候没有触发一次计算而已。所以这里不是必须的
    2.为什么ContinuousEventTimeTrigger/ContinuousProcessingTimeTrigger返回的结果是FIRE而不是FIRE_AND_PURGE?状态是什么时候清理的?
    答案: 首先要明确状态的清理这个逻辑,状态其实包括窗口的状态,触发器的状态等,返回FIRE仅仅是触发计算,而不会清理任何状态,而假设返回FIRE_AND_PURGE的作用是触发计算,并进行窗口状态的清理(注意这里是不包括触发器的状态的清理的),其实状态的清理是由WindowOperator在清理时间到时进行的(对于事件时间是窗口结束时间+迟到容忍间隔时间,对于处理时间是窗口结束时间),所以不必要在窗口结束时间到的时候返回FIRE_AND_PURGE,可以统一由WindowOperator在清理时间到之后统一清理状态

  • 相关阅读:
    nodejs面经
    Microsoft 365 Office安装指南
    SQL server中字段自增:IDENTITY、序列Sequence
    Netty 进阶学习(十)-- 协议设计与解析
    LVGL库入门教程02-基本控件与交互
    react hook---钩子函数的使用,useState,useReducer,useEffect,useRef,useContext,自定义hook
    【C++】unordered_map和unordered_set
    一篇博客通关Redis技术栈
    多线程-异步编排
    数智未来 持续创新 | 易趋受邀出席CIAS 2022中国数智汽车峰会
  • 原文地址:https://blog.csdn.net/lixia0417mul2/article/details/133547647