flink 学习笔记 — 状态 State

回顾

    在之前的学习中我们了解到,flink 作为低延时的流式数据处理框架,本身是有状态的。状态 state 是为了保存一些操作符 operator 的中间结果,同时,通过状态可以保证精确一致语义。

State 分类

    State 从其实现方式可分为:Keyed State 和 Operator State,从管理方式可分为:Raw State 和 Managed State。

Keyed State
  • Keyed State 通常是与 keys 相关的,其函数和操作只能在 KeyedStream 中使用。事实上,keyed 与 hive 中的分区极其类似,每一个 key 只能属于某一个 keyed state。
DataStream<Tuple2<String, Integer>> counts =
            text.flatMap(new Tokenizer())
            .keyBy(0)   // 使用 keyby 方法进行划分,不同的 task 之间不会出现相同的 key
            .sum(1);
keyby.png
  • 如上,有3个并行度的 WordCount 任务,在 keyby 之后,相同的 key 会被划分到相同的 task 中进行处理。
Operator State
  • non-keyed state,每一个 operator state 都仅与一个 operator 的实例绑定。
  • 常见的 operator state 是 source state,例如记录当前 source 的 offset
Managed State

    Managed State 是由 Flink Runtime 中管理的 State ,并将状态数据转换为 hashtable 或者 RocksDB 的对象进行存储。

mysu_bj.png
  • ValueState:与 key 对应单个的值,在我们统计流式数据中的单词个数时,事实上,状态就是以 ValueState 存在,每次在状态值上进行更新。在其内部,调用 update(T value) 方法进行状态值的更新。
public interface ValueState<T> extends State {

    /**
     * Returns the current value for the state. When the state is not
     * partitioned the returned value is the same for all inputs in a given
     * operator instance. If state partitioning is applied, the value returned
     * depends on the current operator input, as the operator maintains an
     * independent state for each partition.
     *
     * <p>If you didn't specify a default value when creating the {@link ValueStateDescriptor}
     * this will return {@code null} when to value was previously set using {@link #update(Object)}.
     *
     * @return The state value corresponding to the current input.
     *
     * @throws IOException Thrown if the system cannot access the state.
     */
    T value() throws IOException;

    /**
     * Updates the operator state accessible by {@link #value()} to the given
     * value. The next time {@link #value()} is called (for the same state
     * partition) the returned state will represent the updated value. When a
     * partitioned state is updated with null, the state for the current key
     * will be removed and the default value is returned on the next access.
     *
     * @param value The new value for the state.
     *
     * @throws IOException Thrown if the system cannot access the state.
     */
    void update(T value) throws IOException;

}
  • ListState:与 key 对应的元素的列表的状态 list,内部定义 update(T)和 addAll(T)两个方法
public interface ListState<T> extends MergingState<T, Iterable<T>> {

    /**
     * Updates the operator state accessible by {@link #get()} by updating existing values to
     * to the given list of values. The next time {@link #get()} is called (for the same state
     * partition) the returned state will represent the updated list.
     *
     * <p>If null or an empty list is passed in, the state value will be null.
     *
     * @param values The new values for the state.
     *
     * @throws Exception The method may forward exception thrown internally (by I/O or functions).
     */
    void update(List<T> values) throws Exception;

    /**
     * Updates the operator state accessible by {@link #get()} by adding the given values
     * to existing list of values. The next time {@link #get()} is called (for the same state
     * partition) the returned state will represent the updated list.
     *
     * <p>If null or an empty list is passed in, the state value remains unchanged.
     *
     * @param values The new values to be added to the state.
     *
     * @throws Exception The method may forward exception thrown internally (by I/O or functions).
     */
    void addAll(List<T> values) throws Exception;
}
  • ReducingState:定义与 key 相关的数据元素单个聚合的状态值。
  • AggregatingState:定义与 key 相关的数据元素单个聚合的状态值。
Raw State

    Raw State 是由算子本身进行管理的 State ,此时状态都是以字节数组的形式保存到 Checkpoint 中,Flink 并不清楚状态数据的内部结构,每次状态的写入和读取都需要算子进行序列化和反序列化。

状态管理

    Flink 中状态管理有三种方案:MemoryStateBackend、FSStateBackend、RocksDBStateBackend。

MemoryStateBackend
  • MemoryStateBackend 基于内存的状态管理器,它通常将状态数据存储在 JVM 内存中,包括 Key/State 及窗口中缓存的数据。但是由于内存本身的限制,基于内存的状态管理会造成内存溢出。因此,这种状态管理机制通常在本地测试中使用,生产中禁止使用内存状态管理器。
 env.setStateBackend(new MemoryStateBackend());
FSStateBackend
  • FSStateBackend 基于文件的状态管理,和内存管理机制不同,FSStateBackend 通常把状态数据保存在本地文件系统,或者HDFS文件系统中。在初始化时,需要传入文件路径。基于文件的状态管理机制,适用于状态数据很大的数据,此时,如果使用内存状态管理器,很容易就把内存撑爆。通常情况下,为了保证文件状态安全性,会把文件状态保存在 HDFS 中,此时,借助 HDFS 的多副本的策略,保证文件状态不丢失。
env.setStateBackend(new FsStateBackend(""));

// 源码
public FsStateBackend(Path checkpointDataUri) {
        this(checkpointDataUri.toUri());
    }

RocksDBStateBackend
  • RocksDBStateBackend 基于内存和文件系统的状态管理器,这是基于三方的状态管理器。通常,先把状态放在内存中,等到到达一定的大小时,会将状态数据刷到文件中。
env.setStateBackend(new RocksDBStateBackend(""));

总结

    状态是 Flink 容错机制的基石,了解 State 的机制,可以更好的管理 Checkpoint,更好的进行失败任务的恢复。

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,902评论 5 468
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,037评论 2 377
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,978评论 0 332
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,867评论 1 272
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,763评论 5 360
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,104评论 1 277
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,565评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,236评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,379评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,313评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,363评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,034评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,637评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,719评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,952评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,371评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,948评论 2 341

推荐阅读更多精彩内容