diff --git a/ee/spec/features/boards/swimlanes/epics_swimlanes_filtering_spec.rb b/ee/spec/features/boards/swimlanes/epics_swimlanes_filtering_spec.rb index db9918b74af43f5932b67fdda70a6f992f627352..44bec87863fa62c758e15ab22b893232b4300d4b 100644 --- a/ee/spec/features/boards/swimlanes/epics_swimlanes_filtering_spec.rb +++ b/ee/spec/features/boards/swimlanes/epics_swimlanes_filtering_spec.rb @@ -44,7 +44,7 @@ context 'filtering' do before do - stub_const("Gitlab::QueryLimiting::Transaction::THRESHOLD", 200) + allow(Gitlab::QueryLimiting::Transaction).to receive(:threshold).and_return(200) stub_licensed_features(epics: true, swimlanes: true) sign_in(user) diff --git a/ee/spec/features/issues/user_bulk_edits_issues_spec.rb b/ee/spec/features/issues/user_bulk_edits_issues_spec.rb index 5487d050d2472df3d696668cd9f43bba96a39cb9..aaeae7de1dfaf65f9ba6902e4b96d0727b95bdda 100644 --- a/ee/spec/features/issues/user_bulk_edits_issues_spec.rb +++ b/ee/spec/features/issues/user_bulk_edits_issues_spec.rb @@ -169,7 +169,7 @@ context 'at group level' do before do # avoid raising QueryLimiting exception for bulk inserts - stub_const("::Gitlab::QueryLimiting::Transaction::THRESHOLD", 110) + allow(Gitlab::QueryLimiting::Transaction).to receive(:threshold).and_return(110) end it_behaves_like 'bulk edit option in sidebar', :group diff --git a/ee/spec/features/merge_trains/user_adds_merge_request_to_merge_train_spec.rb b/ee/spec/features/merge_trains/user_adds_merge_request_to_merge_train_spec.rb index 94db0691d4494ef405b691ed434780f132fc612c..6a6394190b195c9e871801a6bebb3875382b66d3 100644 --- a/ee/spec/features/merge_trains/user_adds_merge_request_to_merge_train_spec.rb +++ b/ee/spec/features/merge_trains/user_adds_merge_request_to_merge_train_spec.rb @@ -17,7 +17,7 @@ end before do - stub_const('Gitlab::QueryLimiting::Transaction::THRESHOLD', 200) + allow(Gitlab::QueryLimiting::Transaction).to receive(:threshold).and_return(200) stub_feature_flags(disable_merge_trains: false) stub_licensed_features(merge_pipelines: true, merge_trains: true) project.add_maintainer(user) diff --git a/ee/spec/features/registrations/combined_registration_spec.rb b/ee/spec/features/registrations/combined_registration_spec.rb index 305584b18653b0bdeb373895dbba0562f922702d..c093a5af206c427207ad0af71f07ea3c3b223d6f 100644 --- a/ee/spec/features/registrations/combined_registration_spec.rb +++ b/ee/spec/features/registrations/combined_registration_spec.rb @@ -9,7 +9,7 @@ before do # https://gitlab.com/gitlab-org/gitlab/-/issues/340302 - stub_const('Gitlab::QueryLimiting::Transaction::THRESHOLD', 136) + allow(Gitlab::QueryLimiting::Transaction).to receive(:threshold).and_return(136) stub_feature_flags(feature_flags) allow(Gitlab).to receive(:com?).and_return(true) sign_in(user) diff --git a/ee/spec/features/registrations/saas_user_registration_spec.rb b/ee/spec/features/registrations/saas_user_registration_spec.rb index a5886d210ff9ac13a8e9e60d67254b0fa45af499..0ab0dd4bd1dc9c05a9ddbc20a0a24dbaa19bf832 100644 --- a/ee/spec/features/registrations/saas_user_registration_spec.rb +++ b/ee/spec/features/registrations/saas_user_registration_spec.rb @@ -39,7 +39,7 @@ # The groups_and_projects_controller (on `click_on 'Create project'`) is over # the query limit threshold, so we have to adjust it. # https://gitlab.com/gitlab-org/gitlab/-/issues/338737 - stub_const('Gitlab::QueryLimiting::Transaction::THRESHOLD', 136) + allow(Gitlab::QueryLimiting::Transaction).to receive(:threshold).and_return(136) end def fill_in_sign_up_form(user) diff --git a/ee/spec/requests/api/epics_spec.rb b/ee/spec/requests/api/epics_spec.rb index 33a590817d4486b7e76e162e982ce6cfcbd589b6..e8fa2a74d5f0ebf8a755a7d61967d96117330977 100644 --- a/ee/spec/requests/api/epics_spec.rb +++ b/ee/spec/requests/api/epics_spec.rb @@ -949,7 +949,7 @@ include_context 'with labels' it 'updates the epic with labels param as array' do - stub_const("Gitlab::QueryLimiting::Transaction::THRESHOLD", 110) + allow(Gitlab::QueryLimiting::Transaction).to receive(:threshold).and_return(110) params[:labels] = ['label1', 'label2', 'foo, bar', '&,?'] diff --git a/ee/spec/requests/api/graphql/mutations/issues/promote_to_epic_spec.rb b/ee/spec/requests/api/graphql/mutations/issues/promote_to_epic_spec.rb index c1e7393275aa85e401e86dbed2091f065a14476b..c2ec5e9ec459f732bd071e467aab1f9dc809a278 100644 --- a/ee/spec/requests/api/graphql/mutations/issues/promote_to_epic_spec.rb +++ b/ee/spec/requests/api/graphql/mutations/issues/promote_to_epic_spec.rb @@ -52,7 +52,7 @@ def mutation_response # todo: investigate too many qeuries issue as part of Project Management Database and Query Performance # epic: https://gitlab.com/groups/gitlab-org/-/epics/5804 # specific issue: https://gitlab.com/gitlab-org/gitlab/-/issues/333845 - stub_const('Gitlab::QueryLimiting::Transaction::THRESHOLD', 110) + allow(Gitlab::QueryLimiting::Transaction).to receive(:threshold).and_return(110) end it 'returns an error if the user is not allowed to update the issue' do diff --git a/ee/spec/requests/api/graphql/mutations/security_policy/create_security_policy_project_spec.rb b/ee/spec/requests/api/graphql/mutations/security_policy/create_security_policy_project_spec.rb index d6a176c2efaea770cec25e5a1a4182c4feb39ade..2dd7e5e7b4d3cb0c242cc670eff56d0a24305d8c 100644 --- a/ee/spec/requests/api/graphql/mutations/security_policy/create_security_policy_project_spec.rb +++ b/ee/spec/requests/api/graphql/mutations/security_policy/create_security_policy_project_spec.rb @@ -38,7 +38,7 @@ def mutation_response # TODO: investigate too many qeuries issue as part of Project Management Database and Query Performance # Epic: https://gitlab.com/groups/gitlab-org/-/epics/5804 # Issue: https://gitlab.com/gitlab-org/gitlab/-/issues/348344 - stub_const('Gitlab::QueryLimiting::Transaction::THRESHOLD', 140) + allow(Gitlab::QueryLimiting::Transaction).to receive(:threshold).and_return(140) stub_licensed_features(security_orchestration_policies: true) end diff --git a/ee/spec/requests/projects/merge_requests_controller_spec.rb b/ee/spec/requests/projects/merge_requests_controller_spec.rb index 9578353e4ffd1684c48d26daaf34aa19c7df7c06..66599c3ae744111c8bb00af70258a41b623f3547 100644 --- a/ee/spec/requests/projects/merge_requests_controller_spec.rb +++ b/ee/spec/requests/projects/merge_requests_controller_spec.rb @@ -25,7 +25,7 @@ def get_show # Issue: https://gitlab.com/gitlab-org/gitlab/-/issues/343375 # More: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/73034#note_720186839 # https://docs.gitlab.com/ee/development/query_count_limits.html#disable-query-limiting - stub_const('Gitlab::QueryLimiting::Transaction::THRESHOLD', 110) + allow(Gitlab::QueryLimiting::Transaction).to receive(:threshold).and_return(110) expect_next_instance_of(SecurityReportsMrWidgetPromptExperiment) do |instance| expect(instance).to receive(:publish) end diff --git a/ee/spec/requests/registrations/project_creation_spec.rb b/ee/spec/requests/registrations/project_creation_spec.rb index a50282e633fda7faef3c9b4283a43cb8d66e3ac3..afdc189efcc8d2ff80efaf68f1e3c212b74e3597 100644 --- a/ee/spec/requests/registrations/project_creation_spec.rb +++ b/ee/spec/requests/registrations/project_creation_spec.rb @@ -33,7 +33,8 @@ context 'when group and project can be created' do it 'creates a group' do - stub_const('Gitlab::QueryLimiting::Transaction::THRESHOLD', 138) # 204 before creating learn gitlab in worker + # 204 before creating learn gitlab in worker + allow(Gitlab::QueryLimiting::Transaction).to receive(:threshold).and_return(138) expect { post users_sign_up_groups_projects_path, params: params }.to change(Group, :count).by(1) end diff --git a/lib/gitlab/query_limiting/transaction.rb b/lib/gitlab/query_limiting/transaction.rb index 2e31849caaa84303b05f9e46390d352a2b3ddc1d..46c0a0ddf7aa6baac2f693bc19f52c927fd974a5 100644 --- a/lib/gitlab/query_limiting/transaction.rb +++ b/lib/gitlab/query_limiting/transaction.rb @@ -14,8 +14,13 @@ class Transaction # The maximum number of SQL queries that can be executed in a request. For # the sake of keeping things simple we hardcode this value here, it's not # supposed to be changed very often anyway. - THRESHOLD = 100 - LOG_THRESHOLD = THRESHOLD * 1.5 + def self.threshold + 100 + end + + def self.log_threshold + threshold * 1.5 + end # Error that is raised whenever exceeding the maximum number of queries. ThresholdExceededError = Class.new(StandardError) @@ -76,7 +81,7 @@ def ignorable?(sql) end def executed_sql(sql) - return if @count > LOG_THRESHOLD || ignorable?(sql) + return if @count > self.class.log_threshold || ignorable?(sql) @sql_executed << sql end @@ -86,15 +91,15 @@ def raise_error? end def threshold_exceeded? - count > THRESHOLD + count > self.class.threshold end def error_message header = 'Too many SQL queries were executed' header = "#{header} in #{action}" if action - msg = "a maximum of #{THRESHOLD} is allowed but #{count} SQL queries were executed" + msg = "a maximum of #{self.class.threshold} is allowed but #{count} SQL queries were executed" log = @sql_executed.each_with_index.map { |sql, i| "#{i}: #{sql}" }.join("\n").presence - ellipsis = '...' if @count > LOG_THRESHOLD + ellipsis = '...' if @count > self.class.log_threshold ["#{header}: #{msg}", log, ellipsis].compact.join("\n") end @@ -105,3 +110,5 @@ def enabled? end end end + +Gitlab::QueryLimiting::Transaction.prepend_mod diff --git a/spec/features/projects/settings/user_transfers_a_project_spec.rb b/spec/features/projects/settings/user_transfers_a_project_spec.rb index 6041dca305b85461a55dff097b5e56e1544a7485..23e10a36cee8db83f33d4012b0c8024b4e83e714 100644 --- a/spec/features/projects/settings/user_transfers_a_project_spec.rb +++ b/spec/features/projects/settings/user_transfers_a_project_spec.rb @@ -8,7 +8,7 @@ let(:group) { create(:group) } before do - stub_const('Gitlab::QueryLimiting::Transaction::THRESHOLD', 120) + allow(Gitlab::QueryLimiting::Transaction).to receive(:threshold).and_return(120) group.add_owner(user) sign_in(user) diff --git a/spec/lib/gitlab/query_limiting/transaction_spec.rb b/spec/lib/gitlab/query_limiting/transaction_spec.rb index 27da1f2355699182e980ec2a8077f742e31da628..d8eb2040cccc95df070b8c2cb37a407117d14289 100644 --- a/spec/lib/gitlab/query_limiting/transaction_spec.rb +++ b/spec/lib/gitlab/query_limiting/transaction_spec.rb @@ -52,7 +52,7 @@ context 'when the query threshold is exceeded' do let(:transaction) do trans = described_class.new - trans.count = described_class::THRESHOLD + 1 + trans.count = described_class.threshold + 1 trans end @@ -120,7 +120,7 @@ it 'returns true when the threshold is exceeded' do transaction = described_class.new - transaction.count = described_class::THRESHOLD + 1 + transaction.count = described_class.threshold + 1 expect(transaction.threshold_exceeded?).to eq(true) end @@ -129,7 +129,7 @@ describe '#error_message' do it 'returns the error message to display when the threshold is exceeded' do transaction = described_class.new - transaction.count = max = described_class::THRESHOLD + transaction.count = max = described_class.threshold expect(transaction.error_message).to eq( "Too many SQL queries were executed: a maximum of #{max} " \ @@ -139,7 +139,7 @@ it 'includes a list of executed queries' do transaction = described_class.new - transaction.count = max = described_class::THRESHOLD + transaction.count = max = described_class.threshold %w[foo bar baz].each { |sql| transaction.executed_sql(sql) } message = transaction.error_message @@ -154,7 +154,7 @@ it 'indicates if the log is truncated' do transaction = described_class.new - transaction.count = described_class::THRESHOLD * 2 + transaction.count = described_class.threshold * 2 message = transaction.error_message @@ -163,7 +163,7 @@ it 'includes the action name in the error message when present' do transaction = described_class.new - transaction.count = max = described_class::THRESHOLD + transaction.count = max = described_class.threshold transaction.action = 'UsersController#show' expect(transaction.error_message).to eq( diff --git a/spec/requests/api/internal/base_spec.rb b/spec/requests/api/internal/base_spec.rb index 1f6c241b3f542a9876a2af8d3384e552ed95fa8a..4173a854421b2661a03f6d19043f7d9d6668a21c 100644 --- a/spec/requests/api/internal/base_spec.rb +++ b/spec/requests/api/internal/base_spec.rb @@ -1033,7 +1033,7 @@ def request context 'git push' do before do - stub_const('Gitlab::QueryLimiting::Transaction::THRESHOLD', 120) + allow(Gitlab::QueryLimiting::Transaction).to receive(:threshold).and_return(120) end subject { push_with_path(key, full_path: path, changes: '_any') } diff --git a/spec/support/helpers/graphql_helpers.rb b/spec/support/helpers/graphql_helpers.rb index 9d745f2cb7038ae8686b7842d87a2f8617e7055f..cf87ee697b693943b4d453db6b871a2a88bd14d8 100644 --- a/spec/support/helpers/graphql_helpers.rb +++ b/spec/support/helpers/graphql_helpers.rb @@ -717,7 +717,7 @@ def allow_high_graphql_recursion end def allow_high_graphql_transaction_threshold - stub_const("Gitlab::QueryLimiting::Transaction::THRESHOLD", 1000) + allow(Gitlab::QueryLimiting::Transaction).to receive(:threshold).and_return(1000) end def allow_high_graphql_query_size diff --git a/spec/support/shared_examples/requests/api/issuable_update_shared_examples.rb b/spec/support/shared_examples/requests/api/issuable_update_shared_examples.rb index a3378d4619b0ee2f562673068976845745e18a4a..1045a92f3322e4464180e3914a749e6d5ce2d3fa 100644 --- a/spec/support/shared_examples/requests/api/issuable_update_shared_examples.rb +++ b/spec/support/shared_examples/requests/api/issuable_update_shared_examples.rb @@ -20,7 +20,7 @@ end it 'updates the issuable with labels param as array' do - stub_const("Gitlab::QueryLimiting::Transaction::THRESHOLD", 110) + allow(Gitlab::QueryLimiting::Transaction).to receive(:threshold).and_return(110) params = { labels: ['label1', 'label2', 'foo, bar', '&,?'] }