Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 1 addition & 10 deletions lib/active_record_proxy_adapters/primary_replica_proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -173,15 +173,6 @@ def connection_for(role, sql_string)
update_primary_latest_write_timestamp if !replica_connection?(connection) && write_statement?(sql_string)

result
ensure
# Check the connection back into its own pool, not whatever pool currently
# answers to the :reading role. In Rails system tests,
# ActiveRecord::TestFixtures#setup_shared_connection_pool reassigns the
# :reading role's pool_config on every transactional-fixture test's
# before_setup, so re-resolving `replica_pool` here can return a pool
# different from the one the connection was checked out of — which would
# check a replica adapter into the primary pool and poison it.
connection.pool.checkin(connection) if replica_connection?(connection)
end

def connected_to(role:, &block)
Expand All @@ -195,7 +186,7 @@ def replica_connection?(connection)
end

def checkout_replica_connection
replica_pool.checkout(proxy_checkout_timeout)
replica_pool.lease_connection
# rescue NoDatabaseError to avoid crashing when running db:create rake task
# rescue ConnectionNotEstablished to handle connectivity issues in the replica
# (for example, replication delay)
Expand Down
71 changes: 0 additions & 71 deletions spec/active_record_proxy_adapters/primary_replica_proxy_spec.rb
Comment thread
mateuscruz marked this conversation as resolved.

This file was deleted.

25 changes: 12 additions & 13 deletions spec/shared_examples/a_sql_statement.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,21 @@
require "shared_contexts/a_proxied_method_setup"

RSpec.shared_examples_for "a SQL read statement" do
it "checks out a connection from the replica pool" do
allow(replica_pool).to receive(:checkout).and_call_original
it "leases a connection from the replica pool" do
allow(replica_pool).to receive(:lease_connection).and_call_original

run_test

expect(replica_pool).to have_received(:checkout).once
expect(replica_pool).to have_received(:lease_connection).once
end

it "checks replica connection back in to the pool" do
it "uses the leased replica connection" do
conn = instance_double(adapter_class, method_name => nil, pool: replica_pool)
allow(replica_pool).to receive(:checkout).and_return(conn)
allow(replica_pool).to receive(:checkin)
allow(replica_pool).to receive(:lease_connection).and_return(conn)

run_test

expect(replica_pool).to have_received(:checkin).with(conn).once
expect(conn).to have_received(method_name).once
end

context "when a transaction is open" do
Expand All @@ -31,11 +30,11 @@
end

it "does not checkout a connection from the replica pool" do
allow(replica_pool).to receive(:checkout).and_call_original
allow(replica_pool).to receive(:lease_connection).and_call_original

primary_adapter.transaction { run_test }

expect(replica_pool).not_to have_received(:checkout)
expect(replica_pool).not_to have_received(:lease_connection)
end
end

Expand All @@ -49,22 +48,22 @@
end

it "does not checkout a connection from the replica pool" do
allow(replica_pool).to receive(:checkout).and_call_original
allow(replica_pool).to receive(:lease_connection).and_call_original

model_class.connected_to(role: TestHelper.writing_role) { run_test }

expect(replica_pool).not_to have_received(:checkout)
expect(replica_pool).not_to have_received(:lease_connection)
end
end
end

RSpec.shared_examples_for "a SQL write statement" do
it "does not checkout a connection from replica pool" do
allow(replica_pool).to receive(:checkout).and_call_original
allow(replica_pool).to receive(:lease_connection).and_call_original

run_test

expect(replica_pool).not_to have_received(:checkout)
expect(replica_pool).not_to have_received(:lease_connection)
end

it "sends query to primary connection" do
Expand Down
Loading