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

fix_incoherent_packages_size_on_project_statistics_spec.rb « background_migration « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2a53d39b6b13c8819ccf1ca523ba0c27d4a3e406 (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
# frozen_string_literal: true

require 'spec_helper'

# rubocop: disable RSpec/MultipleMemoizedHelpers
RSpec.describe Gitlab::BackgroundMigration::FixIncoherentPackagesSizeOnProjectStatistics,
  feature_category: :package_registry do
  let(:project_statistics_table) { table(:project_statistics) }
  let(:packages_table) { table(:packages_packages) }
  let(:package_files_table) { table(:packages_package_files) }
  let(:projects_table) { table(:projects) }
  let(:namespaces_table) { table(:namespaces) }

  let!(:group) { namespaces_table.create!(name: 'group', path: 'group', type: 'Group') }

  let!(:project_1_namespace) do
    namespaces_table.create!(name: 'project1', path: 'project1', type: 'Project', parent_id: group.id)
  end

  let!(:project_2_namespace) do
    namespaces_table.create!(name: 'project2', path: 'project2', type: 'Project', parent_id: group.id)
  end

  let!(:project_3_namespace) do
    namespaces_table.create!(name: 'project3', path: 'project3', type: 'Project', parent_id: group.id)
  end

  let!(:project_4_namespace) do
    namespaces_table.create!(name: 'project4', path: 'project4', type: 'Project', parent_id: group.id)
  end

  let!(:project_1) do
    projects_table.create!(
      namespace_id: group.id,
      name: 'project1',
      path: 'project1',
      project_namespace_id: project_1_namespace.id
    )
  end

  let!(:project_2) do
    projects_table.create!(
      namespace_id: group.id,
      name: 'project2',
      path: 'project2',
      project_namespace_id: project_2_namespace.id
    )
  end

  let!(:project_3) do
    projects_table.create!(
      namespace_id: group.id,
      name: 'project3',
      path: 'project3',
      project_namespace_id: project_3_namespace.id
    )
  end

  let!(:project_4) do
    projects_table.create!(
      namespace_id: group.id,
      name: 'project4',
      path: 'project4',
      project_namespace_id: project_4_namespace.id
    )
  end

  let!(:coherent_non_zero_statistics) do
    project_statistics_table.create!(namespace_id: group.id, project_id: project_1.id, packages_size: 200)
  end

  let!(:incoherent_non_zero_statistics) do
    project_statistics_table.create!(namespace_id: group.id, project_id: project_2.id, packages_size: 5)
  end

  let!(:coherent_zero_statistics) do
    project_statistics_table.create!(namespace_id: group.id, project_id: project_4.id, packages_size: 0)
  end

  let!(:incoherent_zero_statistics) do
    project_statistics_table.create!(namespace_id: group.id, project_id: project_3.id, packages_size: 0)
  end

  let!(:package_1) do
    packages_table.create!(project_id: project_1.id, name: 'test1', version: '1.2.3', package_type: 2)
  end

  let!(:package_2) do
    packages_table.create!(project_id: project_2.id, name: 'test2', version: '1.2.3', package_type: 2)
  end

  let!(:package_3) do
    packages_table.create!(project_id: project_2.id, name: 'test3', version: '1.2.3', package_type: 2)
  end

  let!(:package_4) do
    packages_table.create!(project_id: project_3.id, name: 'test4', version: '1.2.3', package_type: 2)
  end

  let!(:package_5) do
    packages_table.create!(project_id: project_3.id, name: 'test5', version: '1.2.3', package_type: 2)
  end

  let!(:package_file_1_1) do
    package_files_table.create!(package_id: package_1.id, file_name: 'test.txt', file: 'test', size: 100)
  end

  let!(:package_file_1_2) do
    package_files_table.create!(package_id: package_1.id, file_name: 'test.txt', file: 'test', size: 100)
  end

  let!(:package_file_2_1) do
    package_files_table.create!(package_id: package_2.id, file_name: 'test.txt', file: 'test', size: 100)
  end

  let!(:package_file_3_1) do
    package_files_table.create!(package_id: package_3.id, file_name: 'test.txt', file: 'test', size: 100)
  end

  let!(:package_file_4_1) do
    package_files_table.create!(package_id: package_4.id, file_name: 'test.txt', file: 'test', size: 100)
  end

  let!(:package_file_5_1) do
    package_files_table.create!(package_id: package_5.id, file_name: 'test.txt', file: 'test', size: 100)
  end

  let(:migration) do
    described_class.new(
      start_id: project_statistics_table.minimum(:id),
      end_id: project_statistics_table.maximum(:id),
      batch_table: :project_statistics,
      batch_column: :id,
      sub_batch_size: 1000,
      pause_ms: 0,
      connection: ApplicationRecord.connection
    )
  end

  describe '#filter_batch' do
    it 'selects all package size statistics' do
      expected = project_statistics_table.pluck(:id)
      actual = migration.filter_batch(project_statistics_table).pluck(:id)

      expect(actual).to match_array(expected)
    end
  end

  describe '#perform', :aggregate_failures, :clean_gitlab_redis_cache do
    subject(:perform) { migration.perform }

    shared_examples 'not updating project statistics' do
      it 'does not change them' do
        expect(FlushCounterIncrementsWorker).not_to receive(:perform_in)
        expect { perform }
          .to not_change { incoherent_non_zero_statistics.reload.packages_size }
          .and not_change { coherent_non_zero_statistics.reload.packages_size }
          .and not_change { incoherent_zero_statistics.reload.packages_size }
          .and not_change { coherent_zero_statistics.reload.packages_size }
        expect_buffered_update(incoherent_non_zero_statistics, 0)
        expect_buffered_update(incoherent_zero_statistics, 0)
      end
    end

    shared_examples 'enqueuing a buffered updates' do |updates|
      it 'fixes the packages_size stat' do
        updates_for_stats = updates.deep_transform_keys { |k| public_send(k) }
        updates_for_stats.each do |stat, amount|
          expect(FlushCounterIncrementsWorker)
            .to receive(:perform_in).with(
              ::Gitlab::Counters::BufferedCounter::WORKER_DELAY,
              'ProjectStatistics',
              stat.id,
              :packages_size
            )

          expect(::Gitlab::BackgroundMigration::Logger)
            .to receive(:info).with(
              migrator: described_class::MIGRATOR,
              project_id: stat.project_id,
              old_size: stat.packages_size,
              new_size: stat.packages_size + amount
            )
        end

        expect { perform }
          .to not_change { incoherent_non_zero_statistics.reload.packages_size }
          .and not_change { coherent_non_zero_statistics.reload.packages_size }
          .and not_change { incoherent_zero_statistics.reload.packages_size }
          .and not_change { coherent_zero_statistics.reload.packages_size }

        updates_for_stats.each do |stat, amount|
          expect_buffered_update(stat, amount)
        end
      end
    end

    context 'with incoherent packages_size' do
      it_behaves_like 'enqueuing a buffered updates',
        incoherent_non_zero_statistics: 195,
        incoherent_zero_statistics: 200

      context 'with updates waiting on redis' do
        before do
          insert_packages_size_update(incoherent_non_zero_statistics, -50)
          insert_packages_size_update(incoherent_zero_statistics, -50)
        end

        it_behaves_like 'enqueuing a buffered updates',
          incoherent_non_zero_statistics: 195,
          incoherent_zero_statistics: 200
      end
    end

    context 'with no incoherent packages_size' do
      before do
        incoherent_non_zero_statistics.update!(packages_size: 200)
        incoherent_zero_statistics.update!(packages_size: 200)
      end

      it_behaves_like 'not updating project statistics'
    end

    def insert_packages_size_update(stat, amount)
      Gitlab::Redis::SharedState.with do |redis|
        redis.set(redis_key(stat), amount)
      end
    end

    def expect_buffered_update(stat, expected)
      amount = Gitlab::Redis::SharedState.with do |redis|
        redis.get(redis_key(stat)).to_i
      end
      expect(amount).to eq(expected)
    end

    def redis_key(stats)
      "project:{#{stats.project_id}}:counters:ProjectStatistics:#{stats.id}:packages_size"
    end
  end
end
# rubocop: enable RSpec/MultipleMemoizedHelpers