FIX: `PostgreSQLFallbackHandler` was bouncing in and out of readonly.
This commit is contained in:
parent
74e4251aff
commit
fcc86d3a9d
|
@ -39,7 +39,7 @@ class PostgreSQLFallbackHandler
|
||||||
end
|
end
|
||||||
|
|
||||||
Discourse.disable_readonly_mode
|
Discourse.disable_readonly_mode
|
||||||
master = true
|
self.master = true
|
||||||
end
|
end
|
||||||
rescue => e
|
rescue => e
|
||||||
if e.message.include?("could not connect to server")
|
if e.message.include?("could not connect to server")
|
||||||
|
@ -77,6 +77,10 @@ class PostgreSQLFallbackHandler
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def verify?
|
||||||
|
!master && !running && !recently_checked?
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def config
|
def config
|
||||||
|
@ -110,7 +114,7 @@ module ActiveRecord
|
||||||
fallback_handler = ::PostgreSQLFallbackHandler.instance
|
fallback_handler = ::PostgreSQLFallbackHandler.instance
|
||||||
config = config.symbolize_keys
|
config = config.symbolize_keys
|
||||||
|
|
||||||
if !fallback_handler.master && !fallback_handler.running
|
if fallback_handler.verify?
|
||||||
connection = postgresql_connection(config.dup.merge({
|
connection = postgresql_connection(config.dup.merge({
|
||||||
host: config[:replica_host], port: config[:replica_port]
|
host: config[:replica_host], port: config[:replica_port]
|
||||||
}))
|
}))
|
||||||
|
@ -148,9 +152,7 @@ module ActiveRecord
|
||||||
end
|
end
|
||||||
|
|
||||||
def switch_back?
|
def switch_back?
|
||||||
if !fallback_handler.master && !fallback_handler.running
|
fallback_handler.verify_master if fallback_handler.verify?
|
||||||
fallback_handler.verify_master
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -13,8 +13,10 @@ describe ActiveRecord::ConnectionHandling do
|
||||||
}).symbolize_keys!
|
}).symbolize_keys!
|
||||||
end
|
end
|
||||||
|
|
||||||
|
let(:postgresql_fallback_handler) { PostgreSQLFallbackHandler.instance }
|
||||||
|
|
||||||
after do
|
after do
|
||||||
::PostgreSQLFallbackHandler.instance.setup!
|
postgresql_fallback_handler.setup!
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "#postgresql_fallback_connection" do
|
describe "#postgresql_fallback_connection" do
|
||||||
|
@ -58,18 +60,26 @@ describe ActiveRecord::ConnectionHandling do
|
||||||
})).returns(@replica_connection)
|
})).returns(@replica_connection)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
expect(postgresql_fallback_handler.master).to eq(true)
|
||||||
|
|
||||||
expect { ActiveRecord::Base.postgresql_fallback_connection(config) }
|
expect { ActiveRecord::Base.postgresql_fallback_connection(config) }
|
||||||
.to raise_error(PG::ConnectionBad)
|
.to raise_error(PG::ConnectionBad)
|
||||||
|
|
||||||
expect{ ActiveRecord::Base.postgresql_fallback_connection(config) }
|
expect{ ActiveRecord::Base.postgresql_fallback_connection(config) }
|
||||||
.to change{ Discourse.readonly_mode? }.from(false).to(true)
|
.to change{ Discourse.readonly_mode? }.from(false).to(true)
|
||||||
|
|
||||||
|
expect(postgresql_fallback_handler.master).to eq(false)
|
||||||
|
|
||||||
with_multisite_db(multisite_db) do
|
with_multisite_db(multisite_db) do
|
||||||
|
expect(postgresql_fallback_handler.master).to eq(true)
|
||||||
|
|
||||||
expect { ActiveRecord::Base.postgresql_fallback_connection(multisite_config) }
|
expect { ActiveRecord::Base.postgresql_fallback_connection(multisite_config) }
|
||||||
.to raise_error(PG::ConnectionBad)
|
.to raise_error(PG::ConnectionBad)
|
||||||
|
|
||||||
expect{ ActiveRecord::Base.postgresql_fallback_connection(multisite_config) }
|
expect{ ActiveRecord::Base.postgresql_fallback_connection(multisite_config) }
|
||||||
.to change{ Discourse.readonly_mode? }.from(false).to(true)
|
.to change{ Discourse.readonly_mode? }.from(false).to(true)
|
||||||
|
|
||||||
|
expect(postgresql_fallback_handler.master).to eq(false)
|
||||||
end
|
end
|
||||||
|
|
||||||
ActiveRecord::Base.unstub(:postgresql_connection)
|
ActiveRecord::Base.unstub(:postgresql_connection)
|
||||||
|
@ -92,6 +102,8 @@ describe ActiveRecord::ConnectionHandling do
|
||||||
|
|
||||||
expect(Discourse.readonly_mode?).to eq(false)
|
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_pool.connections.count).to eq(0)
|
||||||
|
|
||||||
expect(ActiveRecord::Base.connection)
|
expect(ActiveRecord::Base.connection)
|
||||||
|
|
Loading…
Reference in New Issue