如何为在 Debian Jessie 上运行的守护进程提高文件描述符的最大数量?

LBC*_*LBC 7 networking linux debian database performance-tuning

我正在使用pgBouncer作为 PostgreSQL 的连接池系统。我的系统是 12 核、64GB RAM 和 1Gbps 网络接口,运行 Debian 8.1。现在我想将开放套接字连接的限制提高到 10.000 个并发客户端。在进行 DB 基准测试时,pgbench实用程序会阻止大约 950 个并发客户端,这似乎达到了 1024 个打开 fds 的限制,就像过去一样。我检查了fs.file-max内核参数和pgbench运行用户的资源限制:

# sysctl fs.file-max
fs.file-max = 6598264
# su - postgres
$ ulimit -Sn
65536
$ fgrep files /proc/self/limits
Max open files            65536                65536                files
$ 
Run Code Online (Sandbox Code Playgroud)

但是,限制proc表明pgBouncer(以用户身份运行postgres)的最大打开文件的软限制仅为 1024 个最大打开文件:

$ ps -e | fgrep pgbouncer
 9840 ?        00:00:00 pgbouncer
$ fgrep files /proc/9840/limits
Limit                     Soft Limit           Hard Limit           Units
Max open files            1024                 4096                 files
$
Run Code Online (Sandbox Code Playgroud)

我试图通过将提高上限ulimit -S -n 5000/etc/default/pgbouncer(在启动/停止脚本读取/etc/init.d),但没有奏效。然后我尝试nofile设置/etc/security/limits.conf并确保它在 PAM 中启用,但无济于事。

那么,究竟在哪里start-stop-daemon降低nofile守护进程的限制?我偶然发现了这个针对 Debian 的旧错误报告,但似乎从未应用过该补丁。

顺便说一句:fs.file-max真的是nofiles像许多关于调整的博客文章中建议的那样替换以前系统的(注意复数)内核变量吗?让我想知道它是在fs参数部分。在我的 IRIX 系统上,它rlimit_no_files_max在资源部分被调用,这对我来说比将它放在该fs部分更有意义..

我在这里做错了什么?在 Debian 8.1 中为守护进程更改此参数的正确位置在哪里?

提前致谢,

斯特凡

Mor*_*gan 8

您可以在 pgbouncer init 脚本的“开始”部分使用“ulimit”:

/etc/init.d/pgbouncer:

    [... snip ...]
    case "$1" in
      start)
        # Check if we are still disabled in /etc/default/pgbouncer
        [ "${START:-}" = "0" ] && exit 0
        log_daemon_msg "Starting PgBouncer" $NAME
        test -d $PIDDIR || install -d -o postgres -g postgres -m 2775 $PIDDIR

        ### set whatever limits you want ###
        ulimit -n 20000

        $SSD --start --chuid $RUNASUSER --oknodo -- $OPTS 2> /dev/null
        log_end_msg $?
        ;;
    [... snip ...]
Run Code Online (Sandbox Code Playgroud)

添加此行后,应在重新启动后产生效果:

这样做,否则你会被骂:

# systemctl daemon-reload
Run Code Online (Sandbox Code Playgroud)

然后:

# /etc/init.d/pgbouncer restart
Run Code Online (Sandbox Code Playgroud)

更新:

我的原始答案显示您可以在 init 脚本中添加“ulimit”,它适用于 Debian 8。尽管 Deb8 是一个 systemd 发行版,但 pgbouncer 的默认安装似乎仍然使用 init 脚本。

对于 Centos7,它完全使用 systemd 管理 pgbouncer,您需要使用 systemd.service 文件。

首先,Centos7 上 pgbouncer 的默认服务文件中似乎存在一个错误,您需要将“Type=forked”设置为“Type=simple”。

在 pgbouncer.service 文件中,您还可以在 [Service] 部分添加一个“LimitNOFILE=##”...因此

/etc/systemd/system/pgbouncer.service

    ## good practice to include the default service file as it may change with future updates
    .include /lib/systemd/system/pgbouncer.service

    ## change the Type= per this bug
    ## http://www.postgresql.org/message-id/554A7105.1050002@gmx.net
    Type=simple

    ## Add a service section and set the max number of open files
    [Service]
    LimitNOFILE=12345
Run Code Online (Sandbox Code Playgroud)

可能值得验证最大打开文件数是瓶颈。你可以检查你的日志,本质上是“打开的文件太多”错误消息。在我超过允许打开文件的最大数量的每种情况下,该过程都会抱怨......

/var/log/postgresql/pgbouncer.log

ERROR S: login failed: FATAL: could not open relation mapping file (...): Too many open files in system
ERROR S: login failed: FATAL: could not open file (...): Too many open files in system
ERROR S: login failed: FATAL: pipe() failed: Too many open files in system
WARNING sbuf_connect failed: Too many open files in system
Run Code Online (Sandbox Code Playgroud)

/var/log/postgresql/postgresql-9.4-main.log

LOG:  out of file descriptors: Too many open files in system; release and retry
PANIC:  could not open file (...): Too many open files in system
LOG:  server process (...) was terminated by signal 6: Aborted
DETAIL:  Failed process was running: END;
LOG:  terminating any other active server processes
Run Code Online (Sandbox Code Playgroud)

我们不需要担心 pgbench 的可用 FD,因为如果它们不够用,pgbench 将不会运行:

$ ulimit -S -n 100
$ pgbench -h 192.168.122.69 -p 6432 -U postgres -d booktown -c 200 -t 10000
You need at least 202 open files but you are only allowed to use 100.
Use limit/ulimit to increase the limit before using pgbench.
Run Code Online (Sandbox Code Playgroud)