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 ...@@ -98,9 +98,13 @@ module Gitlab
end end
def self.expire_cache! def self.expire_cache!
CACHE_KEYS.each do |raw_key| expire_cache_keys!(CACHE_KEYS)
l1_cache.expire(raw_key) end
l2_cache.expire(raw_key)
def self.expire_cache_keys!(keys)
keys.each do |key|
l1_cache.expire(key)
l2_cache.expire(key)
end end
true true
......
...@@ -7,6 +7,12 @@ module Gitlab ...@@ -7,6 +7,12 @@ module Gitlab
FOREIGN_SERVER = 'gitlab_secondary' FOREIGN_SERVER = 'gitlab_secondary'
FOREIGN_SCHEMA = '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 class << self
# Return if FDW is enabled for this instance # Return if FDW is enabled for this instance
# #
...@@ -54,6 +60,10 @@ module Gitlab ...@@ -54,6 +60,10 @@ module Gitlab
ActiveRecord::Schema.tables.reject { |table| table.start_with?('pg_') }.count ActiveRecord::Schema.tables.reject { |table| table.start_with?('pg_') }.count
end end
def expire_cache!
Gitlab::Geo.expire_cache_keys!(CACHE_KEYS)
end
private private
def fdw_capable? def fdw_capable?
......
...@@ -49,6 +49,8 @@ module Gitlab ...@@ -49,6 +49,8 @@ module Gitlab
ActiveRecord::Base.connection.execute(sql) ActiveRecord::Base.connection.execute(sql)
end end
end end
Gitlab::Geo::Fdw.expire_cache!
end end
def foreign_server_configured? def foreign_server_configured?
......
...@@ -152,6 +152,14 @@ describe Gitlab::Geo::Fdw, :geo do ...@@ -152,6 +152,14 @@ describe Gitlab::Geo::Fdw, :geo do
end end
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 def with_foreign_connection
Geo::TrackingBase.connection Geo::TrackingBase.connection
end end
......
...@@ -160,14 +160,35 @@ describe Gitlab::Geo, :geo, :request_store do ...@@ -160,14 +160,35 @@ describe Gitlab::Geo, :geo, :request_store do
describe '.expire_cache!' do describe '.expire_cache!' do
it 'clears the Geo cache keys', :request_store do it 'clears the Geo cache keys', :request_store do
described_class::CACHE_KEYS.each do |raw_key| described_class::CACHE_KEYS.each do |key|
expanded_key = "geo:#{raw_key}:#{Gitlab::VERSION}:#{Rails.version}" content = "#{key}-content"
expect(Rails.cache).to receive(:delete).with(expanded_key).and_call_original described_class.cache_value(key) { content }
expect(Gitlab::ThreadMemoryCache.cache_backend).to receive(:delete).with(expanded_key).and_call_original expect(described_class.cache_value(key)).to eq(content)
end end
described_class.expire_cache! 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
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