Skip to content
代码片段 群组 项目
提交 3da39617 编辑于 作者: Jason Goodman's avatar Jason Goodman 提交者: Douglas Barbosa Alexandre
浏览文件

Use the namespace storage forks cost factor application setting

Refactor classes using the placeholder constant
Replace constant with application setting
Extract CostFactor module
上级 ae267f79
No related branches found
No related tags found
无相关合并请求
---
name: namespace_storage_forks_cost_factor
introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/126775
rollout_issue_url: https://gitlab.com/gitlab-org/gitlab/-/issues/419181
milestone: '16.3'
type: development
group: group::utilization
default_enabled: false
...@@ -29,7 +29,7 @@ def forks_size_reduction ...@@ -29,7 +29,7 @@ def forks_size_reduction
end end
def inverted_cost_factor_for_forks def inverted_cost_factor_for_forks
1 - ::Namespaces::Storage::RootSize::COST_FACTOR_FOR_FORKS ::Namespaces::Storage::CostFactor.inverted_cost_factor_for_forks(namespace)
end end
override :from_namespace_statistics override :from_namespace_statistics
......
...@@ -11,11 +11,7 @@ def cost_factored_storage_size ...@@ -11,11 +11,7 @@ def cost_factored_storage_size
private private
def cost_factor def cost_factor
if project.forked? && (project.root_ancestor.paid? || !project.private?) ::Namespaces::Storage::CostFactor.cost_factor_for(project)
::Namespaces::Storage::RootSize::COST_FACTOR_FOR_FORKS
else
::Namespaces::Storage::RootSize::COST_FACTOR
end
end end
override :storage_size_components override :storage_size_components
......
# frozen_string_literal: true
module Namespaces
module Storage
module CostFactor
extend self
FULL_COST = 1.0
def cost_factor_for(project)
if project.forked? && (project.root_ancestor.paid? || !project.private?)
forks_cost_factor(project.root_ancestor)
else
FULL_COST
end
end
def inverted_cost_factor_for_forks(root_namespace)
FULL_COST - forks_cost_factor(root_namespace)
end
private
def forks_cost_factor(root_namespace)
if ::Gitlab::CurrentSettings.should_check_namespace_plan? &&
::Feature.enabled?(:namespace_storage_forks_cost_factor, root_namespace)
::Gitlab::CurrentSettings.namespace_storage_forks_cost_factor
else
FULL_COST
end
end
end
end
end
...@@ -6,8 +6,6 @@ class RootSize ...@@ -6,8 +6,6 @@ class RootSize
CURRENT_SIZE_CACHE_KEY = 'root_storage_current_size' CURRENT_SIZE_CACHE_KEY = 'root_storage_current_size'
EXPIRATION_TIME = 10.minutes EXPIRATION_TIME = 10.minutes
LIMIT_CACHE_NAME = 'root_storage_size_limit' LIMIT_CACHE_NAME = 'root_storage_size_limit'
COST_FACTOR_FOR_FORKS = 1.0
COST_FACTOR = 1.0
def initialize(root_namespace) def initialize(root_namespace)
@root_namespace = root_namespace.root_ancestor # just in case the true root isn't passed @root_namespace = root_namespace.root_ancestor # just in case the true root isn't passed
......
...@@ -69,7 +69,7 @@ ...@@ -69,7 +69,7 @@
context 'with a cost factor for forks' do context 'with a cost factor for forks' do
before do before do
stub_const('Namespaces::Storage::RootSize::COST_FACTOR_FOR_FORKS', 0.1) stub_ee_application_setting(namespace_storage_forks_cost_factor: 0.1)
end end
it 'displays the storage size with the cost factor applied' do it 'displays the storage size with the cost factor applied' do
...@@ -93,7 +93,7 @@ ...@@ -93,7 +93,7 @@
context 'with a cost factor for forks' do context 'with a cost factor for forks' do
before do before do
stub_const('Namespaces::Storage::RootSize::COST_FACTOR_FOR_FORKS', 0.1) stub_ee_application_setting(namespace_storage_forks_cost_factor: 0.1)
end end
it 'displays the total storage size taking into account the cost factor' do it 'displays the total storage size taking into account the cost factor' do
......
...@@ -96,9 +96,13 @@ ...@@ -96,9 +96,13 @@
describe '#cost_factored_storage_size', :saas do describe '#cost_factored_storage_size', :saas do
let_it_be(:group) { create(:group_with_plan, plan: :ultimate_plan) } let_it_be(:group) { create(:group_with_plan, plan: :ultimate_plan) }
before do
stub_ee_application_setting(check_namespace_plan: true)
end
context 'with a cost factor for forks' do context 'with a cost factor for forks' do
before do before do
stub_const("::Namespaces::Storage::RootSize::COST_FACTOR_FOR_FORKS", 0.05) stub_ee_application_setting(namespace_storage_forks_cost_factor: 0.05)
end end
context 'with a free plan' do context 'with a free plan' do
...@@ -166,7 +170,7 @@ ...@@ -166,7 +170,7 @@
context 'with a fork cost factor of 1' do context 'with a fork cost factor of 1' do
before do before do
stub_const("::Namespaces::Storage::RootSize::COST_FACTOR_FOR_FORKS", 1.0) stub_ee_application_setting(namespace_storage_forks_cost_factor: 1.0)
end end
it 'considers forks to take up their full actual disk storage' do it 'considers forks to take up their full actual disk storage' do
...@@ -179,7 +183,7 @@ ...@@ -179,7 +183,7 @@
context 'with a fork cost factor of 0' do context 'with a fork cost factor of 0' do
before do before do
stub_const("::Namespaces::Storage::RootSize::COST_FACTOR_FOR_FORKS", 0) stub_ee_application_setting(namespace_storage_forks_cost_factor: 0)
end end
it 'considers forks to take up no storage at all' do it 'considers forks to take up no storage at all' do
...@@ -192,7 +196,7 @@ ...@@ -192,7 +196,7 @@
context 'when the cost factor would result in a fractional storage_size' do context 'when the cost factor would result in a fractional storage_size' do
before do before do
stub_const("::Namespaces::Storage::RootSize::COST_FACTOR_FOR_FORKS", 0.1) stub_ee_application_setting(namespace_storage_forks_cost_factor: 0.1)
end end
it 'rounds to the nearest integer' do it 'rounds to the nearest integer' do
......
...@@ -82,7 +82,8 @@ ...@@ -82,7 +82,8 @@
context 'when there is a cost factor for forks' do context 'when there is a cost factor for forks' do
before do before do
stub_const('Namespaces::Storage::RootSize::COST_FACTOR_FOR_FORKS', 0.1) stub_ee_application_setting(check_namespace_plan: true)
stub_ee_application_setting(namespace_storage_forks_cost_factor: 0.1)
end end
where(:plan, :fork_visibility) do where(:plan, :fork_visibility) do
......
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Namespaces::Storage::CostFactor, feature_category: :consumables_cost_management do
let(:full_cost) { 1.0 }
let(:forks_cost_factor) { 0.2 }
let(:paid_group) { build_group }
let(:free_group) { build_group(paid: false) }
before do
stub_ee_application_setting(namespace_storage_forks_cost_factor: forks_cost_factor)
stub_ee_application_setting(check_namespace_plan: true)
stub_feature_flags(namespace_storage_forks_cost_factor: true)
end
describe '.cost_factor_for' do
it 'returns the forks cost factor for a project that is a fork' do
project = build_fork(group: paid_group)
expect(described_class.cost_factor_for(project)).to eq(forks_cost_factor)
end
it 'returns full cost for a project that is not a fork' do
project = build_project(group: paid_group)
expect(described_class.cost_factor_for(project)).to eq(full_cost)
end
it 'returns full cost for a private fork in a free namespace' do
project = build_fork(group: free_group, visibility_level: Gitlab::VisibilityLevel::PRIVATE)
expect(described_class.cost_factor_for(project)).to eq(full_cost)
end
it 'returns full cost when the feature flag is false' do
stub_feature_flags(namespace_storage_forks_cost_factor: false)
project = build_fork(group: paid_group)
expect(described_class.cost_factor_for(project)).to eq(full_cost)
end
it 'returns full cost when namespace plans are not checked' do
stub_ee_application_setting(check_namespace_plan: false)
project = build_fork(group: paid_group)
expect(described_class.cost_factor_for(project)).to eq(full_cost)
end
end
describe '.inverted_cost_factor_for_forks' do
it 'returns the inverse of the cost factor for forks' do
expect(described_class.inverted_cost_factor_for_forks(paid_group)).to eq(0.8)
end
it 'returns the inverse of full cost when the feature flag is false' do
stub_feature_flags(namespace_storage_forks_cost_factor: false)
expect(described_class.inverted_cost_factor_for_forks(paid_group)).to eq(0)
end
it 'returns the inverse of full cost when namespace plans are not checked' do
stub_ee_application_setting(check_namespace_plan: false)
expect(described_class.inverted_cost_factor_for_forks(paid_group)).to eq(0)
end
end
def build_group(paid: true)
group = build_stubbed(:group)
allow(group).to receive(:paid?).and_return(paid)
group
end
def build_project(group:, visibility_level: Gitlab::VisibilityLevel::PUBLIC)
project = build_stubbed(:project, visibility_level: visibility_level)
allow(project).to receive(:root_ancestor).and_return(group)
project
end
def build_fork(group:, visibility_level: Gitlab::VisibilityLevel::PUBLIC)
project = build_project(group: group, visibility_level: visibility_level)
allow(project).to receive(:forked?).and_return(true)
project
end
end
...@@ -212,7 +212,8 @@ ...@@ -212,7 +212,8 @@
end end
before do before do
stub_const("#{described_class}::COST_FACTOR_FOR_FORKS", 0.05) stub_ee_application_setting(check_namespace_plan: true)
stub_ee_application_setting(namespace_storage_forks_cost_factor: 0.05)
end end
it 'returns the cost factored storage size' do it 'returns the cost factored storage size' do
......
0% 加载中 .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册