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
@@ -0,0 +1,132 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Octopus::RelationProxy do
|
4
|
+
describe 'shard tracking' do
|
5
|
+
before :each do
|
6
|
+
@client = Client.using(:canada).create!
|
7
|
+
@client.items << Item.using(:canada).create!
|
8
|
+
@relation = @client.items
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'remembers the shard on which a relation was created' do
|
12
|
+
expect(@relation.current_shard).to eq(:canada)
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'can define collection association with the same name as ancestor private method' do
|
16
|
+
@client.comments << Comment.using(:canada).create!(open: true)
|
17
|
+
expect(@client.comments.open).to be_a_kind_of(ActiveRecord::Relation)
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'can be dumped and loaded' do
|
21
|
+
expect(Marshal.load(Marshal.dump(@relation))).to eq @relation
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'maintains the current shard when using where.not(...)' do
|
25
|
+
where_chain = @relation.where
|
26
|
+
expect(where_chain.current_shard).to eq(@relation.current_shard)
|
27
|
+
not_relation = where_chain.not("1=0")
|
28
|
+
expect(not_relation.current_shard).to eq(@relation.current_shard)
|
29
|
+
end
|
30
|
+
|
31
|
+
context 'when a new relation is constructed from the original relation' do
|
32
|
+
context 'and a where(...) is used' do
|
33
|
+
it 'does not tamper with the original relation' do
|
34
|
+
relation = Item.using(:canada).where(id: 1)
|
35
|
+
original_sql = relation.to_sql
|
36
|
+
new_relation = relation.where(id: 2)
|
37
|
+
expect(relation.to_sql).to eq(original_sql)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
context 'and a where.not(...) is used' do
|
42
|
+
it 'does not tamper with the original relation' do
|
43
|
+
relation = Item.using(:canada).where(id: 1)
|
44
|
+
original_sql = relation.to_sql
|
45
|
+
new_relation = relation.where.not(id: 2)
|
46
|
+
expect(relation.to_sql).to eq(original_sql)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
context 'when comparing to other Relation objects' do
|
52
|
+
before :each do
|
53
|
+
@relation.reset
|
54
|
+
end
|
55
|
+
|
56
|
+
it 'is equal to its clone' do
|
57
|
+
expect(@relation).to eq(@relation.clone)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
it "can deliver methods in ActiveRecord::Batches correctly when given a block" do
|
62
|
+
expect { @relation.find_each(&:inspect) }.not_to raise_error
|
63
|
+
end
|
64
|
+
|
65
|
+
it "can deliver methods in ActiveRecord::Batches correctly as an enumerator" do
|
66
|
+
expect { @relation.find_each.each(&:inspect) }.not_to raise_error
|
67
|
+
end
|
68
|
+
|
69
|
+
it "can deliver methods in ActiveRecord::Batches correctly as a lazy enumerator" do
|
70
|
+
expect { @relation.find_each.lazy.each(&:inspect) }.not_to raise_error
|
71
|
+
end
|
72
|
+
|
73
|
+
context 'under Rails 4' do
|
74
|
+
it 'is an Octopus::RelationProxy' do
|
75
|
+
expect{@relation.ar_relation}.not_to raise_error
|
76
|
+
end
|
77
|
+
|
78
|
+
it 'should be able to return its ActiveRecord::Relation' do
|
79
|
+
expect(@relation.ar_relation.is_a?(ActiveRecord::Relation)).to be true
|
80
|
+
end
|
81
|
+
|
82
|
+
it 'is equal to an identically-defined, but different, RelationProxy' do
|
83
|
+
i = @client.items
|
84
|
+
expect(@relation).to eq(i)
|
85
|
+
expect(@relation.__id__).not_to eq(i.__id__)
|
86
|
+
end
|
87
|
+
|
88
|
+
it 'is equal to its own underlying ActiveRecord::Relation' do
|
89
|
+
expect(@relation).to eq(@relation.ar_relation)
|
90
|
+
expect(@relation.ar_relation).to eq(@relation)
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
context 'when no explicit shard context is provided' do
|
95
|
+
it 'uses the correct shard' do
|
96
|
+
expect(@relation.count).to eq(1)
|
97
|
+
end
|
98
|
+
|
99
|
+
it 'lazily evaluates on the correct shard' do
|
100
|
+
# Do something to force Client.connection_proxy.current_shard to change
|
101
|
+
_some_count = Client.using(:brazil).count
|
102
|
+
expect(@relation.select(:client_id).count).to eq(1)
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
context 'when an explicit, but different, shard context is provided' do
|
107
|
+
it 'uses the correct shard' do
|
108
|
+
expect(Item.using(:brazil).count).to eq(0)
|
109
|
+
_clients_on_brazil = Client.using(:brazil).all
|
110
|
+
Octopus.using(:brazil) do
|
111
|
+
expect(@relation.count).to eq(1)
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
it 'uses the correct shard in block when method_missing is triggered on CollectionProxy objects' do
|
116
|
+
Octopus.using(:brazil) do
|
117
|
+
@client.items.each do |item|
|
118
|
+
expect(item.current_shard).to eq(:canada)
|
119
|
+
expect(ActiveRecord::Base.connection.current_shard).to eq(:brazil)
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
it 'lazily evaluates on the correct shard' do
|
125
|
+
expect(Item.using(:brazil).count).to eq(0)
|
126
|
+
Octopus.using(:brazil) do
|
127
|
+
expect(@relation.select(:client_id).count).to eq(1)
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
@@ -0,0 +1,91 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'when the database is replicated and has slave groups' do
|
4
|
+
|
5
|
+
it 'should pick the slave group based on current_slave_grup when you have a replicated model' do
|
6
|
+
|
7
|
+
OctopusHelper.using_environment :replicated_slave_grouped do
|
8
|
+
# The following two calls of `create!` both creates cats in :master(The database `octopus_shard_1`)
|
9
|
+
# which is configured through RAILS_ENV and database.yml
|
10
|
+
Cat.create!(:name => 'Thiago1')
|
11
|
+
Cat.create!(:name => 'Thiago2')
|
12
|
+
|
13
|
+
# See "replicated_slave_grouped" defined in shards.yml
|
14
|
+
# We have:
|
15
|
+
# The database `octopus_shard_1` as :slave21 which is a member of the slave group :slaves2, and as :master
|
16
|
+
# The databse `octopus_shard_2` as :slave11 which is a member of the slave group :slaves1
|
17
|
+
# When a select-count query is sent to `octopus_shard_1`, it should return 2 because we have create two cats in :master .
|
18
|
+
# When a select-count query is sent to `octopus_shard_2`, it should return 0.
|
19
|
+
|
20
|
+
# The query goes to `octopus_shard_1`
|
21
|
+
expect(Cat.using(:master).count).to eq(2)
|
22
|
+
# The query goes to `octopus_shard_1`
|
23
|
+
expect(Cat.count).to eq(2)
|
24
|
+
# The query goes to `octopus_shard_2`
|
25
|
+
expect(Cat.using(:slave_group => :slaves1).count).to eq(0)
|
26
|
+
# The query goes to `octopus_shard_1`
|
27
|
+
expect(Cat.using(:slave_group => :slaves2).count).to eq(2)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'should distribute queries between slaves in a slave group in round-robin' do
|
32
|
+
OctopusHelper.using_environment :replicated_slave_grouped do
|
33
|
+
# The query goes to :master(`octopus_shard_1`)
|
34
|
+
Cat.create!(:name => 'Thiago1')
|
35
|
+
# The query goes to :master(`octopus_shard_1`)
|
36
|
+
Cat.create!(:name => 'Thiago2')
|
37
|
+
|
38
|
+
# The query goes to :slave32(`octopus_shard_2`)
|
39
|
+
expect(Cat.using(:slave_group => :slaves3).count).to eq(0)
|
40
|
+
# The query goes to :slave31(`octopus_shard_1`)
|
41
|
+
expect(Cat.using(:slave_group => :slaves3).count).to eq(2)
|
42
|
+
# The query goes to :slave32(`octopus_shard_2`)
|
43
|
+
expect(Cat.using(:slave_group => :slaves3).count).to eq(0)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'should make queries to master when slave groups are configured but not selected' do
|
48
|
+
OctopusHelper.using_environment :replicated_slave_grouped do
|
49
|
+
# All the queries go to :master(`octopus_shard_1`)
|
50
|
+
|
51
|
+
Cat.create!(:name => 'Thiago1')
|
52
|
+
Cat.create!(:name => 'Thiago2')
|
53
|
+
|
54
|
+
# In `database.yml` and `shards.yml`, we have configured 1 master and 4 slaves.
|
55
|
+
# So we can ensure Octopus is not distributing queries between them
|
56
|
+
# by asserting 1 + 4 = 5 queries go to :master(`octopus_shard_1`)
|
57
|
+
expect(Cat.count).to eq(2)
|
58
|
+
expect(Cat.count).to eq(2)
|
59
|
+
expect(Cat.count).to eq(2)
|
60
|
+
expect(Cat.count).to eq(2)
|
61
|
+
expect(Cat.count).to eq(2)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
it 'should keep sending to slaves in a using block' do
|
66
|
+
OctopusHelper.using_environment :replicated_slave_grouped do
|
67
|
+
Cat.create!(:name => 'Thiago1')
|
68
|
+
Cat.create!(:name => 'Thiago2')
|
69
|
+
|
70
|
+
expect(Cat.count).to eq(2)
|
71
|
+
Octopus.using(:slave_group => :slaves1) do
|
72
|
+
expect(Cat.count).to eq(0)
|
73
|
+
expect(Cat.count).to eq(0)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
it 'should restore previous slave group after a using block' do
|
79
|
+
OctopusHelper.using_environment :replicated_slave_grouped do
|
80
|
+
Cat.create!(:name => 'Thiago1')
|
81
|
+
Cat.create!(:name => 'Thiago2')
|
82
|
+
|
83
|
+
Octopus.using(:slave_group => :slaves1) do
|
84
|
+
Octopus.using(:slave_group => :slaves2) do
|
85
|
+
expect(Cat.count).to eq(2)
|
86
|
+
end
|
87
|
+
expect(Cat.count).to eq(0)
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
@@ -1,69 +1,138 @@
|
|
1
|
-
require
|
1
|
+
require 'spec_helper'
|
2
2
|
|
3
|
-
describe
|
4
|
-
|
3
|
+
describe 'when the database is replicated' do
|
4
|
+
let(:slave_pool) do
|
5
|
+
ActiveRecord::Base.connection_proxy.shards['slave1']
|
6
|
+
end
|
7
|
+
|
8
|
+
let(:slave_connection) do
|
9
|
+
slave_pool.connection
|
10
|
+
end
|
11
|
+
|
12
|
+
let(:master_pool) do
|
13
|
+
ActiveRecord::Base.connection_proxy.shards['master']
|
14
|
+
end
|
15
|
+
|
16
|
+
let(:master_connection) do
|
17
|
+
master_pool.connection
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'should send all writes/reads queries to master when you have a non replicated model' do
|
5
21
|
OctopusHelper.using_environment :production_replicated do
|
6
|
-
u = User.create!(:name =>
|
7
|
-
User.count.
|
8
|
-
User.find(u.id).
|
22
|
+
u = User.create!(:name => 'Replicated')
|
23
|
+
expect(User.count).to eq(1)
|
24
|
+
expect(User.find(u.id)).to eq(u)
|
9
25
|
end
|
10
26
|
end
|
11
27
|
|
12
|
-
it
|
28
|
+
it 'should send all writes queries to master' do
|
13
29
|
OctopusHelper.using_environment :production_replicated do
|
14
|
-
Cat.create!(:name =>
|
15
|
-
Cat.find_by_name(
|
16
|
-
Client.create!(:name =>
|
17
|
-
Client.find_by_name(
|
30
|
+
Cat.create!(:name => 'Slave Cat')
|
31
|
+
expect(Cat.find_by_name('Slave Cat')).to be_nil
|
32
|
+
Client.create!(:name => 'Slave Client')
|
33
|
+
expect(Client.find_by_name('Slave Client')).not_to be_nil
|
18
34
|
end
|
19
35
|
end
|
20
36
|
|
21
|
-
it
|
37
|
+
it 'should allow to create multiple models on the master' do
|
22
38
|
OctopusHelper.using_environment :production_replicated do
|
23
|
-
Cat.create!([{:name =>
|
24
|
-
Cat.find_by_name(
|
25
|
-
Cat.find_by_name(
|
39
|
+
Cat.create!([{ :name => 'Slave Cat 1' }, { :name => 'Slave Cat 2' }])
|
40
|
+
expect(Cat.find_by_name('Slave Cat 1')).to be_nil
|
41
|
+
expect(Cat.find_by_name('Slave Cat 2')).to be_nil
|
26
42
|
end
|
27
43
|
end
|
28
44
|
|
29
|
-
|
30
|
-
|
45
|
+
context 'when updating model' do
|
46
|
+
it 'should send writes to master' do
|
47
|
+
OctopusHelper.using_environment :replicated_with_one_slave do
|
48
|
+
Cat.using(:slave1).create!(:name => 'Cat')
|
49
|
+
cat = Cat.find_by_name('Cat')
|
50
|
+
cat.name = 'New name'
|
51
|
+
|
52
|
+
expect(master_connection).to receive(:update).and_call_original
|
31
53
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
Cat.using(:master).find(cat1.id).should eq(cat1)
|
37
|
-
Cat.using(:master).find(cat1.id).should eq(cat1)
|
38
|
-
Cat.using(:master).find(cat1.id).should eq(cat1)
|
54
|
+
cat.save!
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
39
58
|
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
Cat.find(cat3.id).id.should eq(cat3.id)
|
59
|
+
context 'when querying' do
|
60
|
+
it 'Reads from slave' do
|
61
|
+
OctopusHelper.using_environment :replicated_with_one_slave do
|
62
|
+
expect(master_connection).not_to receive(:select)
|
45
63
|
|
46
|
-
|
64
|
+
Cat.where(:name => 'Catman2').first
|
47
65
|
end
|
48
66
|
end
|
49
67
|
end
|
50
68
|
|
51
|
-
|
52
|
-
|
69
|
+
context 'When record is read from slave' do
|
70
|
+
it 'Should write associations to master' do
|
71
|
+
OctopusHelper.using_environment :replicated_with_one_slave do
|
72
|
+
client = Client.using(:slave1).create!(:name => 'Client')
|
73
|
+
|
74
|
+
client = Client.find(client.id)
|
75
|
+
|
76
|
+
expect(master_connection).to receive(:insert).and_call_original
|
77
|
+
|
78
|
+
client.items.create!(:name => 'Item')
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
|
84
|
+
describe 'When enabling the query cache' do
|
85
|
+
include_context 'with query cache enabled' do
|
86
|
+
it 'should do the queries with cache' do
|
87
|
+
OctopusHelper.using_environment :replicated_with_one_slave do
|
88
|
+
cat1 = Cat.using(:master).create!(:name => 'Master Cat 1')
|
89
|
+
_ct2 = Cat.using(:master).create!(:name => 'Master Cat 2')
|
90
|
+
expect(Cat.using(:master).find(cat1.id)).to eq(cat1)
|
91
|
+
expect(Cat.using(:master).find(cat1.id)).to eq(cat1)
|
92
|
+
expect(Cat.using(:master).find(cat1.id)).to eq(cat1)
|
93
|
+
|
94
|
+
cat3 = Cat.using(:slave1).create!(:name => 'Slave Cat 3')
|
95
|
+
_ct4 = Cat.using(:slave1).create!(:name => 'Slave Cat 4')
|
96
|
+
expect(Cat.find(cat3.id).id).to eq(cat3.id)
|
97
|
+
expect(Cat.find(cat3.id).id).to eq(cat3.id)
|
98
|
+
expect(Cat.find(cat3.id).id).to eq(cat3.id)
|
99
|
+
|
100
|
+
# Rails 5.1 count the cached queries as regular queries.
|
101
|
+
# TODO: How we can verify if the queries are using cache on Rails 5.1? - @thiagopradi
|
102
|
+
expected_records = Octopus.rails51? || Octopus.rails52? ? 19 : 14
|
103
|
+
|
104
|
+
expect(counter.query_count).to eq(expected_records)
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
describe 'When enabling the query cache with slave unavailable' do
|
111
|
+
it "should not raise can't connect error" do
|
112
|
+
OctopusHelper.using_environment :replicated_with_one_slave_unavailable do
|
113
|
+
expect {
|
114
|
+
ActiveRecord::Base.connection.enable_query_cache!
|
115
|
+
}.to_not raise_error
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
it 'should allow #using syntax to send queries to master' do
|
121
|
+
Cat.create!(:name => 'Master Cat')
|
53
122
|
|
54
123
|
OctopusHelper.using_environment :production_fully_replicated do
|
55
|
-
Cat.using(:master).find_by_name(
|
124
|
+
expect(Cat.using(:master).find_by_name('Master Cat')).not_to be_nil
|
56
125
|
end
|
57
126
|
end
|
58
127
|
|
59
|
-
it
|
128
|
+
it 'should send the count query to a slave' do
|
60
129
|
OctopusHelper.using_environment :production_replicated do
|
61
|
-
Cat.create!(:name =>
|
62
|
-
Cat.count.
|
130
|
+
Cat.create!(:name => 'Slave Cat')
|
131
|
+
expect(Cat.count).to eq(0)
|
63
132
|
end
|
64
133
|
end
|
65
134
|
|
66
|
-
def active_support_subscribed(callback, *args, &
|
135
|
+
def active_support_subscribed(callback, *args, &_block)
|
67
136
|
subscriber = ActiveSupport::Notifications.subscribe(*args, &callback)
|
68
137
|
yield
|
69
138
|
ensure
|
@@ -71,51 +140,57 @@ describe "when the database is replicated" do
|
|
71
140
|
end
|
72
141
|
end
|
73
142
|
|
74
|
-
|
75
|
-
describe "when the database is replicated and the entire application is replicated" do
|
143
|
+
describe 'when the database is replicated and the entire application is replicated' do
|
76
144
|
before(:each) do
|
77
|
-
Octopus.
|
78
|
-
OctopusHelper.clean_connection_proxy
|
145
|
+
allow(Octopus).to receive(:env).and_return('production_fully_replicated')
|
146
|
+
OctopusHelper.clean_connection_proxy
|
79
147
|
end
|
80
148
|
|
81
|
-
it
|
149
|
+
it 'should send all writes queries to master' do
|
82
150
|
OctopusHelper.using_environment :production_fully_replicated do
|
83
|
-
Cat.create!(:name =>
|
84
|
-
Cat.find_by_name(
|
85
|
-
Client.create!(:name =>
|
86
|
-
Client.find_by_name(
|
151
|
+
Cat.create!(:name => 'Slave Cat')
|
152
|
+
expect(Cat.find_by_name('Slave Cat')).to be_nil
|
153
|
+
Client.create!(:name => 'Slave Client')
|
154
|
+
expect(Client.find_by_name('Slave Client')).to be_nil
|
87
155
|
end
|
88
156
|
end
|
89
157
|
|
90
|
-
it
|
158
|
+
it 'should send all writes queries to master' do
|
91
159
|
OctopusHelper.using_environment :production_fully_replicated do
|
92
|
-
Cat.create!(:name =>
|
93
|
-
Cat.find_by_name(
|
94
|
-
Client.create!(:name =>
|
95
|
-
Client.find_by_name(
|
160
|
+
Cat.create!(:name => 'Slave Cat')
|
161
|
+
expect(Cat.find_by_name('Slave Cat')).to be_nil
|
162
|
+
Client.create!(:name => 'Slave Client')
|
163
|
+
expect(Client.find_by_name('Slave Client')).to be_nil
|
96
164
|
end
|
97
165
|
end
|
98
166
|
|
99
|
-
it
|
100
|
-
Keyboard.create!(:name =>
|
167
|
+
it 'should work with validate_uniquess_of' do
|
168
|
+
Keyboard.create!(:name => 'thiago')
|
101
169
|
|
102
170
|
OctopusHelper.using_environment :production_fully_replicated do
|
103
|
-
k = Keyboard.new(:name =>
|
104
|
-
k.save.
|
105
|
-
k.errors.full_messages.
|
171
|
+
k = Keyboard.new(:name => 'thiago')
|
172
|
+
expect(k.save).to be false
|
173
|
+
expect(k.errors.full_messages).to eq(['Name has already been taken'])
|
106
174
|
end
|
107
175
|
end
|
108
176
|
|
109
|
-
it
|
177
|
+
it 'should reset current shard if slave throws an exception' do
|
110
178
|
OctopusHelper.using_environment :production_fully_replicated do
|
111
|
-
Cat.create!(:name =>
|
112
|
-
Cat.connection.current_shard.
|
113
|
-
|
114
|
-
|
115
|
-
rescue
|
116
|
-
end
|
117
|
-
Cat.connection.current_shard.should eql(:master)
|
179
|
+
Cat.create!(:name => 'Slave Cat')
|
180
|
+
expect(Cat.connection.current_shard).to eql(:master)
|
181
|
+
Cat.where(:rubbish => true)
|
182
|
+
expect(Cat.connection.current_shard).to eql(:master)
|
118
183
|
end
|
119
184
|
end
|
120
|
-
end
|
121
185
|
|
186
|
+
it 'should reset current shard if slave throws an exception with custom master' do
|
187
|
+
OctopusHelper.using_environment :production_fully_replicated do
|
188
|
+
Octopus.config[:master_shard] = :slave2
|
189
|
+
Cat.create!(:name => 'Slave Cat')
|
190
|
+
expect(Cat.connection.current_shard).to eql(:slave2)
|
191
|
+
Cat.where(:rubbish => true)
|
192
|
+
expect(Cat.connection.current_shard).to eql(:slave2)
|
193
|
+
Octopus.config[:master_shard] = nil
|
194
|
+
end
|
195
|
+
end
|
196
|
+
end
|
@@ -1,17 +1,97 @@
|
|
1
|
-
require
|
1
|
+
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Octopus::ScopeProxy do
|
4
|
-
it
|
5
|
-
@user1 = User.using(:brazil).create!(:name =>
|
6
|
-
@user2 = User.using(:brazil).create!(:name =>
|
7
|
-
@user3 = User.using(:brazil).create!(:name =>
|
8
|
-
|
9
|
-
User.using(:brazil).where(:name =>
|
10
|
-
User.using(:brazil).where(:name =>
|
11
|
-
User.using(:brazil).where(:name =>
|
4
|
+
it 'should allow nested queries' do
|
5
|
+
@user1 = User.using(:brazil).create!(:name => 'Thiago P', :number => 3)
|
6
|
+
@user2 = User.using(:brazil).create!(:name => 'Thiago', :number => 1)
|
7
|
+
@user3 = User.using(:brazil).create!(:name => 'Thiago', :number => 2)
|
8
|
+
|
9
|
+
expect(User.using(:brazil).where(:name => 'Thiago').where(:number => 4).order(:number).all).to eq([])
|
10
|
+
expect(User.using(:brazil).where(:name => 'Thiago').using(:canada).where(:number => 2).using(:brazil).order(:number).all).to eq([@user3])
|
11
|
+
expect(User.using(:brazil).where(:name => 'Thiago').using(:canada).where(:number => 4).using(:brazil).order(:number).all).to eq([])
|
12
|
+
end
|
13
|
+
|
14
|
+
context 'When array-like-selecting an item in a group' do
|
15
|
+
before(:each) do
|
16
|
+
User.using(:brazil).create!(:name => 'Evan', :number => 1)
|
17
|
+
User.using(:brazil).create!(:name => 'Evan', :number => 2)
|
18
|
+
User.using(:brazil).create!(:name => 'Evan', :number => 3)
|
19
|
+
@evans = User.using(:brazil).where(:name => 'Evan')
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'allows a block to select an item' do
|
23
|
+
expect(@evans.select { |u| u.number == 2 }.first.number).to eq(2)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
context 'When selecting a field within a scope' do
|
28
|
+
before(:each) do
|
29
|
+
User.using(:brazil).create!(:name => 'Evan', :number => 4)
|
30
|
+
@evan = User.using(:brazil).where(:name => 'Evan')
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'allows single field selection' do
|
34
|
+
expect(@evan.select('name').first.name).to eq('Evan')
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'allows selection by array' do
|
38
|
+
expect(@evan.select(['name']).first.name).to eq('Evan')
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'allows multiple selection by string' do
|
42
|
+
expect(@evan.select('id, name').first.id).to be_a(Fixnum)
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'allows multiple selection by array' do
|
46
|
+
expect(@evan.select(%w(id name)).first.id).to be_a(Fixnum)
|
47
|
+
end
|
48
|
+
|
49
|
+
it 'allows multiple selection by symbol' do
|
50
|
+
expect(@evan.select(:id, :name).first.id).to be_a(Fixnum)
|
51
|
+
end
|
52
|
+
|
53
|
+
it 'allows multiple selection by string and symbol' do
|
54
|
+
expect(@evan.select(:id, 'name').first.id).to be_a(Fixnum)
|
55
|
+
end
|
12
56
|
end
|
13
57
|
|
14
58
|
it "should raise a exception when trying to send a query to a shard that don't exists" do
|
15
|
-
|
59
|
+
expect { User.using(:dont_exists).all }.to raise_exception('Nonexistent Shard Name: dont_exists')
|
60
|
+
end
|
61
|
+
|
62
|
+
context "dup / clone" do
|
63
|
+
before(:each) do
|
64
|
+
User.using(:brazil).create!(:name => 'Thiago', :number => 1)
|
65
|
+
end
|
66
|
+
|
67
|
+
it "should change it's object id" do
|
68
|
+
user = User.using(:brazil).where(id: 1)
|
69
|
+
dupped_object = user.dup
|
70
|
+
cloned_object = user.clone
|
71
|
+
|
72
|
+
expect(dupped_object.object_id).not_to eq(user.object_id)
|
73
|
+
expect(cloned_object.object_id).not_to eq(user.object_id)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
context 'When iterated with Enumerable methods' do
|
78
|
+
before(:each) do
|
79
|
+
User.using(:brazil).create!(:name => 'Evan', :number => 1)
|
80
|
+
User.using(:brazil).create!(:name => 'Evan', :number => 2)
|
81
|
+
User.using(:brazil).create!(:name => 'Evan', :number => 3)
|
82
|
+
@evans = User.using(:brazil).where(:name => 'Evan')
|
83
|
+
end
|
84
|
+
|
85
|
+
it 'allows each method' do
|
86
|
+
expect(@evans.each.count).to eq(3)
|
87
|
+
end
|
88
|
+
|
89
|
+
it 'allows each_with_index method' do
|
90
|
+
expect(@evans.each_with_index.to_a.flatten.count).to eq(6)
|
91
|
+
end
|
92
|
+
|
93
|
+
it 'allows map method' do
|
94
|
+
expect(@evans.map(&:number)).to eq([1, 2, 3])
|
95
|
+
end
|
16
96
|
end
|
17
97
|
end
|
@@ -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
|
+
allow(Octopus).to receive(:env).and_return('sharded_replicated_slave_grouped')
|
15
|
+
|
16
|
+
expect(Cat.using(:russia).count).to eq(2)
|
17
|
+
# It distributes queries between two slaves in the slave group
|
18
|
+
expect(Cat.using(:shard => :russia, :slave_group => :slaves1).count).to eq(0)
|
19
|
+
expect(Cat.using(:shard => :russia, :slave_group => :slaves1).count).to eq(2)
|
20
|
+
expect(Cat.using(:shard => :russia, :slave_group => :slaves1).count).to eq(0)
|
21
|
+
# It distributes queries between two slaves in the slave group
|
22
|
+
expect(Cat.using(:shard => :russia, :slave_group => :slaves2).count).to eq(2)
|
23
|
+
expect(Cat.using(:shard => :russia, :slave_group => :slaves2).count).to eq(0)
|
24
|
+
expect(Cat.using(:shard => :russia, :slave_group => :slaves2).count).to eq(2)
|
25
|
+
|
26
|
+
expect(Cat.using(:europe).count).to eq(0)
|
27
|
+
expect(Cat.using(:shard => :europe, :slave_group => :slaves1)
|
28
|
+
.count).to eq(0)
|
29
|
+
expect(Cat.using(:shard => :europe, :slave_group => :slaves2)
|
30
|
+
.count).to eq(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
|
+
expect(Cat.count).to eq(2)
|
46
|
+
expect(Cat.count).to eq(2)
|
47
|
+
expect(Cat.count).to eq(2)
|
48
|
+
expect(Cat.count).to eq(2)
|
49
|
+
expect(Cat.count).to eq(2)
|
50
|
+
expect(Cat.count).to eq(2)
|
51
|
+
expect(Cat.count).to eq(2)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|