2

我为我的 Rails 应用程序运行了 4 个 Unicorn 进程,它们耗尽了所有可用的 MySQL 连接,导致它因“连接太多”错误而崩溃。今天我不得不重启我的数据库实例 4 次。=(

流程

$ ps斧头| grep [u]ni
21618 ? sl 0:15 unicorn master -D -c /home/deployer/apps/XXX/shared/config/unicorn.rb -E production                                                           
21632 ? Sl 0:20 unicorn worker[0] -D -c /home/deployer/apps/XXX/shared/config/unicorn.rb -E production                                                        
21636 ? Sl 0:14 独角兽工人[1] -D -c /home/deployer/apps/XXX/shared/config/unicorn.rb -E production                                                        
21640 ? Sl 0:20 独角兽工人[2] -D -c /home/deployer/apps/XXX/shared/config/unicorn.rb -E production                                                        
21645 ? Sl 0:12 独角兽工人[3] -D -c /home/deployer/apps/XXX/shared/config/unicorn.rb -E production  

我的database.yml正在为 ActiveRecord 池设置 22 个连接...

...
生产:
  适配器:mysql2
  编码:utf8
  数据库:xxx
  用户名:xxx
  密码:xxx
  主持人:xxx
  端口:3306
  游泳池:22
...

Unicorn 配置文件如下所示:

工作目录“/home/deployer/apps/XXX/current”
pid "/home/deployer/apps/XXX/shared/pids/unicorn.pid"
stderr_path "/home/deployer/apps/XXX/shared/log/unicorn.log"
stdout_path "/home/deployer/apps/XXX/shared/log/unicorn.log"

听“/tmp/unicorn.XXX.sock”
worker_processes 4
超时 100

preload_app 真

before_fork 做 |服务器,工人|
  # 断开连接,因为数据库连接不会继续
  如果定义?活动记录::基础
    ActiveRecord::Base.connection.disconnect!
  结尾

  # 退出旧的独角兽进程
  old_pid = "#{server.config[:pid]}.oldbin"
  如果 File.exists?(old_pid) && server.pid != old_pid
    开始
      Process.kill("QUIT", File.read(old_pid).to_i)
    拯救 Errno::ENOENT, Errno::ESRCH
      # 其他人为我们完成了我们的工作
    结尾
  结尾
结尾

after_fork 做 |服务器,工人|
  # 在worker中再次启动数据库连接
  如果定义了?(ActiveRecord::Base)
    ActiveRecord::Base.establish_connection
  结尾
  child_pid = server.config[:pid].sub(".pid", ".#{worker.nr}.pid")
  system("echo #{Process.pid} > #{child_pid}")
结尾

如果我们查看 DB 控制台,我们会看到类似这样的内容。他们已经吃掉了大部分的联系。(我现在只有独角兽在运行)在我看来应该有 1 个连接 * 4 个独角兽 = 4 个连接。

mysql> 显示完整的进程列表;
+-----+----------+-------------------------------- ------------------+------------------------+------ ---+--------+-------+------------------------+
| 身份证 | 用户 | 主持人 | 分贝 | 命令 | 时间 | 状态 | 信息 |
+-----+----------+-------------------------------- ------------------+------------------------+------ ---+--------+-------+------------------------+
| 2 | rdsadmin | 本地主机:31383 | 空 | 睡眠 | 9 | | 空 |
| 52 | 水平 | 212.100.140.42:50683 | leveltravel_production | 查询 | 0 | 空 | 显示完整的进程列表 |
| 74 | 水平 | ip-10-55-10-151.eu-west-1.compute.internal:38197 | leveltravel_production | 睡眠 | 5 | | 空 |
| 75 | 水平 | ip-10-55-10-151.eu-west-1.compute.internal:38199 | leveltravel_production | 睡眠 | 8 | | 空 |
| 76 | 水平 | ip-10-55-10-151.eu-west-1.compute.internal:38201 | leveltravel_production | 睡眠 | 8 | | 空 |

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

| 157 | 水平 | ip-10-55-10-151.eu-west-1.compute.internal:38321 | leveltravel_production | 睡眠 | 154 | | 空 |
| 158 | 水平 | ip-10-55-10-151.eu-west-1.compute.internal:38322 | leveltravel_production | 睡眠 | 17 | | 空 |
| 159 | 水平 | ip-10-55-10-151.eu-west-1.compute.internal:38325 | leveltravel_production | 睡眠 | 54 | | 空 |
| 160 | 水平 | ip-10-55-10-151.eu-west-1.compute.internal:38326 | leveltravel_production | 睡眠 | 54 | | 空 |
| 161 | 水平 | ip-10-55-10-151.eu-west-1.compute.internal:38327 | leveltravel_production | 睡眠 | 54 | | 空 |
| 162 | 水平 | ip-10-55-10-151.eu-west-1.compute.internal:38329 | leveltravel_production | 睡眠 | 42 | | 空 |
+-----+----------+-------------------------------- ------------------+------------------------+------ ---+--------+-------+------------------------+
一组 90 行(0.15 秒)

您还可以查看 sidekiq 存储库中的 Issue #503,了解此问题的背景https://github.com/mperham/sidekiq/issues/503

4

1 回答 1

9

您已经运行了 4 个独角兽进程。这是过程,而不是线程。
每个进程在池中有 22 个连接。他们总共有 22*4 = 88 个连接。
如果您想为 4 个工作进程提供 4 个连接,您可以在 database.yml 中设置 pool: 1

于 2012-11-14T20:05:00.767 回答