Commit 55810a99 authored by jejacks0n's avatar jejacks0n

Add a caching layer on Gitlab::Redis::SharedState

- Version 0.4.3 has improved caching behavior and order of operations
when determining the variant.
parent d8a2b7a3
......@@ -479,7 +479,7 @@ gem 'flipper', '~> 0.17.1'
gem 'flipper-active_record', '~> 0.17.1'
gem 'flipper-active_support_cache_store', '~> 0.17.1'
gem 'unleash', '~> 0.1.5'
gem 'gitlab-experiment', '~> 0.4.2'
gem 'gitlab-experiment', '~> 0.4.3'
# Structured logging
gem 'lograge', '~> 0.5'
......
......@@ -427,7 +427,7 @@ GEM
github-markup (1.7.0)
gitlab-chronic (0.10.5)
numerizer (~> 0.2)
gitlab-experiment (0.4.2)
gitlab-experiment (0.4.3)
activesupport (>= 3.0)
scientist (~> 1.5, >= 1.5.0)
gitlab-fog-azure-rm (1.0.0)
......@@ -1357,7 +1357,7 @@ DEPENDENCIES
gitaly (~> 13.7.0.pre.rc1)
github-markup (~> 1.7.0)
gitlab-chronic (~> 0.10.5)
gitlab-experiment (~> 0.4.2)
gitlab-experiment (~> 0.4.3)
gitlab-fog-azure-rm (~> 1.0)
gitlab-labkit (= 0.13.3)
gitlab-license (~> 1.0)
......
......@@ -21,4 +21,63 @@ class ApplicationExperiment < Gitlab::Experiment
def resolve_variant_name
variant_names.first if Feature.enabled?(name, self, type: :experiment)
end
# Cache is an implementation on top of Gitlab::Redis::SharedState that also
# adheres to the ActiveSupport::Cache::Store interface and uses the redis
# hash data type.
#
# Since Gitlab::Experiment can use any type of caching layer, utilizing the
# long lived shared state interface here gives us an efficient way to store
# context keys and the variant they've been assigned -- while also giving us
# a simple way to clean up an experiments data upon resolution.
#
# The data structure:
# key: experiment.name
# fields: context key => variant name
#
# The keys are expected to be `experiment_name:context_key`, which is the
# default cache key strategy. So running `cache.fetch("foo:bar", "value")`
# would create/update a hash with the key of "foo", with a field named
# "bar" that has "value" assigned to it.
class Cache < ActiveSupport::Cache::Store
# Clears the entire cache for a given experiment. Be careful with this
# since it would reset all resolved variants for the entire experiment.
def clear(key:)
pool do |redis|
unless %w[hash none].include?(redis.type(hkey(key)[0]))
raise ArgumentError, 'invalid call to clear a non-hash cache key'
end
redis.del(hkey(key)[0])
end
end
private
def pool
raise ArgumentError, 'missing block' unless block_given?
Gitlab::Redis::SharedState.with { |redis| yield redis }
end
def hkey(key)
key.split(':') # this assumes the default strategy in gitlab-experiment
end
def read_entry(key, **options)
value = pool { |redis| redis.hget(*hkey(key)) }
value.nil? ? nil : ActiveSupport::Cache::Entry.new(value)
end
def write_entry(key, entry, **options)
return false unless Feature.enabled?(:caching_experiments)
return false if entry.value.blank? # don't cache any empty values
pool { |redis| redis.hset(*hkey(key), entry.value) }
end
def delete_entry(key, **options)
pool { |redis| redis.hdel(*hkey(key)) }
end
end
end
---
name: caching_experiments
introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/49669
rollout_issue_url:
milestone: '13.7'
type: development
group: group::adoption
default_enabled: false
......@@ -2,4 +2,5 @@
Gitlab::Experiment.configure do |config|
config.base_class = 'ApplicationExperiment'
config.cache = ApplicationExperiment::Cache.new
end
......@@ -64,6 +64,11 @@ RSpec.describe Projects::IssuesController do
end
describe 'the null hypothesis experiment', :snowplow do
before do
# disable experiment caching
allow(Gitlab::Experiment::Configuration).to receive(:cache).and_return(nil)
end
it 'defines the expected before actions' do
expect(controller).to use_before_action(:run_null_hypothesis_experiment)
end
......
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe ApplicationExperiment::Cache do
let(:key_name) { 'experiment_name' }
let(:field_name) { 'abc123' }
let(:key_field) { [key_name, field_name].join(':') }
let(:shared_state) { Gitlab::Redis::SharedState }
around do |example|
shared_state.with { |r| r.del(key_name) }
example.run
shared_state.with { |r| r.del(key_name) }
end
it "allows reading, writing and deleting" do
# we test them all together because they are largely interdependent
expect(subject.read(key_field)).to be_nil
expect(shared_state.with { |r| r.hget(key_name, field_name) }).to be_nil
subject.write(key_field, 'value')
expect(subject.read(key_field)).to eq('value')
expect(shared_state.with { |r| r.hget(key_name, field_name) }).to eq('value')
subject.delete(key_field)
expect(subject.read(key_field)).to be_nil
expect(shared_state.with { |r| r.hget(key_name, field_name) }).to be_nil
end
it "handles the fetch with a block behavior (which is read/write)" do
expect(subject.fetch(key_field) { 'value1' }).to eq('value1') # rubocop:disable Style/RedundantFetchBlock
expect(subject.fetch(key_field) { 'value2' }).to eq('value1') # rubocop:disable Style/RedundantFetchBlock
end
it "can clear a whole experiment cache key" do
subject.write(key_field, 'value')
subject.clear(key: key_field)
expect(shared_state.with { |r| r.get(key_name) }).to be_nil
end
it "doesn't allow clearing a key from the cache that's not a hash (definitely not an experiment)" do
shared_state.with { |r| r.set(key_name, 'value') }
expect { subject.clear(key: key_name) }.to raise_error(
ArgumentError,
'invalid call to clear a non-hash cache key'
)
end
context "when the :caching_experiments feature is disabled" do
before do
stub_feature_flags(caching_experiments: false)
end
it "doesn't write to the cache" do
subject.write(key_field, 'value')
expect(subject.read(key_field)).to be_nil
end
end
end
......@@ -31,10 +31,6 @@ RSpec.describe ApplicationExperiment do
end
describe "tracking events", :snowplow do
before do
allow(Gitlab::Tracking).to receive(:event)
end
it "doesn't track if excluded" do
subject.exclude { true }
......@@ -82,4 +78,50 @@ RSpec.describe ApplicationExperiment do
end
end
end
context "when caching" do
let(:cache) { ApplicationExperiment::Cache.new }
before do
cache.clear(key: subject.name)
subject.use { } # setup the control
subject.try { } # setup the candidate
allow(Gitlab::Experiment::Configuration).to receive(:cache).and_return(cache)
end
it "caches the variant determined by the variant resolver" do
expect(subject.variant.name).to eq('candidate') # we should be in the experiment
subject.run
expect(cache.read(subject.cache_key)).to eq('candidate')
end
it "doesn't cache a variant if we don't explicitly provide one" do
# by not caching "empty" variants, we effectively create a mostly
# optimal combination of caching and rollout flexibility. If we cached
# every control variant assigned, we'd inflate the cache size and
# wouldn't be able to roll out to subjects that we'd already assigned to
# the control.
stub_feature_flags(stub: false) # simulate being not rolled out
expect(subject.variant.name).to eq('control') # if we ask, it should be control
subject.run
expect(cache.read(subject.cache_key)).to be_nil
end
it "caches a control variant if we assign it specifically" do
# by specifically assigning the control variant here, we're guaranteeing
# that this context will always get the control variant unless we delete
# the field from the cache (or clear the entire experiment cache) -- or
# write code that would specify a different variant.
subject.run(:control)
expect(cache.read(subject.cache_key)).to eq('control')
end
end
end
......@@ -28,7 +28,7 @@ RSpec.describe Quality::TestLevel do
context 'when level is unit' do
it 'returns a pattern' do
expect(subject.pattern(:unit))
.to eq("spec/{bin,channels,config,db,dependencies,elastic,elastic_integration,factories,finders,frontend,graphql,haml_lint,helpers,initializers,javascripts,lib,models,policies,presenters,rack_servers,replicators,routing,rubocop,serializers,services,sidekiq,support_specs,tasks,uploaders,validators,views,workers,tooling}{,/**/}*_spec.rb")
.to eq("spec/{bin,channels,config,db,dependencies,elastic,elastic_integration,experiments,factories,finders,frontend,graphql,haml_lint,helpers,initializers,javascripts,lib,models,policies,presenters,rack_servers,replicators,routing,rubocop,serializers,services,sidekiq,support_specs,tasks,uploaders,validators,views,workers,tooling}{,/**/}*_spec.rb")
end
end
......@@ -103,7 +103,7 @@ RSpec.describe Quality::TestLevel do
context 'when level is unit' do
it 'returns a regexp' do
expect(subject.regexp(:unit))
.to eq(%r{spec/(bin|channels|config|db|dependencies|elastic|elastic_integration|factories|finders|frontend|graphql|haml_lint|helpers|initializers|javascripts|lib|models|policies|presenters|rack_servers|replicators|routing|rubocop|serializers|services|sidekiq|support_specs|tasks|uploaders|validators|views|workers|tooling)})
.to eq(%r{spec/(bin|channels|config|db|dependencies|elastic|elastic_integration|experiments|factories|finders|frontend|graphql|haml_lint|helpers|initializers|javascripts|lib|models|policies|presenters|rack_servers|replicators|routing|rubocop|serializers|services|sidekiq|support_specs|tasks|uploaders|validators|views|workers|tooling)})
end
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