2018-10-06 19:10:08 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-04-19 12:53:07 -04:00
|
|
|
require 'flipper/adapters/active_record'
|
|
|
|
require 'flipper/adapters/active_support_cache_store'
|
|
|
|
|
2017-05-31 17:06:01 -04:00
|
|
|
class Feature
|
2019-09-13 09:26:31 -04:00
|
|
|
prepend_if_ee('EE::Feature') # rubocop: disable Cop/InjectEnterpriseEditionModule
|
|
|
|
|
2017-05-31 17:06:01 -04:00
|
|
|
# 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'
|
2017-10-30 11:10:31 -04:00
|
|
|
|
|
|
|
def self.feature_names
|
|
|
|
pluck(:key)
|
|
|
|
end
|
2017-05-31 17:06:01 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
class FlipperGate < Flipper::Adapters::ActiveRecord::Gate
|
|
|
|
superclass.table_name = 'feature_gates'
|
|
|
|
end
|
|
|
|
|
|
|
|
class << self
|
2017-06-21 10:49:51 -04:00
|
|
|
delegate :group, to: :flipper
|
|
|
|
|
2017-05-31 17:06:01 -04:00
|
|
|
def all
|
|
|
|
flipper.features.to_a
|
|
|
|
end
|
|
|
|
|
|
|
|
def get(key)
|
|
|
|
flipper.feature(key)
|
|
|
|
end
|
|
|
|
|
2017-10-30 11:10:31 -04:00
|
|
|
def persisted_names
|
2020-02-12 07:09:01 -05:00
|
|
|
return [] unless Gitlab::Database.exists?
|
|
|
|
|
2019-06-18 16:50:46 -04:00
|
|
|
Gitlab::SafeRequestStore[:flipper_persisted_names] ||=
|
|
|
|
begin
|
|
|
|
# We saw on GitLab.com, this database request was called 2300
|
|
|
|
# times/s. Let's cache it for a minute to avoid that load.
|
2019-07-02 09:58:47 -04:00
|
|
|
Gitlab::ThreadMemoryCache.cache_backend.fetch('flipper:persisted_names', expires_in: 1.minute) do
|
|
|
|
FlipperFeature.feature_names
|
|
|
|
end
|
2019-06-18 16:50:46 -04:00
|
|
|
end
|
2017-10-30 11:10:31 -04:00
|
|
|
end
|
|
|
|
|
2017-05-31 17:06:01 -04:00
|
|
|
def persisted?(feature)
|
|
|
|
# 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.
|
2018-07-19 20:05:24 -04:00
|
|
|
persisted_names.include?(feature.name.to_s)
|
2017-05-31 17:06:01 -04:00
|
|
|
end
|
|
|
|
|
2018-09-05 09:14:16 -04:00
|
|
|
# use `default_enabled: true` to default the flag to being `enabled`
|
|
|
|
# unless set explicitly. The default is `disabled`
|
2018-09-04 14:34:37 -04:00
|
|
|
def enabled?(key, thing = nil, default_enabled: false)
|
2020-01-10 07:07:47 -05:00
|
|
|
# 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?
|
|
|
|
|
2018-09-04 14:34:37 -04:00
|
|
|
feature = Feature.get(key)
|
|
|
|
|
2018-09-05 09:14:16 -04:00
|
|
|
# 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`
|
|
|
|
!default_enabled || Feature.persisted?(feature) ? feature.enabled?(thing) : true
|
2017-06-21 10:49:51 -04:00
|
|
|
end
|
|
|
|
|
2018-09-04 14:34:37 -04:00
|
|
|
def disabled?(key, thing = nil, default_enabled: false)
|
2018-08-20 14:52:56 -04:00
|
|
|
# we need to make different method calls to make it easy to mock / define expectations in test mode
|
2018-09-04 14:34:37 -04:00
|
|
|
thing.nil? ? !enabled?(key, default_enabled: default_enabled) : !enabled?(key, thing, default_enabled: default_enabled)
|
Add repository languages for projects
Our friends at GitHub show the programming languages for a long time,
and inspired by that this commit means to create about the same
functionality.
Language detection is done through Linguist, as before, where the
difference is that we cache the result in the database. Also, Gitaly can
incrementaly scan a repository. This is done through a shell out, which
creates overhead of about 3s each run. For now this won't be improved.
Scans are triggered by pushed to the default branch, usually `master`.
However, one exception to this rule the charts page. If we're requesting
this expensive data anyway, we just cache it in the database.
Edge cases where there is no repository, or its empty are caught in the
Repository model. This makes use of Redis caching, which is probably
already loaded.
The added model is called RepositoryLanguage, which will make it harder
if/when GitLab supports multiple repositories per project. However, for
now I think this shouldn't be a concern. Also, Language could be
confused with the i18n languages and felt like the current name was
suiteable too.
Design of the Project#Show page is done with help from @dimitrieh. This
change is not visible to the end user unless detections are done.
2018-06-06 07:10:59 -04:00
|
|
|
end
|
|
|
|
|
2017-06-21 10:49:51 -04:00
|
|
|
def enable(key, thing = true)
|
|
|
|
get(key).enable(thing)
|
|
|
|
end
|
|
|
|
|
|
|
|
def disable(key, thing = false)
|
|
|
|
get(key).disable(thing)
|
2017-05-12 12:44:03 -04:00
|
|
|
end
|
|
|
|
|
2017-06-21 10:49:51 -04:00
|
|
|
def enable_group(key, group)
|
|
|
|
get(key).enable_group(group)
|
2017-05-12 12:44:03 -04:00
|
|
|
end
|
|
|
|
|
2017-06-21 10:49:51 -04:00
|
|
|
def disable_group(key, group)
|
|
|
|
get(key).disable_group(group)
|
2017-05-12 12:44:03 -04:00
|
|
|
end
|
|
|
|
|
2019-07-30 20:41:11 -04:00
|
|
|
def remove(key)
|
|
|
|
feature = get(key)
|
|
|
|
return unless persisted?(feature)
|
|
|
|
|
|
|
|
feature.remove
|
|
|
|
end
|
|
|
|
|
2017-05-31 17:06:01 -04:00
|
|
|
def flipper
|
2018-10-05 10:20:19 -04:00
|
|
|
if Gitlab::SafeRequestStore.active?
|
|
|
|
Gitlab::SafeRequestStore[:flipper] ||= build_flipper_instance
|
|
|
|
else
|
|
|
|
@flipper ||= build_flipper_instance
|
|
|
|
end
|
2018-05-31 12:12:48 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def build_flipper_instance
|
|
|
|
Flipper.new(flipper_adapter).tap { |flip| flip.memoize = true }
|
2017-05-31 17:06:01 -04:00
|
|
|
end
|
2017-07-10 09:54:49 -04:00
|
|
|
|
|
|
|
# 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-04-19 12:53:07 -04:00
|
|
|
|
|
|
|
def flipper_adapter
|
|
|
|
active_record_adapter = Flipper::Adapters::ActiveRecord.new(
|
|
|
|
feature_class: FlipperFeature,
|
|
|
|
gate_class: FlipperGate)
|
|
|
|
|
2019-07-02 14:19:30 -04:00
|
|
|
# Redis L2 cache
|
|
|
|
redis_cache_adapter =
|
|
|
|
Flipper::Adapters::ActiveSupportCacheStore.new(
|
|
|
|
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
|
2018-04-19 12:53:07 -04:00
|
|
|
Flipper::Adapters::ActiveSupportCacheStore.new(
|
2019-07-02 14:19:30 -04:00
|
|
|
redis_cache_adapter,
|
|
|
|
l1_cache_backend,
|
|
|
|
expires_in: 1.minute)
|
|
|
|
end
|
|
|
|
|
|
|
|
def l1_cache_backend
|
|
|
|
Gitlab::ThreadMemoryCache.cache_backend
|
|
|
|
end
|
|
|
|
|
|
|
|
def l2_cache_backend
|
|
|
|
Rails.cache
|
2018-04-19 12:53:07 -04:00
|
|
|
end
|
2017-05-31 17:06:01 -04:00
|
|
|
end
|
2019-01-07 05:07:14 -05:00
|
|
|
|
|
|
|
class Target
|
|
|
|
attr_reader :params
|
|
|
|
|
|
|
|
def initialize(params)
|
|
|
|
@params = params
|
|
|
|
end
|
|
|
|
|
|
|
|
def gate_specified?
|
2019-02-07 15:27:03 -05:00
|
|
|
%i(user project group feature_group).any? { |key| params.key?(key) }
|
2019-01-07 05:07:14 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def targets
|
2019-02-07 15:27:03 -05:00
|
|
|
[feature_group, user, project, group].compact
|
2019-01-07 05:07:14 -05:00
|
|
|
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-02-07 15:27:03 -05:00
|
|
|
|
|
|
|
def group
|
|
|
|
return unless params.key?(:group)
|
|
|
|
|
|
|
|
Group.find_by_full_path(params[:group])
|
|
|
|
end
|
2019-01-07 05:07:14 -05:00
|
|
|
end
|
2017-05-31 17:06:01 -04:00
|
|
|
end
|