Skip to content
代码片段 群组 项目
提交 4843e5c1 编辑于 作者: Gregorius Marco's avatar Gregorius Marco
浏览文件

Add concurrency option on sidekiq-cluster CLI

The new concurrency option is a fixed value that doesnt depend on the
queues length. It is means to be a simple replacement over the
min-concurrency and max-concurrency.
https://gitlab.com/gitlab-com/gl-infra/scalability/-/issues/2760

Changelog: added
上级 816105e8
No related branches found
No related tags found
无相关合并请求
...@@ -40,6 +40,8 @@ def initialize(log_output = $stderr) ...@@ -40,6 +40,8 @@ def initialize(log_output = $stderr)
# https://ruby.social/@getajobmike/109326475545816363 # https://ruby.social/@getajobmike/109326475545816363
@max_concurrency = 20 @max_concurrency = 20
@min_concurrency = 0 @min_concurrency = 0
# TODO: to be set to 20 once max_concurrency and min_concurrency is removed https://gitlab.com/gitlab-com/gl-infra/scalability/-/issues/2760
@concurrency = 0
@environment = ENV['RAILS_ENV'] || 'development' @environment = ENV['RAILS_ENV'] || 'development'
@metrics_dir = ENV["prometheus_multiproc_dir"] || File.absolute_path("tmp/prometheus_multiproc_dir/sidekiq") @metrics_dir = ENV["prometheus_multiproc_dir"] || File.absolute_path("tmp/prometheus_multiproc_dir/sidekiq")
@pid = nil @pid = nil
...@@ -143,6 +145,7 @@ def start_and_supervise_workers(queue_groups) ...@@ -143,6 +145,7 @@ def start_and_supervise_workers(queue_groups)
directory: @rails_path, directory: @rails_path,
max_concurrency: @max_concurrency, max_concurrency: @max_concurrency,
min_concurrency: @min_concurrency, min_concurrency: @min_concurrency,
concurrency: @concurrency,
dryrun: @dryrun, dryrun: @dryrun,
timeout: @soft_timeout_seconds timeout: @soft_timeout_seconds
) )
...@@ -220,6 +223,10 @@ def option_parser ...@@ -220,6 +223,10 @@ def option_parser
abort opt.to_s abort opt.to_s
end end
opt.on('-c', '--concurrency INT', 'Number of threads to use with Sidekiq (default: 0)') do |int|
@concurrency = int.to_i
end
opt.on('-m', '--max-concurrency INT', 'Maximum threads to use with Sidekiq (default: 20, 0 to disable)') do |int| opt.on('-m', '--max-concurrency INT', 'Maximum threads to use with Sidekiq (default: 20, 0 to disable)') do |int|
@max_concurrency = int.to_i @max_concurrency = int.to_i
end end
......
...@@ -36,12 +36,13 @@ module SidekiqCluster ...@@ -36,12 +36,13 @@ module SidekiqCluster
# #
# Returns an Array containing the waiter threads (from Process.detach) of # Returns an Array containing the waiter threads (from Process.detach) of
# the started processes. # the started processes.
def self.start(queues, env: :development, directory: Dir.pwd, max_concurrency: 20, min_concurrency: 0, timeout: DEFAULT_SOFT_TIMEOUT_SECONDS, dryrun: false) def self.start(queues, env: :development, directory: Dir.pwd, max_concurrency: 20, min_concurrency: 0, concurrency: 0, timeout: DEFAULT_SOFT_TIMEOUT_SECONDS, dryrun: false)
queues.map.with_index do |pair, index| queues.map.with_index do |pair, index|
start_sidekiq(pair, env: env, start_sidekiq(pair, env: env,
directory: directory, directory: directory,
max_concurrency: max_concurrency, max_concurrency: max_concurrency,
min_concurrency: min_concurrency, min_concurrency: min_concurrency,
concurrency: concurrency,
worker_id: index, worker_id: index,
timeout: timeout, timeout: timeout,
dryrun: dryrun) dryrun: dryrun)
...@@ -51,11 +52,13 @@ def self.start(queues, env: :development, directory: Dir.pwd, max_concurrency: 2 ...@@ -51,11 +52,13 @@ def self.start(queues, env: :development, directory: Dir.pwd, max_concurrency: 2
# Starts a Sidekiq process that processes _only_ the given queues. # Starts a Sidekiq process that processes _only_ the given queues.
# #
# Returns the PID of the started process. # Returns the PID of the started process.
def self.start_sidekiq(queues, env:, directory:, max_concurrency:, min_concurrency:, worker_id:, timeout:, dryrun:) # rubocop: disable Metrics/ParameterLists -- max_concurrency and min_concurrency will be removed in 17.0
def self.start_sidekiq(queues, env:, directory:, max_concurrency:, min_concurrency:, concurrency:, worker_id:, timeout:, dryrun:)
# rubocop: enable Metrics/ParameterLists
counts = count_by_queue(queues) counts = count_by_queue(queues)
cmd = %w[bundle exec sidekiq] cmd = %w[bundle exec sidekiq]
cmd << "-c#{self.concurrency(queues, min_concurrency, max_concurrency)}" cmd << "-c#{self.concurrency(queues, min_concurrency, max_concurrency, concurrency)}"
cmd << "-e#{env}" cmd << "-e#{env}"
cmd << "-t#{timeout}" cmd << "-t#{timeout}"
cmd << "-gqueues:#{proc_details(counts)}" cmd << "-gqueues:#{proc_details(counts)}"
...@@ -101,7 +104,9 @@ def self.proc_details(counts) ...@@ -101,7 +104,9 @@ def self.proc_details(counts)
end.join(',') end.join(',')
end end
def self.concurrency(queues, min_concurrency, max_concurrency) def self.concurrency(queues, min_concurrency, max_concurrency, concurrency)
return concurrency if concurrency > 0
concurrency_from_queues = queues.length + 1 concurrency_from_queues = queues.length + 1
max = max_concurrency > 0 ? max_concurrency : concurrency_from_queues max = max_concurrency > 0 ? max_concurrency : concurrency_from_queues
min = [min_concurrency, max].min min = [min_concurrency, max].min
......
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
let(:cli) { described_class.new('/dev/null') } let(:cli) { described_class.new('/dev/null') }
let(:timeout) { Gitlab::SidekiqCluster::DEFAULT_SOFT_TIMEOUT_SECONDS } let(:timeout) { Gitlab::SidekiqCluster::DEFAULT_SOFT_TIMEOUT_SECONDS }
let(:default_options) do let(:default_options) do
{ env: 'test', directory: Dir.pwd, max_concurrency: 20, min_concurrency: 0, dryrun: false, timeout: timeout } { env: 'test', directory: Dir.pwd, max_concurrency: 20, min_concurrency: 0, dryrun: false, timeout: timeout, concurrency: 0 }
end end
let(:sidekiq_exporter_enabled) { false } let(:sidekiq_exporter_enabled) { false }
...@@ -125,6 +125,18 @@ ...@@ -125,6 +125,18 @@
end end
end end
context 'with --concurrency flag' do
it 'starts Sidekiq workers for specified queues with the fixed concurrency' do
expected_queues = [%w[foo bar baz], %w[solo]].each { |queues| queues.concat(described_class::DEFAULT_QUEUES) }
expect(Gitlab::SidekiqConfig::CliMethods).to receive(:worker_queues).and_return(%w[foo bar baz])
expect(Gitlab::SidekiqCluster).to receive(:start)
.with(expected_queues, default_options.merge(concurrency: 2))
.and_return([])
cli.run(%w[foo,bar,baz solo -c 2])
end
end
context 'with --timeout flag' do context 'with --timeout flag' do
it 'when given', 'starts Sidekiq workers with given timeout' do it 'when given', 'starts Sidekiq workers with given timeout' do
expect(Gitlab::SidekiqCluster).to receive(:start) expect(Gitlab::SidekiqCluster).to receive(:start)
......
...@@ -42,7 +42,8 @@ ...@@ -42,7 +42,8 @@
min_concurrency: 0, min_concurrency: 0,
worker_id: an_instance_of(Integer), worker_id: an_instance_of(Integer),
timeout: 25, timeout: 25,
dryrun: false dryrun: false,
concurrency: 0
} }
expect(described_class).to receive(:start_sidekiq).ordered.with(%w[foo bar baz], expected_options) expect(described_class).to receive(:start_sidekiq).ordered.with(%w[foo bar baz], expected_options)
...@@ -55,7 +56,7 @@ ...@@ -55,7 +56,7 @@
describe '.start_sidekiq' do describe '.start_sidekiq' do
let(:first_worker_id) { 0 } let(:first_worker_id) { 0 }
let(:options) do let(:options) do
{ env: :production, directory: 'foo/bar', max_concurrency: 20, min_concurrency: 0, worker_id: first_worker_id, timeout: 10, dryrun: false } { env: :production, directory: 'foo/bar', max_concurrency: 20, min_concurrency: 0, worker_id: first_worker_id, timeout: 10, dryrun: false, concurrency: 0 }
end end
let(:env) { { "ENABLE_SIDEKIQ_CLUSTER" => "1", "SIDEKIQ_WORKER_ID" => first_worker_id.to_s } } let(:env) { { "ENABLE_SIDEKIQ_CLUSTER" => "1", "SIDEKIQ_WORKER_ID" => first_worker_id.to_s } }
...@@ -102,21 +103,28 @@ ...@@ -102,21 +103,28 @@
describe '.concurrency' do describe '.concurrency' do
using RSpec::Parameterized::TableSyntax using RSpec::Parameterized::TableSyntax
where(:queue_count, :min, :max, :expected) do where(:queue_count, :min, :max, :fixed_concurrency, :expected) do
2 | 0 | 0 | 3 # No min or max specified # without fixed concurrency
2 | 0 | 9 | 3 # No min specified, value < max 2 | 0 | 0 | 0 | 3 # No min or max specified
2 | 1 | 4 | 3 # Value between min and max 2 | 0 | 9 | 0 | 3 # No min specified, value < max
2 | 4 | 5 | 4 # Value below range 2 | 1 | 4 | 0 | 3 # Value between min and max
5 | 2 | 3 | 3 # Value above range 2 | 4 | 5 | 0 | 4 # Value below range
2 | 1 | 1 | 1 # Value above explicit setting (min == max) 5 | 2 | 3 | 0 | 3 # Value above range
0 | 3 | 3 | 3 # Value below explicit setting (min == max) 2 | 1 | 1 | 0 | 1 # Value above explicit setting (min == max)
1 | 4 | 3 | 3 # Min greater than max 0 | 3 | 3 | 0 | 3 # Value below explicit setting (min == max)
1 | 4 | 3 | 0 | 3 # Min greater than max
# with fixed concurrency, expected always equal to fixed_concurrency
1 | 0 | 20 | 20 | 20
1 | 0 | 20 | 10 | 10
1 | 20 | 20 | 10 | 10
5 | 0 | 0 | 10 | 10
end end
with_them do with_them do
let(:queues) { Array.new(queue_count) } let(:queues) { Array.new(queue_count) }
it { expect(described_class.concurrency(queues, min, max)).to eq(expected) } it { expect(described_class.concurrency(queues, min, max, fixed_concurrency)).to eq(expected) }
end end
end end
end end
0% 加载中 .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册