Add redis sentinel support to ruby part of code (#31744)

This commit is contained in:
David Roetzel 2024-09-04 16:10:45 +02:00 committed by GitHub
parent 9ba81eae3e
commit ef2bc8ea26
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 112 additions and 46 deletions

View file

@ -1,34 +1,33 @@
# frozen_string_literal: true # frozen_string_literal: true
class Mastodon::RedisConfiguration class Mastodon::RedisConfiguration
DEFAULTS = {
host: 'localhost',
port: 6379,
db: 0,
}.freeze
def base def base
@base ||= { @base ||= setup_config(prefix: nil, defaults: DEFAULTS)
url: setup_base_redis_url, .merge(namespace: base_namespace)
driver: driver,
namespace: base_namespace,
}
end end
def sidekiq def sidekiq
@sidekiq ||= { @sidekiq ||= setup_config(prefix: 'SIDEKIQ_')
url: setup_prefixed_redis_url(:sidekiq), .merge(namespace: sidekiq_namespace)
driver: driver,
namespace: sidekiq_namespace,
}
end end
def cache def cache
@cache ||= { @cache ||= setup_config(prefix: 'CACHE_')
url: setup_prefixed_redis_url(:cache), .merge({
driver: driver, namespace: cache_namespace,
namespace: cache_namespace, expires_in: 10.minutes,
expires_in: 10.minutes, connect_timeout: 5,
connect_timeout: 5, pool: {
pool: { size: Sidekiq.server? ? Sidekiq[:concurrency] : Integer(ENV['MAX_THREADS'] || 5),
size: Sidekiq.server? ? Sidekiq[:concurrency] : Integer(ENV['MAX_THREADS'] || 5), timeout: 5,
timeout: 5, },
}, })
}
end end
private private
@ -55,42 +54,53 @@ class Mastodon::RedisConfiguration
namespace ? "#{namespace}_cache" : 'cache' namespace ? "#{namespace}_cache" : 'cache'
end end
def setup_base_redis_url def setup_config(prefix: nil, defaults: {})
url = ENV.fetch('REDIS_URL', nil) prefix = "#{prefix}REDIS_"
return url if url.present?
user = ENV.fetch('REDIS_USER', '') url = ENV.fetch("#{prefix}URL", nil)
password = ENV.fetch('REDIS_PASSWORD', '') user = ENV.fetch("#{prefix}USER", nil)
host = ENV.fetch('REDIS_HOST', 'localhost') password = ENV.fetch("#{prefix}PASSWORD", nil)
port = ENV.fetch('REDIS_PORT', 6379) host = ENV.fetch("#{prefix}HOST", defaults[:host])
db = ENV.fetch('REDIS_DB', 0) port = ENV.fetch("#{prefix}PORT", defaults[:port])
db = ENV.fetch("#{prefix}DB", defaults[:db])
name = ENV.fetch("#{prefix}SENTINEL_MASTER", nil)
sentinels = parse_sentinels(ENV.fetch("#{prefix}SENTINELS", nil))
construct_uri(host, port, db, user, password) return { url:, driver: } if url
end
def setup_prefixed_redis_url(prefix) if name.present? && sentinels.present?
prefix = "#{prefix.to_s.upcase}_" host = name
url = ENV.fetch("#{prefix}REDIS_URL", nil) port = nil
db ||= 0
return url if url.present?
user = ENV.fetch("#{prefix}REDIS_USER", nil)
password = ENV.fetch("#{prefix}REDIS_PASSWORD", nil)
host = ENV.fetch("#{prefix}REDIS_HOST", nil)
port = ENV.fetch("#{prefix}REDIS_PORT", nil)
db = ENV.fetch("#{prefix}REDIS_DB", nil)
if host.nil?
base[:url]
else else
construct_uri(host, port, db, user, password) sentinels = nil
end
url = construct_uri(host, port, db, user, password)
if url.present?
{ url:, driver:, name:, sentinels: }
else
# Fall back to base config. This has defaults for the URL
# so this cannot lead to an endless loop.
base
end end
end end
def construct_uri(host, port, db, user, password) def construct_uri(host, port, db, user, password)
return nil if host.blank?
Addressable::URI.parse("redis://#{host}:#{port}/#{db}").tap do |uri| Addressable::URI.parse("redis://#{host}:#{port}/#{db}").tap do |uri|
uri.user = user if user.present? uri.user = user if user.present?
uri.password = password if password.present? uri.password = password if password.present?
end.normalize.to_str end.normalize.to_str
end end
def parse_sentinels(sentinels_string)
(sentinels_string || '').split(',').map do |sentinel|
host, port = sentinel.split(':')
port = port.present? ? port.to_i : 26_379
{ host: host, port: port }
end.presence
end
end end

View file

@ -45,6 +45,20 @@ RSpec.describe Mastodon::RedisConfiguration do
it 'uses the url from the base config' do it 'uses the url from the base config' do
expect(subject[:url]).to eq 'redis://localhost:6379/0' expect(subject[:url]).to eq 'redis://localhost:6379/0'
end end
context 'when the base config uses sentinel' do
around do |example|
ClimateControl.modify REDIS_SENTINELS: '192.168.0.1:3000,192.168.0.2:4000', REDIS_SENTINEL_MASTER: 'mainsentinel' do
example.run
end
end
it 'uses the sentinel configuration from base config' do
expect(subject[:url]).to eq 'redis://mainsentinel/0'
expect(subject[:name]).to eq 'mainsentinel'
expect(subject[:sentinels]).to contain_exactly({ host: '192.168.0.1', port: 3000 }, { host: '192.168.0.2', port: 4000 })
end
end
end end
context "when the `#{prefix}_REDIS_URL` environment variable is present" do context "when the `#{prefix}_REDIS_URL` environment variable is present" do
@ -72,6 +86,39 @@ RSpec.describe Mastodon::RedisConfiguration do
end end
end end
shared_examples 'sentinel support' do |prefix = nil|
prefix = prefix ? "#{prefix}_" : ''
context 'when configuring sentinel support' do
around do |example|
ClimateControl.modify "#{prefix}REDIS_PASSWORD": 'testpass1', "#{prefix}REDIS_HOST": 'redis2.example.com', "#{prefix}REDIS_SENTINELS": '192.168.0.1:3000,192.168.0.2:4000', "#{prefix}REDIS_SENTINEL_MASTER": 'mainsentinel' do
example.run
end
end
it 'constructs the url using the sentinel master name' do
expect(subject[:url]).to eq 'redis://:testpass1@mainsentinel/0'
end
it 'includes the sentinel master name and list of sentinels' do
expect(subject[:name]).to eq 'mainsentinel'
expect(subject[:sentinels]).to contain_exactly({ host: '192.168.0.1', port: 3000 }, { host: '192.168.0.2', port: 4000 })
end
end
context 'when giving sentinels without port numbers' do
around do |example|
ClimateControl.modify "#{prefix}REDIS_SENTINELS": '192.168.0.1,192.168.0.2', "#{prefix}REDIS_SENTINEL_MASTER": 'mainsentinel' do
example.run
end
end
it 'uses the default sentinel port' do
expect(subject[:sentinels]).to contain_exactly({ host: '192.168.0.1', port: 26_379 }, { host: '192.168.0.2', port: 26_379 })
end
end
end
describe '#base' do describe '#base' do
subject { redis_environment.base } subject { redis_environment.base }
@ -81,6 +128,8 @@ RSpec.describe Mastodon::RedisConfiguration do
url: 'redis://localhost:6379/0', url: 'redis://localhost:6379/0',
driver: :hiredis, driver: :hiredis,
namespace: nil, namespace: nil,
name: nil,
sentinels: nil,
}) })
end end
end end
@ -113,12 +162,15 @@ RSpec.describe Mastodon::RedisConfiguration do
url: 'redis://:testpass@redis.example.com:3333/3', url: 'redis://:testpass@redis.example.com:3333/3',
driver: :hiredis, driver: :hiredis,
namespace: nil, namespace: nil,
name: nil,
sentinels: nil,
}) })
end end
end end
include_examples 'setting a different driver' include_examples 'setting a different driver'
include_examples 'setting a namespace' include_examples 'setting a namespace'
include_examples 'sentinel support'
end end
describe '#sidekiq' do describe '#sidekiq' do
@ -127,6 +179,7 @@ RSpec.describe Mastodon::RedisConfiguration do
include_examples 'secondary configuration', 'SIDEKIQ' include_examples 'secondary configuration', 'SIDEKIQ'
include_examples 'setting a different driver' include_examples 'setting a different driver'
include_examples 'setting a namespace' include_examples 'setting a namespace'
include_examples 'sentinel support', 'SIDEKIQ'
end end
describe '#cache' do describe '#cache' do
@ -139,6 +192,8 @@ RSpec.describe Mastodon::RedisConfiguration do
namespace: 'cache', namespace: 'cache',
expires_in: 10.minutes, expires_in: 10.minutes,
connect_timeout: 5, connect_timeout: 5,
name: nil,
sentinels: nil,
pool: { pool: {
size: 5, size: 5,
timeout: 5, timeout: 5,
@ -166,5 +221,6 @@ RSpec.describe Mastodon::RedisConfiguration do
include_examples 'secondary configuration', 'CACHE' include_examples 'secondary configuration', 'CACHE'
include_examples 'setting a different driver' include_examples 'setting a different driver'
include_examples 'sentinel support', 'CACHE'
end end
end end