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 ...@@ -65,11 +65,13 @@ module Gitlab
next unless can_replay?(event_log) next unless can_replay?(event_log)
if event_log.repository_updated_event 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 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 elsif event_log.repositories_changed_event
handle_repositories_changed(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 end
end end
...@@ -102,7 +104,8 @@ module Gitlab ...@@ -102,7 +104,8 @@ module Gitlab
Gitlab::Geo.current_node&.projects_include?(event_log.project_id) Gitlab::Geo.current_node&.projects_include?(event_log.project_id)
end 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) registry = ::Geo::ProjectRegistry.find_or_initialize_by(project_id: updated_event.project_id)
case updated_event.source case updated_event.source
...@@ -112,10 +115,9 @@ module Gitlab ...@@ -112,10 +115,9 @@ module Gitlab
registry.resync_wiki = true registry.resync_wiki = true
end end
Gitlab::Geo::Logger.info( log_event_info(
class: self.class.name, event.created_at,
message: "Repository update", message: "Repository update",
cursor_delay_s: (Time.now - updated_event.created_at).to_f.round(3),
project_id: updated_event.project_id, project_id: updated_event.project_id,
source: updated_event.source, source: updated_event.source,
resync_repository: registry.resync_repository, resync_repository: registry.resync_repository,
...@@ -124,7 +126,8 @@ module Gitlab ...@@ -124,7 +126,8 @@ module Gitlab
registry.save! registry.save!
end 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 # Once we remove system hooks we can refactor
# GeoRepositoryDestroyWorker to avoid doing this # GeoRepositoryDestroyWorker to avoid doing this
full_path = File.join(deleted_event.repository_storage_path, full_path = File.join(deleted_event.repository_storage_path,
...@@ -133,12 +136,11 @@ module Gitlab ...@@ -133,12 +136,11 @@ module Gitlab
deleted_event.project_id, deleted_event.project_id,
deleted_event.deleted_project_name, deleted_event.deleted_project_name,
full_path) full_path)
Gitlab::Geo::Logger.info( log_event_info(event.created_at,
class: self.class.name, message: "Deleted project",
message: "Deleted project", project_id: deleted_event.project_id,
project_id: deleted_event.project_id, full_path: full_path,
full_path: full_path, job_id: job_id)
job_id: job_id)
# No need to create a project entry if it doesn't exist # No need to create a project entry if it doesn't exist
::Geo::ProjectRegistry.where(project_id: deleted_event.project_id).delete_all ::Geo::ProjectRegistry.where(project_id: deleted_event.project_id).delete_all
end end
...@@ -155,6 +157,33 @@ module Gitlab ...@@ -155,6 +157,33 @@ module Gitlab
end end
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 = {}) def log_info(message, params = {})
Gitlab::Geo::Logger.info({ class: self.class.name, message: message }.merge(params)) Gitlab::Geo::Logger.info({ class: self.class.name, message: message }.merge(params))
end end
......
...@@ -7,6 +7,10 @@ FactoryGirl.define do ...@@ -7,6 +7,10 @@ FactoryGirl.define do
trait :deleted_event do trait :deleted_event do
repository_deleted_event factory: :geo_repository_deleted_event repository_deleted_event factory: :geo_repository_deleted_event
end end
trait :renamed_event do
repository_renamed_event factory: :geo_repository_rename_event
end
end end
factory :geo_repository_updated_event, class: Geo::RepositoryUpdatedEvent do factory :geo_repository_updated_event, class: Geo::RepositoryUpdatedEvent do
...@@ -41,4 +45,17 @@ FactoryGirl.define do ...@@ -41,4 +45,17 @@ FactoryGirl.define do
factory :geo_repositories_changed_event, class: Geo::RepositoriesChangedEvent do factory :geo_repositories_changed_event, class: Geo::RepositoriesChangedEvent do
geo_node geo_node
end 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 end
...@@ -150,5 +150,31 @@ describe Gitlab::Geo::LogCursor::Daemon do ...@@ -150,5 +150,31 @@ describe Gitlab::Geo::LogCursor::Daemon do
end end
end 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
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