debian-mirror-gitlab/lib/feature.rb

245 lines
6.8 KiB
Ruby
Raw Normal View History

2018-12-13 13:39:08 +05:30
# frozen_string_literal: true
2018-11-08 19:23:39 +05:30
require 'flipper/adapters/active_record'
require 'flipper/adapters/active_support_cache_store'
2017-09-10 17:25:29 +05:30
class Feature
# Classes to override flipper table names
class FlipperFeature < Flipper::Adapters::ActiveRecord::Feature
# Using `self.table_name` won't work. ActiveRecord bug?
superclass.table_name = 'features'
2018-03-17 18:26:18 +05:30
def self.feature_names
pluck(:key)
end
2017-09-10 17:25:29 +05:30
end
class FlipperGate < Flipper::Adapters::ActiveRecord::Gate
superclass.table_name = 'feature_gates'
end
2020-11-24 15:15:51 +05:30
class ActiveSupportCacheStoreAdapter < Flipper::Adapters::ActiveSupportCacheStore
# overrides methods in EE
end
2020-06-23 00:09:42 +05:30
InvalidFeatureFlagError = Class.new(Exception) # rubocop:disable Lint/InheritException
2017-09-10 17:25:29 +05:30
class << self
delegate :group, to: :flipper
def all
flipper.features.to_a
end
def get(key)
flipper.feature(key)
end
2018-03-17 18:26:18 +05:30
def persisted_names
2020-03-13 15:44:24 +05:30
return [] unless Gitlab::Database.exists?
2020-07-28 23:09:34 +05:30
# This loads names of all stored feature flags
# and returns a stable Set in the following order:
# - Memoized: using Gitlab::SafeRequestStore or @flipper
# - L1: using Process cache
# - L2: using Redis cache
# - DB: using a single SQL query
flipper.adapter.features
2018-03-17 18:26:18 +05:30
end
2020-06-23 00:09:42 +05:30
def persisted_name?(feature_name)
2017-09-10 17:25:29 +05:30
# Flipper creates on-memory features when asked for a not-yet-created one.
# If we want to check if a feature has been actually set, we look for it
# on the persisted features list.
2020-06-23 00:09:42 +05:30
persisted_names.include?(feature_name.to_s)
2017-09-10 17:25:29 +05:30
end
2018-11-20 20:47:30 +05:30
# use `default_enabled: true` to default the flag to being `enabled`
# unless set explicitly. The default is `disabled`
2020-06-23 00:09:42 +05:30
# TODO: remove the `default_enabled:` and read it from the `defintion_yaml`
# check: https://gitlab.com/gitlab-org/gitlab/-/issues/30228
2020-07-28 23:09:34 +05:30
def enabled?(key, thing = nil, type: :development, default_enabled: false)
2020-06-23 00:09:42 +05:30
if check_feature_flags_definition?
if thing && !thing.respond_to?(:flipper_id)
raise InvalidFeatureFlagError,
"The thing '#{thing.class.name}' for feature flag '#{key}' needs to include `FeatureGate` or implement `flipper_id`"
end
2020-07-28 23:09:34 +05:30
Feature::Definition.valid_usage!(key, type: type, default_enabled: default_enabled)
2020-06-23 00:09:42 +05:30
end
2020-03-13 15:44:24 +05:30
# During setup the database does not exist yet. So we haven't stored a value
# for the feature yet and return the default.
return default_enabled unless Gitlab::Database.exists?
2020-06-23 00:09:42 +05:30
feature = get(key)
2018-11-20 20:47:30 +05:30
# If we're not default enabling the flag or the feature has been set, always evaluate.
# `persisted?` can potentially generate DB queries and also checks for inclusion
# in an array of feature names (177 at last count), possibly reducing performance by half.
# So we only perform the `persisted` check if `default_enabled: true`
2020-06-23 00:09:42 +05:30
!default_enabled || Feature.persisted_name?(feature.name) ? feature.enabled?(thing) : true
2017-09-10 17:25:29 +05:30
end
2020-07-28 23:09:34 +05:30
def disabled?(key, thing = nil, type: :development, default_enabled: false)
2018-11-20 20:47:30 +05:30
# we need to make different method calls to make it easy to mock / define expectations in test mode
2020-07-28 23:09:34 +05:30
thing.nil? ? !enabled?(key, type: type, default_enabled: default_enabled) : !enabled?(key, thing, type: type, default_enabled: default_enabled)
2018-11-18 11:00:15 +05:30
end
2017-09-10 17:25:29 +05:30
def enable(key, thing = true)
get(key).enable(thing)
end
def disable(key, thing = false)
get(key).disable(thing)
end
def enable_group(key, group)
get(key).enable_group(group)
end
def disable_group(key, group)
get(key).disable_group(group)
end
2020-06-23 00:09:42 +05:30
def enable_percentage_of_time(key, percentage)
get(key).enable_percentage_of_time(percentage)
end
2019-10-12 21:52:04 +05:30
2020-06-23 00:09:42 +05:30
def disable_percentage_of_time(key)
get(key).disable_percentage_of_time
2019-10-12 21:52:04 +05:30
end
2020-06-23 00:09:42 +05:30
def enable_percentage_of_actors(key, percentage)
get(key).enable_percentage_of_actors(percentage)
2018-11-08 19:23:39 +05:30
end
2020-06-23 00:09:42 +05:30
def disable_percentage_of_actors(key)
get(key).disable_percentage_of_actors
end
def remove(key)
return unless persisted_name?(key)
get(key).remove
end
def reset
Gitlab::SafeRequestStore.delete(:flipper) if Gitlab::SafeRequestStore.active?
@flipper = nil
2017-09-10 17:25:29 +05:30
end
# This method is called from config/initializers/flipper.rb and can be used
# to register Flipper groups.
# See https://docs.gitlab.com/ee/development/feature_flags.html#feature-groups
def register_feature_groups
end
2018-11-08 19:23:39 +05:30
2020-07-28 23:09:34 +05:30
def register_definitions
return unless check_feature_flags_definition?
2021-01-03 14:25:43 +05:30
Feature::Definition.reload!
2020-07-28 23:09:34 +05:30
end
2020-11-24 15:15:51 +05:30
def register_hot_reloader
return unless check_feature_flags_definition?
Feature::Definition.register_hot_reloader!
end
2020-06-23 00:09:42 +05:30
private
def flipper
if Gitlab::SafeRequestStore.active?
Gitlab::SafeRequestStore[:flipper] ||= build_flipper_instance
else
@flipper ||= build_flipper_instance
end
end
def build_flipper_instance
2018-11-08 19:23:39 +05:30
active_record_adapter = Flipper::Adapters::ActiveRecord.new(
feature_class: FlipperFeature,
gate_class: FlipperGate)
2019-09-30 21:07:59 +05:30
# Redis L2 cache
redis_cache_adapter =
2020-11-24 15:15:51 +05:30
ActiveSupportCacheStoreAdapter.new(
2019-09-30 21:07:59 +05:30
active_record_adapter,
l2_cache_backend,
expires_in: 1.hour)
# Thread-local L1 cache: use a short timeout since we don't have a
# way to expire this cache all at once
2020-06-23 00:09:42 +05:30
flipper_adapter = Flipper::Adapters::ActiveSupportCacheStore.new(
2019-09-30 21:07:59 +05:30
redis_cache_adapter,
l1_cache_backend,
expires_in: 1.minute)
2020-06-23 00:09:42 +05:30
Flipper.new(flipper_adapter).tap do |flip|
flip.memoize = true
end
end
def check_feature_flags_definition?
# We want to check feature flags usage only when
# running in development or test environment
Gitlab.dev_or_test_env?
2019-09-30 21:07:59 +05:30
end
def l1_cache_backend
2020-05-24 23:13:21 +05:30
Gitlab::ProcessMemoryCache.cache_backend
2019-09-30 21:07:59 +05:30
end
def l2_cache_backend
Rails.cache
2018-11-08 19:23:39 +05:30
end
2017-09-10 17:25:29 +05:30
end
2019-03-02 22:35:43 +05:30
class Target
attr_reader :params
def initialize(params)
@params = params
end
def gate_specified?
2019-07-07 11:18:12 +05:30
%i(user project group feature_group).any? { |key| params.key?(key) }
2019-03-02 22:35:43 +05:30
end
def targets
2019-07-07 11:18:12 +05:30
[feature_group, user, project, group].compact
2019-03-02 22:35:43 +05:30
end
private
# rubocop: disable CodeReuse/ActiveRecord
def feature_group
return unless params.key?(:feature_group)
Feature.group(params[:feature_group])
end
# rubocop: enable CodeReuse/ActiveRecord
def user
return unless params.key?(:user)
UserFinder.new(params[:user]).find_by_username!
end
def project
return unless params.key?(:project)
Project.find_by_full_path(params[:project])
end
2019-07-07 11:18:12 +05:30
def group
return unless params.key?(:group)
Group.find_by_full_path(params[:group])
end
2019-03-02 22:35:43 +05:30
end
2017-09-10 17:25:29 +05:30
end
2020-06-23 00:09:42 +05:30
2020-11-24 15:15:51 +05:30
Feature::ActiveSupportCacheStoreAdapter.prepend_if_ee('EE::Feature::ActiveSupportCacheStoreAdapter')