【问题标题】:Unicorn Memory Usage filling up almost all the RAM独角兽内存使用几乎填满了所有 RAM
【发布时间】:2013-08-12 01:58:57
【问题描述】:

这里基本上有3个问题:

1) Unicorn 似乎正在稳步填满所有 RAM,导致我手动移除工作人员。

2) Unicorn 似乎出于某种原因产生了额外的工人,尽管我已经指定了固定数量的工人(其中 7 个)。这部分导致 RAM 堆积,这也导致我手动移除工作人员。

3) 就我而言,零停机部署是不可靠的。有时它会接收更改,有时我会收到网关超时。每次部署都会变得非常紧张。

我不太喜欢使用 Monit,因为它会在没有等待工人完成他们的请求的情况下杀死工人。

那么,这正常吗?使用 Unicorn 进行部署的其他人是否也有同样的问题,即 RAM 无法控制地增长?

还有哪些工人产生的工人数量与定义的工人数量不匹配?

另一种选择是独角兽杀手,我会在阅读 Unicorn Eating Memory 后尝试一下。

小更新:

因此,New Relic 告诉我内存几乎达到了 95%。所以我不得不杀了一个工人。有趣的是,杀死该工人会导致内存下降很多,如下图所示。

这是怎么回事?

作为参考,这是我的unicorn.rbunicorn_init.sh。希望有人告诉我那里的某个地方有错误。

unicorn.rb

root = "/home/deployer/apps/myapp/current"
working_directory root
pid "#{root}/tmp/pids/unicorn.pid"
stderr_path "#{root}/log/unicorn.stderr.log"
stdout_path "#{root}/log/unicorn.log"

listen "/tmp/unicorn.myapp.sock"
worker_processes 7
timeout 30

preload_app true

before_exec do |_|
  ENV["BUNDLE_GEMFILE"] = '/home/deployer/apps/myapp/current/Gemfile'
end

before_fork do |server, worker|
  # Disconnect since the database connection will not carry over
  if defined? ActiveRecord::Base
    ActiveRecord::Base.connection.disconnect!
  end

  old_pid = "#{root}/tmp/pids/unicorn.pid.oldbin`"
  if old_pid != server.pid
    begin
      sig = (worker.nr + 1) >= server.worker_processes ? :QUIT : :TTOU
      Process.kill(sig, File.read(old_pid).to_i)
    rescue Errno::ENOENT, Errno::ESRCH
    end
  end
  sleep 1
end

after_fork do |server, worker|
  # Start up the database connection again in the worker
  if defined?(ActiveRecord::Base)
    ActiveRecord::Base.establish_connection
  end

  Redis.current.quit
  Rails.cache.reconnect
end

unicorn_init.sh

#!/bin/sh
set -e

# Feel free to change any of the following variables for your app:
TIMEOUT=${TIMEOUT-60}
APP_ROOT=/home/deployer/apps/myapp/current
PID=$APP_ROOT/tmp/pids/unicorn.pid
CMD="cd $APP_ROOT; BUNDLE_GEMFILE=/home/deployer/apps/myapp/current/Gemfile bundle exec unicorn -D -c $APP_ROOT/config/unicorn.rb -E production"
AS_USER=deployer
set -u
OLD_PIN="$PID.oldbin"

sig () {
  test -s "$PID" && kill -$1 `cat $PID`
}

oldsig () {
  test -s $OLD_PIN && kill -$1 `cat $OLD_PIN`
}

run () {
  if [ "$(id -un)" = "$AS_USER" ]; then
    eval $1
  else
    su -c "$1" - $AS_USER
  fi
}

case "$1" in
start)
  sig 0 && echo >&2 "Already running" && exit 0
  run "$CMD"
  ;;
stop)
  sig QUIT && exit 0
  echo >&2 "Not running"
  ;;
force-stop)
  sig TERM && exit 0
  echo >&2 "Not running"
  ;;
restart|reload)
  sig USR2 && echo reloaded OK && exit 0
  echo >&2 "Couldn't reload, starting '$CMD' instead"
  run "$CMD"
  ;;
upgrade)
  if sig USR2 && sleep 2 && sig 0 && oldsig QUIT
  then
    n=$TIMEOUT
    while test -s $OLD_PIN && test $n -ge 0
    do
      printf '.' && sleep 1 && n=$(( $n - 1 ))
    done
    echo

    if test $n -lt 0 && test -s $OLD_PIN
    then
      echo >&2 "$OLD_PIN still exists after $TIMEOUT seconds"
      exit 1
    fi
    exit 0
  fi
  echo >&2 "Couldn't upgrade, starting '$CMD' instead"
  run "$CMD"
  ;;
reopen-logs)
  sig USR1
  ;;
*)
  echo >&2 "Usage: $0 <start|stop|restart|upgrade|force-stop|reopen-logs>"
  exit 1
  ;;
esac

【问题讨论】:

  • 你是如何生成这个图表的?

标签: ruby-on-rails ruby unicorn


【解决方案1】:

您似乎有两个问题:1)您在优雅重启的协调方面存在错误,导致老独角兽工人和老主人留下来; 2) 您的应用(不是独角兽)正在泄漏内存。

对于前者,查看您的 before_fork 代码,您似乎正在使用来自 the example config 的内存限制方法但是,您在 .oldbin 文件名中有一个错字(一个无关的反引号结束)这意味着您永远不会向旧进程发出信号,因为您无法从不存在的文件中读取 pid。

对于后者,您将不得不进行调查和演练。在您的应用程序中查找随时间累积数据的缓存语义;仔细检查全局变量、类变量和类实例变量的所有使用,它们可以保留从请求到请求的数据引用。运行一些内存配置文件来描述您的内存使用情况。您可以通过在工人增长超过某个上限时杀死工人来缓解内存泄漏; unicorn-worker-killer 让这一切变得简单。

【讨论】:

  • 谢谢!我会立即修复并报告。珍惜时间。
  • @dbenhur 能否进一步解释一下什么是“优雅重启”以及这种内存限制方法?
  • @tulio84z 回复:正常重启,请阅读docs 中的“替换正在运行的独角兽可执行文件的过程”。至于 unicorn_worker_killer 的商业用途,请阅读license
  • @dbenhur 如何解决这个问题:“运行一些内存配置文件来描述您的内存使用情况。”
【解决方案2】:

使用unicorn-worker-killer,这样可以更轻松地杀死消耗大量 RAM 的工人 :)

【讨论】:

    猜你喜欢
    • 2013-01-26
    • 2015-09-05
    • 2020-08-09
    • 1970-01-01
    • 2019-04-04
    • 2014-01-15
    • 2012-01-05
    • 2014-05-22
    • 1970-01-01
    相关资源
    最近更新 更多