<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>TINY - HELLO JAVA | SCALA | RUST.</title>
    <description>tiny's blog</description>
    <link>https://itinycheng.github.io/</link>
    <atom:link href="https://itinycheng.github.io/feed.xml" rel="self" type="application/rss+xml"/>
    <pubDate>Sun, 11 Jul 2021 09:22:26 +0000</pubDate>
    <lastBuildDate>Sun, 11 Jul 2021 09:22:26 +0000</lastBuildDate>
    <generator>Jekyll v3.9.0</generator>
    
      <item>
        <title>DorisDB,ClickHouse,TiDB的对比与选型分析</title>
        <description>&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#系统对比&quot; id=&quot;markdown-toc-系统对比&quot;&gt;系统对比&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#实时人群圈选场景需求说明&quot; id=&quot;markdown-toc-实时人群圈选场景需求说明&quot;&gt;实时人群圈选场景需求说明&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#功能选型&quot; id=&quot;markdown-toc-功能选型&quot;&gt;功能选型&lt;/a&gt;    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#tidb&quot; id=&quot;markdown-toc-tidb&quot;&gt;TIDB&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#dorisdb&quot; id=&quot;markdown-toc-dorisdb&quot;&gt;DorisDB&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#clickhouse&quot; id=&quot;markdown-toc-clickhouse&quot;&gt;ClickHouse&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#功能选型-1&quot; id=&quot;markdown-toc-功能选型-1&quot;&gt;功能选型&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#性能测试&quot; id=&quot;markdown-toc-性能测试&quot;&gt;性能测试&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;系统对比&quot;&gt;系统对比&lt;/h2&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt; &lt;/th&gt;
      &lt;th&gt;DorisDB&lt;/th&gt;
      &lt;th&gt;ClickHouse&lt;/th&gt;
      &lt;th&gt;TIDB&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;版本&lt;/td&gt;
      &lt;td&gt;DorisDB-SE-1.15.2&lt;/td&gt;
      &lt;td&gt;ClickHouse server version 21.6.5 revision 54448&lt;/td&gt;
      &lt;td&gt;TiDB-v5.1.0 TiDB Server Community Edition (dev)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;定位&lt;/td&gt;
      &lt;td&gt;ROLAP&lt;br /&gt;MPP 执行架构&lt;br /&gt;支持物化视图(RollUp)&lt;br /&gt;使用 Index 加速查询&lt;br /&gt;向量化计算（AVX2 指令集）&lt;br /&gt;稀疏索引&lt;/td&gt;
      &lt;td&gt;ROLAP&lt;br /&gt;MPP 架构的列存储库，多样化的表引擎&lt;br /&gt;以有序存储为核心的完全列存储实现&lt;br /&gt;并行扫磁盘上的数据块，降低单个查询的 latency&lt;br /&gt;极致向量化计算&lt;br /&gt;稀疏索引&lt;/td&gt;
      &lt;td&gt;HTAP&lt;br /&gt;行存储（TiKV 存储方式）&lt;br /&gt;列存储（TiFlash 组件实现）&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;语言层面&lt;/td&gt;
      &lt;td&gt;Front End: Java, Back End: C++&lt;/td&gt;
      &lt;td&gt;C++&lt;/td&gt;
      &lt;td&gt;TiDB: Go, TiKV: Rust, RocksDB: C++&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;架构层面&lt;/td&gt;
      &lt;td&gt;Front End:&lt;br /&gt;管理元数据&lt;br /&gt;监督管理 BE 的上下线&lt;br /&gt;解析 SQL，分发执行计划&lt;br /&gt;协调数据导入&lt;br /&gt;&lt;br /&gt;Back End: &lt;br /&gt;接收并执行计算任务&lt;br /&gt;存储数据，管理副本&lt;br /&gt;执行 compact 任务&lt;/td&gt;
      &lt;td&gt;Zookeeper: &lt;br /&gt;执行分布式 DDL；&lt;br /&gt;多副本之间的数据同步任务管理下发；&lt;br /&gt;&lt;br /&gt;ClickHouse:&lt;br /&gt;数据存储查询；&lt;br /&gt;架构扁平化，可部署任意规模集群；&lt;/td&gt;
      &lt;td&gt;TIDB 作为计算&amp;amp;调度层：&lt;br /&gt;SQL 解析/分发/谓词下推到存储层；&lt;br /&gt;集群信息收集，replica 管理等；&lt;br /&gt;&lt;br /&gt;TiKV+RocksDB 作为数据存储层：&lt;br /&gt;TiKV 实现了事务支持/Raft 协议/数据读写；&lt;br /&gt;数据最终存放在 RocksDB；&lt;br /&gt;TiFlash: 列存储，用来加速查询&amp;amp;支持 OLAP 相关需求；&lt;br /&gt;TiSpark: 计算引擎，用来支持 OLAP 相关需求；&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;系统安装&amp;amp;维护&lt;/td&gt;
      &lt;td&gt;成本低；&lt;br /&gt;&lt;br /&gt;收费版本提供 DorisManager 工具来安装维护集群，免费版本需要手动安装；&lt;br /&gt;看官方文档说：“DorisDB 是一个自治的系统，节点的上下线，集群扩缩容都可通过一条简单的 SQL 命令来完成，数据的自动 Reblance”；&lt;br /&gt;看各种技术文提到 DorisDB 维护成本也是较低，但不清楚是否能达到官方文档中的效果和是否有各种异常出现；&lt;/td&gt;
      &lt;td&gt;成本高&lt;br /&gt;&lt;br /&gt;Clickhouse 需安装的组件较少，安装起来也较为简单；但集群不能自发感知机器拓扑变化，不能自动 Rebalance 数据，扩容/缩容/上下线分片、副本等都需要修改配置文件或执行 SQL 语句做数据迁移表变更的操作，有些时还需需要重启服务，维护成本高；&lt;br /&gt;&lt;/td&gt;
      &lt;td&gt;成本中；&lt;br /&gt;&lt;br /&gt;个人感觉 TiDB 的组件较多，&lt;br /&gt;计算存储相关就有 TiDB，TiKV，TiFlash，TiSpark 等；&lt;br /&gt;所以系统安装相对麻烦一点（需要安装的组件较多），对于扩容/缩容/升级等操作官方有提供 TiUP 组件，操作相对简单；&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;监控&amp;amp;报警&lt;/td&gt;
      &lt;td&gt;收费版本中可使用 DorisManager 提供的控报警的功能；&lt;br /&gt;免费版本可以通过 Prometheus+Grafana 进行监控报警；&lt;/td&gt;
      &lt;td&gt;Clikckhouse 支持对接 Prometheus ，可以通过 Prometheus+Grafana 进行监控报警；&lt;/td&gt;
      &lt;td&gt;官方提供一套 Prometheus+Grafana 来实现监控报警的功能；&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;系统升级&lt;/td&gt;
      &lt;td&gt;可以滚动/平滑的升级，官方有相关升级文档&lt;/td&gt;
      &lt;td&gt;通过 yum 命令进行升级&lt;/td&gt;
      &lt;td&gt;通过 TiUP 实施在线/离线升级&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;集群能力&lt;/td&gt;
      &lt;td&gt;官方文档中说集群规模可扩展到数百个节点，&lt;br /&gt;支持 10PB 级别的数据分析；&lt;/td&gt;
      &lt;td&gt;官方提供案例集群规模在 300+，20 万亿+条的数据，&lt;br /&gt;10PB+级别的数据；&lt;/td&gt;
      &lt;td&gt;官方文档中有说最大支持 512 个节点，&lt;br /&gt;集群最大容量 PB 级；&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;SQL 语句支持&lt;/td&gt;
      &lt;td&gt;支持标准 SQL 语法&lt;/td&gt;
      &lt;td&gt;支持大部分的标准 SQL 语法&lt;br /&gt;有部分非标准 SQL 语法，有一定学习成本&lt;/td&gt;
      &lt;td&gt;支持标准 SQL 语法&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;SQL 数据类型&lt;/td&gt;
      &lt;td&gt;Number, String[1, 65533], Date, HyperLogLog, Bitmap, Array, Nested-Array&lt;/td&gt;
      &lt;td&gt;Number, String[1, ∞], Date, Bitmap, Enum, UUID, IP, Tuple, Array, Nested Array, Map, Nested Type, AggregateFunction, Lambda Expression&lt;/td&gt;
      &lt;td&gt;Number, String[1, ∞], Date, Blob, Binary, Enum, Set, Json&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Schema 变更&lt;/td&gt;
      &lt;td&gt;支持对 column/rollup/index 等进行增删改的操作；&lt;/td&gt;
      &lt;td&gt;Distributed, Merge, MergeTree 等表引擎支持 Alter 语句，可以对 column/index 等进行增删改的操作；ALTER 操作会阻塞对表的所有读写操作；&lt;/td&gt;
      &lt;td&gt;支持对 column/index 进行增删改的操作；&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;函数支持&lt;/td&gt;
      &lt;td&gt;支持日期/地理/字符串/聚合/窗口/bitmap/hash/cast 等函数；&lt;br /&gt; 支持 UDF，但需要写 C++；&lt;/td&gt;
      &lt;td&gt;支持函数较 DorisDB 丰富，复合数据类型的函数支持也很丰富，诸如：hasAny, hasAll 很是用；&lt;br /&gt; 支持 UDF，但需要写 C++；&lt;/td&gt;
      &lt;td&gt;支持的函数种类也很多，但在 JSON/复合结构的函数支持上一般；&lt;br /&gt; 暂不支持 UDF；&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;数据模型&lt;/td&gt;
      &lt;td&gt;明细模型，聚合模型，更新模型（允许定义 UNIQUE KEY，导入数据时需要将所有字段补全才能够完成更新操作）&lt;/td&gt;
      &lt;td&gt;多样的表引擎提供了多样的数据聚合&amp;amp;分析模型 https://clickhouse.tech/docs/en/engines/table-engines/&lt;/td&gt;
      &lt;td&gt;TiFlash：列存储，TiSpark：&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;事务支持&lt;/td&gt;
      &lt;td&gt;每个导入任务都是一个事务&lt;/td&gt;
      &lt;td&gt;没有完整的事务支持&lt;/td&gt;
      &lt;td&gt;支持分布式事务，提供乐观事务与悲观事务两种事务模型&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;写入支持&lt;/td&gt;
      &lt;td&gt;支持从 Mysql, Hive, HDFS, S3, Local Disk, Kafka 等系统拉取数据；&lt;br /&gt;支持通过 Spark, Flink, DataX, Insert into table select … 等方式写入数据；&lt;br /&gt;支持 CSV、ORCFile、Parquet、Json 等文件格式；&lt;/td&gt;
      &lt;td&gt;支持从 PostgreSQL, Mysql, Hive, HDFS, Kafka, Local Disk, 等系统拉取数据；&lt;br /&gt;支持通过 Spark，Flink，Insert into table select …等方式写入数据；&lt;br /&gt;支持 CSV, TSV, Json, Protobuf, Avro, Parquet, ORC, XML…等数据格式；&lt;/td&gt;
      &lt;td&gt;支持从 Mysql, CSV, SQL 文件导入到 TiDB；&lt;br /&gt;支持通过 Load data…等方式写入数据；&lt;br /&gt;TiDB Lightning 支持 Dumpling, CSV, Amazon Aurora Parquet 等数据格式；&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;更新支持&lt;/td&gt;
      &lt;td&gt;支持 Insert/Insert into table select…语句；&lt;br /&gt;不支持 update 语句，需要采用 Insert+Merge；&lt;/td&gt;
      &lt;td&gt;支持 Insert/Insert into table select…语句；&lt;br /&gt;支持 update 语句，通过语法 ALTER TABLE table_name UPDATE column1 = expr1 [, …] WHERE filter_expr 实现，更新相当于重建分区，较重操作，以异步方式执行；&lt;/td&gt;
      &lt;td&gt;TiDB 支持 Insert/Insert into table select…语句；&lt;br /&gt;TiDB 支持 Update 语句；&lt;br /&gt;TiFlash 可以与 TiKV 数据保持一致，通过 Raft 实现时数据同步；&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;删除支持&lt;/td&gt;
      &lt;td&gt;支持语句：DELETE FROM table_name [PARTITION partition_name] WHERE filter_expr&lt;/td&gt;
      &lt;td&gt;支持语句：ALTER TABLE [db.]table [ON CLUSTER cluster] DELETE WHERE filter_expr&lt;/td&gt;
      &lt;td&gt;TiDB 支持 Delete 语句&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;查询支持&lt;/td&gt;
      &lt;td&gt;Select 语句基本符合 SQL92 标准，支持 inner join，outer join，semi join，anti join，cross join。在 inner join 条件里除了支持等值 join，还支持不等值 join， 为了性能考虑，推荐使用等值 join。其它 join 只支持等值 join。&lt;/td&gt;
      &lt;td&gt;有非标准查询语法，支持 Join 语句，详细看社区文档：&lt;br /&gt;https://clickhouse.tech/docs/en/sql-reference/statements/select/&lt;/td&gt;
      &lt;td&gt;SELECT 语句与 MySQL 完全兼容；&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;数据交换&lt;/td&gt;
      &lt;td&gt;外部表支持：Mysql, HDFS, ElasticSearch, Hive;&lt;br /&gt;交互支持：Kafka, Flink, Spark, Hive, Http, Mysql wire protocol&lt;/td&gt;
      &lt;td&gt;外部表支持：Mysql, HDFS, Http url, Jdbc, Odbc；&lt;br /&gt;交互支持：Flink(通过 clickhouse-jdbc 实现), Spark, Http, Jdbc, Odbc, Native Tcp, Command line, Mysql wire protocol, SQL 可视化工具&lt;/td&gt;
      &lt;td&gt;交互支持：通过 Flink，TiSpark 进行查询操作；&lt;br /&gt; 对外暴漏的是 SQL 层，可以通过 JDBC, Mysql wire protocol 进行读写;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;集群模式&lt;/td&gt;
      &lt;td&gt;Front End + Back End&lt;/td&gt;
      &lt;td&gt;Zookeeper + Clickhouse&lt;/td&gt;
      &lt;td&gt;TiDB + PD + TiKV + TiFlash + TiSpark&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;问题遗留&lt;/td&gt;
      &lt;td&gt;当前不支持 update 语句，2021.7 月的新版本会支持&lt;/td&gt;
      &lt;td&gt;optimize table $name final 性能不高，多表 join 性能一般&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;简短概括&lt;/td&gt;
      &lt;td&gt;Doris 依靠索引+Rollup+分区分桶等方案实现高效的 OLAP，支持明细查询；复合数据类型支持有限，维护成本低；&lt;/td&gt;
      &lt;td&gt;Clickhouse 依靠列存储+向量化+分片+多样的表引擎实现高效 OLAP，支持明细查询；功能强大，维护成本高；&lt;/td&gt;
      &lt;td&gt;Tidb 更多是满足了 TP 场景，对与 AP 支持不足，OLAP 能力弱于 Doris&amp;amp;Clickhouse，支持明细查询；官方目标是支持 100%TP 场景和 80%AP 场景，大数据量/复杂 AP 场景通过 TiSpark 解决，维护成本较低；&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;综合对比&lt;/td&gt;
      &lt;td&gt;DorisDB 和 Clickhouse 都是为 OLAP 而设计的系统，DorisDB 在系统运维等方面十分方便，但相对 Clickhouse 在对复合数据类型支持上不够，暂不支持 Update 操作，在数据模型支持上也稍弱于 Clickhouse，SQL 函数支持上没 ClickHouse 丰富；&lt;/td&gt;
      &lt;td&gt;Clickhouse 集群拓扑变化，分片上下线等都无法自动感知并进行 Reblance，维护成本高，多表 join 性能不稳定，但从 OLAP 引擎功能来讲 Clickhouse 是三者中最强大的一个，且有一定 Update 能力（OLAP 本身不善长 Update 操作）；&lt;/td&gt;
      &lt;td&gt;TiDB 的 TP 能力三者最好，AP 能力较差，毕竟大家都说是分布式 Mysql，增/删/改/事务能力强，能同步 Mysql binlog，但 AP 能力更多依靠 TiFlash/TiSpark，查询聚合耗时较其他两者长且不可控，组件较多，学习安装成本感觉较 DorisDB 高一些；&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;实时人群圈选场景需求说明&quot;&gt;实时人群圈选场景需求说明&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;画像本身是一张以&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;user_id&lt;/code&gt;为主键，标签为列名的大宽表；&lt;/li&gt;
  &lt;li&gt;画像标签数据有大量的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Array&lt;/code&gt;,&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MAP&lt;/code&gt;,&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Array&amp;lt;Map&amp;gt;&lt;/code&gt;等复合类型；&lt;/li&gt;
  &lt;li&gt;画像标签需要实时增/删，对存储要求是：支持实时的增/删/改列的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DDL&lt;/code&gt;语句；&lt;/li&gt;
  &lt;li&gt;标签数据需实时增量的更新，对存储要求是：支持大数据量的实时&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UPDATE&lt;/code&gt;语句；&lt;/li&gt;
  &lt;li&gt;人群圈选需要实时对任意标签列实施高效的组合过滤操作；&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;功能选型&quot;&gt;功能选型&lt;/h2&gt;

&lt;h3 id=&quot;tidb&quot;&gt;TIDB&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;strong&gt;优点：&lt;/strong&gt;

    &lt;ol&gt;
      &lt;li&gt;支持实时高频的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UPDATE&lt;/code&gt;操作；&lt;/li&gt;
    &lt;/ol&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;strong&gt;缺点：&lt;/strong&gt;

    &lt;ol&gt;
      &lt;li&gt;
        复合数据类型只支持&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Set&lt;/code&gt;/&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;JSON&lt;/code&gt;，对&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;JSON&lt;/code&gt;来说，其内部结构不可见，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Schema&lt;/code&gt;定义不够明确；&lt;br /&gt;
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;JSON&lt;/code&gt;这种半结构化数据格式是以&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Binary&lt;/code&gt;形式进行序列化存储的（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;JSON&lt;/code&gt;本身不能创建索引，需要反序列化解析后才能做过滤操作？），用于圈选性能较差，为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;JSON&lt;/code&gt;定义的函数不够丰富，且无法通过自定义函数来补充；
      &lt;/li&gt;
      &lt;li&gt;
        数据写入当前好想只能通过 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Insert Into&lt;/code&gt; 语句，性能一般，不支持直接导入&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;JSON&lt;/code&gt;数据；&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TiDB&lt;/code&gt; 有&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DM&lt;/code&gt;工具，但只能实时同步 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Mysql Binlog&lt;/code&gt;，缺少与多种其他存储介质的数据交换功能；&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Spark/Flink&lt;/code&gt;的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Sink Connector&lt;/code&gt;好像没有，可能需要自己手动实现；
      &lt;/li&gt;
      &lt;li&gt;
        缺少和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Hadoop&lt;/code&gt;生态圈的交互以及数据同步组件，可能需要通过&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Jdbc&lt;/code&gt;或&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TiKV API&lt;/code&gt;接口实现；
      &lt;/li&gt;
    &lt;/ol&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;strong&gt;其他：&lt;/strong&gt;

    &lt;ol&gt;
      &lt;li&gt;读数据有&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TiKV Client Java Api&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Flink Source Connector&lt;/code&gt;是通过这个包自己定制直接读取 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TiKV&lt;/code&gt;数据的，可以做一些&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Filter Push Down&lt;/code&gt;；&lt;/li&gt;
    &lt;/ol&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;dorisdb&quot;&gt;DorisDB&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;strong&gt;优点：&lt;/strong&gt;

    &lt;ol&gt;
      &lt;li&gt;集群易维护；&lt;/li&gt;
      &lt;li&gt;支持&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Array&lt;/code&gt;这种复合结构（当前只明细模型可用）；&lt;/li&gt;
      &lt;li&gt;多表关联查询性能良好；&lt;/li&gt;
    &lt;/ol&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;strong&gt;缺点：&lt;/strong&gt;

    &lt;ol&gt;
      &lt;li&gt;
        不开源，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DorisDB&lt;/code&gt;源码全都看不到，社区活跃度较&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ClickHouse&lt;/code&gt;差；
      &lt;/li&gt;
      &lt;li&gt;
        支持复合数据类型不够，遇到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Array&lt;/code&gt;,&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Map&lt;/code&gt;或&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Array&amp;lt;Map&amp;gt;&lt;/code&gt;类型的标签需要打成宽表或拆表（多表&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Join&lt;/code&gt;性能良好）；
      &lt;/li&gt;
      &lt;li&gt;
        当前不支持&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UPDATE&lt;/code&gt;操作， 需要通过&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;INSERT OVERWRITE&lt;/code&gt;模拟更新操作（官方说是 7 月版本会支持&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UPDATE&lt;/code&gt;）；
      &lt;/li&gt;
      &lt;li&gt;
        当前只能在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Duplicate Table&lt;/code&gt;中定义&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Array&lt;/code&gt;类型，而圈选功能需要选择更新/聚合模型，这使得&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Array&lt;/code&gt;类型标签必须拆分到新表中，不太能满足圈人的功能需求；
      &lt;/li&gt;
    &lt;/ol&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;strong&gt;其他：&lt;/strong&gt;

    &lt;ol&gt;
      &lt;li&gt;
        发现一个比较怪的问题，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Apache Doris&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DorisDB&lt;/code&gt;感觉上有分歧？按道理&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Apache Doris&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DorisDB&lt;/code&gt;标准版应该是同一个产品定位吧，两者间的的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Flink Connector&lt;/code&gt;实现代码差异较大，内置的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UDF&lt;/code&gt;函数也各不相同，源码层面二者还是有一定差别的；
      &lt;/li&gt;
      &lt;li&gt;
        &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Apache Doris&lt;/code&gt;暂时不支持符合结构数据，如：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Array&lt;/code&gt;,&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Map&lt;/code&gt;等；
      &lt;/li&gt;
    &lt;/ol&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;clickhouse&quot;&gt;ClickHouse&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;strong&gt;优点：&lt;/strong&gt;

    &lt;ol&gt;
      &lt;li&gt;
        支持&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UPDATE&lt;/code&gt;操作（但性能一般，不如&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TiDB&lt;/code&gt;，可以用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;INSERT OVERWRITE&lt;/code&gt;模拟）；
      &lt;/li&gt;
      &lt;li&gt;
        有多样的表引擎和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SQL&lt;/code&gt;函数；
      &lt;/li&gt;
      &lt;li&gt;
        支持多样的复合数据类型 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Array&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Map&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Nested Type&lt;/code&gt;（但尝试写&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;JSON&lt;/code&gt;数据到类型为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Array&amp;lt;Nested&amp;gt;&lt;/code&gt;列时失败，可能是写入方法问题？）；
      &lt;/li&gt;
      &lt;li&gt;
        &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OLAP&lt;/code&gt;系统，查询性能较好；
      &lt;/li&gt;
    &lt;/ol&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;strong&gt;缺点：&lt;/strong&gt;

    &lt;ol&gt;
      &lt;li&gt;集群维护成本高；&lt;/li&gt;
      &lt;li&gt;多表&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Join&lt;/code&gt;性能不稳定；&lt;/li&gt;
      &lt;li&gt;非标准&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SQL&lt;/code&gt;，有学习成本；&lt;/li&gt;
      &lt;li&gt;每个表都需要手动分区分片，在维护的表较多时，日常维护成本昂贵；&lt;/li&gt;
    &lt;/ol&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;strong&gt;其他：&lt;/strong&gt;

    &lt;ol&gt;
      &lt;li&gt;当前工程用的开源协议是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Apache License Version 2.0&lt;/code&gt;；&lt;/li&gt;
      &lt;li&gt;源码没贡献到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Apache&lt;/code&gt;社区，项目规划走向被俄罗斯团队把控；&lt;/li&gt;
    &lt;/ol&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;功能选型-1&quot;&gt;功能选型&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;结论：&lt;/strong&gt;
从功能角度出发更倾向于选择&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ClickHouse&lt;/code&gt;；&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;原因：&lt;/strong&gt;
    &lt;ol&gt;
      &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ClickHouse&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DorisDB&lt;/code&gt;在做&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OLAP&lt;/code&gt;的性能和功能上高于&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TiDB&lt;/code&gt;；&lt;/li&gt;
      &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DorisDB&lt;/code&gt;的主要问题是对复合数据类型的支持不够（比如&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Array&lt;/code&gt;），这使得很多是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Array&lt;/code&gt;类型的列必须进行拆表操作，业务成本高，增加了标签数据写入和查询等业务实现的复杂度；&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Update&lt;/code&gt;这个必须的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Feature&lt;/code&gt;功能尚未看到；函数支持没&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ClickHouse&lt;/code&gt;丰富，在做查询时有些过滤规则没办法实现（比如：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Array hasAny/hasAll&lt;/code&gt;）；&lt;/li&gt;
      &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TiDB&lt;/code&gt;对复合数据类型的支持不够，只有&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Set&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;JSON&lt;/code&gt;这种复合结构，与&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Hadoop&lt;/code&gt;生态或其他外部存储结合度不高，数据的导入导出不够方便，支持导入的数据类型也不够丰富，使用上不太方便；函数支持上没&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ClickHouse&lt;/code&gt;丰富，没办法做&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Array hasAny/hasAll&lt;/code&gt;等操作；另外&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TiDB&lt;/code&gt;的查询性能不保证满足需求，需做测试；&lt;/li&gt;
      &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ClickHouse&lt;/code&gt;从功能角度来讲是最能满足用户圈选需求的系统，唯一的问题是维护成本较高，当前国内已有公司将&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Clickhouse&lt;/code&gt;应用到了画像场景；&lt;/li&gt;
    &lt;/ol&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;性能测试&quot;&gt;性能测试&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;机器配置&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt; &lt;/th&gt;
      &lt;th&gt;ClickHouse&lt;/th&gt;
      &lt;th&gt;TiDB(No TiFlash)&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Linux 版本&lt;/td&gt;
      &lt;td&gt;CentOS Linux release 7.2.1511&lt;/td&gt;
      &lt;td&gt;CentOS Linux release 7.9.2009&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;CPU&lt;/td&gt;
      &lt;td&gt;QEMU Virtual CPU version (cpu64-rhel6)&lt;/td&gt;
      &lt;td&gt;Intel(R) Xeon(R) Platinum 8163 CPU @ 2.50GHz&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;CPU 核数&lt;/td&gt;
      &lt;td&gt;8C&lt;/td&gt;
      &lt;td&gt;8C&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;CPU 线程数&lt;/td&gt;
      &lt;td&gt;8&lt;/td&gt;
      &lt;td&gt;8&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;内存大小&lt;/td&gt;
      &lt;td&gt;8G&lt;/td&gt;
      &lt;td&gt;32G&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;磁盘大小&lt;/td&gt;
      &lt;td&gt;1T&lt;/td&gt;
      &lt;td&gt;1T&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;SQL 语句&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;pre&gt;&lt;code class=&quot;language-SQL&quot;&gt;
-- 表`user_profile_a`数据量：4528641条
-- 表`user_profile_b`数据量：12836360条

-- SQL-1: 基本数据类型的组合过滤
select count(*) from  user_profile_b where channel_type = 'nature' and user_type = 2 and lang = 'zh_CN' and region = 'CHN' and country = 'CHN' and gender = 0;

-- SQL-2 过滤double类型数值，通过Math.random() 为每条测试数据随机生成一个score
select count(*) from user_profile_ where score &amp;gt; 0.2;

-- SQL-3: 数组类型的过滤
select count() from user_profile_b where hasAny(user_watch_list, ['TSLA']); -- Clickhouse
select count(*) from  user_profile_b where JSON_CONTAINS(user_watch_list, '&quot;TSLA&quot;', '$'); -- TIDB

-- SQL-4: 组合SQL-1, SQL-3的所有条件过滤
select count() from  user_profile_b where channel_type = 'nature' and user_type = 2 and lang = 'zh_CN' and region = 'CHN' and country = 'CHN' and gender = 0 and hasAny(user_watch_list, ['TSLA']); -- ClickHouse
select count(*) from  user_profile_b where channel_type = 'nature' and user_type = 2 and lang = 'zh_CN' and region = 'CHN' and country = 'CHN' and gender = 0 and JSON_CONTAINS(user_watch_list, '&quot;TSLA&quot;', '$'); -- TiDB

-- SQL-5: Join两张表，基本数据类型过滤条件
select count(*) from user_profile_b b, user_profile_a a where a.uuid = b.uuid and a.channel_type = 'nature' and a.user_type = 2 and b.lang = 'zh_CN' and b.region = 'CHN' and b.country = 'CHN' and b.gender = 0;

-- SQL-6: Join两张表，带有Array类型的过滤条件
select count() from user_profile_b b, user_profile_a a where a.uuid = b.uuid and a.channel_type = 'nature' and a.user_type = 2 and b.lang = 'zh_CN' and b.region = 'CHN' and b.country = 'CHN' and b.gender = 0 and hasAny(a.user_watch_list, ['TSLA']);  -- ClickHouse
select count(*) from user_profile_b b, user_profile_a a where a.uuid = b.uuid and a.channel_type = 'nature' and a.user_type = 2 and b.lang = 'zh_CN' and b.region = 'CHN' and b.country = 'CHN' and b.gender = 0 and JSON_CONTAINS(a.user_watch_list, '&quot;TSLA&quot;', '$');  -- TiDB

&lt;/code&gt;&lt;/pre&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;查询性能对比测试&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt; &lt;/th&gt;
      &lt;th&gt;ClickHouse&lt;/th&gt;
      &lt;th&gt;TiDB(No TiFlash)&lt;/th&gt;
      &lt;th&gt;Explain&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;SQL-1&lt;/td&gt;
      &lt;td&gt;0.069s&lt;/td&gt;
      &lt;td&gt;1.695s&lt;/td&gt;
      &lt;td&gt;单表简单数据类型的条件组合查询&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;SQL-2&lt;/td&gt;
      &lt;td&gt;0.029s&lt;/td&gt;
      &lt;td&gt;1.113s&lt;/td&gt;
      &lt;td&gt;单表&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Double&lt;/code&gt;类型数据的条件查询&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;SQL-3&lt;/td&gt;
      &lt;td&gt;1.732s&lt;/td&gt;
      &lt;td&gt;30.935s&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TiDB&lt;/code&gt;官方文档说：”使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Binary&lt;/code&gt;格式进行序列化，对&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;JSON&lt;/code&gt;的内部字段的查询、解析加快”，但从测试结果来看与&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CK&lt;/code&gt;相差较多&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;SQL-4&lt;/td&gt;
      &lt;td&gt;0.229s&lt;/td&gt;
      &lt;td&gt;1.142s&lt;/td&gt;
      &lt;td&gt;应该有做&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SQL&lt;/code&gt;优化：过滤简单数据类型的条件前置，减少需过滤&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Array&lt;/code&gt;条件的数据量，查询性能较&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SQL-3&lt;/code&gt;提升不少&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;SQL-5&lt;/td&gt;
      &lt;td&gt;0.626s&lt;/td&gt;
      &lt;td&gt;0.536s&lt;/td&gt;
      &lt;td&gt;两张表&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Join&lt;/code&gt;情况下&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TiDB&lt;/code&gt;要略好于 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CK&lt;/code&gt;，看来&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CK&lt;/code&gt;多表&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Join&lt;/code&gt;性能确实不太好？&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;SQL-6&lt;/td&gt;
      &lt;td&gt;1.243s&lt;/td&gt;
      &lt;td&gt;0.880s&lt;/td&gt;
      &lt;td&gt;两张表&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Join&lt;/code&gt;情况下&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TiDB&lt;/code&gt;要略好于 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CK&lt;/code&gt;，看来&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CK&lt;/code&gt;多表&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Join&lt;/code&gt;性能确实不太好？&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;ul&gt;
  &lt;li&gt;性能测试使用的都是单节点的机器，因某些原因未能在同样的软硬件基础上做性能测试；&lt;/li&gt;
  &lt;li&gt;此处没有将&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DorisDB&lt;/code&gt;纳入对比，一方面是因为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DorisDB&lt;/code&gt;满足不了画像场景的业务需求，比如对&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Array&lt;/code&gt;等复合类型支持不够，另一方面和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DorisDB&lt;/code&gt;的工作人员沟通得到了一些性能方面的测试结论，自己假设了&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DorisDB&lt;/code&gt;的单表性能与&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ClickHouse&lt;/code&gt;相当，多表&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Join&lt;/code&gt;性能高于&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ClickHouse&lt;/code&gt;；&lt;/li&gt;
  &lt;li&gt;本次性能测试场景并不完整，单从具体得到的结果来看，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ClickHouse&lt;/code&gt;在多表&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Join&lt;/code&gt;方面表现一般，若有多表做复杂&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Join&lt;/code&gt;或大表间做&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Join&lt;/code&gt;操作的需求，建议做更多具体的性能测试；&lt;/li&gt;
  &lt;li&gt;从&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Array&lt;/code&gt;查询优化来讲，在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ClickHouse&lt;/code&gt;中可以考虑将&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Array&lt;/code&gt;转&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Bitmap&lt;/code&gt;来优化查询；&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TiDB&lt;/code&gt;有&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TiFlash&lt;/code&gt;，但不太清楚如何处理&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Array&lt;/code&gt;这类复合类型，是否能满足性能需求；&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Insert into select&lt;/code&gt;语句的性能大都取决于&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Select&lt;/code&gt;，非特别大数据量需写入的情况下&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Insert&lt;/code&gt;耗时相对较少；&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ClickHouse&lt;/code&gt;的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Delete&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Update&lt;/code&gt;语句性能不高，这个需要再做测试，或用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Insert&lt;/code&gt;语句替换；&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ClickHouse&lt;/code&gt;的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;optimize table $table final&lt;/code&gt;性能不高，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1200W&lt;/code&gt;数据测试需要&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;8s～14s&lt;/code&gt;时间，生产上使用该操作时最好单独周期执行，比如&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;5min&lt;/code&gt;执行一次；&lt;/li&gt;
&lt;/ul&gt;
</description>
        <pubDate>Sun, 11 Jul 2021 00:00:00 +0000</pubDate>
        <link>https://itinycheng.github.io/2021/07/11/comparison-and-selection-of-dorisdb-tidb-clickhouse/</link>
        <guid isPermaLink="true">https://itinycheng.github.io/2021/07/11/comparison-and-selection-of-dorisdb-tidb-clickhouse/</guid>
        
        <category>doris</category>
        
        <category>clickhouse</category>
        
        <category>tidb</category>
        
        
        <category>olap</category>
        
      </item>
    
      <item>
        <title>Flink Streaming 的启动流程源码分析</title>
        <description>&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#前言&quot; id=&quot;markdown-toc-前言&quot;&gt;前言&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#启动流程分析&quot; id=&quot;markdown-toc-启动流程分析&quot;&gt;启动流程分析&lt;/a&gt;    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#client-初始化-job&quot; id=&quot;markdown-toc-client-初始化-job&quot;&gt;[Client] 初始化 Job&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#server-启动-jobmanager&quot; id=&quot;markdown-toc-server-启动-jobmanager&quot;&gt;[Server] 启动 JobManager&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#server-启动-taskmanager&quot; id=&quot;markdown-toc-server-启动-taskmanager&quot;&gt;[Server] 启动 TaskManager&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#内存计算&quot; id=&quot;markdown-toc-内存计算&quot;&gt;内存计算&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#参考文档&quot; id=&quot;markdown-toc-参考文档&quot;&gt;参考文档&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;前言&quot;&gt;前言&lt;/h2&gt;

当前团队&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Flink&lt;/code&gt;集群使用的版本是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1.7.2&lt;/code&gt;，采用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;per-job on yarn&lt;/code&gt;的运行模式，在近一年多的使用过程中碰到过多次内存相关的问题，比如：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;beyond the 'PHYSICAL' memory limit... Killing container.&lt;/code&gt;，总是感觉&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Flink Streaming&lt;/code&gt;在实际场景中的内存管理不够完美，会遇到各样的问题。在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Flink 1.10&lt;/code&gt;版本 release 后，了解到该版本对&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TaskExecutor&lt;/code&gt;的内存配置做了重新设计，内心有想要去了解的冲动，然而看过社区文档后又有了更多的疑问，比如：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TaskExecutor&lt;/code&gt;对应的 JVM 进程在启动时只会有&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-Xmx -Xms -XX:MaxDirectMemorySize&lt;/code&gt;三个内存相关参数是通过&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Flink&lt;/code&gt;计算得出的，新增的细粒度配置能给&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;JVM&lt;/code&gt;这三个启动参数带来多少变化，或是只是一个方便内存计算的工具，对于对&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Flink&lt;/code&gt;内存较为了解的人来讲，通过旧的内存配置参数可以完成与新配置一样的效果。

起初这篇文章计划写&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Flink Streaming&lt;/code&gt;新/旧内存管理对比相关的内容，然而之后一段时间的业余精力被学习&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Rust&lt;/code&gt;消耗掉啦，6 月底才算有时间开篇；之前在阅读内存管理代码同时参杂读了些任务启动相关代码，所以就扩展下之前计划写的文章范围：以描述&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Flink Streaming&lt;/code&gt;整个启动流程为主，辅以内存分配/管理相关代码分析，阅读的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Flink&lt;/code&gt;代码版本定为最新版本&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1.11.0&lt;/code&gt;。

&lt;h2 id=&quot;启动流程分析&quot;&gt;启动流程分析&lt;/h2&gt;

&lt;strong&gt;启动流程：&lt;/strong&gt; 从脚本提交到任务启动成功（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Flink-Web-Dashboard&lt;/code&gt;展示的任务状态为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;running&lt;/code&gt;）的整个流程，这个流程大致分为 3 个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Stage&lt;/code&gt;：

&lt;ol&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Client&lt;/code&gt;端：封装信息提交给&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Yarn&lt;/code&gt;，注册回调函数，轮训 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Job&lt;/code&gt; 状态；&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Yarn&lt;/code&gt;分配&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Container&lt;/code&gt;启动&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AppMaster&lt;/code&gt;；&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AppMaster&lt;/code&gt;向&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Yarn&lt;/code&gt;申请资源启动&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TaskManager&lt;/code&gt;；&lt;/li&gt;
&lt;/ol&gt;

&lt;strong&gt;脚本示例：&lt;/strong&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;// Per-job model
flink run &lt;span class=&quot;nt&quot;&gt;-m&lt;/span&gt; yarn-cluster &lt;span class=&quot;nt&quot;&gt;-yn&lt;/span&gt; 24 &lt;span class=&quot;nt&quot;&gt;-ys&lt;/span&gt; 2 &lt;span class=&quot;nt&quot;&gt;-ytm&lt;/span&gt; 6g &lt;span class=&quot;nt&quot;&gt;-ynm&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$job_name&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-c&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$main_class&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-d&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-yq&lt;/span&gt; ./&lt;span class=&quot;nv&quot;&gt;$job_jar&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$params&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;strong&gt;StreamGraph：&lt;/strong&gt;

&lt;pre&gt;&lt;code class=&quot;language-mermaid&quot;&gt;graph LR
A(KafkaSource) --&amp;gt; B(MapOperator)
B --&amp;gt; C(KafkaSink)
&lt;/code&gt;&lt;/pre&gt;

&lt;h3 id=&quot;client-初始化-job&quot;&gt;[Client] 初始化 Job&lt;/h3&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;
&lt;span class=&quot;c1&quot;&gt;// Client源码调用流程&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nl&quot;&gt;CliFrontend:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;CliFrontend&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;parseParameters&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;CliFrontend&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;run&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nl&quot;&gt;UserMainClass:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;args:&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Array&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;])&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;StreamExecutionEnvironment&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;getExecutionEnvironment&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;StreamExecutionEnvironment&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;addSource&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;DataStream&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;map&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;DataStream&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;addSink&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;StreamExecutionEnvironment&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;execute&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;StreamExecutionEnvironment&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;getStreamGraph&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;StreamExecutionEnvironment&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;getStreamGraphGenerator&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;StreamGraphGenerator&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;generate&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;StreamExecutionEnvironment&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;execute&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;StreamGraph&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;StreamExecutionEnvironment&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;executeAsync&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;DefaultExecutorServiceLoader&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;getExecutorFactory&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;YarnJobClusterExecutorFactory&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;getExecutor&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;YarnJobClusterExecutor&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;execute&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nl&quot;&gt;PipelineExecutorUtils:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;getJobGraph&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;YarnClusterClientFactory&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;createClusterDescriptor&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;YarnClusterDescriptor&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;deployJobCluster&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;YarnClusterDescriptor&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;deployInternal&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;YarnClusterDescriptor&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;startAppMaster&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;YarnClientImpl&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;submitApplication&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;CompletableFuture&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;ClusterClientJobClientAdapter&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;action&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;get&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;JobClient&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;invoke&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;JobListener&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;JobClient&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;`&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;action&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;create&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;JobExecutionResult&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;invoke&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;JobListener&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;JobExecutionResult&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;`&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;strong&gt;1. 任务提交脚本会触发调用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;org.apache.flink.client.cli.CliFrontend::main(String[] args)&lt;/code&gt;，然后依次会执行：&lt;/strong&gt;

&lt;ul&gt;
  &lt;li&gt;调用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EnvironmentInformation::logEnvironmentInfo&lt;/code&gt;加载 JVM 上线文环境；&lt;/li&gt;
  &lt;li&gt;调用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GlobalConfiguration::loadConfiguration&lt;/code&gt;加载&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;flink-conf.yaml&lt;/code&gt;配置信息；&lt;/li&gt;
  &lt;li&gt;调用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CliFrontend.loadCustomCommandLines&lt;/code&gt;返回由&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GenericCLI&lt;/code&gt;,&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FlinkYarnSessionCli&lt;/code&gt;,&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DefaultCLI&lt;/code&gt;组成的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;List&amp;lt;CustomCommandLine&amp;gt;&lt;/code&gt;对象；&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FlinkYarnSessionCli&lt;/code&gt;中包含与&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;yarn-cluster&lt;/code&gt;提交模式相关，可以通过&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Command&lt;/code&gt;命令提交的参数列表，如：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-yid, -ynm, -yqu&lt;/code&gt;等等；&lt;/li&gt;
  &lt;li&gt;调用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CliFrontend.new&lt;/code&gt;创建&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CliFrontend&lt;/code&gt;对象，利用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[3]&lt;/code&gt;中的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;List&amp;lt;CustomCommandLine&amp;gt;&lt;/code&gt;获取有效可用于解析&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Command&lt;/code&gt;参数的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Options&lt;/code&gt;列表，附值给成员变量&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;customCommandLineOptions&lt;/code&gt;；&lt;/li&gt;
  &lt;li&gt;调用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CliFrontend.parseParameters&lt;/code&gt;，匹配&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Command&lt;/code&gt;第一个参数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;run&lt;/code&gt;然后调用后序函数；&lt;/li&gt;
  &lt;li&gt;
    调用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CliFrontend.run&lt;/code&gt;：

    &lt;ul&gt;
      &lt;li&gt;解析&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Command&lt;/code&gt;参数并封装为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CommandLine&lt;/code&gt;对象；&lt;/li&gt;
      &lt;li&gt;创建&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ProgramOptions&lt;/code&gt;对象，是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Job Command&lt;/code&gt;参数的封装类，持有&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CommandLine&lt;/code&gt;解析得来的参数；&lt;/li&gt;
      &lt;li&gt;将&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ProgramOptions&lt;/code&gt;传入&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PackagedProgram&lt;/code&gt;的构造函数，创建&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PackagedProgram&lt;/code&gt;对象，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PackagedProgram&lt;/code&gt;负责具体具体调用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UserJar&lt;/code&gt;的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Main&lt;/code&gt;函数；&lt;/li&gt;
      &lt;li&gt;执行&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CliFrontend.executeProgram&lt;/code&gt;为任务执行上下文创建&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ExecutionEnvironmentFactory&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;StreamExecutionEnvironmentFactory&lt;/code&gt;对象，然后调用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PackagedProgram.invokeInteractiveModeForExecution&lt;/code&gt;方法反射调用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UserJar&lt;/code&gt;的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;main&lt;/code&gt;函数，执行具体任务逻辑；&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;strong&gt;2. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UserJar&lt;/code&gt;的任务入口函数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UserMainClass::main(args: Array[String])&lt;/code&gt;被调用后，会依次执行：&lt;/strong&gt;

&lt;ul&gt;
  &lt;li&gt;调用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;StreamExecutionEnvironment::getExecutionEnvironment&lt;/code&gt;创建&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;StreamExecutionEnvironment&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;StreamContextEnvironment&lt;/code&gt;对象；&lt;/li&gt;
  &lt;li&gt;调用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;StreamExecutionEnvironment.addSource&lt;/code&gt;创建&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DataStreamSource&lt;/code&gt;对象，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DataStreamSource&lt;/code&gt;对象内持有上下文环境中的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;StreamExecutionEnvironment&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;StreamTransformation&lt;/code&gt;对象，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;StreamTransformation&lt;/code&gt;中持有&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FlinkKafkaConsumer&lt;/code&gt;对象；&lt;/li&gt;
  &lt;li&gt;调用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DataStream.map&lt;/code&gt;创建&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OneInputTransformation&lt;/code&gt;对象，其内部持有上游的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;StreamTransformation&lt;/code&gt;和上线文中的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;StreamExecutionEnvironment&lt;/code&gt;对象；最后将&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OneInputTransformation&lt;/code&gt;添加到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;StreamExecutionEnvironment&lt;/code&gt;的成员变量&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;transformations&lt;/code&gt;列表中；&lt;/li&gt;
  &lt;li&gt;调用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DataStream.addSink&lt;/code&gt;创建&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DataStreamSink&lt;/code&gt;对象，并将其添加到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;StreamExecutionEnvironment&lt;/code&gt;的成员变量&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;transformations&lt;/code&gt;列表中；&lt;/li&gt;
  &lt;li&gt;最后调用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;StreamExecutionEnvironment.execute&lt;/code&gt;开始执行&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Job&lt;/code&gt;创建和任务提交；&lt;/li&gt;
&lt;/ul&gt;

&lt;strong&gt;3. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;StreamExecutionEnvironment.execute&lt;/code&gt;在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Client&lt;/code&gt;端的代码执行流程：&lt;/strong&gt;

&lt;ul&gt;
  &lt;li&gt;
    调用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;StreamExecutionEnvironment.getStreamGraph&lt;/code&gt;，先创建&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;StreamGraphGenerator&lt;/code&gt;对象，然后调用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;StreamGraphGenerator.generate&lt;/code&gt;生成&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;StreamGraph&lt;/code&gt;，生成&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;StreamGraph&lt;/code&gt;流程如下：

    &lt;ul&gt;
      &lt;li&gt;根据&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Job&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;StreamExecutionEnvironment&lt;/code&gt;的配置以及上下文信息创建&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;StreamGraph&lt;/code&gt;对象；&lt;/li&gt;
      &lt;li&gt;遍历&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;StreamExecutionEnvironment.transformations&lt;/code&gt;，对每个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;StreamTransformation&lt;/code&gt;进行解析；&lt;/li&gt;
      &lt;li&gt;从&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;StreamTransformation&lt;/code&gt;构建出&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;StreamNode&lt;/code&gt;并存放到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;StreamGraph&lt;/code&gt;对象的成员变量&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Map&amp;lt;Integer, StreamNode&amp;gt; streamNodes&lt;/code&gt;中，一个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;StreamNode&lt;/code&gt;包含着一个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FlinkOperator&lt;/code&gt;和这个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Operator&lt;/code&gt;运行所需的参数/配置信息；&lt;/li&gt;
      &lt;li&gt;调用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;StreamGraph.addEdge&lt;/code&gt;，构建每个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;StreamNode&lt;/code&gt;的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Input StreamEdge&lt;/code&gt; 和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Output StreamEdge&lt;/code&gt;对象，分别添加到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;StreamNode&lt;/code&gt;的成员变量&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;inEdges&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;outEdges&lt;/code&gt;中；&lt;/li&gt;
      &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;StreamEdge&lt;/code&gt;中包含它上下游&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;StreamNode&lt;/code&gt;的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Id&lt;/code&gt;值，数据传递规则&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ShuffleMode&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;StreamPartitioner&lt;/code&gt;等信息；&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;调用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;StreamExecutionEnvironment.execute(StreamGraph)&lt;/code&gt;执行任务提交流程并等待任务状态返回；&lt;/li&gt;
  &lt;li&gt;在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;StreamExecutionEnvironment.executeAsync&lt;/code&gt;内通过调用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DefaultExecutorServiceLoader.getExecutorFactory&lt;/code&gt;检索&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;jar&lt;/code&gt;s 的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;META-INF.services&lt;/code&gt;目录，加载适合的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ExecutorFactory&lt;/code&gt;（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Java SPI&lt;/code&gt;），当前&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Job&lt;/code&gt;可用的是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;YarnJobClusterExecutorFactory&lt;/code&gt;；&lt;/li&gt;
  &lt;li&gt;通过&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;YarnJobClusterExecutorFactory&lt;/code&gt;获取&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;YarnJobClusterExecutor&lt;/code&gt;，然后执行&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;YarnJobClusterExecutor.execute&lt;/code&gt;：
    &lt;ul&gt;
      &lt;li&gt;调用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PipelineExecutorUtils.getJobGraph&lt;/code&gt;将&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;StreamGraph&lt;/code&gt;转换为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;JobGraph&lt;/code&gt;，转换的重要逻辑在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;StreamingJobGraphGenerator.createJobGraph&lt;/code&gt;内，创建&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;JobGraph&lt;/code&gt;的主要操作有：创建一个包有 32 位随机数的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;JobID&lt;/code&gt;；为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Graph&lt;/code&gt;的每个顶点生成一个全局唯一的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;hash&lt;/code&gt;数（用户可通过&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DataStream.uid&lt;/code&gt;设置）；生成&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;JobVertex&lt;/code&gt;，它是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Flink Task&lt;/code&gt;的上层抽象，包含&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Operators&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;invokableClass&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SlotSharing&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OperatorChaining&lt;/code&gt;等信息，存放在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;JobGraph&lt;/code&gt;的成员变量&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;taskVertices&lt;/code&gt;中；此外还有, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ExecutionConfig&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SavepointConfig&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;JarPath&lt;/code&gt;s, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Classpath&lt;/code&gt;s 等信息；&lt;/li&gt;
      &lt;li&gt;调用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;YarnClusterClientFactory.getClusterSpecification&lt;/code&gt;从&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Configuration&lt;/code&gt;中解析当前提交&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Job&lt;/code&gt;的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;JobManager/TaskManager&lt;/code&gt;的内存信息，用于校验&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Yarn Cluster&lt;/code&gt;是否有足够的资源分配给&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Job&lt;/code&gt;启动；&lt;/li&gt;
      &lt;li&gt;调用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;YarnClusterDescriptor.deployJobCluster&lt;/code&gt;执行具体的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Job&lt;/code&gt;提交流程，返回一个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ClusterClientJobClientAdapter&lt;/code&gt;对象，其内部通过&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RestClusterClient&lt;/code&gt;对象与&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Yarn Cluster&lt;/code&gt;通信，可获取&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Job&lt;/code&gt;状态或是执行一些其它操作；&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;strong&gt;4. 调用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;YarnClusterDescriptor.deployJobCluster&lt;/code&gt;执行&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Job&lt;/code&gt;提交：&lt;/strong&gt;

&lt;ul&gt;
  &lt;li&gt;
    调用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;YarnClusterDescriptor.deployInternal&lt;/code&gt;代码逻辑阻塞直到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;JobManager&lt;/code&gt;提交&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Yarn&lt;/code&gt;完成，逻辑如下：

    &lt;ul&gt;
      &lt;li&gt;执行&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Kerberos&lt;/code&gt;认证（如果有需要）；&lt;/li&gt;
      &lt;li&gt;校验上下文环境&amp;amp;配置是否满足&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Job&lt;/code&gt;提交条件；&lt;/li&gt;
      &lt;li&gt;查看是否存在合适的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;yarn queue&lt;/code&gt;；&lt;/li&gt;
      &lt;li&gt;校验/适配&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ClusterSpecification&lt;/code&gt;（利用集群&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Yarn Container vCores/Memory&lt;/code&gt;的配置）；&lt;/li&gt;
      &lt;li&gt;调用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;YarnClusterDescriptor.startAppMaster&lt;/code&gt;启动&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AppMaster&lt;/code&gt;（下文详解）；&lt;/li&gt;
      &lt;li&gt;将调用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;startAppMaster&lt;/code&gt;返回的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;JobManager&lt;/code&gt;相关信息和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;applicationId&lt;/code&gt;写入&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Configuration&lt;/code&gt;；&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;
    调用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;YarnClusterDescriptor.startAppMaster&lt;/code&gt;：

    &lt;ul&gt;
      &lt;li&gt;从&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Configuration&lt;/code&gt;获取&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FileSystem&lt;/code&gt;配置信息，然后从&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;plugins&lt;/code&gt;目录加载&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;jar&lt;/code&gt;s
初始化&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FileSystem&lt;/code&gt;实例；&lt;/li&gt;
      &lt;li&gt;将&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Zookeeper Namespace&lt;/code&gt;写入&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Configuration&lt;/code&gt;对象，可以通过&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;high-availability.cluster-id&lt;/code&gt;配置，默认是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;applicationId&lt;/code&gt;；&lt;/li&gt;
      &lt;li&gt;创建&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;YarnApplicationFileUploader&lt;/code&gt;对象，然后将&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;log4j.properties&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;logback.xml&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;flink-conf.yaml&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;yarn-site.xml&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UserJars&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SystemJars&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PluginsJars&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;JobGraph序列化&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;kerberos配置/认证&lt;/code&gt;等文件上传到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;hdfs:///user/$username/.flink/$applicationId/&lt;/code&gt;目录下；&lt;/li&gt;
      &lt;li&gt;调用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;JobManagerProcessUtils::processSpecFromConfigWithNewOptionToInterpretLegacyHeap&lt;/code&gt;，从&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Configuration&lt;/code&gt;获取&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Memory&lt;/code&gt;配置并计算出&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;JobManager&lt;/code&gt;所在进程的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Memory&lt;/code&gt;分配数值，最终以&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-Xmx, -Xms, -XX:MaxMetaspaceSize, -XX:MaxDirectMemorySize&lt;/code&gt;形式用到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;JVM&lt;/code&gt;进程启动；此外，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Memory&lt;/code&gt;计算对旧版配置&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FLINK_JM_HEAP, jobmanager.heap.size, jobmanager.heap.mb&lt;/code&gt;做了兼容处理；&lt;/li&gt;
      &lt;li&gt;调用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;YarnClusterDescriptor.setupApplicationMasterContainer&lt;/code&gt;创建&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ContainerLaunchContext&lt;/code&gt;（启动&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Container&lt;/code&gt;所需的信息集合）；&lt;/li&gt;
      &lt;li&gt;将&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ApplicationName&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ContainerLaunchContext&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Resource&lt;/code&gt;（向&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ResourceManager&lt;/code&gt;申请资源）, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CLASSPATH&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Environment Variables&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ApplicationType&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;yarn.application.node-label&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;yarn.tags&lt;/code&gt;等信息封装到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ApplicationSubmissionContext&lt;/code&gt;；此时，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ApplicationSubmissionContext&lt;/code&gt;就封装了&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ResourceManager&lt;/code&gt;启动&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ApplicationMaster&lt;/code&gt;所需的所有信息；&lt;/li&gt;
      &lt;li&gt;为当前线程添加&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;提交任务失败&lt;/code&gt;的回调函数，用于在提交任务失败后&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;kill Application &amp;amp; delete Files that have been uploaded to HDFS&lt;/code&gt;；&lt;/li&gt;
      &lt;li&gt;调用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;YarnClientImpl.submitApplication&lt;/code&gt;将任务提交给&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Yarn Client Api&lt;/code&gt;处理；&lt;/li&gt;
      &lt;li&gt;轮训&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;YarnClientImpl.getApplicationReport&lt;/code&gt;等待提交任务提交成功（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AppMaster&lt;/code&gt;正常启动），最后返回任务状态&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ApplicationReport&lt;/code&gt;；&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;YarnClientImpl.submitApplication&lt;/code&gt;内通过调用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rmClient.submitApplication&lt;/code&gt;向&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Yarn Client&lt;/code&gt;提交&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Job&lt;/code&gt;：&lt;/strong&gt;

&lt;ul&gt;
  &lt;li&gt;成员变量&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;YarnClientImpl.rmClient&lt;/code&gt;通过调用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ConfiguredRMFailoverProxyProvider.getProxy&lt;/code&gt;获取到，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;YarnClientImpl.rmClient&lt;/code&gt;实例是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ApplicationClientProtocolPBClientImpl&lt;/code&gt;的代理对象，其内部通过&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ProtoBuf + RpcEngine&lt;/code&gt;提交任务到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Yarn Server&lt;/code&gt;端；&lt;/li&gt;
  &lt;li&gt;在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Yarn Server&lt;/code&gt;端&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ClientRMService.submitApplication&lt;/code&gt;会收到所有来自&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Yarn Client&lt;/code&gt;的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Job&lt;/code&gt;提交请求，执行后序的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AppMaster&lt;/code&gt;启动操作；&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;server-启动-jobmanager&quot;&gt;[Server] 启动 JobManager&lt;/h3&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;// Server端在Container中启动JobManager的流程&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nl&quot;&gt;YarnJobClusterEntrypoint:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;EnvironmentInformation&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;logEnvironmentInfo&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;SignalHandler&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;register&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;JvmShutdownSafeguard&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;installAsShutdownHook&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;YarnEntrypointUtils&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;logYarnEnvironmentInformation&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;YarnEntrypointUtils&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;loadConfiguration&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nl&quot;&gt;ClusterEntrypoint:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;runClusterEntrypoint&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;YarnJobClusterEntrypoint&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;startCluster&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;PluginUtils&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;createPluginManagerFromRootFolder&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;ClusterEntrypoint&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;configureFileSystems&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;ClusterEntrypoint&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;installSecurityContext&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;ClusterEntrypoint&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;runCluster&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;ClusterEntrypoint&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;initializeServices&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;YarnJobClusterEntrypoint&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;createDispatcherResourceManagerComponentFactory&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;DefaultDispatcherResourceManagerComponentFactory&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;create&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;JobRestEndpointFactory&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;createRestEndpoint&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;MiniDispatcherRestEndpoint&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;start&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;YarnResourceManagerFactory&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;createResourceManager&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;YarnResourceManager&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;start&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;ZooKeeperUtils&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;createLeaderRetrievalService&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;ZooKeeperLeaderRetrievalService&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;start&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;DefaultDispatcherRunnerFactory&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;createDispatcherRunner&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;JobDispatcherLeaderProcessFactoryFactory&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;createFactory&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nl&quot;&gt;DefaultDispatcherRunner:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;create&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nl&quot;&gt;DispatcherRunnerLeaderElectionLifecycleManager:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;createFor&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nl&quot;&gt;DispatcherRunnerLeaderElectionLifecycleManager:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;ZooKeeperLeaderElectionService&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;start&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;ZooKeeperLeaderElectionService&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;isLeader&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;DefaultDispatcherRunner&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;grantLeadership&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;DefaultDispatcherRunner&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;startNewDispatcherLeaderProcess&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;JobDispatcherLeaderProcess&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;start&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;AbstractDispatcherLeaderProcess&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;startInternal&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;JobDispatcherLeaderProcess&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;onStart&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;DefaultDispatcherGatewayServiceFactory&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;create&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;JobDispatcherFactory&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;createDispatcher&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;create&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;AkkaServer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;MiniDispatcher&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;start&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;call&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;AkkaServer&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;to&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;execute&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;job&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;start&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Dispatcher&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;onStart&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;DefaultDispatcherBootstrap&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;initialize&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;AbstractDispatcherBootstrap&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;launchRecoveredJobGraphs&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Dispatcher&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;runRecoveredJob&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Dispatcher&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;runJob&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// 调用链太长，从`Dispatcher.runJob`新建&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Dispatcher&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;runJob&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Dispatcher&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;createJobManagerRunner&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;DefaultJobManagerRunnerFactory&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;createJobManagerRunner&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nl&quot;&gt;JobManagerRunnerImpl:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;DefaultJobMasterServiceFactory&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;createJobMasterService&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nl&quot;&gt;JobMaster:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Dispatcher&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;startJobManagerRunner&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;JobManagerRunnerImpl&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;start&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;ZooKeeperLeaderElectionService&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;start&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;ZooKeeperLeaderElectionService&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;isLeader&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;JobManagerRunnerImpl&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;grantLeadership&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;JobManagerRunnerImpl&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;verifyJobSchedulingStatusAndStartJobManager&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;JobManagerRunnerImpl&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;startJobMaster&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;ZooKeeperRunningJobsRegistry&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;setJobRunning&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;JobMaster&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;start&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;JobMaster&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;startJobExecution&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;JobMaster&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;setNewFencingToken&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;JobMaster&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;startJobMasterServices&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;JobMaster&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;resetAndStartScheduler&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;DefaultJobManagerJobMetricGroupFactory&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;create&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;JobMaster&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;createScheduler&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;DefaultSchedulerFactory&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;createInstance&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nl&quot;&gt;DefaultScheduler:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;JobMaster&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;startScheduling&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;SchedulerBase&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;registerJobStatusListener&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;SchedulerBase&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;startScheduling&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;SchedulerBase&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;registerJobMetrics&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;SchedulerBase&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;startAllOperatorCoordinators&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;DefaultScheduler&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;startSchedulingInternal&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;DefaultScheduler&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;prepareExecutionGraphForNgScheduling&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;EagerSchedulingStrategy&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;startScheduling&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;EagerSchedulingStrategy&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;allocateSlotsAndDeploy&lt;/span&gt;   &lt;span class=&quot;c1&quot;&gt;// stream job use EagerSchedulingStrategy as default Scheduler&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;DefaultScheduler&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;allocateSlotsAndDeploy&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;// explain ExecutionVertexID&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;DefaultScheduler&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;allocateSlots&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;DefaultExecutionSlotAllocator&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;allocateSlotsFor&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;DefaultExecutionSlotAllocator&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;allocateSlot&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;NormalSlotProviderStrategy&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;allocateSlot&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;SchedulerImpl&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;allocateSlot&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;SchedulerImpl&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;allocateSlotInternal&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;SchedulerImpl&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;internalAllocateSlot&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;SchedulerImpl&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;allocateSingleSlot&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;SchedulerImpl&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;requestNewAllocatedSlot&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;SlotPoolImpl&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;requestNewAllocatedBatchSlot&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;SlotPoolImpl&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;requestNewAllocatedSlotInternal&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;SlotPoolImpl&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;requestSlotFromResourceManager&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;ResourceManager&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;requestSlot&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;SlotManagerImpl&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;registerSlotRequest&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;SlotManagerImpl&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;internalRequestSlot&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;SlotManagerImpl&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;fulfillPendingSlotRequestWithPendingTaskManagerSlot&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;SlotManagerImpl&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;allocateResource&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;ResourceActionsImpl&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;allocateResource&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;YarnResourceManager&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;startNewWorker&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;YarnResourceManager&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;requestYarnContainer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;AMRMClientAsyncImpl&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;addContainerRequest&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;DefaultScheduler&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;waitForAllSlotsAndDeploy&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;DefaultScheduler&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;deployAll&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;DefaultScheduler&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;deployOrHandleError&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;DefaultScheduler&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;deployTaskSafe&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;DefaultExecutionVertexOperations&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;deploy&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;ExecutionVertex&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;deploy&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Execution&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;deploy&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;TaskDeploymentDescriptorFactory&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;createDeploymentDescriptor&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;RpcTaskManagerGateway&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;submitTask&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;TaskExecutor&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;submitTask&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;invoke&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;TaskExecutor&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;via&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;RPC&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;which&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;created&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;by&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;calling&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;JobMaster&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;registerTaskManager&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;strong&gt;1. 在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Per-job&lt;/code&gt;模式下，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AppMaster&lt;/code&gt;进程的启动入口是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;YarnJobClusterEntrypoint.main&lt;/code&gt;：&lt;/strong&gt;

&lt;ul&gt;
  &lt;li&gt;调用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EnvironmentInformation::logEnvironmentInfo&lt;/code&gt;打印环境变量信息；&lt;/li&gt;
  &lt;li&gt;调用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SignalHandler::register&lt;/code&gt;注册&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TERM, HUP, INT&lt;/code&gt;等终止/退出信号处理（例如：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;kill&lt;/code&gt;）；&lt;/li&gt;
  &lt;li&gt;从&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;System::getEnv&lt;/code&gt;获取变量&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PWD&lt;/code&gt;，该目录为进程的启动目录，目录下包含进程启动所需的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;jar, config, job.graph, launch_container.sh&lt;/code&gt;等文件（通过&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ln&lt;/code&gt;创建的链接文件），&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PWD&lt;/code&gt;目录位置：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;${yarn.nodemanager.local-dirs}/usercache/hadoop/appcache/${applicationId}/${containerId}&lt;/code&gt;；&lt;/li&gt;
  &lt;li&gt;调用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;YarnEntrypointUtils::logYarnEnvironmentInformation&lt;/code&gt;打印&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Yarn&lt;/code&gt;相关信息；&lt;/li&gt;
  &lt;li&gt;调用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;YarnEntrypointUtils.loadConfiguration&lt;/code&gt;从&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;flink-conf.yaml&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;System.env&lt;/code&gt;构建&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Configuration&lt;/code&gt;对象；&lt;/li&gt;
  &lt;li&gt;创建&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;YarnJobClusterEntrypoint&lt;/code&gt;对象，并为当前进程添加&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;shutDownHook&lt;/code&gt;（在进程退出前进行删除本地文件的操作）；&lt;/li&gt;
  &lt;li&gt;调用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ClusterEntrypoint::runClusterEntrypoint&lt;/code&gt;，函数内部通过调用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;YarnJobClusterEntrypoint.startCluster&lt;/code&gt;做&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AppMaster&lt;/code&gt;启动操作，然后为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;YarnJobClusterEntrypoint&lt;/code&gt;对象注册了监控&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Termination&lt;/code&gt;的回调函数，用于打印进程结束的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;exit code&lt;/code&gt;等信息；&lt;/li&gt;
&lt;/ul&gt;

&lt;strong&gt;2. 调用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;YarnJobClusterEntrypoint.startCluster&lt;/code&gt;，依次执行：&lt;/strong&gt;

&lt;ul&gt;
  &lt;li&gt;调用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PluginUtils::createPluginManagerFromRootFolder&lt;/code&gt;，将&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;plugin&lt;/code&gt;的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;name, jars&lt;/code&gt;填充到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PluginDescriptor&lt;/code&gt;，然后将&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PluginDescriptor&lt;/code&gt;和委托给&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;parent of plugin.classLoader&lt;/code&gt;加载的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;包名列表&lt;/code&gt;封装到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DefaultPluginManager&lt;/code&gt;；&lt;/li&gt;
  &lt;li&gt;调用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ClusterEntrypoint.configureFileSystems&lt;/code&gt;内部通过&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Java SPI&lt;/code&gt;去加载并初始化&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;所有jars&lt;/code&gt;（包括&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;common/plugin jars&lt;/code&gt;）的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;META-INF/services/&lt;/code&gt;目录下的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FileSystemFactory&lt;/code&gt;服务；&lt;/li&gt;
  &lt;li&gt;调用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ClusterEntrypoint.installSecurityContext&lt;/code&gt;，创建&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ZooKeeperModule, HadoopModule, JaasModule&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HadoopSecurityContext&lt;/code&gt;对象并存放在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SecurityUtils&lt;/code&gt;的成员变量，然后通过创建的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HadoopSecurityContext&lt;/code&gt;对象触发执行&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ClusterEntrypoint.runCluster&lt;/code&gt;；从代码阅读来看：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;installSecurityContext&lt;/code&gt;的目的在于向运行环境添加必要的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;用户权限&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;环境变量&lt;/code&gt;配置；&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ClusterEntrypoint.runCluster&lt;/code&gt;执行流程：
    &lt;ul&gt;
      &lt;li&gt;调用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ClusterEntrypoint.initializeServices&lt;/code&gt;初始化多个服务模块；
        &lt;ul&gt;
          &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;commonRpcService&lt;/code&gt;：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AkkaRpc&lt;/code&gt;服务，处理本地和远程的服务调用请求；&lt;/li&gt;
          &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;haServices&lt;/code&gt;：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ZooKeeperHaServices&lt;/code&gt;服务，处理与&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;zookeeper&lt;/code&gt;的交互请求，如&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;JobManager/ResourceManager&lt;/code&gt;等组件的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;leader&lt;/code&gt;选举；&lt;/li&gt;
          &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;blobServer&lt;/code&gt;：处理&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Job BLOB文件&lt;/code&gt;的/上传/下载/清除等操作，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BLOB&lt;/code&gt;包括&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Jar&lt;/code&gt;s，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RPC消息&lt;/code&gt;等内容；&lt;/li&gt;
          &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;heartbeatServices&lt;/code&gt;：管理&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Job&lt;/code&gt;各组件的心跳服务；&lt;/li&gt;
          &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;metricRegistry&lt;/code&gt;：监控指标注册，持有&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Job&lt;/code&gt;各监控指标和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MetricReporter&lt;/code&gt;s 信息；&lt;/li&gt;
          &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;processMetricGroup&lt;/code&gt;：系统/进程监控指标组，包含&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CPU&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Memory&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GC&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ClassLoader&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Network&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Swap&lt;/code&gt;等监控指标；&lt;/li&gt;
          &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;archivedExecutionGraphStore&lt;/code&gt;：用于保存序列化的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ExecutionGraph&lt;/code&gt;？&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;调用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;YarnJobClusterEntrypoint.createDispatcherResourceManagerComponentFactory&lt;/code&gt;创建工厂对象&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DefaultDispatcherResourceManagerComponentFactory&lt;/code&gt;，工厂对象用于创建&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DispatcherResourceManagerComponent&lt;/code&gt;对象，该对象的创建过程是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Job&lt;/code&gt;启动的核心逻辑所在；另外提一下，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FileJobGraphRetriever&lt;/code&gt;将本地文件&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;job.graph&lt;/code&gt;反序列化为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;JobGraph&lt;/code&gt;；&lt;/li&gt;
      &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DefaultDispatcherResourceManagerComponentFactory.create&lt;/code&gt;执行流程：
        &lt;ul&gt;
          &lt;li&gt;创建并启动&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Flink-Web-Dashboard&lt;/code&gt;的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Rest&lt;/code&gt;接口服务&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;WebMonitorEndpoint&lt;/code&gt;；&lt;/li&gt;
          &lt;li&gt;创建并启动&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;YarnResourceManager&lt;/code&gt;以及其内部服务&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SlotManager&lt;/code&gt;，负责&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Job&lt;/code&gt;的资源管理，如：申请/释放/记录；&lt;/li&gt;
          &lt;li&gt;创建并启动&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MiniDispatcher&lt;/code&gt;，在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Dispatcher&lt;/code&gt;启动过程中会创建&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;JobManager&lt;/code&gt;线程完成任务的启动；&lt;/li&gt;
          &lt;li&gt;向 ZooKeeper 注册&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ResourceManager&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Dispatcher&lt;/code&gt;的监听协调服务，用于服务的容错恢复，通过在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Zookeeper&lt;/code&gt;中保持了一个锁文件来协调服务；&lt;/li&gt;
          &lt;li&gt;上述&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;WebMonitorEndpoint, YarnResourceManager, MiniDispatcher&lt;/code&gt;三个服务在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ZooKeeper&lt;/code&gt;下保存着各自的相关信息，通过&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ZooKeeperHaServices&lt;/code&gt;保证服务的高可用；三个服务的启动过程是通过&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AkkaRPC + 状态机&lt;/code&gt;的设计模式实现的，在各服务对象创建过程中注册生成&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AkkaServer&lt;/code&gt;，在服务启动过程中通过&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AkkaRPC&lt;/code&gt;调用不同&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;状态机函数&lt;/code&gt;，最后回调&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;onStart&lt;/code&gt;执行实际的启动逻辑，完整的启动逻辑较为复杂，需耐心翻阅；状态机默认状态是：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;StoppedState.STOPPED&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AkkaServer&lt;/code&gt;创建逻辑在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AkkaRpcService.startServer&lt;/code&gt;内，另外，在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AkkaRpcService.startServer&lt;/code&gt;内通过调用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AkkaRpcService.registerAkkaRpcActor&lt;/code&gt;创建&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AkkaRpcActor&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AkkaRpcActor&lt;/code&gt;为具体接收消息并执行&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;状态机&lt;/code&gt;逻辑的入口，接收消息的处理逻辑在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AbstractActor.createReceive&lt;/code&gt;内；&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;strong&gt;3. 调用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Dispatcher.runJob&lt;/code&gt;进入&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;JobManager&lt;/code&gt;启动流程：&lt;/strong&gt;

&lt;ul&gt;
  &lt;li&gt;调用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Dispatcher.createJobManagerRunner&lt;/code&gt;创建&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;JobManagerRunnerImpl&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;JobMaster&lt;/code&gt;对象，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;JobMaster&lt;/code&gt;代表了一个运行的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;JobGraph&lt;/code&gt;，在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;JobMaster::new&lt;/code&gt;过程中创建了&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RpcServer&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DefaultScheduler&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SchedulerImpl&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SlotPoolImpl&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ExecutionGraph&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CheckpointCoordinator&lt;/code&gt;等重要对象，创建&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ExecutionGraph&lt;/code&gt;入口：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SchedulerBase.createExecutionGraph&lt;/code&gt;；&lt;/li&gt;
  &lt;li&gt;调用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Dispatcher.startJobManagerRunner&lt;/code&gt;执行整个任务的提交流程：启动&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;JobManager&lt;/code&gt;端各服务模块，申请&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Slot&lt;/code&gt;资源启动&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TaskManager&lt;/code&gt;，提交/执行&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Task&lt;/code&gt;等流程；&lt;/li&gt;
&lt;/ul&gt;

&lt;strong&gt;4. 从调用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;JobManagerRunnerImpl.start&lt;/code&gt;开始到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DefaultScheduler.allocateSlotsAndDeploy&lt;/code&gt;结束&lt;/strong&gt;

&lt;ul&gt;
  &lt;li&gt;在这段代码调用区间内主要做了一些服务的启动，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Job&lt;/code&gt;状态的变更和监听服务的添加，为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;JobManager&lt;/code&gt;添加监控指标；代码较简单，在此不一一说明；&lt;/li&gt;
&lt;/ul&gt;

&lt;strong&gt;5. 调用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DefaultScheduler.allocateSlots&lt;/code&gt;进入资源申请流程：&lt;/strong&gt;
// TODO
find slot from existing resource, if don‘t find request resourceManager for a new resource
AMRMClientAsyncImpl.CallbackHandlerThread // get Resource that have been allocated and call &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;YarnResourceManager.onContainersAllocated&lt;/code&gt; to process TaskManager
AMRMClientAsyncImpl.HeartbeatThread // invoke &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AMRMClientImpl.allocate&lt;/code&gt; periodically, ResourceManager will be called to allocate Resource
YarnResourceManager.requestYarnContainer // put request of allocate Container to member variable &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AMRMClientImpl.ask&lt;/code&gt;

&lt;strong&gt;6. 调用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DefaultScheduler.waitForAllSlotsAndDeploy&lt;/code&gt;进入&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Task&lt;/code&gt;发布流程&lt;/strong&gt;

// TODO

获取到的 container 资源存放在纳贡，以供给后序 slot 分配；
start TaskManager and register it to JobManager

&lt;h3 id=&quot;server-启动-taskmanager&quot;&gt;[Server] 启动 TaskManager&lt;/h3&gt;

// TODO

&lt;ol&gt;
  &lt;li&gt;YarnResourceManager.onContainersAllocated&lt;/li&gt;
  &lt;li&gt;org.apache.flink.yarn.YarnTaskExecutorRunner&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;内存计算&quot;&gt;内存计算&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;start command for TaskManager -&amp;gt; TaskExecutorProcessUtils.processSpecFromWorkerResourceSpec&lt;/li&gt;
  &lt;li&gt;resource request for TaskManager -&amp;gt; WorkerSpecContainerResourceAdapter.createAndMapContainerResource&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;参考文档&quot;&gt;参考文档&lt;/h2&gt;
</description>
        <pubDate>Fri, 26 Jun 2020 00:00:00 +0000</pubDate>
        <link>https://itinycheng.github.io/2020/06/26/flink-streaming-on-yarn-startup-process/</link>
        <guid isPermaLink="true">https://itinycheng.github.io/2020/06/26/flink-streaming-on-yarn-startup-process/</guid>
        
        <category>flink</category>
        
        <category>源码</category>
        
        
        <category>flink</category>
        
      </item>
    
      <item>
        <title>产品线迁移私有云的问题汇总</title>
        <description>&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#概述&quot; id=&quot;markdown-toc-概述&quot;&gt;概述&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#说明&quot; id=&quot;markdown-toc-说明&quot;&gt;说明&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#问题记录&quot; id=&quot;markdown-toc-问题记录&quot;&gt;问题记录&lt;/a&gt;    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#dubbo-253-升级到-276&quot; id=&quot;markdown-toc-dubbo-253-升级到-276&quot;&gt;Dubbo 2.5.3 升级到 2.7.6&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#hive-013-升级-121311&quot; id=&quot;markdown-toc-hive-013-升级-121311&quot;&gt;Hive 0.13 升级 1.2.1/3.1.1&lt;/a&gt;        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#upgrade-to-121&quot; id=&quot;markdown-toc-upgrade-to-121&quot;&gt;&lt;strong&gt;Upgrade to 1.2.1&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#upgrade-to-311&quot; id=&quot;markdown-toc-upgrade-to-311&quot;&gt;&lt;strong&gt;Upgrade to 3.1.1&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#kafka-offset-不提交导致的数据重复消费&quot; id=&quot;markdown-toc-kafka-offset-不提交导致的数据重复消费&quot;&gt;Kafka Offset 不提交导致的数据重复消费&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#jdk7-升级到-jdk8&quot; id=&quot;markdown-toc-jdk7-升级到-jdk8&quot;&gt;JDK7 升级到 JDK8&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#其他问题&quot; id=&quot;markdown-toc-其他问题&quot;&gt;其他问题&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;概述&quot;&gt;概述&lt;/h2&gt;

公司领导在 19 年底确定将所有服务迁移到某云服务，我所在团队负责的两条产品线需在六月前完成迁移；这两条产品线承载着公司 80%数据收集和报表计算&amp;amp;查询服务，指标较多且系统复杂，迁移压力略大；从 20 年初开始制定迁移方案，进行实际的迁移操作，整个迁移在团队 4 个人的全职投入和在 DBA、基础组件同事的协助下最终在 5 月中旬完成迁移。本文目的在于简单记录下自己在迁移过程中所遇到的一些技术问题。

&lt;h2 id=&quot;说明&quot;&gt;说明&lt;/h2&gt;

两条产品线主要功能有数据收集，实时数据处理，离线数据处理，指标查询四类，涉及到的需迁移的技术组件如下：

&lt;ul&gt;
  &lt;li&gt;数据收集：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Collector（自研，Go 实现）&lt;/code&gt;；&lt;/li&gt;
  &lt;li&gt;实时处理：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Kafka&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Flink&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Etl-formwork（自研 Pipeline）&lt;/code&gt;；&lt;/li&gt;
  &lt;li&gt;离线处理：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Crontab + Hive + Python2&lt;/code&gt;；&lt;/li&gt;
  &lt;li&gt;存储服务：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Druid&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Hadoop&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Cassandra&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Mongo&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ES&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Mysql&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Redis&lt;/code&gt;；&lt;/li&gt;
  &lt;li&gt;其它服务：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Zookeeper&lt;/code&gt;,&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Jetty&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Dubbo&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Grafana&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Prometheus&lt;/code&gt;；&lt;/li&gt;
  &lt;li&gt;此外还有些相关组件不在本文关注范围内，例如：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Nginx&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VIP&lt;/code&gt;等；&lt;/li&gt;
&lt;/ul&gt;

在整个迁移过程中自己主要负责：实时/离线计算任务的迁移，实时/历史数据同步，指标核对工具的开发，保证迁移后各指标的准确无误；需迁移技术组件包括：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Kafka&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Flink&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Zookeeper&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Dubbo&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Crontab + Hive + Python&lt;/code&gt;；

&lt;h2 id=&quot;问题记录&quot;&gt;问题记录&lt;/h2&gt;

&lt;h3 id=&quot;dubbo-253-升级到-276&quot;&gt;Dubbo 2.5.3 升级到 2.7.6&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;
    对&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Dubbo&lt;/code&gt;进行升级操作的原因：新环境中的调用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Dubbo&lt;/code&gt;服务耗时较长，导致实时数据处理出现淤积的情况，在 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Github&lt;/code&gt;找到一个类似的&lt;a href=&quot;https://github.com/apache/dubbo/issues/5217&quot;&gt;issue&lt;/a&gt;，随后对&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Dubbo&lt;/code&gt;进行了升级操作，问题得以解决；
  &lt;/li&gt;
  &lt;li&gt;
    &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Dubbo 2.7.6&lt;/code&gt;基本可以向后兼容性&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;2.5.3&lt;/code&gt;，只需升级&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Dubbo&lt;/code&gt;依赖版本，添加&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Curator&lt;/code&gt;依赖，然后将用户代码引入的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Dubbo&lt;/code&gt;类的路径变更为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;com.apache&lt;/code&gt;即可，如：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;com.alibaba.dubbo.config.ApplicationConfig&lt;/code&gt;变更为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;org.apache.dubbo.config.ReferenceConfig&lt;/code&gt;；
  &lt;/li&gt;
  &lt;li&gt;
    服务升级后发现在运行日志中有如下报错：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ERROR org.apache.dubbo.qos.server.Server - [DUBBO] qos-server can not bind localhost:22222&lt;/code&gt;，直接在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Dubbo&lt;/code&gt;配置中关闭&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;QOS&lt;/code&gt;服务即可；
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;hive-013-升级-121311&quot;&gt;Hive 0.13 升级 1.2.1/3.1.1&lt;/h3&gt;

您没看错，确实是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Hive 0.13&lt;/code&gt;，且&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Hive SQL&lt;/code&gt;提交到一个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Hadoop 0.20&lt;/code&gt;的集群上~#.#～；这些批脚本对应的是一条产品线的离线指标计算的业务，这些脚本包含：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Shell&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Python&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Hive SQL&lt;/code&gt;等脚本，并依赖了一个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Mongo&lt;/code&gt;库，一个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Jetty&lt;/code&gt;服务，三个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Mysql&lt;/code&gt;库，八个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;User Jar&lt;/code&gt;，几十个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UDF&lt;/code&gt;；本次迁移必须对&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Hadoop/Hive&lt;/code&gt;做升级，且将脚本的业务逻辑梳理清楚；这些批脚本的迁移大概用了 1 个月的时间；

&lt;h4 id=&quot;upgrade-to-121&quot;&gt;&lt;strong&gt;Upgrade to 1.2.1&lt;/strong&gt;&lt;/h4&gt;

从&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0.13&lt;/code&gt;升级到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1.2.1&lt;/code&gt;过程十分顺利，在自己定义的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HiveCli&lt;/code&gt; 初始化脚本中添加一行配置（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;set hive.support.sql11.reserved.keywords=false;&lt;/code&gt;）即可；主要是因为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Hive&lt;/code&gt;从&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1.2.0&lt;/code&gt;版本开始，根据&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SQL2011&lt;/code&gt;标准增加了大量&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;reserved keyword&lt;/code&gt;，通过这个配置可以保证包含&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;reserved keyword&lt;/code&gt;的 SQL 可正常执行；参考：

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://issues.apache.org/jira/browse/HIVE-6617&quot;&gt;https://issues.apache.org/jira/browse/HIVE-6617 &lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://cwiki.apache.org/confluence/display/Hive/LanguageManual+DDL&quot;&gt;https://cwiki.apache.org/confluence/display/Hive/LanguageManual+DDL&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;upgrade-to-311&quot;&gt;&lt;strong&gt;Upgrade to 3.1.1&lt;/strong&gt;&lt;/h4&gt;

最初迁移时是计划合并多个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Hadoop&lt;/code&gt;集群到公司公用的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Hadoop 3.1.0&lt;/code&gt;集群，对应的 Hive 版本也升级到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3.1.1&lt;/code&gt;，但最终因牵动项目多（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Druid&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Etl-Framework&lt;/code&gt;），升级风险高而放弃；在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Hive&lt;/code&gt;的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0.13&lt;/code&gt;版本升级到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3.1.1&lt;/code&gt;的测试阶段也解决了多个 SQL 迁移问题，在此做下记录。

&lt;ul&gt;
  &lt;li&gt;
    &lt;strong&gt;hive.support.sql11.reserved.keywords 配置不可用&lt;/strong&gt;
在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Hive 2.3&lt;/code&gt;之后配置&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;set hive.support.sql11.reserved.keywords=false;&lt;/code&gt;已经被移除，所以在 SQL 中有用到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;reserved keyword&lt;/code&gt;做标识符时需要放在反引号（``）内，以消除歧义；社区 issue: &lt;a href=&quot;https://issues.apache.org/jira/browse/HIVE-14872&quot;&gt;https://issues.apache.org/jira/browse/HIVE-14872&lt;/a&gt;。
  &lt;/li&gt;
  &lt;li&gt;
    &lt;strong&gt;Union 两张字段类型不一致的表&lt;/strong&gt;
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Hive 3.1.1&lt;/code&gt;环境下当&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;union all&lt;/code&gt;两个字段类型不一致的列时，会收到如下报错信息：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FAILED: SemanticException 17:71 Schema of both sides of union should match: Column user_offset is of type string on first table and type int on second table. Error encountered near token 'charge_device'&lt;/code&gt;；看起来是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Hive3&lt;/code&gt;对&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;union all&lt;/code&gt;两侧的数据集进行了类型一致性校验；对类型不一致的列进行&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CAST(user_offset as int)&lt;/code&gt;修改后 SQL 可正常执行；
  &lt;/li&gt;
  &lt;li&gt;
    &lt;strong&gt;hive.strict.checks.type.safety&lt;/strong&gt;
严格类型安全检查，默认是 true，该属性不允许 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bigint 和 string 间的比较&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bigint 和 double 间的比较&lt;/code&gt;；将属性设置为 false，可以解除不允许上述两种不同类型间的比较的限制，在 SQL 的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Where&lt;/code&gt;条件中经常会出现这种类型不一致的条件比较。
  &lt;/li&gt;
  &lt;li&gt;
    &lt;strong&gt;SQL 执行耗时增加：&lt;/strong&gt;
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Hive 3.1.1&lt;/code&gt;环境下发现个 SQL 执行特别慢，而同样 SQL 在 Hive0.13 都可以很快执行完成；经过进一步测试发现在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Hive3&lt;/code&gt; 下，查询同一张表的不同字段（如下文提供的 SQL），性能差别很大，在单表数据量&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;40W+&lt;/code&gt;的测试条件下，查询某个字段要用 2 个多小时，而查询其他字段只需&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3～5分钟&lt;/code&gt;；同样数据集同样的 SQL 以相同并行度在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Hive 0.13&lt;/code&gt;下执行，查询时长都可保持在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3~5分钟&lt;/code&gt;；在修改表数据存储格式（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;STORED AS PARQUET&lt;/code&gt;）后查询慢的问题可以解决，通过&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;hive --debug&lt;/code&gt;进行&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;远程Debug&lt;/code&gt;发现&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Hive3&lt;/code&gt;对数据反序列化阶段变代码变化较大，但没找到问题根源，先留下个问题和数据，以后再结合&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;JMX&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Arthas&lt;/code&gt;这些工具看个究竟吧，示例：
  &lt;/li&gt;
&lt;/ul&gt;

&lt;pre&gt;&lt;code class=&quot;language-HIVE&quot;&gt;-- 表结构：
CREATE TABLE `tmp_logout`(
  `user` map&amp;lt;string,string&amp;gt; COMMENT 'user_info',
  `device` map&amp;lt;string,string&amp;gt; COMMENT 'device_info',
  `app` map&amp;lt;string,string&amp;gt; COMMENT 'app_info',
  `event` struct&amp;lt;eventType:string,attribute:map&amp;lt;string,string&amp;gt;,eventDatas:array&amp;lt;struct&amp;lt;key:string,value:string,type:string&amp;gt;&amp;gt;&amp;gt; COMMENT 'event_info')
PARTITIONED BY (`job_time` bigint, `timezone` int)
STORED AS TEXTFILE;

-- sql_1：执行速度快
insert overwrite directory '/user/hadoop/output/tmp_logout'
select app['product_id'] as pid from tmp_logout where job_time=20200420120000 and app['product_id'] is not NULL;

--sql_2：刚开始执行较快，但随时间增加执行越来越慢，最终执行完需要2H+，GC时间逐渐增加
insert overwrite directory '/user/hadoop/output/logout'
 select event.attribute['event_time'] as event_time from tmp_logout where job_time=20200420120000 and app['product_id'] is not NULL;
&lt;/code&gt;&lt;/pre&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;其他&lt;/strong&gt;
尝试将执行引擎从&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MR&lt;/code&gt;切换到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Spark&lt;/code&gt;上时，也发现了很多 SQL 执行报错的问题，比如：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Hive UDAF&lt;/code&gt; 执行报错；在 Hive 配置&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;set hive.groupby.skewindata=true;&lt;/code&gt;的情况下，有些&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;group by&lt;/code&gt;的 SQL 的执行报错（经&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Calcite&lt;/code&gt;优化后的执行计划不一样），等等；总之，需调整的的地方还是挺多的。&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;kafka-offset-不提交导致的数据重复消费&quot;&gt;Kafka Offset 不提交导致的数据重复消费&lt;/h2&gt;

用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Flink&lt;/code&gt;写了个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Kafka To Kafka&lt;/code&gt;数据拷贝工具，由于迁移前期两个机房之间的网络不太稳定，为防止数据拷贝任务频繁重启，给&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Flink Job&lt;/code&gt;添加了 checkpoint 失败不重启的配置：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;env.getCheckpointConfig.setFailOnCheckpointingErrors(false)&lt;/code&gt;，这个配置导致了数据重复消费的问题；

&lt;strong&gt;现象：&lt;/strong&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Checkpoint&lt;/code&gt;失败持续了 6 个小时之久且没一次成功；查看日志发现每次 checkpoint 时候都会有如下报错信息：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;java.lang.IllegalStateException: Correlation id for response (204658) does not match request (204657)&lt;/code&gt;；&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FlinkKafkaConsumer09&lt;/code&gt;的 Offset 提交模式是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OffsetCommitMode.ON_CHECKPOINTS&lt;/code&gt;；查看&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Kafka-Manager&lt;/code&gt;的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Consumer Offset&lt;/code&gt;监控，发现一直没变化；重启 Job 发现 6 小时内的数据出现重复消费的问题。

&lt;strong&gt;解决：&lt;/strong&gt; 在社区看到一个相同的问题：&lt;a href=&quot;https://issues.apache.org/jira/browse/KAFKA-4669&quot;&gt;https://issues.apache.org/jira/browse/KAFKA-4669&lt;/a&gt;；后来关闭了测试数据拷贝并将流量较大的几个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Topic&lt;/code&gt;切到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;kafka-mirror-maker.sh&lt;/code&gt;上，问题得以暂时解决；当前 BUG 并未根除，当前问题也先遗留下来后序再看看源码吧。

&lt;h2 id=&quot;jdk7-升级到-jdk8&quot;&gt;JDK7 升级到 JDK8&lt;/h2&gt;

在升级 JDK 版本后，业务代码调用 ES 接口时报错，相关信息如下：

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;
&lt;span class=&quot;c1&quot;&gt;// 调用 ES API 的用户代码&lt;/span&gt;
&lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;valueOf&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hits&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;field&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;collFields&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]).&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;getValue&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;());&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// SearchHitField.java 被调用的方法&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;V&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;V&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;getValue&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// 报错信息：java.lang.ClassCastException: java.lang.Object cannot be cast to [C&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// 用户代码反编译结果：&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// String value = String.valueOf((char[]) hits[0].field(collFields[i]).getValue());&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// 正确写法&lt;/span&gt;
&lt;span class=&quot;nc&quot;&gt;Object&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hits&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;field&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;collFields&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]).&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;getValue&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;valueOf&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

从上述信息我们可以判断，当前这个报错是因为：在编过程中，用户代码上下文没有足够的信息给到编译器进行泛型类型的推断；贴一个文章作为学习参考：&lt;a href=&quot;http://lovestblog.cn/blog/2016/04/03/type-inference/?from=groupmessage&quot;&gt;http://lovestblog.cn/blog/2016/04/03/type-inference/?from=groupmessage&lt;/a&gt;；

&lt;h2 id=&quot;其他问题&quot;&gt;其他问题&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;
    公司机房有高 IO 需求的服务如：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Druid Historical, Kafka, Cassandra, ES, Mysql, etc.&lt;/code&gt; 都是用的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;12块HDD做Raid5&lt;/code&gt;；而到云服务是单独的磁盘阵列，能加多块盘，但没办法做&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Raid5&lt;/code&gt;，单块磁盘读写上限限制到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;80M/s&lt;/code&gt;，此外：听说磁盘和主机是分别存放并通过光纤链接到一起的；这使得前期压测花费了不少时间在磁盘性能测试上，在整个迁移过程中也做了几次的磁盘参数调整，或是更换高性能 SSD 的工作；
  &lt;/li&gt;
  &lt;li&gt;
    &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Mysql&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Redis&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Cassandra&lt;/code&gt;这些服务是通过建立跨机房集群来完成实时数据迁移的，对跨机房的网络稳定性要求较高，期间由于大量历史数据跨机房拷贝导致了网络堵塞，这影响到了这些跨机房服务的正常运行；
  &lt;/li&gt;
  &lt;li&gt;
    &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Druid&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Hadoop&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ES&lt;/code&gt;等服务是通过在云上新装集群，然后通过&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;distcp&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;elasticdump&lt;/code&gt;命令将历史数据跨机房拷贝到云服务上的；
  &lt;/li&gt;
  &lt;li&gt;
    刚开始时，机房间的通信用是一根 10G 光纤，所有团队服务都走这一根光纤，互相会有影响，后又拉了根 5G 光纤；
  &lt;/li&gt;
  &lt;li&gt;
    &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Kafka&lt;/code&gt;集群在负载不高的情况下，上游数据写入出现淤积的情况，运维同事帮忙调整了下网卡参数（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ethtool -L eth0 combined 2&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ethtool -l eth0&lt;/code&gt;）并重启机器后，淤积情况有所缓解；
  &lt;/li&gt;
  &lt;li&gt;
    &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Kafka 0.8.1&lt;/code&gt;版本问题较多，迁移到了&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Kafka 0.9&lt;/code&gt;集群，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Kafka0.8.1&lt;/code&gt;遇到的问题有：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;recovery threads&lt;/code&gt;只有一个线程且不能配置；删除&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Topic&lt;/code&gt;会导致&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Broker&lt;/code&gt;的报错日志激增，必须手动清楚&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ZK&lt;/code&gt;上的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Delete Topic&lt;/code&gt;信息，并重启所有&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Broker&lt;/code&gt;；&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;auto.leader.rebalance.enable=false&lt;/code&gt;是默认配置，会导致&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Broker&lt;/code&gt;压力不均衡，需修改为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;true&lt;/code&gt;；
  &lt;/li&gt;
&lt;/ul&gt;
</description>
        <pubDate>Sun, 24 May 2020 00:00:00 +0000</pubDate>
        <link>https://itinycheng.github.io/2020/05/24/migrate-app-and-game-product-lines-to-cloud/</link>
        <guid isPermaLink="true">https://itinycheng.github.io/2020/05/24/migrate-app-and-game-product-lines-to-cloud/</guid>
        
        <category>迁移</category>
        
        <category>机房</category>
        
        <category>云服务</category>
        
        
        <category>flink</category>
        
      </item>
    
      <item>
        <title>flink日常问题记录</title>
        <description>&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#intro&quot; id=&quot;markdown-toc-intro&quot;&gt;Intro&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#issue-checkpoint-failed&quot; id=&quot;markdown-toc-issue-checkpoint-failed&quot;&gt;issue: Checkpoint Failed&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;intro&quot;&gt;Intro&lt;/h2&gt;
在Flink的日常使用过程中总会遇到一些值得记录的问题，有些问题复杂度不高，不必要用单独的篇幅记录，就将这些问题都汇总到当前这篇文内。

&lt;h2 id=&quot;issue-checkpoint-failed&quot;&gt;issue: Checkpoint Failed&lt;/h2&gt;

&lt;strong&gt;环境：&lt;/strong&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Flink 1.7.1&lt;/code&gt; + &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Hadoop 3.1.0（新IDC机房）&lt;/code&gt; + &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Kafka 0.9（云机房）&lt;/code&gt;

&lt;strong&gt;背景：&lt;/strong&gt; 公司决定将现有机房迁移到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;某云+新的IDC机房&lt;/code&gt;；&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Hadoop 3.1.0&lt;/code&gt;公共大集群，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Kafka 0.9&lt;/code&gt;部署在团队自己的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;云主机&lt;/code&gt;上；

&lt;strong&gt;Job描述：&lt;/strong&gt; 实时ETL，Flink读取Kafka数据经过处理后写入ES或Kafka等存储，State Operator为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FlinkKafkaConsumer&lt;/code&gt;，checkpoint state为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;kafka consumer offset&lt;/code&gt;，每个state大小在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;7～10K&lt;/code&gt;之间；

&lt;strong&gt;异常信息：&lt;/strong&gt;
&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;mi&quot;&gt;2020&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mo&quot;&gt;01&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mo&quot;&gt;02&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;17&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;18&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;53&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;337&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;INFO&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;org&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;apache&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;flink&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;runtime&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;executiongraph&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;ExecutionGraph&lt;/span&gt;        &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;nl&quot;&gt;Source:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;analytics_standarddata_weapp_share&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;druid&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;XXXXXXXXXX&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c2e77749126a67db62d38bdb166b5696&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;switched&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;RUNNING&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;to&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;FAILED&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;
&lt;span class=&quot;nc&quot;&gt;AsynchronousException&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;lang&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;Exception&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Could&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;materialize&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;checkpoint&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;60&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;operator&lt;/span&gt; &lt;span class=&quot;nl&quot;&gt;Source:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;analytics_standarddata_weapp_share&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;druid&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;XXXXXXXXXX&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;).}&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;at&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;org&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;apache&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;flink&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;streaming&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;runtime&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;tasks&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;StreamTask&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;$AsyncCheckpointExceptionHandler&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;tryHandleCheckpointException&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;StreamTask&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1153&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;at&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;org&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;apache&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;flink&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;streaming&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;runtime&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;tasks&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;StreamTask&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;$AsyncCheckpointRunnable&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;handleExecutionException&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;StreamTask&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;947&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;at&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;org&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;apache&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;flink&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;streaming&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;runtime&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;tasks&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;StreamTask&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;$AsyncCheckpointRunnable&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;StreamTask&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;884&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;at&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;util&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;concurrent&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;Executors&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;$RunnableAdapter&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;call&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Executors&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;511&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;at&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;util&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;concurrent&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;FutureTask&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;FutureTask&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;266&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;at&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;util&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;concurrent&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;ThreadPoolExecutor&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;runWorker&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;ThreadPoolExecutor&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1149&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;at&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;util&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;concurrent&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;ThreadPoolExecutor&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;$Worker&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;ThreadPoolExecutor&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;624&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;at&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;lang&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;Thread&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Thread&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;748&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;nc&quot;&gt;Caused&lt;/span&gt; &lt;span class=&quot;nl&quot;&gt;by:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;lang&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;Exception&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Could&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;materialize&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;checkpoint&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;60&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;operator&lt;/span&gt; &lt;span class=&quot;nl&quot;&gt;Source:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;analytics_standarddata_weapp_share&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;druid&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;XXXXXXXXXX&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;).&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;at&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;org&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;apache&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;flink&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;streaming&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;runtime&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;tasks&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;StreamTask&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;$AsyncCheckpointRunnable&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;handleExecutionException&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;StreamTask&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;942&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;more&lt;/span&gt;
&lt;span class=&quot;nc&quot;&gt;Caused&lt;/span&gt; &lt;span class=&quot;nl&quot;&gt;by:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;util&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;concurrent&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;ExecutionException&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;io&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;IOException&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Could&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;flush&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;and&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;close&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;the&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;file&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;system&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;output&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stream&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;to&lt;/span&gt; &lt;span class=&quot;nl&quot;&gt;hdfs:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;saas&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;app&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;flink&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;checkpoints&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a3bc71ac57f33961637adf340e9c28f&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;chk&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;60&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;253&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fc668&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;9241&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;41&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c7&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;994&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;e72aa7281ef&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;order&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;to&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;obtain&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;the&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stream&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;state&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;handle&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;at&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;util&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;concurrent&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;FutureTask&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;report&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;FutureTask&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;122&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;at&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;util&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;concurrent&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;FutureTask&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;FutureTask&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;192&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;at&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;org&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;apache&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;flink&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;util&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;FutureUtil&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;runIfNotDoneAndGet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;FutureUtil&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;53&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;at&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;org&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;apache&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;flink&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;streaming&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;api&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;operators&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;OperatorSnapshotFinalizer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;init&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;OperatorSnapshotFinalizer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;53&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;at&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;org&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;apache&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;flink&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;streaming&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;runtime&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;tasks&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;StreamTask&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;$AsyncCheckpointRunnable&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;StreamTask&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;853&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;more&lt;/span&gt;
&lt;span class=&quot;nc&quot;&gt;Caused&lt;/span&gt; &lt;span class=&quot;nl&quot;&gt;by:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;io&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;IOException&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Could&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;flush&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;and&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;close&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;the&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;file&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;system&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;output&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stream&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;to&lt;/span&gt; &lt;span class=&quot;nl&quot;&gt;hdfs:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;saas&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;app&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;flink&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;checkpoints&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a3bc71ac57f33961637adf340e9c28f&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;chk&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;60&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;253&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fc668&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;9241&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;41&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c7&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;994&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;e72aa7281ef&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;order&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;to&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;obtain&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;the&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stream&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;state&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;handle&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;at&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;org&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;apache&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;flink&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;runtime&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;filesystem&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;FsCheckpointStreamFactory&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;$FsCheckpointStateOutputStream&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;closeAndGetHandle&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;FsCheckpointStreamFactory&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;326&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;at&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;org&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;apache&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;flink&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;runtime&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;DefaultOperatorStateBackend&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;$DefaultOperatorStateBackendSnapshotStrategy&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;callInternal&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;DefaultOperatorStateBackend&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;767&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;at&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;org&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;apache&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;flink&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;runtime&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;DefaultOperatorStateBackend&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;$DefaultOperatorStateBackendSnapshotStrategy&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;callInternal&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;DefaultOperatorStateBackend&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;696&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;at&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;org&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;apache&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;flink&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;runtime&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;AsyncSnapshotCallable&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;call&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;AsyncSnapshotCallable&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;76&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;at&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;util&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;concurrent&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;FutureTask&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;FutureTask&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;266&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;at&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;org&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;apache&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;flink&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;util&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;FutureUtil&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;runIfNotDoneAndGet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;FutureUtil&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;50&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;more&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;......&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;strong&gt;异常说明：&lt;/strong&gt;
每隔&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10～30min&lt;/code&gt;就会出现一次&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;state checkpoint failed.&lt;/code&gt;，进而导致Job重启（checkpoint默认策略，可配置），&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Job Log&lt;/code&gt;中发现上述错误信息；

&lt;strong&gt;异常排查：&lt;/strong&gt;
&lt;ul&gt;
  &lt;li&gt;起初怀疑是机房间的网络不稳定（Kafka和Flink在同一个城市内的不同机房～～），虽然有20G的专线；找运维同事协助测试的网络延迟属正常，延迟都在2～3ms之间（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;icmp_seq=105 ttl=58 time=2.25 ms  11:09:05&lt;/code&gt;），没有网络波动出现；&lt;/li&gt;
  &lt;li&gt;然后怀疑是Hdfs问题，因checkpoint主要做了两个事情：跨机房获取&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;consumed offset&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;write state to HDFS&lt;/code&gt;；&lt;/li&gt;
  &lt;li&gt;重新check错误日志，找到异常在Flink框架内中最早发生位置，即：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FsCheckpointStreamFactory$FsCheckpointStateOutputStream.closeAndGetHandle(...)&lt;/code&gt;，阅读源码，了解具体操作；&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FsCheckpointStateOutputStream.closeAndGetHandle(...)&lt;/code&gt;操作很简单，就是将state从byte[]写入到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HDFS File&lt;/code&gt;，中间没有多余的操作；&lt;/li&gt;
  &lt;li&gt;计划统计从刚进入&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FsCheckpointStateOutputStream.closeAndGetHandle(...)&lt;/code&gt;到抛出异常的总耗时，用来定位是否存在写HDFS慢的情况，进而导致整个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;checkpoint failed&lt;/code&gt;；&lt;/li&gt;
  &lt;li&gt;修改源码，在方法开始执行/抛出异常时记录&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;system timestamp&lt;/code&gt;，在异常信息上添加这些打点的timestamp信息；&lt;/li&gt;
  &lt;li&gt;重新编译源码：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cd flink-runtime/&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mvn clean compile&lt;/code&gt;，得到编译好的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FsCheckpointStreamFactory$FsCheckpointStateOutputStream.class&lt;/code&gt;文件；&lt;/li&gt;
  &lt;li&gt;用新编译的.class文件替换flink-dist_2.11-1.7.1.jar中的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FsCheckpointStreamFactory$FsCheckpointStateOutputStream.class&lt;/code&gt;，操作指南：
    &lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# step 1：将flink-dist_2.11-1.7.1.jar中的文件解压出来：&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; jar &lt;span class=&quot;nt&quot;&gt;-xvf&lt;/span&gt; flink-dist_2.11-1.7.1.jar org/apache/flink/runtime/state/filesystem/FsCheckpointStreamFactory&lt;span class=&quot;se&quot;&gt;\$&lt;/span&gt;FsCheckpointStateOutputStream.class
&lt;span class=&quot;c&quot;&gt;# step 2：用新的.class覆盖刚解压出来的&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;cp&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-f&lt;/span&gt; &amp;lt;new_class_file&amp;gt; org/apache/flink/runtime/state/filesystem/
&lt;span class=&quot;c&quot;&gt;# step 3：将新的.class文件加入到flink-dist_2.11-1.7.1.jar&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; jar &lt;span class=&quot;nt&quot;&gt;-uvf&lt;/span&gt; flink-dist_2.11-1.7.1.jar org/apache/flink/runtime/state/filesystem/FsCheckpointStreamFactory&lt;span class=&quot;se&quot;&gt;\$&lt;/span&gt;FsCheckpointStateOutputStream.class
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;
  &lt;/li&gt;
  &lt;li&gt;重新提交程序，当出现当前异常时查看错误日志，如下：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Could not flush and close the file system output stream to hdfs:/saas/app/flink/checkpoints/cbf5c27f63a7a4724538f3fd9f2ef551/chk-363/01fc22d3-f24b-40c1-9728-cc50dc2e14d2 in order to obtain the stream state handle, enter timestamp:1578025396026, failed timestamp:1578025433855, duration: 37829&lt;/code&gt;；方法总执行时间为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;37_829ms&lt;/code&gt;，远超过checkpoint timeout设置&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10_000ms&lt;/code&gt;；到此，可以初步判定checkpoint异常和写HDFS耗时过长有关，但并不清楚每隔&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10～30min&lt;/code&gt;就出现一次写HDFS慢的原因；&lt;/li&gt;
  &lt;li&gt;与熟悉Hadoop同事沟通这个问题，最终发现是因为HDFS周期性的做&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Directory Scan&lt;/code&gt;耗时太长造成的，HDFS Log：
&lt;img src=&quot;/images/posts/flink/hdfs-log-1.png&quot; alt=&quot;hdfs directory scan&quot; /&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DirectoryScanner&lt;/code&gt;属于一个Hadoop版本缺陷，具体可查看jira：https://issues.apache.org/jira/browse/HDFS-14476&lt;/li&gt;
  &lt;li&gt;到此，问题已明确，下一步的修复方案也就可以拟定咯，比如：增大hdfs-core.xml中的scan interval配置&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dfs.datanode.directoryscan.interval&lt;/code&gt;，修改job checkpoint相关配置（setCheckpointTimeout/setFailOnCheckpointingErrors/etc.），然而这些都不治本，最好还是Hadoop打补丁，但又没那么快；&lt;/li&gt;
  &lt;li&gt;最后，公共Hadoop集群有&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;2E&lt;/code&gt;个文件，略微有点多惹～～～&lt;/li&gt;
&lt;/ul&gt;
</description>
        <pubDate>Sun, 05 Jan 2020 00:00:00 +0000</pubDate>
        <link>https://itinycheng.github.io/2020/01/05/flink-issues-encountered/</link>
        <guid isPermaLink="true">https://itinycheng.github.io/2020/01/05/flink-issues-encountered/</guid>
        
        <category>flink</category>
        
        <category>issue</category>
        
        
        <category>flink</category>
        
      </item>
    
      <item>
        <title>一个简单的Flink Job，一次复杂的问题排查</title>
        <description>&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#概括&quot; id=&quot;markdown-toc-概括&quot;&gt;概括&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#问题描述&quot; id=&quot;markdown-toc-问题描述&quot;&gt;问题描述&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#问题分析&quot; id=&quot;markdown-toc-问题分析&quot;&gt;问题分析&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#问题遗留思考&quot; id=&quot;markdown-toc-问题遗留思考&quot;&gt;问题遗留&amp;amp;思考&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#总结&quot; id=&quot;markdown-toc-总结&quot;&gt;总结&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#reference&quot; id=&quot;markdown-toc-reference&quot;&gt;Reference&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;概括&quot;&gt;概括&lt;/h2&gt;

本文主要是记录一个非常简单的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Flink Job&lt;/code&gt;在从Standalone迁移OnYarn时所遇到的一个因内存占用超出限制而引发的Container频繁被Yarn Kill的问题。问题的解决过程主要经历了：Flink监控指标分析，GC日志的排查，TaskManger内存分析，Container的内存计算方法，栈内存的分析等内容；

&lt;h2 id=&quot;问题描述&quot;&gt;问题描述&lt;/h2&gt;

&lt;strong&gt;起因：&lt;/strong&gt; 希望将&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Flink Standalone&lt;/code&gt;上一个简单的Job迁移到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Flink On Yarn&lt;/code&gt;，迁移前的版本为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Flink 1.3.2&lt;/code&gt;，迁移目标版本为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Flink 1.7.1 + Hadoop 3.1.0&lt;/code&gt;；

&lt;strong&gt;Job描述：&lt;/strong&gt;

&lt;ul&gt;
  &lt;li&gt;有10个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Kafka Topic&lt;/code&gt;，每个Topic的Partition数为21；&lt;/li&gt;
  &lt;li&gt;有10个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ES Index&lt;/code&gt;，每个Index对应一个Topic；&lt;/li&gt;
  &lt;li&gt;Job消费Topic数据，经过Filter将结果写入对应的Index；&lt;/li&gt;
  &lt;li&gt;任务提交脚本：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;flink run -m yarn-cluster -yn 11 -ys 2 -ytm 6g -ynm original-data-filter-prod -d -yq XXX.jar 21&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;Job逻辑视图如下：
    &lt;pre&gt;&lt;code class=&quot;language-mermaid&quot;&gt;graph LR
A(Source: KafkaSource) --&amp;gt; B(Filter: Operator)
B --&amp;gt; C(Sink: Es Index)
&lt;/code&gt;&lt;/pre&gt;
  &lt;/li&gt;
  &lt;li&gt;图中&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;source/filter/sink&lt;/code&gt;的并行度一致，以保证&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Operator Chain&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;由于所有Operator都chain在一起，则运行的总Task数量为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10 Topic * 21 parallelism = 210&lt;/code&gt;，在考虑到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Slot Sharing&lt;/code&gt;情况下每个Container内运行的Task数为：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10个20，1个10&lt;/code&gt;；&lt;/li&gt;
  &lt;li&gt;执行计划：
&lt;img src=&quot;/images/posts/flink/execution-graph-1.png&quot; alt=&quot;execution graph 1&quot; /&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;strong&gt;异常描述：&lt;/strong&gt;

Job正常启动后TaskManager所在的Container每3～5min会被Yarn Kill掉，然后ApplicationMaster会重新向Yarn申请一个新的Container以启动之前被Kill掉的，整个Job会陷入&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Kill Container&lt;/code&gt;/&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Apply For New Container&lt;/code&gt;/&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Run Task/ Kill Container...&lt;/code&gt;的循环，在Flink和Yarn的日志里都会发现如下错误信息：

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;mi&quot;&gt;2019&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;11&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;25&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;12&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;41&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;138&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;INFO&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;org&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;apache&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;flink&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;yarn&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;YarnResourceManager&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Closing&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;TaskExecutor&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;connection&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;container_e03_1559725928417_0577_01_001065&lt;/span&gt; &lt;span class=&quot;nl&quot;&gt;because:&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2019&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;11&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;25&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;12&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;36.159&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Container&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pid&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;97191&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;containerID&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;container_e03_1559725928417_0577_01_001065&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;running&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;168853504&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;B&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;beyond&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;the&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;PHYSICAL&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;'&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;memory&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;limit&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Current&lt;/span&gt; &lt;span class=&quot;nl&quot;&gt;usage:&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;6.2&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;GB&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;of&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;GB&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;physical&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;memory&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;used&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;9.8&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;GB&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;of&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;60&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;GB&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;virtual&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;memory&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;used&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Killing&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;container&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;
&lt;span class=&quot;nc&quot;&gt;Dump&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;of&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;the&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;process&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tree&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;container_e03_1559725928417_0577_01_001065&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;|-&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;PID&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;PPID&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;PGRPID&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;SESSID&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;CMD_NAME&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;USER_MODE_TIME&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;MILLIS&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;SYSTEM_TIME&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;MILLIS&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;VMEM_USAGE&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;BYTES&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;RSSMEM_USAGE&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;PAGES&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;FULL_CMD_LINE&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;|-&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;97215&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;97191&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;97191&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;97191&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;87864&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3747&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10359721984&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1613717&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;usr&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;default&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bin&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;java&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Xms4425m&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Xmx4425m&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;XX:&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;MaxDirectMemorySize&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1719&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;XX:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;UseG1GC&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;XX:&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;MaxGCPauseMillis&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;XX:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;ParallelRefProcEnabled&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;XX:&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;ErrorFile&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;home&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hadoop&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;apache&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hadoop&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;latest&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;logs&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;userlogs&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;application_1559725928417_0577&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;container_e03_1559725928417_0577_01_001065&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hs_err_pid&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;log&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;Xloggc:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;home&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hadoop&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;apache&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hadoop&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;latest&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;logs&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;userlogs&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;application_1559725928417_0577&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;container_e03_1559725928417_0577_01_001065&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gc&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;log&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;XX:&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;HeapDumpPath&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;home&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hadoop&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;apache&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hadoop&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;latest&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;logs&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;userlogs&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;application_1559725928417_0577&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;container_e03_1559725928417_0577_01_001065&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;XX:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;PrintGCDetails&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;XX:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;PrintGCDateStamps&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;XX:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;HeapDumpOnOutOfMemoryError&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Dlog&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;file&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;home&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hadoop&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;apache&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hadoop&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;latest&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;logs&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;userlogs&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;application_1559725928417_0577&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;container_e03_1559725928417_0577_01_001065&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;taskmanager&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;log&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Dlogback&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;configurationFile&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;file:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;./&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;logback&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;xml&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Dlog4j&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;configuration&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;file:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;./&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;log4j&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;properties&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;org&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;apache&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;flink&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;yarn&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;YarnTaskExecutorRunner&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;--&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;configDir&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;|-&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;97191&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;97189&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;97191&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;97191&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bash&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;118067200&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;371&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bin&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bash&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;usr&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;default&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bin&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;java&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Xms4425m&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Xmx4425m&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;XX:&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;MaxDirectMemorySize&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1719&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;XX:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;UseG1GC&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;XX:&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;MaxGCPauseMillis&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;XX:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;ParallelRefProcEnabled&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;XX:&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;ErrorFile&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;home&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hadoop&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;apache&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hadoop&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;latest&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;logs&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;userlogs&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;application_1559725928417_0577&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;container_e03_1559725928417_0577_01_001065&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hs_err_pid&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;log&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;Xloggc:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;home&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hadoop&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;apache&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hadoop&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;latest&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;logs&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;userlogs&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;application_1559725928417_0577&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;container_e03_1559725928417_0577_01_001065&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gc&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;log&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;XX:&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;HeapDumpPath&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;home&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hadoop&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;apache&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hadoop&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;latest&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;logs&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;userlogs&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;application_1559725928417_0577&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;container_e03_1559725928417_0577_01_001065&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;XX:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;PrintGCDetails&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;XX:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;PrintGCDateStamps&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;XX:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;HeapDumpOnOutOfMemoryError&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Dlog&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;file&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;home&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hadoop&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;apache&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hadoop&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;latest&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;logs&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;userlogs&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;application_1559725928417_0577&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;container_e03_1559725928417_0577_01_001065&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;taskmanager&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;log&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Dlogback&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;configurationFile&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;file:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;./&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;logback&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;xml&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Dlog4j&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;configuration&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;file:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;./&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;log4j&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;properties&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;org&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;apache&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;flink&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;yarn&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;YarnTaskExecutorRunner&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;--&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;configDir&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;home&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hadoop&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;apache&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hadoop&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;latest&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;logs&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;userlogs&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;application_1559725928417_0577&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;container_e03_1559725928417_0577_01_001065&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;taskmanager&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;out&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;home&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hadoop&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;apache&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hadoop&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;latest&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;logs&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;userlogs&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;application_1559725928417_0577&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;container_e03_1559725928417_0577_01_001065&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;taskmanager&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;err&lt;/span&gt;

&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2019&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;11&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;25&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;12&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;36.175&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Container&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;killed&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;on&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;request&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Exit&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;code&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;143&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2019&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;11&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;25&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;12&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;38.293&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Container&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;exited&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;non&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;zero&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;exit&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;code&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;143&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;问题分析&quot;&gt;问题分析&lt;/h2&gt;

&lt;strong&gt;看到上述异常信息，第一直觉是heap或off-heap的Memory占用过大，应该先分析下Container的内存占用情况。&lt;/strong&gt;

&lt;em&gt;信息：&lt;/em&gt;
&lt;ul&gt;
  &lt;li&gt;Container进程启动使用的Memory配置：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-Xms4425m -Xmx4425m -XX:MaxDirectMemorySize=1719m&lt;/code&gt;；&lt;/li&gt;
  &lt;li&gt;生产环境中与Memory相关的Flink配置：
    &lt;div class=&quot;language-yml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# defined configuration&lt;/span&gt;
&lt;span class=&quot;s&quot;&gt;taskmanager.heap.size&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;2048m&lt;/span&gt;   &lt;span class=&quot;c1&quot;&gt;# 默认为2G，当前Job中被覆盖为6G，指定Container Memory(heap+off-heap)；&lt;/span&gt;
&lt;span class=&quot;s&quot;&gt;taskmanager.memory.preallocate&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;true&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# Whether pre-allocated memory of taskManager managed&lt;/span&gt;
&lt;span class=&quot;s&quot;&gt;containerized.heap-cutoff-min&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;900&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# 安全边界，从Container移除的最小 Heap Memory Size&lt;/span&gt;
&lt;span class=&quot;s&quot;&gt;containerized.heap-cutoff-ratio&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;0.2&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# 移除的Heap Memory的比例，用于计算Container进程的heap和max off-heap的大小，max off-heap越大留给Flink之外服务的Memory越多，这些空间一般被用作Container，Socket通信或是一些堆外Cache等；&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Flink Web Dashboard&lt;/code&gt;中heap/off-heap的监控如下图：
&lt;img src=&quot;/images/posts/flink/memory-metric-1.png&quot; alt=&quot;execution graph 1&quot; /&gt;&lt;/li&gt;
  &lt;li&gt;被Kill的Container最后一次GC Log：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[Eden: 642.0M(642.0M)-&amp;gt;0.0B(676.0M) Survivors: 32.0M-&amp;gt;26.0M Heap: 3893.9M(4426.0M)-&amp;gt;3245.6M(4426.0M)]&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;em&gt;分析：&lt;/em&gt;
通过&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Flink Log&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Web Dashborad&lt;/code&gt;查看到上述四项有关Container启动/运行时的信息，发现heap和off-heap都在合理的范围（有足够的空闲Memory），但heap占用的Memory一直在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3.8G&lt;/code&gt;左右，感觉上这个值属正常，但仍有侥幸心理：先降低heap占用试下。这时做了一次Flink配置修改：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;taskmanager.memory.preallocate: false&lt;/code&gt;，这个配置主要是针对TaskManager Managed Memory的，与之相关的还有&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;taskmanager.memory.off-heap&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;taskmanager.memory.fraction&lt;/code&gt;等，详细说明见：&lt;a href=&quot;https://ci.apache.org/projects/flink/flink-docs-release-1.9/ops/config.html#taskmanager&quot;&gt;Flink Configuration Doc&lt;/a&gt;。

修改配置后重启Job，正常运行了几分钟后问题又再次出现，Container被Kill前最后一次GC Log：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[Eden: 192.0M(192.0M)-&amp;gt;0.0B(194.0M) Survivors: 28.0M-&amp;gt;26.0M Heap: 2629.3M(4426.0M)-&amp;gt;250.2M(4426.0M)]&lt;/code&gt;，从&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Web Dashboard&lt;/code&gt;中观察到的最大&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;heap used&lt;/code&gt;是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3.7G&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;off-heap used&lt;/code&gt;是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1.3G&lt;/code&gt;。

&lt;em&gt;思考：&lt;/em&gt;
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;heap/off-heap memory&lt;/code&gt;在任意时间的实际使用量都在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-Xmx4425m -XX:MaxDirectMemorySize=1719m&lt;/code&gt;范围内（而且有足够的空闲空间）变化，同时&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Xmx + MaxDirectMemorySize = 6G&lt;/code&gt;，所以任何时间的Memory使用：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;heap + off-heap &amp;lt; 6G&lt;/code&gt;；另一方面：若&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;heap + off-heap &amp;gt; 6G&lt;/code&gt;，则应该抛出OOM的异常，所以判定当前问题与Heap/Off-heap的实际占用没有直接关系。接下来有两个疑问：1. Container Memory的计算逻辑是什么，使得计算结果出现大于6G的情况？2. 一个JVM进程包括：Heap, Off-heap,  Non-heap, Stack等区域，前三个在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Web Dashboard&lt;/code&gt;可以看到并且内存总和不大于6G，而Stack内存占用并不能直接观察到，当前问题是否由Stack占用的Memory过大而引起？

&lt;strong&gt;现在我们不但可以确定当前问题与Heap/Off-heap没有直接的因果关系，而且下一步的分析方向可以明确了。&lt;/strong&gt;

&lt;strong&gt;1. Container Memory的计算逻辑：&lt;/strong&gt;

&lt;ul&gt;
  &lt;li&gt;Hadoop NodeManager有一个Monitor线程，它负责监控Container使用的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Physical Memory&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Virtual Memory&lt;/code&gt;是否超出限制，默认检查周期：3s，代码逻辑可查看：MonitoringThread。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Container Memory&lt;/code&gt;的内存量计算有&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ProcfsBasedProcessTree&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CombinedResourceCalculator&lt;/code&gt;两种实现，默认情况下使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ProcfsBasedProcessTree&lt;/code&gt;。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ProcfsBasedProcessTree&lt;/code&gt;通过&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Page数量 * Page大小&lt;/code&gt;来计算&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Physical Memory&lt;/code&gt;的使用量，其中Page数量从文件&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/proc/&amp;lt;pid&amp;gt;/stat&lt;/code&gt;解析获得，单Page大小通知执行shell命令&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;getconfg PAGESIZE&lt;/code&gt;获取，代码截图如下：
&lt;img src=&quot;/images/posts/flink/yarn-container-2.png&quot; alt=&quot;execution graph 1&quot; /&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;strong&gt;2. Stack Memory的分配区域：&lt;/strong&gt;

由于JVM功底并不深，初听&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Stack Memory&lt;/code&gt;具体在哪个区域分配，还真不能准确回答～～。&lt;a href=&quot;https://docs.oracle.com/javase/specs/jvms/se12/html/jvms-2.html#jvms-2.5.2&quot;&gt;JVM规范定义&lt;/a&gt;：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Each Java Virtual Machine thread has a private Java Virtual Machine stack, created at the same time as the thread. A Java Virtual Machine stack stores frames (§2.6). A Java Virtual Machine stack is analogous to the stack of a conventional language such as C: it holds local variables and partial results, and plays a part in method invocation and return. Because the Java Virtual Machine stack is never manipulated directly except to push and pop frames, frames may be heap allocated. The memory for a Java Virtual Machine stack does not need to be contiguous.&lt;/code&gt;。这段定义并没对Stack Memory的分配做明确定义，只有两句线索：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;frames may be heap allocated&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;stack does not need to be contiguous&lt;/code&gt;，各JVM产品可对&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Stack Memory&lt;/code&gt;的分配做各自的灵活实现。对自己来说C++暂时是座难翻越的山，也不必要那么兴师动众～～～，直接上测试Code看吧：

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;cm&quot;&gt;/**
 * jvm env: Oracle JDK-8
 * jvm conf:
 * -Xmx200m -Xms200m -Xss5m -XX:MaxDirectMemorySize=10m -XX:NativeMemoryTracking=detail
 */&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;nc&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Thread&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;list&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;ArrayList&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10000&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;500&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;--&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;nc&quot;&gt;Thread&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;thread&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Thread&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;method&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;));&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;thread&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;start&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;thread&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;forEach&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;thread&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;try&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;thread&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;join&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
            &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;catch&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;InterruptedException&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;printStackTrace&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
            &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;});&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;method&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;loop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1024&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;16&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;loop&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;method&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(++&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;try&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;nc&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
                &lt;span class=&quot;nc&quot;&gt;Thread&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;sleep&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;60&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;60&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
            &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;catch&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;InterruptedException&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;printStackTrace&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
            &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

查看测试DEMO的内存占用，执行Command：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;jcmd &amp;lt;pid&amp;gt; VM.native_memory scale=MB&lt;/code&gt;结果如下：
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Native Memory Tracking:

Total: reserved=4028MB, committed=2766MB
-                 Java Heap (reserved=100MB, committed=100MB)
                            (mmap: reserved=100MB, committed=100MB)

-                     Class (reserved=1032MB, committed=5MB)
                            (classes #860)
                            (  instance classes #748, array classes #112)
                            (mmap: reserved=1032MB, committed=5MB)
                            (  Metadata:   )
                            (    reserved=8MB, committed=4MB)
                            (    used=1MB)
                            (    free=4MB)
                            (    waste=0MB =0.00%)
                            (  Class space:)
                            (    reserved=1024MB, committed=1MB)
                            (    used=0MB)
                            (    free=0MB)
                            (    waste=0MB =0.00%)

-                    Thread (reserved=2575MB, committed=2575MB)
                            (thread #529)
                            (stack: reserved=2573MB, committed=2573MB)
                            (malloc=2MB #2663)
                            (arena=1MB #1057)

-                      Code (reserved=242MB, committed=7MB)
                            (mmap: reserved=242MB, committed=7MB)

-                        GC (reserved=56MB, committed=56MB)
                            (malloc=21MB #1143)
                            (mmap: reserved=36MB, committed=36MB)

-                  Internal (reserved=3MB, committed=3MB)
                            (malloc=3MB #15173)

-                    Symbol (reserved=1MB, committed=1MB)
                            (malloc=1MB #1352)

-    Native Memory Tracking (reserved=1MB, committed=1MB)

-        Shared class space (reserved=17MB, committed=17MB)
                            (mmap: reserved=17MB, committed=17MB)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;strong&gt;结论：&lt;/strong&gt; 从&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Native Memory Tracking&lt;/code&gt;可以看到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Thread Stack Memory&lt;/code&gt;占用了&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;2573MB&lt;/code&gt;，而&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;heap/off-heap&lt;/code&gt;的Memory占用非常低；所以，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Stack Memory&lt;/code&gt;的分配是在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;heap/off-heap&lt;/code&gt;之外的区域（虽然暂时不完全了解Stack Memory的分配细节）。&lt;strong&gt;此时，我们可以合理怀疑当前问题是由&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Stack Memory&lt;/code&gt;占用过多引起的。&lt;/strong&gt;

补充一张测试DEMO的Memory监控（非必须），可以简单的关注下Heap/GC情况：
&lt;img src=&quot;/images/posts/flink/stack-memory-1.png&quot; alt=&quot;execution graph 1&quot; /&gt;

&lt;strong&gt;Container的实际线程情况：&lt;/strong&gt;
从VisualVM的线程监控中可以看到一个Container进程内有&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1734个Live Thread&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1622个Daemon Thread&lt;/code&gt;，对于一个只有&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;2C6G&lt;/code&gt;的进程来讲，线程数太多，不仅会影响到Memory使用，也会给GC/线程上下文切换带来更大的压力；在所有活跃线程里有&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1200+&lt;/code&gt;个是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;es transport client&lt;/code&gt;，占总活跃线程的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;70%+&lt;/code&gt;，如下图：
&lt;img src=&quot;/images/posts/flink/container-threads-1.png&quot; alt=&quot;container jmx es&quot; /&gt;

&lt;strong&gt;到此，可得出解题方案，即：&lt;/strong&gt; 减少Container内的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ES Sink&lt;/code&gt;实例个数以达到降低&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;es transport client&lt;/code&gt;线程数量的目的。将&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ES Sink&lt;/code&gt;的并行度由21调整为2（写数据到ES速度大概为1200+条/s，2个并行度可以满足需求），重启Job后运行正常，当前问题得到完美解决，调整后的执行计划如下图：
&lt;img src=&quot;/images/posts/flink/execution-graph-2.png&quot; alt=&quot;execution graph 2&quot; /&gt;

&lt;h2 id=&quot;问题遗留思考&quot;&gt;问题遗留&amp;amp;思考&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;em&gt;内存组成：&lt;/em&gt; 除去上述提到的内存使用外还有socket send/receive buffer也占用了一部分空间。
  &lt;/li&gt;
  &lt;li&gt;
    &lt;em&gt;快速失败：&lt;/em&gt; 当前Yarn或Flink中没有可用的配置来指定&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Kill Container&lt;/code&gt;或&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;allocate Container&lt;/code&gt;的最大次数，以达到在超过某个限制时候，让Job快速失败的目的；Flink中有&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;yarn.maximum-failed-containers&lt;/code&gt;配置，当并不适用当前场景；还是需要自己写脚本来完成类似的功能；贴一个反复申请Container的截图：
&lt;img src=&quot;/images/posts/flink/yarn-container-1.png&quot; alt=&quot;yarn container 1&quot; /&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;em&gt;胡思乱想：&lt;/em&gt; Flink的Task之间不能共享Operator，Operator也不会有单例的实现，唯一存在的是并行度和Task分配运行策略；若当前问题中一个Container进程内多个Task之间可用共享一个单例的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ES Sink&lt;/code&gt;多好～～～，这样就可以少一层物理机间的数据传递。
  &lt;/li&gt;
  &lt;li&gt;
    &lt;em&gt;注意小心：&lt;/em&gt; Slot Sharing是个很好的特性，可以很大程度提高资源利用率，但也需小心，不要让单个Slot内的Task数量过多。
  &lt;/li&gt;
  &lt;li&gt;
    &lt;em&gt;另一种选择：&lt;/em&gt; 其实，我们可以通过增大&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;containerized.heap-cutoff-ratio&lt;/code&gt;到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0.4&lt;/code&gt;或是更高来解决当前问题（预留足够多的Memory给到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Stack/Container/Socket&lt;/code&gt;使用），但这样势必会减少Heap的大小，考虑到Task内不断增多的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Local Cache&lt;/code&gt;，所以并未做此调整；另一方面，通过JMX观测到GC的CPU占用超过20%且抖动厉害，只增大&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MaxDirectMemorySize&lt;/code&gt;是解决不了这个GC问题的，而通过减少线程数可以很好减少GCRoot，降低线程切换频率以降低GC压力；
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;总结&quot;&gt;总结&lt;/h2&gt;

起初对Job的设计思考中是想尽量减少资源占用，最极端的办法就是将数据读取/处理/写出的整个流程放在同一个JVM进程中，这样Operator间的数据传递就可以在进程内部完成而减少网络间的数据传递；由此想到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Chain All Operators Together&lt;/code&gt;，这样就必须把&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;source/filter/sink&lt;/code&gt;的并行度设置为相同的值；然而，这就让每个Task都持有一个Sink实例，同时因为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Slot Sharing&lt;/code&gt;特性的存在，每个Container内部会运行多个Task，这就导致单个Container中存在多个Sink实例，特别是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Es Sink&lt;/code&gt;这种较为重的Operator（内部维护的线程/缓存/状态较多）；&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Es Sink&lt;/code&gt;实例对象的增加导致了线程数的成倍增加，所有线程持有的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Stack Memory&lt;/code&gt;总和也成倍增加，用于运行&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Job Task&lt;/code&gt;的Memory数量进一步减少，另一方面：每个Container所持有的2个CPU资源也很难支撑太多数量的线程高效运行。

所以，在了解到上述内容后，首先需要做的事情是减少Container内的线程数（降低&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Stack Memory&lt;/code&gt;占用，减少频繁的线程上下文切换），减少线程办法就是减少&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ES Sink&lt;/code&gt;的数量（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;source/filter&lt;/code&gt;线程少可忽略不计），从得出了当前的解决方案；

最后，将&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Es Sink&lt;/code&gt;并行度调整为2后问题得到彻底解决，至今Job已平稳运行了半个多月；

&lt;em&gt;注：&lt;/em&gt; JVM很重要，清晰定位问题很重要；在最初的问题排查过程中，自己总试图从&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;heap/off-heap&lt;/code&gt;占用寻找答案，甚至还将&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;heap/off-heap&lt;/code&gt;占用的Memory进行拆分加和以计算是否存在使用过量的情况，耗费了不少精力。

&lt;h2 id=&quot;reference&quot;&gt;Reference&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;https://ci.apache.org/projects/flink/flink-docs-master/ops/config.html&lt;/li&gt;
  &lt;li&gt;https://docs.oracle.com/javase/specs/jvms/se7/html/jvms-2.html#jvms-2.5.2&lt;/li&gt;
  &lt;li&gt;http://blog.jamesdbloom.com/JVMInternals.html#stack&lt;/li&gt;
  &lt;li&gt;https://stackoverflow.com/questions/41120129/java-stack-and-heap-memory-management&lt;/li&gt;
  &lt;li&gt;https://dzone.com/articles/troubleshoot-outofmemoryerror-unable-to-create-new&lt;/li&gt;
  &lt;li&gt;https://stackoverflow.com/questions/36946455/stack-heap-in-jvm&lt;/li&gt;
  &lt;li&gt;https://www.baeldung.com/java-stack-heap&lt;/li&gt;
  &lt;li&gt;https://www.maolintu.com/2018/02/03/jvm-stack-vs-heap-vs-method-area/&lt;/li&gt;
  &lt;li&gt;https://gribblelab.org/CBootCamp/7_Memory_Stack_vs_Heap.html&lt;/li&gt;
  &lt;li&gt;https://docs.oracle.com/en/java/javase/13/vm/native-memory-tracking.html&lt;/li&gt;
  &lt;li&gt;http://coding-geek.com/jvm-memory-model/&lt;/li&gt;
&lt;/ul&gt;
</description>
        <pubDate>Sun, 08 Dec 2019 00:00:00 +0000</pubDate>
        <link>https://itinycheng.github.io/2019/12/08/flink-es-job's-task-always-killed-by-yarn/</link>
        <guid isPermaLink="true">https://itinycheng.github.io/2019/12/08/flink-es-job's-task-always-killed-by-yarn/</guid>
        
        <category>flink</category>
        
        <category>elasticsearch</category>
        
        <category>yarn</category>
        
        
        <category>flink</category>
        
      </item>
    
      <item>
        <title>Flink Slot详解与Job Execution Graph优化</title>
        <description>&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#intro&quot; id=&quot;markdown-toc-intro&quot;&gt;Intro&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#flink-slot&quot; id=&quot;markdown-toc-flink-slot&quot;&gt;Flink Slot&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#operator-chain&quot; id=&quot;markdown-toc-operator-chain&quot;&gt;Operator Chain&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#slot-sharing&quot; id=&quot;markdown-toc-slot-sharing&quot;&gt;Slot Sharing&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#operator-chain--slot-sharing-api&quot; id=&quot;markdown-toc-operator-chain--slot-sharing-api&quot;&gt;Operator Chain &amp;amp; Slot Sharing API&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#迁移onyarn后job性能下降的问题&quot; id=&quot;markdown-toc-迁移onyarn后job性能下降的问题&quot;&gt;迁移OnYarn后Job性能下降的问题&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#summary&quot; id=&quot;markdown-toc-summary&quot;&gt;Summary&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#reference&quot; id=&quot;markdown-toc-reference&quot;&gt;Reference&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;intro&quot;&gt;Intro&lt;/h2&gt;

近期公司内部的Flink Job从Standalone迁移OnYarn时发现Job性能较之前有所降低，迁移前有8.3W+/S的数据消费速度，迁移到Yarn后分配同样的资源但消费速度降为7.8W+/S，且较之前的消费速度有轻微的抖动；经过简单的原因分析和测试验证得以最终解决，方案概述：在保持分配给Job的资源不变的情况下将总Container数量减半，每个Container持有的资源从&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1C2G 1Slot&lt;/code&gt;变更为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;2C4G 2Slot&lt;/code&gt;。本文写作的起因是经历该问题后发现深入理解Slot和Flink Runtime Graph是十分必要的；本文主要分为两个部分，第一部分详细的分析Flink Slot与Job运行关系，第二部详细的说下遇到的问题和解决方案。

&lt;h2 id=&quot;flink-slot&quot;&gt;Flink Slot&lt;/h2&gt;

Flink集群是由JobManager(JM), TaskManager(TM)两大组件组成的，每个JM/TM都是运行在一个独立的JVM进程中，JM相当于Master 是集群的管理节点，TM相当于Worker 是集群的工作节点，每个TM最少持有1个Slot，Slot是Flink执行Job时的最小资源分配单位，在Slot中运行着具体的Task任务。对TM而言：它占用着一定数量的CPU和Memory资源，具体可通过&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;taskmanager.numberOfTaskSlots&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;taskmanager.heap.size&lt;/code&gt;来配置，实际上&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;taskmanager.numberOfTaskSlots&lt;/code&gt;只是指定TM的Slot数量并不能隔离指定数量的CPU给TM使用，在不考虑Slot Sharing(下文详述)的情况下一个Slot内运行着一个SubTask(Task实现Runable，SubTask是一个执行Task的具体实例)，所以官方建议&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;taskmanager.numberOfTaskSlots&lt;/code&gt;配置的Slot数量和CPU相等或成比例；当然，我们可以借助Yarn等调度系统，用Flink On Yarn的模式来为Yarn Container分配指定数量的CPU资源以达到较严格的CPU隔离（Yarn采用Cgroup做基于时间片的资源调度，每个Container内运行着一个JM/TM实例）；&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;taskmanager.heap.size&lt;/code&gt;用来配置TM的Memory，如果一个TM有N个Slot，则每个Slot分配到的Memory大小为整个TM Memory的1/N，同一个TM内的Slots只有Memory隔离，CPU是共享的；对Job而言：一个Job所需的Slot数量大于等于Operator配置的最大Parallelism数，在保持所有Operator的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;slotSharingGroup&lt;/code&gt;一致的前提下Job所需的Slot数量与Job中Operator配置的最大Parallelism相等。

关于TM/Slot之间的关系可以参考如下从官方文档截取到的三张图：

&lt;strong&gt;图一：&lt;/strong&gt; Flink On Yarn的Job提交过程，从图中我们可以了解到每个JM/TM实例都分属于不同的Yarn Container，且每个Container内只会有一个JM或TM实例；通过对Yarn的学习我们可以了解到每个Container都是一个独立的进程，一台物理机可以有多个Container存在（多个进程），每个Container都持有一定数量的CPU和Memory资源，而且是资源隔离的，进程间不共享，这就可以保证同一台机器上的多个TM之间是资源隔离的（Standalone模式下同一台机器下若有多个TM是做不到TM之间的CPU资源隔离）。
&lt;img src=&quot;/images/posts/flink/flink_on_yarn.svg&quot; alt=&quot;flink on yarn&quot; /&gt;

&lt;strong&gt;图二：&lt;/strong&gt; Flink Job运行图，图中有两个TM，各自有3个Slot，2个Slot内有Task在执行，1个Slot空闲；若这两个TM在不同Container或容器上则其占用的CPU和Memory是相互隔离的；TM内多个Slot间是各自拥有 1/3 TM的Memory，共享TM的CPU，网络（Tcp：ZK, Akka, Netty服务等），心跳信息，Flink结构化的数据集等。
&lt;img src=&quot;/images/posts/flink/processes.svg&quot; alt=&quot;processes&quot; /&gt;

&lt;strong&gt;图三：&lt;/strong&gt; Task Slot的内部结构图，Slot内运行着具体的Task，它是在线程中执行的Runable对象（每个虚线框代表一个线程），这些Task实例在源码中对应的类是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;org.apache.flink.runtime.taskmanager.Task&lt;/code&gt;；每个Task都是由一组Operators Chaining在一起的工作集合，Flink Job的执行过程可看作一张DAG图，Task是DAG图上的顶点（Vertex），顶点之间通过数据传递方式相互链接构成整个Job的Execution Graph。
&lt;img src=&quot;/images/posts/flink/tasks_slots.svg&quot; alt=&quot;processes&quot; /&gt;

&lt;h2 id=&quot;operator-chain&quot;&gt;Operator Chain&lt;/h2&gt;
Operator Chain是指将Job中的Operators按照一定策略（例如：single output operator可以chain在一起）链接起来并放置在一个Task线程中执行；Operator Chain默认开启，可通过&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;StreamExecutionEnvironment.disableOperatorChaining()&lt;/code&gt;关闭，Flink Operator类似Storm中的Bolt，在Strom中上游Bolt到下游会经过网络上的数据传递，而Flink的Operator Chain将多个Operator链接到一起执行，减少了数据传递/线程切换等环节，降低系统开销的同时增加了资源利用率和Job性能；实际开发过程中需要开发者了解这些原理并能合理分配Memory和CPU给到每个Task线程。

&lt;em&gt;注：&lt;/em&gt; 【一个需要注意的地方】Chained的Operators之间的数据传递默认需要经过数据的拷贝（例如：kryo.copy(…)），将上游Operator的输出序列化出一个新对象并传递给下游Operator，可以通过&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ExecutionConfig.enableObjectReuse()&lt;/code&gt;开启对象重用，这样就关闭了这层copy操作，可以减少对象序列化开销和GC压力等，具体源码可阅读&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;org.apache.flink.streaming.runtime.tasks.OperatorChain&lt;/code&gt;与&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;org.apache.flink.streaming.runtime.tasks.OperatorChain.CopyingChainingOutput&lt;/code&gt;；官方建议开发人员在完全了解reuse内部机制后才使用该功能，冒然使用可能会给程序带来bug。

Operator Chain效果可参考如下官方文档截图：

&lt;strong&gt;图四：&lt;/strong&gt; 图的上半部分是StreamGraph视角，有Task类别无并行度，如图：Job Runtime时有三种类型的Task，分别是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Source-&amp;gt;Map&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;keyBy/window/apply&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Sink&lt;/code&gt;，其中&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Source-&amp;gt;Map&lt;/code&gt;是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Source()&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Map()&lt;/code&gt;chaining在一起的Task；图的下半部分是一个Job Runtime期的实际状态，Job最大的并行度为2，有5个SubTask（即5个执行线程）；若没有Operator Chain，则&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Source()&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Map()&lt;/code&gt;分属不同的Thread，Task线程数会增加到7，线程切换和数据传递开销等较之前有所增加，处理延迟和性能会较之前差。补充：在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;slotSharingGroup&lt;/code&gt;用默认或相同组名时，当前Job运行需2个Slot（与Job最大Parallelism相等）。
&lt;img src=&quot;/images/posts/flink/tasks_chains.svg&quot; alt=&quot;/images/posts/flink/tasks_chains.png&quot; /&gt;

&lt;h2 id=&quot;slot-sharing&quot;&gt;Slot Sharing&lt;/h2&gt;
Slot Sharing是指来自同一个Job且拥有相同&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;slotSharingGroup&lt;/code&gt;(默认：default)名称的不同Task的SubTask之间可以共享一个Slot，这使得一个Slot有机会持有Job的一整条Pipeline，这也是上文我们提到的在默认slotSharing的条件下Job启动所需的Slot数和Job中Operator的最大parallelism相等的原因；通过Slot Sharing机制可以更进一步提高Job运行性能，在Slot数不变的情况下增加了Operator可设置的最大的并行度，让类似window这种消耗资源的Task以最大的并行度分布在不同TM上，同时像map, filter这种较简单的操作也不会独占Slot资源，降低资源浪费的可能性。

具体Slot Sharing效果可参考如下官方文档截图：

&lt;strong&gt;图五：&lt;/strong&gt; 图的左下角是一个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;soure-map-reduce&lt;/code&gt;模型的Job，source和map是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;4 parallelism&lt;/code&gt;，reduce是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3 parallelism&lt;/code&gt;，总计11个SubTask；这个Job最大Parallelism是4，所以将这个Job发布到左侧上面的两个TM上时得到图右侧的运行图，一共占用四个Slot，有三个Slot拥有完整的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;source-map-reduce&lt;/code&gt;模型的Pipeline，如右侧图所示；注：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map&lt;/code&gt;的结果会&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;shuffle&lt;/code&gt;到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;reduce&lt;/code&gt;端，右侧图的箭头只是说Slot内数据Pipline，没画出Job的数据&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;shuffle&lt;/code&gt;过程。
&lt;img src=&quot;/images/posts/flink/slots.svg&quot; alt=&quot;slots&quot; /&gt;

&lt;strong&gt;图六：&lt;/strong&gt; 图中包含&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;source-map[6 parallelism]&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;keyBy/window/apply[6 parallelism]&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sink[1 parallelism]&lt;/code&gt;三种Task，总计占用了6个Slot；由左向右开始第一个slot内部运行着3个SubTask[3 Thread]，持有Job的一条完整pipeline；剩下5个Slot内分别运行着2个SubTask[2 Thread]，数据最终通过网络传递给&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Sink&lt;/code&gt;完成数据处理。
&lt;img src=&quot;/images/posts/flink/slot_sharing.svg&quot; alt=&quot;/images/posts/flink/tasks_chains.png&quot; /&gt;

&lt;h2 id=&quot;operator-chain--slot-sharing-api&quot;&gt;Operator Chain &amp;amp; Slot Sharing API&lt;/h2&gt;

Flink在默认情况下有策略对Job进行Operator Chain 和 Slot Sharing的控制，比如：将并行度相同且连续的SingleOutputStreamOperator操作chain在一起（chain的条件较苛刻，不止单一输出这一条，具体可阅读&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;org.apache.flink.streaming.api.graph.StreamingJobGraphGenerator.isChainable(...)&lt;/code&gt;），Job的所有Task都采用名为default的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;slotSharingGroup&lt;/code&gt;做Slot Sharing。但在实际的需求场景中，我们可能会遇到需人为干预Job的Operator Chain 或 Slot Sharing策略的情况，本段就重点关注下用于改变默认Chain 和 Sharing策略的API。

&lt;strong&gt;StreamExecutionEnvironment.disableOperatorChaining()：&lt;/strong&gt; 关闭整个Job的Operator Chain，每个Operator独自占有一个Task，如上&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;图四&lt;/code&gt;所描述的Job，如果&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;disableOperatorChaining&lt;/code&gt;则 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;source-&amp;gt;map&lt;/code&gt;会拆开为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;source()&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map()&lt;/code&gt;两种Task，Job实际的Task数会增加到7；这个设置会降低Job性能，在非生产环境的测试或profiling时可以借助以更好分析问题，实际生产过程中不建议使用。

&lt;strong&gt;someStream.filter(…).map(…).startNewChain().map()：&lt;/strong&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;startNewChain()&lt;/code&gt;是指从当前&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Operator[map]&lt;/code&gt;开始一个新的chain，即：两个map会chaining在一起而filter不会（因为startNewChain的存在使得第一次map与filter断开了chain）。

&lt;strong&gt;someStream.map(…).disableChaining()：&lt;/strong&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;disableChaining()&lt;/code&gt;是指当前&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Operator[map]&lt;/code&gt;禁用Operator Chain，即：Operator[map]会独自占用一个Task。

&lt;strong&gt;someStream.map(…).slotSharingGroup(“name”)：&lt;/strong&gt; 默认情况下所有Operator的slotGroup都为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;default&lt;/code&gt;，可以通过&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;slotSharingGroup()&lt;/code&gt;进行自定义，Flink会将拥有相同slotGroup名称的Operators运行在相同Slot内，不同slotGroup名称的Operators运行在其他Slot内。

Operator Chain有三种策略&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ALWAYS&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NEVER&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HEAD&lt;/code&gt;，详细可查看&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;org.apache.flink.streaming.api.operators.ChainingStrategy&lt;/code&gt;；&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;startNewChain()&lt;/code&gt;对应的策略是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ChainingStrategy.HEAD&lt;/code&gt;（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;StreamOperator&lt;/code&gt;的默认策略），&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;disableChaining()&lt;/code&gt;对应的策略是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ChainingStrategy.NEVER&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ALWAYS&lt;/code&gt;是尽可能的将Operators chaining在一起； 在通常情况下&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ALWAYS&lt;/code&gt;是效率最高，很多Operator会将默认策略覆盖为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ALWAYS&lt;/code&gt;，如filter, map, flatMap等函数。

&lt;h2 id=&quot;迁移onyarn后job性能下降的问题&quot;&gt;迁移OnYarn后Job性能下降的问题&lt;/h2&gt;

&lt;strong&gt;JOB说明：&lt;/strong&gt;

类似StreamETL，100 parallelism，即：一个流式的ETL Job，不包含window等操作，Job的并行度为100；

&lt;strong&gt;环境说明：&lt;/strong&gt;

&lt;ol&gt;
  &lt;li&gt;
    Standalone下的Job Execution Graph：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10TMs * 10Slots-per-TM &lt;/code&gt;，即：Job的Task运行在10个TM节点上，每个TM上占用10个Slot，每个Slot可用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1C2G&lt;/code&gt;资源，GCConf：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-XX:+UseG1GC -XX:MaxGCPauseMillis=100&lt;/code&gt;；
  &lt;/li&gt;
  &lt;li&gt;
    OnYarn下初始状态的Job Execution Graph：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;100TMs * 1Slot-per-TM&lt;/code&gt;，即：Job的Task运行在100个Container上，每个Container上的TM持有1个Slot，每个Container分配&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1C2G&lt;/code&gt;资源，GCConf：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-XX:+UseG1GC -XX:MaxGCPauseMillis=100&lt;/code&gt;；
  &lt;/li&gt;
  &lt;li&gt;
    OnYarn下调整后的Job Execution Graph：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;50TMs * 2Slot-per-TM&lt;/code&gt;，即：Job的Task运行在50个Container上，每个Container上的TM持有2个Slot，每个Container分配&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;2C4G&lt;/code&gt;资源，GCConfig：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-XX:+UseG1GC -XX:MaxGCPauseMillis=100&lt;/code&gt;；
  &lt;/li&gt;
&lt;/ol&gt;

&lt;em&gt;注：&lt;/em&gt; OnYarn下使用了与Standalone一致的GC配置，当前Job在Standalone或OnYarn环境中运行时，YGC, FGC频率基本相同，OnYarn下单个Container的堆内存较小使得单次GC耗时减少，生产环境中大家最好对比下CMS和G1，选择更好的GC策略，当前上下文中暂时认为GC对Job性能影响可忽略不计。

&lt;strong&gt;问题分析：&lt;/strong&gt;

引起Job性能降低的原因不难定位，贴一张Container的线程图（VisualVM中的截图）&lt;strong&gt;图7&lt;/strong&gt;：在一个1C2G的Container内有126个活跃线程，守护线程78个；首先，在一个1C2G的Container中运行着126个活跃线程，频繁的线程切换是会经常出现的，这让本来不就不充裕的CPU显得更加的匮乏；其次，真正与数据处理相关的线程是红色画笔圈起的14条线程（2条&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Kafka Partition Consumer&lt;/code&gt;，Consumers和Operators包含在这个两个线程内；12条&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Kafka Producer&lt;/code&gt;线程，将处理好的数据sink到Kafka Topic），这14条线程之外的大多数线程在相同TM不同Slot间可以共用，比如：ZK-Curator, Dubbo-Client, GC-Thread, Flink-Akka, Flink-Netty, Flink-Metrics等线程，完全可以通过增加TM下Slot数量达到多个SubTask共享的目的；此时我们会很自然的得出一个解决办法：在Job使用资源不变的情况下，减少Container数量的同时增加单个Container持有的CPU, Memory, Slot数量，比如上文环境说明中从&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;方案2&lt;/code&gt;调整到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;方案3&lt;/code&gt;，实际调整后的Job运行稳定了许多且消费速度与Standalone基本持平。
&lt;img src=&quot;/images/posts/flink/container-threads.jpg&quot; alt=&quot;container threads 1&quot; /&gt;

&lt;em&gt;注：&lt;/em&gt;
当前问题是公司内迁移类似StreamETL的Job时遇到的，解决方案简单不带有普适性，对于带有window算子的Job需要更仔细缜密的问题分析；
当前公司Deploy到Yarn集群的Job都配置了JMX/Prometheus两种监控，单个Container下Slot数量越多每次scrape的数据越多，实际生成环境中需观测是否会影响Job正常运行，在测试时将Container配置为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3C6G 3Slot&lt;/code&gt;时发现一次&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;java.lang.OutOfMemoryError: Direct buffer memory&lt;/code&gt;的异常，初步判断与Prometheus Client相关，可适当调整JVM的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MaxDirectMemorySize&lt;/code&gt;来解决，异常如下&lt;strong&gt;图8&lt;/strong&gt;：
&lt;img src=&quot;/images/posts/flink/prom-exception.png&quot; alt=&quot;Prometheus exception&quot; /&gt;

&lt;h2 id=&quot;summary&quot;&gt;Summary&lt;/h2&gt;

Operator Chain是将多个Operator链接在一起放置在一个Task中，只针对Operator；Slot Sharing是在一个Slot中执行多个Task，针对的是Operator Chain之后的Task；这两种优化的都充分利用了计算资源，减少了不必要的开销，提升了Job的运行性能。此外，Operator Chain的源码在streaming包下，只在流处理任务中有这个机制；Slot Sharing在flink-runtime包下，似乎应用更广泛一些（具体没考究）。

最后，只有充分的了解Slot，Operator Chain，Slot Sharing是什么以及各自的作用和相互间的关系，才能编写出优秀的代码并高效的运行在集群上。

结尾：贴一张官方讲解Slot的详图供大家参考学习：
&lt;img src=&quot;/images/posts/flink/slots_parallelism.svg&quot; alt=&quot;slots parallelism&quot; /&gt;

&lt;h2 id=&quot;reference&quot;&gt;Reference&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://ci.apache.org/projects/flink/flink-docs-stable/internals/job_scheduling.html#scheduling&quot;&gt;https://ci.apache.org/projects/flink/flink-docs-stable/internals/job_scheduling.html#scheduling&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://ci.apache.org/projects/flink/flink-docs-stable/dev/stream/operators/#task-chaining-and-resource-groups&quot;&gt;https://ci.apache.org/projects/flink/flink-docs-stable/dev/stream/operators/#task-chaining-and-resource-groups&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://ci.apache.org/projects/flink/flink-docs-stable/ops/config.html#configuring-taskmanager-processing-slots&quot;&gt;https://ci.apache.org/projects/flink/flink-docs-stable/ops/config.html#configuring-taskmanager-processing-slots&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://ci.apache.org/projects/flink/flink-docs-stable/ops/deployment/yarn_setup.html#background--internals&quot;&gt;https://ci.apache.org/projects/flink/flink-docs-stable/ops/deployment/yarn_setup.html#background–internals&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://ci.apache.org/projects/flink/flink-docs-stable/concepts/runtime.html#task-slots-and-resources&quot;&gt;https://ci.apache.org/projects/flink/flink-docs-stable/concepts/runtime.html#task-slots-and-resources&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://flink.apache.org/visualizer/&quot;&gt;https://flink.apache.org/visualizer/&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</description>
        <pubDate>Thu, 01 Aug 2019 00:00:00 +0000</pubDate>
        <link>https://itinycheng.github.io/2019/08/01/flink-slot-explanation-and-execution-graph-optimization/</link>
        <guid isPermaLink="true">https://itinycheng.github.io/2019/08/01/flink-slot-explanation-and-execution-graph-optimization/</guid>
        
        <category>flink</category>
        
        <category>yarn</category>
        
        <category>slot</category>
        
        <category>InfoQ</category>
        
        
        <category>flink</category>
        
      </item>
    
      <item>
        <title>Flink Standalone/OnYarn使用过程中遇到的若干问题记录</title>
        <description>&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#前言&quot; id=&quot;markdown-toc-前言&quot;&gt;前言&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#问题汇总&quot; id=&quot;markdown-toc-问题汇总&quot;&gt;问题汇总&lt;/a&gt;    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#flink与hadoop版本兼容性问题&quot; id=&quot;markdown-toc-flink与hadoop版本兼容性问题&quot;&gt;Flink与Hadoop版本兼容性问题&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#flink-avro序列化与allownull&quot; id=&quot;markdown-toc-flink-avro序列化与allownull&quot;&gt;Flink-Avro序列化与AllowNull&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#classloader导致failover-failed&quot; id=&quot;markdown-toc-classloader导致failover-failed&quot;&gt;ClassLoader导致Failover Failed&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#taskmanagernetworknumberofbuffers配置&quot; id=&quot;markdown-toc-taskmanagernetworknumberofbuffers配置&quot;&gt;taskmanager.network.numberOfBuffers配置&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#onyarn下log的打印&quot; id=&quot;markdown-toc-onyarn下log的打印&quot;&gt;OnYarn下log的打印&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#prometheus监控&quot; id=&quot;markdown-toc-prometheus监控&quot;&gt;Prometheus监控&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#总结&quot; id=&quot;markdown-toc-总结&quot;&gt;总结&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#参考&quot; id=&quot;markdown-toc-参考&quot;&gt;参考&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;前言&quot;&gt;前言&lt;/h2&gt;

&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Flink&lt;/code&gt;是由公司前辈引入，最初版本是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Flink 1.1.5&lt;/code&gt;，后升级到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Flink 1.3.2&lt;/code&gt;一直以来&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Flink Job&lt;/code&gt;都是跑在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Standalone&lt;/code&gt;集群上的，为规避&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Standalone&lt;/code&gt;集群中&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Job&lt;/code&gt;相互影响的问题（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Standalone&lt;/code&gt;无法做资源隔离），前辈们又新做了&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Flink&lt;/code&gt;集群，将互相影响的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Job&lt;/code&gt;拆分到不同集群中，这时团队内部有多个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Flink Standalone&lt;/code&gt;集群，虽然解决了部分&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Job&lt;/code&gt;互相影响的问题，但有些服务是和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Kafka&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Druid&lt;/code&gt;部署在同一台物理机器的（不同进程），又出现过几次不同服务互相影响的情况；这些&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Flink Standalone&lt;/code&gt;集群维护/升级起来也挺麻烦的，同时新&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Job&lt;/code&gt;越来越多，生活苦不堪言~.~；兄弟们从2018年初就喊着&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OnYarn&lt;/code&gt;，结果由于人力、机器、业务迭代等各种问题一拖再拖；到2018年底终于开始了这项工作，虽然只1个人力投入，经历2-3个月的时间团队的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;streaming Job&lt;/code&gt;已经稳定运行在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OnYarn&lt;/code&gt;模式下，本文就重点记录下这段时所遇到的一些重点问题，当前生产运行版本是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Flink 1.7.1 + Hadoop 3.1.0&lt;/code&gt;

&lt;h2 id=&quot;问题汇总&quot;&gt;问题汇总&lt;/h2&gt;

&lt;h3 id=&quot;flink与hadoop版本兼容性问题&quot;&gt;Flink与Hadoop版本兼容性问题&lt;/h3&gt;

由于官方没有依赖&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Hadoop 3.1.0&lt;/code&gt;的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Flink&lt;/code&gt;下载包，所以这步需要自行编译&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Flink&lt;/code&gt;源码，可参考官方Doc：&lt;a href=&quot;https://ci.apache.org/projects/flink/flink-docs-release-1.7/flinkDev/building.html&quot;&gt;https://ci.apache.org/projects/flink/flink-docs-release-1.7/flinkDev/building.html&lt;/a&gt;；建议用公司内部环境（Jenkins, JDK, etc.）并结合官方Doc来完成打包。

简单列下我司打包的Jenkins配置：
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Branches to build&lt;/code&gt;: refs/tags/release-1.7.1
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Repository URL&lt;/code&gt;: git@gitlab.company.com:git/flink.git
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Build - Goals and options&lt;/code&gt;: clean install -DskipTests -Dfast -Dhadoop.version=3.1.0
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Post Steps - Execute shell&lt;/code&gt;: cd flink-dist/target/flink-1.7.1-bin
tar -czvf flink-1.7.1-hadoop31-scala_2.11.tar.gz flink-1.7.1/

&lt;strong&gt;报错：&lt;/strong&gt;
使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;./bin/flink run -m yarn-cluster&lt;/code&gt;的方式提交Job时出现如下报错：

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;
&lt;span class=&quot;n&quot;&gt;client端&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;：&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;------------------------------------------------------------&lt;/span&gt;
 &lt;span class=&quot;nc&quot;&gt;The&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;program&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;finished&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;the&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;following&lt;/span&gt; &lt;span class=&quot;nl&quot;&gt;exception:&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;lang&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;RuntimeException&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Error&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;deploying&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;the&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;YARN&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cluster&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;at&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;org&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;apache&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;flink&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;yarn&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;cli&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;FlinkYarnSessionCli&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;createCluster&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;FlinkYarnSessionCli&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;556&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;at&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;org&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;apache&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;flink&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;yarn&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;cli&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;FlinkYarnSessionCli&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;createCluster&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;FlinkYarnSessionCli&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;72&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;at&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;org&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;apache&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;flink&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;client&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;CliFrontend&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;createClient&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;CliFrontend&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;962&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;at&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;org&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;apache&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;flink&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;client&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;CliFrontend&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;CliFrontend&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;243&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;at&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;org&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;apache&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;flink&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;client&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;CliFrontend&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;parseParameters&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;CliFrontend&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1086&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;at&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;org&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;apache&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;flink&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;client&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;CliFrontend&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;call&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;CliFrontend&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1133&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;at&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;org&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;apache&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;flink&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;client&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;CliFrontend&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;call&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;CliFrontend&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1130&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;at&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;org&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;apache&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;flink&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;runtime&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;security&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;HadoopSecurityContext&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;HadoopSecurityContext&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;43&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;at&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;security&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;AccessController&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;doPrivileged&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Native&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Method&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;at&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;javax&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;security&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;auth&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;Subject&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;doAs&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Subject&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;422&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;at&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;org&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;apache&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;hadoop&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;security&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;UserGroupInformation&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;doAs&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;UserGroupInformation&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1682&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;at&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;org&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;apache&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;flink&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;runtime&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;security&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;HadoopSecurityContext&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;runSecured&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;HadoopSecurityContext&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;40&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;at&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;org&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;apache&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;flink&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;client&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;CliFrontend&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;CliFrontend&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1130&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;nc&quot;&gt;Caused&lt;/span&gt; &lt;span class=&quot;nl&quot;&gt;by:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;lang&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;RuntimeException&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Couldn&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;deploy&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Yarn&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cluster&lt;/span&gt;
  	&lt;span class=&quot;n&quot;&gt;at&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;org&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;apache&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;flink&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;yarn&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;AbstractYarnClusterDescriptor&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;deploy&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;AbstractYarnClusterDescriptor&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;443&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
  	&lt;span class=&quot;n&quot;&gt;at&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;org&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;apache&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;flink&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;yarn&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;cli&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;FlinkYarnSessionCli&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;createCluster&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;FlinkYarnSessionCli&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;554&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
  	&lt;span class=&quot;o&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;12&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;more&lt;/span&gt;
  &lt;span class=&quot;nc&quot;&gt;Caused&lt;/span&gt; &lt;span class=&quot;nl&quot;&gt;by:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;org&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;apache&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;flink&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;yarn&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;AbstractYarnClusterDescriptor&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;$YarnDeploymentException&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;The&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;YARN&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;application&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;unexpectedly&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;switched&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;to&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;state&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;FAILED&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;during&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;deployment&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;server端&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;：&lt;/span&gt;
  &lt;span class=&quot;nc&quot;&gt;Uncaught&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;error&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;thread&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;flink&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;akka&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;remote&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;default&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;remote&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dispatcher&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;shutting&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;down&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;JVM&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;since&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;akka&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;jvm&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;exit&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;on&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fatal&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;'&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;enabled&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;ActorSystem&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;flink&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;lang&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;VerifyError&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;org&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;jboss&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;netty&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;channel&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;socket&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nio&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;NioWorkerPool&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nl&quot;&gt;method:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;newWorker&lt;/span&gt;  &lt;span class=&quot;nl&quot;&gt;signature:&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Ljava&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;util&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;concurrent&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Executor&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;)&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Lorg&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;jboss&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;netty&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;channel&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;socket&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nio&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;AbstractNioWorker&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;)&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Wrong&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;function&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;at&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;akka&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;remote&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;transport&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;netty&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;NettyTransport&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;init&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;NettyTransport&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;scala&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;295&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;at&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;akka&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;remote&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;transport&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;netty&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;NettyTransport&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;init&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;NettyTransport&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;scala&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;251&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;at&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sun&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;reflect&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;NativeConstructorAccessorImpl&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;newInstance0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Native&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Method&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;strong&gt;原因：&lt;/strong&gt;
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Flink 1.3.2&lt;/code&gt;依赖的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Netty3&lt;/code&gt;的版本较低，与&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Hadoop 3.1.0&lt;/code&gt;不兼容导致的报错；不兼容的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Netty&lt;/code&gt;版本为：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Netty-3.4.0.Final&lt;/code&gt; 和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Netty-3.10.6.Final&lt;/code&gt;；曾尝试通过修改&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NioWorkerPool&lt;/code&gt;的源文件并编译新的class文件以解决这个问题，但以失败告终；

&lt;strong&gt;解决：&lt;/strong&gt;
升级&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Flink&lt;/code&gt;版本；经测试：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Flink 1.6, 1.7&lt;/code&gt; + &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Hadoop 3.1.0&lt;/code&gt;编译出的包是可以正常提交给&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Hadoop 3.1.0&lt;/code&gt;集群的；

&lt;strong&gt;结论：&lt;/strong&gt;
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Flink 1.3.2&lt;/code&gt;与&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Hadoop 3.1.0&lt;/code&gt;存在兼容性问题，若需提交&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Flink Job&lt;/code&gt;到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Hadoop 3.1.0&lt;/code&gt;及以上，最好使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Flink 1.6+&lt;/code&gt;

&lt;h3 id=&quot;flink-avro序列化与allownull&quot;&gt;Flink-Avro序列化与AllowNull&lt;/h3&gt;
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Flink&lt;/code&gt;类型系统和序列化方式可关注官方doc：
&lt;a href=&quot;https://ci.apache.org/projects/flink/flink-docs-master/dev/types_serialization.html&quot;&gt;https://ci.apache.org/projects/flink/flink-docs-master/dev/types_serialization.html&lt;/a&gt;
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Flink&lt;/code&gt;默认支持&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PoJoSerializer&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AvroSerializer&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;KryoSerializer&lt;/code&gt;三种序列化，官方推荐使用Flink自己实现的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PojoSerializer&lt;/code&gt;序列化（在遇到无法用PoJoSerializer转化的时默认用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;KryoSerializer&lt;/code&gt;替代，具体可关注&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GenericTypeInfo&lt;/code&gt;），&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Kryo&lt;/code&gt;性能较&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Avro&lt;/code&gt;好些，但无供跨语言的解决方案且序列化结果的自解释性较&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Avro&lt;/code&gt;差；经过功能性能等多方面的调研，计划&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Task&lt;/code&gt;间数据传递用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Kryo&lt;/code&gt;，提高数据序列化性能，状态的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;checkpoint&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;savepoint&lt;/code&gt;用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Avro&lt;/code&gt;，让状态有更好的兼容&amp;amp;可迁移性；此功能从&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Flink 1.7+&lt;/code&gt;开始支持具体关注doc:
&lt;a href=&quot;https://ci.apache.org/projects/flink/flink-docs-release-1.7/dev/stream/state/custom_serialization.html&quot;&gt;https://ci.apache.org/projects/flink/flink-docs-release-1.7/dev/stream/state/custom_serialization.html&lt;/a&gt;

OK，接下来说遇到的在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Avro&lt;/code&gt;下的对象序列化异常，本次bug和上边的内容无关~,~. 这个报错只是因为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Flink-Avro&lt;/code&gt;不支持&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;null&lt;/code&gt;的序列化；

&lt;strong&gt;报错：&lt;/strong&gt;
开启&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Avro&lt;/code&gt;序列化（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;enableForceAvro()&lt;/code&gt;），&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Task&lt;/code&gt;间通过网络传递数据对象时，若对象内部有&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;field&lt;/code&gt;为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;null&lt;/code&gt;，数据无法正常序列化抛出如下异常：

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;
&lt;span class=&quot;n&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;lang&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;RuntimeException&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;com&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;app&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;flink&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;model&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;Line&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;null&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;of&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;field&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;switchKey&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;of&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;com&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;app&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;flink&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;model&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;Line&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;at&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;org&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;apache&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;flink&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;streaming&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;runtime&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;io&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;RecordWriterOutput&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;pushToRecordWriter&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;RecordWriterOutput&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;104&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;at&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;org&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;apache&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;flink&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;streaming&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;runtime&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;io&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;RecordWriterOutput&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;collect&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;RecordWriterOutput&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;83&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;at&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;org&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;apache&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;flink&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;streaming&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;runtime&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;io&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;RecordWriterOutput&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;collect&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;RecordWriterOutput&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;41&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
	&lt;span class=&quot;o&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;more&lt;/span&gt;
&lt;span class=&quot;nc&quot;&gt;Caused&lt;/span&gt; &lt;span class=&quot;nl&quot;&gt;by:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;lang&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;NullPointerException&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;com&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;app&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;flink&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;model&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;Line&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;null&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;of&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;field&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;switchKey&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;of&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;com&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;app&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;flink&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;model&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;Line&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;at&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;org&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;apache&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;avro&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;reflect&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;ReflectDatumWriter&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;write&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;ReflectDatumWriter&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;145&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;at&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;org&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;apache&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;avro&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;generic&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;GenericDatumWriter&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;write&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;GenericDatumWriter&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;58&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;at&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;org&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;apache&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;flink&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;api&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;typeutils&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;runtime&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;AvroSerializer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;serialize&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;AvroSerializer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;135&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
	&lt;span class=&quot;o&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;18&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;more&lt;/span&gt;
&lt;span class=&quot;nc&quot;&gt;Caused&lt;/span&gt; &lt;span class=&quot;nl&quot;&gt;by:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;lang&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;NullPointerException&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;at&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;org&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;apache&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;avro&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;specific&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;SpecificDatumWriter&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;writeString&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;SpecificDatumWriter&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;65&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;at&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;org&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;apache&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;avro&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;generic&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;GenericDatumWriter&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;write&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;GenericDatumWriter&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;76&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;at&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;org&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;apache&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;avro&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;reflect&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;ReflectDatumWriter&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;write&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;ReflectDatumWriter&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;143&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;at&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;org&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;apache&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;avro&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;generic&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;GenericDatumWriter&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;writeField&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;GenericDatumWriter&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;114&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;at&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;org&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;apache&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;avro&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;reflect&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;ReflectDatumWriter&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;writeField&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;ReflectDatumWriter&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;175&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;at&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;org&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;apache&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;avro&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;generic&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;GenericDatumWriter&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;writeRecord&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;GenericDatumWriter&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;104&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;at&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;org&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;apache&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;avro&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;generic&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;GenericDatumWriter&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;write&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;GenericDatumWriter&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;66&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;at&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;org&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;apache&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;avro&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;reflect&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;ReflectDatumWriter&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;write&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;ReflectDatumWriter&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;143&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;strong&gt;原因：&lt;/strong&gt;
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Flink&lt;/code&gt;在初始化&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Avro Serializer&lt;/code&gt;时使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;org.apache.avro.reflect.ReflectData&lt;/code&gt;构造&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DatumReader&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DatumWriter&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ReflectData&lt;/code&gt;本身不支持&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;null&lt;/code&gt;的序列化（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Avro&lt;/code&gt;有提供&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ReflectData&lt;/code&gt;的扩展类&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;org.apache.avro.reflect.AllowNull&lt;/code&gt;来支持&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;null&lt;/code&gt;的序列化，但&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Flink&lt;/code&gt;并未使用），从而导致&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;null field&lt;/code&gt;的序列化异常；而&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Kryo&lt;/code&gt;默认支持&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;null field&lt;/code&gt;，程序可正常运行；&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Flink 1.3.2&lt;/code&gt;下代码截图如下：

Avro:
&lt;img src=&quot;/images/posts/flink/avro-serializer-1.png&quot; alt=&quot;avro serializer&quot; /&gt;

Kryo:
&lt;img src=&quot;/images/posts/flink/kryo-serializer.png&quot; alt=&quot;kryo serializer&quot; /&gt;

&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Flink 1.7&lt;/code&gt;后对集成&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Avro&lt;/code&gt;的代码变更较大，但依旧会出现当前Bug，代码逻辑入口：
&lt;img src=&quot;/images/posts/flink/avro-serializer-2.png&quot; alt=&quot;kryo serializer&quot; /&gt;

&lt;strong&gt;解决：&lt;/strong&gt;
&lt;ol&gt;
  &lt;li&gt;给需要序列化的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;field&lt;/code&gt;值赋默认值；&lt;/li&gt;
  &lt;li&gt;改用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Kryo&lt;/code&gt;（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;enableForceKryo()&lt;/code&gt;）序列化；&lt;/li&gt;
  &lt;li&gt;去除&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;enableForceXXX()&lt;/code&gt;尝试下默认的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PoJoSerializer&lt;/code&gt;；&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;classloader导致failover-failed&quot;&gt;ClassLoader导致Failover Failed&lt;/h3&gt;

本问题在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Kafka0.8 + Flink 1.7&lt;/code&gt;（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cannot guarantee End-to-End exactly once&lt;/code&gt;）环境下造成了数据丢失的问题；

&lt;strong&gt;报错：&lt;/strong&gt;
&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;
&lt;span class=&quot;n&quot;&gt;异常一&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;：&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;kafka异常&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;（&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;导致了Job重启&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;）&lt;/span&gt;
&lt;span class=&quot;nc&quot;&gt;Caused&lt;/span&gt; &lt;span class=&quot;nl&quot;&gt;by:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;lang&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;Exception&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Could&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;complete&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;snapshot&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;69&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;operator&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;wep&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;at&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;org&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;apache&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;flink&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;streaming&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;api&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;operators&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;AbstractStreamOperator&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;snapshotState&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;AbstractStreamOperator&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;422&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;at&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;org&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;apache&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;flink&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;streaming&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;runtime&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;tasks&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;StreamTask&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;$CheckpointingOperation&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;checkpointStreamOperator&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;StreamTask&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1113&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;at&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;org&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;apache&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;flink&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;streaming&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;runtime&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;tasks&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;StreamTask&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;$CheckpointingOperation&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;executeCheckpointing&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;StreamTask&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1055&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;at&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;org&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;apache&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;flink&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;streaming&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;runtime&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;tasks&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;StreamTask&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;checkpointState&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;StreamTask&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;729&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;at&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;org&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;apache&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;flink&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;streaming&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;runtime&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;tasks&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;StreamTask&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;performCheckpoint&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;StreamTask&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;641&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;at&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;org&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;apache&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;flink&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;streaming&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;runtime&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;tasks&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;StreamTask&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;triggerCheckpointOnBarrier&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;StreamTask&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;586&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
	&lt;span class=&quot;o&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;more&lt;/span&gt;
&lt;span class=&quot;nc&quot;&gt;Caused&lt;/span&gt; &lt;span class=&quot;nl&quot;&gt;by:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;lang&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;Exception&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Failed&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;to&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;send&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;to&lt;/span&gt; &lt;span class=&quot;nl&quot;&gt;Kafka:&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;This&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;server&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;the&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;leader&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;that&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;topic&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;partition&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;at&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;org&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;apache&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;flink&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;streaming&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;connectors&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;kafka&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;FlinkKafkaProducerBase&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;checkErroneous&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;FlinkKafkaProducerBase&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;375&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;at&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;org&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;apache&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;flink&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;streaming&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;connectors&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;kafka&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;FlinkKafkaProducerBase&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;snapshotState&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;FlinkKafkaProducerBase&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;363&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;at&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;org&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;apache&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;flink&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;streaming&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;util&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;functions&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;StreamingFunctionUtils&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;trySnapshotFunctionState&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;StreamingFunctionUtils&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;118&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;at&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;org&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;apache&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;flink&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;streaming&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;util&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;functions&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;StreamingFunctionUtils&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;snapshotFunctionState&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;StreamingFunctionUtils&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;99&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;at&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;org&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;apache&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;flink&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;streaming&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;api&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;operators&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;AbstractUdfStreamOperator&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;snapshotState&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;AbstractUdfStreamOperator&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;90&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;at&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;org&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;apache&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;flink&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;streaming&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;api&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;operators&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;AbstractStreamOperator&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;snapshotState&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;AbstractStreamOperator&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;395&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
	&lt;span class=&quot;o&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;13&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;more&lt;/span&gt;


&lt;span class=&quot;n&quot;&gt;异常二&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;：&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Job重启&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;，&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Dubbo异常&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;com&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;alibaba&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;dubbo&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;rpc&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;RpcException&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Rpc&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cluster&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;invoker&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;com&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;etl&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;commons&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;api&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;GenericService&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;on&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;consumer&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;xxx&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;xx&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;xx&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;xx&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;use&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dubbo&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;version&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;flink&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;now&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;destroyed&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;!&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Can&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;invoke&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;any&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;more&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;at&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;com&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;alibaba&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;dubbo&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;rpc&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;cluster&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;support&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;AbstractClusterInvoker&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;checkWheatherDestoried&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;AbstractClusterInvoker&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;233&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;at&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;com&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;alibaba&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;dubbo&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;rpc&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;cluster&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;support&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;AbstractClusterInvoker&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;invoke&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;AbstractClusterInvoker&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;215&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;at&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;com&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;alibaba&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;dubbo&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;rpc&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;cluster&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;support&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;wrapper&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;MockClusterInvoker&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;invoke&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;MockClusterInvoker&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;72&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;at&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;com&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;alibaba&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;dubbo&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;rpc&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;proxy&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;InvokerInvocationHandler&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;invoke&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;InvokerInvocationHandler&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;52&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;at&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;com&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;alibaba&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;dubbo&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;common&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;bytecode&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;proxy0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;queryBaseDimension&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;proxy0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;strong&gt;过程：&lt;/strong&gt;
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Kafka&lt;/code&gt;抖动或异常导致&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Flink sink data to Kafka&lt;/code&gt;时丢失&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Topic Leader&lt;/code&gt;（出现异常一），进而导致&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Flink Job&lt;/code&gt;重启；
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Flink Job&lt;/code&gt;重启正常，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Job&lt;/code&gt;状态是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;running&lt;/code&gt;，但&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;flink operator&lt;/code&gt;中的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Dubbo&lt;/code&gt;未初始化导致无法提供服务（在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Flink Job&lt;/code&gt;重启过程中由于&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DubboService.java&lt;/code&gt;中代码不规范而导致&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Dubbo&lt;/code&gt;服务未初始化，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Dubbo&lt;/code&gt;服务不可用）；
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Flink Source&lt;/code&gt;开始消费数据，当需调用含有&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DubboService&lt;/code&gt;的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Operator&lt;/code&gt;处理数据时抛异常（出现异常二），数据无法正常处理并下发到下一个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ChainOperator&lt;/code&gt;；
上游数据不断被消费，但无法正常处理并下发，这就照成的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Source&lt;/code&gt;端丢数的现象；

&lt;strong&gt;原因：&lt;/strong&gt;
在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Standalone&lt;/code&gt;模式下&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Flink Job&lt;/code&gt;的失败自动重启&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Dubbo&lt;/code&gt;服务可以正常初始化的，但在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OnYarn&lt;/code&gt;模式下初始化&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Dubbo&lt;/code&gt;服务会不可用；
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Flink Standalone&lt;/code&gt;下失败重启过程中，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DubboService&lt;/code&gt;会先卸载再重新加载，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Dubbo&lt;/code&gt;服务正常初始化；
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OnYarn&lt;/code&gt;下失败重启的过程中类&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DubboService&lt;/code&gt;不会被JVM卸载，同时代码书写不规范，导致内存驻留了已被closed的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DubboService&lt;/code&gt;；
调用已经closed的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Dubbo&lt;/code&gt;服务则数据无法正常处理并抛异常；

问题代码：
&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;DubboService&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dubboService&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;init&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
   &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dubboService&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;nc&quot;&gt;Dubbo&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;init&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;logger&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;info&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Dubbo initialized.&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
   &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;logger&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;info&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Dubbo already existed.&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
   &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// 未对dubboService做置空处理&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;destroy&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;dubboService&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;destroy&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;strong&gt;解决：&lt;/strong&gt;
在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;destory&lt;/code&gt;方法中清除所有&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;static&lt;/code&gt;的共享变量，使得&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;job&lt;/code&gt;重启时候可以正常重新初始化&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Dubbo&lt;/code&gt;服务，如下代码：
修改代码：
&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;destroy&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
   &lt;span class=&quot;n&quot;&gt;dubboService&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;destroy&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
   &lt;span class=&quot;c1&quot;&gt;// 置空&lt;/span&gt;
   &lt;span class=&quot;n&quot;&gt;dubboService&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
 &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;strong&gt;补充：&lt;/strong&gt;

&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OnYarn&lt;/code&gt;的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Flink Job&lt;/code&gt;重启时候是不会释放&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Container&lt;/code&gt;并重新申请的，自动重启前后的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;JobId&lt;/code&gt;保持不变；
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Standalone&lt;/code&gt;模式下，User code的是在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TaskManager&lt;/code&gt;环境中加载启动的用的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ClassLoader&lt;/code&gt;是：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;org.apache.flink.runtime.execution.librarycache.FlinkUserCodeClassLoader&lt;/code&gt;；
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OnYarn&lt;/code&gt;模式下（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bin/flink run -m yarn-cluster&lt;/code&gt;），&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;User Code&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Flink Framework&lt;/code&gt;一并提交到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Container&lt;/code&gt;中，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;User Code&lt;/code&gt;加载用的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ClassLoader&lt;/code&gt;是：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sun.misc.Launcher$AppClassLoader&lt;/code&gt;；
由于&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ClassLoader&lt;/code&gt;不同，导致&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;User Code&lt;/code&gt;卸载时机不同，也就导致在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OnYarn&lt;/code&gt;模式下&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Restart Job&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DubboService&lt;/code&gt;初始化异常；

可参考Flink Docs：https://ci.apache.org/projects/flink/flink-docs-release-1.7/monitoring/debugging_classloading.html

&lt;strong&gt;注：&lt;/strong&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;coding&lt;/code&gt;时候尽量用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RichFunction&lt;/code&gt; 的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;open()/close()&lt;/code&gt;方法初始化对象，尽可能用（单例）对象持有各种资源，减少使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;static&lt;/code&gt;（生命周期不可控），及时对&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;static&lt;/code&gt;修饰对象手动置空；

&lt;h3 id=&quot;taskmanagernetworknumberofbuffers配置&quot;&gt;taskmanager.network.numberOfBuffers配置&lt;/h3&gt;

解决该问题需要对Flink的Task数据交换&amp;amp;内存管理有深刻的理解，可参考文章：
&lt;a href=&quot;https://cwiki.apache.org/confluence/display/FLINK/Data+exchange+between+tasks&quot;&gt;https://cwiki.apache.org/confluence/display/FLINK/Data+exchange+between+tasks&lt;/a&gt;

&lt;strong&gt;环境：&lt;/strong&gt;

&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Job&lt;/code&gt;的运行&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Topology&lt;/code&gt;图：
&lt;pre&gt;&lt;code class=&quot;language-mermaid&quot;&gt;graph LR
A(Task-Front `parallelism:30`) --&amp;gt;|shuffle| D(Task-End `parallelism:90`)
&lt;/code&gt;&lt;/pre&gt;
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Task-Front&lt;/code&gt;负责从Kafka读取数据、清洗、校验、格式化，其并行度为30；
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Task-End&lt;/code&gt;负责数据的解析&amp;amp;处理，并发送到不同的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Kafka Topic&lt;/code&gt;；
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Task-Front&lt;/code&gt;并行度为30，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Task-End&lt;/code&gt;并行度为90；
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Task-Front&lt;/code&gt;通过网络IO将数据传递给&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Task-End&lt;/code&gt;；

Job压测环境：
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Flink 1.3.2&lt;/code&gt;的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Standalone&lt;/code&gt;模式；
2个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;JobManager&lt;/code&gt;高可用；
8个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TaskManager&lt;/code&gt;（一台物理机一个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TaskManager&lt;/code&gt;），主要配置如下：
&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;s&quot;&gt;taskmanager.numberOfTaskSlots&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;13&lt;/span&gt;
&lt;span class=&quot;s&quot;&gt;taskmanager.network.numberOfBuffers&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;10240&lt;/span&gt;
&lt;span class=&quot;s&quot;&gt;taskmanager.memory.segment-size&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;32768&lt;/span&gt;   &lt;span class=&quot;c1&quot;&gt;# 32K&lt;/span&gt;
&lt;span class=&quot;s&quot;&gt;taskmanager.memory.size&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;15g&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;strong&gt;问题：&lt;/strong&gt;
在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Standalone&lt;/code&gt;下由于&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;taskmanager.network.numberOfBuffers&lt;/code&gt;配置数量过小导致&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;data exchange buffer&lt;/code&gt;不够用，消费&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Kafka&lt;/code&gt;数据的速度减缓并出现大幅度的抖动，严重情况下导致&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FlinkKafkaConsumer&lt;/code&gt;停止消费数据，从&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;JMX&lt;/code&gt;监控看到如下现象：
：
&lt;img src=&quot;/images/posts/flink/task-buffer-1.png&quot; alt=&quot;buffer-1&quot; /&gt;
&lt;img src=&quot;/images/posts/flink/task-buffer-2.png&quot; alt=&quot;buffer-2&quot; /&gt;
&lt;img src=&quot;/images/posts/flink/task-buffer-3.png&quot; alt=&quot;buffer-3&quot; /&gt;

&lt;strong&gt;分析：&lt;/strong&gt;
从&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;JMX&lt;/code&gt;监控看，主要是因为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Buffer&lt;/code&gt;原因导致的线程&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;wait&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cpu&lt;/code&gt;抖动；
共8台机器，每台机器上有13个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Slot&lt;/code&gt;，根据&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Job&lt;/code&gt;是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;30 + 90&lt;/code&gt;的并行度来看，每台机器上同时存在多个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Task-Front&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Task-End&lt;/code&gt;；
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Task-Front&lt;/code&gt;需将处理好的数据存储到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ResultPartition&lt;/code&gt;；
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Task-End&lt;/code&gt;需将接收到的数据存储到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;InputGate&lt;/code&gt;以备后续处理；
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Task-End&lt;/code&gt;将处理好的数据直接发送给&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Kafka Topic&lt;/code&gt;，所以不需要&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ResultPartition&lt;/code&gt;；
一个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Task-Front&lt;/code&gt;需将自己处理的数据&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;shuffle&lt;/code&gt;到90个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Task-End&lt;/code&gt;；

通过&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Flink Buffer&lt;/code&gt;分配原理可知，需要&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;30*90&lt;/code&gt;个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ResultPartition&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;90*30&lt;/code&gt;个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;InputGate&lt;/code&gt;；
若平均分则每个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ResultPartition/InputGate&lt;/code&gt;可分配到的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Buffer&lt;/code&gt;数量是：
(10240 * 8) / (30*90 + 90 * 30) ≈ 15个(每个32k)；
在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;8w+/s, 5~10k/条&lt;/code&gt;的数据量下的15个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Buffer&lt;/code&gt;会在很短时间被填满，造成&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Buffer&lt;/code&gt;紧缺，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Task-Front&lt;/code&gt;无法尽快将数据&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;shuffle&lt;/code&gt;到下游，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Task-End&lt;/code&gt;无法获取足够的数据来处理；

&lt;strong&gt;解决：&lt;/strong&gt;
增大&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;taskmanager.network.numberOfBuffers&lt;/code&gt;数量，最好保证每个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ResultPartition/InputGate&lt;/code&gt;分配到90+（压测预估值，不保证效果最佳）的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Buffer&lt;/code&gt;数量；

&lt;strong&gt;补充：&lt;/strong&gt;
在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OnYarn&lt;/code&gt;下几乎不会遇到该为题，了解该问题有助于理解&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Flink&lt;/code&gt;内存管理&amp;amp;数据传递；
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Flink 1.5&lt;/code&gt;之后&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Buffer&lt;/code&gt;默认用堆外内存，并&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;deprecated&lt;/code&gt;了&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;taskmanager.network.numberOfBuffers&lt;/code&gt;，用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;taskmanager.network.memory.max&lt;/code&gt;与&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;taskmanager.network.memory.min&lt;/code&gt;代替；
关于&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Buffer&lt;/code&gt;分配&amp;amp;动态调整等逻辑可关注以下几个类：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TaskManagerRunner&lt;/code&gt; / &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Task&lt;/code&gt; / &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NetworkEnvironment&lt;/code&gt; / &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LocalBufferPool&lt;/code&gt; / &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;InputGate&lt;/code&gt; / &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ResultPartition&lt;/code&gt; / &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MemorySegment&lt;/code&gt;，在此不做源码解读，最后贴两个截图：
&lt;img src=&quot;/images/posts/flink/task-buffer-4.png&quot; alt=&quot;buffer-4&quot; /&gt;
&lt;img src=&quot;/images/posts/flink/task-buffer-5.png&quot; alt=&quot;buffer-5&quot; /&gt;

&lt;h3 id=&quot;onyarn下log的打印&quot;&gt;OnYarn下log的打印&lt;/h3&gt;

&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Standalone&lt;/code&gt;与&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OnYarn&lt;/code&gt;下的日志配置和输出路径有较大的区别，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Standalone&lt;/code&gt;下直接在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;maven&lt;/code&gt;的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;resources&lt;/code&gt;目录下配置&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;log4j2.xml/log4j.properties&lt;/code&gt;即可完成日志配置，但在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OnYarn&lt;/code&gt;下 需要通过修改&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;${flink.dir}/conf/log4j.properties&lt;/code&gt;的配置来定义日志输出，这些配置在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;container.sh&lt;/code&gt;启动&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TaskManager/JobManager&lt;/code&gt;时被加载以初始化&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;log4j&lt;/code&gt;，配置如下：

&lt;div class=&quot;language-properties highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# Log all infos in the given file
&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;log4j.appender.file&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;org.apache.log4j.DailyRollingFileAppender&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# ${log.file} input by luanch_container.sh
&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;log4j.appender.file.file&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;${log.file}&lt;/span&gt;
&lt;span class=&quot;py&quot;&gt;log4j.appender.file.append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;false&lt;/span&gt;
&lt;span class=&quot;py&quot;&gt;log4j.appender.file.encoding&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;UTF-8&lt;/span&gt;
&lt;span class=&quot;py&quot;&gt;log4j.appender.file.DatePattern&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'.'yyyy-MM-dd&lt;/span&gt;
&lt;span class=&quot;py&quot;&gt;log4j.appender.file.layout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;org.apache.log4j.PatternLayout&lt;/span&gt;
&lt;span class=&quot;py&quot;&gt;log4j.appender.file.layout.ConversionPattern&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p %-60c %x - %m%n&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;${flink.dir}/conf&lt;/code&gt;下不同log配置文件所适用的范围如下图：
&lt;img src=&quot;/images/posts/flink/log-path-1.png&quot; alt=&quot;log ui&quot; /&gt;

&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;${flink.dir}&lt;/code&gt;中默认不包含&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DailyRollingFileAppender&lt;/code&gt;的依赖，所以在使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DailyRollingFileAppender&lt;/code&gt;时还需要添加依赖&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apache-log4j-extras-1.2.17.jar&lt;/code&gt;到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;${flink.dir}/lib/&lt;/code&gt;目录，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Flink Job&lt;/code&gt;的日志会输出到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Yarn Container&lt;/code&gt;的日志目录下（由&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;yarn-site.xml&lt;/code&gt;中的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;yarn.nodemanager.log-dirs&lt;/code&gt;指定），&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Container&lt;/code&gt;的启动脚本（可在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;YarnUI log&lt;/code&gt;中找到）示例：

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;// log.file 即日志输出目录
&lt;span class=&quot;nb&quot;&gt;exec&lt;/span&gt; /bin/bash &lt;span class=&quot;nt&quot;&gt;-c&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$JAVA_HOME&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;/bin/java -Xms1394m -Xmx1394m -XX:MaxDirectMemorySize=654m -XX:+UseG1GC -XX:MaxGCPauseMillis=100 -Dlog.file=/home/hadoop/apache/hadoop/latest/logs/userlogs/application_1547994202702_0027/container_e02_1547994202702_0027_01_000002/taskmanager.log -Dlogback.configurationFile=file:./logback.xml -Dlog4j.configuration=file:./log4j.properties org.apache.flink.yarn.YarnTaskExecutorRunner --configDir . 1&amp;gt; /home/hadoop/apache/hadoop/latest/logs/userlogs/application_1547994202702_0027/container_e02_1547994202702_0027_01_000002/taskmanager.out 2&amp;gt; /home/hadoop/apache/hadoop/latest/logs/userlogs/application_1547994202702_0027/container_e02_1547994202702_0027_01_000002/taskmanager.err&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;strong&gt;运行中的FlinkOnYarn Job日志查看：&lt;/strong&gt;
直接通过&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;YarnUI&lt;/code&gt;界面查看每个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Container&lt;/code&gt;的日志输出，如下图：
&lt;img src=&quot;/images/posts/flink/log-path-3.png&quot; alt=&quot;log ui&quot; /&gt;

在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;container&lt;/code&gt;所在节点的对应目录下通过&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tail, less&lt;/code&gt;等shell命令查看日志文件，如下图：
&lt;img src=&quot;/images/posts/flink/log-path-2.png&quot; alt=&quot;log ui&quot; /&gt;

在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;hadoop&lt;/code&gt;配置&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;enable log aggregation&lt;/code&gt;时，可以通过&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;yarn logs -applicationId ${application_id}&lt;/code&gt;获取log；

&lt;strong&gt;已完成的FlinkOnYarn Job日志查看：&lt;/strong&gt;
在配置&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;hadoop log aggregation&lt;/code&gt;时，可以通过&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;yarn logs -applicationId ${application_id}&lt;/code&gt;获取log；

&lt;strong&gt;注：&lt;/strong&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Spark&lt;/code&gt;中&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HistoryServer&lt;/code&gt;支持用户从&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;YarnUI&lt;/code&gt;中查看已完成&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Job&lt;/code&gt;的日志等，但&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Flink&lt;/code&gt;中的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HistoryServer&lt;/code&gt;在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OnYarn&lt;/code&gt;下不可用，留下问题后续解决；

&lt;h3 id=&quot;prometheus监控&quot;&gt;Prometheus监控&lt;/h3&gt;

&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FlinkUI&lt;/code&gt;的指标监控使用起来不够方便，同时在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Job&lt;/code&gt;比较大时往&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FlinkUI&lt;/code&gt;上添加监控指标时卡顿现象非常明显，需要选择一个更好的Job监控工具来完成Job的监控任务，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Prometheus&lt;/code&gt;是一个很好的选择；具体说明可参考官方Doc：&lt;a href=&quot;https://ci.apache.org/projects/flink/flink-docs-release-1.7/monitoring/metrics.html&quot;&gt;https://ci.apache.org/projects/flink/flink-docs-release-1.7/monitoring/metrics.html&lt;/a&gt;；
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Flink&lt;/code&gt;从1.4版本开始支持&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Prometheus&lt;/code&gt;监控&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PrometheusReporter&lt;/code&gt;，在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Flink1.6&lt;/code&gt;时有加入了&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PrometheusPushGatewayReporter&lt;/code&gt;的功能；
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PrometheusReporter&lt;/code&gt;是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Prometheus&lt;/code&gt;被动通过&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Http&lt;/code&gt;请求&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Flink&lt;/code&gt;集群以获取指标数据；
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PrometheusPushGatewayReporter&lt;/code&gt;是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Flink&lt;/code&gt;主动将指标推送到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Gateway&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Prometheus&lt;/code&gt;从&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Gateway&lt;/code&gt;获取指标数据；

&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OnYarn&lt;/code&gt;下&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Flink&lt;/code&gt;端开启&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Prometheus&lt;/code&gt;监控的配置：
&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;s&quot;&gt;// Flink可以同时定义多个reportor，本示例定义了jmx和prometheus两种reportor&lt;/span&gt;
&lt;span class=&quot;s&quot;&gt;// JMX用RMI，Prom用Http&lt;/span&gt;
&lt;span class=&quot;s&quot;&gt;metrics.reporters&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;my_jmx_reporter,prom&lt;/span&gt;
&lt;span class=&quot;s&quot;&gt;metrics.reporter.my_jmx_reporter.class&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;org.apache.flink.metrics.jmx.JMXReporter&lt;/span&gt;
&lt;span class=&quot;s&quot;&gt;metrics.reporter.my_jmx_reporter.port&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;9020-9045&lt;/span&gt;

&lt;span class=&quot;s&quot;&gt;metrics.reporter.prom.class&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;org.apache.flink.metrics.prometheus.PrometheusReporter&lt;/span&gt;
&lt;span class=&quot;s&quot;&gt;metrics.reporter.prom.port&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;9250-9275&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

当&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Job&lt;/code&gt;分配到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Yarn Container&lt;/code&gt;启动&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;JobManager/TaskManager&lt;/code&gt;的过程中会实例化&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;metrics.reporter.xxx.class&lt;/code&gt;配置的类，启动监控服务；
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PrometheusReporter&lt;/code&gt;是通过暴漏Http地址来提供监控指标访问功能的，在每个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;JobManager/TaskManager&lt;/code&gt;启动的过程中都会实例化一个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PrometheusReporter&lt;/code&gt;对象，并启动&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HttpServer&lt;/code&gt;服务绑定到一个端口上
（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;metrics.reporter.prom.port&lt;/code&gt;配置的区间从小到大第一个未占用的端口）；因此&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;metrics.reporter.prom.port&lt;/code&gt;配置的区间可允许的最大端口数必须大于等于一台机器最大可启动的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Container&lt;/code&gt;数量，
否则会有监控服务启动失败的报错，如下：

&lt;strong&gt;报错：&lt;/strong&gt;
&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;mi&quot;&gt;2019&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mo&quot;&gt;03&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;12&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;44&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;39&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;623&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;INFO&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;org&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;apache&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;flink&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;runtime&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;metrics&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;MetricRegistryImpl&lt;/span&gt;           &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Configuring&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;prom&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;port&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;9250&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;org&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;apache&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;flink&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;metrics&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;prometheus&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;PrometheusReporter&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;}.&lt;/span&gt;
&lt;span class=&quot;mi&quot;&gt;2019&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mo&quot;&gt;03&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;12&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;44&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;39&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;627&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;ERROR&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;org&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;apache&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;flink&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;runtime&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;metrics&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;MetricRegistryImpl&lt;/span&gt;           &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Could&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;instantiate&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;metrics&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;reporter&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;prom&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Metrics&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;might&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;be&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;exposed&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reported&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;lang&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;RuntimeException&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Could&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;start&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;PrometheusReporter&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;HTTP&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;server&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;on&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;any&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;configured&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;port&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;nl&quot;&gt;Ports:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;9250&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;at&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;org&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;apache&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;flink&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;metrics&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;prometheus&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;PrometheusReporter&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;open&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;PrometheusReporter&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;70&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;at&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;org&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;apache&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;flink&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;runtime&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;metrics&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;MetricRegistryImpl&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;init&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;MetricRegistryImpl&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;153&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;at&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;org&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;apache&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;flink&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;runtime&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;taskexecutor&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;TaskManagerRunner&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;init&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;TaskManagerRunner&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;137&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;at&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;org&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;apache&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;flink&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;runtime&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;taskexecutor&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;TaskManagerRunner&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;runTaskManager&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;TaskManagerRunner&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;330&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;at&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;org&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;apache&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;flink&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;runtime&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;taskexecutor&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;TaskManagerRunner&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;call&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;TaskManagerRunner&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;301&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;at&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;org&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;apache&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;flink&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;runtime&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;taskexecutor&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;TaskManagerRunner&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;call&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;TaskManagerRunner&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;298&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;总结&quot;&gt;总结&lt;/h2&gt;

本文简单总结了Flink使用过程中遇到的几个重要的问题，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;taskmanager.network.numberOfBuffers配置&lt;/code&gt;的问题有在stackoverflow, FlinkCommunity问过，但没得到较为准确的答案，最终通过gg文档&amp;amp;源码才逐渐理解，源码&amp;amp;Google的重要性不言而喻！

&lt;h2 id=&quot;参考&quot;&gt;参考&lt;/h2&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://cwiki.apache.org/confluence/display/FLINK/Data+exchange+between+tasks&quot;&gt;https://cwiki.apache.org/confluence/display/FLINK/Data+exchange+between+tasks&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://ci.apache.org/projects/flink/flink-docs-release-1.7/flinkDev/building.html&quot;&gt;https://ci.apache.org/projects/flink/flink-docs-release-1.7/flinkDev/building.html&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://ci.apache.org/projects/flink/flink-docs-release-1.7/dev/stream/state/custom_serialization.html&quot;&gt;https://ci.apache.org/projects/flink/flink-docs-release-1.7/dev/stream/state/custom_serialization.html&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://ci.apache.org/projects/flink/flink-docs-master/dev/types_serialization.html&quot;&gt;https://ci.apache.org/projects/flink/flink-docs-master/dev/types_serialization.html&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://ci.apache.org/projects/flink/flink-docs-release-1.7/monitoring/debugging_classloading.html&quot;&gt;https://ci.apache.org/projects/flink/flink-docs-release-1.7/monitoring/debugging_classloading.html&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://ci.apache.org/projects/flink/flink-docs-release-1.7/flinkDev/building.html&quot;&gt;https://ci.apache.org/projects/flink/flink-docs-release-1.7/flinkDev/building.html&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://ci.apache.org/projects/flink/flink-docs-release-1.7/monitoring/logging.html&quot;&gt;https://ci.apache.org/projects/flink/flink-docs-release-1.7/monitoring/logging.html&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;https://ci.apache.org/projects/flink/flink-docs-release-1.7/monitoring/historyserver.html&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://ci.apache.org/projects/flink/flink-docs-release-1.7/monitoring/debugging_classloading.html&quot;&gt;https://ci.apache.org/projects/flink/flink-docs-release-1.7/monitoring/debugging_classloading.html&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://ci.apache.org/projects/flink/flink-docs-release-1.7/monitoring/metrics.html&quot;&gt;https://ci.apache.org/projects/flink/flink-docs-release-1.7/monitoring/metrics.html&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://ci.apache.org/projects/flink/flink-docs-release-1.7/dev/types_serialization.html&quot;&gt;https://ci.apache.org/projects/flink/flink-docs-release-1.7/dev/types_serialization.html&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</description>
        <pubDate>Sun, 31 Mar 2019 00:00:00 +0000</pubDate>
        <link>https://itinycheng.github.io/2019/03/31/issues-encountered-druing-flink-migration/</link>
        <guid isPermaLink="true">https://itinycheng.github.io/2019/03/31/issues-encountered-druing-flink-migration/</guid>
        
        <category>flink</category>
        
        <category>yarn</category>
        
        
        <category>flink</category>
        
      </item>
    
      <item>
        <title>Scala&amp;Java的协变与逆变</title>
        <description>&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#前言&quot; id=&quot;markdown-toc-前言&quot;&gt;前言&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#术语讲解&quot; id=&quot;markdown-toc-术语讲解&quot;&gt;术语讲解&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#scala语法讲解&quot; id=&quot;markdown-toc-scala语法讲解&quot;&gt;Scala语法讲解&lt;/a&gt;    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#协变covariant&quot; id=&quot;markdown-toc-协变covariant&quot;&gt;协变（covariant）&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#逆变contravariance&quot; id=&quot;markdown-toc-逆变contravariance&quot;&gt;逆变（contravariance）&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#不变invariance&quot; id=&quot;markdown-toc-不变invariance&quot;&gt;不变（invariance）&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#协变逆变应用&quot; id=&quot;markdown-toc-协变逆变应用&quot;&gt;协变逆变应用&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#总结&quot; id=&quot;markdown-toc-总结&quot;&gt;总结&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#参考&quot; id=&quot;markdown-toc-参考&quot;&gt;参考&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;前言&quot;&gt;前言&lt;/h2&gt;

第一次听说&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;协变&amp;amp;逆变&lt;/code&gt;是刚接触&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Scala&lt;/code&gt;的时候，但&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;协变&amp;amp;逆变&lt;/code&gt;却不是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Scala&lt;/code&gt;所特有的，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Java&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;C#&lt;/code&gt;等语言也有&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;协变&amp;amp;逆变&lt;/code&gt;的概念，本文首先会解释&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;协变&amp;amp;逆变&lt;/code&gt;这两个术语的含义，然后进一步探讨它们在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Scala&lt;/code&gt;中具体的书写语法和使用场景。

&lt;h2 id=&quot;术语讲解&quot;&gt;术语讲解&lt;/h2&gt;

&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;协变&amp;amp;逆变&lt;/code&gt;的概念入门可以关注下边这两个篇文章，本段内容也是学习这两篇文章所得到的：&lt;a href=&quot;https://www.clear.rice.edu/comp310/JavaResources/generics/co_contra_host_visitor.html&quot;&gt;Covariance and Contravariance of Hosts and Visitors&lt;/a&gt;, &lt;a href=&quot;https://www.originate.com/thinking/stories/cheat-codes-for-contravariance-and-covariance/&quot;&gt;Cheat Codes for Contravariance and Covariance&lt;/a&gt;。

&lt;ul&gt;
  &lt;li&gt;定义三个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;实体&lt;/code&gt;：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Fuel&lt;/code&gt;,&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt; Plant&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Bamboo&lt;/code&gt;；图中的符号&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;gt;&lt;/code&gt;表示可替代性（类比多态：子类可作为父类使用），&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Plant&lt;/code&gt;可以做为这一种&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Fuel&lt;/code&gt;（燃料），&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Bamboo&lt;/code&gt;是一种&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Plant&lt;/code&gt;，在作为燃料的这件事情上也可以认为 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Bamboo extends Plant&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Plant extends Fuel&lt;/code&gt;；&lt;/li&gt;
  &lt;li&gt;定义&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Box&lt;/code&gt;：用于实体（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Fuel&lt;/code&gt;,&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt; Plant&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Bamboo&lt;/code&gt;）的存放；
&lt;img src=&quot;/images/posts/variance/entities.png&quot; alt=&quot;entities&quot; /&gt;&lt;/li&gt;
  &lt;li&gt;定义三个可消费实体的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;消费者&lt;/code&gt;：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Burn&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Animal&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Panda&lt;/code&gt;；
&lt;img src=&quot;/images/posts/variance/consumers.png&quot; alt=&quot;consumers&quot; /&gt;
&lt;br /&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;
&lt;span class=&quot;cm&quot;&gt;/** box contains entities */&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Box&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;cm&quot;&gt;/**
   * 若有一个Box&amp;lt;Plant&amp;gt;，可以用BoxVisitor&amp;lt;Fuel,_&amp;gt;, BoxVisitor&amp;lt;Plant,_&amp;gt;来消费他，不能用BoxVisitor&amp;lt;Bamboo,_&amp;gt;来消费；
   * 对于BoxVisitor来讲T是逆变的，只能接收类型为T或T父类的BoxVisitor，即BoxVisitor&amp;lt;? super T&amp;gt;；
   *
   */&lt;/span&gt;
  &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;R&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;R&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;accept&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;BoxVisitor&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;?&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;super&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;R&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;visitor&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;cm&quot;&gt;/** consume entities in box */&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;BoxVisitor&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;U&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;R&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;R&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;visit&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Box&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;?&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;U&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;box&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Burn&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;R&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;BoxVisitor&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Fuel&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;R&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;cm&quot;&gt;/**
   * 对于Burn而言，它可消费Fuel,Plant,Bamboo，所以visit可接收任何有Fuel属性的Box；
   * 由此Box&amp;lt;T&amp;gt;可以被认为是`协变`的，即如果T extends U 则 Box&amp;lt;T&amp;gt; extends Box&amp;lt;U&amp;gt;
   */&lt;/span&gt;
  &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;R&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;visit&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Box&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;?&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Fuel&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;box&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;协变&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Burn&lt;/code&gt;可以燃烧一切包括：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Fuel&lt;/code&gt;, ` Plant&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;, &lt;/code&gt;Bamboo&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;；&lt;/code&gt;Animal&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;可以吃所有类型的植物包括：&lt;/code&gt;Plant&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;, &lt;/code&gt;Bamboo&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;；&lt;/code&gt;Panda&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;只吃一种植物：&lt;/code&gt;Bamboo`。由此，可得出下面这关系张图：
&lt;img src=&quot;/images/posts/variance/covariance-of-boxes.png&quot; alt=&quot;covariance-of-boxes&quot; /&gt;

如果消费者是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Burn&lt;/code&gt;，我可以把&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Box&amp;lt;Fuel&amp;gt;&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Box&amp;lt;Plant&amp;gt;&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Box&amp;lt;Bamboo&amp;gt;&lt;/code&gt;传递给它；如果消费者是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Animal&lt;/code&gt;，我可以把&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Box&amp;lt;Plant&amp;gt;&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Box&amp;lt;Bamboo&amp;gt;&lt;/code&gt;传递给它；如果消费者是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Panda&lt;/code&gt;，我可以把&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Box&amp;lt;Bamboo&amp;gt;&lt;/code&gt;传递给它。

对于&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Box&amp;lt;T&amp;gt;&lt;/code&gt;而言，它存储的实体对象用于被使用/消费，所以它是一个生产者，若能将&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Box&amp;lt;Fuel&amp;gt;&lt;/code&gt;传给某个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BoxVisitor&lt;/code&gt;，则也能将&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Box&amp;lt;Plant&amp;gt;&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Box&amp;lt;Bamboo&amp;gt;&lt;/code&gt;传递给这个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BoxVisitor&lt;/code&gt;，这时&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Box&amp;lt;Fuel&amp;gt;&lt;/code&gt;中的泛型是上界（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;upper bound&lt;/code&gt;），需要用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;extends&lt;/code&gt;关键字来定义&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Box&amp;lt;T&amp;gt;&lt;/code&gt;的范围，该范围能被上文中的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BoxVisitor&lt;/code&gt;消费。即，若&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BoxVisitor&lt;/code&gt;能接收装满&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;T&lt;/code&gt;的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Box&lt;/code&gt;，则肯定能接收装满&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;T&lt;/code&gt;子类的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Box&lt;/code&gt;（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Box&amp;lt;T&amp;gt;&lt;/code&gt;是生产者，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;remember: Prdoucer Extends&lt;/code&gt;），&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Box&amp;lt;T&amp;gt;&lt;/code&gt;随着它的泛型&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;T&lt;/code&gt;进行协变，协变的简单定义：

如果：

&lt;pre&gt;&lt;code class=&quot;language-mermaid&quot;&gt;graph LR
A(Class A) --&amp;gt;|extends| B(Class B)
&lt;/code&gt;&lt;/pre&gt;

则：

&lt;pre&gt;&lt;code class=&quot;language-mermaid&quot;&gt;graph LR
A(Box &amp;amp;lt A &amp;amp;gt) --&amp;gt;|extends| B(Box &amp;amp;lt B &amp;amp;gt)
&lt;/code&gt;&lt;/pre&gt;

&lt;strong&gt;注：&lt;/strong&gt;：Java中的数组是协变的，如下列Java代码可正常编译，但运行时会抛出&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ArrayStoreException&lt;/code&gt;：

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nc&quot;&gt;Box&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;Bamboo&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bamboos&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Box&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;Bamboo&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;];&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;bamboos&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Box&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;Bamboo&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;nc&quot;&gt;Stream&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;of&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bamboos&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;forEach&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nc&quot;&gt;Box&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;Plant&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;plants&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bamboos&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;plants&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Box&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;Plant&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// throw ArrayStoreException at running stage.&lt;/span&gt;
&lt;span class=&quot;nc&quot;&gt;Stream&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;of&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;plants&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;forEach&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;逆变&lt;/strong&gt;
可以将装满&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Fuel&lt;/code&gt;的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Box&lt;/code&gt;，传递给&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Burn&lt;/code&gt;（汽油只可以燃烧）；可以将装满&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Plant&lt;/code&gt;的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Box&lt;/code&gt;传递给&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Burn&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Animal&lt;/code&gt;（植物可以燃烧&amp;amp;吃）；可以将装满&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Bamboo&lt;/code&gt;的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Box&lt;/code&gt;传递给&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Burn&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Animal&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Panda&lt;/code&gt;（竹子可以燃烧，可以给动物&amp;amp;熊猫吃）。由此我们可以得出下面的这张图：
&lt;img src=&quot;/images/posts/variance/contravariance-of-visitors.png&quot; alt=&quot;covariance-of-boxes&quot; /&gt;&lt;/li&gt;
&lt;/ul&gt;

如果我有&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Box&amp;lt;Fuel&amp;gt;&lt;/code&gt;，我可以把它传递消费者&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Burn&lt;/code&gt;；如果我有&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Box&amp;lt;Plant&amp;gt;&lt;/code&gt;，我可以把它传递给消费者&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Animal&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Burn&lt;/code&gt;；如果我有&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Box&amp;lt;Bamboo&amp;gt;&lt;/code&gt;，我可以把它传递给消费者&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Panda&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Animal&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Burn&lt;/code&gt;。

对于&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BoxVisitor&amp;lt;T&amp;gt;&lt;/code&gt;本身而言，它是一个可消费&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;T&amp;gt;&lt;/code&gt;的消费者，若一个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Box&amp;lt;Bamboo&amp;gt;&lt;/code&gt;能被&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Panda&lt;/code&gt;（即: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BoxVisitor&amp;lt;Bamboo&amp;gt;&lt;/code&gt;）消费，则其必然也能被&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Animal&lt;/code&gt;（即: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BoxVisitor&amp;lt;Plant&amp;gt;&lt;/code&gt;）, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Burn&lt;/code&gt;（即: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BoxVisitor&amp;lt;Fuel&amp;gt;&lt;/code&gt;）消费，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BoxVisitor&amp;lt;Bamboo&amp;gt;&lt;/code&gt;中的泛型&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Bamboo&lt;/code&gt;是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BoxVisitor&lt;/code&gt;的下界（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lower bound&lt;/code&gt;），需用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;supper&lt;/code&gt;来定义&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BoxVisitor&amp;lt;T&amp;gt;&lt;/code&gt;中&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;T&lt;/code&gt;的取值范围，这个范围内的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BoxVisitor&lt;/code&gt;都可用于消费&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Box&amp;lt;T&amp;gt;&lt;/code&gt;（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BoxVisitor&amp;lt;T&amp;gt;&lt;/code&gt;是消费者，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;remember: Consumer Super&lt;/code&gt;），&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BoxVisitor&amp;lt;T&amp;gt;&lt;/code&gt;是逆变的，逆变简单定义：

如果：

&lt;pre&gt;&lt;code class=&quot;language-mermaid&quot;&gt;graph LR
A(Class A) --&amp;gt;|extends| B(Class B)
&lt;/code&gt;&lt;/pre&gt;

则：

&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BoxVisitor&amp;lt;Parent&amp;gt; extends BoxVisitor&amp;lt;Child&amp;gt;&lt;/code&gt;

&lt;pre&gt;&lt;code class=&quot;language-mermaid&quot;&gt;graph LR
B(BoxVisitor &amp;amp;lt B &amp;amp;gt) --&amp;gt;|extends| A(BoxVisitor &amp;amp;lt A &amp;amp;gt)
&lt;/code&gt;&lt;/pre&gt;

&lt;h2 id=&quot;scala语法讲解&quot;&gt;Scala语法讲解&lt;/h2&gt;

协变&amp;amp;逆变在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Java&lt;/code&gt;中并没有上升到语法层面，而&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Scala&lt;/code&gt;在语法层面对协变&amp;amp;逆变进行的详细的语法设计，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Scala&lt;/code&gt;中用类型参数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[T]&lt;/code&gt;表示不变，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[+T]&lt;/code&gt;表示协变，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[-T]&lt;/code&gt;表示逆变，具体&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Scala&lt;/code&gt;中协变&amp;amp;逆变的语法特性&amp;amp;约束会在下文进行详细的分析讲解。

定义若干类结构：

&lt;div class=&quot;language-scala highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Fuel&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Plant&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Fuel&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Bamboo&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Plant&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// 声明类型`Box`，其类型参数`T`是协变的&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Box&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;+T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;](&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ts&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;T*&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// 类型参数逆变&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;BoxVisitor&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;-T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;协变covariant&quot;&gt;协变（covariant）&lt;/h3&gt;

一个类可被定义为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;协变&lt;/code&gt;的前提是：这个类型类似&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Producer Extends&lt;/code&gt;，并且是只读的（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;read-only&lt;/code&gt;），若非只读则会出现如下问题场景（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ArrayStoreException&lt;/code&gt;异常）：

&lt;div class=&quot;language-scala highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;// 假设Array是协变的&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// 创建一个Circle数组并装入10个Circle对象&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;circles&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Array&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Circle&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;Array&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;fill&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Circle&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(..))&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// 当Aarry[T]是协变时该行代码有效&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;shapes&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Array&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Shape&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;circles&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// 将数组中第一个元素修改为Square对象（Square is a subtype of Shape）&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// 若协变非read-only,此处编译通过，但运行会抛异常（向Array[Circle]添加Square对象，这是不被允许的）&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;shapes&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Square&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(..)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;strong&gt;子类对象赋值给父类变量&lt;/strong&gt;

因为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Bamboo &amp;lt;: Plant &amp;lt;: Fuel&lt;/code&gt;而且&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Box[+T]&lt;/code&gt;是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;协变&lt;/code&gt;的，所以&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Box[Bamboo] &amp;lt;: Box[Plant] &amp;lt;: Box[Fuel]&lt;/code&gt;，同时由多态原则：子类可看做父类，最终得出下边有效代码：

&lt;div class=&quot;language-scala highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;bamboos&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Box&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Bamboo&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Box&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Bamboo&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;](&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Bamboo&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;())&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// 根据协变特性，下列代码可正常运行&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;plants&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Box&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Plant&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bamboos&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;fuels&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Box&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Fuel&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bamboos&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;strong&gt;协变类型内部函数限定&lt;/strong&gt;

&lt;div class=&quot;language-scala highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;cm&quot;&gt;/**
 * covariance：readonly, non-writable
 * 即：入参：限定不能将A &amp;amp; A的子类传递给Foo
 * 返回值：无限制
 */&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;trait&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Foo&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;+T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// def fun[①](②)(op: ③): ④&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;fun0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;U&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;&amp;gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;](&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;u&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;U&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;op&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;U&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;U&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;fun1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;U&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;&amp;gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;](&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;u&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;U&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;op&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;U&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;U&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

定义的协变类&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Foo[+T]&lt;/code&gt;，类内部定义函数的通式包含着组成函数的所有元素：①：类型参数定义，②：对象类型入参定义，③：函数类型的入参定义，④：返回值/函数定义，在协变类里每个位置的条件限制如下：
①类型参数：不能定义子类类型&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[U]&lt;/code&gt;（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[U &amp;lt;: T]&lt;/code&gt;），原因：Covariant type T occurs in contravariant position in type T of value U；
②对象入参：入参的类型不能为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;T&lt;/code&gt; &amp;amp; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;subType of T&lt;/code&gt;，原因：Covariant type T occurs in contravariant position in type T of value u；
③函数入参：函数的出参不能为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;T&lt;/code&gt; &amp;amp; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;subType of T&lt;/code&gt;，原因：Covariant type T occurs in contravariant position in type U =&amp;gt; T of value op；
④返回值：无限制；
④返回函数： 函数入参不能为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;T&lt;/code&gt; &amp;amp; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;subType of T&lt;/code&gt;，原因：Covariant type T occurs in contravariant position in type T =&amp;gt; T of value fun；

&lt;em&gt;In A Picture:&lt;/em&gt;
&lt;img src=&quot;/images/posts/variance/covariance-class-inner.png&quot; alt=&quot;covariance-class-inner&quot; /&gt;
逆变位置：只允许定义或传入&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;super T&lt;/code&gt;的类型，或是与&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;T&lt;/code&gt;无父子关系的类型；
协变位置：对类型无限制，可任意定义；
个人理解：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;协变&lt;/code&gt;可看做生产者&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Producer Extends&lt;/code&gt;并且 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;readonly&lt;/code&gt;；所以协变类内部不能存在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;add(_ extends T)&lt;/code&gt;的函数，对函数的返回值则无限制，所以就有了上图中的语法约束；

&lt;h3 id=&quot;逆变contravariance&quot;&gt;逆变（contravariance）&lt;/h3&gt;
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;逆变&lt;/code&gt;是与&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;协变&lt;/code&gt;相对的一个概念，可以将&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;逆变&lt;/code&gt;看做消费者&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Comsumer super&lt;/code&gt;，并且是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;write-only&lt;/code&gt;的，若非&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;write-only&lt;/code&gt;会出现如下问题场景：

&lt;div class=&quot;language-scala highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;// 假设Array是逆变的&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// 首先创建数组并装入Shape对象&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;shapes&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Array&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Shape&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;Array&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;fill&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Shape&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(..),&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Shape&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(..))&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// Works only if Array is contravariant&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;circles&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Array&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Circle&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;shapes&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// 编译异常，circles(0)实际是Shape对象，是Circle的父类&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;circle&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Circle&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;circles&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;strong&gt;父类对象赋值给子类变量&lt;/strong&gt;

&lt;div class=&quot;language-scala highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;  &lt;span class=&quot;c1&quot;&gt;// 由于BoxVisitor是逆变的，所以下边代码可正常编译运行&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;fuelVisitor&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;BoxVisitor&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Fuel&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;BoxVisitor&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Fuel&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;plantVisitor&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;BoxVisitor&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Plant&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fuelVisitor&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;bambooVisitor&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;BoxVisitor&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Bamboo&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fuelVisitor&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// 作为消费者，能消费类型`T`则必定能消费T的父类，但不一定能消费子类，&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// 因为子类在拥有所有父类可被外部访问的变量和方法的同时扩展或覆盖了父类的变量和方法&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;strong&gt;逆变类型的内部函数限定&lt;/strong&gt;

&lt;div class=&quot;language-scala highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;cm&quot;&gt;/**
 * contravariance：write-only, non-readable
 * consumer super
 */&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;trait&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Foo&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;-T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// def fun[①](②)(op: ③): ④&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;fun0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;U&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;&amp;lt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;](&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;u&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;U&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;op&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;U&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;U&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;fun1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;U&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;&amp;lt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;](&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;u&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;U&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;op&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;U&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;U&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

在逆变类&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Foo[-T]&lt;/code&gt;中函数通式（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;def fun[①](②)(op: ③): ④&lt;/code&gt;）的各位置的条件限制如下：
①类型参数：不能定义父类类型&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[U]&lt;/code&gt;（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[U &amp;gt;: T]&lt;/code&gt;），原因：Contravariant type T occurs in covariant position in type T of value U；
②对象入参：无限制；
③函数入参：函数的入参不能为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;T&lt;/code&gt; &amp;amp; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;superType of T&lt;/code&gt;，原因：Contravariant type T occurs in covariant position in type T =&amp;gt; U of value op；函数出参无限制；
④返回值：不能为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;T&lt;/code&gt; &amp;amp; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;superType of T&lt;/code&gt;，原因：Contravariant type T occurs in covariant position in type T of value fun；
④返回函数： 函数出参不能为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;T&lt;/code&gt; &amp;amp; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;superType of T&lt;/code&gt;，原因：Contravariant type T occurs in covariant position in type U =&amp;gt; T of value fun1；

&lt;em&gt;In A Picture:&lt;/em&gt;
&lt;img src=&quot;/images/posts/variance/contravariance-class-inner.png&quot; alt=&quot;contravariance-class-inner&quot; /&gt;
协变位置：只允许定义或传入&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;extends T&lt;/code&gt;的类型；
逆变位置：对类型无限制，可任意定义；
个人理解：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;逆变&lt;/code&gt;可看做消费者&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Consumer super&lt;/code&gt;并且 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;writeonly&lt;/code&gt;；所以协变类内部不能存在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;return (_ extends T)&lt;/code&gt;的函数，对函数的入参则无限制，所以就有了上图中的语法约束；

&lt;h3 id=&quot;不变invariance&quot;&gt;不变（invariance）&lt;/h3&gt;

&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;不变&lt;/code&gt;就是平时常见的类型参数，比如&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ListBuffer[T]&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ArrayBuffer[T]&lt;/code&gt;等都是不变的，可以对它们进行 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;read&lt;/code&gt; &amp;amp; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;write&lt;/code&gt;等操作，只要满足严格的类型约束要求即可；

&lt;h3 id=&quot;协变逆变应用&quot;&gt;协变逆变应用&lt;/h3&gt;
scala中常见的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;协变&lt;/code&gt;/&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;逆变&lt;/code&gt;类有：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;List[+T]&lt;/code&gt;,  &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Seq[+A]&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Option[+A]&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Future[+T]&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Tuple[..]&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Map[A, +B]&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Function1[-T1, +R]&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CanBuildFrom[-From, -Elem, +To]&lt;/code&gt;, etc.

&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Function1[-T1, +R]&lt;/code&gt;的另一种常见写法&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-T1 =&amp;gt; +R&lt;/code&gt;（入参逆变，出参协变）

&lt;strong&gt;示例&lt;/strong&gt;

&lt;div class=&quot;language-scala highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Array&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;])&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Unit&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// 协变&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;list&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;](&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;A&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;B&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;CC&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;toInt&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;))&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// 协变&amp;amp;逆变组合&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;fun&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v1&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;CharSequence&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;v1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;toString&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;toInt&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;toInt&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fun&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;ABC&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;))&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// 协变，入参类型允许的范围`List[_ &amp;lt;: CharSequence]`&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;toInt&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;CharSequence&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;])&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Seq&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// 协变&amp;amp;逆变组合使用，第一个入参类型允许的范围`Function1[_&amp;gt;:String, _&amp;lt;:Seq[Int]]`&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// 第一个入参类型范围的另一种表示：`_ &amp;gt;: String =&amp;gt; _ &amp;lt;:Seq[Int]`&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;toInt&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fun&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Seq&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;in&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Seq&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;fun&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;in&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;总结&quot;&gt;总结&lt;/h3&gt;

本文主要对&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;协变&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;逆变&lt;/code&gt;进行了详细的语法讲解&amp;amp;场景演示，但这些只是开始，大家需在平时工作中多结合应用场景多练习才能达到灵活运用的目的；此外&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;协变&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;逆变&lt;/code&gt;只在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;scala&lt;/code&gt;编译期进行语法校验，不影响&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;runtime&lt;/code&gt;，编译出的字节码会被部分裁切以满足&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;JVM&lt;/code&gt;字节码规范。

本文示例的代码存储在工程：https://github.com/itinycheng/jvm-lang-tutorial ，包&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;com.tiny.lang.java.generic.variance&lt;/code&gt; 和 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;com.tiny.lang.scala.generic.variance&lt;/code&gt;中。

&lt;h3 id=&quot;参考&quot;&gt;参考&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://typelevel.org/blog/2016/02/04/variance-and-functors.html&quot;&gt;https://typelevel.org/blog/2016/02/04/variance-and-functors.html&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.clear.rice.edu/comp310/JavaResources/generics/co_contra_host_visitor.html&quot;&gt;https://www.clear.rice.edu/comp310/JavaResources/generics/co_contra_host_visitor.html&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://stackoverflow.com/questions/27414991/contravariance-vs-covariance-in-scala&quot;&gt;https://stackoverflow.com/questions/27414991/contravariance-vs-covariance-in-scala&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://yq.aliyun.com/articles/111741&quot;&gt;https://yq.aliyun.com/articles/111741&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.zhihu.com/question/35339328&quot;&gt;https://www.zhihu.com/question/35339328&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://stackoverflow.com/questions/2723397/what-is-pecs-producer-extends-consumer-super&quot;&gt;https://stackoverflow.com/questions/2723397/what-is-pecs-producer-extends-consumer-super&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.scala-lang.org/tour/variances.html&quot;&gt;https://docs.scala-lang.org/tour/variances.html&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.microsoft.com/zh-cn/dotnet/standard/generics/covariance-and-contravariance&quot;&gt;https://docs.microsoft.com/zh-cn/dotnet/standard/generics/covariance-and-contravariance&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://my.oschina.net/xinxingegeya/blog/486671&quot;&gt;https://my.oschina.net/xinxingegeya/blog/486671&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</description>
        <pubDate>Wed, 30 Jan 2019 00:00:00 +0000</pubDate>
        <link>https://itinycheng.github.io/2019/01/30/scala-vs-java's-generics-2/</link>
        <guid isPermaLink="true">https://itinycheng.github.io/2019/01/30/scala-vs-java's-generics-2/</guid>
        
        <category>generic</category>
        
        <category>scala</category>
        
        <category>covariant</category>
        
        <category>contravariance</category>
        
        
        <category>generic</category>
        
      </item>
    
      <item>
        <title>Scala类型参数详解&amp;对比Java泛型体系</title>
        <description>&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#前言&quot; id=&quot;markdown-toc-前言&quot;&gt;前言&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#语法讲解&quot; id=&quot;markdown-toc-语法讲解&quot;&gt;语法讲解&lt;/a&gt;    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#多重上下界upperlower-bound&quot; id=&quot;markdown-toc-多重上下界upperlower-bound&quot;&gt;[多重]上/下界（upper/lower bound）&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#多重视图界定view-bound&quot; id=&quot;markdown-toc-多重视图界定view-bound&quot;&gt;[多重]视图界定（view bound）&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#多重上下文界定context-bound&quot; id=&quot;markdown-toc-多重上下文界定context-bound&quot;&gt;[多重]上下文界定（context bound）&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#类型约束&quot; id=&quot;markdown-toc-类型约束&quot;&gt;类型约束&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#协变covariant-不变invariance逆变contravariance&quot; id=&quot;markdown-toc-协变covariant-不变invariance逆变contravariance&quot;&gt;协变（covariant）、 不变（invariance）、逆变（contravariance）&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#总结&quot; id=&quot;markdown-toc-总结&quot;&gt;总结&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#参考&quot; id=&quot;markdown-toc-参考&quot;&gt;参考&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;前言&quot;&gt;前言&lt;/h2&gt;

&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Scala&lt;/code&gt;类型参数与&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Java&lt;/code&gt;的泛型体系是较相似的语言特性，但&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Scala&lt;/code&gt;类型参数体系的实现比&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Java&lt;/code&gt;更为的多样和复杂；&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Scala&lt;/code&gt;类型参数体系包含如下：[多重]上界（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;T &amp;lt;: UpperBound&lt;/code&gt;）、[多重]下界（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;T &amp;gt;: LowerBound&lt;/code&gt;）、[多重]视图界定（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;T &amp;lt;% ViewBound&lt;/code&gt;）、[多重]上下文界定（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;T : ContextBound&lt;/code&gt;）、类型约束（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;T =:= U&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;T &amp;lt;:&amp;lt; U&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;T &amp;lt;%&amp;lt; U&lt;/code&gt;）、协变（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;+T&lt;/code&gt;）、逆变（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-T&lt;/code&gt;）；可以认为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Scala&lt;/code&gt;类型参数在覆盖Java泛型所有特性的基础上又做了更多的扩展延伸，即&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Java&lt;/code&gt;泛型是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Scala&lt;/code&gt;类型参数的一个子集。本文主要讲解&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Scala&lt;/code&gt;参数类型的各种特性并辅助与&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Java&lt;/code&gt;泛型做对比分析，是一个供给Java Developer学习用的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Scala&lt;/code&gt;类型参数入门指南。

&lt;h2 id=&quot;语法讲解&quot;&gt;语法讲解&lt;/h2&gt;

&lt;strong&gt;首先定义若干类结构&lt;/strong&gt;

&lt;div class=&quot;language-scala highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;
&lt;span class=&quot;k&quot;&gt;trait&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Food&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;trait&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Medicine&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Fruit&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Food&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Apple&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Fruit&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// 橘子是一种水果，橘子皮有药用价值&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Orange&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Fruit&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Medicine&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;多重上下界upperlower-bound&quot;&gt;[多重]上/下界（upper/lower bound）&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;T &amp;lt;: upperBound&lt;/code&gt; 类型上界&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;T &amp;gt;: lowerBound&lt;/code&gt; 类型下界&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;T &amp;gt;: lowerBound &amp;lt;: upperBound&lt;/code&gt; 多重界定&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;with&lt;/code&gt; 连接多个上界或多个下界&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
  &lt;li&gt;
    用符号&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;:&lt;/code&gt;来表示类型参数的上界，如&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;_ &amp;lt;: Food&lt;/code&gt; 表示任意类型&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;_&lt;/code&gt;是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Food&lt;/code&gt;的子类，默认情况下&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Scala&lt;/code&gt;类的上界是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Any&lt;/code&gt;（类似&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Java&lt;/code&gt;中的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Object&lt;/code&gt;），即任意类型参数可表示为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;_ &amp;lt;: Any&lt;/code&gt;。
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;scala&lt;/code&gt;支持同时声明多个上界，两个上界类型之前用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;with&lt;/code&gt;连接，例如：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;_ &amp;lt;: Fruit with Medicine&lt;/code&gt;给任意类型&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;_&lt;/code&gt;定义了两个上界：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Fruit&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Medicine&lt;/code&gt;，在上文定义的类中只有&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Orange&lt;/code&gt;满足这个多重上界的限定。&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;with&lt;/code&gt;可以多次使用，用以连接多个上界类型，语法如下：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;_ &amp;lt;: TypeA with TypeB with TypeC with ...&lt;/code&gt;。
  &lt;/li&gt;
  &lt;li&gt;
    用符号&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;gt;:&lt;/code&gt;来表示类型参数的下界，如：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;_&amp;gt;:Fruit&lt;/code&gt;表示任意类型&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;_&lt;/code&gt;需满足是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Fruit&lt;/code&gt;的父类的上界限定，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;scala&lt;/code&gt;也支持多重上界的语法，如：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;_ &amp;gt;: Fruit with Food&lt;/code&gt;表示任意类型&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;_&lt;/code&gt;必须同时满足是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Fruit&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Food&lt;/code&gt;的父类，上文定义类中无满足该要求的类，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AnyRef&lt;/code&gt;可以满足这个多重上界的要求（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;scala&lt;/code&gt;中&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AnyRef&lt;/code&gt;是任意引用类型的父类）。
  &lt;/li&gt;
  &lt;li&gt;
    上界和下界一起使用可被称为多重界定，即一个类型参数时有上界和下界，如：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;_ &amp;gt;: Apple &amp;lt;: Food&lt;/code&gt;表示任意类型&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;_&lt;/code&gt;需满足以&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Apple&lt;/code&gt;作为下界，同时以&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Food&lt;/code&gt;作为上界，上文定义的类中&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Food&lt;/code&gt;满足该限定；此外，一个类型参数也可以同时拥有多上界和多个下界，如：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;_ &amp;gt;: Apple with Food &amp;lt;: AnyRef with Serializable&lt;/code&gt;定义了两个下界&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Apple&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Food&lt;/code&gt;，以及两个上界&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AnyRef&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Serializable&lt;/code&gt;。
  &lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;代码示例&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-scala highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;
&lt;span class=&quot;cm&quot;&gt;/**
  * 函数接收一个ListBuffer类型的入参，ListBuffer存放的对象必须是Fruit子类
  */&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;test0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;ListBuffer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;&amp;lt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Fruit&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;])&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Unit&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// compile error: list ++= List(new Fruit,new Orange,new Apple)&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;foreach&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;cm&quot;&gt;/**
  * define a generic type `T` which has an upper bounds `Fruit`
  */&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;test1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;&amp;lt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Fruit&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;](&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;ListBuffer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;])&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Unit&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// compile error :list ++= List(new Fruit,new Orange,new Apple)&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;foreach&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;cm&quot;&gt;/**
 * 函数接收一个ListBuffer类型入参，ListBuffer存放对象必须是Fruit父类
 */&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;test2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;ListBuffer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;&amp;gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Fruit&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;])&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Unit&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;list&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;++=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Apple&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Fruit&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;foreach&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;cm&quot;&gt;/**
  * @see [[test2]]
  */&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;test3&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;&amp;gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Fruit&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;](&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;ListBuffer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;])&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Unit&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;list&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;++=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Apple&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Fruit&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;foreach&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;cm&quot;&gt;/**
  * multi upper bounds
  */&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;test4&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;&amp;lt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Fruit&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Medicine&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;](&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;ListBuffer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;])&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Unit&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;//compile error: list ++= List(new Orange)&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;foreach&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;cm&quot;&gt;/**
  * multi lower bounds
  */&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;test5&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;&amp;gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Fruit&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Medicine&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;](&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;ListBuffer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;])&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Unit&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;list&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;++=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Orange&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;foreach&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;cm&quot;&gt;/**
  * have upper &amp;amp; lower bounds at the same time
  */&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;test6&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;&amp;gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Apple&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;&amp;lt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Food&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;](&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;ListBuffer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;])&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Unit&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// compile error: list += new Fruit&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;foreach&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;cm&quot;&gt;/**
  * multi upper &amp;amp; lower bounds
  */&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;test7&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;&amp;gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Apple&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Food&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;&amp;lt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;AutoCloseable&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Serializable&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;](&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;ListBuffer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;])&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Unit&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// compile error: list ++= List(new Orange, new Apple)&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;foreach&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;strong&gt;实际上：&lt;/strong&gt; 上界对应的是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Java&lt;/code&gt;中的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;extends&lt;/code&gt;关键字，下界对应&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Java&lt;/code&gt;中的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;super&lt;/code&gt;关键字，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;scala&lt;/code&gt;的上下界语法特性比&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Java&lt;/code&gt;的语法表现更全面；在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Java&lt;/code&gt;中无法定义一个泛型&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;T super Fruit&amp;gt;&lt;/code&gt;（可能在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Java&lt;/code&gt;看来&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;super of every class is Object&lt;/code&gt;）；但&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Java&lt;/code&gt;支持多重的上界，比如定义一个泛型&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;T&lt;/code&gt;必须继承两个类&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;T extends Fruit &amp;amp; Medicine&amp;gt;&lt;/code&gt;，其中&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;amp;&lt;/code&gt;与&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;scala&lt;/code&gt;中的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;with&lt;/code&gt;关键字相对。将&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;scala&lt;/code&gt;源码编译成字节码，然后反编译字节码进行观察发现之前定义的带有上下界的类型参数只剩下&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;extends&lt;/code&gt;关键字（对应&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;:&lt;/code&gt;），而&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;super&lt;/code&gt;关键字（对应&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;gt;:&lt;/code&gt;）基本被擦除（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Be Erased&lt;/code&gt;），从这一层面来讲，上下界是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;scala&lt;/code&gt;特有的语法层特性，是编译时特性非运行时，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;scala&lt;/code&gt;源码编译成字节码时会进行语法表达的转换和裁切，以符合JVM的字节码规范。

&lt;h3 id=&quot;多重视图界定view-bound&quot;&gt;[多重]视图界定（view bound）&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;T &amp;lt;% viewBound&lt;/code&gt; 视图界定（deprecated from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;scala 2.11&lt;/code&gt;）&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
  &lt;li&gt;
    用符号&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;%&lt;/code&gt;来表示视图界定，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;T &amp;lt;% Juice&lt;/code&gt;表示在当前&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.scala&lt;/code&gt;文件的上下文中，存在一个隐式函数可以将类型&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;T&lt;/code&gt;转换为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Juice&lt;/code&gt;。
  &lt;/li&gt;
  &lt;li&gt;
    &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;scala&lt;/code&gt;语法也支持多重的视图界定，如：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;T &amp;lt;% Juice &amp;lt;% Soup&lt;/code&gt;表示类型&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;T&lt;/code&gt;既可以隐式转换成&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Juice&lt;/code&gt;也可以转换成&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Soup&lt;/code&gt;。
  &lt;/li&gt;
&lt;/ol&gt;

&lt;strong&gt;注：&lt;/strong&gt; 视图界定在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Scala 2.11&lt;/code&gt;版本已经&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;deprecated&lt;/code&gt;，请用隐式参数替换，如下代码示例中的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;test2&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;test3&lt;/code&gt;；

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;首先定义若干类和隐私转换&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-scala highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;
&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Juice&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;food&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Fruit&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;){&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;juice&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Juice&quot;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Soup&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;food&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Fruit&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;){&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;soup&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Soup&quot;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;cm&quot;&gt;/**
  * 水果可以榨汁
  */&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;implicit&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;fruitToJuice&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fruit&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Fruit&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Juice&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Juice&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fruit&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;cm&quot;&gt;/**
  * 水果可以做汤
  */&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;implicit&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;fruitToSoup&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fruit&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Fruit&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Soup&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Soup&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fruit&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;代码示例&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-scala highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;
&lt;span class=&quot;c1&quot;&gt;// 引入隐式转换函数&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;fruitToJuice&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fruitToSoup&lt;/span&gt;

&lt;span class=&quot;cm&quot;&gt;/**
  * 该函数要求传入的ListBuffer中的T的对象可以隐式转换成Juice
  * view bounds are deprecated,use implicit parameter instead.
  * input parameter `T` can convert to `Juice`
  */&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;test0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;&amp;lt;%&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Juice&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;](&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;ListBuffer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;])&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Unit&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;foreach&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;juice&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;))&lt;/span&gt;

&lt;span class=&quot;cm&quot;&gt;/**
  * multi view bound
  */&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;test1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;&amp;lt;%&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Juice&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;&amp;lt;%&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Soup&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;](&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;ListBuffer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;])&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Unit&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// 通过隐式转换成Juice而拥有juice函数&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;foreach&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;juice&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;))&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// 通过隐式转换成Juice而拥有soup函数&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;foreach&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;soup&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;cm&quot;&gt;/**
  * Use this function to replace [[test0()]]
  */&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;test2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;](&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;ListBuffer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;])(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;implicit&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fun&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Juice&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Unit&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;foreach&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;juice&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;))&lt;/span&gt;


&lt;span class=&quot;cm&quot;&gt;/**
  * Use this function to replace [[test1()]]
  */&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;test3&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;](&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;ListBuffer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;])(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;implicit&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f1&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Juice&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f2&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Soup&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Unit&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;foreach&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;juice&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;))&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;foreach&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;soup&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// 调用函数&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Array&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;])&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Unit&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;fruits&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;ListBuffer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Fruit&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;](&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Apple&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Orange&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Fruit&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;test0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fruits&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;test1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fruits&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;test2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fruits&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;test3&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fruits&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;strong&gt;实际上：&lt;/strong&gt; 视图界定是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;scala&lt;/code&gt;为了让coding更简洁高效而设计出的一个语法糖，其实现是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;scala&lt;/code&gt;编译器在编译生成字节码过程中的一个自动插入隐式函数到所需位置的操作；如在上边的代码中&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ListBuffer[Fruit]&lt;/code&gt;作为入参传入&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;test0&lt;/code&gt;，在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;foreach&lt;/code&gt;时&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Fruit&lt;/code&gt;对象是没有&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;juice&lt;/code&gt;函数供给调用的，编译这段代码时不通过，这时编译器会从上下文中找隐式转换，找到了&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fruitToJuice&lt;/code&gt;，并调用该函数将&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fruit&lt;/code&gt;对象转换为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Juice&lt;/code&gt;，然后在调用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Juice&lt;/code&gt;中的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;juice&lt;/code&gt;函数（.class反编译结果类似&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;list.foreach(i =&amp;gt; println(fruitToJuice(i).juice))&lt;/code&gt;）；&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Java&lt;/code&gt;中无对应特性与之对应，即每个调用必须明确，编译器并不做类似自动化的补全操作。

&lt;h3 id=&quot;多重上下文界定context-bound&quot;&gt;[多重]上下文界定（context bound）&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;T: contextBound&lt;/code&gt; 上下文界定（存在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ContextBound[T]&lt;/code&gt;的隐式值）&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
  &lt;li&gt;
    上下文界定的形式为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;T : M&lt;/code&gt;，其中&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;M&lt;/code&gt;是另一个泛型类，它要求必须存在一个类型为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;M[T]&lt;/code&gt;的隐式值。如：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[T : Ordering]&lt;/code&gt;表示必须存在一个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Ordering[Fruit]&lt;/code&gt;的隐式值，在使用隐式值的地方声明&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;隐式参数&lt;/code&gt;（如：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(implicit ordering: Ordering[T])&lt;/code&gt;）。
  &lt;/li&gt;
  &lt;li&gt;
    多重上下文界定形式为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;T : M : N&lt;/code&gt;，表示当前代码上下文中必须存在类型&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;M[T]&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;N[T]&lt;/code&gt;两个隐式值，同样，在使用隐式值的地方可以显式的声明&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;隐式参数&lt;/code&gt;。
  &lt;/li&gt;
&lt;/ol&gt;

&lt;strong&gt;首先定义隐式值&lt;/strong&gt;

&lt;div class=&quot;language-scala highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;
&lt;span class=&quot;k&quot;&gt;implicit&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;object&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;FruitOrdering&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Ordering&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Fruit&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;override&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;compare&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Fruit&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Fruit&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;getClass&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;getName&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;compareTo&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;getClass&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;getName&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;cm&quot;&gt;/**
 * alternative
 *
 * has the same effect as FruitOrdering， but not singleton， better use `FruitOrdering`
 */&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;implicit&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;ordering&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Ordering&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Fruit&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Ordering&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Fruit&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;override&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;compare&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Fruit&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Fruit&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;getClass&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;getName&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;compareTo&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;getClass&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;getName&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;代码示例&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-scala highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;
&lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;FruitOrdering&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;scala.reflect.ClassTag&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;test0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;T:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Ordering&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;](&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;first&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;second&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Unit&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// compile error: val arr = new Array[T](2)&lt;/span&gt;
  &lt;span class=&quot;nf&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;first&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;, &quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;second&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;cm&quot;&gt;/**
  * use `implicit` parameter or else `Fruit extends Ordering[Fruit]`
  * 必须存在一个类型为`Ordering[T]`的隐式值
  */&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;test1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;T:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Ordering&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;](&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;first&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;second&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;implicit&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ordering&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Ordering&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;])&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Unit&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// compile error: val arr = new Array[T](2)&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;small&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;ordering&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;compare&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;first&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;second&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;first&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;second&lt;/span&gt;
  &lt;span class=&quot;nf&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;small&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;cm&quot;&gt;/**
 * ClassTag 用于保存运行时`T`的实际类型,`new Array[T](2)`就可以正常编译通过
 *
 * scala command compile result：
 * `test2: [T](first: T, second: T)(implicit evidence$1: Ordering[T], implicit evidence$2: scala.reflect.ClassTag[T])Unit`
 */&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;test2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;T:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Ordering&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;ClassTag&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;](&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;first&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;second&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;implicit&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ordering&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Ordering&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;])&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Unit&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;arr&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Array&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;](&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;small&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;ordering&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;compare&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;first&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;second&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;first&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;second&lt;/span&gt;
  &lt;span class=&quot;nf&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;, &quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;small&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// 调用函数&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Array&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;])&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Unit&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;test0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Apple&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Orange&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;test1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Apple&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Orange&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;test2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Apple&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Orange&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;strong&gt;说明：&lt;/strong&gt;

&lt;ul&gt;
  &lt;li&gt;
    关于隐式转换，隐式参数相关内容本文并没做太多的讲解，网上相关资料比较多，大家可以尝试自助学习下；
  &lt;/li&gt;
  &lt;li&gt;
    将上述函数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;test0&lt;/code&gt;输入到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;scala command&lt;/code&gt;得到的结果为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;test0: [T](first: T, second: T)(implicit evidence$1: Ordering[T])Unit&lt;/code&gt;，即&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[:Ordering]&lt;/code&gt;被编译成了隐式参数：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;implicit evidence$1: Ordering[T]&lt;/code&gt;，这是上下文界定的特定编译方式，大家需要牢记这个编译规则；
  &lt;/li&gt;
  &lt;li&gt;
    上述函数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;test1&lt;/code&gt;中显式的添加了隐式参数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;implicit ordering: Ordering[T]&lt;/code&gt;，其原因是在编码阶段函数内部需要对&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Ordering[T]&lt;/code&gt;的实例对象进行调用，不得不添加该隐式参数（编译期动态插入的隐式参数在编码阶段引用不到），&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;test1&lt;/code&gt;输入到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;scala command&lt;/code&gt;得到的结果为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;test1: [T](first: T, second: T)(implicit evidence$1: Ordering[T], implicit ordering: Ordering[T])Unit&lt;/code&gt;，在当前代码上下文中&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;main&lt;/code&gt;函数调用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;test1&lt;/code&gt;的入参为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(Apple, Orange, FruitOrdering ,FruitOrdering) &lt;/code&gt;，即单例对象&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FruitOrdering&lt;/code&gt;会同时出现在第三、四参数位上；
  &lt;/li&gt;
  &lt;li&gt;
    上述函数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;test2&lt;/code&gt;所处的上下文中并没有找到与&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ClassTag&lt;/code&gt;相对应的隐式值，这是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;scala&lt;/code&gt;编译器在编译时对&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ClassTag&lt;/code&gt;做特殊处理，在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;scala command&lt;/code&gt;下的编译的结果在预料之中，出现&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ClassTag&lt;/code&gt;的隐式参数，反编译字节码会发现&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;new Array[T](2)&lt;/code&gt;被转换为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;classTag.newArray(2)&lt;/code&gt;，其内部通过调用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Java&lt;/code&gt;中的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Array.newInstance(..)&lt;/code&gt;动态创建数组对象；&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;main&lt;/code&gt;函数中调用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;test2&lt;/code&gt;的代码的入参中隐式参数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ClassTag&lt;/code&gt;被编译成&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ClassTag..MODULE$.apply(Fruit.class)&lt;/code&gt;，这是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;scala&lt;/code&gt;编译器的对&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ClassTag&lt;/code&gt;的特殊处理，大家明白是编译器行为即可。
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;类型约束&quot;&gt;类型约束&lt;/h3&gt;

类型约束是一种比较严格的类型限定方式：

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;T =:= U&lt;/code&gt; 表示T与U的类型相同&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;T &amp;lt;:&amp;lt; U&lt;/code&gt; 表示T是U的子类型&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;T &amp;lt;%&amp;lt; U&lt;/code&gt; 表示T可被隐式转换为U (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;2.10 deprecated&lt;/code&gt;,&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;2.11 removed&lt;/code&gt;)&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;T =:= U&lt;/code&gt;是严格的类型约束，要求两个类型完全相等（包括类型参数），如：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;List =:= List&lt;/code&gt; is true，但是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;List[Apple] =:= List[Orange]&lt;/code&gt; is false；
  &lt;/li&gt;
  &lt;li&gt;
    &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;T &amp;lt;:&amp;lt; U&lt;/code&gt;是严格的类型约束（与&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;:&lt;/code&gt;相比），要求前者&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;T&lt;/code&gt;必须是后者&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;U&lt;/code&gt;的子类或类型相同；
  &lt;/li&gt;
  &lt;li&gt;
    &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;T &amp;lt;%&amp;lt; U&lt;/code&gt;在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;scala 2.10&lt;/code&gt;已经被标注deprecated，在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;scala 2.11&lt;/code&gt;被移除；同时视图界定&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;%&lt;/code&gt;从&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;scala 2.11&lt;/code&gt;开始被标记为deprecated，在未来版本可能会被移除掉，在大家用到视图界定时候最好的选择是在需要隐式转换的地方进行显式的声明（可关注本文&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;视图界定&lt;/code&gt;所讲述的内容）；
  &lt;/li&gt;
&lt;/ol&gt;

&lt;strong&gt;注：&lt;/strong&gt; 类型约束顾名思义是为对类型进行约束的，仅此而已；

&lt;div class=&quot;language-scala highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;
&lt;span class=&quot;cm&quot;&gt;/**
  * restrict: T eq Orange
  */&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;test0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;](&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;implicit&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ev&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=:=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Fruit&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;])&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Unit&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;foreach&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;cm&quot;&gt;/**
  * same as [[test0()]]
  */&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;test1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;](&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;implicit&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ev&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=:=[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;T&lt;/span&gt;, &lt;span class=&quot;kt&quot;&gt;Orange&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;])&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Unit&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;nf&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;test2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;](&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;])(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;implicit&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ev&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;&amp;lt;:&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Fruit&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Unit&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;lis&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Fruit&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;list&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;lis&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;foreach&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;strong&gt;说明：&lt;/strong&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;=:=&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;:&amp;lt;&lt;/code&gt;实际上是两个在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Predef.scala&lt;/code&gt;定义好的两个类（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sealed abstract class =:=[From, To] extends (From =&amp;gt; To) with Serializable&lt;/code&gt; 与 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sealed abstract class &amp;lt;:&amp;lt;[-From, +To] extends (From =&amp;gt; To) with Serializable&lt;/code&gt;），并在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;scala&lt;/code&gt;编译器的协助之下完成类型约束的行为，该行为发生于代码编译期间（感慨&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;scala&lt;/code&gt;各种语法糖给编译器带来了大量的编译压力哈哈~~）；
  &lt;/li&gt;
  &lt;li&gt;
    上述函数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;test0&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;test2&lt;/code&gt;中隐式参数的类型是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;scala&lt;/code&gt;的中缀写法，原始写法如&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;test1&lt;/code&gt;所示&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;=:=[A,B]&lt;/code&gt;（带两个类型参数的类），当类有且仅有两个类型参数时候才能用中缀写法，更直观示例：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;def foo(f: Function1[String, Int])&lt;/code&gt;可以替换为中缀形式&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;def foo(f: String Function1 Int)&lt;/code&gt;；
  &lt;/li&gt;
  &lt;li&gt;
    &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;T &amp;lt;:&amp;lt; U&lt;/code&gt;与&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;T &amp;lt;: U&lt;/code&gt;的异同点说明，二者都表示&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;T&lt;/code&gt;是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;U&lt;/code&gt;的子类，但&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;:&amp;lt;&lt;/code&gt;是更严格的类型约束， 要求在满足&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;T&lt;/code&gt;是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;U&lt;/code&gt;子类的条件时不能对&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;T&lt;/code&gt;做类型推导&amp;amp;隐式转换的操作；而&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;:&lt;/code&gt;则可以与类型推导&amp;amp;隐式转换配合使用；

    &lt;ul&gt;
      &lt;li&gt;&lt;strong&gt;类型推导：&lt;/strong&gt;&lt;/li&gt;
    &lt;/ul&gt;

    下述代码在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;main&lt;/code&gt;函数中用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;test3(1, List(new Apple))&lt;/code&gt;调用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;def test3[A, B &amp;lt;: A](a: A, b: B)&lt;/code&gt;时编译器正常编译通过，调用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;test3&lt;/code&gt;时传入的第一个参数是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Int&lt;/code&gt;，第二个参数是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;List[Apple]&lt;/code&gt;，显然不符合&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;B &amp;lt;: A&lt;/code&gt;的约束，为了满足这个约束编译器在做类型推导时会继续向上寻找父类型来匹配是否满足，于是第一个参数被推导为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Any&lt;/code&gt;类型，此时&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;List[Int]&lt;/code&gt;符合&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Any&lt;/code&gt;的子类型，编译通过（用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Java Decompiler&lt;/code&gt;反编译字节码发现&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;main&lt;/code&gt;函数中调用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;test3&lt;/code&gt;的入参类型为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Int, List&lt;/code&gt;，而这种入参类型不符合&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;test3&lt;/code&gt;编译后的入参要求&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;B extends A&lt;/code&gt;，即字节码合法性校验较编译器宽松许多，感觉需要读几本编译原理入门下，哈~）；

    &lt;div class=&quot;language-scala highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;
&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;test3&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;A&lt;/span&gt;, &lt;span class=&quot;kt&quot;&gt;B&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;&amp;lt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;](&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;B&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Unit&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;nf&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;, &quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// 调用函数&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Array&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;])&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Unit&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;nf&quot;&gt;test3&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Apple&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;

    &lt;ul&gt;
      &lt;li&gt;&lt;strong&gt;隐式转换：&lt;/strong&gt;&lt;/li&gt;
    &lt;/ul&gt;

    下述代码中在调用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;foo&lt;/code&gt;时传入的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Apple&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Orange&lt;/code&gt;不能满足&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Apple &amp;lt;: Orange&lt;/code&gt;，但由于隐式函数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;a2o&lt;/code&gt;的存在，在编译&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;main&lt;/code&gt;函数中的第一行 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;foo(new Apple, new Orange)&lt;/code&gt;时会引入隐式参数，将&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Apple&lt;/code&gt;转换为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Orange&lt;/code&gt;，所以可成功编译并执行；而在编译第二行&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bar(new Apple, new Orange)&lt;/code&gt;会报错&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;error: Cannot prove that Apple &amp;lt;:&amp;lt; Orange&lt;/code&gt;；

    &lt;div class=&quot;language-scala highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;
  &lt;span class=&quot;k&quot;&gt;implicit&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;a2o&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Apple&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Orange&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;foo&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;T&lt;/span&gt;, &lt;span class=&quot;kt&quot;&gt;U&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;&amp;lt;:&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;u&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;U&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;OK&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;bar&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;T&lt;/span&gt;, &lt;span class=&quot;kt&quot;&gt;U&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;](&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;u&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;U&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;implicit&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;U&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;&amp;lt;:&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;OK&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Array&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;])&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Unit&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// 编译成功，引入隐式函数`a2o`&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;foo&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Apple&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Orange&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;// 编译失败，`&amp;lt;:&amp;lt;`是严格类型限定&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// error: Cannot prove that Apple &amp;lt;:&amp;lt; Orange&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;bar&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Apple&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Orange&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;协变covariant-不变invariance逆变contravariance&quot;&gt;协变（covariant）、 不变（invariance）、逆变（contravariance）&lt;/h3&gt;

&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;scala&lt;/code&gt;的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;协变&lt;/code&gt;、&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;逆变&lt;/code&gt;、&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;不变&lt;/code&gt;的特性相比较其他更为的难理解，计划单独新开一篇文详细对比讲解；感兴趣的小伙伴们可以移驾：

https://itinycheng.github.io/2019/01/30/scala-vs-java’s-generics-2/

&lt;h2 id=&quot;总结&quot;&gt;总结&lt;/h2&gt;

&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;scala&lt;/code&gt;类型参数体系是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;scala&lt;/code&gt;语言的重要特性，相比&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Java&lt;/code&gt;泛型，其更为复杂多变，且与编译器&amp;amp;隐式转换等互相掺杂配合，使得大家很难在短时间内掌握和灵活使用，建议大家多看&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;scala&lt;/code&gt;源码，多练习，多尝试，多用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;scala command&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Java Decompiler&lt;/code&gt;等工具测试&amp;amp;反编译二进制文件，以求更深刻的了解&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;scala&lt;/code&gt;类型参数的语法特性&amp;amp;内在原理。

本文示例的代码存储在工程：https://github.com/itinycheng/jvm-lang-tutorial ，包&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;com.tiny.lang.java.generic&lt;/code&gt; 和 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;com.tiny.lang.scala.generic&lt;/code&gt;中。

&lt;h2 id=&quot;参考&quot;&gt;参考&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://stackoverflow.com/questions/3427345/what-do-and-mean-in-scala-2-8-and-where-are-they-documented&quot;&gt;https://stackoverflow.com/questions/3427345/what-do-and-mean-in-scala-2-8-and-where-are-they-documented&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.originate.com/thinking/stories/cheat-codes-for-contravariance-and-covariance/&quot;&gt;https://www.originate.com/thinking/stories/cheat-codes-for-contravariance-and-covariance/&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.clear.rice.edu/comp310/JavaResources/generics/co_contra_host_visitor.html&quot;&gt;https://www.clear.rice.edu/comp310/JavaResources/generics/co_contra_host_visitor.html&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://typelevel.org/blog/2016/02/04/variance-and-functors.html&quot;&gt;https://typelevel.org/blog/2016/02/04/variance-and-functors.html&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.scala-lang.org/tour/variances.html&quot;&gt;https://docs.scala-lang.org/tour/variances.html&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://hongjiang.info/scala-type-contraints-and-specialized-methods/&quot;&gt;http://hongjiang.info/scala-type-contraints-and-specialized-methods/&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.scala-lang.org/tutorials/FAQ/context-bounds.html&quot;&gt;https://docs.scala-lang.org/tutorials/FAQ/context-bounds.html&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.zhihu.com/question/35339328&quot;&gt;https://www.zhihu.com/question/35339328&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://stackoverflow.com/questions/2723397/what-is-pecs-producer-extends-consumer-super&quot;&gt;https://stackoverflow.com/questions/2723397/what-is-pecs-producer-extends-consumer-super&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</description>
        <pubDate>Mon, 31 Dec 2018 00:00:00 +0000</pubDate>
        <link>https://itinycheng.github.io/2018/12/31/scala-vs-java's-generics-1/</link>
        <guid isPermaLink="true">https://itinycheng.github.io/2018/12/31/scala-vs-java's-generics-1/</guid>
        
        <category>generic</category>
        
        <category>scala</category>
        
        <category>java</category>
        
        
        <category>generic</category>
        
      </item>
    
      <item>
        <title>多时区的数据写入同一张Druid表带来的一些问题思考</title>
        <description>&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#说在前面的话&quot; id=&quot;markdown-toc-说在前面的话&quot;&gt;说在前面的话：&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#问题描述&quot; id=&quot;markdown-toc-问题描述&quot;&gt;问题描述&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#解决方案&quot; id=&quot;markdown-toc-解决方案&quot;&gt;解决方案&lt;/a&gt;    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#方案一校准每条原始日志的时间戳&quot; id=&quot;markdown-toc-方案一校准每条原始日志的时间戳&quot;&gt;方案一：校准每条原始日志的时间戳&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#方案二查询时针对查询时间区间进行校准&quot; id=&quot;markdown-toc-方案二查询时针对查询时间区间进行校准&quot;&gt;方案二：查询时针对查询时间区间进行校准&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#对比总结&quot; id=&quot;markdown-toc-对比总结&quot;&gt;对比总结&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#引用参考&quot; id=&quot;markdown-toc-引用参考&quot;&gt;引用&amp;amp;参考&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;说在前面的话&quot;&gt;说在前面的话：&lt;/h2&gt;
近来在与两个技术朋友聊天时发现的一个简单有趣的问题，在这做个简短的记录；本文主要围绕问题是什么，在现有技术上下文中如何解决，有哪些优缺点。

&lt;strong&gt;问题预告&lt;/strong&gt;：业务层面多个时区的数据如何映射到一张Druid表，查询时满足不同时区客户查询时都能按照各自时区的实际时间来统计数据；

&lt;h2 id=&quot;问题描述&quot;&gt;问题描述&lt;/h2&gt;

假设我现在有一套统计分析的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Saas&lt;/code&gt;系统（简称：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TA&lt;/code&gt;），其包含两个模块，用户行为收集模块（简称：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;uTracker&lt;/code&gt;）和行为数据统计分析模块（简称：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;uAnalyzer&lt;/code&gt;）类似&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Google Analytics&lt;/code&gt;或是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Baidu统计&lt;/code&gt;。

&lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TA&lt;/code&gt;系统的上下文说明：&lt;/strong&gt;
&lt;ul&gt;
  &lt;li&gt;首先：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TA&lt;/code&gt;的客户将&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;uTracker&lt;/code&gt;引入到自己的App中，用以收集使用该APP的用户的行为数据；&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;uTracker&lt;/code&gt;随着APP的启动而启动并自动收集当前用户行为数据，比如：设备、上下文、访问内容等信息（简称：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;uData&lt;/code&gt;），每条&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;uData&lt;/code&gt;都带有该行为数据生成的时间戳（简称：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dTs&lt;/code&gt;，从设备时钟获取）以及当前设备时钟对应的时区（简称：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dZone&lt;/code&gt;）。&lt;/li&gt;
  &lt;li&gt;其次：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;uTracker&lt;/code&gt;将收集上来的数据进行合并、格式化、压缩等操作之后通过手机网络将数据发送到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;uAnalyzer&lt;/code&gt;的前置数据收集系统；&lt;/li&gt;
  &lt;li&gt;最终：这这些数据经过&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;uAnalyzer&lt;/code&gt;系统的清洗、补余，解析等等操作后入到统一的OLAP库中供给客户实时的查询他APP的新增、活跃、留存等指标数据；&lt;/li&gt;
  &lt;li&gt;数据流程描述如下：&lt;/li&gt;
&lt;/ul&gt;

&lt;pre&gt;&lt;code class=&quot;language-mermaid&quot;&gt;graph TB
A(用户A) --&amp;gt;|visit| UT(集成uTracker的APP)
B(用户B) --&amp;gt;|visit| UT
C(用户C) --&amp;gt;|visit| UT
D(......) --&amp;gt;|visit| UT  
UT --&amp;gt;|send uData| UA(uAnalyzer的前置数据收集模块)
UA --&amp;gt;|pull uData| DP(uAnalyzer的数据处理模块, 以`Flink+Druid`为主)  
&lt;/code&gt;&lt;/pre&gt;

&lt;ul&gt;
  &lt;li&gt;数据查询服务&lt;/li&gt;
&lt;/ul&gt;

&lt;pre&gt;&lt;code class=&quot;language-mermaid&quot;&gt;graph LR
C(uAnalyzer解析数据) --&amp;gt;|sink| D(Druid: new_user)
D --&amp;gt;|query| E(uAnalyzer查询新增用户数服务)
&lt;/code&gt;&lt;/pre&gt;

&lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TA&lt;/code&gt;的客户群体说明&lt;/strong&gt;
&lt;ul&gt;
  &lt;li&gt;首先：某个客户A来自美国，客户B来自中国，这两家客户都在自己的APP中集成了&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;uTracker&lt;/code&gt;，并使用我的Saas版&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TA&lt;/code&gt;系统；&lt;/li&gt;
  &lt;li&gt;其次：客户A和客户B的APP的用户群体来自世界各地，其数据都发送到我的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;uAnalyzer&lt;/code&gt;系统中做统计分析；&lt;/li&gt;
  &lt;li&gt;
    最后：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TA&lt;/code&gt;系统有10W+的客户在用，假设大多数的客户都有以上两个企业属性；

    &lt;em&gt;注： 客户A, B其实可类比为亚马逊、淘宝，他们分属不同国家，其用户群体涵盖的是全球范围；&lt;/em&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;strong&gt;问题描述：&lt;/strong&gt;
&lt;ul&gt;
  &lt;li&gt;为了简化问题，现在限定只针对A,B客户APP在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TA&lt;/code&gt;系统中的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;用户新增&lt;/code&gt;指标提出问题；&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;用户新增&lt;/code&gt;指标定义：在客户查询的时间区间内该客户APP新增的用户数量；&lt;/li&gt;
&lt;/ul&gt;

客户A是美国客户，其常用的时区为UTC-5；
客户B是中国客户，其常用时区为UTC+8；
客户A,B的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;用户新增&lt;/code&gt;指标在同一张&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Druid&lt;/code&gt;表（new_user）中；
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Druid&lt;/code&gt;表&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;new_user&lt;/code&gt;采用的时区是UTC+8，粒度是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DAY&lt;/code&gt;；

客户A的APP所对应的用户主要来自两个区域美国本地UTC-5，中国UTC+8；
客户B的APP所对应的用户同样来自两个区域美国本地UTC-5，中国UTC+8；

&lt;strong&gt;问题&lt;/strong&gt;：对于一条美国当地上报的用户数据，若其&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;uTs&lt;/code&gt;在当地时间（UTC-5时区）是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;2018-11-13 12:00:00&lt;/code&gt;则对应的UTC+8时间是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;2018-11-14 01:00:00&lt;/code&gt;；其解析到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Druid&lt;/code&gt;表时是按照&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Druid&lt;/code&gt;表设置的时区来做&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;aggregate&lt;/code&gt;的，当前Durid表时区配置是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UTC+8&lt;/code&gt;，则最终入库到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Druid&lt;/code&gt;时会将UTC-5时间&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;2018-11-13 12:00:00&lt;/code&gt;的数据解析到 到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;2018-11-14 00:00:00&lt;/code&gt;的segment（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Druid&lt;/code&gt;表粒度是’DAY’），这就照成了在查询美国时间&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;2018-11-13&lt;/code&gt;日的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;新增用户数&lt;/code&gt;实际是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;2018-11-12 11:00:00&lt;/code&gt;到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;2018-11-13 13:00:00&lt;/code&gt;的数据。

其实客户需求很简单，按照客户指定的时间区间和时区查询所有新增用户数量（即：全球范围内的新增用户数）；

&lt;h2 id=&quot;解决方案&quot;&gt;解决方案&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;关键字：&lt;br /&gt;
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dataTs&lt;/code&gt;：日志产生的时间，日志生成时的设备时间戳；&lt;br /&gt;
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dataZone&lt;/code&gt;：接收到的数据中包含的时区，从设备时钟获取；&lt;br /&gt;
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;druidZoneConf&lt;/code&gt;：Druid表创建时所设置的时区；&lt;br /&gt;
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;appZoneConf&lt;/code&gt;：查询APP指标时期望用的时区；&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;方案一校准每条原始日志的时间戳&quot;&gt;方案一：校准每条原始日志的时间戳&lt;/h3&gt;

&lt;strong&gt;示例&lt;/strong&gt;：假设某APP的用户分布在中国和美国，其上报数据的时区&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dataZone&lt;/code&gt;中包含UTC+8, UTC-5两种时区，Druid表配置的时区&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;druidZoneConf&lt;/code&gt;是UTC+8，APP设置的查询所用时区&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;appZoneConf&lt;/code&gt;是UTC-5；
在处理每条原始日志时对&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dataTs&lt;/code&gt;进行重新校准，校准好的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;newTimestamp&lt;/code&gt;做为Druid入库的时间戳；

伪代码：
&lt;div class=&quot;language-scala highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;cm&quot;&gt;/**
 * Zone(UTC+8) - Zone(UTC-5) = 8 - (-5) = 13
 */&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;calibrate&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Zone&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Zone&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;newTimestamp&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dts&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;calibrate&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;appZoneConf&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;druidZoneConf&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;strong&gt;优点&lt;/strong&gt;：支持的所有的granularity（时间粒度：秒，分，时，日，周，月，年），代码变动量小，可沿用当前的所有Druid表；&lt;br /&gt;
&lt;strong&gt;缺点&lt;/strong&gt;：历史数据没办法处理；APP在创建时必须选择时区且后续无法变更（已入库数据无法适配变更）；

&lt;h3 id=&quot;方案二查询时针对查询时间区间进行校准&quot;&gt;方案二：查询时针对查询时间区间进行校准&lt;/h3&gt;

在数据解析并写入Druid的过程中不对时间戳&amp;amp;时区做干扰处理，在指标查询过程中针对时区进行查询区间的换算；

&lt;strong&gt;示例&lt;/strong&gt;：Druid表的存储数据所用的时区是UTC+8，美国用户查看指标采用的时区是UTC-5，查询的时间区间是[2018-11-11, 2018-11-13]，根据时区差异校准后的数据查询区间为[2018-11-11 13:00:00, 2018-11-13 13:00:00]；(druid有针对时区的查询方案，但也要求&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;=小时&lt;/code&gt;的粒度)

时间校准伪代码：
&lt;div class=&quot;language-scala highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;cm&quot;&gt;/**
 * Zone(UTC+8) - Zone(UTC-5) = 8 - (-5) = 13
 */&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;calibrate&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Zone&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Zone&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;newStartTime&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;startTime&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;calibrate&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;appZoneConf&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;druidZoneConf&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;newEndTime&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;endTime&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;calibrate&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;appZoneConf&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;druidZoneConf&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;em&gt;注&lt;/em&gt;：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Druid&lt;/code&gt;本身的查询也支持时区的转换，可参照如下链接： &lt;a href=&quot;http://druid.io/docs/latest/querying/granularities.html&quot;&gt;http://druid.io/docs/latest/querying/granularities.html&lt;/a&gt;

&lt;strong&gt;优点&lt;/strong&gt;：不修改原始数据的时间戳，不造成歧义容易理解；筛选数据所用时区可随意变更，无历史数据迁移问题；&lt;br /&gt;
&lt;strong&gt;缺点&lt;/strong&gt;：只支持部分granularity（时间粒度：秒，分，时），其他粒度（天，月，周，年）不支持，需要对表进行重新的设计规划；
&lt;strong&gt;此外&lt;/strong&gt;：一些国家使用非整数的时区，如：印度使用的是UTC +5:30，这时在使用方案二前提下无法去查询&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;roll-up by HOUR&lt;/code&gt;的表；
&lt;h2 id=&quot;对比总结&quot;&gt;对比总结&lt;/h2&gt;

若在系统建设初期没考虑到跨时区等问题，到后期再更正就有存在比较大的工作量投入，且牵涉到历史数据处理迁移的问题；方案一是较为简单治标方案，比较适用于已有系统的变更；方案二则是一个比较能治标治本的方案，但需要在项目初期规划时候就考量进去，同时遇到数据量特别大的场景也需要将性能纳入考量，毕竟对同一个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Druid Schema&lt;/code&gt;来讲粒度从&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DAY&lt;/code&gt;变为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HOUR&lt;/code&gt;数据最大增长24倍；

&lt;h2 id=&quot;引用参考&quot;&gt;引用&amp;amp;参考&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;[1]. &lt;a href=&quot;https://baike.baidu.com/item/时间戳&quot;&gt;define timestamp&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;[2]. &lt;a href=&quot;http://druid.io/docs/latest/querying/granularities.html&quot;&gt;query druid with timezone&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;[3]. &lt;a href=&quot;http://druid.io/docs/latest/ingestion/ingestion-spec.html&quot;&gt;create druid ingestion spec with timezone &lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</description>
        <pubDate>Tue, 13 Nov 2018 00:00:00 +0000</pubDate>
        <link>https://itinycheng.github.io/2018/11/13/timezone-problem-about-druid-table-design/</link>
        <guid isPermaLink="true">https://itinycheng.github.io/2018/11/13/timezone-problem-about-druid-table-design/</guid>
        
        <category>timezone</category>
        
        <category>druid</category>
        
        <category>analytics</category>
        
        
        <category>product-design</category>
        
      </item>
    
  </channel>
</rss>
