Commit b3fb39a5 authored by Furkan Ayhan's avatar Furkan Ayhan

Fix CI mirror table sync race condition

After consuming sync events, we need to remove those.
Before this commit, we used to use first id and last id range of events.
However, this caused a race condition. If an event is on-the-fly
transaction, and before and after events are tired to be consumed,
that event gets lost.
parent 951313ad
...@@ -28,18 +28,16 @@ module Ci ...@@ -28,18 +28,16 @@ module Ci
return if events.empty? return if events.empty?
first = events.first processed_events = []
last_processed = nil
begin begin
events.each do |event| events.each do |event|
@sync_class.sync!(event) @sync_class.sync!(event)
last_processed = event processed_events << event
end end
ensure ensure
# remove events till the one that was last succesfully processed @sync_event_class.id_in(processed_events).delete_all
@sync_event_class.id_in(first.id..last_processed.id).delete_all if last_processed
end end
end end
......
...@@ -71,6 +71,24 @@ RSpec.describe Ci::ProcessSyncEventsService do ...@@ -71,6 +71,24 @@ RSpec.describe Ci::ProcessSyncEventsService do
expect { execute }.not_to change(Projects::SyncEvent, :count) expect { execute }.not_to change(Projects::SyncEvent, :count)
end end
end end
it 'does not delete non-executed events' do
new_project = create(:project)
sync_event_class.delete_all
project1.update!(group: parent_group_2)
new_project.update!(group: parent_group_1)
project2.update!(group: parent_group_1)
new_project_sync_event = new_project.sync_events.last
allow(sync_event_class).to receive(:preload_synced_relation).and_return(
sync_event_class.where.not(id: new_project_sync_event)
)
expect { execute }.to change(Projects::SyncEvent, :count).from(3).to(1)
expect(new_project_sync_event.reload).to be_persisted
end
end end
context 'for Namespaces::SyncEvent' do context 'for Namespaces::SyncEvent' 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