Draft: Add instance concurrency limit multiplier for Sidekiq
What does this MR do and why?
Adds an instance-wide concurrency limit multiplier for Sidekiq workers, sidekiq_concurrency_limit_multiplier.
Explicit worker concurrency limits (concurrency_limit -> { 300 }) are sized for GitLab.com. On a smaller instance, a limit can be higher than the total number of Sidekiq threads. The limit then caps nothing, and one worker's backlog can saturate shared dependencies such as the primary database. Today, administrators of such instances have no persistent way to fit these limits to their size: the existing override API is per worker and expires after at most one day.
With this change, an administrator sets a multiplier, greater than 0, in Admin > Settings > Preferences > Sidekiq or through the application settings API. Every explicit limit is multiplied by it and rounded up. For example, 0.1 turns a limit of 300 into 30.
- The default of
1keeps the defined limits, so nothing changes until an administrator sets the value. That is why there is no feature flag. - A positive limit never drops below 1. Unlimited (
0) and paused (negative) limits are not changed. - Operator overrides, limits calculated from shard capacity, and
ConcurrencyLimit::ResumeWorkerare not scaled. - Adaptive throttling uses the scaled limit as its maximum.
- Values above 1 are accepted, for instances that want to raise limits. The docs don't recommend them, because they raise each limit above the value its worker was designed for.
- The value is stored in the existing
rate_limitssettings column, so no migration is needed. Processes pick up a change within the application settings cache period (60 seconds by default), without a restart.
The multiplier is converted to a rational number before multiplying, because 300 * 0.1 in floating point rounds up to 31.
References
- Related: https://gitlab.com/gitlab-org/gitlab/-/work_items/638594 (capacity-scaled concurrency ceilings)
- Related override API: !260640 (merged) and !260641 (merged)
Open questions for reviewers:
- Should limits that come from other settings, such as
import_jobs_concurrency_limit, be scaled too? They are scaled in this MR. - Should calculated shard defaults also be scaled? They are not, because they are already sized from the instance's capacity.
rate_limitsis aclusterwide: truecolumn. Is that acceptable for this setting?
Screenshots or screen recordings
To be added after validating the settings page in GDK.
| Before | After |
|---|---|
How to set up and validate locally
-
In the Rails console, check the current limit of a worker with an explicit limit:
Security::SyncProjectPolicyWorker.get_concurrency_limit # => 300 -
Go to Admin > Settings > Preferences, expand Sidekiq, set Concurrency limit multiplier to
0.1, and select Save changes. -
After about a minute, check the limit again:
Security::SyncProjectPolicyWorker.get_concurrency_limit # => 30 Gitlab::SidekiqMiddleware::ConcurrencyLimit::ConcurrencyLimitService.current_limit('Security::SyncProjectPolicyWorker') # => 30 -
Set the multiplier through the API and confirm that values of 0 or less are rejected:
curl --request PUT --header "PRIVATE-TOKEN: <admin token>" \ "http://gdk.test:3000/api/v4/application/settings?sidekiq_concurrency_limit_multiplier=0.5" curl --request PUT --header "PRIVATE-TOKEN: <admin token>" \ "http://gdk.test:3000/api/v4/application/settings?sidekiq_concurrency_limit_multiplier=0" # => 400
Database load
Adds no database queries per job and no migration. A multiplier below 1 lowers peak database concurrency from the capped workers. It does not change what each job costs.
| Check | Result |
|---|---|
| Where the value is stored | The existing rate_limits jsonb column on application_settings. No migration and no new column. |
| Read path | scale_concurrency_limit reads Gitlab::CurrentSettings.sidekiq_concurrency_limit_multiplier. Gitlab::CurrentSettings memoizes the settings record in Gitlab::SafeRequestStore. Below that, ApplicationSetting.cached reads Gitlab::ProcessMemoryCache, an in-process store that expires after 60 seconds by default. A call reaches the database only when that process cache has expired, and the reload is shared with every other settings read in the process. |
| When it is read | Only for workers with an explicit positive limit: on enqueue (ConcurrencyLimit::Client and LimitManager#enqueue_limit), when a job starts (LimitManager#current_limit), and in the throttling recovery service. Unlimited (0) and paused (negative) limits return before the settings read. |
| Per job | The server middleware runs after RequestStoreMiddleware, so every read in one job uses the same memoized settings object. No SQL. |
| Effect on load | Each capped worker runs at most ceil(limit * multiplier) jobs at once, so peak concurrent database work from those workers drops by up to that factor. The total work is the same and drains over a longer time. Deferred jobs wait in Redis, not in the database. |
Code checked
app/workers/concerns/worker_attributes.rb:get_concurrency_limitandscale_concurrency_limit.lib/gitlab/current_settings.rb:Gitlab::SafeRequestStore.fetch(:current_application_settings).app/models/concerns/cacheable_attributes.rbandApplicationSetting.cache_backend:Gitlab::ProcessMemoryCache, expiry fromapplication_settings_cache_seconds, default 60.lib/gitlab/sidekiq_middleware.rb:RequestStoreMiddlewarecomes beforeConcurrencyLimit::Serverin the server chain.lib/gitlab/sidekiq_middleware/concurrency_limit/limit_manager.rb,concurrency_limit/middleware.rbandthrottling/recovery_service.rb: the callers ofget_concurrency_limit.
MR acceptance checklist
Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.