ar-octopus 0.8.1 → 0.10.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +6 -14
- data/.gitignore +1 -0
- data/.rspec +1 -1
- data/.rubocop.yml +46 -0
- data/.rubocop_todo.yml +56 -0
- data/.travis.yml +7 -12
- data/Appraisals +11 -4
- data/Gemfile +1 -1
- data/README.mkdn +138 -63
- data/Rakefile +23 -16
- data/ar-octopus.gemspec +23 -20
- data/gemfiles/rails42.gemfile +7 -0
- data/gemfiles/{rails32.gemfile → rails5.gemfile} +2 -2
- data/gemfiles/{rails4.gemfile → rails51.gemfile} +2 -2
- data/gemfiles/rails52.gemfile +7 -0
- data/lib/ar-octopus.rb +1 -1
- data/lib/octopus/{rails3/abstract_adapter.rb → abstract_adapter.rb} +4 -15
- data/lib/octopus/association.rb +8 -99
- data/lib/octopus/association_shard_tracking.rb +74 -0
- data/lib/octopus/collection_association.rb +17 -0
- data/lib/octopus/collection_proxy.rb +16 -0
- data/lib/octopus/exception.rb +4 -0
- data/lib/octopus/finder_methods.rb +8 -0
- data/lib/octopus/load_balancing/round_robin.rb +20 -0
- data/lib/octopus/load_balancing.rb +4 -0
- data/lib/octopus/{rails3/log_subscriber.rb → log_subscriber.rb} +6 -2
- data/lib/octopus/migration.rb +187 -110
- data/lib/octopus/model.rb +151 -131
- data/lib/octopus/persistence.rb +45 -0
- data/lib/octopus/proxy.rb +297 -232
- data/lib/octopus/proxy_config.rb +251 -0
- data/lib/octopus/query_cache_for_shards.rb +24 -0
- data/lib/octopus/railtie.rb +1 -3
- data/lib/octopus/relation_proxy.rb +70 -0
- data/lib/octopus/result_patch.rb +19 -0
- data/lib/octopus/scope_proxy.rb +54 -36
- data/lib/octopus/shard_tracking/attribute.rb +22 -0
- data/lib/octopus/shard_tracking/dynamic.rb +11 -0
- data/lib/octopus/shard_tracking.rb +46 -0
- data/lib/octopus/singular_association.rb +9 -0
- data/lib/octopus/slave_group.rb +13 -0
- data/lib/octopus/version.rb +1 -1
- data/lib/octopus.rb +125 -33
- data/lib/tasks/octopus.rake +2 -2
- data/sample_app/Gemfile +3 -3
- data/sample_app/autotest/discover.rb +2 -2
- data/sample_app/config/application.rb +1 -1
- data/sample_app/config/boot.rb +1 -1
- data/sample_app/config/environments/test.rb +1 -1
- data/sample_app/config/initializers/session_store.rb +1 -1
- data/sample_app/config/initializers/wrap_parameters.rb +1 -1
- data/sample_app/config/routes.rb +1 -1
- data/sample_app/db/migrate/20100720210335_create_sample_users.rb +2 -2
- data/sample_app/db/schema.rb +10 -10
- data/sample_app/db/seeds.rb +3 -3
- data/sample_app/features/step_definitions/seeds_steps.rb +4 -4
- data/sample_app/features/step_definitions/web_steps.rb +3 -4
- data/sample_app/features/support/env.rb +3 -4
- data/sample_app/features/support/paths.rb +4 -4
- data/sample_app/lib/tasks/cucumber.rake +43 -44
- data/sample_app/spec/spec_helper.rb +3 -3
- data/spec/config/shards.yml +78 -0
- data/spec/migrations/10_create_users_using_replication.rb +4 -4
- data/spec/migrations/11_add_field_in_all_slaves.rb +4 -4
- data/spec/migrations/12_create_users_using_block.rb +8 -8
- data/spec/migrations/13_create_users_using_block_and_using.rb +5 -5
- data/spec/migrations/14_create_users_on_shards_of_a_group_with_versions.rb +3 -3
- data/spec/migrations/15_create_user_on_shards_of_default_group_with_versions.rb +3 -3
- data/spec/migrations/1_create_users_on_master.rb +4 -4
- data/spec/migrations/2_create_users_on_canada.rb +4 -4
- data/spec/migrations/3_create_users_on_both_shards.rb +4 -4
- data/spec/migrations/4_create_users_on_shards_of_a_group.rb +4 -4
- data/spec/migrations/5_create_users_on_multiples_groups.rb +3 -3
- data/spec/migrations/6_raise_exception_with_invalid_shard_name.rb +4 -4
- data/spec/migrations/7_raise_exception_with_invalid_multiple_shard_names.rb +4 -4
- data/spec/migrations/8_raise_exception_with_invalid_group_name.rb +4 -4
- data/spec/migrations/9_raise_exception_with_multiple_invalid_group_names.rb +5 -5
- data/spec/octopus/association_shard_tracking_spec.rb +1036 -0
- data/spec/octopus/collection_proxy_spec.rb +16 -0
- data/spec/octopus/load_balancing/round_robin_spec.rb +15 -0
- data/spec/octopus/log_subscriber_spec.rb +5 -5
- data/spec/octopus/migration_spec.rb +83 -49
- data/spec/octopus/model_spec.rb +544 -292
- data/spec/octopus/octopus_spec.rb +64 -31
- data/spec/octopus/proxy_spec.rb +145 -141
- data/spec/octopus/query_cache_for_shards_spec.rb +40 -0
- data/spec/octopus/relation_proxy_spec.rb +132 -0
- data/spec/octopus/replicated_slave_grouped_spec.rb +91 -0
- data/spec/octopus/replication_spec.rb +140 -65
- data/spec/octopus/scope_proxy_spec.rb +90 -10
- data/spec/octopus/sharded_replicated_slave_grouped_spec.rb +55 -0
- data/spec/octopus/sharded_spec.rb +10 -10
- data/spec/spec_helper.rb +8 -6
- data/spec/support/active_record/connection_adapters/modify_config_adapter.rb +1 -3
- data/spec/support/database_connection.rb +2 -2
- data/spec/support/database_models.rb +18 -17
- data/spec/support/octopus_helper.rb +32 -25
- data/spec/support/query_count.rb +1 -3
- data/spec/support/shared_contexts.rb +3 -3
- data/spec/tasks/octopus.rake_spec.rb +10 -10
- metadata +112 -70
- data/.ruby-version +0 -1
- data/init.rb +0 -1
- data/lib/octopus/association_collection.rb +0 -49
- data/lib/octopus/has_and_belongs_to_many_association.rb +0 -17
- data/lib/octopus/rails3/persistence.rb +0 -39
- data/lib/octopus/rails3/singular_association.rb +0 -34
- data/rails/init.rb +0 -1
- data/spec/octopus/association_spec.rb +0 -712
data/lib/octopus/model.rb
CHANGED
@@ -1,70 +1,169 @@
|
|
1
1
|
require 'active_support/deprecation'
|
2
2
|
|
3
|
-
module Octopus
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
module SharedMethods
|
12
|
-
def clean_table_name
|
13
|
-
return unless self.connection_proxy.should_clean_table_name?
|
3
|
+
module Octopus
|
4
|
+
module Model
|
5
|
+
def self.extended(base)
|
6
|
+
base.send(:include, Octopus::ShardTracking::Attribute)
|
7
|
+
base.send(:include, InstanceMethods)
|
8
|
+
base.extend(ClassMethods)
|
9
|
+
end
|
14
10
|
|
15
|
-
|
16
|
-
|
17
|
-
|
11
|
+
module SharedMethods
|
12
|
+
def using(shard)
|
13
|
+
if block_given?
|
14
|
+
raise Octopus::Exception, <<-EOF
|
15
|
+
#{name}.using is not allowed to receive a block, it works just like a regular scope.
|
18
16
|
|
19
|
-
|
20
|
-
|
21
|
-
|
17
|
+
If you are trying to scope everything to a specific shard, use Octopus.using instead.
|
18
|
+
EOF
|
19
|
+
end
|
22
20
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
self
|
21
|
+
if Octopus.enabled?
|
22
|
+
Octopus::ScopeProxy.new(shard, self)
|
23
|
+
else
|
24
|
+
self
|
25
|
+
end
|
29
26
|
end
|
30
27
|
end
|
31
28
|
|
32
|
-
|
33
|
-
|
34
|
-
|
29
|
+
module InstanceMethods
|
30
|
+
include SharedMethods
|
31
|
+
|
32
|
+
def self.included(base)
|
33
|
+
base.send(:alias_method, :equality_without_octopus, :==)
|
34
|
+
base.send(:alias_method, :==, :equality_with_octopus)
|
35
|
+
base.send(:alias_method, :eql?, :==)
|
36
|
+
base.send(:alias_method, :perform_validations_without_octopus, :perform_validations)
|
37
|
+
base.send(:alias_method, :perform_validations, :perform_validations_with_octopus)
|
38
|
+
end
|
35
39
|
|
36
40
|
def set_current_shard
|
37
41
|
return unless Octopus.enabled?
|
42
|
+
shard = self.class.connection_proxy.current_shard
|
43
|
+
self.current_shard = shard if self.class.allowed_shard?(shard)
|
44
|
+
end
|
45
|
+
|
46
|
+
def init_with(coder)
|
47
|
+
obj = super
|
48
|
+
|
49
|
+
return obj unless Octopus.enabled?
|
50
|
+
return obj if obj.class.connection_proxy.current_model_replicated?
|
51
|
+
|
52
|
+
current_shard_value = coder['attributes']['current_shard'].value if coder['attributes']['current_shard'].present? && coder['attributes']['current_shard'].value.present?
|
53
|
+
|
54
|
+
coder['attributes'].send(:attributes).send(:values).delete('current_shard')
|
55
|
+
coder['attributes'].send(:attributes).send(:delegate_hash).delete('current_shard')
|
56
|
+
|
57
|
+
obj.current_shard = current_shard_value if current_shard_value.present?
|
58
|
+
obj
|
59
|
+
end
|
60
|
+
|
61
|
+
def should_set_current_shard?
|
62
|
+
self.respond_to?(:current_shard) && !current_shard.nil?
|
63
|
+
end
|
64
|
+
|
65
|
+
def equality_with_octopus(comparison_object)
|
66
|
+
equality_without_octopus(comparison_object) && comparison_object.current_shard.to_s == current_shard.to_s
|
67
|
+
end
|
38
68
|
|
39
|
-
|
40
|
-
|
69
|
+
def perform_validations_with_octopus(*args)
|
70
|
+
if Octopus.enabled? && should_set_current_shard?
|
71
|
+
Octopus.using(current_shard) do
|
72
|
+
perform_validations_without_octopus(*args)
|
73
|
+
end
|
41
74
|
else
|
42
|
-
|
75
|
+
perform_validations_without_octopus(*args)
|
43
76
|
end
|
44
77
|
end
|
45
|
-
|
46
|
-
after_initialize :set_current_shard
|
47
78
|
end
|
48
79
|
|
49
|
-
|
50
|
-
|
51
|
-
|
80
|
+
module ClassMethods
|
81
|
+
include SharedMethods
|
82
|
+
|
83
|
+
def self.extended(base)
|
84
|
+
base.class_attribute(:replicated)
|
85
|
+
base.class_attribute(:sharded)
|
86
|
+
base.class_attribute(:allowed_shards)
|
87
|
+
base.hijack_methods
|
88
|
+
end
|
89
|
+
|
90
|
+
def replicated_model
|
91
|
+
self.replicated = true
|
92
|
+
end
|
93
|
+
|
94
|
+
def sharded_model
|
95
|
+
self.sharded = true
|
96
|
+
end
|
97
|
+
|
98
|
+
def allow_shard(*shards)
|
99
|
+
self.allowed_shards ||= []
|
100
|
+
self.allowed_shards += shards
|
101
|
+
end
|
102
|
+
|
103
|
+
def hijack_methods
|
104
|
+
after_initialize :set_current_shard
|
105
|
+
|
106
|
+
around_save :run_on_shard, :unless => lambda { self.class.custom_octopus_connection }
|
107
|
+
|
108
|
+
class_attribute :custom_octopus_connection
|
109
|
+
|
110
|
+
class << self
|
111
|
+
attr_accessor :custom_octopus_table_name
|
112
|
+
|
113
|
+
alias_method :connection_without_octopus, :connection
|
114
|
+
alias_method :connection, :connection_with_octopus
|
115
|
+
|
116
|
+
alias_method :connection_pool_without_octopus, :connection_pool
|
117
|
+
alias_method :connection_pool, :connection_pool_with_octopus
|
118
|
+
|
119
|
+
alias_method :clear_all_connections_without_octopus!, :clear_all_connections!
|
120
|
+
alias_method :clear_all_connections!, :clear_all_connections_with_octopus!
|
121
|
+
|
122
|
+
alias_method :clear_active_connections_without_octopus!, :clear_active_connections!
|
123
|
+
alias_method :clear_active_connections!, :clear_active_connections_with_octopus!
|
124
|
+
|
125
|
+
alias_method :connected_without_octopus?, :connected?
|
126
|
+
alias_method :connected?, :connected_with_octopus?
|
127
|
+
|
128
|
+
def table_name=(value = nil)
|
129
|
+
self.custom_octopus_table_name = true
|
130
|
+
super
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
def connection_proxy
|
136
|
+
ActiveRecord::Base.class_variable_defined?(:@@connection_proxy) &&
|
137
|
+
ActiveRecord::Base.class_variable_get(:@@connection_proxy) ||
|
138
|
+
ActiveRecord::Base.class_variable_set(:@@connection_proxy, Octopus::Proxy.new)
|
52
139
|
end
|
53
140
|
|
54
|
-
def
|
55
|
-
|
141
|
+
def should_use_normal_connection?
|
142
|
+
if !Octopus.enabled?
|
143
|
+
true
|
144
|
+
elsif custom_octopus_connection
|
145
|
+
!connection_proxy.block || !allowed_shard?(connection_proxy.current_shard)
|
146
|
+
end
|
56
147
|
end
|
57
148
|
|
58
|
-
def
|
149
|
+
def allowed_shard?(shard)
|
150
|
+
if custom_octopus_connection
|
151
|
+
allowed_shards && shard && (allowed_shards.include?(shard.to_s) || allowed_shards.include?(shard.to_sym))
|
152
|
+
else
|
153
|
+
true
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
def connection_with_octopus
|
59
158
|
if should_use_normal_connection?
|
60
159
|
connection_without_octopus
|
61
160
|
else
|
62
|
-
|
63
|
-
|
161
|
+
connection_proxy.current_model = self
|
162
|
+
connection_proxy
|
64
163
|
end
|
65
164
|
end
|
66
165
|
|
67
|
-
def
|
166
|
+
def connection_pool_with_octopus
|
68
167
|
if should_use_normal_connection?
|
69
168
|
connection_pool_without_octopus
|
70
169
|
else
|
@@ -72,7 +171,7 @@ module Octopus::Model
|
|
72
171
|
end
|
73
172
|
end
|
74
173
|
|
75
|
-
def
|
174
|
+
def clear_active_connections_with_octopus!
|
76
175
|
if should_use_normal_connection?
|
77
176
|
clear_active_connections_without_octopus!
|
78
177
|
else
|
@@ -80,7 +179,7 @@ module Octopus::Model
|
|
80
179
|
end
|
81
180
|
end
|
82
181
|
|
83
|
-
def
|
182
|
+
def clear_all_connections_with_octopus!
|
84
183
|
if should_use_normal_connection?
|
85
184
|
clear_all_connections_without_octopus!
|
86
185
|
else
|
@@ -88,7 +187,7 @@ module Octopus::Model
|
|
88
187
|
end
|
89
188
|
end
|
90
189
|
|
91
|
-
def
|
190
|
+
def connected_with_octopus?
|
92
191
|
if should_use_normal_connection?
|
93
192
|
connected_without_octopus?
|
94
193
|
else
|
@@ -96,100 +195,21 @@ module Octopus::Model
|
|
96
195
|
end
|
97
196
|
end
|
98
197
|
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
alias_method_chain :clear_all_connections!, :octopus
|
103
|
-
alias_method_chain :clear_active_connections!, :octopus
|
104
|
-
alias_method_chain :connected?, :octopus
|
198
|
+
def set_table_name_with_octopus(value = nil, &block)
|
199
|
+
self.custom_octopus_table_name = true
|
200
|
+
set_table_name_without_octopus(value, &block)
|
105
201
|
end
|
106
|
-
end
|
107
|
-
end
|
108
|
-
|
109
|
-
module InstanceMethods
|
110
|
-
include SharedMethods
|
111
202
|
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
base.send(:alias_method, :eql?, :==)
|
116
|
-
base.send(:alias_method_chain, :perform_validations, :octopus)
|
117
|
-
end
|
118
|
-
|
119
|
-
def should_set_current_shard?
|
120
|
-
self.respond_to?(:current_shard) && !self.current_shard.nil?
|
121
|
-
end
|
122
|
-
|
123
|
-
def run_on_shard(&block)
|
124
|
-
if self.current_shard
|
125
|
-
self.class.connection_proxy.run_queries_on_shard(self.current_shard, &block)
|
126
|
-
else
|
127
|
-
yield
|
128
|
-
end
|
129
|
-
end
|
130
|
-
|
131
|
-
def equality_with_octopus(comparison_object)
|
132
|
-
equality_without_octopus(comparison_object) && comparison_object.current_shard == current_shard
|
133
|
-
end
|
134
|
-
|
135
|
-
def perform_validations_with_octopus(*args)
|
136
|
-
if Octopus.enabled? and should_set_current_shard?
|
137
|
-
Octopus.using(self.current_shard) do
|
138
|
-
perform_validations_without_octopus(*args)
|
139
|
-
end
|
140
|
-
else
|
141
|
-
perform_validations_without_octopus(*args)
|
203
|
+
def octopus_establish_connection(spec = ENV['DATABASE_URL'])
|
204
|
+
self.custom_octopus_connection = true if spec
|
205
|
+
establish_connection(spec)
|
142
206
|
end
|
143
|
-
end
|
144
|
-
end
|
145
|
-
|
146
|
-
module ClassMethods
|
147
|
-
include SharedMethods
|
148
|
-
|
149
|
-
def self.extended(base)
|
150
|
-
base.class_attribute(:replicated)
|
151
|
-
base.class_attribute(:sharded)
|
152
|
-
base.hijack_methods
|
153
|
-
end
|
154
|
-
|
155
|
-
def replicated_model
|
156
|
-
self.replicated = true
|
157
|
-
end
|
158
|
-
|
159
|
-
def sharded_model
|
160
|
-
self.sharded = true
|
161
|
-
end
|
162
207
|
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
attr_accessor :custom_octopus_table_name
|
167
|
-
|
168
|
-
if Octopus.rails3?
|
169
|
-
alias_method_chain(:set_table_name, :octopus)
|
170
|
-
end
|
171
|
-
|
172
|
-
def table_name=(value = nil)
|
173
|
-
self.custom_octopus_table_name = true
|
174
|
-
super
|
175
|
-
end
|
208
|
+
def octopus_set_table_name(value = nil)
|
209
|
+
ActiveSupport::Deprecation.warn 'Calling `octopus_set_table_name` is deprecated and will be removed in Octopus 1.0.', caller
|
210
|
+
set_table_name(value)
|
176
211
|
end
|
177
212
|
end
|
178
|
-
|
179
|
-
def set_table_name_with_octopus(value = nil, &block)
|
180
|
-
self.custom_octopus_table_name = true
|
181
|
-
set_table_name_without_octopus(value, &block)
|
182
|
-
end
|
183
|
-
|
184
|
-
def octopus_establish_connection(spec = ENV['DATABASE_URL'])
|
185
|
-
self.custom_octopus_connection = true if spec
|
186
|
-
establish_connection(spec)
|
187
|
-
end
|
188
|
-
|
189
|
-
def octopus_set_table_name(value = nil)
|
190
|
-
ActiveSupport::Deprecation.warn "Calling `octopus_set_table_name` is deprecated and will be removed in Octopus 1.0.", caller
|
191
|
-
set_table_name(value)
|
192
|
-
end
|
193
213
|
end
|
194
214
|
end
|
195
215
|
|
@@ -0,0 +1,45 @@
|
|
1
|
+
module Octopus
|
2
|
+
module Persistence
|
3
|
+
def update_attribute(*args)
|
4
|
+
run_on_shard { super }
|
5
|
+
end
|
6
|
+
|
7
|
+
def update_attributes(*args)
|
8
|
+
run_on_shard { super }
|
9
|
+
end
|
10
|
+
|
11
|
+
def update_attributes!(*args)
|
12
|
+
run_on_shard { super }
|
13
|
+
end
|
14
|
+
|
15
|
+
def reload(*args)
|
16
|
+
run_on_shard { super }
|
17
|
+
end
|
18
|
+
|
19
|
+
def delete
|
20
|
+
run_on_shard { super }
|
21
|
+
end
|
22
|
+
|
23
|
+
def destroy
|
24
|
+
run_on_shard { super }
|
25
|
+
end
|
26
|
+
|
27
|
+
def touch(*args)
|
28
|
+
run_on_shard { super }
|
29
|
+
end
|
30
|
+
|
31
|
+
def update_column(*args)
|
32
|
+
run_on_shard { super }
|
33
|
+
end
|
34
|
+
|
35
|
+
def increment!(*args)
|
36
|
+
run_on_shard { super }
|
37
|
+
end
|
38
|
+
|
39
|
+
def decrement!(*args)
|
40
|
+
run_on_shard { super }
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
ActiveRecord::Base.send(:include, Octopus::Persistence)
|