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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::Database::LoadBalancing::Sticking, :redis do
let(:sticking) do
described_class.new(ActiveRecord::Base.load_balancer)
end
after do
Gitlab::Database::LoadBalancing::Session.clear_session
end
shared_examples 'sticking' do
before do
allow(ActiveRecord::Base.load_balancer)
.to receive(:primary_write_location)
.and_return('foo')
end
it 'sticks an entity to the primary', :aggregate_failures do
allow(ActiveRecord::Base.load_balancer)
.to receive(:primary_only?)
.and_return(false)
ids.each do |id|
expect(sticking)
.to receive(:set_write_location_for)
.with(:user, id, 'foo')
end
expect(Gitlab::Database::LoadBalancing::Session.current)
.to receive(:use_primary!)
subject
end
it 'does not update the write location when no replicas are used' do
expect(sticking).not_to receive(:set_write_location_for)
subject
end
end
shared_examples 'tracking status in redis' do
describe '#stick_or_unstick_request' do
it 'sticks or unsticks a single object and updates the Rack environment' do
expect(sticking)
.to receive(:unstick_or_continue_sticking)
.with(:user, 42)
env = {}
sticking.stick_or_unstick_request(env, :user, 42)
expect(env[Gitlab::Database::LoadBalancing::RackMiddleware::STICK_OBJECT].to_a)
.to eq([[sticking, :user, 42]])
end
it 'sticks or unsticks multiple objects and updates the Rack environment' do
expect(sticking)
.to receive(:unstick_or_continue_sticking)
.with(:user, 42)
.ordered
expect(sticking)
.to receive(:unstick_or_continue_sticking)
.with(:runner, '123456789')
.ordered
env = {}
sticking.stick_or_unstick_request(env, :user, 42)
sticking.stick_or_unstick_request(env, :runner, '123456789')
expect(env[Gitlab::Database::LoadBalancing::RackMiddleware::STICK_OBJECT].to_a).to eq(
[
[sticking, :user, 42],
[sticking, :runner,
'123456789']
])
end
end
describe '#stick_if_necessary' do
it 'does not stick if no write was performed' do
allow(Gitlab::Database::LoadBalancing::Session.current)
.to receive(:performed_write?)
.and_return(false)
expect(sticking).not_to receive(:stick)
sticking.stick_if_necessary(:user, 42)
end
it 'sticks to the primary if a write was performed' do
allow(Gitlab::Database::LoadBalancing::Session.current)
.to receive(:performed_write?)
.and_return(true)
expect(sticking)
.to receive(:stick)
.with(:user, 42)
sticking.stick_if_necessary(:user, 42)
end
end
describe '#all_caught_up?' do
let(:lb) { ActiveRecord::Base.load_balancer }
let(:last_write_location) { 'foo' }
before do
allow(ActiveSupport::Notifications).to receive(:instrument).and_call_original
allow(sticking)
.to receive(:last_write_location_for)
.with(:user, 42)
.and_return(last_write_location)
end
context 'when no write location could be found' do
let(:last_write_location) { nil }
it 'returns true' do
expect(lb).not_to receive(:select_up_to_date_host)
expect(sticking.all_caught_up?(:user, 42)).to eq(true)
end
end
context 'when all secondaries have caught up' do
before do
allow(lb).to receive(:select_up_to_date_host).with('foo').and_return(true)
end
it 'returns true, and unsticks' do
expect(sticking)
.to receive(:unstick)
.with(:user, 42)
expect(sticking.all_caught_up?(:user, 42)).to eq(true)
end
it 'notifies with the proper event payload' do
expect(ActiveSupport::Notifications)
.to receive(:instrument)
.with('caught_up_replica_pick.load_balancing', { result: true })
.and_call_original
sticking.all_caught_up?(:user, 42)
end
end
context 'when the secondaries have not yet caught up' do
before do
allow(lb).to receive(:select_up_to_date_host).with('foo').and_return(false)
end
it 'returns false' do
expect(sticking.all_caught_up?(:user, 42)).to eq(false)
end
it 'notifies with the proper event payload' do
expect(ActiveSupport::Notifications)
.to receive(:instrument)
.with('caught_up_replica_pick.load_balancing', { result: false })
.and_call_original
sticking.all_caught_up?(:user, 42)
end
end
end
describe '#unstick_or_continue_sticking' do
let(:lb) { ActiveRecord::Base.load_balancer }
it 'simply returns if no write location could be found' do
allow(sticking)
.to receive(:last_write_location_for)
.with(:user, 42)
.and_return(nil)
expect(lb).not_to receive(:select_up_to_date_host)
sticking.unstick_or_continue_sticking(:user, 42)
end
it 'unsticks if all secondaries have caught up' do
allow(sticking)
.to receive(:last_write_location_for)
.with(:user, 42)
.and_return('foo')
allow(lb).to receive(:select_up_to_date_host).with('foo').and_return(true)
expect(sticking)
.to receive(:unstick)
.with(:user, 42)
sticking.unstick_or_continue_sticking(:user, 42)
end
it 'continues using the primary if the secondaries have not yet caught up' do
allow(sticking)
.to receive(:last_write_location_for)
.with(:user, 42)
.and_return('foo')
allow(lb).to receive(:select_up_to_date_host).with('foo').and_return(false)
expect(Gitlab::Database::LoadBalancing::Session.current)
.to receive(:use_primary!)
sticking.unstick_or_continue_sticking(:user, 42)
end
end
describe '#stick' do
it_behaves_like 'sticking' do
let(:ids) { [42] }
subject { sticking.stick(:user, ids.first) }
end
end
describe '#bulk_stick' do
it_behaves_like 'sticking' do
let(:ids) { [42, 43] }
subject { sticking.bulk_stick(:user, ids) }
end
end
describe '#mark_primary_write_location' do
it 'updates the write location with the load balancer' do
allow(ActiveRecord::Base.load_balancer)
.to receive(:primary_write_location)
.and_return('foo')
allow(ActiveRecord::Base.load_balancer)
.to receive(:primary_only?)
.and_return(false)
expect(sticking)
.to receive(:set_write_location_for)
.with(:user, 42, 'foo')
sticking.mark_primary_write_location(:user, 42)
end
it 'does nothing when no replicas are used' do
expect(sticking).not_to receive(:set_write_location_for)
sticking.mark_primary_write_location(:user, 42)
end
end
describe '#unstick' do
it 'removes the sticking data from Redis' do
sticking.set_write_location_for(:user, 4, 'foo')
sticking.unstick(:user, 4)
expect(sticking.last_write_location_for(:user, 4)).to be_nil
end
end
describe '#last_write_location_for' do
it 'returns the last WAL write location for a user' do
sticking.set_write_location_for(:user, 4, 'foo')
expect(sticking.last_write_location_for(:user, 4)).to eq('foo')
end
end
describe '#select_caught_up_replicas' do
let(:lb) { ActiveRecord::Base.load_balancer }
context 'with no write location' do
before do
allow(sticking)
.to receive(:last_write_location_for)
.with(:project, 42)
.and_return(nil)
end
it 'returns false and does not try to find caught up hosts' do
expect(lb).not_to receive(:select_up_to_date_host)
expect(sticking.select_caught_up_replicas(:project, 42)).to be false
end
end
context 'with write location' do
before do
allow(sticking)
.to receive(:last_write_location_for)
.with(:project, 42)
.and_return('foo')
end
it 'returns true, selects hosts, and unsticks if any secondary has caught up' do
expect(lb).to receive(:select_up_to_date_host).and_return(true)
expect(sticking)
.to receive(:unstick)
.with(:project, 42)
expect(sticking.select_caught_up_replicas(:project, 42)).to be true
end
end
end
end
context 'with multi-store feature flags turned on' do
it_behaves_like 'tracking status in redis'
end
context 'when both multi-store feature flags are off' do
before do
stub_feature_flags(use_primary_and_secondary_stores_for_db_load_balancing: false)
stub_feature_flags(use_primary_store_as_default_for_db_load_balancing: false)
end
it_behaves_like 'tracking status in redis'
end
describe '#redis_key_for' do
it 'returns a String' do
expect(sticking.redis_key_for(:user, 42))
.to eq('database-load-balancing/write-location/main/user/42')
end
end
end
|