diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2019-10-16 15:06:17 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2019-10-16 15:06:17 +0000 |
commit | 00c78fb814d7ce00989ac04edd6cdaa3239da284 (patch) | |
tree | f04920f08eb4e481ce27bd1d96862676dff735dc /lib | |
parent | d2ffc30fd583e86d4122bb5061098f4f3ca7b3f1 (diff) | |
download | gitlab-ce-00c78fb814d7ce00989ac04edd6cdaa3239da284.tar.gz |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'lib')
-rw-r--r-- | lib/gitlab/cluster/lifecycle_events.rb | 68 | ||||
-rw-r--r-- | lib/gitlab/cluster/mixins/puma_cluster.rb | 19 | ||||
-rw-r--r-- | lib/gitlab/cluster/mixins/unicorn_http_server.rb | 19 | ||||
-rw-r--r-- | lib/gitlab/metrics/exporter/base_exporter.rb | 7 | ||||
-rw-r--r-- | lib/gitlab/metrics/exporter/web_exporter.rb | 39 | ||||
-rw-r--r-- | lib/gitlab/metrics/samplers/puma_sampler.rb | 2 | ||||
-rw-r--r-- | lib/gitlab/submodule_links.rb | 6 |
7 files changed, 141 insertions, 19 deletions
diff --git a/lib/gitlab/cluster/lifecycle_events.rb b/lib/gitlab/cluster/lifecycle_events.rb index 3fbbccbf56e..294ffad02ce 100644 --- a/lib/gitlab/cluster/lifecycle_events.rb +++ b/lib/gitlab/cluster/lifecycle_events.rb @@ -8,14 +8,50 @@ module Gitlab # watchdog threads. This lets us abstract away the Unix process # lifecycles of Unicorn, Sidekiq, Puma, Puma Cluster, etc. # - # We have three lifecycle events. + # We have the following lifecycle events. # - # - before_fork (only in forking processes) - # In forking processes (Unicorn and Puma in multiprocess mode) this - # will be called exactly once, on startup, before the workers are - # forked. This will be called in the parent process. - # - worker_start - # - before_master_restart (only in forking processes) + # - on_master_start: + # + # Unicorn/Puma Cluster: This will be called exactly once, + # on startup, before the workers are forked. This is + # called in the PARENT/MASTER process. + # + # Sidekiq/Puma Single: This is called immediately. + # + # - on_before_fork: + # + # Unicorn/Puma Cluster: This will be called exactly once, + # on startup, before the workers are forked. This is + # called in the PARENT/MASTER process. + # + # Sidekiq/Puma Single: This is not called. + # + # - on_worker_start: + # + # Unicorn/Puma Cluster: This is called in the worker process + # exactly once before processing requests. + # + # Sidekiq/Puma Single: This is called immediately. + # + # - on_before_phased_restart: + # + # Unicorn/Puma Cluster: This will be called before a graceful + # shutdown of workers starts happening. + # This is called on `master` process. + # + # Sidekiq/Puma Single: This is not called. + # + # - on_before_master_restart: + # + # Unicorn: This will be called before a new master is spun up. + # This is called on forked master before `execve` to become + # a new masterfor Unicorn. This means that this does not really + # affect old master process. + # + # Puma Cluster: This will be called before a new master is spun up. + # This is called on `master` process. + # + # Sidekiq/Puma Single: This is not called. # # Blocks will be executed in the order in which they are registered. # @@ -34,15 +70,17 @@ module Gitlab end def on_before_fork(&block) - return unless in_clustered_environment? - # Defer block execution (@before_fork_hooks ||= []) << block end - def on_before_master_restart(&block) - return unless in_clustered_environment? + # Read the config/initializers/cluster_events_before_phased_restart.rb + def on_before_phased_restart(&block) + # Defer block execution + (@master_phased_restart ||= []) << block + end + def on_before_master_restart(&block) # Defer block execution (@master_restart_hooks ||= []) << block end @@ -70,8 +108,14 @@ module Gitlab end end + def do_before_phased_restart + @master_phased_restart&.each do |block| + block.call + end + end + def do_before_master_restart - @master_restart_hooks && @master_restart_hooks.each do |block| + @master_restart_hooks&.each do |block| block.call end end diff --git a/lib/gitlab/cluster/mixins/puma_cluster.rb b/lib/gitlab/cluster/mixins/puma_cluster.rb new file mode 100644 index 00000000000..e9157d9f1e4 --- /dev/null +++ b/lib/gitlab/cluster/mixins/puma_cluster.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +module Gitlab + module Cluster + module Mixins + module PumaCluster + def self.prepended(base) + raise 'missing method Puma::Cluster#stop_workers' unless base.method_defined?(:stop_workers) + end + + def stop_workers + Gitlab::Cluster::LifecycleEvents.do_before_phased_restart + + super + end + end + end + end +end diff --git a/lib/gitlab/cluster/mixins/unicorn_http_server.rb b/lib/gitlab/cluster/mixins/unicorn_http_server.rb new file mode 100644 index 00000000000..765fd0c2baa --- /dev/null +++ b/lib/gitlab/cluster/mixins/unicorn_http_server.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +module Gitlab + module Cluster + module Mixins + module UnicornHttpServer + def self.prepended(base) + raise 'missing method Unicorn::HttpServer#reexec' unless base.method_defined?(:reexec) + end + + def reexec + Gitlab::Cluster::LifecycleEvents.do_before_phased_restart + + super + end + end + end + end +end diff --git a/lib/gitlab/metrics/exporter/base_exporter.rb b/lib/gitlab/metrics/exporter/base_exporter.rb index 13483e437d8..01d1ec9305e 100644 --- a/lib/gitlab/metrics/exporter/base_exporter.rb +++ b/lib/gitlab/metrics/exporter/base_exporter.rb @@ -54,8 +54,11 @@ module Gitlab if server # we close sockets if thread is not longer running # this happens, when the process forks - server.listeners.each(&:close) unless thread.alive? - server.shutdown + if thread.alive? + server.shutdown + else + server.listeners.each(&:close) + end end @server = nil diff --git a/lib/gitlab/metrics/exporter/web_exporter.rb b/lib/gitlab/metrics/exporter/web_exporter.rb index f4b42759fb9..597ac289193 100644 --- a/lib/gitlab/metrics/exporter/web_exporter.rb +++ b/lib/gitlab/metrics/exporter/web_exporter.rb @@ -7,23 +7,60 @@ module Gitlab module Metrics module Exporter class WebExporter < BaseExporter + ExporterCheck = Struct.new(:exporter) do + def readiness + Gitlab::HealthChecks::Result.new( + 'web_exporter', exporter.running) + end + end + + attr_reader :running + # This exporter is always run on master process def initialize super self.additional_checks = [ + WebExporter::ExporterCheck.new(self), Gitlab::HealthChecks::PumaCheck, Gitlab::HealthChecks::UnicornCheck ] end def settings - Settings.monitoring.web_exporter + Gitlab.config.monitoring.web_exporter end def log_filename File.join(Rails.root, 'log', 'web_exporter.log') end + + private + + def start_working + @running = true + super + end + + def stop_working + @running = false + wait_in_blackout_period if server && thread.alive? + super + end + + def wait_in_blackout_period + return unless blackout_seconds > 0 + + @server.logger.info( + message: 'starting blackout...', + duration_s: blackout_seconds) + + sleep(blackout_seconds) + end + + def blackout_seconds + settings['blackout_seconds'].to_i + end end end end diff --git a/lib/gitlab/metrics/samplers/puma_sampler.rb b/lib/gitlab/metrics/samplers/puma_sampler.rb index 8a24d4f3663..f788f51b1ce 100644 --- a/lib/gitlab/metrics/samplers/puma_sampler.rb +++ b/lib/gitlab/metrics/samplers/puma_sampler.rb @@ -1,7 +1,5 @@ # frozen_string_literal: true -require 'puma/state_file' - module Gitlab module Metrics module Samplers diff --git a/lib/gitlab/submodule_links.rb b/lib/gitlab/submodule_links.rb index 18fd604a3b0..b0ee0877f30 100644 --- a/lib/gitlab/submodule_links.rb +++ b/lib/gitlab/submodule_links.rb @@ -6,6 +6,7 @@ module Gitlab def initialize(repository) @repository = repository + @cache_store = {} end def for(submodule, sha) @@ -18,8 +19,9 @@ module Gitlab attr_reader :repository def submodule_urls_for(sha) - strong_memoize(:"submodule_urls_for_#{sha}") do - repository.submodule_urls_for(sha) + @cache_store.fetch(sha) do + submodule_urls = repository.submodule_urls_for(sha) + @cache_store[sha] = submodule_urls end end |