Skip to content

Commit

Permalink
Added support for Makara 0.6 and dropped support for Makara < 0.6
Browse files Browse the repository at this point in the history
  • Loading branch information
ankane committed Jun 17, 2024
1 parent 5ace4c9 commit 08f5683
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 17 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.0.0 (unreleased)

- Added support for Makara 0.6 and dropped support for Makara < 0.6

## 0.5.0 (unreleased)

- Dropped support for Makara 0.4
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ default: &default
makara:
sticky: true
connections:
- role: master
- role: primary
name: primary
url: <%= ENV["DATABASE_URL"] %>
- name: replica
Expand Down
2 changes: 1 addition & 1 deletion distribute_reads.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ Gem::Specification.new do |spec|

spec.required_ruby_version = ">= 3"

spec.add_dependency "makara", ">= 0.5"
spec.add_dependency "makara", ">= 0.6.0.pre"
end
2 changes: 1 addition & 1 deletion lib/distribute_reads.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def self.logger
def self.replication_lag(connection: nil)
connection ||= ActiveRecord::Base.connection

replica_pool = connection.instance_variable_get(:@slave_pool)
replica_pool = connection.instance_variable_get(:@replica_pool)
if replica_pool && replica_pool.connections.size > 1
log "Multiple replicas available, lag only reported for one"
end
Expand Down
22 changes: 11 additions & 11 deletions lib/distribute_reads/appropriate_pool.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,30 @@ module AppropriatePool
def _appropriate_pool(*args)
if Thread.current[:distribute_reads]
if Thread.current[:distribute_reads][:replica]
if @slave_pool.completely_blacklisted?
if @replica_pool.completely_blacklisted?
raise DistributeReads::NoReplicasAvailable, "No replicas available" if Thread.current[:distribute_reads][:failover] == false
DistributeReads.log "No replicas available. Falling back to master pool."
@master_pool
DistributeReads.log "No replicas available. Falling back to primary pool."
@primary_pool
else
@slave_pool
@replica_pool
end
elsif Thread.current[:distribute_reads][:primary] || needs_master?(*args) || (blacklisted = @slave_pool.completely_blacklisted?)
elsif Thread.current[:distribute_reads][:primary] || needs_primary?(*args) || (blacklisted = @replica_pool.completely_blacklisted?)
if blacklisted
if Thread.current[:distribute_reads][:failover] == false
raise DistributeReads::NoReplicasAvailable, "No replicas available"
else
DistributeReads.log "No replicas available. Falling back to master pool."
DistributeReads.log "No replicas available. Falling back to primary pool."
end
end
stick_to_master(*args) if DistributeReads.by_default
@master_pool
stick_to_primary(*args) if DistributeReads.by_default
@primary_pool
elsif in_transaction?
@master_pool
@primary_pool
else
@slave_pool
@replica_pool
end
elsif !DistributeReads.by_default
@master_pool
@primary_pool
else
super
end
Expand Down
2 changes: 1 addition & 1 deletion lib/distribute_reads/global_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def distribute_reads(**options)
# TODO possibly per connection
Thread.current[:distribute_reads][:primary] = true
Thread.current[:distribute_reads][:replica] = false
DistributeReads.log "#{message}. Falling back to master pool."
DistributeReads.log "#{message}. Falling back to primary pool."
break
else
raise DistributeReads::TooMuchLag, message
Expand Down
2 changes: 1 addition & 1 deletion test/distribute_reads_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ def with_eager_load
end

def with_replicas_blacklisted
ActiveRecord::Base.connection.instance_variable_get(:@slave_pool).stub(:completely_blacklisted?, true) do
ActiveRecord::Base.connection.instance_variable_get(:@replica_pool).stub(:completely_blacklisted?, true) do
yield
end
end
Expand Down
2 changes: 1 addition & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def adapter
sticky: true,
connections: [
{
role: "master",
role: "primary",
name: "primary",
database: "distribute_reads_test_primary"
},
Expand Down

0 comments on commit 08f5683

Please sign in to comment.