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 的事件。

watermark 示意图

假设我们准备对这个数据流做窗口聚合操作,时间窗口大小为 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 中的事件时间触发器和处理时间触发器的更多相关文章

  1. 「Flink」Flink中的时间类型

    Flink中的时间类型和窗口是非常重要概念,是学习Flink必须要掌握的两个知识点. Flink中的时间类型 时间类型介绍 Flink流式处理中支持不同类型的时间.分为以下几种: 处理时间 Flink ...

  2. Flink Streaming基于滚动窗口的事件时间分析

    使用flink-1.9.0进行的测试,在不同的并行度下,Flink对事件时间的处理逻辑不同.包括1.1在并行度为1的本地模式分析和1.2在多并行度的本地模式分析两部分.通过理论结合源码进行验证,得到具 ...

  3. 【源码解析】Flink 是如何基于事件时间生成Timestamp和Watermark

    生成Timestamp和Watermark 的三个重载方法介绍可参见上一篇博客: Flink assignAscendingTimestamps 生成水印的三个重载方法 之前想研究下Flink是怎么处 ...

  4. 「Flink」事件时间与水印

    我们先来以滚动时间窗口为例,来看一下窗口的几个时间参数与Flink流处理系统时间特性的关系. 获取窗口开始时间Flink源代码 获取窗口的开始时间为以下代码: org.apache.flink.str ...

  5. Flink架构(三)- 事件-时间(Event-Time)处理

    3. 事件-时间(Event-Time)处理 在“时间语义”中,我们强调了在流处理应用中时间语义的重要性,并解释了处理时间与事件时间的不同点.处理时间较好理解,因为它基于本地机器的时间,它产生的是有点 ...

  6. Flink Application Development DataStream API Event Time--Flink应用开发DataStream API事件时间

    目录 概览 事件时间 接下来去哪儿 水印生成 水印策略简介 使用水印策略 处理空闲源 写水印生成代码 写周期WatermarkGenerator代码 写符号形式的WatermarkGenerator代 ...

  7. Flink学习(二)Flink中的时间

    摘自Apache Flink官网 最早的streaming 架构是storm的lambda架构 分为三个layer batch layer serving layer speed layer 一.在s ...

  8. C#中在多个地方调用同一个触发器从而触发同一个自定义委托的事件

    场景 在Winfom中可以在页面上多个按钮或者右键的点击事件中触发同一个自定义的委托事件. 实现 在位置一按钮点击事件中触发 string parentPath = System.IO.Directo ...

  9. Flink中的window、watermark和ProcessFunction

    一.Flink中的window 1,window简述  window 是一种切割无限数据为有限块进行处理的手段.Window 是无限数据流处理的核心,Window 将一个无限的 stream 拆分成有 ...

  10. 彻底搞清Flink中的Window

    窗口 在流处理应用中,数据是连续不断的,因此我们不可能等到所有数据都到了才开始处理.当然我们可以每来一个消息就处理一次,但是有时我们需要做一些聚合类的处理,例如:在过去的1分钟内有多少用户点击了我们的 ...

随机推荐

  1. Linux下错误解决方案

    错误 "E: Unable to correct problems, you have held broken packages."这种问题包破坏问题,可能是由于镜像源与系统版本不 ...

  2. 为什么说 Swoole 是 PHP 程序员技术水平的分水岭?

    大家好,我是码农先森. 谈到这个话题有些朋友心中不免会有疑惑,为什么是 Swoole 而不是其他呢?因为 Swoole 是基于 C/C++ 语言开发的高性能异步通信扩展,覆盖的特性足够的多,有利于 P ...

  3. Snap 使用

    Snap 是一个或多个应用程序的捆绑包,可在许多不同的 Linux 发行版中使用,无需依赖或修改.Snap 可从 Snap Store(一个拥有数百万用户的公共应用程序商店)中发现和安装.很多常用的软 ...

  4. iptables 工作过程整理

    转载注明出处: 1.概念和工作原理 iptables是Linux系统中用来配置防火墙的命令.iptables是工作在TCP/IP的二.三.四层,当主机收到一个数据包后,数据包先在内核空间处理,若发现目 ...

  5. 2024-09-04:用go语言,给定一个长度为n的数组 happiness,表示每个孩子的幸福值,以及一个正整数k,我们需要从这n个孩子中选出k个孩子。 在筛选过程中,每轮选择一个孩子时,所有尚未选

    2024-09-04:用go语言,给定一个长度为n的数组 happiness,表示每个孩子的幸福值,以及一个正整数k,我们需要从这n个孩子中选出k个孩子. 在筛选过程中,每轮选择一个孩子时,所有尚未选 ...

  6. redis 基准性能测试与变慢优化

    redis 参考目录: 生产级Redis 高并发分布式锁实战1:高并发分布式锁如何实现 https://www.cnblogs.com/yizhiamumu/p/16556153.html 生产级Re ...

  7. JavaScript Library – Lit

    前言 我写过一篇关于 Lit 的文章,Material Design, Angular Material, MDC, MWC, Lit 的关系. 如今 material-web MWC 已经发布 1. ...

  8. JavaScript – 基本语法

    参考 阮一峰 – 基本语法 Switch switch 经常用来取代 else if, 因为可读性比价高, 而且通常性能也比较好. standard 长这样 const orderStatus = ' ...

  9. docker安装及基本的镜像拉取

    docker 使用存储库安装 卸载它们以及相关的依赖项. yum remove docker \ docker-client \ docker-client-latest \ docker-commo ...

  10. 如何更改Wordpress语言为中文

    在使用WordPress的时候,一般安装默认语言是英文,可以在后台设置里面直接修改站点语言为简体中文,当后台没有语言选项框的这一栏,如下图所示,该怎么办呢? 这个时候我们可以找到文件wp-config ...