当前位置: > Linux故障 >

新解Linux 遭遇 Too many open files

时间:2014-06-23 20:37来源:linux.it.net.cn 作者:IT网

最近需要做一个Oracle BPM Enterprise for WebLogic Server的VM用于测试,而且操作系统得是Linux x86,而我自己机器都是跑x86_64的。

虽然根据Configuration Matrix,Ubuntu和Oracle 10g XE不是被支持的配置,但是用于测试,发行版根本不会是一个问题。因为一直以来,我怕麻烦一直用Debian或Ubuntu来作测试了:-)

环境:
OS: Ubuntu 8.10 Intrepid Ibex x86
Kernel: 2.6.27-7-generic
JDK: Sun JDK 1.6.0_10
Weblogic Server 10gR3 on JRockit 1.6.0_05 (R27.6.0-50 linux ia32)
注:我用的是Oracle Service Bus 10gR3的安装介质,包含了Weblogic Server 10gR3。
Oracle 10g XE for Debian/Ubuntu.deb package

注意:推荐创建一个新用户和目标目录用来安装和跑OBPM和WLS,安全原因;-) 当然也可以先sudo -s安装,然后把目标目录及其子目录的owner改成新创建的用户。

安装Oracle Service Bus和Oracle BPM 10gR3完毕之后
1. 启动用root启动admin center
/opt/OracleBPMwlHome/bin/./obpmadmcenter

2. Configuration – Directory tab,添加directory,更多信息请看官方安装指南。

通常设置都会自动完成,不像早期的5.7,一切Data Source,JMS modules,Realm都要手工配置,WAR/EAR要手工deploy。

不巧的是,progress bar在70%的时候停住了,被告知去看logs。
Oracle BPM

看了WLS和BPM Admin Center log之后发现如下的Exception,问题很明显是出在执行WLST的环节上:
java.io.FileNotFoundException: /opt/bea/user_projects/domains/bpm/config/config.xml (Too many open files)

原因很简单,几乎所有的Linux发行版本基于安全考虑都会限制用户在Terminal session由中能打开文件(实际上是File Descriptors)的最大数。1024 max open files对于WLS部署来说是绝对不够用的;-)

注:比较流行的发行版本,比如Debian/Ubuntu/Arch Linux/Gentoo的shell session限制都是1024。

临时解决方法:
只对当前Terminal session起作用,用以下命令增加该数值。
ulimit -n 131072
继续在此session中启动obpmadmcenter来配置directory和创建新的WLS domain用于部署bpm。如果选择修改一个已经存在的WLS domain的话,在执行启动脚本的Terminal session中需要用同样的方法增加该数值,否则多数会得到同样的错误。
Soft limit ulimit -a
Hard limit ulimit -aH
可以用来查看当前session user的各种限制,当然包括修改过的数值。


永久性解决方法:
1. 修改/etc/security/limits.conf (root)
增加如下
$user hard nofile 131072
$user是用来启动WLS的用户。
2048是建议的数值,若遇到同样问题可能需要再次增加。

*表示所有用户:

* soft nofile 131072
* hard nofile 131072
 

参考Oracle Enterprise Linux的推荐设置:

oracle hard nofile 131072
oracle soft nofile 131072
oracle hard nproc 131072
oracle soft nproc 131072
oracle soft core unlimited
oracle hard core unlimited
oracle soft memlock 3500000
oracle hard memlock 3500000
# Recommended stack hard limit 32MB for oracle installations
# oracle hard stack 32768
 

2. 其他来自Debian GNU/Linux官方文档和Oracle Technology Network的解决方法,直接修改内核参数,无须重启系统。
sysctl -w fs.file-max 65536
或者
echo "65536" > /proc/sys/fs/file-max
两者作用是相同的,前者改内核参数,后者直接作用于内核参数在虚拟文件系统(procfs, psuedo file system)上对应的文件而已。
可以用下面的命令查看新的限制
sysctl -a | grep fs.file-max
或者
cat /proc/sys/fs/file-max

修改内核参数
/etc/sysctl.conf
echo "fs.file-max=65536" >> /etc/sysctl.conf
sysctl -p

查看当前file handles使用情况:

sysctl -a | grep fs.file-nr

或者

cat /proc/sys/fs/file-nr
825 0 65536

输出格式: The number of allocated file handles, the number of free file handles, and the maximum number of file handles.

另外一个命令:
lsof | wc -l
有点让我困惑的是,以上两个命令获得的结果总是不相同的;-( 原因如下:
简单来说 file-nr 给出的是 File Descriptors (文件描述符,数据结构,程序用来打开文件所需要的 handle),而 lsof 列出的是 Open Files (文件),包括不是用文件描述符的。例如:当前目录,映射到内存中的 library 文件和可执行的文本文件(脚本?)。通常 lsof 输出要比 file-nr 大。

举个简单的例子:当前系统中Firefox打开的文件数:
lsof -p pid | wc -l
或者
lsof | grep pid | wc -l
再看一下这个进程pid所占用的文件描述符数
ls /proc/pid/fd | wc -l
对比一下就明白了,注:载入到内存的library文件详情可以看/proc/pid/maps。

此外,用sysctl来修改内核参数fs.file-max和用ulimit的区别,花了不少时间研究,讨教了Linux/FreeBSD/Solaris/OpenSolaris老鸟Jockey同学,得到点拨之后终于基本弄清楚其概念和区别了。

优先级(Open File Descriptors):
soft limit < hard limit < kernel (NR_OPEN => /proc/sys/fs/nr_open) < 实现最大file descriptor数采用的数据结构所导致的限制

The Linux kernel provides the getrlimit and setrlimit system calls to get and set resource limits per process. Each resource has an associated soft and hard limit. The soft limit is the value that the kernel enforces for the corresponding resource. The hard limit acts as a ceiling for the soft limit: an unprivileged process may only set its soft limit to a value in the range from 0 up to the hard limit, and (irreversibly) lower its hard limit. A privileged process (one with the CAP_SYS_RESOURCE capability) may make arbitrary changes to either limit value.

作为测试环境,尤其是用VMWare guest OS的形式,安装OpenSSH Server, webmin, phpsysinfo等工具可以提高效率。

针对Oracle Enterprise Linux和Red Hat Enterprise Linux的快捷解决方法:
另外OEL和RHEL可以直接安装oracle-validated包来解决安装Oracle数据库和中间件所需要的包依赖和系统配置问题,推荐!
yum install oracle-validated
或者下载后手动安装。

(责任编辑:IT)
------分隔线----------------------------