Commit d63e1672 authored by George Koltsov's avatar George Koltsov

Update export workers to read from replica if available

Changelog: changed

 - Group/Project export workers do a lot of reads from
   the database in order to perform the export with
   a handful of writes
 - Because of this reason, read from replicas to reduce
   the load of primary database
parent 1c23292f
---
name: load_balancing_for_export_workers
introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/68153
rollout_issue_url: https://gitlab.com/gitlab-org/gitlab/-/issues/338615
milestone: '14.2'
type: development
group: group::import
default_enabled: false
...@@ -31,12 +31,14 @@ module Gitlab ...@@ -31,12 +31,14 @@ module Gitlab
end end
def execute def execute
read_from_replica_if_available do
serialize_root serialize_root
includes.each do |relation_definition| includes.each do |relation_definition|
serialize_relation(relation_definition) serialize_relation(relation_definition)
end end
end end
end
def serialize_relation(definition) def serialize_relation(definition)
raise ArgumentError, 'definition needs to be Hash' unless definition.is_a?(Hash) raise ArgumentError, 'definition needs to be Hash' unless definition.is_a?(Hash)
...@@ -166,6 +168,13 @@ module Gitlab ...@@ -166,6 +168,13 @@ module Gitlab
) )
]) ])
end end
def read_from_replica_if_available(&block)
return yield unless ::Feature.enabled?(:load_balancing_for_export_workers, type: :development, default_enabled: :yaml)
return yield unless ::Gitlab::Database::LoadBalancing.enable?
::Gitlab::Database::LoadBalancing::Session.current.use_replicas_for_read_queries(&block)
end
end end
end end
end end
......
...@@ -156,6 +156,41 @@ RSpec.describe Gitlab::ImportExport::Json::StreamingSerializer do ...@@ -156,6 +156,41 @@ RSpec.describe Gitlab::ImportExport::Json::StreamingSerializer do
subject.execute subject.execute
end end
end end
describe 'load balancing' do
context 'when feature flag load_balancing_for_export_workers is enabled' do
before do
stub_feature_flags(load_balancing_for_export_workers: true)
end
context 'when enabled', :db_load_balancing do
it 'reads from replica' do
expect(Gitlab::Database::LoadBalancing::Session.current).to receive(:use_replicas_for_read_queries).and_call_original
subject.execute
end
end
context 'when disabled' do
it 'reads from primary' do
allow(Gitlab::Database::LoadBalancing).to receive(:enable?).and_return(false)
expect(Gitlab::Database::LoadBalancing::Session.current).not_to receive(:use_replicas_for_read_queries)
subject.execute
end
end
end
context 'when feature flag load_balancing_for_export_workers is disabled' do
it 'reads from primary' do
stub_feature_flags(load_balancing_for_export_workers: false)
expect(Gitlab::Database::LoadBalancing::Session.current).not_to receive(:use_replicas_for_read_queries)
subject.execute
end
end
end
end end
describe '.batch_size' do describe '.batch_size' 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