[{"data":1,"prerenderedAt":573},["ShallowReactive",2],{"post-\u002Fblog\u002F2022-09-25-flink-memory-model":3,"surround-\u002Fblog\u002F2022-09-25-flink-memory-model":566},{"_path":4,"_dir":5,"_draft":6,"_partial":6,"_locale":7,"title":8,"description":9,"date":10,"tags":11,"cover":16,"source":17,"body":18,"_type":560,"_id":561,"_source":562,"_file":563,"_stem":564,"_extension":565},"\u002Fblog\u002F2022-09-25-flink-memory-model","blog",false,"","Flink内存模型的思考","从线上 Flink 任务延时告警出发，定位问题并深入分析 Flink 内存模型，包括内存划分、各模块作用及内存计算公式。","2022-09-25",[12,13,14,15],"简书","历史归档","Flink","大数据","https:\u002F\u002Fupload-images.jianshu.io\u002Fupload_images\u002F16281566-b69c692e653feeb3.jpg","https:\u002F\u002Fwww.jianshu.com\u002Fp\u002Fab4e81986ad9",{"type":19,"children":20,"toc":547},"root",[21,29,38,43,48,53,109,118,125,130,137,142,147,154,159,193,198,205,218,226,231,236,264,270,305,312,317,324,330,372,377,384,439,445,468,473,480,485,492,503,508,513,518,541],{"type":22,"tag":23,"props":24,"children":26},"element","h2",{"id":25},"背景",[27],{"type":28,"value":25},"text",{"type":22,"tag":30,"props":31,"children":32},"p",{},[33],{"type":22,"tag":34,"props":35,"children":37},"img",{"alt":7,"src":36},"https:\u002F\u002Fupload-images.jianshu.io\u002Fupload_images\u002F16281566-b69c692e653feeb3.jpg?imageMogr2\u002Fauto-orient\u002Fstrip%7CimageView2\u002F2\u002Fw\u002F853\u002Fformat\u002Fwebp",[],{"type":22,"tag":30,"props":39,"children":40},{},[41],{"type":28,"value":42},"从2022-08-07晚上开始该任务触发延时告警，此处省略几十条。",{"type":22,"tag":23,"props":44,"children":46},{"id":45},"定位问题",[47],{"type":28,"value":45},{"type":22,"tag":30,"props":49,"children":50},{},[51],{"type":28,"value":52},"该线上Flink提交参数如下，其余参数均是1.0版本的默认值",{"type":22,"tag":54,"props":55,"children":59},"pre",{"className":56,"code":57,"language":58,"meta":7,"style":7},"language-bash shiki shiki-themes github-light github-dark","-D state.backend.incremental=true\n--taskManagerMemory=10240\n","bash",[60],{"type":22,"tag":61,"props":62,"children":63},"code",{"__ignoreMap":7},[64,88],{"type":22,"tag":65,"props":66,"children":69},"span",{"class":67,"line":68},"line",1,[70,76,82],{"type":22,"tag":65,"props":71,"children":73},{"style":72},"--shiki-default:#6F42C1;--shiki-dark:#B392F0",[74],{"type":28,"value":75},"-D",{"type":22,"tag":65,"props":77,"children":79},{"style":78},"--shiki-default:#032F62;--shiki-dark:#9ECBFF",[80],{"type":28,"value":81}," state.backend.incremental=",{"type":22,"tag":65,"props":83,"children":85},{"style":84},"--shiki-default:#005CC5;--shiki-dark:#79B8FF",[86],{"type":28,"value":87},"true\n",{"type":22,"tag":65,"props":89,"children":91},{"class":67,"line":90},2,[92,98,104],{"type":22,"tag":65,"props":93,"children":95},{"style":94},"--shiki-default:#24292E;--shiki-dark:#E1E4E8",[96],{"type":28,"value":97},"--taskManagerMemory",{"type":22,"tag":65,"props":99,"children":101},{"style":100},"--shiki-default:#D73A49;--shiki-dark:#F97583",[102],{"type":28,"value":103},"=",{"type":22,"tag":65,"props":105,"children":106},{"style":78},[107],{"type":28,"value":108},"10240\n",{"type":22,"tag":110,"props":111,"children":112},"blockquote",{},[113],{"type":22,"tag":30,"props":114,"children":115},{},[116],{"type":28,"value":117},"--taskManagerMemory=10240 等价于 taskmanager.memory.process.size=10gb",{"type":22,"tag":30,"props":119,"children":120},{},[121],{"type":22,"tag":34,"props":122,"children":124},{"alt":7,"src":123},"https:\u002F\u002Fupload-images.jianshu.io\u002Fupload_images\u002F16281566-fc708e5eb1159dcb.jpg?imageMogr2\u002Fauto-orient\u002Fstrip%7CimageView2\u002F2\u002Fw\u002F1200\u002Fformat\u002Fwebp",[],{"type":22,"tag":30,"props":126,"children":127},{},[128],{"type":28,"value":129},"从上图能够看出数据流入变的特别小(kafka topic流入数据一直是稳定的10MB\u002Fs)，Flink任务数据延时一直是上升趋势，那么一定是程序遇到了性能瓶颈。",{"type":22,"tag":30,"props":131,"children":132},{},[133],{"type":22,"tag":34,"props":134,"children":136},{"alt":7,"src":135},"https:\u002F\u002Fupload-images.jianshu.io\u002Fupload_images\u002F16281566-08218773b292b539.jpg?imageMogr2\u002Fauto-orient\u002Fstrip%7CimageView2\u002F2\u002Fw\u002F1200\u002Fformat\u002Fwebp",[],{"type":22,"tag":30,"props":138,"children":139},{},[140],{"type":28,"value":141},"从上图能够看到源kafka topic 每个分区的消息滞后几十万条",{"type":22,"tag":30,"props":143,"children":144},{},[145],{"type":28,"value":146},"从grafana监控并不能定位Flink程序哪个调用栈出现了性能瓶颈，打个火焰图看看，如下图：",{"type":22,"tag":30,"props":148,"children":149},{},[150],{"type":22,"tag":34,"props":151,"children":153},{"alt":7,"src":152},"https:\u002F\u002Fupload-images.jianshu.io\u002Fupload_images\u002F16281566-c59e20adfd274816.jpg?imageMogr2\u002Fauto-orient\u002Fstrip%7CimageView2\u002F2\u002Fw\u002F1200\u002Fformat\u002Fwebp",[],{"type":22,"tag":30,"props":155,"children":156},{},[157],{"type":28,"value":158},"从上面的火焰图可以分析该程序大部分时间都是 rocksdb 在进行写数据与压缩数据，由于subtask子任务较多也占用了很多网络资源进行shuffle，由此得出结论rocksdb内存不够导致磁盘的IO增多，网络传输也是一个性能瓶颈点，我们如何在不增加总体内存的背景下增加rocksdb内存以及网络内存?通过以下配置参数调整内存比例：",{"type":22,"tag":54,"props":160,"children":164},{"className":161,"code":162,"language":163,"meta":7,"style":7},"language-csharp shiki shiki-themes github-light github-dark","-D state.backend.rocksdb.memory.fixed-per-slot=1gb\n-D taskmanager.memory.network.max=2gb\n-D taskmanager.memory.network.fraction=0.2\n","csharp",[165],{"type":22,"tag":61,"props":166,"children":167},{"__ignoreMap":7},[168,176,184],{"type":22,"tag":65,"props":169,"children":170},{"class":67,"line":68},[171],{"type":22,"tag":65,"props":172,"children":173},{},[174],{"type":28,"value":175},"-D state.backend.rocksdb.memory.fixed-per-slot=1gb\n",{"type":22,"tag":65,"props":177,"children":178},{"class":67,"line":90},[179],{"type":22,"tag":65,"props":180,"children":181},{},[182],{"type":28,"value":183},"-D taskmanager.memory.network.max=2gb\n",{"type":22,"tag":65,"props":185,"children":187},{"class":67,"line":186},3,[188],{"type":22,"tag":65,"props":189,"children":190},{},[191],{"type":28,"value":192},"-D taskmanager.memory.network.fraction=0.2\n",{"type":22,"tag":30,"props":194,"children":195},{},[196],{"type":28,"value":197},"运行一段时间后发现该任务还是会有数据延迟，继续定位问题发现该任务的主要瓶颈在于window算子的数据倾斜，如下图所示：",{"type":22,"tag":30,"props":199,"children":200},{},[201],{"type":22,"tag":34,"props":202,"children":204},{"alt":7,"src":203},"https:\u002F\u002Fupload-images.jianshu.io\u002Fupload_images\u002F16281566-d25df7f94dda9731.jpg?imageMogr2\u002Fauto-orient\u002Fstrip%7CimageView2\u002F2\u002Fw\u002F1190\u002Fformat\u002Fwebp",[],{"type":22,"tag":30,"props":206,"children":207},{},[208,210,216],{"type":28,"value":209},"数据倾斜问题一直是分布式计算的难点，如果不处理的话一味的 ",{"type":22,"tag":61,"props":211,"children":213},{"className":212},[],[214],{"type":28,"value":215},"加资源",{"type":28,"value":217}," 起不到任何的效果，反而是对资源的浪费。",{"type":22,"tag":110,"props":219,"children":220},{},[221],{"type":22,"tag":30,"props":222,"children":223},{},[224],{"type":28,"value":225},"数据倾斜由于不是本篇文章分享的重点，如何解决数据倾斜放在下一篇文章分享。",{"type":22,"tag":23,"props":227,"children":229},{"id":228},"疑问",[230],{"type":28,"value":228},{"type":22,"tag":30,"props":232,"children":233},{},[234],{"type":28,"value":235},"观测线上Flink 1.10任务带来的思考：",{"type":22,"tag":237,"props":238,"children":239},"ol",{},[240,254,259],{"type":22,"tag":241,"props":242,"children":243},"li",{},[244,246,252],{"type":28,"value":245},"通过 ",{"type":22,"tag":61,"props":247,"children":249},{"className":248},[],[250],{"type":28,"value":251},"taskmanager.memory.process.size=10gb",{"type":28,"value":253}," 该参数设置Container内存为10GB，通过Web UI监控发现tm内存远远达不到10GB到底其他的内存哪里去了呢？",{"type":22,"tag":241,"props":255,"children":256},{},[257],{"type":28,"value":258},"Flink的内存是如何划分的呢？",{"type":22,"tag":241,"props":260,"children":261},{},[262],{"type":28,"value":263},"Flink每一块内存到底有什么作用呢？",{"type":22,"tag":23,"props":265,"children":267},{"id":266},"flink内存模型",[268],{"type":28,"value":269},"Flink内存模型",{"type":22,"tag":30,"props":271,"children":272},{},[273,275,281,283,288,290,296,298,303],{"type":28,"value":274},"要想深入了解上面几个问题不得不从Flink的内存模型入手，首先得提一下Flink框架源码所使用的开发语言 ",{"type":22,"tag":61,"props":276,"children":278},{"className":277},[],[279],{"type":28,"value":280},"Java",{"type":28,"value":282}," ，说到 ",{"type":22,"tag":61,"props":284,"children":286},{"className":285},[],[287],{"type":28,"value":280},{"type":28,"value":289}," 首先会联想到 ",{"type":22,"tag":61,"props":291,"children":293},{"className":292},[],[294],{"type":28,"value":295},"JVM",{"type":28,"value":297}," ，相比于 ",{"type":22,"tag":61,"props":299,"children":301},{"className":300},[],[302],{"type":28,"value":295},{"type":28,"value":304}," 内存控制Flink框架对于内存的管理更加的丰富，并且让开发者更加精细化的控制Flink程序的每个模块内存，首先我们来看下官放给出的Flink内存模型如下图：",{"type":22,"tag":30,"props":306,"children":307},{},[308],{"type":22,"tag":34,"props":309,"children":311},{"alt":7,"src":310},"https:\u002F\u002Fupload-images.jianshu.io\u002Fupload_images\u002F16281566-f0c11b2bb4e27fb2.png?imageMogr2\u002Fauto-orient\u002Fstrip%7CimageView2\u002F2\u002Fw\u002F588\u002Fformat\u002Fwebp",[],{"type":22,"tag":30,"props":313,"children":314},{},[315],{"type":28,"value":316},"Flink内存主要分为两部分：container内存、flink内存，其中内存类型和Spark内存类型一样分为堆内(heap)和堆外(off-heap)，各模块内存使用场景及配置参数说明如下：",{"type":22,"tag":30,"props":318,"children":319},{},[320],{"type":22,"tag":34,"props":321,"children":323},{"alt":7,"src":322},"https:\u002F\u002Fupload-images.jianshu.io\u002Fupload_images\u002F16281566-37fd5b12f1bf021e.jpg?imageMogr2\u002Fauto-orient\u002Fstrip%7CimageView2\u002F2\u002Fw\u002F1067\u002Fformat\u002Fwebp",[],{"type":22,"tag":23,"props":325,"children":327},{"id":326},"内存计算公式如下",[328],{"type":28,"value":329},"内存计算公式如下：",{"type":22,"tag":331,"props":332,"children":333},"ul",{},[334,339,352,357,362,367],{"type":22,"tag":241,"props":335,"children":336},{},[337],{"type":28,"value":338},"JVM Metaspace Memory：默认256MB",{"type":22,"tag":241,"props":340,"children":341},{},[342,344,350],{"type":28,"value":343},"JVM Overhead Memory：默认整个container内存的0.1\n实际计算：1024M ",{"type":22,"tag":345,"props":346,"children":347},"em",{},[348],{"type":28,"value":349},"0.1 \u003C 192 M? 192 M: (1024M",{"type":28,"value":351}," 0.1 > 1Gb? 1Gb: 1024M * 0.1) = 192 M\n因此默认内存为192MB",{"type":22,"tag":241,"props":353,"children":354},{},[355],{"type":28,"value":356},"Flink Memory：Container Memory - JVM Metaspace - JVM Overhead",{"type":22,"tag":241,"props":358,"children":359},{},[360],{"type":28,"value":361},"Network Memory： Flink Memory * 0.1",{"type":22,"tag":241,"props":363,"children":364},{},[365],{"type":28,"value":366},"Manager Memory：Flink Memory * 0.4",{"type":22,"tag":241,"props":368,"children":369},{},[370],{"type":28,"value":371},"堆内内存：Flink Memory - Network Memory - Manager Memory",{"type":22,"tag":23,"props":373,"children":375},{"id":374},"示例",[376],{"type":28,"value":374},{"type":22,"tag":378,"props":379,"children":381},"h3",{"id":380},"线上-lbs-任务配置如下",[382],{"type":28,"value":383},"线上 LBS 任务配置如下：",{"type":22,"tag":54,"props":385,"children":387},{"className":161,"code":386,"language":163,"meta":7,"style":7},"-D state.backend.rocksdb.memory.fixed-per-slot=1gb\n-D state.backend.incremental=true\n--taskManagerMemory=10240\n-D taskmanager.memory.network.max=2gb\n-D taskmanager.memory.network.fraction=0.2\n-D taskmanager.numberOfTaskSlots=2\n",[388],{"type":22,"tag":61,"props":389,"children":390},{"__ignoreMap":7},[391,398,406,414,422,430],{"type":22,"tag":65,"props":392,"children":393},{"class":67,"line":68},[394],{"type":22,"tag":65,"props":395,"children":396},{},[397],{"type":28,"value":175},{"type":22,"tag":65,"props":399,"children":400},{"class":67,"line":90},[401],{"type":22,"tag":65,"props":402,"children":403},{},[404],{"type":28,"value":405},"-D state.backend.incremental=true\n",{"type":22,"tag":65,"props":407,"children":408},{"class":67,"line":186},[409],{"type":22,"tag":65,"props":410,"children":411},{},[412],{"type":28,"value":413},"--taskManagerMemory=10240\n",{"type":22,"tag":65,"props":415,"children":417},{"class":67,"line":416},4,[418],{"type":22,"tag":65,"props":419,"children":420},{},[421],{"type":28,"value":183},{"type":22,"tag":65,"props":423,"children":425},{"class":67,"line":424},5,[426],{"type":22,"tag":65,"props":427,"children":428},{},[429],{"type":28,"value":192},{"type":22,"tag":65,"props":431,"children":433},{"class":67,"line":432},6,[434],{"type":22,"tag":65,"props":435,"children":436},{},[437],{"type":28,"value":438},"-D taskmanager.numberOfTaskSlots=2\n",{"type":22,"tag":378,"props":440,"children":442},{"id":441},"实际内存计算如下",[443],{"type":28,"value":444},"实际内存计算如下：",{"type":22,"tag":237,"props":446,"children":447},{},[448,453,458,463],{"type":22,"tag":241,"props":449,"children":450},{},[451],{"type":28,"value":452},"Flink Memory = 10240 - 256 - 1024 = 8960MB",{"type":22,"tag":241,"props":454,"children":455},{},[456],{"type":28,"value":457},"Network Memory = 8960 * 0.2 = 1792MB",{"type":22,"tag":241,"props":459,"children":460},{},[461],{"type":28,"value":462},"Manager Memory = 8960 * 0.4 = 3584MB",{"type":22,"tag":241,"props":464,"children":465},{},[466],{"type":28,"value":467},"Task Heap Memory = 8960 - 1792 - 3584 - 128 - 128 = 3328MB\u002F3.25GB",{"type":22,"tag":30,"props":469,"children":470},{},[471],{"type":28,"value":472},"每个模块内存如下图：",{"type":22,"tag":30,"props":474,"children":475},{},[476],{"type":22,"tag":34,"props":477,"children":479},{"alt":7,"src":478},"https:\u002F\u002Fupload-images.jianshu.io\u002Fupload_images\u002F16281566-deb308dee4edf7b3.jpg?imageMogr2\u002Fauto-orient\u002Fstrip%7CimageView2\u002F2\u002Fw\u002F1061\u002Fformat\u002Fwebp",[],{"type":22,"tag":30,"props":481,"children":482},{},[483],{"type":28,"value":484},"Flink Web UI如下图：",{"type":22,"tag":30,"props":486,"children":487},{},[488],{"type":22,"tag":34,"props":489,"children":491},{"alt":7,"src":490},"https:\u002F\u002Fupload-images.jianshu.io\u002Fupload_images\u002F16281566-fda21dbb73c20a3a.jpg?imageMogr2\u002Fauto-orient\u002Fstrip%7CimageView2\u002F2\u002Fw\u002F1200\u002Fformat\u002Fwebp",[],{"type":22,"tag":30,"props":493,"children":494},{},[495,501],{"type":22,"tag":61,"props":496,"children":498},{"className":497},[],[499],{"type":28,"value":500},"JVM Heap Memory",{"type":28,"value":502}," 实际分配比公式计算出来的结果要多10MB左右，大差不差的能够计算出Flink内存的分布方便开发者根据自身任务的情况调整每一个模块的内存比例以及总体内存。至于差距的这部分内存我也会继续找原因，有知道的大佬评论区可以告诉我哪里计算的有问题。",{"type":22,"tag":23,"props":504,"children":506},{"id":505},"成果",[507],{"type":28,"value":505},{"type":22,"tag":30,"props":509,"children":510},{},[511],{"type":28,"value":512},"该任务优化后一直处于较为稳定的状态，再没有收到过延时告警",{"type":22,"tag":23,"props":514,"children":516},{"id":515},"总结",[517],{"type":28,"value":515},{"type":22,"tag":237,"props":519,"children":520},{},[521,526,531,536],{"type":22,"tag":241,"props":522,"children":523},{},[524],{"type":28,"value":525},"该篇文章能够让我们清楚线上Flink问题的定位流程是什么样的",{"type":22,"tag":241,"props":527,"children":528},{},[529],{"type":28,"value":530},"基于Flink内存模型能够清楚Flink每一块内存的分配",{"type":22,"tag":241,"props":532,"children":533},{},[534],{"type":28,"value":535},"Flink每一块内存的作用。",{"type":22,"tag":241,"props":537,"children":538},{},[539],{"type":28,"value":540},"加资源并不能解决所有任务的性能问题",{"type":22,"tag":542,"props":543,"children":544},"style",{},[545],{"type":28,"value":546},"html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}",{"title":7,"searchDepth":186,"depth":186,"links":548},[549,550,551,552,553,554,558,559],{"id":25,"depth":90,"text":25},{"id":45,"depth":90,"text":45},{"id":228,"depth":90,"text":228},{"id":266,"depth":90,"text":269},{"id":326,"depth":90,"text":329},{"id":374,"depth":90,"text":374,"children":555},[556,557],{"id":380,"depth":186,"text":383},{"id":441,"depth":186,"text":444},{"id":505,"depth":90,"text":505},{"id":515,"depth":90,"text":515},"markdown","content:blog:2022-09-25-flink-memory-model.md","content","blog\u002F2022-09-25-flink-memory-model.md","blog\u002F2022-09-25-flink-memory-model","md",[567,570],{"_path":568,"title":569},"\u002Fblog\u002F2022-09-26-flink-memory-settings","Flink内存设置思考",{"_path":571,"title":572},"\u002Fblog\u002F2020-05-12-canal-kafka-mysql","Canal + Kafka 实现 MySQL 的 binlog 近实时同步",1796523532993]