Add Geo::CacheInvalidationEvent model

These changes introduce a new Geo event that can be triggered
to invalidate the cache for a specific key on Geo secondaries.

This event is necessary because enabling/disabling features does
not propagate correctly to Geo secondaries. It could be used for
other caches in the future (or existing caches that we currently
set to very short expiration periods on the secondary).
parent 8834fec1
......@@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20180930171532) do
ActiveRecord::Schema.define(version: 20181001172651) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
......@@ -1076,6 +1076,10 @@ ActiveRecord::Schema.define(version: 20180930171532) do
add_index "gcp_clusters", ["project_id"], name: "index_gcp_clusters_on_project_id", unique: true, using: :btree
create_table "geo_cache_invalidation_events", id: :bigserial, force: :cascade do |t|
t.string "key", null: false
end
create_table "geo_event_log", id: :bigserial, force: :cascade do |t|
t.datetime "created_at", null: false
t.integer "repository_updated_event_id", limit: 8
......@@ -1089,8 +1093,10 @@ ActiveRecord::Schema.define(version: 20180930171532) do
t.integer "job_artifact_deleted_event_id", limit: 8
t.integer "upload_deleted_event_id", limit: 8
t.integer "reset_checksum_event_id", limit: 8
t.integer "cache_invalidation_event_id", limit: 8
end
add_index "geo_event_log", ["cache_invalidation_event_id"], name: "index_geo_event_log_on_cache_invalidation_event_id", using: :btree
add_index "geo_event_log", ["repositories_changed_event_id"], name: "index_geo_event_log_on_repositories_changed_event_id", using: :btree
add_index "geo_event_log", ["repository_created_event_id"], name: "index_geo_event_log_on_repository_created_event_id", using: :btree
add_index "geo_event_log", ["repository_deleted_event_id"], name: "index_geo_event_log_on_repository_deleted_event_id", using: :btree
......@@ -3171,6 +3177,7 @@ ActiveRecord::Schema.define(version: 20180930171532) do
add_foreign_key "gcp_clusters", "projects", on_delete: :cascade
add_foreign_key "gcp_clusters", "services", on_delete: :nullify
add_foreign_key "gcp_clusters", "users", on_delete: :nullify
add_foreign_key "geo_event_log", "geo_cache_invalidation_events", column: "cache_invalidation_event_id", name: "fk_42c3b54bed", on_delete: :cascade
add_foreign_key "geo_event_log", "geo_hashed_storage_migrated_events", column: "hashed_storage_migrated_event_id", name: "fk_27548c6db3", on_delete: :cascade
add_foreign_key "geo_event_log", "geo_job_artifact_deleted_events", column: "job_artifact_deleted_event_id", name: "fk_176d3fbb5d", on_delete: :cascade
add_foreign_key "geo_event_log", "geo_lfs_object_deleted_events", column: "lfs_object_deleted_event_id", name: "fk_d5af95fcd9", on_delete: :cascade
......
# frozen_string_literal: true
module Geo
class CacheInvalidationEvent < ActiveRecord::Base
include Geo::Model
include Geo::Eventable
validates :key, presence: true
end
end
......@@ -3,7 +3,8 @@ module Geo
include Geo::Model
include ::EachBatch
EVENT_CLASSES = %w[Geo::RepositoryCreatedEvent
EVENT_CLASSES = %w[Geo::CacheInvalidationEvent
Geo::RepositoryCreatedEvent
Geo::RepositoryUpdatedEvent
Geo::RepositoryDeletedEvent
Geo::RepositoryRenamedEvent
......@@ -15,6 +16,10 @@ module Geo
Geo::JobArtifactDeletedEvent
Geo::UploadDeletedEvent].freeze
belongs_to :cache_invalidation_event,
class_name: 'Geo::CacheInvalidationEvent',
foreign_key: :cache_invalidation_event_id
belongs_to :repository_created_event,
class_name: 'Geo::RepositoryCreatedEvent',
foreign_key: :repository_created_event_id
......@@ -82,7 +87,8 @@ module Geo
lfs_object_deleted_event ||
job_artifact_deleted_event ||
upload_deleted_event ||
reset_checksum_event
reset_checksum_event ||
cache_invalidation_event
end
def project_id
......
# frozen_string_literal: true
class CreateGeoCacheInvalidationEvents < ActiveRecord::Migration
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
def change
create_table :geo_cache_invalidation_events, id: :bigserial do |t|
t.string :key, null: false
end
add_column :geo_event_log, :cache_invalidation_event_id, :integer, limit: 8
end
end
# frozen_string_literal: true
class AddGeoCacheInvalidationEventsForeignKey < ActiveRecord::Migration
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
disable_ddl_transaction!
def up
add_concurrent_foreign_key :geo_event_log, :geo_cache_invalidation_events, column: :cache_invalidation_event_id, on_delete: :cascade
add_concurrent_index :geo_event_log, :cache_invalidation_event_id
end
def down
remove_foreign_key :geo_event_log, column: :cache_invalidation_event_id
remove_concurrent_index :geo_event_log, :cache_invalidation_event_id
end
end
......@@ -39,6 +39,10 @@ FactoryBot.define do
trait :reset_checksum_event do
reset_checksum_event factory: :geo_reset_checksum_event
end
trait :cache_invalidation_event do
cache_invalidation_event factory: :geo_cache_invalidation_event
end
end
factory :geo_repository_created_event, class: Geo::RepositoryCreatedEvent do
......@@ -146,4 +150,8 @@ FactoryBot.define do
factory :geo_reset_checksum_event, class: Geo::ResetChecksumEvent do
project
end
factory :geo_cache_invalidation_event, class: Geo::CacheInvalidationEvent do
sequence(:key) { |n| "cache-key-#{n}" }
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Geo::CacheInvalidationEvent, type: :model do
describe 'validations' do
it { is_expected.to validate_presence_of(:key) }
end
end
......@@ -2,6 +2,7 @@ require 'spec_helper'
RSpec.describe Geo::EventLog, type: :model do
describe 'relationships' do
it { is_expected.to belong_to(:cache_invalidation_event).class_name('Geo::CacheInvalidationEvent').with_foreign_key('cache_invalidation_event_id') }
it { is_expected.to belong_to(:repositories_changed_event).class_name('Geo::RepositoriesChangedEvent').with_foreign_key('repositories_changed_event_id') }
it { is_expected.to belong_to(:repository_created_event).class_name('Geo::RepositoryCreatedEvent').with_foreign_key('repository_created_event_id') }
it { is_expected.to belong_to(:repository_deleted_event).class_name('Geo::RepositoryDeletedEvent').with_foreign_key('repository_deleted_event_id') }
......@@ -103,6 +104,13 @@ RSpec.describe Geo::EventLog, type: :model do
expect(subject.event).to eq reset_checksum_event
end
it 'returns cache_invalidation_event when set' do
cache_invalidation_event = build(:geo_cache_invalidation_event)
subject.cache_invalidation_event = cache_invalidation_event
expect(subject.event).to eq cache_invalidation_event
end
end
describe '#project_id' do
......
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