activerecord-turntable 3.0.0.alpha3 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitmodules +3 -0
- data/.rubocop.yml +18 -0
- data/.rubocop_todo.yml +153 -0
- data/.travis.yml +20 -4
- data/CHANGELOG.md +32 -0
- data/Guardfile +2 -2
- data/README.md +68 -14
- data/Rakefile +42 -0
- data/activerecord-turntable.gemspec +13 -3
- data/gemfiles/rails_edge.gemfile +8 -0
- data/lib/active_record/turntable/active_record_ext/abstract_adapter.rb +3 -1
- data/lib/active_record/turntable/active_record_ext/activerecord_import_ext.rb +5 -7
- data/lib/active_record/turntable/active_record_ext/acts_as_archive_extension.rb +2 -2
- data/lib/active_record/turntable/active_record_ext/association.rb +3 -3
- data/lib/active_record/turntable/active_record_ext/clever_load.rb +2 -2
- data/lib/active_record/turntable/active_record_ext/database_tasks.rb +10 -8
- data/lib/active_record/turntable/active_record_ext/fixtures.rb +15 -13
- data/lib/active_record/turntable/active_record_ext/log_subscriber.rb +6 -0
- data/lib/active_record/turntable/active_record_ext/persistence.rb +25 -23
- data/lib/active_record/turntable/active_record_ext/schema_dumper.rb +8 -75
- data/lib/active_record/turntable/algorithm/range_algorithm.rb +6 -7
- data/lib/active_record/turntable/algorithm/range_bsearch_algorithm.rb +6 -7
- data/lib/active_record/turntable/base.rb +2 -17
- data/lib/active_record/turntable/cluster_helper_methods.rb +7 -4
- data/lib/active_record/turntable/connection_proxy.rb +4 -2
- data/lib/active_record/turntable/migration.rb +3 -5
- data/lib/active_record/turntable/mixer.rb +20 -19
- data/lib/active_record/turntable/pool_proxy.rb +20 -14
- data/lib/active_record/turntable/query_cache.rb +1 -1
- data/lib/active_record/turntable/railties/databases.rake +12 -12
- data/lib/active_record/turntable/seq_shard.rb +1 -1
- data/lib/active_record/turntable/sequencer/barrage.rb +3 -2
- data/lib/active_record/turntable/sequencer.rb +33 -29
- data/lib/active_record/turntable/shard.rb +8 -8
- data/lib/active_record/turntable/sharding_condition.rb +14 -14
- data/lib/active_record/turntable/sql_tree_patch.rb +7 -3
- data/lib/active_record/turntable/util.rb +4 -2
- data/lib/active_record/turntable/version.rb +1 -1
- data/lib/active_record/turntable.rb +6 -5
- data/lib/activerecord-turntable.rb +1 -0
- metadata +120 -101
- data/lib/active_record/turntable/helpers/test_helper.rb +0 -25
- data/lib/active_record/turntable/helpers.rb +0 -9
- data/spec/active_record/turntable/active_record_ext/association_preloader_spec.rb +0 -78
- data/spec/active_record/turntable/active_record_ext/association_spec.rb +0 -81
- data/spec/active_record/turntable/active_record_ext/clever_load_spec.rb +0 -72
- data/spec/active_record/turntable/active_record_ext/fixture_set_spec.rb +0 -27
- data/spec/active_record/turntable/active_record_ext/locking_optimistic_spec.rb +0 -28
- data/spec/active_record/turntable/active_record_ext/migration_spec.rb +0 -38
- data/spec/active_record/turntable/active_record_ext/persistence_spec.rb +0 -211
- data/spec/active_record/turntable/active_record_ext/sequencer_spec.rb +0 -22
- data/spec/active_record/turntable/active_record_ext/test_fixtures_spec.rb +0 -34
- data/spec/active_record/turntable/algorithm/modulo_algorithm_spec.rb +0 -34
- data/spec/active_record/turntable/algorithm/range_algorithm_spec.rb +0 -34
- data/spec/active_record/turntable/algorithm/range_bsearch_algorithm_spec.rb +0 -34
- data/spec/active_record/turntable/algorithm_spec.rb +0 -100
- data/spec/active_record/turntable/base_spec.rb +0 -13
- data/spec/active_record/turntable/cluster_spec.rb +0 -48
- data/spec/active_record/turntable/config_spec.rb +0 -17
- data/spec/active_record/turntable/connection_proxy_spec.rb +0 -252
- data/spec/active_record/turntable/finder_spec.rb +0 -40
- data/spec/active_record/turntable/mixer/fader_spec.rb +0 -4
- data/spec/active_record/turntable/mixer_spec.rb +0 -112
- data/spec/active_record/turntable/query_cache_spec.rb +0 -28
- data/spec/active_record/turntable/sequencer/api_spec.rb +0 -38
- data/spec/active_record/turntable/sequencer/barrage_spec.rb +0 -22
- data/spec/active_record/turntable/sequencer/mysql_spec.rb +0 -22
- data/spec/active_record/turntable/shard_spec.rb +0 -21
- data/spec/active_record/turntable/sql_tree_patch_spec.rb +0 -34
- data/spec/active_record/turntable/transaction_spec.rb +0 -35
- data/spec/active_record/turntable_spec.rb +0 -30
- data/spec/config/database.yml +0 -35
- data/spec/config/turntable.yml +0 -56
- data/spec/fabricators/.gitkeep +0 -0
- data/spec/fabricators/turntable_fabricator.rb +0 -12
- data/spec/fixtures/cards.yml +0 -11
- data/spec/migrations/.gitkeep +0 -0
- data/spec/migrations/001_create_users.rb +0 -17
- data/spec/migrations/002_create_user_statuses.rb +0 -16
- data/spec/migrations/003_create_cards.rb +0 -14
- data/spec/migrations/004_create_cards_users.rb +0 -15
- data/spec/models/card.rb +0 -3
- data/spec/models/cards_user.rb +0 -10
- data/spec/models/cards_users_histories.rb +0 -7
- data/spec/models/events_users_history.rb +0 -7
- data/spec/models/user.rb +0 -7
- data/spec/models/user_status.rb +0 -6
- data/spec/spec_helper.rb +0 -38
- data/spec/support/matchers/be_saved_to.rb +0 -6
- data/spec/support/turntable_helper.rb +0 -30
|
@@ -1,211 +0,0 @@
|
|
|
1
|
-
require "spec_helper"
|
|
2
|
-
require "logger"
|
|
3
|
-
|
|
4
|
-
describe ActiveRecord::Turntable::ActiveRecordExt::Persistence do
|
|
5
|
-
before(:all) do
|
|
6
|
-
reload_turntable!(File.join(File.dirname(__FILE__), "../../../config/turntable.yml"))
|
|
7
|
-
end
|
|
8
|
-
|
|
9
|
-
before(:each) do
|
|
10
|
-
establish_connection_to(:test)
|
|
11
|
-
truncate_shard
|
|
12
|
-
end
|
|
13
|
-
|
|
14
|
-
around(:each) do |example|
|
|
15
|
-
old = ActiveRecord::Base.logger
|
|
16
|
-
ActiveRecord::Base.logger = Logger.new(STDOUT)
|
|
17
|
-
example.run
|
|
18
|
-
ActiveRecord::Base.logger = old
|
|
19
|
-
end
|
|
20
|
-
|
|
21
|
-
let(:user) {
|
|
22
|
-
u = User.new({ nickname: "foobar" })
|
|
23
|
-
u.id = 1
|
|
24
|
-
u.updated_at = Time.current - 1.day
|
|
25
|
-
u.save
|
|
26
|
-
u
|
|
27
|
-
}
|
|
28
|
-
let(:user_status){
|
|
29
|
-
stat = user.create_user_status(hp: 10, mp: 10)
|
|
30
|
-
stat.updated_at = Time.current - 1.day
|
|
31
|
-
stat.save
|
|
32
|
-
stat
|
|
33
|
-
}
|
|
34
|
-
let(:card){
|
|
35
|
-
Card.create!(name: "foobar")
|
|
36
|
-
}
|
|
37
|
-
let(:cards_user){
|
|
38
|
-
user.cards_users.create(card: card)
|
|
39
|
-
}
|
|
40
|
-
context "When creating record" do
|
|
41
|
-
context "with blob column" do
|
|
42
|
-
let(:blob_value) { "\123\123\123" }
|
|
43
|
-
let(:user) {
|
|
44
|
-
u = User.new(nickname: "x", blob: blob_value)
|
|
45
|
-
u.id = 1
|
|
46
|
-
u.save
|
|
47
|
-
u
|
|
48
|
-
}
|
|
49
|
-
subject { user }
|
|
50
|
-
its(:blob) { is_expected.to eq(user.reload.blob) }
|
|
51
|
-
end
|
|
52
|
-
end
|
|
53
|
-
|
|
54
|
-
context "When the model is sharded by surrogate key" do
|
|
55
|
-
it "should not changed from normal operation when updating" do
|
|
56
|
-
user.nickname = "fizzbuzz"
|
|
57
|
-
strio = StringIO.new
|
|
58
|
-
ActiveRecord::Base.logger = Logger.new(strio)
|
|
59
|
-
expect {
|
|
60
|
-
user.save!
|
|
61
|
-
}.to_not raise_error
|
|
62
|
-
expect(strio.string).to match(/WHERE `users`\.`id` = #{user.id}[^\s]*$/)
|
|
63
|
-
end
|
|
64
|
-
|
|
65
|
-
it "should be saved to target_shard" do
|
|
66
|
-
expect(user).to be_saved_to(user.turntable_shard)
|
|
67
|
-
end
|
|
68
|
-
|
|
69
|
-
it "should change updated_at when updating" do
|
|
70
|
-
user.nickname = "fizzbuzz"
|
|
71
|
-
|
|
72
|
-
expect { user.save! }.to change(user, :updated_at)
|
|
73
|
-
end
|
|
74
|
-
|
|
75
|
-
it "should not changed from normal operation when destroying" do
|
|
76
|
-
strio = StringIO.new
|
|
77
|
-
ActiveRecord::Base.logger = Logger.new(strio)
|
|
78
|
-
expect { user.destroy }.to_not raise_error
|
|
79
|
-
expect(strio.string).to match(/WHERE `users`\.`id` = #{user.id}[^\s]*$/)
|
|
80
|
-
end
|
|
81
|
-
end
|
|
82
|
-
|
|
83
|
-
context "When called Callbacks" do
|
|
84
|
-
before do
|
|
85
|
-
class ::User
|
|
86
|
-
after_destroy :on_destroy
|
|
87
|
-
after_save :on_update
|
|
88
|
-
|
|
89
|
-
def on_destroy
|
|
90
|
-
end
|
|
91
|
-
|
|
92
|
-
def on_update
|
|
93
|
-
end
|
|
94
|
-
end
|
|
95
|
-
end
|
|
96
|
-
|
|
97
|
-
context "on update once" do
|
|
98
|
-
it "callback should be called once" do
|
|
99
|
-
expect(user).to receive(:on_update).once
|
|
100
|
-
user.save
|
|
101
|
-
end
|
|
102
|
-
end
|
|
103
|
-
context "on destroy once" do
|
|
104
|
-
it "callback should be called once" do
|
|
105
|
-
expect(user).to receive(:on_destroy).once
|
|
106
|
-
user.destroy
|
|
107
|
-
end
|
|
108
|
-
end
|
|
109
|
-
end
|
|
110
|
-
|
|
111
|
-
context "When the model is sharded by other key" do
|
|
112
|
-
it "should send shard_key condition when updating" do
|
|
113
|
-
cards_user.num = 10
|
|
114
|
-
|
|
115
|
-
strio = StringIO.new
|
|
116
|
-
ActiveRecord::Base.logger = Logger.new(strio)
|
|
117
|
-
expect {
|
|
118
|
-
cards_user.save!
|
|
119
|
-
}.to_not raise_error
|
|
120
|
-
expect(strio.string).to match(/`cards_users`\.`user_id` = #{cards_user.user_id}[^\s]*($|\s)/)
|
|
121
|
-
end
|
|
122
|
-
|
|
123
|
-
it "should change updated_at when updating" do
|
|
124
|
-
cards_user
|
|
125
|
-
|
|
126
|
-
Timecop.travel(1.day.from_now) do
|
|
127
|
-
expect {
|
|
128
|
-
cards_user.num = 2
|
|
129
|
-
cards_user.save!
|
|
130
|
-
}.to change(cards_user, :updated_at)
|
|
131
|
-
end
|
|
132
|
-
end
|
|
133
|
-
|
|
134
|
-
it "should send shard_key condition when destroying" do
|
|
135
|
-
strio = StringIO.new
|
|
136
|
-
ActiveRecord::Base.logger = Logger.new(strio)
|
|
137
|
-
expect {
|
|
138
|
-
cards_user.destroy
|
|
139
|
-
}.to_not raise_error
|
|
140
|
-
expect(strio.string).to match(/`cards_users`\.`user_id` = #{cards_user.user_id}[^\s]*($|\s)/)
|
|
141
|
-
end
|
|
142
|
-
|
|
143
|
-
it "should warn when creating without shard_key" do
|
|
144
|
-
skip "doesn't need to implemented soon"
|
|
145
|
-
end
|
|
146
|
-
|
|
147
|
-
it "should execute one query when reloading" do
|
|
148
|
-
user; cards_user
|
|
149
|
-
strio = StringIO.new
|
|
150
|
-
ActiveRecord::Base.logger = Logger.new(strio)
|
|
151
|
-
|
|
152
|
-
expect { cards_user.reload }.to_not raise_error
|
|
153
|
-
|
|
154
|
-
expect(strio.string.split("\n").select { |stmt| stmt =~ /SELECT/ and stmt !~ /Turntable/ }).to have(1).items
|
|
155
|
-
end
|
|
156
|
-
|
|
157
|
-
it "should execute one query when touching" do
|
|
158
|
-
user; cards_user
|
|
159
|
-
strio = StringIO.new
|
|
160
|
-
ActiveRecord::Base.logger = Logger.new(strio)
|
|
161
|
-
|
|
162
|
-
expect { cards_user.touch }.to_not raise_error
|
|
163
|
-
expect(strio.string.split("\n").select { |stmt| stmt =~ /UPDATE/ and stmt !~ /Turntable/ }).to have(1).items
|
|
164
|
-
end
|
|
165
|
-
|
|
166
|
-
it "should execute one query when locking" do
|
|
167
|
-
user; cards_user
|
|
168
|
-
strio = StringIO.new
|
|
169
|
-
ActiveRecord::Base.logger = Logger.new(strio)
|
|
170
|
-
|
|
171
|
-
expect { cards_user.lock! }.to_not raise_error
|
|
172
|
-
expect(strio.string.split("\n").select { |stmt| stmt =~ /SELECT/ and stmt !~ /Turntable/ }).to have(1).items
|
|
173
|
-
end
|
|
174
|
-
|
|
175
|
-
it "should execute one query when update_columns" do
|
|
176
|
-
user; cards_user
|
|
177
|
-
strio = StringIO.new
|
|
178
|
-
ActiveRecord::Base.logger = Logger.new(strio)
|
|
179
|
-
|
|
180
|
-
expect { cards_user.update_columns(num: 10) }.to_not raise_error
|
|
181
|
-
expect(strio.string.split("\n").select { |stmt| stmt =~ /UPDATE/ and stmt !~ /Turntable/ }).to have(1).items
|
|
182
|
-
end
|
|
183
|
-
end
|
|
184
|
-
|
|
185
|
-
context "When the model is not sharded" do
|
|
186
|
-
it "should not send shard_key condition when updating" do
|
|
187
|
-
card.name = "barbaz"
|
|
188
|
-
strio = StringIO.new
|
|
189
|
-
ActiveRecord::Base.logger = Logger.new(strio)
|
|
190
|
-
expect {
|
|
191
|
-
card.save!
|
|
192
|
-
}.to_not raise_error
|
|
193
|
-
expect(strio.string).to match(/WHERE `cards`\.`id` = #{card.id}[^\s]*$/)
|
|
194
|
-
end
|
|
195
|
-
|
|
196
|
-
it "should not send shard_key condition when destroying" do
|
|
197
|
-
strio = StringIO.new
|
|
198
|
-
ActiveRecord::Base.logger = Logger.new(strio)
|
|
199
|
-
expect {
|
|
200
|
-
card.destroy
|
|
201
|
-
}.to_not raise_error
|
|
202
|
-
expect(strio.string).to match(/WHERE `cards`\.`id` = #{card.id}[^\s]*$/)
|
|
203
|
-
end
|
|
204
|
-
end
|
|
205
|
-
|
|
206
|
-
context "When call reload" do
|
|
207
|
-
subject { cards_user.reload }
|
|
208
|
-
it { is_expected.to be_instance_of(CardsUser) }
|
|
209
|
-
it { is_expected.to eq(cards_user) }
|
|
210
|
-
end
|
|
211
|
-
end
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
require "spec_helper"
|
|
2
|
-
|
|
3
|
-
describe ActiveRecord::Turntable::ActiveRecordExt::Sequencer do
|
|
4
|
-
before(:all) do
|
|
5
|
-
reload_turntable!(File.join(File.dirname(__FILE__), "../../../config/turntable.yml"))
|
|
6
|
-
end
|
|
7
|
-
|
|
8
|
-
before(:each) do
|
|
9
|
-
establish_connection_to(:test)
|
|
10
|
-
truncate_shard
|
|
11
|
-
end
|
|
12
|
-
|
|
13
|
-
context "With sequencer enabled model" do
|
|
14
|
-
subject { User }
|
|
15
|
-
its(:sequence_name) { is_expected.to_not be_nil }
|
|
16
|
-
end
|
|
17
|
-
|
|
18
|
-
context "With sequencer disabled model" do
|
|
19
|
-
subject { Card }
|
|
20
|
-
its(:sequence_name) { is_expected.to be_nil }
|
|
21
|
-
end
|
|
22
|
-
end
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
require "spec_helper"
|
|
2
|
-
|
|
3
|
-
require "active_record"
|
|
4
|
-
require "active_record/turntable/active_record_ext/fixtures"
|
|
5
|
-
|
|
6
|
-
describe ActiveRecord::TestFixtures do
|
|
7
|
-
before(:all) do
|
|
8
|
-
reload_turntable!(File.join(File.dirname(__FILE__), "../../../config/turntable.yml"))
|
|
9
|
-
end
|
|
10
|
-
|
|
11
|
-
before do
|
|
12
|
-
establish_connection_to(:test)
|
|
13
|
-
truncate_shard
|
|
14
|
-
end
|
|
15
|
-
|
|
16
|
-
let(:fixtures_root) { File.join(File.dirname(__FILE__), "../../../fixtures") }
|
|
17
|
-
let(:fixture_file) { File.join(fixtures_root, "cards.yml") }
|
|
18
|
-
let(:test_fixture_class) { Class.new(ActiveSupport::TestCase) { include ActiveRecord::TestFixtures } }
|
|
19
|
-
let(:test_fixture) { test_fixture_class.new("test") }
|
|
20
|
-
let(:cards) { YAML.load(ERB.new(IO.read(fixture_file)).result) }
|
|
21
|
-
|
|
22
|
-
before do
|
|
23
|
-
test_fixture_class.fixture_path = fixtures_root
|
|
24
|
-
end
|
|
25
|
-
|
|
26
|
-
describe "#setup_fixtures" do
|
|
27
|
-
after do
|
|
28
|
-
test_fixture.teardown_fixtures
|
|
29
|
-
end
|
|
30
|
-
|
|
31
|
-
subject { test_fixture.setup_fixtures }
|
|
32
|
-
it { expect { subject }.not_to raise_error }
|
|
33
|
-
end
|
|
34
|
-
end
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
require "spec_helper"
|
|
2
|
-
|
|
3
|
-
describe ActiveRecord::Turntable::Algorithm::ModuloAlgorithm do
|
|
4
|
-
before(:all) do
|
|
5
|
-
reload_turntable!(File.join(File.dirname(__FILE__), "../../../config/turntable.yml"))
|
|
6
|
-
end
|
|
7
|
-
|
|
8
|
-
context "When initialized" do
|
|
9
|
-
before do
|
|
10
|
-
@alg = ActiveRecord::Turntable::Algorithm::ModuloAlgorithm.new(ActiveRecord::Base.turntable_config[:clusters][:mod_cluster])
|
|
11
|
-
end
|
|
12
|
-
|
|
13
|
-
context "#calculate with 1" do
|
|
14
|
-
subject { @alg.calculate(1) }
|
|
15
|
-
it { is_expected.to eq(ActiveRecord::Base.turntable_config[:clusters][:user_cluster][:shards][1][:connection]) }
|
|
16
|
-
end
|
|
17
|
-
|
|
18
|
-
context "#calculate with 3" do
|
|
19
|
-
subject { @alg.calculate(3) }
|
|
20
|
-
it { is_expected.to eq(ActiveRecord::Base.turntable_config[:clusters][:user_cluster][:shards][3][:connection]) }
|
|
21
|
-
end
|
|
22
|
-
|
|
23
|
-
context "#calculate with 5" do
|
|
24
|
-
subject { @alg.calculate(5) }
|
|
25
|
-
it { is_expected.to eq(ActiveRecord::Base.turntable_config[:clusters][:user_cluster][:shards][0][:connection]) }
|
|
26
|
-
end
|
|
27
|
-
|
|
28
|
-
context "#calculate with a value that is not a number" do
|
|
29
|
-
it "raises ActiveRecord::Turntable::CannotSpecifyShardError" do
|
|
30
|
-
expect { @alg.calculate("a") }.to raise_error(ActiveRecord::Turntable::CannotSpecifyShardError)
|
|
31
|
-
end
|
|
32
|
-
end
|
|
33
|
-
end
|
|
34
|
-
end
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
require "spec_helper"
|
|
2
|
-
|
|
3
|
-
describe ActiveRecord::Turntable::Algorithm::RangeAlgorithm do
|
|
4
|
-
before(:all) do
|
|
5
|
-
reload_turntable!(File.join(File.dirname(__FILE__), "../../../config/turntable.yml"))
|
|
6
|
-
end
|
|
7
|
-
|
|
8
|
-
context "When initialized" do
|
|
9
|
-
before do
|
|
10
|
-
@alg = ActiveRecord::Turntable::Algorithm::RangeAlgorithm.new(ActiveRecord::Base.turntable_config[:clusters][:user_cluster])
|
|
11
|
-
end
|
|
12
|
-
|
|
13
|
-
context "#calculate with 1" do
|
|
14
|
-
subject { @alg.calculate(1) }
|
|
15
|
-
it { is_expected.to eq(ActiveRecord::Base.turntable_config[:clusters][:user_cluster][:shards][0][:connection]) }
|
|
16
|
-
end
|
|
17
|
-
|
|
18
|
-
context "#calculate with 19999" do
|
|
19
|
-
subject { @alg.calculate(19999) }
|
|
20
|
-
it { is_expected.to eq(ActiveRecord::Base.turntable_config[:clusters][:user_cluster][:shards][0][:connection]) }
|
|
21
|
-
end
|
|
22
|
-
|
|
23
|
-
context "#calculate with 20000" do
|
|
24
|
-
subject { @alg.calculate(20000) }
|
|
25
|
-
it { is_expected.to eq(ActiveRecord::Base.turntable_config[:clusters][:user_cluster][:shards][1][:connection]) }
|
|
26
|
-
end
|
|
27
|
-
|
|
28
|
-
context "#calculate with 10000000" do
|
|
29
|
-
it "raises ActiveRecord::Turntable::CannotSpecifyShardError" do
|
|
30
|
-
expect { @alg.calculate(10_000_000) }.to raise_error(ActiveRecord::Turntable::CannotSpecifyShardError)
|
|
31
|
-
end
|
|
32
|
-
end
|
|
33
|
-
end
|
|
34
|
-
end
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
require "spec_helper"
|
|
2
|
-
|
|
3
|
-
describe ActiveRecord::Turntable::Algorithm::RangeBsearchAlgorithm do
|
|
4
|
-
before(:all) do
|
|
5
|
-
reload_turntable!(File.join(File.dirname(__FILE__), "../../../config/turntable.yml"))
|
|
6
|
-
end
|
|
7
|
-
|
|
8
|
-
context "When initialized" do
|
|
9
|
-
before do
|
|
10
|
-
@alg = ActiveRecord::Turntable::Algorithm::RangeBsearchAlgorithm.new(ActiveRecord::Base.turntable_config[:clusters][:user_cluster])
|
|
11
|
-
end
|
|
12
|
-
|
|
13
|
-
context "#calculate with 1" do
|
|
14
|
-
subject { @alg.calculate(1) }
|
|
15
|
-
it { is_expected.to eq(ActiveRecord::Base.turntable_config[:clusters][:user_cluster][:shards][0][:connection]) }
|
|
16
|
-
end
|
|
17
|
-
|
|
18
|
-
context "#calculate with 19999" do
|
|
19
|
-
subject { @alg.calculate(19999) }
|
|
20
|
-
it { is_expected.to eq(ActiveRecord::Base.turntable_config[:clusters][:user_cluster][:shards][0][:connection]) }
|
|
21
|
-
end
|
|
22
|
-
|
|
23
|
-
context "#calculate with 20000" do
|
|
24
|
-
subject { @alg.calculate(20000) }
|
|
25
|
-
it { is_expected.to eq(ActiveRecord::Base.turntable_config[:clusters][:user_cluster][:shards][1][:connection]) }
|
|
26
|
-
end
|
|
27
|
-
|
|
28
|
-
context "#calculate with 10000000" do
|
|
29
|
-
it "raises ActiveRecord::Turntable::CannotSpecifyShardError" do
|
|
30
|
-
expect { @alg.calculate(10_000_000) }.to raise_error(ActiveRecord::Turntable::CannotSpecifyShardError)
|
|
31
|
-
end
|
|
32
|
-
end
|
|
33
|
-
end
|
|
34
|
-
end
|
|
@@ -1,100 +0,0 @@
|
|
|
1
|
-
require "spec_helper"
|
|
2
|
-
|
|
3
|
-
describe ActiveRecord::Turntable::Algorithm do
|
|
4
|
-
before(:all) do
|
|
5
|
-
reload_turntable!(File.join(File.dirname(__FILE__), "../../config/turntable.yml"))
|
|
6
|
-
end
|
|
7
|
-
|
|
8
|
-
describe ActiveRecord::Turntable::Algorithm::RangeBsearchAlgorithm do
|
|
9
|
-
let(:algorithm) { ActiveRecord::Turntable::Algorithm::RangeBsearchAlgorithm.new(ActiveRecord::Base.turntable_config[:clusters][:user_cluster]) }
|
|
10
|
-
context "#calculate" do
|
|
11
|
-
it "called with 1 returns user_shard_1" do
|
|
12
|
-
expect(algorithm.calculate(1)).to eq("user_shard_1")
|
|
13
|
-
end
|
|
14
|
-
|
|
15
|
-
it "called with 19999 returns user_shard_1" do
|
|
16
|
-
expect(algorithm.calculate(19999)).to eq("user_shard_1")
|
|
17
|
-
end
|
|
18
|
-
|
|
19
|
-
it "called with 20000 returns user_shard_2" do
|
|
20
|
-
expect(algorithm.calculate(20000)).to eq("user_shard_2")
|
|
21
|
-
end
|
|
22
|
-
|
|
23
|
-
it "called with 100000 returns user_shard_3" do
|
|
24
|
-
expect(algorithm.calculate(100000)).to eq("user_shard_3")
|
|
25
|
-
end
|
|
26
|
-
end
|
|
27
|
-
|
|
28
|
-
context "#calculate_used_shards_with_weight" do
|
|
29
|
-
it "called with 10 returns 1 item" do
|
|
30
|
-
expect(algorithm.calculate_used_shards_with_weight(10)).to have(1).items
|
|
31
|
-
end
|
|
32
|
-
|
|
33
|
-
it "called with 10 returns {\"user_shard_1\" => 10}" do
|
|
34
|
-
expect(algorithm.calculate_used_shards_with_weight(10)).to eq({ "user_shard_1" => 10 })
|
|
35
|
-
end
|
|
36
|
-
|
|
37
|
-
it "called with 65000 returns 2 items" do
|
|
38
|
-
expect(algorithm.calculate_used_shards_with_weight(65000)).to have(2).items
|
|
39
|
-
end
|
|
40
|
-
|
|
41
|
-
it "called with 65000 returns {\"user_shard_1\" => 39999, \"user_shard_2\" => 25001}" do
|
|
42
|
-
expect(algorithm.calculate_used_shards_with_weight(65000)).to eq({ "user_shard_1" => 39999, "user_shard_2" => 25001 })
|
|
43
|
-
end
|
|
44
|
-
end
|
|
45
|
-
end
|
|
46
|
-
|
|
47
|
-
describe ActiveRecord::Turntable::Algorithm::RangeAlgorithm do
|
|
48
|
-
let(:algorithm) { ActiveRecord::Turntable::Algorithm::RangeAlgorithm.new(ActiveRecord::Base.turntable_config[:clusters][:user_cluster]) }
|
|
49
|
-
context "#calculate" do
|
|
50
|
-
it "called with 1 returns user_shard_1" do
|
|
51
|
-
expect(algorithm.calculate(1)).to eq("user_shard_1")
|
|
52
|
-
end
|
|
53
|
-
|
|
54
|
-
it "called with 19999 returns user_shard_1" do
|
|
55
|
-
expect(algorithm.calculate(19999)).to eq("user_shard_1")
|
|
56
|
-
end
|
|
57
|
-
|
|
58
|
-
it "called with 20000 returns user_shard_2" do
|
|
59
|
-
expect(algorithm.calculate(20000)).to eq("user_shard_2")
|
|
60
|
-
end
|
|
61
|
-
|
|
62
|
-
it "called with 100000 returns user_shard_3" do
|
|
63
|
-
expect(algorithm.calculate(100000)).to eq("user_shard_3")
|
|
64
|
-
end
|
|
65
|
-
end
|
|
66
|
-
|
|
67
|
-
context "#calculate_used_shards_with_weight" do
|
|
68
|
-
it "called with 10 returns 1 item" do
|
|
69
|
-
expect(algorithm.calculate_used_shards_with_weight(10)).to have(1).items
|
|
70
|
-
end
|
|
71
|
-
|
|
72
|
-
it "called with 10 returns {\"user_shard_1\" => 10}" do
|
|
73
|
-
expect(algorithm.calculate_used_shards_with_weight(10)).to eq({ "user_shard_1" => 10 })
|
|
74
|
-
end
|
|
75
|
-
|
|
76
|
-
it "called with 65000 returns 2 items" do
|
|
77
|
-
expect(algorithm.calculate_used_shards_with_weight(65000)).to have(2).items
|
|
78
|
-
end
|
|
79
|
-
|
|
80
|
-
it "called with 65000 returns {\"user_shard_1\" => 39999, \"user_shard_2\" => 25001}" do
|
|
81
|
-
expect(algorithm.calculate_used_shards_with_weight(65000)).to eq({ "user_shard_1" => 39999, "user_shard_2" => 25001 })
|
|
82
|
-
end
|
|
83
|
-
end
|
|
84
|
-
end
|
|
85
|
-
|
|
86
|
-
describe ActiveRecord::Turntable::Algorithm::ModuloAlgorithm do
|
|
87
|
-
let(:algorithm) { ActiveRecord::Turntable::Algorithm::ModuloAlgorithm.new(ActiveRecord::Base.turntable_config[:clusters][:mod_cluster]) }
|
|
88
|
-
context "#calculate" do
|
|
89
|
-
it "called with 1 return user_shard_2" do
|
|
90
|
-
expect(algorithm.calculate(1)).to eq("user_shard_2")
|
|
91
|
-
end
|
|
92
|
-
it "called with 3 return user_shard_2" do
|
|
93
|
-
expect(algorithm.calculate(3)).to eq("user_shard_2")
|
|
94
|
-
end
|
|
95
|
-
it "called with 5 return user_shard_1" do
|
|
96
|
-
expect(algorithm.calculate(5)).to eq("user_shard_1")
|
|
97
|
-
end
|
|
98
|
-
end
|
|
99
|
-
end
|
|
100
|
-
end
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
require "spec_helper"
|
|
2
|
-
|
|
3
|
-
describe ActiveRecord::Turntable::Base do
|
|
4
|
-
before(:all) do
|
|
5
|
-
reload_turntable!(File.join(File.dirname(__FILE__), "../../config/turntable.yml"))
|
|
6
|
-
end
|
|
7
|
-
|
|
8
|
-
context "When installed to ActiveRecord::Base" do
|
|
9
|
-
it "ActiveRecord::Base respond_to 'turntable'" do
|
|
10
|
-
expect(ActiveRecord::Base).to respond_to(:turntable)
|
|
11
|
-
end
|
|
12
|
-
end
|
|
13
|
-
end
|
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
require "spec_helper"
|
|
2
|
-
|
|
3
|
-
describe ActiveRecord::Turntable::Cluster do
|
|
4
|
-
before(:all) do
|
|
5
|
-
reload_turntable!(File.join(File.dirname(__FILE__), "../../config/turntable.yml"))
|
|
6
|
-
end
|
|
7
|
-
|
|
8
|
-
before do
|
|
9
|
-
establish_connection_to(:test)
|
|
10
|
-
truncate_shard
|
|
11
|
-
end
|
|
12
|
-
let(:cluster_config) { ActiveRecord::Base.turntable_config[:clusters][:user_cluster] }
|
|
13
|
-
let(:cluster) { ActiveRecord::Turntable::Cluster.new(cluster_config) }
|
|
14
|
-
let(:mysql_mod_cluster_config) { ActiveRecord::Base.turntable_config[:clusters][:mysql_mod_cluster] }
|
|
15
|
-
let(:mysql_mod_cluster) { ActiveRecord::Turntable::Cluster.new(mysql_mod_cluster_config) }
|
|
16
|
-
let(:in_range_shard_key_value) { cluster_config[:shards].last[:less_than] - 1 }
|
|
17
|
-
let(:out_of_range_shard_key_value) { cluster_config[:shards].last[:less_than] }
|
|
18
|
-
|
|
19
|
-
context "When initialized" do
|
|
20
|
-
subject { cluster }
|
|
21
|
-
|
|
22
|
-
its(:shards) { should have(3).items }
|
|
23
|
-
end
|
|
24
|
-
|
|
25
|
-
context "When initialized mysql sequencer type cluster" do
|
|
26
|
-
subject { mysql_mod_cluster }
|
|
27
|
-
|
|
28
|
-
its(:shards) { should have(2).items }
|
|
29
|
-
its(:seq) { is_expected.not_to be nil }
|
|
30
|
-
end
|
|
31
|
-
|
|
32
|
-
describe "#shard_for" do
|
|
33
|
-
subject { cluster.shard_for(value) }
|
|
34
|
-
|
|
35
|
-
context "with argument in shard range value" do
|
|
36
|
-
let(:value) { in_range_shard_key_value }
|
|
37
|
-
let(:expected_shard_name) { cluster_config[:shards].last[:connection] }
|
|
38
|
-
|
|
39
|
-
it { is_expected.to be_instance_of(ActiveRecord::Turntable::Shard) }
|
|
40
|
-
its(:name) { is_expected.to eq expected_shard_name }
|
|
41
|
-
end
|
|
42
|
-
|
|
43
|
-
context "with argument out of shard range value" do
|
|
44
|
-
let(:value) { out_of_range_shard_key_value }
|
|
45
|
-
it { expect { subject }.to raise_error(ActiveRecord::Turntable::CannotSpecifyShardError) }
|
|
46
|
-
end
|
|
47
|
-
end
|
|
48
|
-
end
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
require "spec_helper"
|
|
2
|
-
|
|
3
|
-
describe ActiveRecord::Turntable::Config do
|
|
4
|
-
before(:all) do
|
|
5
|
-
reload_turntable!(File.join(File.dirname(__FILE__), "../../config/turntable.yml"))
|
|
6
|
-
end
|
|
7
|
-
|
|
8
|
-
subject { ActiveRecord::Turntable::Config }
|
|
9
|
-
|
|
10
|
-
it "has config hash" do
|
|
11
|
-
expect(subject.instance.instance_variable_get(:@config)).to be_an_kind_of(Hash)
|
|
12
|
-
end
|
|
13
|
-
|
|
14
|
-
it "has cluster setting" do
|
|
15
|
-
expect(subject[:clusters][:user_cluster]).to be_instance_of(ActiveSupport::HashWithIndifferentAccess)
|
|
16
|
-
end
|
|
17
|
-
end
|