mongodb: directory-sync: open: Too many open files

问题描述

mongodb log:

2019-05-03T15:57:05.627+0800 I STORAGE  [conn2] createCollection: test.site_userid_31917 with generated UUID: a7deb9ff-dfa5-4272-b10d-2da69c7a2e3c
2019-05-03T15:57:05.653+0800 I STORAGE  [conn2] createCollection: test.site_userid_31918 with generated UUID: 8ae5dfdc-c416-482e-be28-ac0af623a512
2019-05-03T15:57:06.016+0800 E -        [ftdc] Assertion: Location13538: couldn't open [/proc/7276/stat] Unknown error src/mongo/util/processinfo_linux.cpp 81
2019-05-03T15:57:06.172+0800 E STORAGE  [conn2] WiredTiger error (24) [1556870226:172133][7276:0x7f135e8b7700], WT_SESSION.create: __posix_directory_sync, 145: /var/lib/mongodb/: directory-sync: open: Too many open files Raw: [1556870226:172133][7276:0x7f135e8b7700], WT_SESSION.create: __posix_directory_sync, 145: /var/lib/mongodb/: directory-sync: open: Too many open files
2019-05-03T15:57:06.172+0800 E STORAGE  [conn2] WiredTiger error (24) [1556870226:172364][7276:0x7f135e8b7700], WT_SESSION.create: __posix_directory_sync, 160: /var/lib/mongodb/index-63839--3770517573993711728.wt: directory-sync: Too many open files Raw: [1556870226:172364][7276:0x7f135e8b7700], WT_SESSION.create: __posix_directory_sync, 160: /var/lib/mongodb/index-63839--3770517573993711728.wt: directory-sync: Too many open files
2019-05-03T15:57:06.172+0800 E STORAGE  [conn2] WiredTiger error (-31804) [1556870226:172415][7276:0x7f135e8b7700], WT_SESSION.create: __wt_panic, 520: the process must exit and restart: WT_PANIC: WiredTiger library panic Raw: [1556870226:172415][7276:0x7f135e8b7700], WT_SESSION.create: __wt_panic, 520: the process must exit and restart: WT_PANIC: WiredTiger library panic
2019-05-03T15:57:06.176+0800 F -        [conn2] Fatal Assertion 50853 at src/mongo/db/storage/wiredtiger/wiredtiger_util.cpp 409
2019-05-03T15:57:06.178+0800 F -        [conn2] 

***aborting after fassert() failure


2019-05-03T15:57:06.305+0800 F -        [conn2] Got signal: 6 (Aborted).
 0x55c4d32c1141 0x55c4d32c0359 0x55c4d32c083d 0x7f13709df890 0x7f137061ae97 0x7f137061c801 0x55c4d18c1609 0x55c4d19bfe06 0x55c4d1a32569 0x55c4d1848a58 0x55c4d1848e72 0x55c4d19fc5fe 0x55c4d19fce3b 0x55c4d19f8dfb 0x55c4d1a548d6 0x55c4d1a17d46 0x55c4d1a1870b 0x55c4d1a18061 0x55c4d1a2d23a 0x55c4d1a2d5b6 0x55c4d1988320 0x55c4d1993d92 0x55c4d1b2f38a 0x55c4d205c5e2 0x55c4d205f27c 0x55c4d2054c3c 0x55c4d204ebf3 0x55c4d1f9f397 0x55c4d1f9f704 0x55c4d1f9fa0a 0x55c4d1fa43bc 0x55c4d1f9ddcf 0x55c4d1f9c022 0x55c4d19730a8 0x55c4d19750cb 0x55c4d19769e9 0x55c4d1977921 0x55c4d196306a 0x55c4d196fb8a 0x55c4d196a907 0x55c4d196e061 0x55c4d2b17b42 0x55c4d1968af0 0x55c4d196bbe5 0x55c4d196a047 0x55c4d196a98d 0x55c4d196e061 0x55c4d2b180a5 0x55c4d3218af4 0x7f13709d46db 0x7f13706fd88f
----- BEGIN BACKTRACE -----

问题出现的环境背景及自己尝试过哪些方法

复制,测试用例:

In [19]: prefix = 'site_userid_'
In [20]: for i in range(100000): 
    ...:     coll_name = prefix + str(i) 
    ...:     coll = db[coll_name] 
    ...:     coll.insert_one({"name": coll_name}) 
    ...:

在创建 30000 个 collection 前的 GIF:

大约在 3000 个 collection 的时候出问题。

已经实验过的方案:

  1. 修改类UNIX file limit
  2. 修改代码 - “定量” 重启 MongoClient

无效。


你期待的结果是什么?实际看到的错误信息又是什么?

欢迎有朋友如果能指出以上是我 MongoDB 哪里配置地不对?
或者是 coding 方面用法有误?

在 google 上查找了一天没有解决。

注:
有疑问在评论区评论。
不要凭感觉贴没有验证过的 solution,

完全可以接受我使用 mongdb 的方式不对的说法,但是请说明理由,以及正确的使用方式。

这个 test case 的 idea 是一个网站(site) 有数(十,百)万个 user id,
每个 user id 根据其唯一的 user id 对应一个 collection,里面存有(比如)user 的 profile 等内容。

不清不楚请勿回答,可在评论区交流,谢谢。

阅读 5.6k
1 个回答

经过讨论,我想问题的答案已经比较清楚了。清楚以下的原则:

  1. 每个集合的数据文件会单独创建一个文件(collection-xxxxxx.wt)
  2. 每个索引文件会单独创建一个文件(index-xxxxxx.wt)
  3. 每个集合会需要一个数据文件和一个默认索引(_id索引)

以上问题代表着每创建1个集合会占用2个文件句柄,64000个句柄最多可以创建32000个集合。但还需要除去MongoDB自身需要使用的句柄(内部文件如wiredTiger.wt之类,加上网络连接),所以当你创建到site_userid_31918就没有足够的句柄了,这是正常的行为。
问题还是出在“为什么要创建这么多集合”上面。如我评论中所述,MongoDB推荐的方式是分片,而不是分库分表。后者在使用上会对程序逻辑有很强的侵入,造成逻辑复杂化。除此之外还会造成其他的问题,比如重启时间会很长。因为这不是MongoDB推荐的使用方式。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进