mirror of
https://github.com/discourse/discourse.git
synced 2025-06-01 17:40:43 +08:00
Merge pull request #4546 from tgxworld/fix_postgresql_failover
Fix postgresql failover
This commit is contained in:
@ -48,8 +48,9 @@ describe ActiveRecord::ConnectionHandling do
|
||||
end
|
||||
|
||||
it 'should failover to a replica server' do
|
||||
current_threads = Thread.list
|
||||
|
||||
RailsMultisite::ConnectionManagement.stubs(:all_dbs).returns(['default', multisite_db])
|
||||
::PostgreSQLFallbackHandler.instance.setup!
|
||||
|
||||
[config, multisite_config].each do |configuration|
|
||||
ActiveRecord::Base.expects(:postgresql_connection).with(configuration).raises(PG::ConnectionBad)
|
||||
@ -60,7 +61,7 @@ describe ActiveRecord::ConnectionHandling do
|
||||
})).returns(@replica_connection)
|
||||
end
|
||||
|
||||
expect(postgresql_fallback_handler.master).to eq(true)
|
||||
expect(postgresql_fallback_handler.master_down?).to eq(nil)
|
||||
|
||||
expect { ActiveRecord::Base.postgresql_fallback_connection(config) }
|
||||
.to raise_error(PG::ConnectionBad)
|
||||
@ -68,10 +69,10 @@ describe ActiveRecord::ConnectionHandling do
|
||||
expect{ ActiveRecord::Base.postgresql_fallback_connection(config) }
|
||||
.to change{ Discourse.readonly_mode? }.from(false).to(true)
|
||||
|
||||
expect(postgresql_fallback_handler.master).to eq(false)
|
||||
expect(postgresql_fallback_handler.master_down?).to eq(true)
|
||||
|
||||
with_multisite_db(multisite_db) do
|
||||
expect(postgresql_fallback_handler.master).to eq(true)
|
||||
expect(postgresql_fallback_handler.master_down?).to eq(nil)
|
||||
|
||||
expect { ActiveRecord::Base.postgresql_fallback_connection(multisite_config) }
|
||||
.to raise_error(PG::ConnectionBad)
|
||||
@ -79,30 +80,18 @@ describe ActiveRecord::ConnectionHandling do
|
||||
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)
|
||||
expect(postgresql_fallback_handler.master_down?).to eq(true)
|
||||
end
|
||||
|
||||
postgresql_fallback_handler.master_up(multisite_db)
|
||||
|
||||
ActiveRecord::Base.unstub(:postgresql_connection)
|
||||
|
||||
current_threads = Thread.list
|
||||
|
||||
expect{ ActiveRecord::Base.connection_pool.checkout }
|
||||
.to change{ Thread.list.size }.by(1)
|
||||
|
||||
# Ensure that we don't try to connect back to the replica when a thread
|
||||
# is running
|
||||
begin
|
||||
ActiveRecord::Base.postgresql_fallback_connection(config)
|
||||
rescue PG::ConnectionBad => e
|
||||
# This is expected if the thread finishes before the above is called.
|
||||
end
|
||||
|
||||
# Wait for the thread to finish execution
|
||||
(Thread.list - current_threads).each(&:join)
|
||||
postgresql_fallback_handler.initiate_fallback_to_master
|
||||
|
||||
expect(Discourse.readonly_mode?).to eq(false)
|
||||
|
||||
expect(PostgreSQLFallbackHandler.instance.master).to eq(true)
|
||||
expect(postgresql_fallback_handler.master_down?).to eq(nil)
|
||||
|
||||
expect(ActiveRecord::Base.connection_pool.connections.count).to eq(0)
|
||||
|
||||
|
Reference in New Issue
Block a user