debian-mirror-gitlab/doc/development/query_count_limits.md

71 lines
2.3 KiB
Markdown
Raw Normal View History

2021-01-29 00:20:46 +05:30
---
stage: none
group: unassigned
2021-02-22 17:27:13 +05:30
info: To determine the technical writer assigned to the Stage/Group associated with this page, see https://about.gitlab.com/handbook/engineering/ux/technical-writing/#assignments
2021-01-29 00:20:46 +05:30
---
2018-03-17 18:26:18 +05:30
# Query Count Limits
Each controller or API endpoint is allowed to execute up to 100 SQL queries and
2021-02-22 17:27:13 +05:30
in test environments we raise an error when this threshold is exceeded.
2018-03-17 18:26:18 +05:30
## Solving Failing Tests
When a test fails because it executes more than 100 SQL queries there are two
solutions to this problem:
2019-02-15 15:39:39 +05:30
- Reduce the number of SQL queries that are executed.
- Whitelist the controller or API endpoint.
2018-03-17 18:26:18 +05:30
You should only resort to whitelisting when an existing controller or endpoint
is to blame as in this case reducing the number of SQL queries can take a lot of
effort. Newly added controllers and endpoints are not allowed to execute more
2021-02-22 17:27:13 +05:30
than 100 SQL queries and no exceptions are made for this rule. _If_ a large
2018-03-17 18:26:18 +05:30
number of SQL queries is necessary to perform certain work it's best to have
this work performed by Sidekiq instead of doing this directly in a web request.
## Whitelisting
2021-02-22 17:27:13 +05:30
In the event that you _have_ to whitelist a controller you must first
2018-03-17 18:26:18 +05:30
create an issue. This issue should (preferably in the title) mention the
controller or endpoint and include the appropriate labels (`database`,
`performance`, and at least a team specific label such as `Discussion`).
2021-02-22 17:27:13 +05:30
After the issue has been created you can whitelist the code in question. For
2018-03-17 18:26:18 +05:30
Rails controllers it's best to create a `before_action` hook that runs as early
as possible. The called method in turn should call
`Gitlab::QueryLimiting.whitelist('issue URL here')`. For example:
```ruby
class MyController < ApplicationController
before_action :whitelist_query_limiting, only: [:show]
def index
# ...
end
def show
# ...
end
def whitelist_query_limiting
Gitlab::QueryLimiting.whitelist('https://gitlab.com/gitlab-org/...')
end
end
```
By using a `before_action` you don't have to modify the controller method in
question, reducing the likelihood of merge conflicts.
For Grape API endpoints there unfortunately is not a reliable way of running a
hook before a specific endpoint. This means that you have to add the whitelist
call directly into the endpoint like so:
```ruby
get '/projects/:id/foo' do
Gitlab::QueryLimiting.whitelist('...')
# ...
end
```