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事务性数据汇完成了,这个事务性数据汇可以构成端到端一致性的一部分

相关推荐
松果财经6 分钟前
让创业更有后劲,长沙用金融铺就“成长跑道”
大数据·人工智能
Deepoch6 分钟前
中国具身智能三大路径:极限挑战、柔性操作、普惠赋能,竞合共生
大数据·人工智能·物联网·机器人·具身模型·deepoc
开利网络22 分钟前
从“流量”到“留量”:长效用户运营的底层逻辑
大数据·运维·人工智能·自动化·云计算
转转技术团队41 分钟前
转转大数据与AI——数据治理安全打标实践
大数据·人工智能·后端
沃达德软件1 小时前
大数据治安防控中心
大数据·人工智能·信息可视化·数据挖掘·数据分析
Sinowintop1 小时前
领航自贸港新赛道:EDI 重构企业跨境业务高效增长体系
大数据·运维·服务器·edi·数据交换·国产edi·海南自贸港
TG:@yunlaoda360 云老大1 小时前
华为云国际站FunctionGraph支持哪些编程语言?
大数据·华为云·产品运营
跨境猫小妹2 小时前
跨境电商深水区:价值增长新范式,重构出海增长逻辑
大数据·人工智能·重构·产品运营·跨境电商·防关联
乐迪信息2 小时前
乐迪信息:AI摄像机识别煤矿出入井车辆数量异常检测
大数据·运维·人工智能·物联网·安全
写代码的【黑咖啡】2 小时前
大数据环境下如何维护模型文档:策略与实践
大数据