Flink Checkpoint-轻量级分布式快照

本文根据 Apache Flink 进阶篇系列直播课程整理而成,由阿里巴巴高级研发工程师唐云(茶干)分享,主要讲解 Flink 中 Checkpoint 的应用实践,包括四个部分,分别是 Checkpoint 与 state 的关系、什么是 state、如何在 Flink 中使用 state 和 Checkpoint 的执行机制。

Checkpoint 与 state 的关系
state 其实就是 Checkpoint 所做的主要持久化备份的主要数据。

Checkpoint 是从 source 触发到下游所有节点完成的一次全局操作。下图可以有一个对 Checkpoint 的直观感受,红框里面可以看到一共触发了 569K 次 Checkpoint,然后全部都成功完成,没有 fail 的。

img

看下图的具体数据统计,其 state 也就 9kb 大小 。

img

什么是 state
先看一个非常经典的 word count 代码,当我们在终端输入 hello world,执行程序会输出什么?

img

答案很明显,(hello, 1) 和 (word,1)。

那么问题来了,如果再次在终端输入 hello world,程序会输入什么?

答案其实也很明显,(hello, 2) 和 (world, 2)。为什么 Flink 知道之前已经处理过一次 hello world,这就是 state 发挥作用了,这里是 keyed state 存储了之前需要统计的数据,所以帮助 Flink 知道 hello 和 world 之前分别出现过一次。

回顾一下刚才这段 word count 代码。keyby 接口的调用会创建 keyed stream 对 key 进行划分,这是使用 keyed state 的前提。在此之后,sum 方法会调用内置的 StreamGroupedReduce 实现。

img

什么是 keyed state
对于 keyed state,有两个特点:

只能应用于 KeyedStream 的函数与操作中,例如 Keyed UDF, window state

keyed state 是已经分区 / 划分好的,每一个 key 只能属于某一个 keyed state

对于如何理解已经分区的概念,我们需要看一下 keyby 的语义,大家可以看到下图左边有三个并发,右边也是三个并发,左边的词进来之后,通过 keyby 会进行相应的分发。例如对于 hello word,hello 这个词通过 hash 运算永远只会到右下方并发的 task 上面去。

img

什么是 operator state

又称为 non-keyed state,每一个 operator state 都仅与一个 operator 的实例绑定

常见的 operator state 是 source state,例如记录当前 source 的 offset

再看一段使用 operator state 的 word count 代码:

http://www.5cocoi.com/muying/beiyun/2020-06-23/268618.html
http://www.5cocoi.com/muying/beiyun/2020-06-23/268616.html
http://www.5cocoi.com/muying/beiyun/2020-06-23/268615.html
http://www.5cocoi.com/muying/beiyun/2020-06-23/268613.html
http://www.5cocoi.com/muying/beiyun/2020-06-23/268611.html
http://www.5cocoi.com/muying/beiyun/2020-06-23/268608.html
http://www.5cocoi.com/muying/beiyun/2020-06-23/268605.html
http://www.5cocoi.com/muying/beiyun/2020-06-23/268604.html
http://www.5cocoi.com/muying/beiyun/2020-06-23/268603.html
http://www.5cocoi.com/muying/beiyun/2020-06-23/268602.html
http://www.5cocoi.com/muying/beiyun/2020-06-23/268600.html
http://www.5cocoi.com/muying/beiyun/2020-06-23/268599.html
http://www.5cocoi.com/muying/beiyun/2020-06-23/268598.html
http://www.5cocoi.com/muying/beiyun/2020-06-23/268593.html
http://www.5cocoi.com/muying/beiyun/2020-06-23/268591.html
http://www.5cocoi.com/muying/beiyun/2020-06-23/268587.html
http://www.5cocoi.com/muying/beiyun/2020-06-23/268585.html
http://www.5cocoi.com/muying/beiyun/2020-06-23/268584.html
http://www.5cocoi.com/muying/beiyun/2020-06-23/268583.html
http://www.5cocoi.com/muying/beiyun/2020-06-23/268579.html
http://www.5cocoi.com/muying/beiyun/2020-06-23/268568.html
http://www.5cocoi.com/muying/beiyun/2020-06-23/268566.html
http://www.5cocoi.com/muying/beiyun/2020-06-22/266688.html
http://www.5cocoi.com/muying/beiyun/2020-06-22/266629.html
http://www.5cocoi.com/muying/beiyun/2020-06-22/266622.html
http://www.5cocoi.com/muying/beiyun/2020-06-22/266620.html
http://www.5cocoi.com/muying/beiyun/2020-06-22/266618.html
http://www.5cocoi.com/muying/beiyun/2020-06-22/266616.html
http://www.5cocoi.com/muying/beiyun/2020-06-22/266613.html
http://www.5cocoi.com/muying/beiyun/2020-06-22/266609.html
http://www.5cocoi.com/muying/beiyun/2020-06-22/266608.html
http://www.5cocoi.com/muying/beiyun/2020-06-22/266604.html
http://www.5cocoi.com/muying/beiyun/2020-06-22/266602.html
http://www.5cocoi.com/muying/beiyun/2020-06-22/266596.html
http://www.5cocoi.com/muying/beiyun/2020-06-22/266593.html
http://www.5cocoi.com/muying/beiyun/2020-06-22/266590.html
http://www.5cocoi.com/muying/beiyun/2020-06-22/266588.html
http://www.5cocoi.com/muying/beiyun/2020-06-22/266586.html
http://www.5cocoi.com/muying/beiyun/2020-06-22/266583.html
http://www.5cocoi.com/muying/beiyun/2020-06-22/266581.html
http://www.5cocoi.com/muying/beiyun/2020-06-22/266578.html
http://www.5cocoi.com/muying/beiyun/2020-06-22/266576.html
http://www.5cocoi.com/muying/beiyun/2020-06-22/266573.html
http://www.5cocoi.com/muying/beiyun/2020-06-22/266572.html
http://www.5cocoi.com/muying/beiyun/2020-06-22/266569.html
http://www.5cocoi.com/muying/beiyun/2020-06-22/266567.html
http://www.5cocoi.com/muying/beiyun/2020-06-22/266566.html
http://www.5cocoi.com/muying/beiyun/2020-06-22/266561.html
http://www.5cocoi.com/muying/beiyun/2020-06-22/266557.html
http://www.5cocoi.com/muying/beiyun/2020-06-22/266555.html

这里的 fromElements 会调用 FromElementsFunction 的类,其中就使用了类型为 list state 的 operator state。根据 state 类型做一个分类如下图:

img

除了从这种分类的角度,还有一种分类的角度是从 Flink 是否直接接管:

Managed State:由 Flink 管理的 state,刚才举例的所有 state 均是 managed state

Raw State:Flink 仅提供 stream 可以进行存储数据,对 Flink 而言 raw state 只是一些 bytes

在实际生产中,都只推荐使用 managed state,本文将围绕该话题进行讨论。

如何在 Flink 中使用 state
下图就前文 word count 的 sum 所使用的 StreamGroupedReduce 类为例讲解了如何在代码中使用 keyed state:

img

下图则对 word count 示例中的 FromElementsFunction 类进行详解并分享如何在代码中使用 operator state:

img

Checkpoint 的执行机制
在介绍 Checkpoint 的执行机制前,我们需要了解一下 state 的存储,因为 state 是 Checkpoint 进行持久化备份的主要角色。

Statebackend 的分类
下图阐释了目前 Flink 内置的三类 state backend,其中 MemoryStateBackend 和 FsStateBackend 在运行时都是存储在 java heap 中的,只有在执行 Checkpoint 时,FsStateBackend 才会将数据以文件格式持久化到远程存储上。而 RocksDBStateBackend 则借用了 RocksDB(内存磁盘混合的 LSM DB)对 state 进行存储。

img

对于 HeapKeyedStateBackend,有两种实现:

支持异步 Checkpoint(默认):存储格式 CopyOnWriteStateMap

仅支持同步 Checkpoint:存储格式 NestedStateMap

特别在 MemoryStateBackend 内使用 HeapKeyedStateBackend 时,Checkpoint 序列化数据阶段默认有最大 5 MB 数据的限制。

对于 RocksDBKeyedStateBackend,每个 state 都存储在一个单独的 column family 内,其中 keyGroup,Key 和 Namespace 进行序列化存储在 DB 作为 key。

img

Checkpoint 执行机制详解
本小节将对 Checkpoint 的执行流程逐步拆解进行讲解,下图左侧是 Checkpoint Coordinator,是整个 Checkpoint 的发起者,中间是由两个 source,一个 sink 组成的 Flink 作业,最右侧的是持久化存储,在大部分用户场景中使用HDFS。

a. 第一步,Checkpoint Coordinator 向所有 source 节点 trigger Checkpoint。

img

b. 第二步,source 节点向下游广播 barrier,并将自己的状态(异步)写入到持久化存储中。这个 barrier 就是实现 Chandy-Lamport 分布式快照算法的核心,下游的 task 只有收到所有 input 的 barrier 才会执行相应的 Checkpoint。

img

c. 第三步,当 task 完成 state 备份后,会将备份数据的地址(state handle)通知给 Checkpoint coordinator。

img

d. 第四步,下游的 sink 节点收集齐上游两个 input 的 barrier 之后,会执行本地快照,这里特地展示了 RocksDB incremental Checkpoint 的流程,首先 RocksDB 会全量刷数据到磁盘上(红色大三角表示),然后 Flink 框架会从中选择没有上传的文件进行持久化备份(紫色小三角)。

img

e. 同样的,sink 节点在完成自己的 Checkpoint 之后,会将 state handle 返回通知 Coordinator。

img

f. 最后,当 Checkpoint coordinator 收集齐所有 task 的 state handle,就认为这一次的 Checkpoint 全局完成了,向持久化存储中再备份一个 Checkpoint meta 文件。

img

Checkpoint 的 EXACTLY_ONCE 语义

为了实现 EXACTLY ONCE 语义,Flink 通过一个 input buffer 将在对齐阶段收到的数据缓存起来,等对齐完成之后再进行处理。而对于 AT LEAST ONCE 语义,无需缓存收集到的数据,会对后续直接处理,所以导致 restore 时,数据可能会被多次处理。下图是官网文档里面就 Checkpoint align 的示意图:

https://giphy.com/channel/5zhq8p
https://giphy.com/channel/um10f0
https://giphy.com/channel/9ctouk
https://giphy.com/channel/gfe7n9
https://giphy.com/channel/yx6x9d
https://giphy.com/channel/lo886c
https://giphy.com/channel/zg874z
https://giphy.com/channel/2h17a1
https://giphy.com/channel/rt965y
https://giphy.com/channel/wngvq1
https://giphy.com/channel/5n309v
https://giphy.com/channel/85457r
https://giphy.com/channel/ttj3jz
https://giphy.com/channel/nocomm
https://giphy.com/channel/2ldq31
https://giphy.com/channel/ghop67
https://giphy.com/channel/263t20
https://giphy.com/channel/hri46x
https://giphy.com/channel/g3yehf
https://giphy.com/channel/ssabmc
https://giphy.com/channel/g3860g
https://giphy.com/channel/9d9ebv
https://giphy.com/channel/sbmt4u
https://giphy.com/channel/b2jh16
https://giphy.com/channel/9y7j8z
https://giphy.com/channel/9u636e
https://giphy.com/channel/qpmf5e
https://giphy.com/channel/979zoo
https://giphy.com/channel/ama3s5
https://giphy.com/channel/owe372
https://giphy.com/channel/23d8m8
https://giphy.com/channel/3femu6
https://giphy.com/channel/0de2yn
https://giphy.com/channel/p8eno4
https://giphy.com/channel/2ua5ta
https://giphy.com/channel/13rld8
https://giphy.com/channel/r98l2t
https://giphy.com/channel/9j0ygt
https://giphy.com/channel/5y4j38
https://giphy.com/channel/wyx4xf
https://giphy.com/channel/izo7jo
https://giphy.com/channel/pqig75
https://giphy.com/channel/2rw8ro
https://giphy.com/channel/541b0j
https://giphy.com/channel/ezq86n
https://giphy.com/channel/hrrryy
https://giphy.com/channel/rp52a4
https://giphy.com/channel/g2pvgf
https://giphy.com/channel/ijuk3l
https://giphy.com/channel/bldk6s
https://giphy.com/channel/tcds67
https://giphy.com/channel/3muu7e
https://giphy.com/channel/bdmask
https://giphy.com/channel/eff3p7
https://giphy.com/channel/tv60j9
https://giphy.com/channel/1odoe3
https://giphy.com/channel/qf5aqx
https://giphy.com/channel/zz6mdm
https://giphy.com/channel/ddc4ck
https://giphy.com/channel/78fgzh
https://giphy.com/channel/h51z9y
https://giphy.com/channel/ld5vwt
https://giphy.com/channel/06wvhz
https://giphy.com/channel/1bjv5b
https://giphy.com/channel/9g0zjf
https://giphy.com/channel/2rab7g
https://giphy.com/channel/e8gdq8
https://giphy.com/channel/51hx78
https://giphy.com/channel/z5gxn3
https://giphy.com/channel/esck7b
https://giphy.com/channel/ulkvm7
https://giphy.com/channel/vol3dc
https://giphy.com/channel/swo42z
https://giphy.com/channel/hoxa6x
https://giphy.com/channel/mmgfld
https://giphy.com/channel/921efp
https://giphy.com/channel/atda1a
https://giphy.com/channel/9hwzrh
https://giphy.com/channel/9birk1
https://giphy.com/channel/llkef4
https://giphy.com/channel/9u5u0n
https://giphy.com/channel/jknmsn
https://giphy.com/channel/ne9f0p
https://giphy.com/channel/4o5ufb
https://giphy.com/channel/3iigqh
https://giphy.com/channel/ha85i2
https://giphy.com/channel/247o15
https://giphy.com/channel/601qi9
https://giphy.com/channel/1dsv0j
https://giphy.com/channel/vwdgfx
https://giphy.com/channel/3mgvoq
https://giphy.com/channel/n5ovmv
https://giphy.com/channel/8a3f8i
https://giphy.com/channel/57llfw
https://giphy.com/channel/0ja04y
https://giphy.com/channel/iozf77
https://giphy.com/channel/p64x5x
https://giphy.com/channel/m4px9p
https://giphy.com/channel/zlssi3
https://giphy.com/channel/ff6yis
https://giphy.com/channel/r1bri0
https://giphy.com/channel/15gfdu
https://giphy.com/channel/t1bb0z
https://giphy.com/channel/phtg30
https://giphy.com/channel/bqqsy3
https://giphy.com/channel/3xudet
https://giphy.com/channel/6jw771
https://giphy.com/channel/nv8uvn
https://giphy.com/channel/d3o9b8
https://giphy.com/channel/297y4x
https://giphy.com/channel/z8piv7
https://giphy.com/channel/7yyw8e
https://giphy.com/channel/21tu0v
https://giphy.com/channel/z2rzqj
https://giphy.com/channel/1arcmt
https://giphy.com/channel/f5znyh
https://giphy.com/channel/g8od1o
https://giphy.com/channel/l2lfon
https://giphy.com/channel/a1n337
https://giphy.com/channel/e04uny
https://giphy.com/channel/woemdm
https://giphy.com/channel/9xxgi6
https://giphy.com/channel/sjr1dq
https://giphy.com/channel/xx5oep
https://giphy.com/channel/cxt6ou
https://giphy.com/channel/s9kcff
https://giphy.com/channel/694nzv
https://giphy.com/channel/3hzviw
https://giphy.com/channel/91x3ni
https://giphy.com/channel/ga8rzj
https://giphy.com/channel/xe0qqq
https://giphy.com/channel/zzax4i
https://giphy.com/channel/gd51h6
https://giphy.com/channel/73aga4
https://giphy.com/channel/m980bk
https://giphy.com/channel/70cuha
https://giphy.com/channel/irpyzj
https://giphy.com/channel/8hb354
https://giphy.com/channel/je5r3r
https://giphy.com/channel/7unoft
https://giphy.com/channel/3h2yb3
https://giphy.com/channel/b1tpjr
https://giphy.com/channel/vundle
https://giphy.com/channel/vwb6lu
https://giphy.com/channel/a9s7kc
https://giphy.com/channel/vu71un
https://giphy.com/channel/mzm9rt
https://giphy.com/channel/11i0oi
https://giphy.com/channel/ta95rh
https://giphy.com/channel/u1lr7k
https://giphy.com/channel/sdu3du
https://giphy.com/channel/avtda8
https://giphy.com/channel/y3sf1j
https://giphy.com/channel/jy424x
https://giphy.com/channel/d4dtku
https://giphy.com/channel/438xxq
https://giphy.com/channel/fzyoaa
https://giphy.com/channel/yf1h88
https://giphy.com/channel/eg56z2
https://giphy.com/channel/amursb
https://giphy.com/channel/a8r992
https://giphy.com/channel/8wmls0
https://giphy.com/channel/2gwiex
https://giphy.com/channel/29xv2h
https://giphy.com/channel/ibyqyt
https://giphy.com/channel/ktvjv2
https://giphy.com/channel/0fn4oi
https://giphy.com/channel/281j88
https://giphy.com/channel/g307y7
https://giphy.com/channel/zhxn1r
https://giphy.com/channel/zhxn1r
https://giphy.com/channel/7b192k
https://giphy.com/channel/yfxhxn
https://giphy.com/channel/w2ff66
https://giphy.com/channel/w2ff66
https://giphy.com/channel/d0gw4x
https://giphy.com/channel/g52104
https://giphy.com/channel/n9o28u
https://giphy.com/channel/0bvbuo

需要特别注意的是,Flink 的 Checkpoint 机制只能保证 Flink 的计算过程可以做到 EXACTLY ONCE,端到端的 EXACTLY ONCE 需要 source 和 sink 支持。

Savepoint 与 Checkpoint 的区别
作业恢复时,二者均可以使用,主要区别如下:

img

你可能感兴趣的:(Flink Checkpoint-轻量级分布式快照)