Unicorn Memory Usage几乎填满了所有RAM

时间:2013-08-10 03:57:26

标签: ruby-on-rails ruby unicorn

New Relic Process snapshot

这里基本上有3个问题:

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

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

3)在我的情况下,零停机部署是不可靠的。有时它会接收更改,有时我会获得网关超时。每次部署都会变得非常紧张。

我真的不喜欢使用Monit,因为它会在不等待工人完成服务请求的情况下杀死工人。

那么,这是正常的吗?使用Unicorn部署的其他人是否存在RAM无法控制地增长的相同问题?

而且,工人产生的工人数量与所定义的工人数量不匹配?

另一个选择是独角兽工人杀手,我会在阅读Unicorn Eating Memory之后尝试。

微小的更新:

enter image description here

因此,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

2 个答案:

答案 0 :(得分:10)

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

对于前者,查看您的before_fork代码,您似乎正在使用the example config中的内存约束方法但是,您在.oldbin文件名中有一个拼写错误(这意味着你永远不会发出旧进程的信号,因为你无法从不存在的文件中读取pid。

对于后者,您将不得不进行调查和钻取。在您的应用程序中查找缓存随时间累积数据的语义;仔细检查所有对globals,class-vars和class-instance-vars的使用,它们可以保留从请求到请求的数据引用。运行一些内存配置文件以表征您的内存使用情况。当工人长大超过某个上限时,可以通过杀死工人来减轻内存泄漏; unicorn-worker-killer让这很容易。

答案 1 :(得分:4)

使用unicorn-worker-killer,这可以更容易地杀死消耗大量RAM的工作人员:)