distribute_reads 0.3.3 → 0.3.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b3e0ef8f494561b701bc58c7119865cf0ff1693364352a218a9a2da9e0fb4060
4
- data.tar.gz: 14b0a9907a9f0592f2c520f325ab689f3ca0eeef924ab89edef698cbbb47bcea
3
+ metadata.gz: 0e52cc02b09f1027e932869c4acacff1d979bbe1c4deb34c952e81bd14c97fc9
4
+ data.tar.gz: 783d432ebe59418e717cd52e2b09ae54174a21000f4cdef96be6e0ebf6651c22
5
5
  SHA512:
6
- metadata.gz: 8b0da6908431d10090a1c942357ef8bfb2fc1dfe480774dabaab873bebf6c4baea1a184d429ec7907d58b18b23c00ff947583c2b7f716055a7224bb98e3c0e0d
7
- data.tar.gz: 2deb9025f60388bb65b03c5f4e27a7f3c8a10d9b174c463437e235a4b07f4eb8f2903e9b87c1d1b3ffd430a2ba4871bb97dc492b318ce13409f2fe775cf88c00
6
+ metadata.gz: 728d62fd84f564e7e35981ae7bd37177c02ab69304b0a9c36e60ec2045af27a43fb284db4f12f5ca9094cbe9fd8108c3f4b6bed0421f4613bee9fb4a50ca8169
7
+ data.tar.gz: 3698ec3ba5c50277a638f013293be1c28a0404baf477a0473e135bf1512641ac9afc6f89060a031f9b256bce162e3585b51640a8672e7c8b8b7ddbb8aa2059ee
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ ## 0.3.4 (2021-08-22)
2
+
3
+ - Added support for Aurora Postgres replication lag
4
+
1
5
  ## 0.3.3 (2020-05-06)
2
6
 
3
7
  - Fixed deprecation warning with MySQL
data/LICENSE.txt CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2017-2020 Andrew Kane
1
+ Copyright (c) 2017-2021 Andrew Kane
2
2
 
3
3
  MIT License
4
4
 
data/README.md CHANGED
@@ -4,7 +4,7 @@ Scale database reads to replicas in Rails
4
4
 
5
5
  :tangerine: Battle-tested at [Instacart](https://www.instacart.com/opensource)
6
6
 
7
- [![Build Status](https://travis-ci.org/ankane/distribute_reads.svg?branch=master)](https://travis-ci.org/ankane/distribute_reads)
7
+ [![Build Status](https://github.com/ankane/distribute_reads/workflows/build/badge.svg?branch=master)](https://github.com/ankane/distribute_reads/actions)
8
8
 
9
9
  ## Installation
10
10
 
@@ -43,23 +43,43 @@ module DistributeReads
43
43
  case connection.adapter_name
44
44
  when "PostgreSQL", "PostGIS"
45
45
  # cache the version number
46
- @server_version_num ||= {}
46
+ @aurora_postgres ||= {}
47
47
  cache_key = connection.pool.object_id
48
+
49
+ unless @aurora_postgres.key?(cache_key)
50
+ @aurora_postgres[cache_key] = connection.select_all("SELECT 1 FROM pg_stat_activity WHERE backend_type = 'aurora runtime'").any?
51
+ end
52
+
53
+ @server_version_num ||= {}
48
54
  @server_version_num[cache_key] ||= connection.select_all("SHOW server_version_num").first["server_version_num"].to_i
49
55
 
50
- lag_condition =
51
- if @server_version_num[cache_key] >= 100000
52
- "pg_last_wal_receive_lsn() = pg_last_wal_replay_lsn()"
53
- else
54
- "pg_last_xlog_receive_location() = pg_last_xlog_replay_location()"
56
+ if @aurora_postgres[cache_key]
57
+ # no way to get session_id at the moment
58
+ # also, pg_is_in_recovery() is always false
59
+ # and pg_settings are the same for writer and readers
60
+ # this means we can't tell:
61
+ # 1. if this is the primary or replica
62
+ # 2. if replica, which one
63
+ status = connection.select_all("SELECT MAX(replica_lag_in_msec) AS replica_lag_in_msec, COUNT(*) AS replica_count FROM aurora_replica_status() WHERE session_id != 'MASTER_SESSION_ID'").first
64
+ if status && status["replica_count"].to_i > 1
65
+ log "Multiple readers available, taking max lag of all of them"
55
66
  end
67
+ status ? status["replica_lag_in_msec"].to_f / 1000.0 : 0.0
68
+ else
69
+ lag_condition =
70
+ if @server_version_num[cache_key] >= 100000
71
+ "pg_last_wal_receive_lsn() = pg_last_wal_replay_lsn()"
72
+ else
73
+ "pg_last_xlog_receive_location() = pg_last_xlog_replay_location()"
74
+ end
56
75
 
57
- connection.select_all(
58
- "SELECT CASE
59
- WHEN NOT pg_is_in_recovery() OR #{lag_condition} THEN 0
60
- ELSE EXTRACT (EPOCH FROM NOW() - pg_last_xact_replay_timestamp())
61
- END AS lag".squish
62
- ).first["lag"].to_f
76
+ connection.select_all(
77
+ "SELECT CASE
78
+ WHEN NOT pg_is_in_recovery() OR #{lag_condition} THEN 0
79
+ ELSE EXTRACT (EPOCH FROM NOW() - pg_last_xact_replay_timestamp())
80
+ END AS lag".squish
81
+ ).first["lag"].to_f
82
+ end
63
83
  when "MySQL", "Mysql2", "Mysql2Spatial", "Mysql2Rgeo"
64
84
  @aurora_mysql ||= {}
65
85
  cache_key = connection.pool.object_id
@@ -149,6 +169,8 @@ end
149
169
 
150
170
  Makara::Proxy.prepend DistributeReads::AppropriatePool
151
171
  Object.include DistributeReads::GlobalMethods
172
+ # TODO uncomment in 0.4.0
173
+ # Object.send :private, :distribute_reads
152
174
 
153
175
  ActiveSupport.on_load(:active_job) do
154
176
  require "distribute_reads/job_methods"
@@ -1,3 +1,3 @@
1
1
  module DistributeReads
2
- VERSION = "0.3.3"
2
+ VERSION = "0.3.4"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: distribute_reads
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.3
4
+ version: 0.3.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Kane
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-05-07 00:00:00.000000000 Z
11
+ date: 2021-08-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: makara
@@ -24,92 +24,8 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0.3'
27
- - !ruby/object:Gem::Dependency
28
- name: bundler
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - ">="
32
- - !ruby/object:Gem::Version
33
- version: '0'
34
- type: :development
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - ">="
39
- - !ruby/object:Gem::Version
40
- version: '0'
41
- - !ruby/object:Gem::Dependency
42
- name: rake
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - ">="
46
- - !ruby/object:Gem::Version
47
- version: '0'
48
- type: :development
49
- prerelease: false
50
- version_requirements: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - ">="
53
- - !ruby/object:Gem::Version
54
- version: '0'
55
- - !ruby/object:Gem::Dependency
56
- name: minitest
57
- requirement: !ruby/object:Gem::Requirement
58
- requirements:
59
- - - ">="
60
- - !ruby/object:Gem::Version
61
- version: '0'
62
- type: :development
63
- prerelease: false
64
- version_requirements: !ruby/object:Gem::Requirement
65
- requirements:
66
- - - ">="
67
- - !ruby/object:Gem::Version
68
- version: '0'
69
- - !ruby/object:Gem::Dependency
70
- name: pg
71
- requirement: !ruby/object:Gem::Requirement
72
- requirements:
73
- - - ">="
74
- - !ruby/object:Gem::Version
75
- version: '0'
76
- type: :development
77
- prerelease: false
78
- version_requirements: !ruby/object:Gem::Requirement
79
- requirements:
80
- - - ">="
81
- - !ruby/object:Gem::Version
82
- version: '0'
83
- - !ruby/object:Gem::Dependency
84
- name: mysql2
85
- requirement: !ruby/object:Gem::Requirement
86
- requirements:
87
- - - ">="
88
- - !ruby/object:Gem::Version
89
- version: '0'
90
- type: :development
91
- prerelease: false
92
- version_requirements: !ruby/object:Gem::Requirement
93
- requirements:
94
- - - ">="
95
- - !ruby/object:Gem::Version
96
- version: '0'
97
- - !ruby/object:Gem::Dependency
98
- name: activejob
99
- requirement: !ruby/object:Gem::Requirement
100
- requirements:
101
- - - ">="
102
- - !ruby/object:Gem::Version
103
- version: '0'
104
- type: :development
105
- prerelease: false
106
- version_requirements: !ruby/object:Gem::Requirement
107
- requirements:
108
- - - ">="
109
- - !ruby/object:Gem::Version
110
- version: '0'
111
- description:
112
- email: andrew@chartkick.com
27
+ description:
28
+ email: andrew@ankane.org
113
29
  executables: []
114
30
  extensions: []
115
31
  extra_rdoc_files: []
@@ -127,7 +43,7 @@ homepage: https://github.com/ankane/distribute_reads
127
43
  licenses:
128
44
  - MIT
129
45
  metadata: {}
130
- post_install_message:
46
+ post_install_message:
131
47
  rdoc_options: []
132
48
  require_paths:
133
49
  - lib
@@ -142,8 +58,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
142
58
  - !ruby/object:Gem::Version
143
59
  version: '0'
144
60
  requirements: []
145
- rubygems_version: 3.1.2
146
- signing_key:
61
+ rubygems_version: 3.2.22
62
+ signing_key:
147
63
  specification_version: 4
148
64
  summary: Scale database reads with replicas in Rails
149
65
  test_files: []