2018-11-19 21:01:13 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-07-10 23:35:47 -04:00
|
|
|
# This file should only be used by sub-classes, not directly by any clients of the sub-classes
|
2021-06-01 14:10:04 -04:00
|
|
|
|
|
|
|
# Explicitly load parts of ActiveSupport because MailRoom does not load
|
|
|
|
# Rails.
|
2017-07-10 23:35:47 -04:00
|
|
|
require 'active_support/core_ext/hash/keys'
|
|
|
|
require 'active_support/core_ext/module/delegation'
|
2021-06-01 14:10:04 -04:00
|
|
|
require 'active_support/core_ext/string/inflections'
|
2017-07-10 23:35:47 -04:00
|
|
|
|
2021-07-22 17:09:40 -04:00
|
|
|
# Explicitly load Redis::Store::Factory so we can read Redis configuration in
|
|
|
|
# TestEnv
|
|
|
|
require 'redis/store/factory'
|
|
|
|
|
2017-07-10 23:35:47 -04:00
|
|
|
module Gitlab
|
|
|
|
module Redis
|
|
|
|
class Wrapper
|
|
|
|
class << self
|
|
|
|
delegate :params, :url, to: :new
|
|
|
|
|
|
|
|
def with
|
2020-03-26 17:07:52 -04:00
|
|
|
pool.with { |redis| yield redis }
|
|
|
|
end
|
|
|
|
|
2020-11-12 16:09:19 -05:00
|
|
|
def version
|
|
|
|
with { |redis| redis.info['redis_version'] }
|
|
|
|
end
|
|
|
|
|
2020-03-26 17:07:52 -04:00
|
|
|
def pool
|
2017-07-10 23:35:47 -04:00
|
|
|
@pool ||= ConnectionPool.new(size: pool_size) { ::Redis.new(params) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def pool_size
|
|
|
|
# heuristic constant 5 should be a config setting somewhere -- related to CPU count?
|
|
|
|
size = 5
|
2020-01-16 13:08:46 -05:00
|
|
|
if Gitlab::Runtime.multi_threaded?
|
|
|
|
size += Gitlab::Runtime.max_threads
|
2017-07-10 23:35:47 -04:00
|
|
|
end
|
2018-01-11 11:34:01 -05:00
|
|
|
|
2017-07-10 23:35:47 -04:00
|
|
|
size
|
|
|
|
end
|
|
|
|
|
|
|
|
def _raw_config
|
|
|
|
return @_raw_config if defined?(@_raw_config)
|
|
|
|
|
2017-07-11 13:38:27 -04:00
|
|
|
@_raw_config =
|
|
|
|
begin
|
|
|
|
if filename = config_file_name
|
|
|
|
ERB.new(File.read(filename)).result.freeze
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
rescue Errno::ENOENT
|
|
|
|
false
|
|
|
|
end
|
2017-07-10 23:35:47 -04:00
|
|
|
end
|
|
|
|
|
2017-08-15 15:08:56 -04:00
|
|
|
def config_file_path(filename)
|
2021-06-01 14:10:04 -04:00
|
|
|
path = File.join(rails_root, 'config', filename)
|
|
|
|
return path if File.file?(path)
|
2017-08-15 15:08:56 -04:00
|
|
|
end
|
|
|
|
|
2021-06-01 14:10:04 -04:00
|
|
|
# We need this local implementation of Rails.root because MailRoom
|
|
|
|
# doesn't load Rails.
|
|
|
|
def rails_root
|
|
|
|
File.expand_path('../../..', __dir__)
|
|
|
|
end
|
2017-07-10 23:35:47 -04:00
|
|
|
|
2021-06-01 14:10:04 -04:00
|
|
|
def config_file_name
|
|
|
|
[
|
2021-06-09 02:10:29 -04:00
|
|
|
# Instance specific config sources:
|
2021-06-01 14:10:04 -04:00
|
|
|
ENV["GITLAB_REDIS_#{store_name.underscore.upcase}_CONFIG_FILE"],
|
|
|
|
config_file_path("redis.#{store_name.underscore}.yml"),
|
2021-06-09 02:10:29 -04:00
|
|
|
|
|
|
|
# The current Redis instance may have been split off from another one
|
|
|
|
# (e.g. TraceChunks was split off from SharedState). There are
|
|
|
|
# installations out there where the lowest priority config source
|
|
|
|
# (resque.yml) contains bogus values. In those cases, config_file_name
|
|
|
|
# should resolve to the instance we originated from (the
|
|
|
|
# "config_fallback") rather than resque.yml.
|
|
|
|
config_fallback&.config_file_name,
|
|
|
|
|
|
|
|
# Global config sources:
|
2021-06-01 14:10:04 -04:00
|
|
|
ENV['GITLAB_REDIS_CONFIG_FILE'],
|
|
|
|
config_file_path('resque.yml')
|
|
|
|
].compact.first
|
|
|
|
end
|
2017-07-10 23:35:47 -04:00
|
|
|
|
2021-06-01 14:10:04 -04:00
|
|
|
def store_name
|
|
|
|
name.demodulize
|
2017-07-10 23:35:47 -04:00
|
|
|
end
|
2020-06-04 08:08:21 -04:00
|
|
|
|
2021-06-09 02:10:29 -04:00
|
|
|
def config_fallback
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2020-06-04 08:08:21 -04:00
|
|
|
def instrumentation_class
|
2021-06-01 14:10:04 -04:00
|
|
|
"::Gitlab::Instrumentation::Redis::#{store_name}".constantize
|
2020-06-04 08:08:21 -04:00
|
|
|
end
|
2017-07-10 23:35:47 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(rails_env = nil)
|
|
|
|
@rails_env = rails_env || ::Rails.env
|
|
|
|
end
|
|
|
|
|
|
|
|
def params
|
|
|
|
redis_store_options
|
|
|
|
end
|
|
|
|
|
|
|
|
def url
|
|
|
|
raw_config_hash[:url]
|
|
|
|
end
|
|
|
|
|
|
|
|
def sentinels
|
|
|
|
raw_config_hash[:sentinels]
|
|
|
|
end
|
|
|
|
|
|
|
|
def sentinels?
|
|
|
|
sentinels && !sentinels.empty?
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def redis_store_options
|
|
|
|
config = raw_config_hash
|
|
|
|
redis_url = config.delete(:url)
|
|
|
|
redis_uri = URI.parse(redis_url)
|
|
|
|
|
2020-06-04 08:08:21 -04:00
|
|
|
config[:instrumentation_class] ||= self.class.instrumentation_class
|
2020-05-21 05:08:04 -04:00
|
|
|
|
2017-07-10 23:35:47 -04:00
|
|
|
if redis_uri.scheme == 'unix'
|
|
|
|
# Redis::Store does not handle Unix sockets well, so let's do it for them
|
|
|
|
config[:path] = redis_uri.path
|
|
|
|
query = redis_uri.query
|
|
|
|
unless query.nil?
|
|
|
|
queries = CGI.parse(redis_uri.query)
|
|
|
|
db_numbers = queries["db"] if queries.key?("db")
|
|
|
|
config[:db] = db_numbers[0].to_i if db_numbers.any?
|
|
|
|
end
|
2018-01-11 11:34:01 -05:00
|
|
|
|
2017-07-10 23:35:47 -04:00
|
|
|
config
|
|
|
|
else
|
|
|
|
redis_hash = ::Redis::Store::Factory.extract_host_options_from_uri(redis_url)
|
|
|
|
# order is important here, sentinels must be after the connection keys.
|
|
|
|
# {url: ..., port: ..., sentinels: [...]}
|
|
|
|
redis_hash.merge(config)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def raw_config_hash
|
|
|
|
config_data = fetch_config
|
|
|
|
|
|
|
|
if config_data
|
|
|
|
config_data.is_a?(String) ? { url: config_data } : config_data.deep_symbolize_keys
|
2021-06-09 02:10:29 -04:00
|
|
|
else
|
|
|
|
{ url: '' }
|
2017-07-10 23:35:47 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def fetch_config
|
2017-07-11 13:38:27 -04:00
|
|
|
return false unless self.class._raw_config
|
|
|
|
|
2021-04-26 08:09:44 -04:00
|
|
|
yaml = YAML.safe_load(self.class._raw_config, aliases: true)
|
2017-07-11 13:38:27 -04:00
|
|
|
|
|
|
|
# If the file has content but it's invalid YAML, `load` returns false
|
|
|
|
if yaml
|
|
|
|
yaml.fetch(@rails_env, false)
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
2017-07-10 23:35:47 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|