Skip to content
This repository has been archived by the owner on Jul 23, 2021. It is now read-only.

Commit

Permalink
Merge pull request #7 from limebike/nick/negative_one_failover
Browse files Browse the repository at this point in the history
Nick/negative one failover
  • Loading branch information
njfix6 authored Jun 14, 2019
2 parents 74723a2 + 209675b commit 2aca510
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 14 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ services:
- postgresql
script: bundle exec rake test
before_script:
- psql -c 'create database distribute_reads_test_primary;' -U postgres
- psql -c 'create database distribute_reads_test_replica;' -U postgres
- createdb distribute_reads_test_primary
- createdb distribute_reads_test_replica
notifications:
email:
on_success: never
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## 0.3.0 [unreleased]

- Use logger instead of stderr
- Dropped support for Active Record < 5

## 0.2.4

Expand Down
5 changes: 3 additions & 2 deletions distribute_reads.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ Gem::Specification.new do |spec|
spec.files = Dir["*.{md,txt}", "{lib}/**/*"]
spec.require_path = "lib"

spec.required_ruby_version = ">= 2.2"
spec.required_ruby_version = ">= 2.4"

spec.add_dependency "makara"
spec.add_dependency "activerecord", ">= 5"

spec.add_development_dependency "bundler"
spec.add_development_dependency "rake"
spec.add_development_dependency "minitest"
spec.add_development_dependency "pg", "< 1"
spec.add_development_dependency "pg"
spec.add_development_dependency "mysql2"
spec.add_development_dependency "activejob"
end
11 changes: 7 additions & 4 deletions lib/distribute_reads.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ def self.lag(connection: nil)
log "Multiple replicas available, lag only reported for one"
end

if %w(PostgreSQL PostGIS).include?(connection.adapter_name)
case connection.adapter_name
when "PostgreSQL", "PostGIS"
# cache the version number
@server_version_num ||= {}
cache_key = connection.pool.object_id
Expand All @@ -66,7 +67,7 @@ def self.lag(connection: nil)
ELSE EXTRACT (EPOCH FROM NOW() - pg_last_xact_replay_timestamp())
END AS lag".squish
).first["lag"].to_f
elsif %w(MySQL Mysql2 Mysql2Spatial Mysql2Rgeo).include?(connection.adapter_name)
when "MySQL", "Mysql2", "Mysql2Spatial", "Mysql2Rgeo"
replica_value = Thread.current[:distribute_reads][:replica]
begin
# makara doesn't send SHOW queries to replica, so we must force it
Expand Down Expand Up @@ -97,6 +98,8 @@ def self.lag(connection: nil)
ensure
Thread.current[:distribute_reads][:replica] = replica_value
end
when "SQLite"
0.0
else
raise DistributeReads::Error, "Option not supported with this adapter"
end
Expand Down Expand Up @@ -125,8 +128,8 @@ def self.default_to_primary=(value)
end
end

Makara::Proxy.send :prepend, DistributeReads::AppropriatePool
Object.send :include, DistributeReads::GlobalMethods
Makara::Proxy.prepend DistributeReads::AppropriatePool
Object.include DistributeReads::GlobalMethods

ActiveSupport.on_load(:active_job) do
require "distribute_reads/job_methods"
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 @@ -21,7 +21,7 @@ def distribute_reads(**options)
if max_lag && !options[:primary]
Array(options[:lag_on] || [ActiveRecord::Base]).each do |base_model|
current_lag = DistributeReads.lag(connection: base_model.connection)
if current_lag > max_lag || current_lag.nil?
if current_lag.nil? || current_lag > max_lag
message = "Replica lag over #{max_lag} seconds#{options[:lag_on] ? " on #{base_model.name} connection" : ""}"
if current_lag.nil?
message = "Replica lag is nil on #{base_model.name}"
Expand Down
6 changes: 3 additions & 3 deletions test/distribute_reads_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,13 @@ def test_max_lag_under

def test_lag_negative_one
error = assert_raises DistributeReads::TooMuchLag do
with_lag(-1) do
with_lag(nil) do
distribute_reads(max_lag: 1) do
assert_replica
end
end
end
assert_equal "Replica lag is -1 on ActiveRecord::Base", error.message
assert_equal "Replica lag is nil on ActiveRecord::Base", error.message
end

def test_max_lag_under_not_stubbed
Expand All @@ -102,7 +102,7 @@ def test_lag_failover
end

def test_lag_failover_negative_one
with_lag(-1) do
with_lag(nil) do
distribute_reads(max_lag: 1, lag_failover: true) do
assert_primary
end
Expand Down
6 changes: 6 additions & 0 deletions test/gemfiles/activerecord50.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
source "https://rubygems.org"

gemspec path: "../.."

gem "activerecord", "~> 5.0.0"
gem "activejob", "~> 5.0.0"
6 changes: 6 additions & 0 deletions test/gemfiles/activerecord51.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
source "https://rubygems.org"

gemspec path: "../.."

gem "activerecord", "~> 5.1.0"
gem "activejob", "~> 5.1.0"
1 change: 0 additions & 1 deletion test/gemfiles/activerecord52.gemfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
source "https://rubygems.org"

# Specify your gem's dependencies in distribute_reads.gemspec
gemspec path: "../.."

gem "activerecord", "~> 5.2.0"
Expand Down
1 change: 0 additions & 1 deletion test/gemfiles/makara3.gemfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
source "https://rubygems.org"

# Specify your gem's dependencies in distribute_reads.gemspec
gemspec path: "../.."

gem "makara", "~> 0.3.0"

0 comments on commit 2aca510

Please sign in to comment.