diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2021-06-04 15:10:17 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2021-06-04 15:10:17 +0300 |
commit | c80a1141e306596202f694b101bfb1aab1864de9 (patch) | |
tree | 46aaee47523ecd57fa6396dae224c3f1cc4079eb /lib | |
parent | 57f8f3552ca37f38f19a6520737ae1ce0009efb3 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'lib')
-rw-r--r-- | lib/gitlab/checks/matching_merge_request.rb | 64 | ||||
-rw-r--r-- | lib/gitlab/redis/cache.rb | 8 | ||||
-rw-r--r-- | lib/gitlab/redis/queues.rb | 8 | ||||
-rw-r--r-- | lib/gitlab/redis/shared_state.rb | 8 | ||||
-rw-r--r-- | lib/gitlab/redis/wrapper.rb | 6 |
5 files changed, 72 insertions, 22 deletions
diff --git a/lib/gitlab/checks/matching_merge_request.rb b/lib/gitlab/checks/matching_merge_request.rb index 2635ad04770..3c4625577d4 100644 --- a/lib/gitlab/checks/matching_merge_request.rb +++ b/lib/gitlab/checks/matching_merge_request.rb @@ -3,22 +3,78 @@ module Gitlab module Checks class MatchingMergeRequest + TOTAL_METRIC = :gitlab_merge_request_match_total + STALE_METRIC = :gitlab_merge_request_match_stale_secondary + def initialize(newrev, branch_name, project) @newrev = newrev @branch_name = branch_name @project = project end - # rubocop: disable CodeReuse/ActiveRecord def match? + if ::Gitlab::Database::LoadBalancing.enable? + # When a user merges a merge request, the following sequence happens: + # + # 1. Sidekiq: MergeService runs and updates the merge request in a locked state. + # 2. Gitaly: The UserMergeBranch RPC runs. + # 3. Gitaly (gitaly-ruby): This RPC calls the pre-receive hook. + # 4. Rails: This hook makes an API request to /api/v4/internal/allowed. + # 5. Rails: This API check does a SQL query for locked merge + # requests with a matching SHA. + # + # Since steps 1 and 5 will happen on different database + # sessions, replication lag could erroneously cause step 5 to + # report no matching merge requests. To avoid this, we check + # the write location to ensure the replica can make this query. + track_session_metrics do + if ::Feature.enabled?(:load_balancing_atomic_replica, @project, default_enabled: :yaml) + ::Gitlab::Database::LoadBalancing::Sticking.select_valid_host(:project, @project.id) + else + ::Gitlab::Database::LoadBalancing::Sticking.unstick_or_continue_sticking(:project, @project.id) + end + end + end + + # rubocop: disable CodeReuse/ActiveRecord @project.merge_requests .with_state(:locked) .where(in_progress_merge_commit_sha: @newrev, target_branch: @branch_name) .exists? + # rubocop: enable CodeReuse/ActiveRecord + end + + private + + def track_session_metrics + before = ::Gitlab::Database::LoadBalancing::Session.current.use_primary? + + yield + + after = ::Gitlab::Database::LoadBalancing::Session.current.use_primary? + + increment_attempt_count + + if !before && after + increment_stale_secondary_count + end + end + + def increment_attempt_count + total_counter.increment + end + + def increment_stale_secondary_count + stale_counter.increment + end + + def total_counter + @total_counter ||= ::Gitlab::Metrics.counter(TOTAL_METRIC, 'Total number of merge request match attempts') + end + + def stale_counter + @stale_counter ||= ::Gitlab::Metrics.counter(STALE_METRIC, 'Total number of merge request match attempts with lagging secondary') end - # rubocop: enable CodeReuse/ActiveRecord end end end - -Gitlab::Checks::MatchingMergeRequest.prepend_mod_with('Gitlab::Checks::MatchingMergeRequest') diff --git a/lib/gitlab/redis/cache.rb b/lib/gitlab/redis/cache.rb index cf4dbe48643..e923cee0292 100644 --- a/lib/gitlab/redis/cache.rb +++ b/lib/gitlab/redis/cache.rb @@ -5,10 +5,10 @@ module Gitlab class Cache < ::Gitlab::Redis::Wrapper CACHE_NAMESPACE = 'cache:gitlab' - class << self - def default_url - 'redis://localhost:6380' - end + private + + def raw_config_hash + super || { url: 'redis://localhost:6380' } end end end diff --git a/lib/gitlab/redis/queues.rb b/lib/gitlab/redis/queues.rb index a0777510cd5..61cb79f8357 100644 --- a/lib/gitlab/redis/queues.rb +++ b/lib/gitlab/redis/queues.rb @@ -9,10 +9,10 @@ module Gitlab SIDEKIQ_NAMESPACE = 'resque:gitlab' MAILROOM_NAMESPACE = 'mail_room:gitlab' - class << self - def default_url - 'redis://localhost:6381' - end + private + + def raw_config_hash + super || { url: 'redis://localhost:6381' } end end end diff --git a/lib/gitlab/redis/shared_state.rb b/lib/gitlab/redis/shared_state.rb index 8bd831741f3..db51d7d9258 100644 --- a/lib/gitlab/redis/shared_state.rb +++ b/lib/gitlab/redis/shared_state.rb @@ -8,10 +8,10 @@ module Gitlab USER_SESSIONS_LOOKUP_NAMESPACE = 'session:lookup:user:gitlab' IP_SESSIONS_LOOKUP_NAMESPACE = 'session:lookup:ip:gitlab2' - class << self - def default_url - 'redis://localhost:6382' - end + private + + def raw_config_hash + super || { url: 'redis://localhost:6382' } end end end diff --git a/lib/gitlab/redis/wrapper.rb b/lib/gitlab/redis/wrapper.rb index ea0802ffbdc..32447d39c02 100644 --- a/lib/gitlab/redis/wrapper.rb +++ b/lib/gitlab/redis/wrapper.rb @@ -51,10 +51,6 @@ module Gitlab end end - def default_url - raise NotImplementedError - end - def config_file_path(filename) path = File.join(rails_root, 'config', filename) return path if File.file?(path) @@ -137,8 +133,6 @@ module Gitlab if config_data config_data.is_a?(String) ? { url: config_data } : config_data.deep_symbolize_keys - else - { url: self.class.default_url } end end |