2018-12-13 13:39:08 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
module Gitlab
|
|
|
|
module Checks
|
|
|
|
class MatchingMergeRequest
|
2021-09-04 01:27:46 +05:30
|
|
|
TOTAL_METRIC = :gitlab_merge_request_match_total
|
|
|
|
STALE_METRIC = :gitlab_merge_request_match_stale_secondary
|
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
def initialize(newrev, branch_name, project)
|
|
|
|
@newrev = newrev
|
|
|
|
@branch_name = branch_name
|
|
|
|
@project = project
|
|
|
|
end
|
|
|
|
|
|
|
|
def match?
|
2021-11-18 22:05:49 +05:30
|
|
|
# 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.
|
2023-06-20 00:43:36 +05:30
|
|
|
# 3. Gitaly: The RPC calls the pre-receive hook.
|
2021-11-18 22:05:49 +05:30
|
|
|
# 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
|
|
|
|
::ApplicationRecord.sticking.select_valid_host(:project, @project.id)
|
2021-09-04 01:27:46 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2016-08-24 12:49:21 +05:30
|
|
|
@project.merge_requests
|
|
|
|
.with_state(:locked)
|
|
|
|
.where(in_progress_merge_commit_sha: @newrev, target_branch: @branch_name)
|
|
|
|
.exists?
|
2021-09-04 01:27:46 +05:30
|
|
|
# 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')
|
2016-08-24 12:49:21 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|