From a3dfd553a1cf56fa1adcd95c4edb02eeb3400ca9 Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Wed, 10 Jun 2020 14:52:05 +0800 Subject: [PATCH] Revert "Bump redis to 4.2.0." This reverts commit 98bc28cea259b823552e1b953506bcf2f76347a9. --- Gemfile.lock | 2 +- config/initializers/002-rails_failover.rb | 2 +- lib/admin_confirmation.rb | 2 +- lib/discourse.rb | 2 +- lib/discourse_redis.rb | 15 +++++++++++++- spec/components/discourse_redis_spec.rb | 25 +++++++++++++++++++++++ 6 files changed, 43 insertions(+), 5 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 3aee17147de..2870d86e7b8 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -310,7 +310,7 @@ GEM msgpack (>= 0.4.3) optimist (>= 3.0.0) rchardet (1.8.0) - redis (4.2.0) + redis (4.1.4) redis-namespace (1.7.0) redis (>= 3.0.4) regexp_parser (1.7.1) diff --git a/config/initializers/002-rails_failover.rb b/config/initializers/002-rails_failover.rb index 2b84cf11a6c..54692b7f822 100644 --- a/config/initializers/002-rails_failover.rb +++ b/config/initializers/002-rails_failover.rb @@ -65,7 +65,7 @@ if ENV["ACTIVE_RECORD_RAILS_FAILOVER"] end RailsFailover::ActiveRecord.register_force_reading_role_callback do - Discourse.redis.exists?( + Discourse.redis.exists( Discourse::PG_READONLY_MODE_KEY, Discourse::PG_FORCE_READONLY_MODE_KEY ) diff --git a/lib/admin_confirmation.rb b/lib/admin_confirmation.rb index 28f305b5ad2..dc132b72f7d 100644 --- a/lib/admin_confirmation.rb +++ b/lib/admin_confirmation.rb @@ -43,7 +43,7 @@ class AdminConfirmation end def self.exists_for?(user_id) - Discourse.redis.exists? "admin-confirmation:#{user_id}" + Discourse.redis.exists "admin-confirmation:#{user_id}" end def self.find_by_code(token) diff --git a/lib/discourse.rb b/lib/discourse.rb index 27066295a98..67ac64c2972 100644 --- a/lib/discourse.rb +++ b/lib/discourse.rb @@ -489,7 +489,7 @@ module Discourse end def self.readonly_mode?(keys = READONLY_KEYS) - recently_readonly? || Discourse.redis.exists?(*keys) + recently_readonly? || Discourse.redis.exists(*keys) end def self.pg_readonly_mode? diff --git a/lib/discourse_redis.rb b/lib/discourse_redis.rb index 9ca18dcdf89..096c86b5744 100644 --- a/lib/discourse_redis.rb +++ b/lib/discourse_redis.rb @@ -193,7 +193,7 @@ class DiscourseRedis end # Proxy key methods through, but prefix the keys with the namespace - [:append, :blpop, :brpop, :brpoplpush, :decr, :decrby, :expire, :expireat, :exists, :exists?, :get, :getbit, :getrange, :getset, + [:append, :blpop, :brpop, :brpoplpush, :decr, :decrby, :expire, :expireat, :get, :getbit, :getrange, :getset, :hdel, :hexists, :hget, :hgetall, :hincrby, :hincrbyfloat, :hkeys, :hlen, :hmget, :hmset, :hset, :hsetnx, :hvals, :incr, :incrby, :incrbyfloat, :lindex, :linsert, :llen, :lpop, :lpush, :lpushx, :lrange, :lrem, :lset, :ltrim, :mapped_hmset, :mapped_hmget, :mapped_mget, :mapped_mset, :mapped_msetnx, :move, :mset, @@ -207,6 +207,19 @@ class DiscourseRedis end end + # Implement our own because https://github.com/redis/redis-rb/issues/698 has stalled + def exists(*keys) + keys.map! { |a| "#{namespace}:#{a}" } if @namespace + + DiscourseRedis.ignore_readonly do + @redis.synchronize do |client| + client.call([:exists, *keys]) do |value| + value > 0 + end + end + end + end + def mget(*args) args.map! { |a| "#{namespace}:#{a}" } if @namespace DiscourseRedis.ignore_readonly { @redis.mget(*args) } diff --git a/spec/components/discourse_redis_spec.rb b/spec/components/discourse_redis_spec.rb index abc9a5917a8..8e4411653bd 100644 --- a/spec/components/discourse_redis_spec.rb +++ b/spec/components/discourse_redis_spec.rb @@ -87,6 +87,31 @@ describe DiscourseRedis do expect(Discourse.recently_readonly?).to eq(true) end end + + describe '.exists' do + it 'should return false when key is not present' do + expect(Discourse.redis.exists('test')).to eq(false) + end + + it 'should return false when keys are not present' do + expect(Discourse.redis.exists('test', 'test2')).to eq(false) + end + + it 'should return true when key is present' do + Discourse.redis.set('test', 1) + + expect(Discourse.redis.exists('test')).to eq(true) + end + + it 'should return true when any key is present' do + Discourse.redis.set('test', 1) + Discourse.redis.set('test2', 1) + + expect(Discourse.redis.exists('test')).to eq(true) + expect(Discourse.redis.exists('test', 'test2')).to eq(true) + expect(Discourse.redis.exists('test2', 'test3')).to eq(true) + end + end end context '.slave_host' do