Commit e8eb27d6 authored by Dmitry Gruzd's avatar Dmitry Gruzd

Merge branch '345279-hll-distinct-counter-uses-relation-connection' into 'master'

HLL BatchDistinctCounter uses relation connection

See merge request gitlab-org/gitlab!74112
parents 2153b254 8f348069
...@@ -2402,7 +2402,6 @@ Database/MultipleDatabases: ...@@ -2402,7 +2402,6 @@ Database/MultipleDatabases:
- 'lib/gitlab/database/migrations/observers/query_log.rb' - 'lib/gitlab/database/migrations/observers/query_log.rb'
- 'lib/gitlab/database/multi_threaded_migration.rb' - 'lib/gitlab/database/multi_threaded_migration.rb'
- 'lib/gitlab/database/partitioning_migration_helpers/backfill_partitioned_table.rb' - 'lib/gitlab/database/partitioning_migration_helpers/backfill_partitioned_table.rb'
- 'lib/gitlab/database/postgres_hll/batch_distinct_counter.rb'
- 'lib/gitlab/database/postgresql_adapter/dump_schema_versions_mixin.rb' - 'lib/gitlab/database/postgresql_adapter/dump_schema_versions_mixin.rb'
- 'lib/gitlab/database/postgresql_database_tasks/load_schema_versions_mixin.rb' - 'lib/gitlab/database/postgresql_database_tasks/load_schema_versions_mixin.rb'
- 'lib/gitlab/database.rb' - 'lib/gitlab/database.rb'
......
...@@ -13,7 +13,9 @@ RSpec.describe Gitlab::UsageDataMetrics do ...@@ -13,7 +13,9 @@ RSpec.describe Gitlab::UsageDataMetrics do
end end
before do before do
allow(ActiveRecord::Base.connection).to receive(:transaction_open?).and_return(false) # rubocop:disable Database/MultipleDatabases allow_next_instance_of(Gitlab::Database::BatchCounter) do |batch_counter|
allow(batch_counter).to receive(:transaction_open?).and_return(false)
end
end end
context 'with instrumentation_class' do context 'with instrumentation_class' do
......
...@@ -31,7 +31,7 @@ module Gitlab ...@@ -31,7 +31,7 @@ module Gitlab
end end
def count(batch_size: nil, mode: :itself, start: nil, finish: nil) def count(batch_size: nil, mode: :itself, start: nil, finish: nil)
raise 'BatchCount can not be run inside a transaction' if @relation.connection.transaction_open? raise 'BatchCount can not be run inside a transaction' if transaction_open?
check_mode!(mode) check_mode!(mode)
...@@ -87,6 +87,10 @@ module Gitlab ...@@ -87,6 +87,10 @@ module Gitlab
results results
end end
def transaction_open?
@relation.connection.transaction_open?
end
def merge_results(results, object) def merge_results(results, object)
return object unless results return object unless results
......
...@@ -57,7 +57,7 @@ module Gitlab ...@@ -57,7 +57,7 @@ module Gitlab
# @param finish final pkey range # @param finish final pkey range
# @return [Gitlab::Database::PostgresHll::Buckets] HyperLogLog data structure instance that can estimate number of unique elements # @return [Gitlab::Database::PostgresHll::Buckets] HyperLogLog data structure instance that can estimate number of unique elements
def execute(batch_size: nil, start: nil, finish: nil) def execute(batch_size: nil, start: nil, finish: nil)
raise 'BatchCount can not be run inside a transaction' if ActiveRecord::Base.connection.transaction_open? # rubocop: disable Database/MultipleDatabases raise 'BatchCount can not be run inside a transaction' if transaction_open?
batch_size ||= DEFAULT_BATCH_SIZE batch_size ||= DEFAULT_BATCH_SIZE
start = actual_start(start) start = actual_start(start)
...@@ -79,6 +79,10 @@ module Gitlab ...@@ -79,6 +79,10 @@ module Gitlab
private private
def transaction_open?
@relation.connection.transaction_open?
end
def unwanted_configuration?(start, finish, batch_size) def unwanted_configuration?(start, finish, batch_size)
batch_size <= MIN_REQUIRED_BATCH_SIZE || batch_size <= MIN_REQUIRED_BATCH_SIZE ||
(finish - start) >= MAX_DATA_VOLUME || (finish - start) >= MAX_DATA_VOLUME ||
......
...@@ -19,7 +19,7 @@ RSpec.describe Gitlab::Database::BatchCount do ...@@ -19,7 +19,7 @@ RSpec.describe Gitlab::Database::BatchCount do
end end
before do before do
allow(ActiveRecord::Base.connection).to receive(:transaction_open?).and_return(in_transaction) allow(model.connection).to receive(:transaction_open?).and_return(in_transaction)
end end
def calculate_batch_size(batch_size) def calculate_batch_size(batch_size)
......
...@@ -21,7 +21,7 @@ RSpec.describe Gitlab::Database::PostgresHll::BatchDistinctCounter do ...@@ -21,7 +21,7 @@ RSpec.describe Gitlab::Database::PostgresHll::BatchDistinctCounter do
end end
before do before do
allow(ActiveRecord::Base.connection).to receive(:transaction_open?).and_return(in_transaction) allow(model.connection).to receive(:transaction_open?).and_return(in_transaction)
end end
context 'unit test for different counting parameters' do context 'unit test for different counting parameters' do
......
...@@ -13,7 +13,9 @@ RSpec.describe Gitlab::UsageDataMetrics do ...@@ -13,7 +13,9 @@ RSpec.describe Gitlab::UsageDataMetrics do
end end
before do before do
allow(ActiveRecord::Base.connection).to receive(:transaction_open?).and_return(false) allow_next_instance_of(Gitlab::Database::BatchCounter) do |batch_counter|
allow(batch_counter).to receive(:transaction_open?).and_return(false)
end
end end
context 'with instrumentation_class' do context 'with instrumentation_class' do
......
...@@ -70,7 +70,7 @@ RSpec.describe Gitlab::Utils::UsageData do ...@@ -70,7 +70,7 @@ RSpec.describe Gitlab::Utils::UsageData do
let(:relation) { double(:relation, connection: double(:connection)) } let(:relation) { double(:relation, connection: double(:connection)) }
before do before do
allow(ActiveRecord::Base.connection).to receive(:transaction_open?).and_return(false) # rubocop: disable Database/MultipleDatabases allow(relation.connection).to receive(:transaction_open?).and_return(false)
end end
it 'delegates counting to counter class instance' do it 'delegates counting to counter class instance' do
...@@ -122,7 +122,7 @@ RSpec.describe Gitlab::Utils::UsageData do ...@@ -122,7 +122,7 @@ RSpec.describe Gitlab::Utils::UsageData do
let(:ci_builds_estimated_cardinality) { 2.0809220082170614 } let(:ci_builds_estimated_cardinality) { 2.0809220082170614 }
before do before do
allow(ActiveRecord::Base.connection).to receive(:transaction_open?).and_return(false) # rubocop: disable Database/MultipleDatabases allow(model.connection).to receive(:transaction_open?).and_return(false)
end end
context 'different counting parameters' do context 'different counting parameters' do
......
...@@ -6,7 +6,9 @@ RSpec.shared_examples 'a correct instrumented metric value' do |params| ...@@ -6,7 +6,9 @@ RSpec.shared_examples 'a correct instrumented metric value' do |params|
let(:metric) { described_class.new(time_frame: time_frame, options: options) } let(:metric) { described_class.new(time_frame: time_frame, options: options) }
before do before do
allow(ActiveRecord::Base.connection).to receive(:transaction_open?).and_return(false) if described_class.respond_to?(:relation) && described_class.relation.respond_to?(:connection)
allow(described_class.relation.connection).to receive(:transaction_open?).and_return(false)
end
end end
it 'has correct value' do it 'has correct value' 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