Flink 中的事件时间触发器和处理时间触发器
EventTimeTrigger
EventTimeTrigger 的触发完全依赖 watermark,换言之,如果 stream 中没有 watermark,就不会触发 EventTimeTrigger。
watermark 之于事件时间就是如此重要,来看一下 watermark 的定义先~
Watermarks 是某个 event time 窗口中所有数据都到齐的标志。
Watermarks 作为数据流的一部分流动并携带时间戳 t,Watermark(t) 断言数据流中不会再有小于时间戳 t 的事件出现。
换言之,当 Watermark(t) 到达时,标志着所有小于时间戳 t 的事件都已到齐,可放心地对时间戳 t 之前的所有事件执行聚合、窗口关闭等动作。
来看一下 《Streaming Systems》书中关于 Watermark 原汁原味的定义:
Watermarks are temporal notions of input completeness in the event-time domain. Worded differently, they are the way the system measures progress and completeness relative to the event times of the records being processed in a stream of events.
That point in event time, E, is the point up to which the system believes all inputs with event times less than E have been observed. In other words, it’s an assertion that no more data with event times less than E will ever be seen again.
如下图所示,watermark 和 event 一样在 pipeline 中流动,并且都携带时间戳,W(4) 表示在此之后不会再收到事件时间小于 4 的事件,W(9) 表示在此之后不会再接收到事件时间小于 9 的事件。
假设我们准备对这个数据流做窗口聚合操作,时间窗口大小为 4 个时间单位,窗口内元素做求和聚合操作。示例代码如下:
input.window(TumblingEventTimeWindows.of(Time.minutes(4L)))
.trigger(EventTimeTrigger.create())
.reduce(new SumReduceFunction());
我们跟踪 Flink 源码看看 EventTimeTrigger 的实现逻辑:
/**
* A Trigger that fires once the watermark passes the end of the window to which a pane belongs.
*/
public class EventTimeTrigger extends Trigger<Object, TimeWindow> {
private static final long serialVersionUID = 1L;
private EventTimeTrigger() {}
@Override
public TriggerResult onElement(
Object element, long timestamp, TimeWindow window, TriggerContext ctx)
throws Exception {
if (window.maxTimestamp() <= ctx.getCurrentWatermark()) {
// if the watermark is already past the window fire immediately
return TriggerResult.FIRE;
} else {
ctx.registerEventTimeTimer(window.maxTimestamp());
return TriggerResult.CONTINUE;
}
}
@Override
public TriggerResult onEventTime(long time, TimeWindow window, TriggerContext ctx) {
return time == window.maxTimestamp() ? TriggerResult.FIRE : TriggerResult.CONTINUE;
}
public static EventTimeTrigger create() {
return new EventTimeTrigger();
}
}
onElement 方法在每次数据进入该 window 时都会触发:首先判断当前的 watermark 是否已经超过了 window 的最大时间(窗口右界时间),如果已经超过,返回触发结果 FIRE;如果尚未超过,则根据窗口右界时间注册一个事件时间定时器,标记触发结果为 CONTINUE。
继续跟踪 registerEventTimeTimer 的动作,发现原来是将定时器放到了一个优先队列 eventTimeTimersQueue 中。优先队列的权重为定时器的时间戳,时间越小越希望先被触发,自然排在队列的前面。
onEventTime 方法是在什么时候被调用呢,一路跟踪来到了 InternalTimerServiceImpl 类,发现只有在 advanceWatermark 的时候会触发 onEventTime,具体逻辑是:当 watermark 到来时,根据 watermark 携带的时间戳 t,从事件时间定时器队列中出队所有时间戳小于 t 的定时器,然后触发 onEventTime。onEventTime 方法的具体实现中,首先比较触发时间是否是自己当前窗口的结束时间,是则 FIRE,否则继续 CONTINUE。
public class InternalTimerServiceImpl<K, N> implements InternalTimerService<N> {
/** Event time timers that are currently in-flight. */
private final KeyGroupedInternalPriorityQueue<TimerHeapInternalTimer<K, N>>
eventTimeTimersQueue;
@Override
public void registerEventTimeTimer(N namespace, long time) {
eventTimeTimersQueue.add(
new TimerHeapInternalTimer<>(time, (K) keyContext.getCurrentKey(), namespace));
}
public void advanceWatermark(long time) throws Exception {
currentWatermark = time;
InternalTimer<K, N> timer;
while ((timer = eventTimeTimersQueue.peek()) != null && timer.getTimestamp() <= time) {
eventTimeTimersQueue.poll();
keyContext.setCurrentKey(timer.getKey());
triggerTarget.onEventTime(timer);
}
}
}
我们以上述示例数据为例,看一下具体的执行过程:
- 当事件到达时,根据事件时间将事件分配到相应的窗口中,事件 '2' 被分配到时间窗口 T1-T4 中,如下图所示:
- 后续时间陆续到达,事件 '2','3','1','3' 都陆续被分配到时间窗口 T1-T4 中,由于 currentWatermark 未超过窗口结束时间 4 ,因此注册事件时间定时器 Timer(4);事件 '7' 被分配到窗口 T5-T8 中,也因为 currentWatermark 未超过窗口结束时间 8,因此注册事件时间定时器 Timer(8)。目前为止事件时间定时器队列中有 2 个定时器。
- 重点来了,接下来到达的是 watermark(4),那么就去事件时间定时器队列中找到所有定时时间小于等于 4 的定时器,Timer(4) 出队,触发 onEventTime 方法,窗口 T1-T4 的结束时间和 Timer(4) 的时间戳相等,FIRE 窗口 T1-T4(图中标记为绿色表示 FIRE)。此时定时器队列中只剩下 1 个定时器。
- 重复同样过程,事件 '5','9','6' 接踵而至,'5', '6' 被分配到窗口 T5-T8 中,'9' 则被分配到新窗口 T9-T12 中。'5','6' 对应的注册定时器为 Timer(8),'9' 注册了一个新定时器 Timer(12)。此时定时器队列中剩下 2 个定时器。
watermark(9) 的到达会促使定时器 Timer(8) 出队,进而 FIRE 窗口 T5-T8。以此类推。
ProcessingTimeTrigger
与 EventTimeTrigger 相比,ProcessingTimeTrigger 就相对简单了,它的触发只依赖系统时间。我们跟踪 Flink 源码看看 ProcessingTimeTrigger 的实现逻辑:
/**
* A Trigger that fires once the current system time passes the end of the window to which a pane belongs.
*/
public class ProcessingTimeTrigger extends Trigger<Object, TimeWindow> {
private static final long serialVersionUID = 1L;
private ProcessingTimeTrigger() {}
@Override
public TriggerResult onElement(
Object element, long timestamp, TimeWindow window, TriggerContext ctx) {
ctx.registerProcessingTimeTimer(window.maxTimestamp());
return TriggerResult.CONTINUE;
}
@Override
public TriggerResult onProcessingTime(long time, TimeWindow window, TriggerContext ctx) {
return TriggerResult.FIRE;
}
/** Creates a new trigger that fires once system time passes the end of the window. */
public static ProcessingTimeTrigger create() {
return new ProcessingTimeTrigger();
}
}
onElement 方法在每次数据进入该 window 时都会触发:直接根据窗口结束时间注册一个处理时间定时器。同样是放到一个处理时间定时器优先队列中。
系统根据系统时间定时地从处理时间定时器队列中取出小于当前系统时间的定时器,然后调用 onProcessingTime 方法,FIRE 窗口。
public class InternalTimerServiceImpl<K, N> implements InternalTimerService<N> {
/** Processing time timers that are currently in-flight. */
private final KeyGroupedInternalPriorityQueue<TimerHeapInternalTimer<K, N>>
@Override
public void registerProcessingTimeTimer(N namespace, long time) {
InternalTimer<K, N> oldHead = processingTimeTimersQueue.peek();
if (processingTimeTimersQueue.add(
new TimerHeapInternalTimer<>(time, (K) keyContext.getCurrentKey(), namespace))) {
long nextTriggerTime = oldHead != null ? oldHead.getTimestamp() : Long.MAX_VALUE;
// check if we need to re-schedule our timer to earlier
if (time < nextTriggerTime) {
if (nextTimer != null) {
nextTimer.cancel(false);
}
nextTimer = processingTimeService.registerTimer(time, this::onProcessingTime);
}
}
}
private void onProcessingTime(long time) throws Exception {
// null out the timer in case the Triggerable calls registerProcessingTimeTimer()
// inside the callback.
nextTimer = null;
InternalTimer<K, N> timer;
while ((timer = processingTimeTimersQueue.peek()) != null && timer.getTimestamp() <= time) {
processingTimeTimersQueue.poll();
keyContext.setCurrentKey(timer.getKey());
triggerTarget.onProcessingTime(timer);
}
if (timer != null && nextTimer == null) {
nextTimer =
processingTimeService.registerTimer(
timer.getTimestamp(), this::onProcessingTime);
}
}
}
欢迎留言、讨论,转载请注明出处。
Flink 中的事件时间触发器和处理时间触发器的更多相关文章
- 「Flink」Flink中的时间类型
Flink中的时间类型和窗口是非常重要概念,是学习Flink必须要掌握的两个知识点. Flink中的时间类型 时间类型介绍 Flink流式处理中支持不同类型的时间.分为以下几种: 处理时间 Flink ...
- Flink Streaming基于滚动窗口的事件时间分析
使用flink-1.9.0进行的测试,在不同的并行度下,Flink对事件时间的处理逻辑不同.包括1.1在并行度为1的本地模式分析和1.2在多并行度的本地模式分析两部分.通过理论结合源码进行验证,得到具 ...
- 【源码解析】Flink 是如何基于事件时间生成Timestamp和Watermark
生成Timestamp和Watermark 的三个重载方法介绍可参见上一篇博客: Flink assignAscendingTimestamps 生成水印的三个重载方法 之前想研究下Flink是怎么处 ...
- 「Flink」事件时间与水印
我们先来以滚动时间窗口为例,来看一下窗口的几个时间参数与Flink流处理系统时间特性的关系. 获取窗口开始时间Flink源代码 获取窗口的开始时间为以下代码: org.apache.flink.str ...
- Flink架构(三)- 事件-时间(Event-Time)处理
3. 事件-时间(Event-Time)处理 在“时间语义”中,我们强调了在流处理应用中时间语义的重要性,并解释了处理时间与事件时间的不同点.处理时间较好理解,因为它基于本地机器的时间,它产生的是有点 ...
- Flink Application Development DataStream API Event Time--Flink应用开发DataStream API事件时间
目录 概览 事件时间 接下来去哪儿 水印生成 水印策略简介 使用水印策略 处理空闲源 写水印生成代码 写周期WatermarkGenerator代码 写符号形式的WatermarkGenerator代 ...
- Flink学习(二)Flink中的时间
摘自Apache Flink官网 最早的streaming 架构是storm的lambda架构 分为三个layer batch layer serving layer speed layer 一.在s ...
- C#中在多个地方调用同一个触发器从而触发同一个自定义委托的事件
场景 在Winfom中可以在页面上多个按钮或者右键的点击事件中触发同一个自定义的委托事件. 实现 在位置一按钮点击事件中触发 string parentPath = System.IO.Directo ...
- Flink中的window、watermark和ProcessFunction
一.Flink中的window 1,window简述 window 是一种切割无限数据为有限块进行处理的手段.Window 是无限数据流处理的核心,Window 将一个无限的 stream 拆分成有 ...
- 彻底搞清Flink中的Window
窗口 在流处理应用中,数据是连续不断的,因此我们不可能等到所有数据都到了才开始处理.当然我们可以每来一个消息就处理一次,但是有时我们需要做一些聚合类的处理,例如:在过去的1分钟内有多少用户点击了我们的 ...
随机推荐
- idea下spring切换jdk版本
1.首先打开项目配置设置 2. 修改project中的配置 3. 修改modules中的配置 这个方法不需要修改pom.xml文件 如果有问题请指正 及时修改 2022年9月10日16:42:16
- AutoMaper使用
使用 AutoMapper 进行赋值 一. 什么是 AutoMapper AutoMapper是对象到对象的映射工具.在完成映射规则之后,AutoMapper可以将源对象转换为目标对象. 二. Aut ...
- Linux 运行 Bitcoin 软件
首先进入官网 bitcoin.org 下载 Bitcoin Core. 下载得到 tar.gz 文件后解压,并安装: tar xzf bitcoin-25.0-x86_64-linux-gnu.tar ...
- 【Jmeter】之批量处理多接口压力测试
一.需求前提 1.有以下三个步骤: ①创建单据 ②审核单据 ③确认单据 让三个相关接口进行一连串批量请求操作,直到所有批量数据确认单据成功. 二.测试计划 需要说明的是,因为每个接口可能处理的不太一样 ...
- Mathematica的介绍及使用方法
Mathematica 是由 Wolfram Research 公司开发的数学软件,可用于数学.物理.工程.生物等领域的计算和建模.其官方网站为 www.wolfram.com/mathematica ...
- vue echarts map 中国地图显示不出来
测试区忽然无法显示中国地图,所以对比了一下测试区与开发环境中echarts版本的区别 测试区echarts版本为 5.4.2 开发环境为5.0.2 所以将package.json中的 "ec ...
- WebShell流量特征检测_中国菜刀篇
80后用菜刀,90后用蚁剑,95后用冰蝎和哥斯拉,以phpshell连接为例,本文主要是对这四款经典的webshell管理工具进行流量分析和检测. 什么是一句话木马? 1.定义 顾名思义就是执行恶意指 ...
- 【YashanDB知识库】自关联外键插入数据时报错:YAS-02033 foreign key constraint violated parent key not found
问题现象 使用如下的sql语句创建自关联外键表: drop table self_f_key; create table self_f_key(t1 number primary key not nu ...
- pgsql 查询及更新json字段的某个属性
pgsql 查询及更新json字段的某个属性 一.查询json字段中的某个属性 查询 t_user 表中json 字段 info 中的 name 属性 select info ->> 'n ...
- 如何使用ChatGPT自带插件
OpenAI的插件将ChatGPT连接到第三方应用程序.这些插件使ChatGPT能够与开发者定义的API进行交互,增强ChatGPT的能力,并使其能够执行广泛的操作.插件使ChatGPT能够做如下事情 ...