源码解析flink的GenericWriteAheadSink为什么做不到精确一次输出

背景

GenericWriteAheadSink是可以用于几乎是精准一次输出的场景,为什么说是几乎精准一次呢?我们从源码的角度分析一下

GenericWriteAheadSink做不到精准一次输出的原因

首先我们看一下flink检查点完成后通知GenericWriteAheadSink开始进行分段的记录输出并提交事务的代码

java 复制代码
pubblic void notifyCheckpointComplete(long checkpointId) throws Exception {
        super.notifyCheckpointComplete(checkpointId);

        synchronized (pendingCheckpoints) {
            Iterator<PendingCheckpoint> pendingCheckpointIt = pendingCheckpoints.iterator();
            while (pendingCheckpointIt.hasNext()) {

                PendingCheckpoint pendingCheckpoint = pendingCheckpointIt.next();

                long pastCheckpointId = pendingCheckpoint.checkpointId;
                int subtaskId = pendingCheckpoint.subtaskId;
                long timestamp = pendingCheckpoint.timestamp;
                StreamStateHandle streamHandle = pendingCheckpoint.stateHandle;

                if (pastCheckpointId <= checkpointId) {
                    try {
                        if (!committer.isCheckpointCommitted(subtaskId, pastCheckpointId)) {
                            try (FSDataInputStream in = streamHandle.openInputStream()) {
                            //开始把分段记录列表的记录进行输出
                                boolean success =
                                        sendValues(
                                                new ReusingMutableToRegularIteratorWrapper<>(
                                                        new InputViewIterator<>(
                                                                new DataInputViewStreamWrapper(in),
                                                                serializer),
                                                        serializer),
                                                pastCheckpointId,
                                                timestamp);
                                if (success) {
                                   //把分段记录列表输出成功后提交事务
                                    committer.commitCheckpoint(subtaskId, pastCheckpointId);
                                    streamHandle.discardState();
                                    pendingCheckpointIt.remove();
                                }
                            }
                        } else {
                            streamHandle.discardState();
                            pendingCheckpointIt.remove();
                        }
                    } catch (Exception e) {
                        // we have to break here to prevent a new (later) checkpoint
                        // from being committed before this one
                        LOG.error("Could not commit checkpoint.", e);
                        break;
                    }
                }
            }
        }
    }

从上面的源码可以看出,sendValue方法和提交事务commitCheckpoint方法并不能保证原子性,这就意味着如果sendValue执行了一部分或者全部,而提交事务方法commitCheckpoint失败,那么此时这个检查点对应的事务相当于就没有完成,在下一个检查点的通知消息中,会把历史检查点的事务重新sendValue然后进行commit一次,这就意味着相同的记录会执行两次sendValue操作,这就是GenericWriteAheadSink不能保证精准一次的原因

相关推荐
文丑颜不良啊14 分钟前
LeetCode2821~2840题目解析
java·leetcode
apcipot_rain1 小时前
C++转Java日记(10)
java·开发语言
简 洁 冬冬1 小时前
泛型方法写法全面辨析:从「能编译」到「能跑」到「能用」
java·泛型
云南新人和数采集团2 小时前
云南靠谱软件开发公司选型指南:以昆明新腾科技为例
大数据·人工智能·科技
LGL6030A2 小时前
Java学习历程31——初识Spring Boot
java·spring boot·学习
AllData公司负责人2 小时前
AI大模型数据开发底座系列说明书:AllData集成DB-GPT开源项目建设 AI 多模态分析底座,涵盖模态解析/智能大屏/MCP 扩展!
大数据·数据库·人工智能·gpt·数据采集·数据接入·模态解析
Lansonli4 小时前
大数据Spark(八十八):Spark任务资源之Standalone集群测试
java·服务器·spark
budingxiaomoli4 小时前
Spring AOP
java·spring·java-ee
特立独行的猫A4 小时前
Ionic + 仓颉语言的 cj-tauri 开发框架实战:开发精美移动端 APP
java·后端·python
nice_lcj5204 小时前
LRU 缓存入门:它是什么、怎么用、面试怎么手写
java·数据结构·缓存·面试·lru