Commit 9c44c30f authored by James Fargher's avatar James Fargher

Merge branch 'mc/backstage/reduce-db-updates-ci-minute-reset' into 'master'

Reduce number of CI reset DB updates

See merge request gitlab-org/gitlab!53740
parents b3be7a8b 73fbbade
......@@ -31,6 +31,7 @@ class ProjectStatistics < ApplicationRecord
scope :for_project_ids, ->(project_ids) { where(project_id: project_ids) }
scope :for_namespaces, -> (namespaces) { where(namespace: namespaces) }
scope :with_any_ci_minutes_used, -> { where.not(shared_runners_seconds: 0) }
def total_repository_size
repository_size + lfs_objects_size
......
---
title: Reset CI minutes only for namespaces that used minutes.
merge_request: 53740
author:
type: changed
......@@ -6,4 +6,5 @@ class NamespaceStatistics < ApplicationRecord
validates :namespace, presence: true
scope :for_namespaces, -> (namespaces) { where(namespace: namespaces) }
scope :with_any_ci_minutes_used, -> { where.not(shared_runners_seconds: 0) }
end
......@@ -99,11 +99,15 @@ module Ci
end
def reset_shared_runners_seconds!(namespaces)
namespace_relation = NamespaceStatistics.for_namespaces(namespaces)
namespace_relation.update_all(shared_runners_seconds: 0, shared_runners_seconds_last_reset: Time.current)
project_relation = ::ProjectStatistics.for_namespaces(namespaces)
project_relation.update_all(shared_runners_seconds: 0, shared_runners_seconds_last_reset: Time.current)
NamespaceStatistics
.for_namespaces(namespaces)
.with_any_ci_minutes_used
.update_all(shared_runners_seconds: 0, shared_runners_seconds_last_reset: Time.current)
::ProjectStatistics
.for_namespaces(namespaces)
.with_any_ci_minutes_used
.update_all(shared_runners_seconds: 0, shared_runners_seconds_last_reset: Time.current)
end
def reset_ci_minutes_notifications!(namespaces)
......
......@@ -10,7 +10,7 @@ class ClearSharedRunnersMinutesWorker # rubocop:disable Scalability/IdempotentWo
feature_category :continuous_integration
LEASE_TIMEOUT = 3600
TIME_SPREAD = 24.hours.seconds.freeze
TIME_SPREAD = 3.hours.seconds.freeze
BATCH_SIZE = 100_000
def perform
......
......@@ -85,17 +85,6 @@ RSpec.describe Ci::Minutes::BatchResetService do
expect(namespace.last_ci_minutes_usage_notification_level).to be_nil
end
end
it 'touches the shared_runners_seconds_last_reset for all namespaces' do
subject
expect(
[
namespace_1.reload, namespace_2.reload, namespace_3.reload,
namespace_4.reload, namespace_5.reload
].map(&:shared_runners_seconds_last_reset)
).to all(be_within(1.second).of(Time.current))
end
end
context 'when ID range is not provided' do
......@@ -121,17 +110,6 @@ RSpec.describe Ci::Minutes::BatchResetService do
expect(namespace_6.last_ci_minutes_notification_at).to be_nil
expect(namespace_6.last_ci_minutes_usage_notification_level).to be_nil
end
it 'touches the shared_runners_seconds_last_reset for all namespaces' do
subject
expect(
[
namespace_1.reload, namespace_2.reload, namespace_3.reload,
namespace_4.reload, namespace_5.reload, namespace_6.reload
].map(&:shared_runners_seconds_last_reset)
).to all(be_within(1.second).of(Time.current))
end
end
context 'when an ActiveRecordError is raised' do
......
......@@ -147,12 +147,12 @@ RSpec.describe ClearSharedRunnersMinutesWorker do
end
it 'runs a worker per batch', :aggregate_failures do
# Spread evenly accross 24 hours (86,400 seconds)
# Spread evenly accross 3 hours (10,800 seconds)
expect(Ci::BatchResetMinutesWorker).to receive(:perform_in).with(0.seconds, 2, 4)
expect(Ci::BatchResetMinutesWorker).to receive(:perform_in).with(21600.seconds, 5, 7)
expect(Ci::BatchResetMinutesWorker).to receive(:perform_in).with(43200.seconds, 8, 10)
expect(Ci::BatchResetMinutesWorker).to receive(:perform_in).with(64800.seconds, 11, 13)
expect(Ci::BatchResetMinutesWorker).to receive(:perform_in).with(86400.seconds, 14, 16)
expect(Ci::BatchResetMinutesWorker).to receive(:perform_in).with(2700.seconds, 5, 7)
expect(Ci::BatchResetMinutesWorker).to receive(:perform_in).with(5400.seconds, 8, 10)
expect(Ci::BatchResetMinutesWorker).to receive(:perform_in).with(8100.seconds, 11, 13)
expect(Ci::BatchResetMinutesWorker).to receive(:perform_in).with(10800.seconds, 14, 16)
subject
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment