Rails 关于 Rails 自动加载引起的慢请求

woshishui · 发布于 2018年01月20日 · 最后由 woshishui 回复于 2018年01月23日 · 395 次阅读

事情背景:

rails 每过一定时间就会出现较慢的请求(从平均20ms变到100ms左右),经过排查,把问题定位到rails本身,排除了以下问题:

  1. 数据库的问题
  2. active record 的connection pool的问题
  3. GC的问题

观察:

  1. rails服务器重启之后,第一次请求会变慢(所谓的变慢已经排除了外部的资源加载的因素),表现为一个method里面对ActiveReocrd的每一个调用都会慢2-5倍,(从 2ms 变成 10ms)。于是总的时长就由20ms -> 100ms.
  2. 各个方法的变化幅度是一致的: 即偶然出现的变慢请求重启之后的第一次请求,3条不同sql变慢幅度一致(eg: sql A都是从10ms变成40ms,sql B都是从2ms变成13ms).
  3. 我由此推测是因为加载导致的,于是在线下的development模式下运行,发现将代码增加一个空格之后,会导致程序出现类似的变慢,但是第二次请求恢复正常。 我推测这是由于rails发现监视的文件变化了,所以将所有常量的加载记录移除了,这样运行新的代码时,解释器发现未知常量,于是调用const_miss方法去加载,从而变慢。

问题:

  1. 请问production环境中,为什么会出现类似的问题?(即所有操作都变慢一定的幅度)。线上环境并没有实时修改代码,也不会自动加载,但是表现是非常类似的。
  2. 如果存在这样的问题,是否需要解决(因为变慢的概率和幅度都不大,暂时可以接受),是否可以解决(通过修改什么参数或者配置?)

END:

@huacnlee @Rei @luikore @jasl ,感谢各位大佬!

共收到 5 条回复

贴 config/application.rb 和 config/environments/production.rb 的配置

是以前后端留下的配置,没怎么看过

Live::Application.configure do
  # Settings specified here will take precedence over those in config/application.rb

  # Code is not reloaded between requests
  config.cache_classes = true

  # Full error reports are disabled and caching is turned on
  config.consider_all_requests_local       = false
  config.action_controller.perform_caching = true

  # Disable Rails's static asset server (Apache or nginx will already do this)
  config.serve_static_files = true

  # Compress JavaScripts and CSS
  config.assets.compress = true

  # Don't fallback to assets pipeline if a precompiled asset is missed
  config.assets.compile = false

  # Generate digests for assets URLs
  config.assets.digest = true

  # Defaults to Rails.root.join("public/assets")
  # config.assets.manifest = YOUR_PATH

  # Specifies the header that your server uses for sending files
  # config.action_dispatch.x_sendfile_header = "X-Sendfile" # for apache
  # config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for nginx

  # Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
  # config.force_ssl = true

  # See everything in the log (default is :info)
  # config.log_level = :debug

  # Prepend all log lines with the following tags
  # config.log_tags = [ :subdomain, :uuid ]

  # Use a different logger for distributed setups
  # config.logger = ActiveSupport::TaggedLogging.new(SyslogLogger.new)

  # Use a different cache store in production
  # config.cache_store = :mem_cache_store

  #memcached server => dragon, tortoise, phoenix
  #config.cache_store = :dalli_store, '10.168.26.54:11211', "10.168.38.207:11211", "10.171.247.15:11211", {:pool_size => 22, :compress => true, :expires_in => 1.days, :socket_timeout => 0.1}

  config.eager_load = true

  # Enable serving of images, stylesheets, and JavaScripts from an asset server
  config.action_controller.asset_host = "https://live-media-g.bczcdn.com"

  # Precompile additional assets (application.js, application.css, and all non-JS/CSS are already added)
  # config.assets.precompile += %w( search.js )

  # Disable delivery errors, bad email addresses will be ignored
  # config.action_mailer.raise_delivery_errors = false

  # Enable threaded mode
  # config.threadsafe!

  # Enable locale fallbacks for I18n (makes lookups for any locale fall back to
  # the I18n.default_locale when a translation can not be found)
  config.i18n.fallbacks = true

  # Send deprecation notices to registered listeners
  config.active_support.deprecation = :notify

  # Log the query plan for queries taking more than this (works
  # with SQLite, MySQL, and PostgreSQL)
  # config.active_record.auto_explain_threshold_in_seconds = 0.5

  config.log_level = :info

  #lograge
  config.lograge.enabled = true
  config.lograge.formatter = Lograge::Formatters::Logstash.new

  config.lograge.custom_options = lambda do |event|
    event.payload.except(:params, :view_runtime, :db_runtime)
  end

end


# 这里是一大堆普通的require
# 这里是一大堆普通的require
# 这里是一大堆普通的require
# 这里是一大堆普通的require
# 这里是一大堆普通的require
# 这里是一大堆普通的require


require 'rails/all'
require 'erb'
require 'whenever'
require 'resque'
require 'uri'

if defined?(Bundler)
  # If you precompile assets before deploying to production, use this line
  Bundler.require(*Rails.groups(:assets => %w(development test)))
  # If you want your assets lazily compiled in production, use this line
  # Bundler.require(:default, :assets, Rails.env)
end

module Live
  class Application < Rails::Application
    # Settings in config/environments/* take precedence over those specified here.
    # Application configuration should go into files in config/initializers
    # -- all .rb files in that directory are automatically loaded.

    # Custom directories with classes and modules you want to be autoloadable.
    # config.autoload_paths += %W(#{config.root}/extras)
    config.autoload_paths << "#{Rails.root}/app/services"

    # Only load the plugins named here, in the order given (default is alphabetical).
    # :all can be used as a placeholder for all plugins not explicitly named.
    # config.plugins = [ :exception_notification, :ssl_requirement, :all ]

    # Activate observers that should always be running.
    # config.active_record.observers = :cacher, :garbage_collector, :forum_observer

    # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
    # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
    # config.time_zone = 'Central Time (US & Canada)'
    config.time_zone = "Chongqing"
    config.active_record.default_timezone = :local
    config.active_record.time_zone_aware_attributes = false

    # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
    # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
    # config.i18n.default_locale = :de

    # Configure the default encoding used in templates for Ruby 1.9.
    config.encoding = "utf-8"

    # Configure sensitive parameters which will be filtered from the log file.
    config.filter_parameters += [:password]
    config.filter_parameters += [:raw_pwd]
    config.filter_parameters += [:raw_pwd_confirmation]

    # Enable escaping HTML in JSON.
    #config.active_support.escape_html_entities_in_json = true

    # Use SQL instead of Active Record's schema dumper when creating the database.
    # This is necessary if your schema can't be completely dumped by the schema dumper,
    # like if you have constraints or database-specific column types
    # config.active_record.schema_format = :sql

    # Enforce whitelist mode for mass assignment.
    # This will create an empty whitelist of attributes available for mass-assignment for all models
    # in your app. As such, your models will need to explicitly whitelist or blacklist accessible
    # parameters by using an attr_accessible or attr_protected declaration.
    # config.active_record.whitelist_attributes = false

    # Enable the asset pipeline
    config.assets.enabled = true

    # Version of your assets, change this if you want to expire all your assets
    config.assets.version = '1.0'

    # assets file config
    ASSETS_DIR_CONFIG = YAML.load( File.read(Rails.root + 'config' + 'assets_path.yml') )end
end

web容器和相关的监控是啥 看看是否有内存溢出的情况导致原本的worker被kill

zj0713001 回复

rainbows,并不是这个原因,内存占用不大,而且是重启后多刷几次就会出现这个现象(概率在 5%-10%左右)

huacnlee 回复

我贴了配置,大神有没有时间瞅一眼有啥问题呢?

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册