Welcome to mirror list, hosted at ThFree Co, Russian Federation.

connection_proxy_spec.rb « load_balancing « database « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ee2718171c0c4ba3eabb1a9fbf2745dfbd33959d (plain)
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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Database::LoadBalancing::ConnectionProxy do
  let(:config) { Gitlab::Database::LoadBalancing::Configuration.new(ActiveRecord::Base) }
  let(:load_balancer) { Gitlab::Database::LoadBalancing::LoadBalancer.new(config) }
  let(:proxy) { described_class.new(load_balancer) }

  describe '#select' do
    it 'performs a read' do
      expect(proxy).to receive(:read_using_load_balancer).with(:select, 'foo')

      proxy.select('foo')
    end
  end

  describe '#select_all' do
    let(:override_proxy) { ActiveRecord::Base.connection.class }

    # We can't use :Gitlab::Utils::Override because this method is dynamically prepended
    it 'method signatures match' do
      expect(proxy.method(:select_all).parameters).to eq(override_proxy.instance_method(:select_all).parameters)
    end

    describe 'using a SELECT query' do
      it 'runs the query on a secondary' do
        arel = double(:arel)

        expect(proxy).to receive(:read_using_load_balancer)
          .with(:select_all, arel, 'foo', [])

        proxy.select_all(arel, 'foo')
      end
    end

    describe 'using a SELECT FOR UPDATE query' do
      it 'runs the query on the primary and sticks to it' do
        arel = double(:arel, locked: true)
        session = Gitlab::Database::LoadBalancing::Session.new

        allow(Gitlab::Database::LoadBalancing::Session).to receive(:current)
          .and_return(session)

        expect(session).to receive(:write!)

        expect(proxy).to receive(:write_using_load_balancer)
          .with(:select_all, arel, 'foo', [])

        proxy.select_all(arel, 'foo')
      end
    end
  end

  Gitlab::Database::LoadBalancing::ConnectionProxy::NON_STICKY_READS.each do |name|
    describe "#{name}" do
      it 'runs the query on the replica' do
        expect(proxy).to receive(:read_using_load_balancer)
          .with(name, 'foo')

        proxy.send(name, 'foo')
      end
    end
  end

  Gitlab::Database::LoadBalancing::ConnectionProxy::STICKY_WRITES.each do |name|
    describe "#{name}" do
      it 'runs the query on the primary and sticks to it' do
        session = Gitlab::Database::LoadBalancing::Session.new

        allow(Gitlab::Database::LoadBalancing::Session).to receive(:current)
          .and_return(session)

        expect(session).to receive(:write!)
        expect(proxy).to receive(:write_using_load_balancer).with(name, 'foo')

        proxy.send(name, 'foo')
      end
    end
  end

  describe '.insert_all!' do
    before do
      ActiveRecord::Schema.define do
        create_table :_test_connection_proxy_bulk_insert, force: true do |t|
          t.string :name, null: true
        end
      end
    end

    after do
      ActiveRecord::Schema.define do
        drop_table :_test_connection_proxy_bulk_insert, force: true
      end
    end

    let(:model_class) do
      Class.new(ApplicationRecord) do
        self.table_name = "_test_connection_proxy_bulk_insert"
      end
    end

    it 'inserts data in bulk' do
      expect(model_class).to receive(:connection)
        .at_least(:once)
        .and_return(proxy)

      expect(proxy).to receive(:write_using_load_balancer)
        .at_least(:once)
        .and_call_original

      expect do
        model_class.insert_all! [
          { name: "item1" },
          { name: "item2" }
        ]
      end.to change { model_class.count }.by(2)
    end
  end

  # We have an extra test for #transaction here to make sure that nested queries
  # are also sent to a primary.
  describe '#transaction' do
    let(:session) { Gitlab::Database::LoadBalancing::Session.new }

    before do
      allow(Gitlab::Database::LoadBalancing::Session).to receive(:current)
        .and_return(session)
    end

    context 'session fallbacks ambiguous queries to replicas' do
      let(:replica) { double(:connection) }

      before do
        allow(session).to receive(:fallback_to_replicas_for_ambiguous_queries?).and_return(true)
        allow(session).to receive(:use_primary?).and_return(false)
        allow(replica).to receive(:transaction).and_yield
        allow(replica).to receive(:select)
      end

      context 'with a read query' do
        it 'runs the transaction and any nested queries on the replica' do
          expect(load_balancer).to receive(:read)
            .twice.and_yield(replica)
          expect(load_balancer).not_to receive(:read_write)
          expect(session).not_to receive(:write!)

          proxy.transaction { proxy.select('true') }
        end
      end

      context 'with a write query' do
        it 'raises an exception' do
          allow(load_balancer).to receive(:read).and_yield(replica)
          allow(load_balancer).to receive(:read_write).and_yield(replica)

          expect do
            proxy.transaction { proxy.insert('something') }
          end.to raise_error(Gitlab::Database::LoadBalancing::ConnectionProxy::WriteInsideReadOnlyTransactionError)
        end
      end
    end

    context 'session does not fallback to replicas for ambiguous queries' do
      let(:primary) { double(:connection) }

      before do
        allow(session).to receive(:fallback_to_replicas_for_ambiguous_queries?).and_return(false)
        allow(session).to receive(:use_replicas_for_read_queries?).and_return(false)
        allow(session).to receive(:use_primary?).and_return(true)
        allow(primary).to receive(:transaction).and_yield
        allow(primary).to receive(:select)
        allow(primary).to receive(:insert)
      end

      context 'with a read query' do
        it 'runs the transaction and any nested queries on the primary and stick to it' do
          expect(load_balancer).to receive(:read_write)
            .twice.and_yield(primary)
          expect(load_balancer).not_to receive(:read)
          expect(session).to receive(:write!)

          proxy.transaction { proxy.select('true') }
        end
      end

      context 'with a write query' do
        it 'runs the transaction and any nested queries on the primary and stick to it' do
          expect(load_balancer).to receive(:read_write)
            .twice.and_yield(primary)
          expect(load_balancer).not_to receive(:read)
          expect(session).to receive(:write!).twice

          proxy.transaction { proxy.insert('something') }
        end
      end
    end
  end

  describe '#method_missing' do
    it 'runs the query on the primary without sticking to it' do
      expect(proxy).to receive(:write_using_load_balancer)
        .with(:foo, 'foo')

      proxy.foo('foo')
    end

    it 'properly forwards keyword arguments' do
      allow(load_balancer).to receive(:read_write)

      expect(proxy).to receive(:write_using_load_balancer).and_call_original

      expect { proxy.case_sensitive_comparison(:table, :attribute, :column, value: :value, format: :format) }
        .not_to raise_error
    end

    context 'current session prefers to fallback ambiguous queries to replicas' do
      let(:session) { double(:session) }

      before do
        allow(Gitlab::Database::LoadBalancing::Session).to receive(:current)
          .and_return(session)
        allow(session).to receive(:fallback_to_replicas_for_ambiguous_queries?).and_return(true)
        allow(session).to receive(:use_primary?).and_return(false)
      end

      it 'runs the query on the replica' do
        expect(proxy).to receive(:read_using_load_balancer).with(:foo, 'foo')

        proxy.foo('foo')
      end

      it 'properly forwards keyword arguments' do
        allow(load_balancer).to receive(:read)

        expect(proxy).to receive(:read_using_load_balancer).and_call_original

        expect { proxy.case_sensitive_comparison(:table, :attribute, :column, value: :value, format: :format) }
          .not_to raise_error
      end
    end
  end

  describe '#read_using_load_balancer' do
    let(:session) { double(:session) }
    let(:connection) { double(:connection) }

    before do
      allow(Gitlab::Database::LoadBalancing::Session).to receive(:current)
        .and_return(session)
    end

    context 'with a regular session' do
      it 'uses a secondary' do
        allow(session).to receive(:use_primary?).and_return(false)
        allow(session).to receive(:use_replicas_for_read_queries?).and_return(false)

        expect(connection).to receive(:foo).with('foo')
        expect(load_balancer).to receive(:read).and_yield(connection)

        proxy.read_using_load_balancer(:foo, 'foo')
      end
    end

    context 'with a regular session and forcing all reads to replicas' do
      it 'uses a secondary' do
        allow(session).to receive(:use_primary?).and_return(false)
        allow(session).to receive(:use_replicas_for_read_queries?).and_return(true)

        expect(connection).to receive(:foo).with('foo')
        expect(load_balancer).to receive(:read).and_yield(connection)

        proxy.read_using_load_balancer(:foo, 'foo')
      end
    end

    context 'with a session using the primary but forcing all reads to replicas' do
      it 'uses a secondary' do
        allow(session).to receive(:use_primary?).and_return(true)
        allow(session).to receive(:use_replicas_for_read_queries?).and_return(true)

        expect(connection).to receive(:foo).with('foo')
        expect(load_balancer).to receive(:read).and_yield(connection)

        proxy.read_using_load_balancer(:foo, 'foo')
      end
    end

    describe 'with a session using the primary' do
      it 'uses the primary' do
        allow(session).to receive(:use_primary?).and_return(true)
        allow(session).to receive(:use_replicas_for_read_queries?).and_return(false)

        expect(connection).to receive(:foo).with('foo')

        expect(load_balancer).to receive(:read_write)
          .and_yield(connection)

        proxy.read_using_load_balancer(:foo, 'foo')
      end
    end
  end

  describe '#write_using_load_balancer' do
    let(:session) { double(:session) }
    let(:connection) { double(:connection) }

    before do
      allow(Gitlab::Database::LoadBalancing::Session).to receive(:current)
        .and_return(session)
    end

    it 'uses but does not stick to the primary' do
      expect(load_balancer).to receive(:read_write).and_yield(connection)
      expect(connection).to receive(:foo).with('foo')
      expect(session).not_to receive(:write!)

      proxy.write_using_load_balancer(:foo, 'foo')
    end
  end
end