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

setup_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: c44637b8d06b5087c6e2e58ddbb7015f27a9eb97 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Database::LoadBalancing::Setup do
  describe '#setup' do
    it 'sets up the load balancer' do
      setup = described_class.new(ActiveRecord::Base)

      expect(setup).to receive(:configure_connection)
      expect(setup).to receive(:setup_connection_proxy)
      expect(setup).to receive(:setup_service_discovery)

      setup.setup
    end
  end

  describe '#configure_connection' do
    it 'configures pool, prepared statements and reconnects to the database' do
      config = double(
        :config,
        configuration_hash: { host: 'localhost', pool: 2, prepared_statements: true },
        env_name: 'test',
        name: 'main'
      )
      model = double(:model, connection_db_config: config)

      expect(ActiveRecord::DatabaseConfigurations::HashConfig)
        .to receive(:new)
        .with('test', 'main', {
          host: 'localhost',
          prepared_statements: false,
          pool: Gitlab::Database.default_pool_size
        })
        .and_call_original

      # HashConfig doesn't implement its own #==, so we can't directly compare
      # the expected value with a pre-defined one.
      expect(model)
        .to receive(:establish_connection)
        .with(an_instance_of(ActiveRecord::DatabaseConfigurations::HashConfig))

      described_class.new(model).configure_connection
    end
  end

  describe '#setup_connection_proxy' do
    it 'sets up the load balancer' do
      model = Class.new(ActiveRecord::Base)
      setup = described_class.new(model)
      config = Gitlab::Database::LoadBalancing::Configuration.new(model)
      lb = instance_spy(Gitlab::Database::LoadBalancing::LoadBalancer)

      allow(lb).to receive(:configuration).and_return(config)

      expect(Gitlab::Database::LoadBalancing::LoadBalancer)
        .to receive(:new)
        .with(setup.configuration)
        .and_return(lb)

      setup.setup_connection_proxy

      expect(model.load_balancer).to eq(lb)
      expect(model.sticking)
        .to be_an_instance_of(Gitlab::Database::LoadBalancing::Sticking)
    end
  end

  describe '#setup_service_discovery' do
    context 'when service discovery is disabled' do
      it 'does nothing' do
        expect(Gitlab::Database::LoadBalancing::ServiceDiscovery)
          .not_to receive(:new)

        described_class.new(ActiveRecord::Base).setup_service_discovery
      end
    end

    context 'when service discovery is enabled' do
      it 'immediately performs service discovery' do
        model = ActiveRecord::Base
        setup = described_class.new(model)
        sv = instance_spy(Gitlab::Database::LoadBalancing::ServiceDiscovery)

        allow(setup.configuration)
          .to receive(:service_discovery_enabled?)
          .and_return(true)

        allow(Gitlab::Database::LoadBalancing::ServiceDiscovery)
          .to receive(:new)
          .with(setup.load_balancer, setup.configuration.service_discovery)
          .and_return(sv)

        expect(sv).to receive(:perform_service_discovery)
        expect(sv).not_to receive(:start)

        setup.setup_service_discovery
      end

      it 'starts service discovery if needed' do
        model = ActiveRecord::Base
        setup = described_class.new(model, start_service_discovery: true)
        sv = instance_spy(Gitlab::Database::LoadBalancing::ServiceDiscovery)

        allow(setup.configuration)
          .to receive(:service_discovery_enabled?)
          .and_return(true)

        allow(Gitlab::Database::LoadBalancing::ServiceDiscovery)
          .to receive(:new)
          .with(setup.load_balancer, setup.configuration.service_discovery)
          .and_return(sv)

        expect(sv).to receive(:perform_service_discovery)
        expect(sv).to receive(:start)

        setup.setup_service_discovery
      end
    end
  end

  context 'uses correct base models', :reestablished_active_record_base do
    using RSpec::Parameterized::TableSyntax

    where do
      {
        "it picks a dedicated CI connection" => {
          env_GITLAB_LOAD_BALANCING_REUSE_PRIMARY_ci: nil,
          request_store_active: false,
          ff_force_no_sharing_primary_model: false,
          expectations: {
            main: { read: 'main_replica', write: 'main' },
            ci: { read: 'ci_replica', write: 'ci' }
          }
        },
        "with re-use of primary connection it uses CI connection for reads" => {
          env_GITLAB_LOAD_BALANCING_REUSE_PRIMARY_ci: 'main',
          request_store_active: false,
          ff_force_no_sharing_primary_model: false,
          expectations: {
            main: { read: 'main_replica', write: 'main' },
            ci: { read: 'ci_replica', write: 'main' }
          }
        },
        "with re-use and FF force_no_sharing_primary_model enabled with RequestStore it sticks FF and uses CI connection for reads and writes" => {
          env_GITLAB_LOAD_BALANCING_REUSE_PRIMARY_ci: 'main',
          request_store_active: true,
          ff_force_no_sharing_primary_model: true,
          expectations: {
            main: { read: 'main_replica', write: 'main' },
            ci: { read: 'ci_replica', write: 'ci' }
          }
        },
        "with re-use and FF force_no_sharing_primary_model enabled without RequestStore it doesn't use FF and uses CI connection for reads only" => {
          env_GITLAB_LOAD_BALANCING_REUSE_PRIMARY_ci: 'main',
          request_store_active: true,
          ff_force_no_sharing_primary_model: false,
          expectations: {
            main: { read: 'main_replica', write: 'main' },
            ci: { read: 'ci_replica', write: 'main' }
          }
        }
      }
    end

    with_them do
      let(:ci_class) do
        Class.new(ActiveRecord::Base) do
          def self.name
            'Ci::ApplicationRecordTemporary'
          end

          establish_connection ActiveRecord::DatabaseConfigurations::HashConfig.new(
            Rails.env,
            'ci',
            ActiveRecord::Base.connection_db_config.configuration_hash
          )
        end
      end

      let(:models) do
        {
          main: ActiveRecord::Base,
          ci: ci_class
        }
      end

      around do |example|
        if request_store_active
          Gitlab::WithRequestStore.with_request_store do
            stub_feature_flags(force_no_sharing_primary_model: ff_force_no_sharing_primary_model)
            RequestStore.clear!

            example.run
          end
        else
          example.run
        end
      end

      before do
        allow(Gitlab).to receive(:dev_or_test_env?).and_return(false)

        # Rewrite `class_attribute` to use rspec mocking and prevent modifying the objects
        allow_next_instance_of(described_class) do |setup|
          allow(setup).to receive(:configure_connection)

          allow(setup).to receive(:setup_class_attribute) do |attribute, value|
            allow(setup.model).to receive(attribute) { value }
          end
        end

        stub_env('GITLAB_LOAD_BALANCING_REUSE_PRIMARY_ci', env_GITLAB_LOAD_BALANCING_REUSE_PRIMARY_ci)

        # Make load balancer to force init with a dedicated replicas connections
        models.each do |_, model|
          described_class.new(model).tap do |subject|
            subject.configuration.hosts = [subject.configuration.replica_db_config.host]
            subject.setup
          end
        end
      end

      it 'results match expectations' do
        result = models.transform_values do |model|
          load_balancer = model.connection.instance_variable_get(:@load_balancer)

          {
            read: load_balancer.read { |connection| connection.pool.db_config.name },
            write: load_balancer.read_write { |connection| connection.pool.db_config.name }
          }
        end

        expect(result).to eq(expectations)
      end

      it 'does return load_balancer assigned to a given connection' do
        models.each do |name, model|
          expect(model.load_balancer.name).to eq(name)
          expect(model.sticking.instance_variable_get(:@load_balancer)).to eq(model.load_balancer)
        end
      end
    end
  end
end