2022-04-21

tomcat 9 發生 "too many open files" (開啟太多檔案)

 前二天早上tomcat突然掛掉,log 顯示如下:

  20-Apr-2022 10:22:41.714 嚴重 [https-jsse-nio2-8443-Acceptor-0] org.apache.tomcat.util.net.Acceptor.run Socket accept failed
 java.util.concurrent.ExecutionException: java.io.IOException: 開啟太多檔案
	at sun.nio.ch.CompletedFuture.get(CompletedFuture.java:69)
	at org.apache.tomcat.util.net.Nio2Endpoint.serverSocketAccept(Nio2Endpoint.java:334)
	at org.apache.tomcat.util.net.Nio2Endpoint.serverSocketAccept(Nio2Endpoint.java:59)
	at org.apache.tomcat.util.net.Acceptor.run(Acceptor.java:95)
	at java.lang.Thread.run(Thread.java:748)
Caused by: java.io.IOException: 開啟太多檔案
	at sun.nio.ch.UnixAsynchronousServerSocketChannelImpl.accept0(Native Method)
	at sun.nio.ch.UnixAsynchronousServerSocketChannelImpl.accept(UnixAsynchronousServerSocketChannelImpl.java:344)
	at sun.nio.ch.UnixAsynchronousServerSocketChannelImpl.implAccept(UnixAsynchronousServerSocketChannelImpl.java:280)
	at sun.nio.ch.AsynchronousServerSocketChannelImpl.accept(AsynchronousServerSocketChannelImpl.java:125)
	... 4 more

初步調查是tomcat開啟了太多檔案,造成"too many open files" (開啟太多檔案),網路上一般說法是說要調整系統的ulimit上限才行,但想想不太對,這系統會需要同時開啟的檔案不多,而且運行了幾年也沒有發生file leak的狀況。

先找出tomcat的pid,再用 ls -al /proc/<pid>/fd | wc -l,就可以知道tomcat目前開啟多少檔案(包含unix socket file)。

後來發現是tomcat自已的bug,會一直存取tomcat-users.xml卻沒有釋放資源,解法是升級 tomcat 9 >= 9.0.14+,或修改 server.xml,加入watchSource="false":

<resource 
 auth="Container"
 description="User database that can be updated and saved"
 factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
 name="UserDatabase" pathname="conf/tomcat-users.xml"
 type="org.apache.catalina.UserDatabase"
 watchsource="false">
ref:
https://bz.apache.org/bugzilla/show_bug.cgi?id=62924#c2
https://zhuanlan.zhihu.com/p/75897823
https://www.cnblogs.com/linus-tan/p/10328801.html

2022-04-16

ubuntu/mint下chromium啟用硬體加速+解決netflix無法觀看

在ubuntu/mint下要啟用chromium的硬體加速,必須是pathced過的版本,

可以用 https://launchpad.net/~saiarcot895/+archive/ubuntu/chromium-beta 
(包含Widevine,觀看netflix等有DRM保護的串流時需要)

或 https://github.com/Eloston/ungoogled-chromium#downloads 
(不包含Widevine)

先裝好對應各顯卡的VA-API driver,啟動chromium時加上以下參數:

chromium-browser --use-gl=desktop --enable-features=VaapiVideoDecoder --disable-features=UseChromeOSDirectVideoDecoder
(chromium 98開始需加上 --disable-features=UseChromeOSDirectVideoDecoder)

====2024.09更新(在128的版本實測有效):==========

https://ubuntuhandbook.org/index.php/2024/01/chromium-va-api-wayland/
https://debugpointnews.com/va-api-chromium-wayland/

chromium 121以後的版本,可以不用再找有va-api patched過的版本,直接使用以下參數開啟硬體加速:

On X11:
chromium-browser --enable-features=VaapiVideoDecodeLinuxGL --use-gl=angle --use-angle=gl --ozone-platform=x11

On Wayland:
chromium-browser --enable-features=VaapiVideoDecodeLinuxGL --use-gl=angle --use-angle=gl --ozone-platform=wayland

=====================================

用ungoogled-chromium又想加上Widevine DRM支援時,
可先下載對應版本的Chrome DEB安裝檔(stable, unstable),
解開後,把 ./opt/google/chrome/WidevineCdm 複製到
 /usr/lib/chromium/WidevineCd 或 ~/.local/lib/WidevineCdm

ref:
https://wiki.archlinux.org/title/Chromium#Hardware_video_acceleration
https://www.linuxuprising.com/2018/08/how-to-enable-hardware-accelerated.html
https://ungoogled-software.github.io/ungoogled-chromium-wiki/faq#linux