From fbbcde1230f220c0e359598187ca66630e462bf2 Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Mon, 7 Nov 2016 15:28:10 +0800 Subject: [PATCH] FIX: Don't treat master as up if it is still loading data. --- lib/discourse_redis.rb | 4 ++-- spec/components/discourse_redis_spec.rb | 12 ++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/discourse_redis.rb b/lib/discourse_redis.rb index b90aa0c0794..9ed43c4e3a9 100644 --- a/lib/discourse_redis.rb +++ b/lib/discourse_redis.rb @@ -98,8 +98,8 @@ class DiscourseRedis options = @options.dup options.delete(:connector) client = Redis::Client.new(options) - client.call([:role]) - @options + loading = client.call([:info]).split("\r\n").include?("loading:1") + loading ? @slave_options : @options rescue Redis::ConnectionError, Redis::CannotConnectError, RuntimeError => ex raise ex if ex.class == RuntimeError && ex.message != "Name or service not known" @fallback_handler.master = false diff --git a/spec/components/discourse_redis_spec.rb b/spec/components/discourse_redis_spec.rb index 730db985b54..8cc8c76d47f 100644 --- a/spec/components/discourse_redis_spec.rb +++ b/spec/components/discourse_redis_spec.rb @@ -70,6 +70,18 @@ describe DiscourseRedis do end end + it "should return the slave config when master is still loading data" do + begin + Redis::Client.any_instance.expects(:call).with([:info]).returns("someconfig:haha\r\nloading:1") + config = connector.resolve + + expect(config[:host]).to eq(slave_host) + expect(config[:port]).to eq(slave_port) + ensure + fallback_handler.master = true + end + end + it "should raise the right error" do error = RuntimeError.new('test error') Redis::Client.any_instance.expects(:call).raises(error).twice