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分钟内有多少用户点击了我们的 ...
随机推荐
- git 修改提交作者及提交日期
进入交互式 rebase 模式 git rebase -i <commit> 你要修改哪次提交的日期,就 rebase 到该提交的上一次提交. git 提示你新的分支要包含哪些提交,默认已 ...
- 【openGauss】运维常用的SQL
一.查模式 SELECT pn.oid AS schema_oid, iss.catalog_name, iss.schema_owner, iss.schema_name FROM informat ...
- JDBC,SQL注入,事务,C3P0于Druid连接池(最详细解析)
JDBC JDBC(Java DataBase Connectivty,Java数据库连接)API,是一种用于执行Sql语句的Java API,可以为关系型数据库提供统一的访问,其由一组Java编写的 ...
- Asp.net core 学习笔记之 authentication + authorization + identity + identity server 4 + angular 第六篇 (authorization 之 simple authorization, role based, claim based, policy based)
authorization 授权是什么 ? 就是某个人必须符合某些条件才能做某些事儿 某个人指的是登入的 user 某些条件指的是 policy requirements 事儿指的是访问 contro ...
- SQL Server Temporary Table & Table Variable (临时表和表变量)
参考: 在数据库中临时表什么时候会被清除呢 Temporary Tables And Table Variables In SQL 基本常识 1. 局部临时表(#开头)只对当前连接有效,当前连接断开时 ...
- C++ 数据输入cin (解决CLoin输入中文程序出错)
数据输入cin 语法:cin >> 变量 解决 CLoin 使用cin输入中文程序无法正常运行 按住Ctrl+alt+shift+/键 弹出对话框选择注册表 取消勾选run.process ...
- map的线程安全,boost库读写锁的实现
*************** map的线程安全 ******************** * * 参考1 * map要实现线程安全必须要加锁,如果使用mutex会产生大量的线程等待,可以 ...
- Qt中一些关于中文的使用
本文包含以下内容: 中文编码 按中文字典排序 中文首字母查找 版本:Qt5.14.2 中文编码 在一些老项目中,发现项目中使用的文件是GBK编码,而新项目使用的是Unicode编码,在有一些操 ...
- [The Trellor] Chapter 1
翻译软件真的翻不好,读英文小说要相信你的脑子. There's only one thing to do in Berlen - that is listening the sound of wind ...
- 流式dma和一致性dma的区别
流式 DMA(Streaming DMA)和一致性 DMA(Consistent DMA)是两种不同的内存映射模式,用于 DMA(直接内存访问)操作.它们的主要区别在于缓存一致性.性能和使用场景.以下 ...