Commit c60ad8bc authored by Robert Speicher's avatar Robert Speicher

Merge branch 'sh-geo-cursor-rename-events' into 'master'

Support handling of rename events in Geo Log Cursor

See merge request !2615
parents 9dc56bd4 a2aef6a8
---
title: Support handling of rename events in Geo Log Cursor
merge_request:
author:
......@@ -65,11 +65,13 @@ module Gitlab
next unless can_replay?(event_log)
if event_log.repository_updated_event
handle_repository_update(event_log.repository_updated_event)
handle_repository_update(event_log)
elsif event_log.repository_deleted_event
handle_repository_delete(event_log.repository_deleted_event)
handle_repository_delete(event_log)
elsif event_log.repositories_changed_event
handle_repositories_changed(event_log.repositories_changed_event)
elsif event_log.repository_renamed_event
handle_repository_rename(event_log)
end
end
end
......@@ -102,7 +104,8 @@ module Gitlab
Gitlab::Geo.current_node&.projects_include?(event_log.project_id)
end
def handle_repository_update(updated_event)
def handle_repository_update(event)
updated_event = event.repository_updated_event
registry = ::Geo::ProjectRegistry.find_or_initialize_by(project_id: updated_event.project_id)
case updated_event.source
......@@ -112,10 +115,9 @@ module Gitlab
registry.resync_wiki = true
end
Gitlab::Geo::Logger.info(
class: self.class.name,
log_event_info(
event.created_at,
message: "Repository update",
cursor_delay_s: (Time.now - updated_event.created_at).to_f.round(3),
project_id: updated_event.project_id,
source: updated_event.source,
resync_repository: registry.resync_repository,
......@@ -124,7 +126,8 @@ module Gitlab
registry.save!
end
def handle_repository_delete(deleted_event)
def handle_repository_delete(event)
deleted_event = event.repository_deleted_event
# Once we remove system hooks we can refactor
# GeoRepositoryDestroyWorker to avoid doing this
full_path = File.join(deleted_event.repository_storage_path,
......@@ -133,12 +136,11 @@ module Gitlab
deleted_event.project_id,
deleted_event.deleted_project_name,
full_path)
Gitlab::Geo::Logger.info(
class: self.class.name,
message: "Deleted project",
project_id: deleted_event.project_id,
full_path: full_path,
job_id: job_id)
log_event_info(event.created_at,
message: "Deleted project",
project_id: deleted_event.project_id,
full_path: full_path,
job_id: job_id)
# No need to create a project entry if it doesn't exist
::Geo::ProjectRegistry.where(project_id: deleted_event.project_id).delete_all
end
......@@ -155,6 +157,33 @@ module Gitlab
end
end
def handle_repository_rename(event)
renamed_event = event.repository_renamed_event
return unless renamed_event.project_id
old_path = renamed_event.old_path_with_namespace
new_path = renamed_event.new_path_with_namespace
job_id = ::GeoRepositoryMoveWorker.perform_async(
renamed_event.project_id, "", old_path, new_path)
log_event_info(event.created_at,
message: "Renaming project",
project_id: renamed_event.project_id,
old_path: old_path,
new_path: new_path,
job_id: job_id)
end
def cursor_delay(created_at)
(Time.now - created_at).to_f.round(3)
end
def log_event_info(created_at, message, params = {})
params[:cursor_delay_s] = cursor_delay(created_at)
log_info(message, params)
end
def log_info(message, params = {})
Gitlab::Geo::Logger.info({ class: self.class.name, message: message }.merge(params))
end
......
......@@ -7,6 +7,10 @@ FactoryGirl.define do
trait :deleted_event do
repository_deleted_event factory: :geo_repository_deleted_event
end
trait :renamed_event do
repository_renamed_event factory: :geo_repository_rename_event
end
end
factory :geo_repository_updated_event, class: Geo::RepositoryUpdatedEvent do
......@@ -41,4 +45,17 @@ FactoryGirl.define do
factory :geo_repositories_changed_event, class: Geo::RepositoriesChangedEvent do
geo_node
end
factory :geo_repository_rename_event, class: Geo::RepositoryRenamedEvent do
project { create(:project, :repository) }
repository_storage_name { project.repository_storage }
repository_storage_path { project.repository_storage_path }
old_path_with_namespace { project.path_with_namespace }
new_path_with_namespace { project.path_with_namespace + '_new' }
old_wiki_path_with_namespace { project.wiki.path_with_namespace }
new_wiki_path_with_namespace { project.wiki.path_with_namespace + '_new' }
old_path { project.path }
new_path { project.path + '_new' }
end
end
......@@ -150,5 +150,31 @@ describe Gitlab::Geo::LogCursor::Daemon do
end
end
end
context 'when processing a repository renamed event' do
let(:event_log) { create(:geo_event_log, :renamed_event) }
let(:project) { event_log.repository_rename_event.project }
let!(:event_log_state) { create(:geo_event_log_state, event_id: event_log.id - 1) }
let(:repository_rename_event) { event_log.repository_renamed_event }
before do
allow(subject).to receive(:exit?).and_return(false, true)
end
it 'does not create a new project registry' do
expect { subject.run! }.not_to change(Geo::ProjectRegistry, :count)
end
it 'schedules a GeoRepositoryDestroyWorker' do
project_id = repository_rename_event.project_id
old_path_with_namespace = repository_rename_event.old_path_with_namespace
new_path_with_namespace = repository_rename_event.new_path_with_namespace
expect(::GeoRepositoryMoveWorker).to receive(:perform_async)
.with(project_id, '', old_path_with_namespace, new_path_with_namespace)
subject.run!
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