From 439db7ca1ece96819948c3abd3b61e9927200087 Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Tue, 2 Jun 2020 17:24:14 +0800 Subject: [PATCH] DEV: Add `REDIS_RAILS_FAILOVER` env to test our new redis failover. --- Gemfile.lock | 2 +- app/models/global_setting.rb | 24 +++++++++++++++++------ config/application.rb | 4 ++++ config/initializers/002-rails_failover.rb | 11 +++++++++++ lib/discourse_redis.rb | 5 ++++- 5 files changed, 38 insertions(+), 8 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 53a0a696ce1..92e1f2a4a7a 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,6 +1,6 @@ GIT remote: https://github.com/discourse/rails_failover - revision: 291bf1c18e9a704556a40204d8160549d0438e96 + revision: 73ceb21624079e007a7d3bf0bf201f0b34dcd6aa specs: rails_failover (0.4.0) activerecord (~> 6.0) diff --git a/app/models/global_setting.rb b/app/models/global_setting.rb index a31df606eb8..cd80df66818 100644 --- a/app/models/global_setting.rb +++ b/app/models/global_setting.rb @@ -169,9 +169,15 @@ class GlobalSetting c[:port] = redis_port if redis_port if redis_slave_host && redis_slave_port - c[:slave_host] = redis_slave_host - c[:slave_port] = redis_slave_port - c[:connector] = DiscourseRedis::Connector + if ENV["REDIS_RAILS_FAILOVER"] + c[:replica_host] = redis_slave_host + c[:replica_port] = redis_slave_port + c[:connector] = RailsFailover::Redis::Connector + else + c[:slave_host] = redis_slave_host + c[:slave_port] = redis_slave_port + c[:connector] = DiscourseRedis::Connector + end end c[:password] = redis_password if redis_password.present? @@ -193,9 +199,15 @@ class GlobalSetting c[:port] = message_bus_redis_port if message_bus_redis_port if message_bus_redis_slave_host && message_bus_redis_slave_port - c[:slave_host] = message_bus_redis_slave_host - c[:slave_port] = message_bus_redis_slave_port - c[:connector] = DiscourseRedis::Connector + if ENV["REDIS_RAILS_FAILOVER"] + c[:replica_host] = redis_slave_host + c[:replica_port] = redis_slave_port + c[:connector] = RailsFailover::Redis::Connector + else + c[:slave_host] = message_bus_redis_slave_host + c[:slave_port] = message_bus_redis_slave_port + c[:connector] = DiscourseRedis::Connector + end end c[:password] = message_bus_redis_password if message_bus_redis_password.present? diff --git a/config/application.rb b/config/application.rb index f2cbb143912..07730bc63b5 100644 --- a/config/application.rb +++ b/config/application.rb @@ -31,6 +31,10 @@ if ENV['ACTIVE_RECORD_RAILS_FAILOVER'] require 'rails_failover/active_record' end +if ENV['REDIS_RAILS_FAILOVER'] + require 'rails_failover/redis' +end + # Global config require_relative '../app/models/global_setting' GlobalSetting.configure! diff --git a/config/initializers/002-rails_failover.rb b/config/initializers/002-rails_failover.rb index 04186541c5f..5b65d579f19 100644 --- a/config/initializers/002-rails_failover.rb +++ b/config/initializers/002-rails_failover.rb @@ -1,5 +1,16 @@ # frozen_string_literal: true +if ENV["REDIS_RAILS_FAILOVER"] + RailsFailover::Redis.on_failover do + Discourse.received_redis_readonly! + end + + RailsFailover::Redis.on_fallback do + Discourse.clear_readonly! + Discourse.request_refresh! + end +end + if ENV["ACTIVE_RECORD_RAILS_FAILOVER"] RailsFailover::ActiveRecord.on_failover do RailsMultisite::ConnectionManagement.each_connection do diff --git a/lib/discourse_redis.rb b/lib/discourse_redis.rb index e273810a3c7..b172c02d122 100644 --- a/lib/discourse_redis.rb +++ b/lib/discourse_redis.rb @@ -176,7 +176,10 @@ class DiscourseRedis STDERR.puts "WARN: Redis is in a readonly state. Performed a noop" end - fallback_handler.verify_master if !fallback_handler.master + if !ENV["REDIS_RAILS_FAILOVER"] + fallback_handler.verify_master if !fallback_handler.master + end + Discourse.received_redis_readonly! nil else