新解Linux 遭遇 Too many open files

來源:互聯網
上載者:User

最近需要做一個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。

看了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
或者下載後手動安裝。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.