flink狀態(tài)容錯(cuò)

什么是State(狀態(tài))?

  • 某task/operator在某時(shí)刻的一個(gè)中間結(jié)果
  • 快照(shapshot)
  • 在flink中狀態(tài)可以理解為一種數(shù)據(jù)結(jié)構(gòu)
  • 舉例
    對(duì)輸入源為<key,value>的數(shù)據(jù),計(jì)算其中某key的最大值主卫,如果使用HashMap逃默,也可以進(jìn)行計(jì)算,但是每次都需要重新遍歷簇搅,使用狀態(tài)的話完域,可以獲取最近的一次計(jì)算結(jié)果,減少了系統(tǒng)的計(jì)算次數(shù)
  • 程序一旦crash瘩将,恢復(fù)
  • 程序擴(kuò)容

State類型

Operator State(算子狀態(tài))


With Operator State (or non-keyed state), each operator state is bound to one parallel operator instance. The Kafka Connector is a good motivating example for the use of Operator State in Flink. Each parallel instance of the Kafka consumer maintains a map of topic partitions and offsets as its Operator State.
The Operator State interfaces support redistributing state among parallel operator instances when the parallelism is changed. There can be different schemes for doing this redistribution.

kafka示例

flink官方文檔用kafka的消費(fèi)者舉例吟税,認(rèn)為kafka消費(fèi)者的partitionId和offset類似flink的operator state

提供的數(shù)據(jù)結(jié)構(gòu):ListState<T>
每一個(gè)Operator都存在自己的狀態(tài)

key State


Keyed State is always relative to keys and can only be used in functions and operators on a KeyedStream.
You can think of Keyed State as Operator State that has been partitioned, or sharded, with exactly one state-partition per key. Each keyed-state is logically bound to a unique composite of <parallel-operator-instance, key>, and since each key “belongs” to exactly one parallel instance of a keyed operator, we can think of this simply as <operator, key>.
Keyed State is further organized into so-called Key Groups. Key Groups are the atomic unit by which Flink can redistribute Keyed State; there are exactly as many Key Groups as the defined maximum parallelism. During execution each parallel instance of a keyed operator works with the keys for one or more Key Groups.

基于KeyStream之上的狀態(tài)
可理解為dataStream.keyBy()之后的Operator State,Operator State是對(duì)每一個(gè)Operator的狀態(tài)進(jìn)行記錄,而key State則是在dataSteam進(jìn)行keyBy()后姿现,記錄相同keyId的keyStream上的狀態(tài)
key State提供的數(shù)據(jù)類型:ValueState<T>肠仪、ListState<T>、ReducingState<T>备典、MapState<T>

狀態(tài)容錯(cuò)

  • Introduction
    Apache Flink offers a fault tolerance mechanism to consistently recover the state of data streaming applications. The mechanism ensures that even in the presence of failures, the program’s state will eventually reflect every record from the data stream exactly once. Note that there is a switch to downgrade the guarantees to at least once (described below).
    The fault tolerance mechanism continuously draws snapshots of the distributed streaming data flow. For streaming applications with small state, these snapshots are very light-weight and can be drawn frequently without impacting the performance much. The state of the streaming applications is stored at a configurable place (such as the master node, or HDFS).
    In case of a program failure (due to machine-, network-, or software failure), Flink stops the distributed streaming dataflow. The system then restarts the operators and resets them to the latest successful checkpoint. The input streams are reset to the point of the state snapshot. Any records that are processed as part of the restarted parallel dataflow are guaranteed to not have been part of the checkpointed state before.
    Note: For this mechanism to realize its full guarantees, the data stream source (such as message queue or broker) needs to be able to rewind the stream to a defined recent point. Apache Kafka has this ability and Flink’s connector to Kafka exploits this ability.
    Note: Because Flink’s checkpoints are realized through distributed snapshots, we use the words snapshot and checkpointinterchangeably.
依靠checkPoint

checkPoint概念:進(jìn)行全局快照异旧,持久化保存所有的task/operator的State

  • 特點(diǎn):
    異步:輕量級(jí),不影響系統(tǒng)處理數(shù)據(jù)
    Barrier機(jī)制
    失敗情況下可回滾致最近一次成功的checkpoint
    周期性
  • 保證exactly-once


    chcekPoint

    Restore
shapshot(快照)
  • Barriers(屏障)
    Barriers是flink分布式快照中的重要元素
    單并行度Barriers

    多并行度Barriers

    Barrier被注入數(shù)據(jù)流中提佣,并隨著數(shù)據(jù)流和記錄一起流動(dòng)吮蛹,每一個(gè)Barrier攜帶者快照ID,并且十分輕量級(jí)拌屏,不會(huì)打斷數(shù)據(jù)的流動(dòng)潮针,不同時(shí)期的快照的barrier可以同時(shí)存在數(shù)據(jù)流中,所以各種快照可以同時(shí)發(fā)生倚喂。
    相對(duì)于單并行度每篷,多并行度的快照需要不同數(shù)據(jù)流中攜帶相同快照ID的Barrier經(jīng)過operator之后,才能進(jìn)行checkpoint
image.png

個(gè)人理解:感覺對(duì)于Flink的狀態(tài)遷移和容錯(cuò)來說雳攘,主要依賴checkpoint機(jī)制带兜,而其中最重要的元素就是Barrier,通過Barrier保證流入Operator的數(shù)據(jù)都進(jìn)行了checkpoint

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末吨灭,一起剝皮案震驚了整個(gè)濱河市刚照,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌喧兄,老刑警劉巖无畔,帶你破解...
    沈念sama閱讀 212,884評(píng)論 6 492
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異吠冤,居然都是意外死亡浑彰,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,755評(píng)論 3 385
  • 文/潘曉璐 我一進(jìn)店門拯辙,熙熙樓的掌柜王于貴愁眉苦臉地迎上來郭变,“玉大人,你說我怎么就攤上這事涯保∷弑簦” “怎么了?”我有些...
    開封第一講書人閱讀 158,369評(píng)論 0 348
  • 文/不壞的土叔 我叫張陵夕春,是天一觀的道長未荒。 經(jīng)常有香客問我,道長及志,這世上最難降的妖魔是什么片排? 我笑而不...
    開封第一講書人閱讀 56,799評(píng)論 1 285
  • 正文 為了忘掉前任,我火速辦了婚禮速侈,結(jié)果婚禮上率寡,老公的妹妹穿的比我還像新娘。我一直安慰自己倚搬,他們只是感情好冶共,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,910評(píng)論 6 386
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著潭枣,像睡著了一般。 火紅的嫁衣襯著肌膚如雪幻捏。 梳的紋絲不亂的頭發(fā)上盆犁,一...
    開封第一講書人閱讀 50,096評(píng)論 1 291
  • 那天,我揣著相機(jī)與錄音篡九,去河邊找鬼谐岁。 笑死,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的伊佃。 我是一名探鬼主播窜司,決...
    沈念sama閱讀 39,159評(píng)論 3 411
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢(mèng)啊……” “哼航揉!你這毒婦竟也來了塞祈?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 37,917評(píng)論 0 268
  • 序言:老撾萬榮一對(duì)情侶失蹤帅涂,失蹤者是張志新(化名)和其女友劉穎议薪,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體媳友,經(jīng)...
    沈念sama閱讀 44,360評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡斯议,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,673評(píng)論 2 327
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了醇锚。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片哼御。...
    茶點(diǎn)故事閱讀 38,814評(píng)論 1 341
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖焊唬,靈堂內(nèi)的尸體忽然破棺而出恋昼,到底是詐尸還是另有隱情,我是刑警寧澤求晶,帶...
    沈念sama閱讀 34,509評(píng)論 4 334
  • 正文 年R本政府宣布焰雕,位于F島的核電站,受9級(jí)特大地震影響芳杏,放射性物質(zhì)發(fā)生泄漏矩屁。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 40,156評(píng)論 3 317
  • 文/蒙蒙 一爵赵、第九天 我趴在偏房一處隱蔽的房頂上張望吝秕。 院中可真熱鬧,春花似錦空幻、人聲如沸烁峭。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,882評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽约郁。三九已至,卻和暖如春但两,著一層夾襖步出監(jiān)牢的瞬間鬓梅,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,123評(píng)論 1 267
  • 我被黑心中介騙來泰國打工谨湘, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留绽快,地道東北人芥丧。 一個(gè)月前我還...
    沈念sama閱讀 46,641評(píng)論 2 362
  • 正文 我出身青樓,卻偏偏與公主長得像坊罢,于是被迫代替她去往敵國和親续担。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,728評(píng)論 2 351

推薦閱讀更多精彩內(nèi)容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,312評(píng)論 0 10
  • 那一年我們?cè)诟咧行@相遇,那一刻在我心中有了你身影诱鞠。 第一次看見你笑挎挖,很美― 那天是我們高中時(shí)代的重要時(shí)刻,也是我...
    冥天軒閱讀 893評(píng)論 0 0
  • 王維《冬晚對(duì)雪憶胡居士家》原詩航夺、注釋蕉朵、翻譯、賞析 【原文】:冬晚對(duì)雪憶胡居士家王維寒更傳曉箭阳掐,清鏡覽衰顏始衅。隔牖風(fēng)驚...
    xcy無名閱讀 388評(píng)論 0 0
  • 看到了知乎上陳素封的千字文回答,知道有簡書這么一個(gè)平臺(tái)缭保,特意注冊(cè)一個(gè)汛闸。 最主要的目的是鍛煉自己的總結(jié)能力和寫作能力...
    村上春竹閱讀 403評(píng)論 1 3