两阶段提交
1 FlinkKafkaProducer继承了TwoPhaseCommitSinkFunction
类
2 TwoPhaseCommitSinkFunction类继承了RichSinkFunction类且实现了CheckPointedFunction接口以及CheckPointListener接口
3 FlinkKafkaProducer重写了TwoPhaseCommitSinkFunction类中的invoke()方法以及CheckPointedFunction接口中的initializeState()方法和snapshotState()方法和CheckPointListener接口中的notifyCheckPointComplete()方法
4 数据写出需要进行的几个步骤:
(1)首先会调父类TwoPhaseCommitSinkFunction中的initializeState()方法初始化状态,如果以前有失败的事务,则会针对失败的事务再次提交-调用initializeState()->recoverAndCommitInternal()->recoverAndCommit()
->commit(),且initializeState()方法每个subTask只会调用一次
(2)调用FlinkKafkaProducer的invoke()方法,直到执行snapshotState()方法中的checkpoint,因为进行了checkpoint,所以事务失败了也会进行回滚,回到事务失败前的状态再次进行处理
(3)会调snapshotState()方法中的precommit()方法
(4)再调notifyCheckPointComplete()方法中的commit()方法
(5)最后调snapshotState()方法中的老状态清除新状态添加的方法