2018-10-22 03:00:50 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-10-20 03:33:44 -04:00
|
|
|
module Gitlab
|
2016-10-27 07:34:09 -04:00
|
|
|
module OptimisticLocking
|
2021-03-02 10:10:57 -05:00
|
|
|
MAX_RETRIES = 100
|
|
|
|
|
2017-02-23 05:44:49 -05:00
|
|
|
module_function
|
2016-10-27 07:34:09 -04:00
|
|
|
|
2021-03-02 10:10:57 -05:00
|
|
|
def retry_lock(subject, max_retries = MAX_RETRIES, name:, &block)
|
|
|
|
start_time = Gitlab::Metrics::System.monotonic_time
|
|
|
|
retry_attempts = 0
|
|
|
|
|
|
|
|
begin
|
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
yield(subject)
|
|
|
|
end
|
|
|
|
rescue ActiveRecord::StaleObjectError
|
|
|
|
raise unless retry_attempts < max_retries
|
2018-04-18 05:19:40 -04:00
|
|
|
|
2021-03-02 10:10:57 -05:00
|
|
|
subject.reset
|
|
|
|
|
|
|
|
retry_attempts += 1
|
|
|
|
retry
|
|
|
|
ensure
|
2021-03-05 07:08:55 -05:00
|
|
|
retry_lock_histogram.observe({}, retry_attempts)
|
2021-03-02 10:10:57 -05:00
|
|
|
|
|
|
|
log_optimistic_lock_retries(
|
|
|
|
name: name,
|
|
|
|
retry_attempts: retry_attempts,
|
2021-03-05 07:08:55 -05:00
|
|
|
start_time: start_time)
|
2021-03-02 10:10:57 -05:00
|
|
|
end
|
2016-10-20 03:33:44 -04:00
|
|
|
end
|
2017-02-23 05:44:49 -05:00
|
|
|
|
2017-02-23 08:56:27 -05:00
|
|
|
alias_method :retry_optimistic_lock, :retry_lock
|
2021-03-02 10:10:57 -05:00
|
|
|
|
2021-03-05 07:08:55 -05:00
|
|
|
def log_optimistic_lock_retries(name:, retry_attempts:, start_time:)
|
2021-03-02 10:10:57 -05:00
|
|
|
return unless retry_attempts > 0
|
|
|
|
|
2021-03-05 07:08:55 -05:00
|
|
|
elapsed_time = Gitlab::Metrics::System.monotonic_time - start_time
|
|
|
|
|
2021-03-02 10:10:57 -05:00
|
|
|
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
|
2021-03-05 07:08:55 -05:00
|
|
|
|
|
|
|
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
|
2016-10-20 03:33:44 -04:00
|
|
|
end
|
|
|
|
end
|