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

182 lines
5.8 KiB
Ruby
Raw Normal View History

2018-12-13 13:39:08 +05:30
# frozen_string_literal: true
2018-03-27 19:54:05 +05:30
module Gitlab
module RepositoryCacheAdapter
extend ActiveSupport::Concern
2018-12-05 23:21:45 +05:30
include Gitlab::Utils::StrongMemoize
2018-03-27 19:54:05 +05:30
class_methods do
2018-12-05 23:21:45 +05:30
# Caches and strongly memoizes the method.
2018-03-27 19:54:05 +05:30
#
# This only works for methods that do not take any arguments.
2018-12-05 23:21:45 +05:30
#
# name - The name of the method to be cached.
# fallback - A value to fall back to if the repository does not exist, or
# in case of a Git error. Defaults to nil.
def cache_method(name, fallback: nil)
uncached_name = alias_uncached_method(name)
define_method(name) do
cache_method_output(name, fallback: fallback) do
__send__(uncached_name) # rubocop:disable GitlabSecurity/PublicSend
end
end
end
2018-03-27 19:54:05 +05:30
2018-12-05 23:21:45 +05:30
# Caches truthy values from the method. All values are strongly memoized,
# and cached in RequestStore.
#
# Currently only used to cache `exists?` since stale false values are
# particularly troublesome. This can occur, for example, when an NFS mount
# is temporarily down.
#
# This only works for methods that do not take any arguments.
#
# name - The name of the method to be cached.
def cache_method_asymmetrically(name)
uncached_name = alias_uncached_method(name)
2018-03-27 19:54:05 +05:30
define_method(name) do
2018-12-05 23:21:45 +05:30
cache_method_output_asymmetrically(name) do
__send__(uncached_name) # rubocop:disable GitlabSecurity/PublicSend
2018-03-27 19:54:05 +05:30
end
end
end
2018-12-05 23:21:45 +05:30
# Strongly memoizes the method.
#
# This only works for methods that do not take any arguments.
#
# name - The name of the method to be memoized.
# fallback - A value to fall back to if the repository does not exist, or
# in case of a Git error. Defaults to nil. The fallback value
# is not memoized.
def memoize_method(name, fallback: nil)
uncached_name = alias_uncached_method(name)
define_method(name) do
memoize_method_output(name, fallback: fallback) do
__send__(uncached_name) # rubocop:disable GitlabSecurity/PublicSend
end
end
end
# Prepends "_uncached_" to the target method name
#
# Returns the uncached method name
def alias_uncached_method(name)
uncached_name = :"_uncached_#{name}"
alias_method(uncached_name, name)
uncached_name
end
end
# RequestStore-backed RepositoryCache to be used. Should be overridden by
# the including class
def request_store_cache
raise NotImplementedError
2018-03-27 19:54:05 +05:30
end
# RepositoryCache to be used. Should be overridden by the including class
def cache
raise NotImplementedError
end
2018-11-08 19:23:39 +05:30
# List of cached methods. Should be overridden by the including class
def cached_methods
raise NotImplementedError
end
2018-12-05 23:21:45 +05:30
# Caches and strongly memoizes the supplied block.
2018-03-27 19:54:05 +05:30
#
2018-12-05 23:21:45 +05:30
# name - The name of the method to be cached.
# fallback - A value to fall back to if the repository does not exist, or
# in case of a Git error. Defaults to nil.
def cache_method_output(name, fallback: nil, &block)
memoize_method_output(name, fallback: fallback) do
cache.fetch(name, &block)
end
end
# Caches truthy values from the supplied block. All values are strongly
# memoized, and cached in RequestStore.
2018-03-27 19:54:05 +05:30
#
2018-12-05 23:21:45 +05:30
# Currently only used to cache `exists?` since stale false values are
# particularly troublesome. This can occur, for example, when an NFS mount
# is temporarily down.
2018-03-27 19:54:05 +05:30
#
2018-12-05 23:21:45 +05:30
# name - The name of the method to be cached.
def cache_method_output_asymmetrically(name, &block)
memoize_method_output(name) do
request_store_cache.fetch(name) do
cache.fetch_without_caching_false(name, &block)
2018-03-27 19:54:05 +05:30
end
end
end
2018-12-05 23:21:45 +05:30
# Strongly memoizes the supplied block.
#
# name - The name of the method to be memoized.
# fallback - A value to fall back to if the repository does not exist, or
# in case of a Git error. Defaults to nil. The fallback value is
# not memoized.
def memoize_method_output(name, fallback: nil, &block)
no_repository_fallback(name, fallback: fallback) do
strong_memoize(memoizable_name(name), &block)
end
end
# Returns the fallback value if the repository does not exist
def no_repository_fallback(name, fallback: nil, &block)
# Avoid unnecessary gRPC invocations
return fallback if fallback && fallback_early?(name)
yield
rescue Gitlab::Git::Repository::NoRepository
# Even if the `#exists?` check in `fallback_early?` passes, these errors
# might still occur (for example because of a non-existing HEAD). We
# want to gracefully handle this and not memoize anything.
fallback
end
2018-03-27 19:54:05 +05:30
# Expires the caches of a specific set of methods
def expire_method_caches(methods)
2018-12-05 23:21:45 +05:30
methods.each do |name|
unless cached_methods.include?(name.to_sym)
2019-09-30 21:07:59 +05:30
Rails.logger.error "Requested to expire non-existent method '#{name}' for Repository" # rubocop:disable Gitlab/RailsLogger
2018-11-08 19:23:39 +05:30
next
end
2018-12-05 23:21:45 +05:30
cache.expire(name)
2018-03-27 19:54:05 +05:30
2018-12-05 23:21:45 +05:30
clear_memoization(memoizable_name(name))
2018-03-27 19:54:05 +05:30
end
2018-12-05 23:21:45 +05:30
expire_request_store_method_caches(methods)
2018-03-27 19:54:05 +05:30
end
private
2018-12-05 23:21:45 +05:30
def memoizable_name(name)
"#{name.to_s.tr('?!', '')}"
end
def expire_request_store_method_caches(methods)
methods.each do |name|
request_store_cache.expire(name)
end
end
# All cached repository methods depend on the existence of a Git repository,
# so if the repository doesn't exist, we already know not to call it.
def fallback_early?(method_name)
# Avoid infinite loop
return false if method_name == :exists?
!exists?
2018-03-27 19:54:05 +05:30
end
end
end