Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"too many open files" error occurs after using it for a while #30

Open
KEINOS opened this issue Apr 12, 2022 · 0 comments
Open

"too many open files" error occurs after using it for a while #30

KEINOS opened this issue Apr 12, 2022 · 0 comments

Comments

@KEINOS
Copy link
Owner

KEINOS commented Apr 12, 2022

Q: After using it for a while, we get "too many open files" errors.

During db.vlog.open error: Unable to open log dir.. Path=/var/folders/8c/lmckjks95fj4h_jqzw4v3k_w0000gn/T/QiiTrans. Error=fcntl /var/folders/8c/lmckjks95fj4h_jqzw4v3k_w0000gn/T/QiiTrans: too many open files

A: Due to the limitation of max file descriptors of the operating system, cache files more than the default size will cause this error.

Are there any Linux specific settings that I should use?

We recommend setting max file descriptors to a high number depending upon the expected size of your data. On Linux and Mac, you can check the file descriptor limit with ulimit -n -H for the hard limit and ulimit -n -S for the soft limit. A soft limit of 65535 is a good lower bound. You can adjust the limit as needed.

(from: Are there any Linux specific settings that I should use? | FAQ | badger @ dgraph.io)

We have a plan to switch the cache DB from badger to SQLite3. Though, as of 2023/03/07, the current workaround is to change the max file descriptors of the OS settings.

On macOS, you need to change the /Library/LaunchDaemons/limit.maxfiles.plist as below.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
  <dict>
    <key>Label</key>
    <string>limit.maxfiles</string>
    <key>ProgramArguments</key>
    <array>
      <string>launchctl</string>
      <string>limit</string>
      <string>maxfiles</string>
      <string>65535</string>
      <string>65535</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
    <key>ServiceIPC</key>
    <false/>
  </dict>
</plist>

しばらく使い続けると "too many open files" エラーが発生する

現象

QiiTrans をしばらく使い続けると以下の too many open files エラーがでて使えなくなります。

  • キャッシュ・ディレクトリを削除すると使えるようになる。
  • キャッシュ・ディレクトリは 2.0 MB 程度。
  • macOS や Linux で発生する。
ログ
$ qiitrans ja en es en ja --verbose
- ストップワード: q
>>> おはようございます。
Opening file: "/var/folders/8c/lmckjks95fj4h_jqzw4v3k_w0000gn/T/QiiTrans/000389.sst" error: open /var/folders/8c/lmckjks95fj4h_jqzw4v3k_w0000gn/T/QiiTrans/000389.sst: too many open files
unable to open: /var/folders/8c/lmckjks95fj4h_jqzw4v3k_w0000gn/T/QiiTrans/000389.sst
github.com/dgraph-io/ristretto/z.OpenMmapFile
	/home/runner/go/pkg/mod/github.com/dgraph-io/ristretto@v0.0.4-0.20210309073149-3836124cdc5a/z/file.go:80
github.com/dgraph-io/badger/v3.newLevelsController.func1
	/home/runner/go/pkg/mod/github.com/dgraph-io/badger/v3@v3.2103.0/levels.go:145
runtime.goexit
	/opt/hostedtoolcache/go/1.17.6/x64/src/runtime/asm_amd64.s:1581

$ # キャッシュを使わない
$ qiitrans ja en es en ja --verbose --no-cache
- ストップワード: q
>>> おはようございます。
Opening file: "/var/folders/8c/lmckjks95fj4h_jqzw4v3k_w0000gn/T/QiiTrans/000203.sst" error: open /var/folders/8c/lmckjks95fj4h_jqzw4v3k_w0000gn/T/QiiTrans/000203.sst: too many open files
unable to open: /var/folders/8c/lmckjks95fj4h_jqzw4v3k_w0000gn/T/QiiTrans/000203.sst
github.com/dgraph-io/ristretto/z.OpenMmapFile
	/home/runner/go/pkg/mod/github.com/dgraph-io/ristretto@v0.0.4-0.20210309073149-3836124cdc5a/z/file.go:80
github.com/dgraph-io/badger/v3.newLevelsController.func1
	/home/runner/go/pkg/mod/github.com/dgraph-io/badger/v3@v3.2103.0/levels.go:145
runtime.goexit
	/opt/hostedtoolcache/go/1.17.6/x64/src/runtime/asm_amd64.s:1581

$ # 容量確認
$ du -d1 -h /var/folders/8c/lmckjks95fj4h_jqzw4v3k_w0000gn/T/QiiTrans
2.0M	/var/folders/8c/lmckjks95fj4h_jqzw4v3k_w0000gn/T/QiiTrans

$ # キャッシュを完全削除してから使う
$ qiitrans ja en es en ja --verbose --clear
- ストップワード: q
>>> おはようございます。
JA -> EN: Good morning.
EN -> ES: Buenos días.
ES -> EN: Good morning.
再翻訳: おはようございます。

原因

ログの流れを見ると、キャッシュを使う・使わない関係なく、DB を読み込む時点でエラーが発生していると思われます。

キャッシュ DB として利用している badger の下記によると、恐らく OS 側の仕様が関係していると思われます。

Are there any Linux specific settings that I should use?

We recommend setting max file descriptors to a high number depending upon the expected size of your data. On Linux and Mac, you can check the file descriptor limit with ulimit -n -H for the hard limit and ulimit -n -S for the soft limit. A soft limit of 65535 is a good lower bound. You can adjust the limit as needed.

Are there any Linux specific settings that I should use? | FAQ | badger @ dgraph.io より)

つまり、OS 側のファイルディスクリプタの制限で、ソフトウェア側が同時に参照できるファイル数を超過したのだと思われます。

確かに、ulimit で制限を確認するとハードとしては無制限に設定されているものの、ソフトは 256 個しか許可されていません。

$ ulimit -n -S
256
$ ulimit -n -H
unlimited
$ launchctl limit maxfiles
	maxfiles    256            unlimited

回避方法

badger 公式が "We recommend setting max file descriptors to a high number" と言っているように、Soft-ulimit の上限を引き上げる必要があります。

  • macOS の場合、以下の .plist ファイルを root:wheel で作成し制限を引き上げる必要がある。
/Library/LaunchDaemons/limit.maxfiles.plist
  • /Library/LaunchDaemons/limit.maxfiles.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
  <dict>
    <key>Label</key>
    <string>limit.maxfiles</string>
    <key>ProgramArguments</key>
    <array>
      <string>launchctl</string>
      <string>limit</string>
      <string>maxfiles</string>
      <string>65535</string>
      <string>65535</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
    <key>ServiceIPC</key>
    <false/>
  </dict>
</plist>

結論

badger はサーバー用途にも使われるファイル・ベースの高速 DB です。しかし、QiiTrans のためだけに、ユーザーに .plist の作成や権限付与といった操作をお願いするのは難しいです。また、256 ファイルの制限は、英文ベースのコントリビュートがメインの場合は、すぐに到達してしまうと思われます。

かと言って DB サーバーを用意する規模でもないため、ここは素直に SQLite3 を使うのが得策と思われます。

参考情報

@KEINOS KEINOS changed the title しばらく使い続けると "too many open files" エラー "too many open files" error occurs after using it for a while Mar 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant