2019-07-31 22:56:46 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
RSpec.shared_examples 'UpdateProjectStatistics' do |with_counter_attribute|
|
2019-09-04 21:01:54 +05:30
|
|
|
let(:project) { subject.project }
|
|
|
|
let(:project_statistics_name) { described_class.project_statistics_name }
|
|
|
|
let(:statistic_attribute) { described_class.statistic_attribute }
|
2019-07-31 22:56:46 +05:30
|
|
|
|
|
|
|
def reload_stat
|
2019-09-04 21:01:54 +05:30
|
|
|
project.statistics.reload.send(project_statistics_name).to_i
|
2019-07-31 22:56:46 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def read_attribute
|
2019-09-04 21:01:54 +05:30
|
|
|
subject.read_attribute(statistic_attribute).to_i
|
2019-07-31 22:56:46 +05:30
|
|
|
end
|
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
def read_pending_increment
|
|
|
|
Gitlab::Redis::SharedState.with do |redis|
|
|
|
|
key = project.statistics.counter_key(project_statistics_name)
|
|
|
|
redis.get(key).to_i
|
|
|
|
end
|
|
|
|
end
|
2019-07-31 22:56:46 +05:30
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
it { is_expected.to be_new_record }
|
2019-07-31 22:56:46 +05:30
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
context 'when feature flag efficient_counter_attribute is disabled' do
|
|
|
|
before do
|
|
|
|
stub_feature_flags(efficient_counter_attribute: false)
|
|
|
|
end
|
2020-10-24 23:57:45 +05:30
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
context 'when creating' do
|
|
|
|
it 'updates the project statistics' do
|
|
|
|
delta0 = reload_stat
|
2020-10-24 23:57:45 +05:30
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
subject.save!
|
2019-09-30 21:07:59 +05:30
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
delta1 = reload_stat
|
2019-09-30 21:07:59 +05:30
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
expect(delta1).to eq(delta0 + read_attribute)
|
|
|
|
expect(delta1).to be > delta0
|
|
|
|
end
|
2019-07-31 22:56:46 +05:30
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
it 'schedules a namespace statistics worker' do
|
|
|
|
expect(Namespaces::ScheduleAggregationWorker)
|
|
|
|
.to receive(:perform_async).once
|
2019-09-30 21:07:59 +05:30
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
subject.save!
|
|
|
|
end
|
2019-07-31 22:56:46 +05:30
|
|
|
end
|
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
context 'when updating' do
|
|
|
|
let(:delta) { 42 }
|
2019-07-31 22:56:46 +05:30
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
before do
|
|
|
|
subject.save!
|
|
|
|
end
|
2019-09-04 21:01:54 +05:30
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
it 'updates project statistics' do
|
|
|
|
expect(ProjectStatistics)
|
|
|
|
.to receive(:increment_statistic)
|
|
|
|
.and_call_original
|
2019-09-30 21:07:59 +05:30
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
subject.write_attribute(statistic_attribute, read_attribute + delta)
|
2019-09-30 21:07:59 +05:30
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
expect { subject.save! }
|
|
|
|
.to change { reload_stat }
|
|
|
|
.by(delta)
|
|
|
|
end
|
2019-09-30 21:07:59 +05:30
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
it 'schedules a namespace statistics worker' do
|
|
|
|
expect(Namespaces::ScheduleAggregationWorker)
|
|
|
|
.to receive(:perform_async).once
|
2019-09-30 21:07:59 +05:30
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
subject.write_attribute(statistic_attribute, read_attribute + delta)
|
2019-09-30 21:07:59 +05:30
|
|
|
subject.save!
|
|
|
|
end
|
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
it 'avoids N + 1 queries' do
|
|
|
|
subject.write_attribute(statistic_attribute, read_attribute + delta)
|
2019-09-30 21:07:59 +05:30
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
control_count = ActiveRecord::QueryRecorder.new do
|
|
|
|
subject.save!
|
|
|
|
end
|
2019-07-31 22:56:46 +05:30
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
subject.write_attribute(statistic_attribute, read_attribute + delta)
|
|
|
|
|
|
|
|
expect do
|
|
|
|
subject.save!
|
|
|
|
end.not_to exceed_query_limit(control_count)
|
|
|
|
end
|
2019-07-31 22:56:46 +05:30
|
|
|
end
|
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
context 'when destroying' do
|
|
|
|
before do
|
|
|
|
subject.save!
|
|
|
|
end
|
2019-07-31 22:56:46 +05:30
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
it 'updates the project statistics' do
|
|
|
|
delta0 = reload_stat
|
2019-07-31 22:56:46 +05:30
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
subject.destroy!
|
2020-10-24 23:57:45 +05:30
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
delta1 = reload_stat
|
|
|
|
|
|
|
|
expect(delta1).to eq(delta0 - read_attribute)
|
|
|
|
expect(delta1).to be < delta0
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'schedules a namespace statistics worker' do
|
|
|
|
expect(Namespaces::ScheduleAggregationWorker)
|
|
|
|
.to receive(:perform_async).once
|
2019-07-31 22:56:46 +05:30
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
subject.destroy!
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when it is destroyed from the project level' do
|
|
|
|
it 'does not update the project statistics' do
|
|
|
|
expect(ProjectStatistics)
|
|
|
|
.not_to receive(:increment_statistic)
|
|
|
|
|
|
|
|
project.update!(pending_delete: true)
|
|
|
|
project.destroy!
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not schedule a namespace statistics worker' do
|
|
|
|
expect(Namespaces::ScheduleAggregationWorker)
|
|
|
|
.not_to receive(:perform_async)
|
2019-09-30 21:07:59 +05:30
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
project.update!(pending_delete: true)
|
|
|
|
project.destroy!
|
|
|
|
end
|
|
|
|
end
|
2019-09-30 21:07:59 +05:30
|
|
|
end
|
2021-01-03 14:25:43 +05:30
|
|
|
end
|
2019-09-30 21:07:59 +05:30
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
def expect_flush_counter_increments_worker_performed
|
|
|
|
expect(FlushCounterIncrementsWorker)
|
|
|
|
.to receive(:perform_in)
|
|
|
|
.with(CounterAttribute::WORKER_DELAY, project.statistics.class.name, project.statistics.id, project_statistics_name)
|
|
|
|
expect(FlushCounterIncrementsWorker)
|
|
|
|
.to receive(:perform_in)
|
|
|
|
.with(CounterAttribute::WORKER_DELAY, project.statistics.class.name, project.statistics.id, :storage_size)
|
2019-07-31 22:56:46 +05:30
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
yield
|
|
|
|
|
|
|
|
# simulate worker running now
|
|
|
|
expect(Namespaces::ScheduleAggregationWorker).to receive(:perform_async)
|
|
|
|
FlushCounterIncrementsWorker.new.perform(project.statistics.class.name, project.statistics.id, project_statistics_name)
|
|
|
|
end
|
|
|
|
|
|
|
|
if with_counter_attribute
|
|
|
|
context 'when statistic is a counter attribute', :clean_gitlab_redis_shared_state do
|
|
|
|
context 'when creating' do
|
|
|
|
it 'stores pending increments for async update' do
|
|
|
|
initial_stat = reload_stat
|
|
|
|
expected_increment = read_attribute
|
|
|
|
|
|
|
|
expect_flush_counter_increments_worker_performed do
|
|
|
|
subject.save!
|
|
|
|
|
|
|
|
expect(read_pending_increment).to eq(expected_increment)
|
|
|
|
expect(expected_increment).to be > initial_stat
|
|
|
|
expect(expected_increment).to be_positive
|
|
|
|
end
|
|
|
|
end
|
2019-07-31 22:56:46 +05:30
|
|
|
end
|
2019-09-30 21:07:59 +05:30
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
context 'when updating' do
|
|
|
|
let(:delta) { 42 }
|
|
|
|
|
|
|
|
before do
|
|
|
|
subject.save!
|
|
|
|
redis_shared_state_cleanup!
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'stores pending increments for async update' do
|
|
|
|
expect(ProjectStatistics)
|
|
|
|
.to receive(:increment_statistic)
|
|
|
|
.and_call_original
|
|
|
|
|
|
|
|
subject.write_attribute(statistic_attribute, read_attribute + delta)
|
|
|
|
|
|
|
|
expect_flush_counter_increments_worker_performed do
|
|
|
|
subject.save!
|
|
|
|
|
|
|
|
expect(read_pending_increment).to eq(delta)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'avoids N + 1 queries' do
|
|
|
|
subject.write_attribute(statistic_attribute, read_attribute + delta)
|
|
|
|
|
|
|
|
control_count = ActiveRecord::QueryRecorder.new do
|
|
|
|
subject.save!
|
|
|
|
end
|
|
|
|
|
|
|
|
subject.write_attribute(statistic_attribute, read_attribute + delta)
|
|
|
|
|
|
|
|
expect do
|
|
|
|
subject.save!
|
|
|
|
end.not_to exceed_query_limit(control_count)
|
|
|
|
end
|
|
|
|
end
|
2019-09-30 21:07:59 +05:30
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
context 'when destroying' do
|
|
|
|
before do
|
|
|
|
subject.save!
|
|
|
|
redis_shared_state_cleanup!
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'stores pending increment for async update' do
|
|
|
|
initial_stat = reload_stat
|
|
|
|
expected_increment = -read_attribute
|
|
|
|
|
|
|
|
expect_flush_counter_increments_worker_performed do
|
|
|
|
subject.destroy!
|
|
|
|
|
|
|
|
expect(read_pending_increment).to eq(expected_increment)
|
|
|
|
expect(expected_increment).to be < initial_stat
|
|
|
|
expect(expected_increment).to be_negative
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when it is destroyed from the project level' do
|
|
|
|
it 'does not update the project statistics' do
|
|
|
|
expect(ProjectStatistics)
|
|
|
|
.not_to receive(:increment_statistic)
|
|
|
|
|
|
|
|
project.update!(pending_delete: true)
|
|
|
|
project.destroy!
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not schedule a namespace statistics worker' do
|
|
|
|
expect(Namespaces::ScheduleAggregationWorker)
|
|
|
|
.not_to receive(:perform_async)
|
|
|
|
|
|
|
|
project.update!(pending_delete: true)
|
|
|
|
project.destroy!
|
|
|
|
end
|
|
|
|
end
|
2019-09-30 21:07:59 +05:30
|
|
|
end
|
|
|
|
end
|
2019-07-31 22:56:46 +05:30
|
|
|
end
|
|
|
|
end
|