replica_pools 2.0.1 → 2.1.0.rc2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/replica_pools/engine.rb +6 -0
- data/lib/replica_pools/query_cache.rb +23 -5
- data/lib/replica_pools/version.rb +1 -1
- data/spec/query_cache_spec.rb +52 -32
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e3df5165498dd9d834fffadadd11d0f41b032457
|
4
|
+
data.tar.gz: 4691bbf3ad9e4390c0bc3c76a388cb1e1fecec9c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 75dafb6740e7d0bc705a8bc57848acfb1d68161856f237ee6589a6010733f19badcf5e0502c232811782c25e8580c91d1162b98997b430cfc3814617ddca7a87
|
7
|
+
data.tar.gz: c6c745c869475560a5f07c5c33b5e3118d569e6891188c99600483a056a8ef540890d24e75201774a857d46eb8955b09aacf080be711f777add018bd8126ae9d
|
data/lib/replica_pools/engine.rb
CHANGED
@@ -21,6 +21,12 @@ module ReplicaPools
|
|
21
21
|
:select_rows, :select, :verify!, :raw_connection, :active?, :reconnect!,
|
22
22
|
:disconnect!, :reset_runtime, :log
|
23
23
|
]
|
24
|
+
elsif ActiveRecord::VERSION::MAJOR == 5
|
25
|
+
[
|
26
|
+
:select_all, :select_one, :select_value, :select_values,
|
27
|
+
:select_rows, :select, :select_prepared, :verify!, :raw_connection,
|
28
|
+
:active?, :reconnect!, :disconnect!, :reset_runtime, :log
|
29
|
+
]
|
24
30
|
else
|
25
31
|
warn "Unsupported ActiveRecord version #{ActiveRecord.version}. Please whitelist the safe methods."
|
26
32
|
end
|
@@ -20,12 +20,30 @@ module ReplicaPools
|
|
20
20
|
# select_all is trickier. it needs to use the leader
|
21
21
|
# connection for cache logic, but ultimately pass its query
|
22
22
|
# through to whatever connection is current.
|
23
|
-
def select_all(
|
24
|
-
|
25
|
-
|
26
|
-
|
23
|
+
def select_all(*args)
|
24
|
+
# there may be more args for Rails 5.0+, but we only care about arel, name, and binds for caching.
|
25
|
+
relation, name, raw_binds = args
|
26
|
+
|
27
|
+
if !query_cache_enabled || locked?(relation)
|
28
|
+
return route_to(current, :select_all, *args)
|
29
|
+
end
|
30
|
+
|
31
|
+
# duplicate binds_from_relation behavior introduced in 4.2.
|
32
|
+
if raw_binds.blank? && relation.is_a?(ActiveRecord::Relation)
|
33
|
+
arel, binds = relation.arel, relation.bind_values
|
34
|
+
else
|
35
|
+
arel, binds = relation, raw_binds
|
36
|
+
end
|
37
|
+
|
38
|
+
sql = to_sql(arel, binds)
|
39
|
+
|
40
|
+
args[0] = sql
|
41
|
+
args[2] = binds
|
42
|
+
|
43
|
+
if Gem::Version.new(ActiveRecord.version) < Gem::Version.new('5.1')
|
44
|
+
cache_sql(sql, binds) { route_to(current, :select_all, *args) }
|
27
45
|
else
|
28
|
-
route_to(current, :select_all,
|
46
|
+
cache_sql(sql, name, binds) { route_to(current, :select_all, *args) }
|
29
47
|
end
|
30
48
|
end
|
31
49
|
|
data/spec/query_cache_spec.rb
CHANGED
@@ -45,42 +45,62 @@ describe ReplicaPools::QueryCache do
|
|
45
45
|
end
|
46
46
|
|
47
47
|
describe "using querycache middleware" do
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
48
|
+
select_all_queries_lambda = lambda do |env|
|
49
|
+
@default_replica1.should_receive(:select_all).exactly(1).and_return([])
|
50
|
+
@default_replica2.should_not_receive(:select_all)
|
51
|
+
@leader.should_not_receive(:select_all)
|
52
|
+
3.times { @proxy.select_all(@sql) }
|
53
|
+
@proxy.next_replica!
|
54
|
+
3.times { @proxy.select_all(@sql) }
|
55
|
+
@proxy.next_replica!
|
56
|
+
3.times { @proxy.select_all(@sql)}
|
57
|
+
@leader.query_cache.keys.size.should == 1
|
58
|
+
[200, {}, nil]
|
59
|
+
end
|
60
|
+
|
61
|
+
insert_update_delete_lambda = lambda do |env|
|
62
|
+
meths = [:insert, :update, :delete, :insert, :update]
|
63
|
+
meths.each do |meth|
|
64
|
+
@leader.should_receive("exec_#{meth}").and_return(true)
|
65
|
+
end
|
66
|
+
|
67
|
+
@default_replica1.should_receive(:select_all).exactly(5).and_return([])
|
68
|
+
@default_replica2.should_receive(:select_all).exactly(0)
|
69
|
+
5.times do |i|
|
70
|
+
@proxy.select_all(@sql)
|
71
|
+
@proxy.select_all(@sql)
|
58
72
|
@leader.query_cache.keys.size.should == 1
|
59
|
-
[
|
60
|
-
|
61
|
-
|
73
|
+
@proxy.send(meths[i], '')
|
74
|
+
@leader.query_cache.keys.size.should == 0
|
75
|
+
end
|
76
|
+
[200, {}, nil]
|
77
|
+
end
|
78
|
+
|
79
|
+
def executor
|
80
|
+
@executor ||= Class.new(ActiveSupport::Executor).tap do |exe|
|
81
|
+
ActiveRecord::QueryCache.install_executor_hooks(exe)
|
82
|
+
end
|
62
83
|
end
|
63
84
|
|
64
|
-
|
65
|
-
mw = ActiveRecord::QueryCache.new lambda { |env|
|
66
|
-
meths = [:insert, :update, :delete, :insert, :update]
|
67
|
-
meths.each do |meth|
|
68
|
-
@leader.should_receive("exec_#{meth}").and_return(true)
|
69
|
-
end
|
85
|
+
if Gem::Version.new(ActiveRecord.version) < Gem::Version.new('5.0')
|
70
86
|
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
87
|
+
it 'should cache queries using select_all' do
|
88
|
+
mw = ActiveRecord::QueryCache.new(select_all_queries_lambda)
|
89
|
+
mw.call({})
|
90
|
+
end
|
91
|
+
|
92
|
+
it 'should invalidate the cache on insert, delete and update' do
|
93
|
+
mw = ActiveRecord::QueryCache.new(insert_update_delete_lambda)
|
94
|
+
mw.call({})
|
95
|
+
end
|
96
|
+
else
|
97
|
+
it 'should cache queries using select_all' do
|
98
|
+
executor.wrap { select_all_queries_lambda }
|
99
|
+
end
|
100
|
+
|
101
|
+
it 'should invalidate the cache on insert, delete and update' do
|
102
|
+
executor.wrap { insert_update_delete_lambda }
|
103
|
+
end
|
83
104
|
end
|
84
105
|
end
|
85
|
-
|
86
106
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: replica_pools
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0.
|
4
|
+
version: 2.1.0.rc2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dan Drabik
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2017-07-31 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activerecord
|
@@ -139,7 +139,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
139
139
|
version: '1.2'
|
140
140
|
requirements: []
|
141
141
|
rubyforge_project:
|
142
|
-
rubygems_version: 2.4.
|
142
|
+
rubygems_version: 2.4.5.2
|
143
143
|
signing_key:
|
144
144
|
specification_version: 4
|
145
145
|
summary: Connection proxy for ActiveRecord for leader / replica setups.
|