debian-mirror-gitlab/lib/gitlab/redis/wrapper.rb

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

219 lines
6.4 KiB
Ruby
Raw Normal View History

2019-02-15 15:39:39 +05:30
# frozen_string_literal: true
2017-09-10 17:25:29 +05:30
# This file should only be used by sub-classes, not directly by any clients of the sub-classes
2021-09-04 01:27:46 +05:30
# Explicitly load parts of ActiveSupport because MailRoom does not load
# Rails.
2017-09-10 17:25:29 +05:30
require 'active_support/core_ext/hash/keys'
require 'active_support/core_ext/module/delegation'
2021-11-18 22:05:49 +05:30
require 'active_support/core_ext/object/blank'
2021-09-04 01:27:46 +05:30
require 'active_support/core_ext/string/inflections'
2017-09-10 17:25:29 +05:30
2021-10-27 15:23:28 +05:30
# Explicitly load Redis::Store::Factory so we can read Redis configuration in
# TestEnv
require 'redis/store/factory'
2017-09-10 17:25:29 +05:30
module Gitlab
module Redis
class Wrapper
class << self
2021-12-11 22:18:48 +05:30
delegate :params, :url, :store, to: :new
2017-09-10 17:25:29 +05:30
def with
2020-04-22 19:07:51 +05:30
pool.with { |redis| yield redis }
end
2021-01-29 00:20:46 +05:30
def version
with { |redis| redis.info['redis_version'] }
end
2020-04-22 19:07:51 +05:30
def pool
2022-01-26 12:08:38 +05:30
@pool ||= ConnectionPool.new(size: pool_size) { redis }
2017-09-10 17:25:29 +05:30
end
def pool_size
# heuristic constant 5 should be a config setting somewhere -- related to CPU count?
size = 5
2020-03-13 15:44:24 +05:30
if Gitlab::Runtime.multi_threaded?
size += Gitlab::Runtime.max_threads
2017-09-10 17:25:29 +05:30
end
2018-03-17 18:26:18 +05:30
2017-09-10 17:25:29 +05:30
size
end
2021-09-04 01:27:46 +05:30
def config_file_path(filename)
path = File.join(rails_root, 'config', filename)
return path if File.file?(path)
2017-09-10 17:25:29 +05:30
end
2021-09-04 01:27:46 +05:30
# We need this local implementation of Rails.root because MailRoom
# doesn't load Rails.
def rails_root
File.expand_path('../../..', __dir__)
2017-09-10 17:25:29 +05:30
end
def config_file_name
2021-09-04 01:27:46 +05:30
[
# Instance specific config sources:
ENV["GITLAB_REDIS_#{store_name.underscore.upcase}_CONFIG_FILE"],
config_file_path("redis.#{store_name.underscore}.yml"),
# The current Redis instance may have been split off from another one
2023-04-23 21:23:45 +05:30
# (e.g. TraceChunks was split off from SharedState).
2021-09-04 01:27:46 +05:30
config_fallback&.config_file_name,
# Global config sources:
2023-04-23 21:23:45 +05:30
ENV['GITLAB_REDIS_CONFIG_FILE']
2021-09-04 01:27:46 +05:30
].compact.first
end
2017-09-10 17:25:29 +05:30
2023-03-17 16:20:25 +05:30
def redis_yml_path
File.join(rails_root, 'config/redis.yml')
end
2021-09-04 01:27:46 +05:30
def store_name
name.demodulize
end
2017-09-10 17:25:29 +05:30
2021-09-04 01:27:46 +05:30
def config_fallback
2017-09-10 17:25:29 +05:30
nil
end
2020-06-23 00:09:42 +05:30
def instrumentation_class
2021-11-18 22:05:49 +05:30
return unless defined?(::Gitlab::Instrumentation::Redis)
2021-09-04 01:27:46 +05:30
"::Gitlab::Instrumentation::Redis::#{store_name}".constantize
2020-06-23 00:09:42 +05:30
end
2022-01-26 12:08:38 +05:30
private
def redis
::Redis.new(params)
end
2017-09-10 17:25:29 +05:30
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
2021-11-18 22:05:49 +05:30
def db
redis_store_options[:db]
end
2017-09-10 17:25:29 +05:30
def sentinels
raw_config_hash[:sentinels]
end
def sentinels?
sentinels && !sentinels.empty?
end
2021-12-11 22:18:48 +05:30
def store(extras = {})
::Redis::Store::Factory.create(redis_store_options.merge(extras))
end
2017-09-10 17:25:29 +05:30
private
def redis_store_options
config = raw_config_hash
2023-03-04 22:38:38 +05:30
config[:instrumentation_class] ||= self.class.instrumentation_class
if config[:cluster].present?
config[:db] = 0 # Redis Cluster only supports db 0
config
else
parse_redis_url(config)
end
end
def parse_redis_url(config)
2017-09-10 17:25:29 +05:30
redis_url = config.delete(:url)
redis_uri = URI.parse(redis_url)
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-03-17 18:26:18 +05:30
2017-09-10 17:25:29 +05:30
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
2021-11-18 22:05:49 +05:30
config_hash =
if config_data
config_data.is_a?(String) ? { url: config_data } : config_data.deep_symbolize_keys
else
{ url: '' }
end
2023-03-04 22:38:38 +05:30
if config_hash[:url].blank? && config_hash[:cluster].blank?
2021-11-18 22:05:49 +05:30
config_hash[:url] = legacy_fallback_urls[self.class.store_name] || legacy_fallback_urls[self.class.config_fallback.store_name]
2017-09-10 17:25:29 +05:30
end
2021-11-18 22:05:49 +05:30
config_hash
end
# These URLs were defined for cache, queues, and shared_state in
# code. They are used only when no config file exists at all for a
# given instance. The configuration does not seem particularly
# useful - it uses different ports on localhost - but we cannot
# confidently delete it as we don't know if any instances rely on
# this.
#
# DO NOT ADD new instances here. All new instances should define a
# `.config_fallback`, which will then be used to look up this URL.
def legacy_fallback_urls
{
'Cache' => 'redis://localhost:6380',
'Queues' => 'redis://localhost:6381',
'SharedState' => 'redis://localhost:6382'
}
2017-09-10 17:25:29 +05:30
end
def fetch_config
2023-03-17 16:20:25 +05:30
redis_yml = read_yaml(self.class.redis_yml_path).fetch(@rails_env, {})
instance_config_yml = read_yaml(self.class.config_file_name)[@rails_env]
2023-04-23 21:23:45 +05:30
resque_yml = read_yaml(self.class.config_file_path('resque.yml'))[@rails_env]
2023-03-17 16:20:25 +05:30
[
redis_yml[self.class.store_name.underscore],
2023-04-23 21:23:45 +05:30
# There are installations out there where the lowest priority config source (resque.yml) contains bogus
# values. In those cases, the configuration should be read for the instance we originated from (the
# "config_fallback"), either from its specific config file or from redis.yml, before falling back to
# resque.yml.
2023-03-17 16:20:25 +05:30
instance_config_yml,
2023-04-23 21:23:45 +05:30
self.class.config_fallback && redis_yml[self.class.config_fallback.store_name.underscore],
resque_yml
2023-03-17 16:20:25 +05:30
].compact.first
end
2017-09-10 17:25:29 +05:30
2023-03-17 16:20:25 +05:30
def read_yaml(path)
YAML.safe_load(ERB.new(File.read(path.to_s)).result, aliases: true) || {}
rescue Errno::ENOENT
{}
2017-09-10 17:25:29 +05:30
end
end
end
end