Flink——理解 allowLateness

本文详细解析了Flink中WindowOperator的allowLateness机制,包括其内部逻辑、代码实现及实际应用效果,帮助读者理解如何有效处理迟到数据。

什么鬼

WindowOperator 里面还有有一个叫做 allowLateness 的东西,这个东西什么鬼呢?简单来说就给迟到的数据第二次机会。我允许它迟到一定的时间。在规定的迟到时间内,只要要数据来了,就会触发第二次窗口计算,那到什么时候就没有第二次机会了呢?下面我们来娓娓道来。

allowLateness 的逻辑过程

二话不说,先来看一下下面的代码,在这段代码中,


```java
WindowOperator 中的成员变量
 
  	/**
	 * The allowed lateness for elements. This is used for:
	 * <ul>
	 *     <li>Deciding if an element should be dropped from a window due to lateness.
	 *     <li>Clearing the state of a window if the system time passes the
	 *         {@code window.maxTimestamp + allowedLateness} landmark.
	 * </ul>
	 */
	protected final long allowedLateness;

从上面的代码中,意思是允许元素迟到多长时间。它两个点作用:

  1. 根据迟到的时间,决定一个元素是否别丢弃。
  2. 如果系统时间超过了运行的最时间,Flink 就会清清理窗口的状态,这个阈值就是 window.maxTimestamp + allowedLateness 。超过这个值就会清楚窗口的状态。

如果想弄清楚这里的逻辑,就要到 WindowOperator onElement 这个方法里面找答案。

先来上代码:

public void processElement(StreamRecord<IN> element) throws Exception {
   
   
		final Collection<W> elementWindows = windowAssigner.assignWindows(
			element.getValue(), element.getTimestamp(), windowAssignerContext);

		//if element is handled by none of assigned elementWindows
		boolean isSkippedElement = true;

		final K key = this.<K>getKeyedStateBackend().getCurrentKey();

		if (windowAssigner instanceof MergingWindowAssigner) {
   
   
			MergingWindowSet<W> mergingWindows = getMergingWindowSet();

			for (W window: elementWindows) {
   
   

				// adding the new window might result in a merge, in that case the actualWindow
				// is the merged window and we work with that. If we don't merge then
				// actualWindow == window
				W actualWindow = mergingWindows.addWindow(window, new MergingWindowSet.MergeFunction<W>() {
   
   
					@Override
					public void merge(W mergeResult,
							Collection<W> mergedWindows, W stateWindowResult,
							Collection<W> mergedStateWindows) throws Exception {
   
   

						if ((windowAssigner.isEventTime() && mergeResult.maxTimestamp() + allowedLateness <= internalTimerService.currentWatermark())) {
   
   
							throw new UnsupportedOperationException("The end timestamp of an " +
									"event-time window cannot become earlier than the current watermark " +
									"by merging. Current watermark: " + internalTimerService.currentWatermark() +
									" window: " + mergeResult);
						} else if (!windowAssigner.isEventTime()) {
   
   
							long currentProcessingTime = internalTimerService.currentProcessingTime();
							if (mergeResult.maxTimestamp() <= currentProcessingTime) {
   
   
								throw new UnsupportedOperationException("The end timestamp of a " +
									"processing-time window cannot become earlier than the current processing time " +
									"by merging. Current processing ti
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值