ar-octopus 0.8.1 → 0.8.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/README.mkdn +80 -55
- data/lib/octopus.rb +20 -6
- data/lib/octopus/{rails3/abstract_adapter.rb → abstract_adapter.rb} +1 -4
- data/lib/octopus/association.rb +5 -99
- data/lib/octopus/association_shard_tracking.rb +105 -0
- data/lib/octopus/collection_association.rb +9 -0
- data/lib/octopus/collection_proxy.rb +14 -0
- data/lib/octopus/has_and_belongs_to_many_association.rb +2 -12
- data/lib/octopus/load_balancing.rb +3 -0
- data/lib/octopus/load_balancing/round_robin.rb +15 -0
- data/lib/octopus/{rails3/log_subscriber.rb → log_subscriber.rb} +0 -0
- data/lib/octopus/model.rb +74 -85
- data/lib/octopus/{rails3/persistence.rb → persistence.rb} +0 -0
- data/lib/octopus/proxy.rb +166 -29
- data/lib/octopus/relation_proxy.rb +39 -0
- data/lib/octopus/scope_proxy.rb +7 -10
- data/lib/octopus/shard_tracking.rb +45 -0
- data/lib/octopus/shard_tracking/attribute.rb +24 -0
- data/lib/octopus/shard_tracking/dynamic.rb +7 -0
- data/lib/octopus/singular_association.rb +7 -0
- data/lib/octopus/slave_group.rb +11 -0
- data/lib/octopus/version.rb +1 -1
- data/spec/config/shards.yml +53 -0
- data/spec/octopus/{association_spec.rb → association_shard_tracking_spec.rb} +1 -1
- data/spec/octopus/collection_proxy_spec.rb +15 -0
- data/spec/octopus/model_spec.rb +2 -2
- data/spec/octopus/octopus_spec.rb +34 -0
- data/spec/octopus/relation_proxy_spec.rb +77 -0
- data/spec/octopus/replicated_slave_grouped_spec.rb +64 -0
- data/spec/octopus/sharded_replicated_slave_grouped_spec.rb +55 -0
- data/spec/support/octopus_helper.rb +1 -0
- metadata +26 -9
- data/lib/octopus/association_collection.rb +0 -49
- data/lib/octopus/rails3/singular_association.rb +0 -34
@@ -0,0 +1,55 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe "when the database is both sharded and replicated" do
|
4
|
+
|
5
|
+
it "should pick the shard based on current_shard when you have a sharded model" do
|
6
|
+
|
7
|
+
OctopusHelper.using_environment :sharded_replicated_slave_grouped do
|
8
|
+
Octopus.using(:russia) do
|
9
|
+
Cat.create!(:name => "Thiago1")
|
10
|
+
Cat.create!(:name => "Thiago2")
|
11
|
+
end
|
12
|
+
|
13
|
+
# We must stub here to make it effective (not in the `before(:each)` block)
|
14
|
+
Octopus.stub(:env).and_return("sharded_replicated_slave_grouped")
|
15
|
+
|
16
|
+
Cat.using(:russia).count.should == 2
|
17
|
+
# It distributes queries between two slaves in the slave group
|
18
|
+
Cat.using(shard: :russia, slave_group: :slaves1).count.should == 0
|
19
|
+
Cat.using(shard: :russia, slave_group: :slaves1).count.should == 2
|
20
|
+
Cat.using(shard: :russia, slave_group: :slaves1).count.should == 0
|
21
|
+
# It distributes queries between two slaves in the slave group
|
22
|
+
Cat.using(shard: :russia, slave_group: :slaves2).count.should == 2
|
23
|
+
Cat.using(shard: :russia, slave_group: :slaves2).count.should == 0
|
24
|
+
Cat.using(shard: :russia, slave_group: :slaves2).count.should == 2
|
25
|
+
|
26
|
+
Cat.using(:europe).count.should == 0
|
27
|
+
Cat.using(shard: :europe, slave_group: :slaves1)
|
28
|
+
.count.should == 0
|
29
|
+
Cat.using(shard: :europe, slave_group: :slaves2)
|
30
|
+
.count.should == 2
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should make queries to master when slave groups are configured for the shard but not selected" do
|
35
|
+
OctopusHelper.using_environment :sharded_replicated_slave_grouped do
|
36
|
+
Octopus.using(:europe) do
|
37
|
+
# All the queries go to :master(`octopus_shard_1`)
|
38
|
+
|
39
|
+
Cat.create!(:name => "Thiago1")
|
40
|
+
Cat.create!(:name => "Thiago2")
|
41
|
+
|
42
|
+
# In `database.yml` and `shards.yml`, we have configured 1 master and 6 slaves for `sharded_replicated_slave_grouped`
|
43
|
+
# So we can ensure Octopus is not distributing queries between them
|
44
|
+
# by asserting 1 + 6 = 7 queries go to :master(`octopus_shard_1`)
|
45
|
+
Cat.count.should == 2
|
46
|
+
Cat.count.should == 2
|
47
|
+
Cat.count.should == 2
|
48
|
+
Cat.count.should == 2
|
49
|
+
Cat.count.should == 2
|
50
|
+
Cat.count.should == 2
|
51
|
+
Cat.count.should == 2
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -21,6 +21,7 @@ module OctopusHelper
|
|
21
21
|
Thread.current["octopus.current_model"] = nil
|
22
22
|
Thread.current["octopus.current_shard"] = nil
|
23
23
|
Thread.current["octopus.current_group"] = nil
|
24
|
+
Thread.current["octopus.current_slave_group"] = nil
|
24
25
|
Thread.current["octopus.block"] = nil
|
25
26
|
Thread.current["octopus.last_current_shard"] = nil
|
26
27
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ar-octopus
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.8.
|
4
|
+
version: 0.8.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Thiago Pradi
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2014-
|
13
|
+
date: 2014-07-03 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: activerecord
|
@@ -164,18 +164,27 @@ files:
|
|
164
164
|
- init.rb
|
165
165
|
- lib/ar-octopus.rb
|
166
166
|
- lib/octopus.rb
|
167
|
+
- lib/octopus/abstract_adapter.rb
|
167
168
|
- lib/octopus/association.rb
|
168
|
-
- lib/octopus/
|
169
|
+
- lib/octopus/association_shard_tracking.rb
|
170
|
+
- lib/octopus/collection_association.rb
|
171
|
+
- lib/octopus/collection_proxy.rb
|
169
172
|
- lib/octopus/has_and_belongs_to_many_association.rb
|
173
|
+
- lib/octopus/load_balancing.rb
|
174
|
+
- lib/octopus/load_balancing/round_robin.rb
|
175
|
+
- lib/octopus/log_subscriber.rb
|
170
176
|
- lib/octopus/migration.rb
|
171
177
|
- lib/octopus/model.rb
|
178
|
+
- lib/octopus/persistence.rb
|
172
179
|
- lib/octopus/proxy.rb
|
173
|
-
- lib/octopus/rails3/abstract_adapter.rb
|
174
|
-
- lib/octopus/rails3/log_subscriber.rb
|
175
|
-
- lib/octopus/rails3/persistence.rb
|
176
|
-
- lib/octopus/rails3/singular_association.rb
|
177
180
|
- lib/octopus/railtie.rb
|
181
|
+
- lib/octopus/relation_proxy.rb
|
178
182
|
- lib/octopus/scope_proxy.rb
|
183
|
+
- lib/octopus/shard_tracking.rb
|
184
|
+
- lib/octopus/shard_tracking/attribute.rb
|
185
|
+
- lib/octopus/shard_tracking/dynamic.rb
|
186
|
+
- lib/octopus/singular_association.rb
|
187
|
+
- lib/octopus/slave_group.rb
|
179
188
|
- lib/octopus/version.rb
|
180
189
|
- lib/tasks/octopus.rake
|
181
190
|
- rails/init.rb
|
@@ -270,14 +279,18 @@ files:
|
|
270
279
|
- spec/migrations/7_raise_exception_with_invalid_multiple_shard_names.rb
|
271
280
|
- spec/migrations/8_raise_exception_with_invalid_group_name.rb
|
272
281
|
- spec/migrations/9_raise_exception_with_multiple_invalid_group_names.rb
|
273
|
-
- spec/octopus/
|
282
|
+
- spec/octopus/association_shard_tracking_spec.rb
|
283
|
+
- spec/octopus/collection_proxy_spec.rb
|
274
284
|
- spec/octopus/log_subscriber_spec.rb
|
275
285
|
- spec/octopus/migration_spec.rb
|
276
286
|
- spec/octopus/model_spec.rb
|
277
287
|
- spec/octopus/octopus_spec.rb
|
278
288
|
- spec/octopus/proxy_spec.rb
|
289
|
+
- spec/octopus/relation_proxy_spec.rb
|
290
|
+
- spec/octopus/replicated_slave_grouped_spec.rb
|
279
291
|
- spec/octopus/replication_spec.rb
|
280
292
|
- spec/octopus/scope_proxy_spec.rb
|
293
|
+
- spec/octopus/sharded_replicated_slave_grouped_spec.rb
|
281
294
|
- spec/octopus/sharded_spec.rb
|
282
295
|
- spec/spec_helper.rb
|
283
296
|
- spec/support/active_record/connection_adapters/modify_config_adapter.rb
|
@@ -335,14 +348,18 @@ test_files:
|
|
335
348
|
- spec/migrations/7_raise_exception_with_invalid_multiple_shard_names.rb
|
336
349
|
- spec/migrations/8_raise_exception_with_invalid_group_name.rb
|
337
350
|
- spec/migrations/9_raise_exception_with_multiple_invalid_group_names.rb
|
338
|
-
- spec/octopus/
|
351
|
+
- spec/octopus/association_shard_tracking_spec.rb
|
352
|
+
- spec/octopus/collection_proxy_spec.rb
|
339
353
|
- spec/octopus/log_subscriber_spec.rb
|
340
354
|
- spec/octopus/migration_spec.rb
|
341
355
|
- spec/octopus/model_spec.rb
|
342
356
|
- spec/octopus/octopus_spec.rb
|
343
357
|
- spec/octopus/proxy_spec.rb
|
358
|
+
- spec/octopus/relation_proxy_spec.rb
|
359
|
+
- spec/octopus/replicated_slave_grouped_spec.rb
|
344
360
|
- spec/octopus/replication_spec.rb
|
345
361
|
- spec/octopus/scope_proxy_spec.rb
|
362
|
+
- spec/octopus/sharded_replicated_slave_grouped_spec.rb
|
346
363
|
- spec/octopus/sharded_spec.rb
|
347
364
|
- spec/spec_helper.rb
|
348
365
|
- spec/support/active_record/connection_adapters/modify_config_adapter.rb
|
@@ -1,49 +0,0 @@
|
|
1
|
-
module Octopus::AssociationCollection
|
2
|
-
|
3
|
-
METHODS = %w[
|
4
|
-
reader
|
5
|
-
writer
|
6
|
-
ids_reader
|
7
|
-
ids_writer
|
8
|
-
create
|
9
|
-
create!
|
10
|
-
build
|
11
|
-
any?
|
12
|
-
count
|
13
|
-
empty?
|
14
|
-
first
|
15
|
-
include?
|
16
|
-
last
|
17
|
-
length
|
18
|
-
load_target
|
19
|
-
many?
|
20
|
-
size
|
21
|
-
select
|
22
|
-
uniq
|
23
|
-
]
|
24
|
-
|
25
|
-
def self.included(base)
|
26
|
-
base.instance_eval do
|
27
|
-
METHODS.each do |m|
|
28
|
-
alias_method_chain m.to_sym, :octopus
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
METHODS.each do |m|
|
34
|
-
m =~ /([^!?]+)([!?])?/
|
35
|
-
method, punctuation = [ $1, $2 ]
|
36
|
-
with = :"#{method}_with_octopus#{punctuation}"
|
37
|
-
without = :"#{method}_without_octopus#{punctuation}"
|
38
|
-
define_method with do |*args, &block|
|
39
|
-
@owner.run_on_shard { send(without, *args, &block) }
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
|
-
def should_wrap_the_connection?
|
44
|
-
@owner.respond_to?(:current_shard) && @owner.current_shard != nil
|
45
|
-
end
|
46
|
-
|
47
|
-
end
|
48
|
-
|
49
|
-
ActiveRecord::Associations::CollectionAssociation.send(:include, Octopus::AssociationCollection)
|
@@ -1,34 +0,0 @@
|
|
1
|
-
module Octopus::SingularAssociation
|
2
|
-
def self.included(base)
|
3
|
-
base.instance_eval do
|
4
|
-
alias_method_chain :reader, :octopus
|
5
|
-
alias_method_chain :writer, :octopus
|
6
|
-
alias_method_chain :create, :octopus
|
7
|
-
alias_method_chain :create!, :octopus
|
8
|
-
alias_method_chain :build, :octopus
|
9
|
-
end
|
10
|
-
end
|
11
|
-
|
12
|
-
def reader_with_octopus(*args)
|
13
|
-
owner.run_on_shard { reader_without_octopus(*args) }
|
14
|
-
end
|
15
|
-
|
16
|
-
def writer_with_octopus(*args)
|
17
|
-
owner.run_on_shard { writer_without_octopus(*args) }
|
18
|
-
end
|
19
|
-
|
20
|
-
def create_with_octopus(*args)
|
21
|
-
owner.run_on_shard { create_without_octopus(*args) }
|
22
|
-
end
|
23
|
-
|
24
|
-
def create_with_octopus!(*args)
|
25
|
-
owner.run_on_shard { create_without_octopus!(*args) }
|
26
|
-
end
|
27
|
-
|
28
|
-
def build_with_octopus(*args)
|
29
|
-
owner.run_on_shard { build_without_octopus(*args) }
|
30
|
-
end
|
31
|
-
|
32
|
-
end
|
33
|
-
|
34
|
-
ActiveRecord::Associations::SingularAssociation.send(:include, Octopus::SingularAssociation)
|