Commit 76920101 authored by Valery Sizov's avatar Valery Sizov

Geo: Invalidate cache after refreshing foreign tables

Invalidate cache after rake geo:db:refresh_foreign_tables
parent d021195a
---
title: 'Geo: Invalidate cache after refreshing foreign tables'
merge_request: 17885
author:
type: fixed
......@@ -98,9 +98,13 @@ module Gitlab
end
def self.expire_cache!
CACHE_KEYS.each do |raw_key|
l1_cache.expire(raw_key)
l2_cache.expire(raw_key)
expire_cache_keys!(CACHE_KEYS)
end
def self.expire_cache_keys!(keys)
keys.each do |key|
l1_cache.expire(key)
l2_cache.expire(key)
end
true
......
......@@ -7,6 +7,12 @@ module Gitlab
FOREIGN_SERVER = 'gitlab_secondary'
FOREIGN_SCHEMA = 'gitlab_secondary'
CACHE_KEYS = %i(
geo_FOREIGN_SCHEMA_exist
geo_foreign_schema_tables_match
geo_fdw_count_tables
).freeze
class << self
# Return if FDW is enabled for this instance
#
......@@ -54,6 +60,10 @@ module Gitlab
ActiveRecord::Schema.tables.reject { |table| table.start_with?('pg_') }.count
end
def expire_cache!
Gitlab::Geo.expire_cache_keys!(CACHE_KEYS)
end
private
def fdw_capable?
......
......@@ -49,6 +49,8 @@ module Gitlab
ActiveRecord::Base.connection.execute(sql)
end
end
Gitlab::Geo::Fdw.expire_cache!
end
def foreign_server_configured?
......
......@@ -152,6 +152,14 @@ describe Gitlab::Geo::Fdw, :geo do
end
end
describe '.expire_cache!' do
it 'calls Gitlab::Geo.expire_cache_keys!' do
expect(Gitlab::Geo).to receive(:expire_cache_keys!).with(Gitlab::Geo::Fdw::CACHE_KEYS)
described_class.expire_cache!
end
end
def with_foreign_connection
Geo::TrackingBase.connection
end
......
......@@ -160,14 +160,35 @@ describe Gitlab::Geo, :geo, :request_store do
describe '.expire_cache!' do
it 'clears the Geo cache keys', :request_store do
described_class::CACHE_KEYS.each do |raw_key|
expanded_key = "geo:#{raw_key}:#{Gitlab::VERSION}:#{Rails.version}"
described_class::CACHE_KEYS.each do |key|
content = "#{key}-content"
expect(Rails.cache).to receive(:delete).with(expanded_key).and_call_original
expect(Gitlab::ThreadMemoryCache.cache_backend).to receive(:delete).with(expanded_key).and_call_original
described_class.cache_value(key) { content }
expect(described_class.cache_value(key)).to eq(content)
end
described_class.expire_cache!
described_class::CACHE_KEYS.each do |key|
expect(described_class.cache_value(key) { nil }).to be_nil
end
end
end
describe '.expire_cache_keys!' do
it 'clears specified keys', :request_store do
cache_data = { one: 1, two: 2 }
cache_data.each do |key, value|
described_class.cache_value(key) { value }
expect(described_class.cache_value(key)).to eq(value)
end
described_class.expire_cache_keys!(cache_data.keys)
cache_data.keys.each do |key|
expect(described_class.cache_value(key) { nil }).to be_nil
end
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