Discourse 占用内存不多

你好!
我昨天检查过,网站当时是正常的。今天早上醒来发现网站已无法访问,显示 502 Bad Gateway 错误。这是监控日志:monitor.txt(9.4 MB)。

哇……怎么有这么多 Java?看起来我的 Jitsi 服务器也曾经这样过:exploding_head
另外,你似乎还在使用主机作为邮件服务器。我最终将邮件服务与应用分离了,这样在需要恢复时会更容易(同时保持整洁,尽可能接近标准的生产环境安装)。为邮件服务单独分配一个带有浮动 IP 的小型实例,可能也更便于监控信誉,而且成本增加也有限。

好的,让我们看看……首先我们搜索“average”:

 06:24:13 up 3 days,  8:13,  0 users,  load average: 0.01, 0.05, 0.08
 06:34:33 up 3 days,  8:23,  0 users,  load average: 12.89, 11.68, 6.35

所以在 06:34 之前不久,可运行进程的数量突然激增,系统“爆炸”了。

再扩大一点搜索范围,我们会发现内存使用量其实并没有太大变化:

 06:24:13 up 3 days,  8:13,  0 users,  load average: 0.01, 0.05, 0.08
              total        used        free      shared  buff/cache   available
Mem:        8167548     3513172     3241404      371940     1412972     4512016
Swap:      10485756           0    10485756
--
 06:34:33 up 3 days,  8:23,  0 users,  load average: 12.89, 11.68, 6.35
              total        used        free      shared  buff/cache   available
Mem:        8167548     3110612     1351844      373268     3705092     4812672
Swap:      10485756           0    10485756

事实上,内存使用量甚至有所下降,也许有某个进程已经终止:

 06:13:53 up 3 days,  8:02,  0 users,  load average: 0.04, 0.16, 0.15
              total        used        free      shared  buff/cache   available
Mem:        8167548     3505540     3259744      371932     1402264     4519680
 06:24:13 up 3 days,  8:13,  0 users,  load average: 0.01, 0.05, 0.08
              total        used        free      shared  buff/cache   available
Mem:        8167548     3513172     3241404      371940     1412972     4512016
 06:34:33 up 3 days,  8:23,  0 users,  load average: 12.89, 11.68, 6.35
              total        used        free      shared  buff/cache   available
Mem:        8167548     3110612     1351844      373268     3705092     4812672
 06:44:53 up 3 days,  8:33,  0 users,  load average: 13.64, 13.25, 9.89
              total        used        free      shared  buff/cache   available
Mem:        8167548     3093212     3618984      373276     1455352     4840140
 06:55:13 up 3 days,  8:44,  0 users,  load average: 11.62, 12.70, 11.42
              total        used        free      shared  buff/cache   available
Mem:        8167548     3140580     3366628      373352     1660340     4792440

看起来 Unicorn 进程已经重启:

Thu Aug  5 06:24:13 CEST 2021
 06:24:13 up 3 days,  8:13,  0 users,  load average: 0.01, 0.05, 0.08
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root     30389  0.0  0.0   2160  1148 ?        Ss   Aug03   0:00          \\_ runsv unicorn
x        13329  0.0  0.0  15132  3712 ?        S    Aug04   0:26              \\_ /bin/bash config/unicorn_launcher -E production -c config/unicorn.conf.rb
x        13365  0.0  3.0 542036 251584 ?       Sl   Aug04   0:23                  \\_ unicorn master -E production -c config/unicorn.conf.rb
x        13846  0.6  4.5 9129356 371852 ?      SNl  Aug04   4:19                  |   \\_ sidekiq 6.2.1 discourse [0 of 5 busy]
x        13858  0.0  3.9 8971616 323264 ?      Sl   Aug04   0:18                  |   \\_ unicorn worker[0] -E production -c config/unicorn.conf.rb
x        13870  0.0  4.0 8975712 330660 ?      Sl   Aug04   0:20                  |   \\_ unicorn worker[1] -E production -c config/unicorn.conf.rb
x        13889  0.0  4.1 8979808 337180 ?      Sl   Aug04   0:20                  |   \\_ unicorn worker[2] -E production -c config/unicorn.conf.rb
x        29760  0.0  0.0  13708  2220 ?        S    06:24   0:00                  \\_ sleep 1

Thu Aug  5 06:34:33 CEST 2021
 06:34:33 up 3 days,  8:23,  0 users,  load average: 12.89, 11.68, 6.35
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root     30389  0.0  0.0   2160  1148 ?        Ss   Aug03   0:00          \\_ runsv unicorn
x         4638  0.4  0.0  15132  3784 ?        S    06:32   0:00              \\_ /bin/bash config/unicorn_launcher -E production -c config/unicorn.conf.rb
x         4642 11.1  2.8 439636 233240 ?       Sl   06:32   0:11                  \\_ unicorn master -E production -c config/unicorn.conf.rb
x         4822 13.1  3.7 8877408 309844 ?      Sl   06:33   0:10                  |   \\_ unicorn worker[1] -E production -c config/unicorn.conf.rb
x         5025 15.6  3.7 8873312 310264 ?      Sl   06:33   0:10                  |   \\_ unicorn worker[0] -E production -c config/unicorn.conf.rb
x         5065 20.6  3.9 8922504 320740 ?      SNl  06:33   0:11                  |   \\_ sidekiq 6.2.1 discourse [0 of 5 busy]
x         5639  0.0  0.0  13708  2264 ?        S    06:34   0:00                  \\_ sleep 1

看起来 Apache 在启动时也遇到了问题:

Thu Aug  5 06:24:13 CEST 2021
 06:24:13 up 3 days,  8:13,  0 users,  load average: 0.01, 0.05, 0.08
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root     13730  0.0  0.1 225016 15840 ?        Ss   Aug02   0:17 /usr/sbin/apache2 -k start
www-data 10232  0.0  0.1 1352372 10972 ?       Sl   Aug03   0:00  \\_ /usr/sbin/apache2 -k start
www-data 27998  0.0  0.1 228880  9608 ?        S    Aug04   0:01  \\_ /usr/sbin/apache2 -k start
www-data 28000  0.0  0.3 3036536 27796 ?       Sl   Aug04   0:53  \\_ /usr/sbin/apache2 -k start
www-data 28002  0.0  0.3 2904772 26524 ?       Sl   Aug04   0:50  \\_ /usr/sbin/apache2 -k start
www-data 28185  0.0  0.1 762472 10952 ?        Sl   Aug04   0:00  \\_ /usr/sbin/apache2 -k start

Thu Aug  5 06:34:33 CEST 2021
 06:34:33 up 3 days,  8:23,  0 users,  load average: 12.89, 11.68, 6.35
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root     13730  0.0  0.1 225024 15856 ?        Ss   Aug02   0:17 /usr/sbin/apache2 -k start
www-data 10232  0.0  0.1 1352372 10972 ?       Sl   Aug03   0:00  \\_ /usr/sbin/apache2 -k start
www-data 28185  0.0  0.1 762472 10952 ?        Sl   Aug04   0:00  \\_ /usr/sbin/apache2 -k start
www-data 30794  0.0  0.1 228888  9620 ?        S    06:25   0:00  \\_ /usr/sbin/apache2 -k start
www-data 31490  0.0  0.1 344564 10852 ?        Sl   06:25   0:00  \\_ /usr/sbin/apache2 -k start
www-data 32095  0.0  0.1 500228 10888 ?        Sl   06:25   0:00  \\_ /usr/sbin/apache2 -k start
www-data 32215 83.2  0.1 369152 10876 ?        Sl   06:25   7:26  \\_ /usr/sbin/apache2 -k start
www-data 32284  0.0  0.1 229400  9936 ?        S    06:25   0:00  \\_ /usr/sbin/apache2 -k start
www-data 32382  187  0.1 410136 10916 ?        Sl   06:25  16:31  \\_ /usr/sbin/apache2 -k start
www-data 32410  0.0  0.1 229400  9936 ?        S    06:25   0:00  \\_ /usr/sbin/apache2 -k start
www-data 32545  0.0  0.1 229400  9936 ?        S    06:25   0:00  \\_ /usr/sbin/apache2 -k start
www-data  2106  0.0  0.1 3032348 11396 ?       Sl   06:27   0:00  \\_ /usr/sbin/apache2 -k start
www-data  2240  0.0  0.1 2901012 11416 ?       Sl   06:27   0:00  \\_ /usr/sbin/apache2 -k start
www-data  2241  0.0  0.1 2901184 14220 ?       Sl   06:27   0:00  \\_ /usr/sbin/apache2 -k start

Thu Aug  5 06:44:53 CEST 2021
 06:44:53 up 3 days,  8:33,  0 users,  load average: 13.64, 13.25, 9.89
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root     13730  0.0  0.1 225024 15856 ?        Ss   Aug02   0:17 /usr/sbin/apache2 -k start
www-data 10232  0.0  0.1 1352372 10972 ?       Sl   Aug03   0:00  \\_ /usr/sbin/apache2 -k start
www-data 28185  0.0  0.1 762472 10952 ?        Sl   Aug04   0:00  \\_ /usr/sbin/apache2 -k start
www-data 30794  0.0  0.1 228888  9620 ?        S    06:25   0:00  \\_ /usr/sbin/apache2 -k start
www-data 31490  0.0  0.1 344564 10852 ?        Sl   06:25   0:00  \\_ /usr/sbin/apache2 -k start
www-data 32095  0.0  0.1 500228 10888 ?        Sl   06:25   0:00  \\_ /usr/sbin/apache2 -k start
www-data 32215 82.1  0.1 369152 10876 ?        Sl   06:25  15:50  \\_ /usr/sbin/apache2 -k start
www-data 32284  0.0  0.1 229400  9936 ?        S    06:25   0:00  \\_ /usr/sbin/apache2 -k start
www-data 32382  188  0.1 410136 10916 ?        Sl   06:25  36:10  \\_ /usr/sbin/apache2 -k start
www-data 32410  0.0  0.1 229400  9936 ?        S    06:25   0:00  \\_ /usr/sbin/apache2 -k start
www-data 32545  0.0  0.1 229400  9936 ?        S    06:25   0:00  \\_ /usr/sbin/apache2 -k start
www-data  2106  0.0  0.1 3032348 11396 ?       Sl   06:27   0:00  \\_ /usr/sbin/apache2 -k start
www-data  2240  0.0  0.1 2901012 11416 ?       Sl   06:27   0:00  \\_ /usr/sbin/apache2 -k start
www-data  2241  0.0  0.1 2901184 14432 ?       Sl   06:27   0:00  \\_ /usr/sbin/apache2 -k start

请注意,这里有大量派生出的 Apache 进程,其中一个占用了 83% 的 CPU,另一个甚至占用了 188%。显然那里出了问题。

不过,对于这一发现,我目前还不确定该如何处理。

也可能值得检查一下内核最近是否有理由终止任何进程:
dmesg -T | egrep -i kille

我运行该命令后没有任何输出。

我认为问题出在我让朋友把 MPM 从 prefork 切换到了 MPM Event。从那以后,Apache 的资源占用显著增加,不过自从部署 Discourse 以来,没有任何网站崩溃或出现异常。我刚刚又改回了 MPM prefork。或许这能解决问题?

我发现 Java 实际上是 Varnish 缓存。我查看了该进程的树状结构,发现其基础是 Varnish,这比 Java 合理多了 :blush:

1 个赞

MPM Event 允许 Apache 运行多线程,从而服务更多访客。它需要与一个独立的 PHP 服务(如今通常是 PHP-FPM)配合使用。如果服务器繁忙,Prefork 会限制 Apache 和 PHP 的性能。如果 Event 资源占用过高,通常是因为 FPM 配置不当而非 Event 本身的问题,但未优化的 MPM Event 也可能成为问题。

对于没有大量并发流量的服务器,Prefork 运行良好,但如今 Apache 的标准配置是 Event/FPM。

我认为这其实是好事……

好的,谢谢您的帮助:slight_smile:

我切换回了 MPM prefork 模式,Apache 已经稳定运行了三天。希望问题就此解决,一切恢复正常。感谢大家提供的帮助:slight_smile:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.