Commit a42d8c84 authored by Stan Hu's avatar Stan Hu

Merge branch 'mk-move-rails-sli-init' into 'master'

Fix startup crash in Puma single mode

See merge request gitlab-org/gitlab!81983
parents 4a78c48c 076ac5bb
...@@ -4,17 +4,7 @@ ...@@ -4,17 +4,7 @@
require ::File.expand_path('../config/environment', __FILE__) require ::File.expand_path('../config/environment', __FILE__)
def master_process?
Prometheus::PidProvider.worker_id == 'puma_master'
end
warmup do |app| warmup do |app|
# The following is necessary to ensure stale Prometheus metrics don't accumulate over time.
# It needs to be done as early as here to ensure metrics files aren't deleted.
# After we hit our app in `warmup`, first metrics and corresponding files already being created,
# for example in `lib/gitlab/metrics/requests_rack_middleware.rb`.
Prometheus::CleanupMultiprocDirService.new.execute if master_process?
client = Rack::MockRequest.new(app) client = Rack::MockRequest.new(app)
client.get('/') client.get('/')
end end
......
...@@ -13,6 +13,24 @@ def prometheus_default_multiproc_dir ...@@ -13,6 +13,24 @@ def prometheus_default_multiproc_dir
end end
end end
def puma_metrics_server_process?
Prometheus::PidProvider.worker_id == 'puma_master'
end
def sidekiq_metrics_server_process?
Gitlab::Runtime.sidekiq? && (!ENV['SIDEKIQ_WORKER_ID'] || ENV['SIDEKIQ_WORKER_ID'] == '0')
end
if puma_metrics_server_process? || sidekiq_metrics_server_process?
# The following is necessary to ensure stale Prometheus metrics don't accumulate over time.
# It needs to be done as early as here to ensure metrics files aren't deleted.
# After we hit our app in `warmup`, first metrics and corresponding files already being created,
# for example in `lib/gitlab/metrics/requests_rack_middleware.rb`.
Prometheus::CleanupMultiprocDirService.new.execute
::Prometheus::Client.reinitialize_on_pid_change(force: true)
end
::Prometheus::Client.configure do |config| ::Prometheus::Client.configure do |config|
config.logger = Gitlab::AppLogger config.logger = Gitlab::AppLogger
...@@ -49,15 +67,9 @@ if Gitlab::Runtime.sidekiq? && (!ENV['SIDEKIQ_WORKER_ID'] || ENV['SIDEKIQ_WORKER ...@@ -49,15 +67,9 @@ if Gitlab::Runtime.sidekiq? && (!ENV['SIDEKIQ_WORKER_ID'] || ENV['SIDEKIQ_WORKER
end end
Gitlab::Cluster::LifecycleEvents.on_master_start do Gitlab::Cluster::LifecycleEvents.on_master_start do
# When running Puma in a Single mode, `on_master_start` and `on_worker_start` are the same.
# Thus, we order these events to run `reinitialize_on_pid_change` with `force: true` first.
::Prometheus::Client.reinitialize_on_pid_change(force: true)
Gitlab::Metrics.gauge(:deployments, 'GitLab Version', {}, :max).set({ version: Gitlab::VERSION, revision: Gitlab.revision }, 1) Gitlab::Metrics.gauge(:deployments, 'GitLab Version', {}, :max).set({ version: Gitlab::VERSION, revision: Gitlab.revision }, 1)
if Gitlab::Runtime.puma? if Gitlab::Runtime.puma?
Gitlab::Metrics::RequestsRackMiddleware.initialize_metrics
Gitlab::Metrics::Samplers::PumaSampler.instance.start Gitlab::Metrics::Samplers::PumaSampler.instance.start
# Starts a metrics server to export metrics from the Puma primary. # Starts a metrics server to export metrics from the Puma primary.
......
...@@ -36,6 +36,10 @@ if Gitlab::Metrics.enabled? && !Rails.env.test? && !(Rails.env.development? && d ...@@ -36,6 +36,10 @@ if Gitlab::Metrics.enabled? && !Rails.env.test? && !(Rails.env.development? && d
config.middleware.use(Gitlab::Metrics::ElasticsearchRackMiddleware) config.middleware.use(Gitlab::Metrics::ElasticsearchRackMiddleware)
end end
if Gitlab::Runtime.puma?
Gitlab::Metrics::RequestsRackMiddleware.initialize_metrics
end
GC::Profiler.enable GC::Profiler.enable
module TrackNewRedisConnections module TrackNewRedisConnections
......
...@@ -70,7 +70,7 @@ module Gitlab ...@@ -70,7 +70,7 @@ module Gitlab
# Hook registration methods (called from initializers) # Hook registration methods (called from initializers)
# #
def on_worker_start(&block) def on_worker_start(&block)
if in_clustered_environment? if in_clustered_puma?
# Defer block execution # Defer block execution
(@worker_start_hooks ||= []) << block (@worker_start_hooks ||= []) << block
else else
...@@ -101,7 +101,7 @@ module Gitlab ...@@ -101,7 +101,7 @@ module Gitlab
end end
def on_master_start(&block) def on_master_start(&block)
if in_clustered_environment? if in_clustered_puma?
on_before_fork(&block) on_before_fork(&block)
else else
on_worker_start(&block) on_worker_start(&block)
...@@ -158,21 +158,8 @@ module Gitlab ...@@ -158,21 +158,8 @@ module Gitlab
end end
end end
def in_clustered_environment?
# Sidekiq doesn't fork
return false if Gitlab::Runtime.sidekiq?
# Puma sometimes forks
return true if in_clustered_puma?
# Default assumption is that we don't fork
false
end
def in_clustered_puma? def in_clustered_puma?
return false unless Gitlab::Runtime.puma? Gitlab::Runtime.puma? && @puma_options && @puma_options[:workers] && @puma_options[:workers] > 0
@puma_options && @puma_options[:workers] && @puma_options[:workers] > 0
end end
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment