switchman 1.14.5 → 1.14.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/app/models/switchman/shard_internal.rb +4 -5
- data/lib/switchman/active_record/calculations.rb +1 -2
- data/lib/switchman/active_record/connection_handler.rb +3 -0
- data/lib/switchman/active_record/connection_pool.rb +14 -0
- data/lib/switchman/connection_pool_proxy.rb +24 -6
- data/lib/switchman/version.rb +1 -1
- data/lib/tasks/switchman.rake +6 -3
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 533a28f86239be95ad7e7dff0a5022082f3e6373fcd4bf8b2a2aabfd69a69147
|
4
|
+
data.tar.gz: 8dfc6d0f03db1b578101f776c0d764c647ba5cc795525cc26f765cfb4df1e19a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1ed1aea70b720d0f78378eaeba6eb3370e52130c7484d652d490330b2d6594d06ab77f2f15854d282000d7b337832b14adfc52d139ac8ac7e6dbf01c498aff4e
|
7
|
+
data.tar.gz: 9a63a6e7df75e80d82ad04ee5730c13e70409218d9aa46e04b79a0c28b05285bfa6e3114e4d39c9b53cbd4131174d9bbc87a0a02ef9fc1b1db5d3d0592d886ac
|
@@ -179,6 +179,8 @@ module Switchman
|
|
179
179
|
# still need a post-uniq, cause the default database server could be NULL or Rails.env in the db
|
180
180
|
database_servers = scope.reorder('database_server_id').select(:database_server_id).distinct.
|
181
181
|
map(&:database_server).compact.uniq
|
182
|
+
# nothing to do
|
183
|
+
return if database_servers.count == 0
|
182
184
|
parallel = [(max_procs.to_f / database_servers.count).ceil, parallel].min if max_procs
|
183
185
|
|
184
186
|
scopes = Hash[database_servers.map do |server|
|
@@ -699,11 +701,8 @@ module Switchman
|
|
699
701
|
# make sure all connection pool proxies are referencing valid pools
|
700
702
|
::ActiveRecord::Base.connection_handler.connection_pools.each do |pool|
|
701
703
|
next unless pool.is_a?(ConnectionPoolProxy)
|
702
|
-
|
703
|
-
|
704
|
-
pool.current_pool
|
705
|
-
end
|
706
|
-
end
|
704
|
+
|
705
|
+
pool.remove_shard!(self)
|
707
706
|
end
|
708
707
|
end
|
709
708
|
|
@@ -121,9 +121,8 @@ module Switchman
|
|
121
121
|
group_fields = group_attrs
|
122
122
|
end
|
123
123
|
|
124
|
-
# .clone below corrects for what I consider a Rails bug. column_alias_for modifies the string in place.
|
125
124
|
# to_s is because Rails 5 returns a string but Rails 6 returns a symbol.
|
126
|
-
group_aliases = group_fields.map { |field| column_alias_for(field.
|
125
|
+
group_aliases = group_fields.map { |field| column_alias_for(field.downcase.to_s).to_s }
|
127
126
|
group_columns = group_aliases.zip(group_fields).map { |aliaz, field|
|
128
127
|
[aliaz, type_for(field), column_name_for(field)]
|
129
128
|
}
|
@@ -49,6 +49,9 @@ module Switchman
|
|
49
49
|
else
|
50
50
|
::ActiveRecord::Base.configurations[::Rails.env] = config.stringify_keys
|
51
51
|
end
|
52
|
+
else
|
53
|
+
# this is probably wrong now
|
54
|
+
Shard.default.remove_instance_variable(:@name) if Shard.default.instance_variable_defined?(:@name)
|
52
55
|
end
|
53
56
|
|
54
57
|
@shard_connection_pools ||= { [:master, Shard.default.database_server.shareable? ? ::Rails.env : Shard.default] => pool}
|
@@ -47,6 +47,20 @@ module Switchman
|
|
47
47
|
end
|
48
48
|
end
|
49
49
|
|
50
|
+
def remove_shard!(shard)
|
51
|
+
synchronize do
|
52
|
+
# The shard might be currently active, so we need to update our own shard
|
53
|
+
if self.shard == shard
|
54
|
+
self.shard = Shard.default
|
55
|
+
end
|
56
|
+
# Update out any connections that may be using this shard
|
57
|
+
@connections.each do |conn|
|
58
|
+
# This will also update the connection's shard to the default shard
|
59
|
+
switch_database(conn) if conn.shard == shard
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
50
64
|
def clear_idle_connections!(since_when)
|
51
65
|
synchronize do
|
52
66
|
@connections.reject! do |conn|
|
@@ -23,7 +23,14 @@ module Switchman
|
|
23
23
|
@category = category
|
24
24
|
@default_pool = default_pool
|
25
25
|
@connection_pools = shard_connection_pools
|
26
|
-
@schema_cache =
|
26
|
+
@schema_cache = default_pool.get_schema_cache(nil) if ::Rails.version >= '6'
|
27
|
+
@schema_cache = SchemaCache.new(self) unless @schema_cache.is_a?(SchemaCache)
|
28
|
+
if ::Rails.version >= '6'
|
29
|
+
@default_pool.set_schema_cache(@schema_cache)
|
30
|
+
@connection_pools.each_value do |pool|
|
31
|
+
pool.set_schema_cache(@schema_cache)
|
32
|
+
end
|
33
|
+
end
|
27
34
|
end
|
28
35
|
|
29
36
|
def active_shard
|
@@ -50,7 +57,7 @@ module Switchman
|
|
50
57
|
pool = current_pool
|
51
58
|
begin
|
52
59
|
connection = pool.connection
|
53
|
-
connection.instance_variable_set(:@schema_cache, @schema_cache)
|
60
|
+
connection.instance_variable_set(:@schema_cache, @schema_cache) unless ::Rails.version >= '6'
|
54
61
|
connection
|
55
62
|
rescue ConnectionError
|
56
63
|
raise if active_shard.database_server == Shard.default.database_server && active_shackles_environment == :master
|
@@ -60,7 +67,7 @@ module Switchman
|
|
60
67
|
pool = create_pool(config.dup)
|
61
68
|
begin
|
62
69
|
connection = pool.connection
|
63
|
-
connection.instance_variable_set(:@schema_cache, @schema_cache)
|
70
|
+
connection.instance_variable_set(:@schema_cache, @schema_cache) unless ::Rails.version >= '6'
|
64
71
|
rescue ConnectionError
|
65
72
|
raise if idx == configs.length - 1
|
66
73
|
next
|
@@ -71,6 +78,10 @@ module Switchman
|
|
71
78
|
end
|
72
79
|
end
|
73
80
|
|
81
|
+
def get_schema_cache(_connection)
|
82
|
+
@schema_cache
|
83
|
+
end
|
84
|
+
|
74
85
|
%w{release_connection
|
75
86
|
disconnect!
|
76
87
|
flush!
|
@@ -98,6 +109,10 @@ module Switchman
|
|
98
109
|
connection_pools.each { |pool| pool.clear_idle_connections!(since_when) }
|
99
110
|
end
|
100
111
|
|
112
|
+
def remove_shard!(shard)
|
113
|
+
connection_pools.each { |pool| pool.remove_shard!(shard) }
|
114
|
+
end
|
115
|
+
|
101
116
|
protected
|
102
117
|
|
103
118
|
def connection_pools
|
@@ -132,15 +147,18 @@ module Switchman
|
|
132
147
|
end
|
133
148
|
end
|
134
149
|
end
|
135
|
-
|
136
|
-
|
137
|
-
|
150
|
+
spec = ::ActiveRecord::ConnectionAdapters::ConnectionSpecification.new(
|
151
|
+
category,
|
152
|
+
config,
|
153
|
+
"#{config[:adapter]}_connection"
|
154
|
+
)
|
138
155
|
# unfortunately the AR code that does this require logic can't really be
|
139
156
|
# called in isolation
|
140
157
|
require "active_record/connection_adapters/#{config[:adapter]}_adapter"
|
141
158
|
|
142
159
|
::ActiveRecord::ConnectionAdapters::ConnectionPool.new(spec).tap do |pool|
|
143
160
|
pool.shard = shard
|
161
|
+
pool.set_schema_cache(@schema_cache) if ::Rails.version >= '6'
|
144
162
|
if ::Rails.version >= '5.0.1'
|
145
163
|
pool.enable_query_cache! if !@connection_pools.empty? && @connection_pools.first.last.query_cache_enabled
|
146
164
|
end
|
data/lib/switchman/version.rb
CHANGED
data/lib/tasks/switchman.rake
CHANGED
@@ -20,7 +20,12 @@ module Switchman
|
|
20
20
|
open = servers.delete('open')
|
21
21
|
|
22
22
|
servers = servers.map { |server| DatabaseServer.find(server) }.compact
|
23
|
-
|
23
|
+
if open
|
24
|
+
open_servers = DatabaseServer.all.select { |server| server.config[:open] }
|
25
|
+
servers.concat(open_servers)
|
26
|
+
servers << DatabaseServer.find(nil) if open_servers.empty?
|
27
|
+
servers.uniq!
|
28
|
+
end
|
24
29
|
servers = DatabaseServer.all - servers if negative
|
25
30
|
end
|
26
31
|
|
@@ -195,9 +200,7 @@ module Switchman
|
|
195
200
|
@filter_database_servers_chain ||= ->(servers) { servers }
|
196
201
|
end
|
197
202
|
end
|
198
|
-
end
|
199
203
|
|
200
|
-
module Switchman
|
201
204
|
module ActiveRecord
|
202
205
|
module PostgreSQLDatabaseTasks
|
203
206
|
def structure_dump(filename, extra_flags=nil)
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: switchman
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.14.
|
4
|
+
version: 1.14.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Cody Cutrer
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2019-08-
|
13
|
+
date: 2019-08-21 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: railties
|