Flink的基于两阶段提交协议的事务数据汇实现

背景

在flink中可以通过使用事务性数据汇实现精准一次的保证,本文基于Kakfa的事务处理来看一下在Flink 内部如何实现基于两阶段提交协议的事务性数据汇.

1。首先在开始进行快照的时候也就是收到checkpoint通知的时候,在snapshot方法中会开启一个新的事务,代码如下:

java 复制代码
   public void snapshotState(FunctionSnapshotContext context) throws Exception {
        // this is like the pre-commit of a 2-phase-commit transaction
        // we are ready to commit and remember the transaction

        checkState(
                currentTransactionHolder != null,
                "bug: no transaction object when performing state snapshot");

        long checkpointId = context.getCheckpointId();
        LOG.debug(
                "{} - checkpoint {} triggered, flushing transaction '{}'",
                name(),
                context.getCheckpointId(),
                currentTransactionHolder);

        preCommit(currentTransactionHolder.handle);
        // 调用kafkaProducer.flush();清理上一个事务的状态(注意不是提交),只是确保前一个事务的所有资源清理完毕
        pendingCommitTransactions.put(checkpointId, currentTransactionHolder);
        LOG.debug("{} - stored pending transactions {}", name(), pendingCommitTransactions);
// 调用producer.beginTransaction();方法开启一个新的kafka事务
        currentTransactionHolder = beginTransactionInternal();
        LOG.debug("{} - started new transaction '{}'", name(), currentTransactionHolder);

        state.clear();
        state.add(
                new State<>(
                        this.currentTransactionHolder,
                        new ArrayList<>(pendingCommitTransactions.values()),
                        userContext));
    }

2.其次在JobManager通知检查点完成的通知方法,也就是notifyCheckpointComplete方法中提交事务

java 复制代码
Iterator<Map.Entry<Long, TransactionHolder<TXN>>> pendingTransactionIterator =
                pendingCommitTransactions.entrySet().iterator();
        Throwable firstError = null;

        while (pendingTransactionIterator.hasNext()) {
            Map.Entry<Long, TransactionHolder<TXN>> entry = pendingTransactionIterator.next();
            Long pendingTransactionCheckpointId = entry.getKey();
            TransactionHolder<TXN> pendingTransaction = entry.getValue();
            if (pendingTransactionCheckpointId > checkpointId) {
                continue;
            }

            LOG.info(
                    "{} - checkpoint {} complete, committing transaction {} from checkpoint {}",
                    name(),
                    checkpointId,
                    pendingTransaction,
                    pendingTransactionCheckpointId);

            logWarningIfTimeoutAlmostReached(pendingTransaction);
            try {
            //调用producer.commitTransaction()方法提交事务
                commit(pendingTransaction.handle);
            } catch (Throwable t) {
                if (firstError == null) {
                    firstError = t;
                }
            }

            LOG.debug("{} - committed checkpoint transaction {}", name(), pendingTransaction);

            pendingTransactionIterator.remove();
        }

        if (firstError != null) {
            throw new FlinkRuntimeException(
                    "Committing one of transactions failed, logging first encountered failure",
                    firstError);
        }

至此,一个两阶段提交的flink事务性数据汇完成了,这个事务性数据汇可以构成端到端一致性的一部分

相关推荐
EkihzniY14 分钟前
涉外政务登记,精准识读保障合规办理
大数据·政务
rainbow72424436 分钟前
系统学习AI的标准化路径,分阶段学习更高效
大数据·人工智能·学习
Guheyunyi1 小时前
节能降耗系统从“经验直觉”推向“精准智控”
大数据·数据库·人工智能·科技·信息可视化
跨境摸鱼1 小时前
选品别只看“需求”,更要看“供给”:亚马逊新思路——用“供给断层”挑出更好打的品
大数据·人工智能·跨境电商·亚马逊·跨境·营销策略
IvanCodes2 小时前
openGauss 实战手册:gsql 常用命令、认证配置与运维工具全解
大数据·数据库·sql·opengauss
CELLGENE BIOSCIENCE2 小时前
精准检测,洞见未来|赛唐生物应邀出席2026张江药谷产业发展闭门交流会,共话药物质量安全新篇章
大数据·人工智能
轻轻唱2 小时前
2026专业PPT设计服务商推荐:TOP10深度评测与选择指南
大数据·人工智能·算法
fanstuck2 小时前
从 0 到 1 构建企业智能体平台:openJiuwen 架构解析与智能客服工作流实战
大数据·人工智能·算法·架构·aigc
迎仔2 小时前
10-流处理引擎Flink介绍:大数据世界的“实时监控中心”
大数据·flink
Elastic 中国社区官方博客3 小时前
Jina Rerankers 为 Elastic 推理服务(EIS)带来了快速、多语言的重排序能力
大数据·人工智能·elasticsearch·搜索引擎·ai·全文检索·jina