debian-mirror-gitlab/lib/gitlab/optimistic_locking.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

63 lines
1.5 KiB
Ruby
Raw Normal View History

2018-12-13 13:39:08 +05:30
# frozen_string_literal: true
2017-08-17 22:00:37 +05:30
module Gitlab
module OptimisticLocking
2021-04-17 20:07:23 +05:30
MAX_RETRIES = 100
2017-08-17 22:00:37 +05:30
module_function
2021-04-17 20:07:23 +05:30
def retry_lock(subject, max_retries = MAX_RETRIES, name:, &block)
start_time = Gitlab::Metrics::System.monotonic_time
retry_attempts = 0
begin
2021-11-18 22:05:49 +05:30
subject.transaction do
2021-04-17 20:07:23 +05:30
yield(subject)
end
rescue ActiveRecord::StaleObjectError
raise unless retry_attempts < max_retries
subject.reset
2018-10-15 14:42:47 +05:30
2021-04-17 20:07:23 +05:30
retry_attempts += 1
retry
ensure
retry_lock_histogram.observe({}, retry_attempts)
log_optimistic_lock_retries(
name: name,
retry_attempts: retry_attempts,
start_time: start_time)
end
2017-08-17 22:00:37 +05:30
end
alias_method :retry_optimistic_lock, :retry_lock
2021-04-17 20:07:23 +05:30
def log_optimistic_lock_retries(name:, retry_attempts:, start_time:)
return unless retry_attempts > 0
elapsed_time = Gitlab::Metrics::System.monotonic_time - start_time
retry_lock_logger.info(
message: "Optimistic Lock released with retries",
name: name,
retries: retry_attempts,
time_s: elapsed_time)
end
def retry_lock_logger
@retry_lock_logger ||= Gitlab::Services::Logger.build
end
def retry_lock_histogram
@retry_lock_histogram ||=
Gitlab::Metrics.histogram(
:gitlab_optimistic_locking_retries,
'Number of retry attempts to execute optimistic retry lock',
{},
[0, 1, 2, 3, 5, 10, 50]
)
end
2017-08-17 22:00:37 +05:30
end
end