diff options
author | Andreas Brandl <abrandl@gitlab.com> | 2018-10-28 17:07:05 +0100 |
---|---|---|
committer | Andreas Brandl <abrandl@gitlab.com> | 2018-12-03 21:26:48 +0100 |
commit | c5fb4682556d955ecbcbd589ffb18a95c7fbe0a1 (patch) | |
tree | 673cdc57d11c3aa80ffbb7acfb07612e603f0cd9 | |
parent | b6a530c9b14e0f7a1f7ea7717d68a105f4d43409 (diff) | |
download | gitlab-ce-c5fb4682556d955ecbcbd589ffb18a95c7fbe0a1.tar.gz |
Flexible approximate counts with fallback strategies.
-rw-r--r-- | lib/gitlab/database/count.rb | 32 | ||||
-rw-r--r-- | spec/lib/gitlab/database/count_spec.rb | 21 |
2 files changed, 32 insertions, 21 deletions
diff --git a/lib/gitlab/database/count.rb b/lib/gitlab/database/count.rb index 55f0cbd18be..beb5a8d3556 100644 --- a/lib/gitlab/database/count.rb +++ b/lib/gitlab/database/count.rb @@ -25,21 +25,17 @@ module Gitlab # # @param [Array] # @return [Hash] of Model -> count mapping - def self.approximate_counts(models) - counts_by_model = {} - - if Gitlab::Database.postgresql? - #counts_by_model = ReltuplesCountStrategy.new(models).count - counts_by_model = reltuples_from_recently_updated(models) - end - - missing_models = models - counts_by_model.keys - - ExactCountStrategy.new(missing_models).count.each do |model, count| - counts_by_model[model] = count + def self.approximate_counts(models, strategies = [ReltuplesCountStrategy, ExactCountStrategy]) + strategies.each_with_object({}) do |strategy, counts_by_model| + if strategy.enabled? + models_with_missing_counts = models - counts_by_model.keys + counts = strategy.new(models_with_missing_counts).count + + counts.each do |model, count| + counts_by_model[model] = count + end + end end - - counts_by_model end # Returns a hash of the table names that have recently updated tuples. @@ -61,6 +57,10 @@ module Gitlab data[model] = model.count end end + + def self.enabled? + true + end end class ReltuplesCountStrategy @@ -92,6 +92,10 @@ module Gitlab {} end + def self.enabled? + Gitlab::Database.postgresql? + end + private def table_names diff --git a/spec/lib/gitlab/database/count_spec.rb b/spec/lib/gitlab/database/count_spec.rb index 9b4b61d7c94..fee6cbbfcba 100644 --- a/spec/lib/gitlab/database/count_spec.rb +++ b/spec/lib/gitlab/database/count_spec.rb @@ -23,10 +23,14 @@ describe Gitlab::Database::Count do end context 'with PostgreSQL', :postgresql do + let(:reltuples_strategy) { double('reltuples_strategy', count: {}) } + + before do + allow(Gitlab::Database::Count::ReltuplesCountStrategy).to receive(:new).with(models).and_return(reltuples_strategy) + end + describe 'when reltuples have not been updated' do it 'counts all models the normal way' do - expect(described_class).to receive(:reltuples_from_recently_updated).with(models).and_return({}) - expect(Project).to receive(:count).and_call_original expect(Identity).to receive(:count).and_call_original expect(described_class.approximate_counts(models)).to eq({ Project => 3, Identity => 1 }) @@ -45,7 +49,7 @@ describe Gitlab::Database::Count do describe 'when some reltuples have been updated' do it 'counts projects in the fast way' do - expect(described_class).to receive(:reltuples_from_recently_updated).with(models).and_return({ Project => 3 }) + expect(reltuples_strategy).to receive(:count).and_return({ Project => 3 }) expect(Project).not_to receive(:count).and_call_original expect(Identity).to receive(:count).and_call_original @@ -53,13 +57,16 @@ describe Gitlab::Database::Count do end end + # TODO: This covers two parts: reltuple strategy itself and the fallback + # TODO: Add spec that covers strategy details for reltuple strategy describe 'when all reltuples have been updated' do - before do - ActiveRecord::Base.connection.execute('ANALYZE projects') - ActiveRecord::Base.connection.execute('ANALYZE identities') - end + #before do + #ActiveRecord::Base.connection.execute('ANALYZE projects') + #ActiveRecord::Base.connection.execute('ANALYZE identities') + #end it 'counts models with the standard way' do + allow(reltuples_strategy).to receive(:count).and_return({ Project => 3, Identity => 1 }) expect(Project).not_to receive(:count) expect(Identity).not_to receive(:count) |