From fcc86d3a9d80da7f796697212deb144f94232699 Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Tue, 8 Mar 2016 09:37:40 +0800 Subject: [PATCH] FIX: `PostgreSQLFallbackHandler` was bouncing in and out of readonly. --- .../postgresql_fallback_adapter.rb | 12 +++++++----- .../postgresql_fallback_adapter_spec.rb | 14 +++++++++++++- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/lib/active_record/connection_adapters/postgresql_fallback_adapter.rb b/lib/active_record/connection_adapters/postgresql_fallback_adapter.rb index 1f54e8a5c10..bb2d75b6052 100644 --- a/lib/active_record/connection_adapters/postgresql_fallback_adapter.rb +++ b/lib/active_record/connection_adapters/postgresql_fallback_adapter.rb @@ -39,7 +39,7 @@ class PostgreSQLFallbackHandler end Discourse.disable_readonly_mode - master = true + self.master = true end rescue => e if e.message.include?("could not connect to server") @@ -77,6 +77,10 @@ class PostgreSQLFallbackHandler end end + def verify? + !master && !running && !recently_checked? + end + private def config @@ -110,7 +114,7 @@ module ActiveRecord fallback_handler = ::PostgreSQLFallbackHandler.instance config = config.symbolize_keys - if !fallback_handler.master && !fallback_handler.running + if fallback_handler.verify? connection = postgresql_connection(config.dup.merge({ host: config[:replica_host], port: config[:replica_port] })) @@ -148,9 +152,7 @@ module ActiveRecord end def switch_back? - if !fallback_handler.master && !fallback_handler.running - fallback_handler.verify_master - end + fallback_handler.verify_master if fallback_handler.verify? end end end diff --git a/spec/components/active_record/connection_adapters/postgresql_fallback_adapter_spec.rb b/spec/components/active_record/connection_adapters/postgresql_fallback_adapter_spec.rb index 0456f616d4a..05e6b68cd9e 100644 --- a/spec/components/active_record/connection_adapters/postgresql_fallback_adapter_spec.rb +++ b/spec/components/active_record/connection_adapters/postgresql_fallback_adapter_spec.rb @@ -13,8 +13,10 @@ describe ActiveRecord::ConnectionHandling do }).symbolize_keys! end + let(:postgresql_fallback_handler) { PostgreSQLFallbackHandler.instance } + after do - ::PostgreSQLFallbackHandler.instance.setup! + postgresql_fallback_handler.setup! end describe "#postgresql_fallback_connection" do @@ -58,18 +60,26 @@ describe ActiveRecord::ConnectionHandling do })).returns(@replica_connection) end + expect(postgresql_fallback_handler.master).to eq(true) + expect { ActiveRecord::Base.postgresql_fallback_connection(config) } .to raise_error(PG::ConnectionBad) expect{ ActiveRecord::Base.postgresql_fallback_connection(config) } .to change{ Discourse.readonly_mode? }.from(false).to(true) + expect(postgresql_fallback_handler.master).to eq(false) + with_multisite_db(multisite_db) do + expect(postgresql_fallback_handler.master).to eq(true) + expect { ActiveRecord::Base.postgresql_fallback_connection(multisite_config) } .to raise_error(PG::ConnectionBad) expect{ ActiveRecord::Base.postgresql_fallback_connection(multisite_config) } .to change{ Discourse.readonly_mode? }.from(false).to(true) + + expect(postgresql_fallback_handler.master).to eq(false) end ActiveRecord::Base.unstub(:postgresql_connection) @@ -92,6 +102,8 @@ describe ActiveRecord::ConnectionHandling do expect(Discourse.readonly_mode?).to eq(false) + expect(PostgreSQLFallbackHandler.instance.master).to eq(true) + expect(ActiveRecord::Base.connection_pool.connections.count).to eq(0) expect(ActiveRecord::Base.connection)