1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::Database::LoadBalancing::Configuration, :request_store do
let(:configuration_hash) { {} }
let(:db_config) { ActiveRecord::DatabaseConfigurations::HashConfig.new('test', 'ci', configuration_hash) }
let(:model) { double(:model, connection_db_config: db_config) }
before do
# It's confusing to think about these specs with this enabled by default so
# we make it disabled by default and just write the specific spec for when
# it's enabled
stub_feature_flags(force_no_sharing_primary_model: false)
end
describe '.for_model' do
context 'when load balancing is not configured' do
it 'uses the default settings' do
config = described_class.for_model(model)
expect(config.hosts).to eq([])
expect(config.max_replication_difference).to eq(8.megabytes)
expect(config.max_replication_lag_time).to eq(60.0)
expect(config.replica_check_interval).to eq(60.0)
expect(config.service_discovery).to eq(
nameserver: 'localhost',
port: 8600,
record: nil,
record_type: 'A',
interval: 60,
disconnect_timeout: 120,
use_tcp: false
)
expect(config.pool_size).to eq(Gitlab::Database.default_pool_size)
end
end
context 'when load balancing is configured' do
let(:configuration_hash) do
{
pool: 4,
load_balancing: {
max_replication_difference: 1,
max_replication_lag_time: 2,
replica_check_interval: 3,
hosts: %w[foo bar],
discover: {
'record' => 'foo.example.com'
}
}
}
end
it 'uses the custom configuration settings' do
config = described_class.for_model(model)
expect(config.hosts).to eq(%w[foo bar])
expect(config.max_replication_difference).to eq(1)
expect(config.max_replication_lag_time).to eq(2.0)
expect(config.replica_check_interval).to eq(3.0)
expect(config.service_discovery).to eq(
nameserver: 'localhost',
port: 8600,
record: 'foo.example.com',
record_type: 'A',
interval: 60,
disconnect_timeout: 120,
use_tcp: false
)
expect(config.pool_size).to eq(4)
end
end
context 'when the load balancing configuration uses strings as the keys' do
let(:configuration_hash) do
{
pool: 4,
load_balancing: {
'max_replication_difference' => 1,
'max_replication_lag_time' => 2,
'replica_check_interval' => 3,
'hosts' => %w[foo bar],
'discover' => {
'record' => 'foo.example.com'
}
}
}
end
it 'uses the custom configuration settings' do
config = described_class.for_model(model)
expect(config.hosts).to eq(%w[foo bar])
expect(config.max_replication_difference).to eq(1)
expect(config.max_replication_lag_time).to eq(2.0)
expect(config.replica_check_interval).to eq(3.0)
expect(config.service_discovery).to eq(
nameserver: 'localhost',
port: 8600,
record: 'foo.example.com',
record_type: 'A',
interval: 60,
disconnect_timeout: 120,
use_tcp: false
)
expect(config.pool_size).to eq(4)
end
end
it 'calls reuse_primary_connection!' do
expect_next_instance_of(described_class) do |subject|
expect(subject).to receive(:reuse_primary_connection!).and_call_original
end
described_class.for_model(model)
end
end
describe '#load_balancing_enabled?' do
it 'returns false when running inside a Rake task' do
config = described_class.new(ActiveRecord::Base, %w[foo bar])
allow(Gitlab::Runtime).to receive(:rake?).and_return(true)
expect(config.load_balancing_enabled?).to eq(false)
end
it 'returns true when hosts are configured' do
config = described_class.new(ActiveRecord::Base, %w[foo bar])
expect(config.load_balancing_enabled?).to eq(true)
end
it 'returns true when a service discovery record is configured' do
config = described_class.new(ActiveRecord::Base)
config.service_discovery[:record] = 'foo'
expect(config.load_balancing_enabled?).to eq(true)
end
it 'returns false when no hosts are configured and service discovery is disabled' do
config = described_class.new(ActiveRecord::Base)
expect(config.load_balancing_enabled?).to eq(false)
end
end
describe '#service_discovery_enabled?' do
it 'returns false when running inside a Rake task' do
allow(Gitlab::Runtime).to receive(:rake?).and_return(true)
config = described_class.new(ActiveRecord::Base)
config.service_discovery[:record] = 'foo'
expect(config.service_discovery_enabled?).to eq(false)
end
it 'returns true when a record is configured' do
config = described_class.new(ActiveRecord::Base)
config.service_discovery[:record] = 'foo'
expect(config.service_discovery_enabled?).to eq(true)
end
it 'returns false when no record is configured' do
config = described_class.new(ActiveRecord::Base)
expect(config.service_discovery_enabled?).to eq(false)
end
end
describe '#pool_size' do
context 'when a custom pool size is used' do
let(:configuration_hash) { { pool: 4 } }
it 'always reads the value from the model configuration' do
config = described_class.new(model)
expect(config.pool_size).to eq(4)
# We can't modify `configuration_hash` as it's only used to populate the
# internal hash used by ActiveRecord; instead of it being used as-is.
allow(model.connection_db_config)
.to receive(:configuration_hash)
.and_return({ pool: 42 })
expect(config.pool_size).to eq(42)
end
end
context 'when the pool size is nil' do
let(:configuration_hash) { {} }
it 'returns the default pool size' do
config = described_class.new(model)
expect(config.pool_size).to eq(Gitlab::Database.default_pool_size)
end
end
end
describe '#db_config_name' do
let(:config) { described_class.new(model) }
subject { config.db_config_name }
it 'returns connection name as symbol' do
is_expected.to eq(:ci)
end
end
describe '#replica_db_config' do
let(:model) { double(:model, connection_db_config: db_config, connection_specification_name: 'Ci::ApplicationRecord') }
let(:config) { described_class.for_model(model) }
it 'returns exactly db_config' do
expect(config.replica_db_config).to eq(db_config)
end
context 'when GITLAB_LOAD_BALANCING_REUSE_PRIMARY_ci=main' do
it 'does not change replica_db_config' do
stub_env('GITLAB_LOAD_BALANCING_REUSE_PRIMARY_ci', 'main')
expect(config.replica_db_config).to eq(db_config)
end
end
end
describe 'reuse_primary_connection!' do
let(:model) { double(:model, connection_db_config: db_config, connection_specification_name: 'Ci::ApplicationRecord') }
let(:config) { described_class.for_model(model) }
context 'when GITLAB_LOAD_BALANCING_REUSE_PRIMARY_* not configured' do
it 'the primary connection uses default specification' do
stub_env('GITLAB_LOAD_BALANCING_REUSE_PRIMARY_ci', nil)
expect(config.primary_connection_specification_name).to eq('Ci::ApplicationRecord')
end
end
context 'when GITLAB_LOAD_BALANCING_REUSE_PRIMARY_ci=main' do
before do
stub_env('GITLAB_LOAD_BALANCING_REUSE_PRIMARY_ci', 'main')
end
it 'the primary connection uses main connection' do
expect(config.primary_connection_specification_name).to eq('ActiveRecord::Base')
end
context 'when force_no_sharing_primary_model feature flag is enabled' do
before do
stub_feature_flags(force_no_sharing_primary_model: true)
end
it 'the primary connection uses ci connection' do
expect(config.primary_connection_specification_name).to eq('Ci::ApplicationRecord')
end
end
end
context 'when GITLAB_LOAD_BALANCING_REUSE_PRIMARY_ci=unknown' do
it 'raises exception' do
stub_env('GITLAB_LOAD_BALANCING_REUSE_PRIMARY_ci', 'unknown')
expect { config.reuse_primary_connection! }.to raise_error /Invalid value for/
end
end
end
end
|