switchman 3.0.19 → 3.0.20
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.rb +7 -3
- data/lib/switchman/active_record/base.rb +11 -0
- data/lib/switchman/database_server.rb +5 -0
- data/lib/switchman/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1e131a4d625dcfe3ff7854591e717574e3fddf49e29cb9a5aa2e1cc9df8c2f10
|
4
|
+
data.tar.gz: 385c067530063b6d4bcdc3f7f0463a57a54082c14be4e00ddf1f520ea5fabb26
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e6c9d4e232befbbf02b1da406d9bae6fd67731b0942ea3c46d29b87b40f2e457e0f0a03edc14547dd3269d619d0f019beab415150229e8d4a2f395cb3a4e51ea
|
7
|
+
data.tar.gz: f6e536576fa2b1f33c2f641ba0dbf0fdb3e487706558efb2bb924af63b8bc939ed45ee491676d5887313d428e64d42cfc13604fc291e561127cd829ced728471
|
@@ -374,12 +374,12 @@ module Switchman
|
|
374
374
|
shard || source_shard || Shard.current
|
375
375
|
end
|
376
376
|
|
377
|
-
private
|
378
|
-
|
379
377
|
def sharding_initialized
|
380
378
|
@sharding_initialized ||= false
|
381
379
|
end
|
382
380
|
|
381
|
+
private
|
382
|
+
|
383
383
|
def add_sharded_model(klass)
|
384
384
|
@sharded_models = (sharded_models + [klass]).freeze
|
385
385
|
initialize_sharding
|
@@ -407,9 +407,13 @@ module Switchman
|
|
407
407
|
|
408
408
|
klass.connects_to shards: connects_to_hash
|
409
409
|
end
|
410
|
-
DatabaseServer.all.each { |db| db.guard! if db.config[:prefer_secondary] } unless @sharding_initialized
|
411
410
|
|
411
|
+
return if @sharding_initialized
|
412
|
+
|
413
|
+
# If we hadn't initialized sharding yet, the servers won't be guarded
|
414
|
+
# The order matters here or guard_servers will be a noop
|
412
415
|
@sharding_initialized = true
|
416
|
+
DatabaseServer.guard_servers
|
413
417
|
end
|
414
418
|
|
415
419
|
# in-process caching
|
@@ -64,6 +64,17 @@ module Switchman
|
|
64
64
|
current_role != current_role(without_overrides: true)
|
65
65
|
end
|
66
66
|
|
67
|
+
def connected_to_stack
|
68
|
+
ret = super
|
69
|
+
# Really early in boot, DatabaseServer may not be loaded yet,
|
70
|
+
# which is fine since we will guard when we initialize sharding in that case
|
71
|
+
return ret unless const_defined?(:DatabaseServer)
|
72
|
+
return ret if Thread.current.thread_variable?(:ar_connected_to_stack)
|
73
|
+
|
74
|
+
DatabaseServer.guard_servers
|
75
|
+
ret
|
76
|
+
end
|
77
|
+
|
67
78
|
# significant change: Allow per-shard roles
|
68
79
|
def current_role(without_overrides: false)
|
69
80
|
return super() if without_overrides
|
@@ -49,6 +49,10 @@ module Switchman
|
|
49
49
|
servers[rand(servers.length)]
|
50
50
|
end
|
51
51
|
|
52
|
+
def guard_servers
|
53
|
+
all.each { |db| db.guard! if db.config[:prefer_secondary] } if Shard.sharding_initialized
|
54
|
+
end
|
55
|
+
|
52
56
|
private
|
53
57
|
|
54
58
|
def reference_role(role)
|
@@ -133,6 +137,7 @@ module Switchman
|
|
133
137
|
# when doing writes (then it falls back to the current
|
134
138
|
# value of GuardRail.environment)
|
135
139
|
def guard!(environment = :secondary)
|
140
|
+
DatabaseServer.send(:reference_role, environment)
|
136
141
|
::ActiveRecord::Base.connected_to_stack << { shard_roles: { id.to_sym => environment }, klasses: [::ActiveRecord::Base] }
|
137
142
|
end
|
138
143
|
|
data/lib/switchman/version.rb
CHANGED