当前位置: 首页 > news >正文

深入排查:Tomcat生成Session ID耗时166秒,竟是因为熵池枯竭?

现象

周一早上,运维小王泡了杯茶,准备开始一天的工作。他像往常一样打开内部管理系统,输入账号密码,点击登录……然后,茶杯见底了,页面还在转圈。
一些内部管理系统,Java的程序部署到服务器之后,由于并发不高,晚上没人使用,第2天或者周一早上开始使用的人会登录特别缓慢。

分析

反复检查程序都没有发现问题,主要一开始怀疑IO的问题,因为内存中的一般不会出现超时的。但是没发现IO问题,却在无意间在上看到了一下提示。

2020-07-25 11:08:50 - [WARN ] [http-nio-8092-exec-10] o.a.c.util.SessionIdGeneratorBase : Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [28,190] milliseconds. 2020-07-25 11:08:50 - [WARN ] [http-nio-8092-exec-6] o.a.c.util.SessionIdGeneratorBase : Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [65,102] milliseconds. 2020-07-25 11:08:50 - [WARN ] [http-nio-8092-exec-2] o.a.c.util.SessionIdGeneratorBase : Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [166,254] milliseconds.

从日志可以看出,SessionIdGeneratorBase这个类要创建一个SecureRandom用于生成sessionID,居然用了几十秒到一百多秒不等的时间。

原因

原因是Tomcat在产生session ID时的SHA1PRANG算法使用了jre的SecureRandom。SHA1PRNG 算法是基于 SHA-1 算法实现且保密性较强的伪随机数生成器。而SHA1PRNG的种子产生器,是通过读取$JAVA_HOME/jre/lib/security/java.security 这个文件获取的,配置项为securerandom.source。

以下是java.security的配置片段:

# "file:/dev/urandom" will enable the native Microsoft CryptoAPI seeding # mechanism for SHA1PRNG. # # By default, an attempt is made to use the entropy gathering device # specified by the "securerandom.source" Security property. If an # exception occurs while accessing the specified URL: # # SHA1PRNG: # the traditional system/thread activity algorithm will be used. # # NativePRNG: # a default value of /dev/random will be used. If neither # are available, the implementation will be disabled. # "file" is the only currently supported protocol type. # # The entropy gathering device can also be specified with the System # property "java.security.egd". For example: # # % java -Djava.security.egd=file:/dev/random MainClass # # Specifying this System property will override the # "securerandom.source" Security property. # # In addition, if "file:/dev/random" or "file:/dev/urandom" is # specified, the "NativePRNG" implementation will be more preferred than # SHA1PRNG in the Sun provider. # securerandom.source=file:/dev/random

这个/dev/random被称为收集环境噪音的熵收集装置(也叫熵池The entropy gathering device),系统将当前运行的随机状态写入其中,作为伪随机数的种子。/dev/random非常适合那些需要非常高质量随机性的场景,比如一次性的支付或生成密钥的场景。但random有个特点就是阻塞式的,直到熵池收集到足够的环境噪声数据。由于长时间无人使用系统,就会导致熵池变空,从而无法产生随机数。我们看到这个配置的注释里也提到了另外一个文件,那就是/dev/urandom。它和/dev/random的区别就是,urandom是ublock的。

因此,由于无法产生随机数,jvm就阻塞住了,直至有足够的环境噪音,这样子随机数的随机行当然是高了很多,但也给系统带来了问题。可以将

securerandom.source=file:/dev/random

改为

securerandom.source=file:/dev/./urandom

为什么会多了个./呢,据说这是jvm的一个bug,有人反馈即使对 securerandom.source 设置为 /dev/urandom 它也仍然使用的 /dev/random。所以使用/dev/./urandom 这样的变通方法。也有人评论说这个不是 bug,是有意为之。

http://www.jsqmd.com/news/483882/

相关文章:

  • 如何使用SoccerOnTable:将足球视频转换为3D AR/VR体验的完整指南
  • 批量字符替换工具技术解析:原理、特性与应用实践
  • 告别死记硬背!图解AVL树的四种旋转,代码实现也不难
  • 【Python入门实战】一周吃透基础语法!
  • 终极指南:如何用《每日等效香烟》App直观了解城市空气污染
  • 编译器扩展与C++兼容性
  • 探索无限智能:`analysis-pinyin` - 汉字拼音分析利器
  • conda建立keras和pytorch环境
  • 软考高项:第22章:组织通用治理(占分分析/考点/题)
  • JavaScript性能优化实战翱拱
  • 探索Damn Vulnerable Defi Foundry:打造DeFi安全专家之路
  • 动态规划(dp)——完全背包题目
  • C++与Rust交互编程
  • 南大通用(GBase 8s)数据库在 Spring Boot 中使用 Flyway 和 Flowable
  • CN_GreenLumaGUI 项目推荐
  • 探索《最佳数据科学资源》项目:一站式学习与进阶宝典
  • 模板编译期计算
  • 常用windows命令【端口-进程查询、查询包含某个字符串的文件】
  • 如何快速掌握 Skylark in Go:灵活强大的配置语言与脚本引擎全指南
  • Spring Aop失效的情況及解决办法
  • WebLaF高级特性详解:动画效果、自定义皮肤与响应式设计
  • 10个创意案例:用react-nice-avatar打造独特用户头像系统
  • 如何在Windows上测试ip和端口
  • 2026年比较好的碱液屏蔽泵品牌推荐:液冷屏蔽泵用户口碑认可厂家 - 行业平台推荐
  • CN_GreenLumaGUI 项目常见问题解决方案
  • 如何用gh_mirrors/ta/tagger快速实现专业级命名实体识别?3步上手教程
  • Mybatis二级缓存
  • e3nn高级教程:如何自定义具有欧几里得对称性的神经网络层
  • 2026年质量好的自吸式屏蔽泵厂家推荐:氟化氢屏蔽泵/氯甲烷屏蔽泵/管道循环屏蔽泵厂家信誉综合参考 - 品牌宣传支持者
  • 10个Biostar Central项目常见问题的终极解决方案