From f84ff26aa9494df9df2db1eee3d86040f8ecd943 Mon Sep 17 00:00:00 2001 From: Krzysztof Kotlarek Date: Wed, 23 Dec 2020 13:14:19 +1100 Subject: [PATCH] FIX: use Redis replica host and port (#11566) Introduce Redis `replica` config and deprecate `slave` --- app/models/global_setting.rb | 32 ++++++++++++++++++++----- config/discourse_defaults.conf | 14 +++++------ config/initializers/400-deprecations.rb | 19 +++++++++++++++ spec/models/global_setting_spec.rb | 8 +++---- 4 files changed, 56 insertions(+), 17 deletions(-) create mode 100644 config/initializers/400-deprecations.rb diff --git a/app/models/global_setting.rb b/app/models/global_setting.rb index abd555af0fe..8543ca6ebbe 100644 --- a/app/models/global_setting.rb +++ b/app/models/global_setting.rb @@ -163,6 +163,26 @@ class GlobalSetting @message_bus_config = nil end + def self.get_redis_replica_host + return redis_replica_host if redis_replica_host.present? + redis_slave_host if respond_to?(:redis_slave_host) && redis_slave_host.present? + end + + def self.get_redis_replica_port + return redis_replica_port if redis_replica_port.present? + redis_slave_port if respond_to?(:redis_slave_port) && redis_slave_port.present? + end + + def self.get_message_bus_redis_replica_host + return message_bus_redis_replica_host if message_bus_redis_replica_host.present? + message_bus_redis_slave_host if respond_to?(:message_bus_redis_slave_host) && message_bus_redis_slave_host.present? + end + + def self.get_message_bus_redis_replica_port + return message_bus_redis_replica_port if message_bus_redis_replica_port.present? + message_bus_redis_slave_port if respond_to?(:message_bus_redis_slave_port) && message_bus_redis_slave_port.present? + end + def self.redis_config @config ||= begin @@ -170,9 +190,9 @@ class GlobalSetting c[:host] = redis_host if redis_host c[:port] = redis_port if redis_port - if redis_slave_host && redis_slave_port && defined?(RailsFailover) - c[:replica_host] = redis_slave_host - c[:replica_port] = redis_slave_port + if get_redis_replica_host && get_redis_replica_port && defined?(RailsFailover) + c[:replica_host] = get_redis_replica_host + c[:replica_port] = get_redis_replica_port c[:connector] = RailsFailover::Redis::Connector end @@ -194,9 +214,9 @@ class GlobalSetting c[:host] = message_bus_redis_host if message_bus_redis_host c[:port] = message_bus_redis_port if message_bus_redis_port - if message_bus_redis_slave_host && message_bus_redis_slave_port - c[:replica_host] = message_bus_redis_slave_host - c[:replica_port] = message_bus_redis_slave_port + if get_message_bus_redis_replica_host && get_message_bus_redis_replica_port + c[:replica_host] = get_message_bus_redis_replica_host + c[:replica_port] = get_message_bus_redis_replica_port c[:connector] = RailsFailover::Redis::Connector end diff --git a/config/discourse_defaults.conf b/config/discourse_defaults.conf index 22ba8dee1c4..d7e80e1f56a 100644 --- a/config/discourse_defaults.conf +++ b/config/discourse_defaults.conf @@ -124,11 +124,11 @@ redis_host = localhost # redis server port redis_port = 6379 -# redis slave server address -redis_slave_host = +# redis replica server address +redis_replica_host = -# redis slave server port -redis_slave_port = 6379 +# redis replica server port +redis_replica_port = 6379 # redis database redis_db = 0 @@ -151,11 +151,11 @@ message_bus_redis_host = localhost # message bus redis server port message_bus_redis_port = 6379 -# message bus redis slave server address -message_bus_redis_slave_host = +# message bus redis replica server address +message_bus_redis_replica_host = # message bus redis slave server port -message_bus_redis_slave_port = 6379 +message_bus_redis_replica_port = 6379 # message bus redis database message_bus_redis_db = 0 diff --git a/config/initializers/400-deprecations.rb b/config/initializers/400-deprecations.rb new file mode 100644 index 00000000000..39373182d16 --- /dev/null +++ b/config/initializers/400-deprecations.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +if !GlobalSetting.skip_redis? + if GlobalSetting.respond_to?(:redis_slave_host) && GlobalSetting.redis_slave_host.present? + Discourse.deprecate("redis_slave_host is deprecated, use redis_replica_host instead", drop_from: "2.8") + end + + if GlobalSetting.respond_to?(:redis_slave_port) && GlobalSetting.redis_slave_port.present? + Discourse.deprecate("redis_slave_port is deprecated, use redis_replica_port instead", drop_from: "2.8") + end + + if GlobalSetting.respond_to?(:message_bus_redis_slave_host) && GlobalSetting.message_bus_redis_slave_host.present? + Discourse.deprecate("message_bus_redis_slave_host is deprecated, use message_bus_redis_replica_host", drop_from: "2.8") + end + + if GlobalSetting.respond_to?(:message_bus_redis_slave_port) && GlobalSetting.message_bus_redis_slave_port.present? + Discourse.deprecate("message_bus_redis_slave_port is deprecated, use message_bus_redis_replica_port", drop_from: "2.8") + end +end diff --git a/spec/models/global_setting_spec.rb b/spec/models/global_setting_spec.rb index 88024182773..349c1914b3e 100644 --- a/spec/models/global_setting_spec.rb +++ b/spec/models/global_setting_spec.rb @@ -67,13 +67,13 @@ describe GlobalSetting do end describe '.redis_config' do - describe 'when slave config is not present' do + describe 'when replica config is not present' do it "should not set any connector" do expect(GlobalSetting.redis_config[:connector]).to eq(nil) end end - describe 'when slave config is present' do + describe 'when replica config is present' do before do GlobalSetting.reset_redis_config! end @@ -83,8 +83,8 @@ describe GlobalSetting do end it "should set the right connector" do - GlobalSetting.expects(:redis_slave_port).returns(6379).at_least_once - GlobalSetting.expects(:redis_slave_host).returns('0.0.0.0').at_least_once + GlobalSetting.expects(:redis_replica_port).returns(6379).at_least_once + GlobalSetting.expects(:redis_replica_host).returns('0.0.0.0').at_least_once expect(GlobalSetting.redis_config[:connector]).to eq(RailsFailover::Redis::Connector) end