2018-10-22 03:00:50 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-29 22:10:41 -04:00
|
|
|
module Gitlab
|
2020-02-24 07:09:00 -05:00
|
|
|
# Reference Counter
|
|
|
|
#
|
|
|
|
# A reference counter is used as a mechanism to identify when
|
|
|
|
# a repository is being accessed by a writable operation.
|
|
|
|
#
|
|
|
|
# Maintenance operations would use this as a clue to when it should
|
|
|
|
# execute significant changes in order to avoid disrupting running traffic
|
2017-08-29 22:10:41 -04:00
|
|
|
class ReferenceCounter
|
|
|
|
REFERENCE_EXPIRE_TIME = 600
|
|
|
|
|
|
|
|
attr_reader :gl_repository, :key
|
|
|
|
|
2020-02-24 07:09:00 -05:00
|
|
|
# Reference Counter instance
|
|
|
|
#
|
|
|
|
# @example
|
|
|
|
# Gitlab::ReferenceCounter.new('project-1')
|
|
|
|
#
|
|
|
|
# @see Gitlab::GlRepository::RepoType.identifier_for_repositorable
|
|
|
|
# @param [String] gl_repository repository identifier
|
2017-08-29 22:10:41 -04:00
|
|
|
def initialize(gl_repository)
|
|
|
|
@gl_repository = gl_repository
|
|
|
|
@key = "git-receive-pack-reference-counter:#{gl_repository}"
|
|
|
|
end
|
|
|
|
|
2020-02-24 07:09:00 -05:00
|
|
|
# Return the actual counter value
|
|
|
|
#
|
|
|
|
# @return [Integer] value
|
2017-08-29 22:10:41 -04:00
|
|
|
def value
|
2020-02-24 07:09:00 -05:00
|
|
|
Gitlab::Redis::SharedState.with do |redis|
|
|
|
|
(redis.get(key) || 0).to_i
|
|
|
|
end
|
2017-08-29 22:10:41 -04:00
|
|
|
end
|
|
|
|
|
2020-02-24 07:09:00 -05:00
|
|
|
# Increase the counter
|
|
|
|
#
|
|
|
|
# @return [Boolean] whether operation was a success
|
2017-08-29 22:10:41 -04:00
|
|
|
def increase
|
|
|
|
redis_cmd do |redis|
|
|
|
|
redis.incr(key)
|
|
|
|
redis.expire(key, REFERENCE_EXPIRE_TIME)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-02-24 07:09:00 -05:00
|
|
|
# Decrease the counter
|
|
|
|
#
|
|
|
|
# @return [Boolean] whether operation was a success
|
2017-08-29 22:10:41 -04:00
|
|
|
def decrease
|
|
|
|
redis_cmd do |redis|
|
|
|
|
current_value = redis.decr(key)
|
|
|
|
if current_value < 0
|
2020-09-09 08:08:22 -04:00
|
|
|
Gitlab::AppLogger.warn("Reference counter for #{gl_repository} decreased" \
|
2020-02-24 07:09:00 -05:00
|
|
|
" when its value was less than 1. Resetting the counter.")
|
2017-08-29 22:10:41 -04:00
|
|
|
redis.del(key)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-02-24 07:09:00 -05:00
|
|
|
|
|
|
|
# Reset the reference counter
|
|
|
|
#
|
|
|
|
# @private Used internally by SRE and debugging purpose
|
|
|
|
# @return [Boolean] whether reset was a success
|
|
|
|
def reset!
|
|
|
|
redis_cmd do |redis|
|
|
|
|
redis.del(key)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# When the reference counter would expire
|
|
|
|
#
|
|
|
|
# @api private Used internally by SRE and debugging purpose
|
|
|
|
# @return [Integer] Number in seconds until expiration or false if never
|
|
|
|
def expires_in
|
|
|
|
Gitlab::Redis::SharedState.with do |redis|
|
|
|
|
redis.ttl(key)
|
|
|
|
end
|
|
|
|
end
|
2017-08-29 22:10:41 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def redis_cmd
|
|
|
|
Gitlab::Redis::SharedState.with { |redis| yield(redis) }
|
2020-02-24 07:09:00 -05:00
|
|
|
|
2017-08-29 22:10:41 -04:00
|
|
|
true
|
2021-04-26 08:09:44 -04:00
|
|
|
rescue StandardError => e
|
2020-09-09 08:08:22 -04:00
|
|
|
Gitlab::AppLogger.warn("GitLab: An unexpected error occurred in writing to Redis: #{e}")
|
2020-02-24 07:09:00 -05:00
|
|
|
|
2017-08-29 22:10:41 -04:00
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|