Commit 1a5fbd44 authored by Toon Claes's avatar Toon Claes

Change how flipping features creates Geo events

To make sure a Geo event is created when a Feature Flag is flipped on
instance level, refactor the code how where the #log_geo_event call is
hooked in the Flipper code.

Closes https://gitlab.com/gitlab-org/gitlab/-/issues/247535
parent 6bcbad7d
---
title: Create a Geo cache invalidation event when toggling feature flags through the
API
merge_request: 42070
author:
type: fixed
...@@ -2,70 +2,42 @@ ...@@ -2,70 +2,42 @@
module EE module EE
module Feature module Feature
module ClassMethods module ActiveSupportCacheStoreAdapter
extend ::Gitlab::Utils::Override extend ::Gitlab::Utils::Override
override :enable override :remove
def enable(key, thing = true) def remove(key)
super super.tap do |result|
log_geo_event(key) log_geo_event(key) if result
end end
override :disable
def disable(key, thing = false)
super
log_geo_event(key)
end
override :enable_group
def enable_group(key, group)
super
log_geo_event(key)
end
override :disable_group
def disable_group(key, group)
super
log_geo_event(key)
end end
override :enable_percentage_of_time override :clear
def enable_percentage_of_time(key, percentage) def clear(key)
super super.tap do |result|
log_geo_event(key) log_geo_event(key) if result
end
end end
override :disable_percentage_of_time override :enable
def disable_percentage_of_time(key) def enable(key, *_)
super super.tap do |result|
log_geo_event(key) log_geo_event(key) if result
end end
override :enable_percentage_of_actors
def enable_percentage_of_actors(key, percentage)
super
log_geo_event(key)
end end
override :disable_percentage_of_actors override :disable
def disable_percentage_of_actors(key) def disable(key, *_)
super super.tap do |result|
log_geo_event(key) log_geo_event(key) if result
end
end end
private private
def log_geo_event(key) def log_geo_event(key)
Geo::CacheInvalidationEventStore.new(cache_store.key_for(key)).create! Geo::CacheInvalidationEventStore.new(key_for(key)).create!
end end
def cache_store
Flipper::Adapters::ActiveSupportCacheStore
end
end
def self.prepended(base)
base.singleton_class.prepend ClassMethods
end end
end end
end end
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe Feature do RSpec.describe Feature, stub_feature_flags: false do
include EE::GeoHelpers include EE::GeoHelpers
describe '.enable' do describe '.enable' do
......
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe API::Features, stub_feature_flags: false do
include EE::GeoHelpers
let_it_be(:admin) { create(:admin) }
before do
Feature.reset
Flipper.unregister_groups
Flipper.register(:perf_team) do |actor|
actor.respond_to?(:admin) && actor.admin?
end
end
describe 'POST /feature' do
let(:feature_name) { 'my_feature' }
context 'when running on a Geo primary node' do
before do
stub_primary_node
allow(Gitlab::Geo).to receive(:secondary_nodes) { [double] }
end
it 'creates Geo cache invalidation event' do
expect do
post api("/features/#{feature_name}", admin), params: { value: 'true' }
end.to change(Geo::CacheInvalidationEvent, :count).by(1)
end
end
end
describe 'DELETE /feature/:name' do
let(:feature_name) { 'my_feature' }
context 'when running on a Geo primary node' do
before do
stub_primary_node
allow(Gitlab::Geo).to receive(:secondary_nodes) { [double] }
end
it 'creates Geo cache invalidation event' do
Feature.enable(feature_name)
expect do
delete api("/features/#{feature_name}", admin)
end.to change(Geo::CacheInvalidationEvent, :count).by(1)
end
end
end
end
...@@ -18,6 +18,10 @@ class Feature ...@@ -18,6 +18,10 @@ class Feature
superclass.table_name = 'feature_gates' superclass.table_name = 'feature_gates'
end end
class ActiveSupportCacheStoreAdapter < Flipper::Adapters::ActiveSupportCacheStore
# overrides methods in EE
end
InvalidFeatureFlagError = Class.new(Exception) # rubocop:disable Lint/InheritException InvalidFeatureFlagError = Class.new(Exception) # rubocop:disable Lint/InheritException
class << self class << self
...@@ -160,7 +164,7 @@ class Feature ...@@ -160,7 +164,7 @@ class Feature
# Redis L2 cache # Redis L2 cache
redis_cache_adapter = redis_cache_adapter =
Flipper::Adapters::ActiveSupportCacheStore.new( ActiveSupportCacheStoreAdapter.new(
active_record_adapter, active_record_adapter,
l2_cache_backend, l2_cache_backend,
expires_in: 1.hour) expires_in: 1.hour)
...@@ -237,4 +241,4 @@ class Feature ...@@ -237,4 +241,4 @@ class Feature
end end
end end
Feature.prepend_if_ee('EE::Feature') Feature::ActiveSupportCacheStoreAdapter.prepend_if_ee('EE::Feature::ActiveSupportCacheStoreAdapter')
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