Commit 8879b166 authored by Fabio Pitino's avatar Fabio Pitino Committed by Sean McGivern

Enable CI minutes update to run idempotently

Add a feature flag to control the new behavior.
Pass build_id to the worker class to track CI
minutes idempotently for jobs in new deployment.
parent 1370dc78
...@@ -14,14 +14,14 @@ module Ci ...@@ -14,14 +14,14 @@ module Ci
return unless consumption > 0 return unless consumption > 0
update_minutes(consumption) update_minutes(consumption, build)
compare_with_live_consumption(build, consumption) compare_with_live_consumption(build, consumption)
end end
private private
def update_minutes(consumption) def update_minutes(consumption, build)
::Ci::Minutes::UpdateProjectAndNamespaceUsageWorker.perform_async(consumption, project.id, namespace.id) ::Ci::Minutes::UpdateProjectAndNamespaceUsageWorker.perform_async(consumption, project.id, namespace.id, build.id)
end end
def compare_with_live_consumption(build, consumption) def compare_with_live_consumption(build, consumption)
......
...@@ -22,7 +22,7 @@ module Ci ...@@ -22,7 +22,7 @@ module Ci
# TODO: fix this condition after the next deployment when `build_id` # TODO: fix this condition after the next deployment when `build_id`
# is made a mandatory argument. # is made a mandatory argument.
# https://gitlab.com/gitlab-org/gitlab/-/issues/331785 # https://gitlab.com/gitlab-org/gitlab/-/issues/331785
if @build_id if @build_id && idempotent_consumption_enabled?
ensure_idempotency { track_usage_of_monthly_minutes(consumption) } ensure_idempotency { track_usage_of_monthly_minutes(consumption) }
else else
track_usage_of_monthly_minutes(consumption) track_usage_of_monthly_minutes(consumption)
...@@ -103,7 +103,10 @@ module Ci ...@@ -103,7 +103,10 @@ module Ci
# Ensure we only add the CI minutes consumption once for the given build # Ensure we only add the CI minutes consumption once for the given build
# even if the worker is retried. # even if the worker is retried.
def ensure_idempotency def ensure_idempotency
return if already_completed? if already_completed?
::Gitlab::AppJsonLogger.info(event: 'ci_minutes_consumption_already_updated', build_id: @build_id)
return
end
yield yield
...@@ -121,6 +124,15 @@ module Ci ...@@ -121,6 +124,15 @@ module Ci
redis.exists(idempotency_cache_key) redis.exists(idempotency_cache_key)
end end
end end
# When running this worker the project might have been deleted.
# In this case we consider the feature flag disabled for backward
# compatibility.
def idempotent_consumption_enabled?
return false unless @project
Feature.enabled?(:idempotent_ci_minutes_consumption, @project, default_enabled: :yaml)
end
end end
end end
end end
---
name: idempotent_ci_minutes_consumption
introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/69994
rollout_issue_url: https://gitlab.com/gitlab-org/gitlab/-/issues/340517
milestone: '14.4'
type: development
group: group::pipeline execution
default_enabled: false
...@@ -170,6 +170,23 @@ RSpec.describe Ci::Minutes::UpdateProjectAndNamespaceUsageService do ...@@ -170,6 +170,23 @@ RSpec.describe Ci::Minutes::UpdateProjectAndNamespaceUsageService do
it_behaves_like 'does not update monthly consumption' it_behaves_like 'does not update monthly consumption'
it_behaves_like 'updates legacy consumption' # not idempotent / to be removed it_behaves_like 'updates legacy consumption' # not idempotent / to be removed
it 'logs the event' do
expect(::Gitlab::AppJsonLogger)
.to receive(:info)
.with(event: 'ci_minutes_consumption_already_updated', build_id: build.id)
.and_call_original
subject
end
context 'when feature flag idempotent_ci_minutes_consumption is disabled' do
before do
stub_feature_flags(idempotent_ci_minutes_consumption: false)
end
it_behaves_like 'updates monthly consumption'
end
context 'when build_id is not provided as parameter' do context 'when build_id is not provided as parameter' do
let(:service) { described_class.new(project.id, namespace.id) } let(:service) { described_class.new(project.id, namespace.id) }
......
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