mina 文件句柄数太高分析与解决

来源:互联网 发布:php获取数组第一个key 编辑:程序博客网 时间:2024/06/11 07:40

Part1部分是一位博友辛苦做出的分析,贴出来。

之后是part2是给出如何解决下面的报错问题


Part1

2014-11-01 19:43:12,997  WARN (AbstractConnector.java:472) - 

java.io.IOException: Too many open files
at sun.nio.ch.ServerSocketChannelImpl.accept0(Native Method)
at sun.nio.ch.ServerSocketChannelImpl.accept(ServerSocketChannelImpl.java:226)
at org.eclipse.jetty.server.ServerConnector.accept(ServerConnector.java:336)
at org.eclipse.jetty.server.AbstractConnector$Acceptor.run(AbstractConnector.java:467)
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:607)
at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:536)
at java.lang.Thread.run(Thread.java:722)


Last login: Sat Nov  1 17:35:34 2014 from 220.184.86.17
[root@www ~]# free
             total       used       free     shared    buffers     cached
Mem:      16282708   16127828     154880          0       9148   13197512
-/+ buffers/cache:    2921168   13361540
Swap:      8208376          0    8208376

上周五20:22左右,服务器突然挂掉,LOG里全是“java.io.IOException: Too many open files”。
我在周六16点多重启服务器以后,第二天发现在周六19:36左右的时候,又挂掉了,现象完全一样。
挂掉时,free很少,cached很多。这次,我没有重启服务器,而是清除了一下cache,就恢复了。

从服务器的流量图中可以看到,从上周五开始,流入的流量(蓝色线)突然飚的很高。
而之前,它都是维持在很低的水平的,感觉像遭受了攻击一样。这个还要再分析了。
从年流量图上看,8月份(Aug)流入的流量也很高,这估计是因为8月中才加的防火墙:


[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. // 查看定时日志,发现20点以前都是好的,21点以后就挂了(内存耗尽,文件句柄用光)  
  2. # cat /home/lsofc.log  
  3. ===========================  
  4. 2014年 11月 07日 星期五 16:00:01 CST  
  5. Memory usage | [Use:5782 MB][Free:10118 MB][Cached:3942 MB]   
  6. File count | [All count:6957][JAVA count:912]   
  7. ===========================  
  8. 2014年 11月 07日 星期五 17:00:01 CST  
  9. Memory usage | [Use:5806 MB][Free:10094 MB][Cached:3950 MB]   
  10. File count | [All count:5855][JAVA count:864]   
  11. ===========================  
  12. 2014年 11月 07日 星期五 18:00:02 CST  
  13. Memory usage | [Use:5807 MB][Free:10093 MB][Cached:3957 MB]   
  14. File count | [All count:5717][JAVA count:980]   
  15. ===========================  
  16. 2014年 11月 07日 星期五 19:00:01 CST  
  17. Memory usage | [Use:5789 MB][Free:10110 MB][Cached:3951 MB]   
  18. File count | [All count:5837][JAVA count:1106]   
  19. ===========================  
  20. 2014年 11月 07日 星期五 20:00:01 CST  
  21. Memory usage | [Use:5830 MB][Free:10070 MB][Cached:3994 MB]   
  22. File count | [All count:6115][JAVA count:1033]   
  23. ===========================  
  24. 2014年 11月 07日 星期五 21:00:01 CST  
  25. Memory usage | [Use:14002 MB][Free:1898 MB][Cached:11084 MB]   
  26. File count | [All count:70417][JAVA count:65718]   
  27. ===========================  
  28. 2014年 11月 07日 星期五 22:00:01 CST  
  29. Memory usage | [Use:15732 MB][Free:169 MB][Cached:12875 MB]   
  30. File count | [All count:70406][JAVA count:65718]   
  31. ===========================  
  32. 2014年 11月 07日 星期五 23:00:01 CST  
  33. Memory usage | [Use:15749 MB][Free:150 MB][Cached:12847 MB]   
  34. File count | [All count:70394][JAVA count:65718]   
  35.   
  36. // 服务器挂掉后,查看各种信息  
  37. # free -m  
  38.              total       used       free     shared    buffers     cached  
  39. Mem:         15901      15742        158          0        127      12784  
  40. -/+ buffers/cache:       2830      13070  
  41. Swap:         8015          2       8013  
  42. // 打开的文件数,超出了限制(65536)  
  43. # lsof -n | wc -l  
  44. 70091  
  45. // 网络相关的文件句柄数,也非常多  
  46. # lsof -n -i | wc -l  
  47. 62548  
  48. // 网络连接数,也非常多  
  49. # netstat -ant | wc -l  
  50. 63777  
  51. // 端口为54104的连接数,也非常多  
  52. # netstat -ant | grep ":54104" | wc -l  
  53. 61101  
  54. // 查看端口为54104的各种状态的连接数,发现CLOSE_WAIT的超多  
  55. # netstat -ant | grep ":54104" | awk '{print $6}' | sort | uniq -c  | sort -nr   
  56.   54166 CLOSE_WAIT  
  57.    6920 ESTABLISHED  
  58.      10 SYN_RECV  
  59.       1 LISTEN  
  60.   
  61. // 所有网络连接数,按状态排序  
  62. # netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print S[a],a}' | sort -nr   
  63. 54184 CLOSE_WAIT  
  64. 8239 ESTABLISHED  
  65. 926 FIN_WAIT2  
  66. 207 FIN_WAIT1  
  67. 64 TIME_WAIT  
  68. 46 LAST_ACK  
  69. 31 SYN_RECV  
  70. 1 CLOSING  
  71. // 所有打开文件数,按进程PID排序  
  72. # lsof -n | awk '{print $2}' | sort -n | uniq -c | sort -nr | more  
  73.   65605 18374  
  74.     741 1348  
  75.     313 1349  
  76.     236 1350  
  77.     149 18407  
  78. // 发现确实是JAVA进程搞的鬼  
  79. # jps  
  80. 18374 DesktopServerLauncher  
  81. 14690 Bootstrap  
  82. // 关闭的JAVA进程  
  83. # ./shutdown.sh   
  84. shutting down  
  85. killing pid 18374  
  86. process has been shutdown  
  87. // 查看一下,确实关闭了  
  88. # jps  
  89. 14690 Bootstrap  
  90. 25788 Jps  
  91. // 再查看打开文件数,就少了JAVA进程的了  
  92. # lsof -n | awk '{print $2}' | sort -n | uniq -c | sort -nr | more  
  93.     637 1348  
  94.     547 1349  
  95.     296 1350  
  96.     126 1336  
  97.     112 14690  
  98.      86 25007  
  99. // 重启JAVA进程  
  100. # ./startup.sh   
  101. starting from ./boot.sh ...       
  102. // 查看一下,确实重启了  
  103. #jps  
  104. 25829 DesktopServerLauncher  
  105. 14690 Bootstrap  
  106. 25893 Jps  
  107. // 刚启动,其打开的文件数,并不多(428)  
  108. # lsof -n | awk '{print $2}' | sort -n | uniq -c | sort -nr | head -5  
  109.    1056 1347  
  110.    1052 1350  
  111.    1040 1349  
  112.    1037 1348  
  113.     428 25829  
  114. // 过了很久,再查看日志。发现:Cached很多,打开文件数很少,系统运行正常。  
  115. // 因此,得出结论:该问题与Cached无关,只跟打开的文件数太多有关系。  
  116. # tail -8 /home/lsofc.log   
  117. ===========================  
  118. 2014年 11月 07日 星期五 23:00:01 CST  
  119. Memory usage | [Use:15749 MB][Free:150 MB][Cached:12847 MB]   
  120. File count | [All count:70394][JAVA count:65718]   
  121. ===========================  
  122. 2014年 11月 08日 星期六 00:00:01 CST  
  123. Memory usage | [Use:14498 MB][Free:1404 MB][Cached:12697 MB]   
  124. File count | [All count:4272][JAVA count:232]  

由此判断,并非系统广播,而是遭受攻击导致的。


Part2

解决办法:将系统限制的文件句柄数调大~

服务器端修改:

  查看系统允许打开的最大文件数

  #cat /proc/sys/fs/file-max

  查看每个用户允许打开的最大文件数

  ulimit -a

  发现系统默认的是open files (-n) 65535,问题就出现在这里。

  在系统文件/etc/security/limits.conf中修改这个数量限制,

  在文件中加入内容:

  * soft nofile 65536

  * hard nofile 65536


  或者敲命令,更加方便:

  1.使用ps -ef |grep java   (java代表你程序,查看你程序进程) 查看你的进程ID,记录ID号,假设进程ID为12

  2.使用:lsof -p 12 | wc -l    查看当前进程id为12的 文件操作状况

  执行该命令出现文件使用情况为 51000

  3.使用命令:ulimit -a   查看每个用户允许打开的最大文件数

  发现系统默认的是open files (-n) 51000,问题就出现在这里。

  4.然后执行:ulimit -n 51000

  将open files (-n) 51000设置成open files (-n) 51000

  这样就增大了用户允许打开的最大文件数


0 0
原创粉丝点击