最近公司用户量暴涨,领导笑嘻嘻,搬砖的心里***,服务器压力骤然上升,最近大数据集群在跑Spark任务时老是报“Container exited with a non-zero exit code 137”这样的错误,详细日志如下:

WARN YarnSchedulerBackend$YarnSchedulerEndpoint: Requesting driver to remove executor 4 for reason Container marked as failed: container_e52_1650941597513_0076_01_000007 on host: host0. Exit status: 137. Diagnostics:Container killed on request. Exit code is 137
Container exited with a non-zero exit code 137. 
Killed by external signal

虽然日志级别是warn,而且yarn会重启一个新的container来重新执行先前失败的task,但是毕竟有的时候重启次数太多了会导致任务执行时间很长甚至于整个任务失败,所以这个问题还是不容忽视的。那到底是因为什么导致容器被killed了呢?是谁把容器给kill了呢?怎么解决这个问题呢?

最开始的时候百度了一下找到一篇文章(Spark 中的“Container killed on request.Exit code is 137”)讲了这个问题,但是折腾半天不好使,后面仔细分析一下问题,才发现是Spark executor运行时占用内存太多而物理内存不足,Linux内核开启了oom killer机制(Linux内核OOM机制的详细分析),Linux内核会根据一定机制选择内存占用过大的进程kill掉。而错误日志里的“Killed by external signal”中的“external signal”指的是“kill -9”(SIGKILL)这个信号(Linux Signal信号详解),而这个信号对应的进程退出码(Linux and Unix exit code tutorial with examples)就是137。而且在Spark报137错误时,能够在被killed 掉的容器所在的主机上的日志文件/var/log/messages能看到如下的日志:

host0 kernel: Out of memory: Kill process 12290 (java) score 152 or sacrifice child
host0 kernel: Killed process 12290 (java), UID 1011, total-vm:23715088kB, anon-rss:8738028kB, file-rss:0kB, shmem-rss:20kB
host0 kernel: java invoked oom-killer: gfp_mask=0x201da, order=0, oom_score_adj=0

日志里面我们会看到“Out of memory”、“java invoked oom-killer”字样。

现在知道这个问题是我们的服务器内存不足导致的,但是怎么解决呢?把oom killer机制关了?这个属于掩耳盗铃。申请升级服务器?不好意思预算紧张。总不能用我的工资升级服务器吧,所以还得自己想其他办法节省内存来规避这个问题。对于Spark要节省内存无非一下几个办法:

 1.使用Kryo序列化。

Spark默认使用Java序列化,而使用Kryo会大大减少对象序列化后数据的大小,当在代码中使用了RDD持久化时,会大大减少存储空间的使用。

2.调整RDD持久化。

通过观察Spark UI中Storage页面可以看到持久化的具体统计数据,项目代码里StorageLevel原本设置为MEMORY_ONLY,这会占用大量的内存,可以在启动参数中把--executor-memory适当调小,并在代码中把StorageLevel设置为 MEMORY_AND_DISK或者MEMORY_AND_DISK_SER(MEMORY_AND_DISK_SER会占用更少的存储空间,但是会占用更多的CPU时间)甚至于DISK_ONLY。虽然这样使一部分数据持久化在磁盘里,但是总比容器被killed然后有重启付出的代价少,而且现在很多云服务器可以用SSD,相比HDD也是鸟枪换了炮。

  3.调整并发度与并行度

        可以通过调大spark.sql.shuffle.partitions、spark.default.parallelism来增加RDD的分区数来减少每个分区的数据量;同时可以减少spark.executor.cores,减少同时运行的task的数量以减少内存的使用。

 4.祛除数据倾斜。

内存占用过高还有一种情况就是数据倾斜,如果老是某些节点的container被killed掉,很可能就是存在数据倾斜,关于数据倾斜具体可参考:Spark性能优化指南

经过实践最有用的是前两种办法,基本上就可以解决这个问题。还有一点就是在配置yarn资源时,不要把内存上限设置得太满,留一定的冗余也不会出现这个问题,我们的集群就是没有注意这个问题,yarn.nodemanager.resource.memory-mb设置得太高了。

最后还想说一点就是在分析这个问题的时候发现Spark on YARN在生成运行executor的脚本文件(就是launch_container.sh)时,最后生成运行executor的shell命令如下:

exec /bin/bash -c "LD_LIBRARY_PATH="/usr/hdp/current/hadoop-client/lib/native:/usr/hdp/current/hadoop-client/lib/native/Linux-amd64-64:$LD_LIBRARY_PATH" $JAVA_HOME/bin/java -server -Xmx10240m '-XX:+UseNUMA' -Djava.io.tmpdir=$PWD/tmp '-Dspark.history.ui.port=18081' '-Dspark.driver.port=37119' -Dspark.yarn.app.container.log.dir=/hadoop/yarn/log/application_1650941597513_0025/container_e52_1650941597513_0025_01_000003 -XX:OnOutOfMemoryError='kill %p' org.apache.spark.executor.CoarseGrainedExecutorBackend --driver-url spark://CoarseGrainedScheduler@had3:37119 --executor-id 2 --hostname had3 --cores 3 --app-id application_1650941597513_0025 --user-class-path file:$PWD/__app__.jar 1>/hadoop/yarn/log/application_1650941597513_0025/container_e52_1650941597513_0025_01_000003/stdout 2>/hadoop/yarn/log/application_1650941597513_0025/container_e52_1650941597513_0025_01_000003/stderr"

其中-Xmx10240m中的数值就对应的是--executor-memory的设置,而且会默认添加一个参数:-XX:OnOutOfMemoryError='kill %p',意思是当出现OutOfMemory时会执行kill命令将当前的进程杀死,至于Spark为什么要这样做源代码解释如下:

  /*** Kill if OOM is raised - leverage yarn's failure handling to cause rescheduling.* Not killing the task leaves various aspects of the executor and (to some extent) the jvm in* an inconsistent state.* TODO: If the OOM is not recoverable by rescheduling it on different node, then do* 'something' to fail job ... akin to blacklisting trackers in mapred ?** The handler if an OOM Exception is thrown by the JVM must be configured on Windows* differently: the 'taskkill' command should be used, whereas Unix-based systems use 'kill'.** As the JVM interprets both %p and %%p as the same, we can use either of them. However,* some tests on Windows computers suggest, that the JVM only accepts '%%p'.** Furthermore, the behavior of the character '%' on the Windows command line differs from* the behavior of '%' in a .cmd file: it gets interpreted as an incomplete environment* variable. Windows .cmd files escape a '%' by '%%'. Thus, the correct way of writing* '%%p' in an escaped way is '%%%%p'.*/private[yarn] def addOutOfMemoryErrorArgument(javaOpts: ListBuffer[String]): Unit = {if (!javaOpts.exists(_.contains("-XX:OnOutOfMemoryError"))) {if (Utils.isWindows) {javaOpts += escapeForShell("-XX:OnOutOfMemoryError=taskkill /F /PID %%%%p")} else {javaOpts += "-XX:OnOutOfMemoryError='kill %p'"}}}

最开始的怀疑kill的问题跟这个有关,但是其实不是的,因为kill对于的退出码是143,而137对应的是“kill -9”,应该是在jvm出现OutOfMemoryError之前就已经被Linux给kill掉了,如果你的日志里面出现的exit code是143,那么大概率是出现了OutOfMemoryError。

Spark:Container exited with a non-zero exit code 137相关推荐

  1. 《南溪的目标检测学习笔记》——验证模式下出现“Process finished with exit code 137 (interrupted by signal 9: SIGKILL)“的问题

    1 问题描述 在验证模式下运行代码的时候,出现这样的错误: Process finished with exit code 137 (interrupted by signal 9: SIGKILL) ...

  2. Process finished with exit code 137 (interrupted by signal 9: SIGKILL)错误

    程序正确运行结束的提示是:Process finished with exit code 0.如果程序出现Process finished with code 137 (interrupted by ...

  3. Process finished with exit code 143

    跑一个深度学习模型 第一次运行出现Process finished with exit code 137 之后再运行均出现Process finished with exit code 143 137 ...

  4. 【异常】Container exited with a non-zero exit code 1 Failing this attempt.Stack trace: ExitCodeException

    [异常]Container exited with a non-zero exit code 1 Failing this attempt.Stack trace: ExitCodeException ...

  5. 【Flink】Flink Container exited with a non-zero exit code 143

    1.概述 偶然查询一个环境的日志,发现这个环境有报错 Flink Container exited with a non-zero exit code 143. 2022-01-30 12:58:16 ...

  6. Hadoop之——Hadoop3.x运行自带的WordCount报错Container exited with a non-zero exit code 1.

    转载请注明出处:https://blog.csdn.net/l1028386804/article/details/93750832 问题: 今天,基于Hadoop3.2.0搭建了Hadoop集群,对 ...

  7. Container killed by the ApplicationMaster, Exit code is 143

    之前发现在map任务里面经常看到Container killed by the ApplicationMaster,挺奇怪,不过任务最终是成功的,就没怎么管.不过最近测试集群跑的任务报143错误,还是 ...

  8. Container killed on request. Exit code is 143_Permission denied: user=dr.who, a---Hadoop3.x启动报错记录001

    Permission denied: user=dr.who, access=WRITE, inode="/":root:supergroup:drwxr-xr-x  这个错误 还 ...

  9. Error: ADB exited with exit code 1 Performing Streamed Install adb: failed to install D:\svn\app\sm

    使用vscode允许flutter项目出现问题:Error: ADB exited with exit code 1 Performing Streamed Install adb: failed t ...

  10. 当idea 出现Process exited with an error: 1(Exit value: 1)问题时

    在idea进行GRPC运行时总是出现Process exited with an error: 1(Exit value: 1)这个错误,让人很是抓狂. 后来看到网上说可以这样解决 把标红处打上对号. ...

最新文章

  1. 京东金融科技学堂开班,AI 在金融云上有了新成果
  2. ArcGis辅助编号(半自动)功能的插件式实现
  3. idea + maven + profile + tomcat 调试 javaee 和js
  4. 三行代码实现快速排序
  5. OpenGL 分层渲染Layered Rendering的实例
  6. ubuntu开启客户端nfs服务_linux系统文件服务
  7. IOS高级编程之二:IOS的数据存储与IO
  8. 发生系统错误53_SAP那些事-推理剧-36-奇怪的付款清账(F-53)报错“TABLE_INVALID_INDEX”...
  9. 基于matlab高等数学实验 pdf,基于MATLAB的高等数学综合性实验的教学设计.pdf
  10. 2020年软考信息安全工程师教程第2版
  11. Windows Builder 使用总结
  12. QQ MSN 网页互动代码
  13. PL/SQL Developer用户登录ORA-01045 user lacks CREATE SESSION privilege logon denied
  14. ipq wifi校准
  15. 你还不知道钉钉服务端API全局错误码吗?
  16. Using Spark's default log4j profile: org/apache/spark/log4j-defaults.properties
  17. 互动送书-《MySQL DBA工作笔记》签名版
  18. linux2019排行网站,2019年Linux系统TOP100排行 国产Deepin排名第十
  19. Unix时间戳和北京时间的相互转换(C语言实现 )
  20. 云桌面初体验 之 爱上无影云桌面

热门文章

  1. MBA——mba的9堂课
  2. atomic java_Java中Atomic类的使用分析
  3. 计算机大学老师简介,南开大学计算机学院导师教师师资介绍简介-李敏
  4. ITIL事件管理流程关键知识
  5. 【javase基础】第六篇:方法的重载与递归
  6. SPI FLASH 波形测量演示实例
  7. [ZT]第14节:粮食战争(14)--我的评论:中国农业的必经之路?
  8. 微软收购雅虎不如收购摩托罗拉
  9. 中国农业大学2020计算机院线,2020年中国农业大学考研分数线公布
  10. R语言安装NLP自然语言分析包