2018-08-30 15:35:32 -04:00
require 'sidekiq/web'
2019-04-25 14:13:52 -04:00
def enable_reliable_fetch?
return true unless Feature :: FlipperFeature . table_exists?
Feature . enabled? ( :gitlab_sidekiq_reliable_fetcher , default_enabled : true )
end
def enable_semi_reliable_fetch_mode?
return true unless Feature :: FlipperFeature . table_exists?
Feature . enabled? ( :gitlab_sidekiq_enable_semi_reliable_fetcher , default_enabled : true )
end
2018-08-30 15:35:32 -04:00
# Disable the Sidekiq Rack session since GitLab already has its own session store.
# CSRF protection still works (https://github.com/mperham/sidekiq/commit/315504e766c4fd88a29b7772169060afc4c40329).
Sidekiq :: Web . set :sessions , false
2017-07-10 23:35:47 -04:00
# Custom Queues configuration
queues_config_hash = Gitlab :: Redis :: Queues . params
queues_config_hash [ :namespace ] = Gitlab :: Redis :: Queues :: SIDEKIQ_NAMESPACE
2015-11-27 15:41:21 -05:00
2016-11-04 15:48:05 -04:00
# Default is to retry 25 times with exponential backoff. That's too much.
Sidekiq . default_worker_options = { retry : 3 }
2019-02-24 23:16:02 -05:00
if Rails . env . development?
Sidekiq . default_worker_options [ :backtrace ] = true
end
2018-04-01 03:46:52 -04:00
enable_json_logs = Gitlab . config . sidekiq . log_format == 'json'
2013-01-09 00:14:05 -05:00
Sidekiq . configure_server do | config |
2017-07-10 23:35:47 -04:00
config . redis = queues_config_hash
2014-07-03 10:23:41 -04:00
config . server_middleware do | chain |
2019-07-29 07:53:12 -04:00
chain . add Gitlab :: SidekiqMiddleware :: Metrics if Settings . monitoring . sidekiq_exporter
2018-04-01 03:46:52 -04:00
chain . add Gitlab :: SidekiqMiddleware :: ArgumentsLogger if ENV [ 'SIDEKIQ_LOG_ARGUMENTS' ] && ! enable_json_logs
2019-02-28 09:45:20 -05:00
chain . add Gitlab :: SidekiqMiddleware :: MemoryKiller if ENV [ 'SIDEKIQ_MEMORY_KILLER_MAX_RSS' ]
2016-07-25 21:05:14 -04:00
chain . add Gitlab :: SidekiqMiddleware :: RequestStoreMiddleware unless ENV [ 'SIDEKIQ_REQUEST_STORE' ] == '0'
2018-11-22 11:28:09 -05:00
chain . add Gitlab :: SidekiqMiddleware :: BatchLoader
2018-12-05 15:54:40 -05:00
chain . add Gitlab :: SidekiqMiddleware :: CorrelationLogger
Fix race conditions for AuthorizedProjectsWorker
There were two cases that could be problematic:
1. Because sometimes AuthorizedProjectsWorker would be scheduled in a
transaction it was possible for a job to run/complete before a
COMMIT; resulting in it either producing an error, or producing no
new data.
2. When scheduling jobs the code would not wait until completion. This
could lead to a user creating a project and then immediately trying
to push to it. Usually this will work fine, but given enough load it
might take a few seconds before a user has access.
The first one is problematic, the second one is mostly just annoying
(but annoying enough to warrant a solution).
This commit changes two things to deal with this:
1. Sidekiq scheduling now takes places after a COMMIT, this is ensured
by scheduling using Rails' after_commit hook instead of doing so in
an arbitrary method.
2. When scheduling jobs the calling thread now waits for all jobs to
complete.
Solution 2 requires tracking of job completions. Sidekiq provides a way
to find a job by its ID, but this involves scanning over the entire
queue; something that is very in-efficient for large queues. As such a
more efficient solution is necessary. There are two main Gems that can
do this in a more efficient manner:
* sidekiq-status
* sidekiq_status
No, this is not a joke. Both Gems do a similar thing (but slightly
different), and the only difference in their name is a dash vs an
underscore. Both Gems however provide far more than just checking if a
job has been completed, and both have their problems. sidekiq-status
does not appear to be actively maintained, with the last release being
in 2015. It also has some issues during testing as API calls are not
stubbed in any way. sidekiq_status on the other hand does not appear to
be very popular, and introduces a similar amount of code.
Because of this I opted to write a simple home grown solution. After
all, all we need is storing a job ID somewhere so we can efficiently
look it up; we don't need extra web UIs (as provided by sidekiq-status)
or complex APIs to update progress, etc.
This is where Gitlab::SidekiqStatus comes in handy. This namespace
contains some code used for tracking, removing, and looking up job IDs;
all without having to scan over an entire queue. Data is removed
explicitly, but also expires automatically just in case.
Using this API we can now schedule jobs in a fork-join like manner: we
schedule the jobs in Sidekiq, process them in parallel, then wait for
completion. By using Sidekiq we can leverage all the benefits such as
being able to scale across multiple cores and hosts, retrying failed
jobs, etc.
The one downside is that we need to make sure we can deal with
unexpected increases in job processing timings. To deal with this the
class Gitlab::JobWaiter (used for waiting for jobs to complete) will
only wait a number of seconds (30 by default). Once this timeout is
reached it will simply return.
For GitLab.com almost all AuthorizedProjectWorker jobs complete in
seconds, only very rarely do we spike to job timings of around a minute.
These in turn seem to be the result of external factors (e.g. deploys),
in which case a user is most likely not able to use the system anyway.
In short, this new solution should ensure that jobs are processed
properly and that in almost all cases a user has access to their
resources whenever they need to have access.
2017-01-22 12:22:02 -05:00
chain . add Gitlab :: SidekiqStatus :: ServerMiddleware
end
2018-04-01 03:46:52 -04:00
if enable_json_logs
Sidekiq . logger . formatter = Gitlab :: SidekiqLogging :: JSONFormatter . new
config . options [ :job_logger ] = Gitlab :: SidekiqLogging :: StructuredLogger
end
Fix race conditions for AuthorizedProjectsWorker
There were two cases that could be problematic:
1. Because sometimes AuthorizedProjectsWorker would be scheduled in a
transaction it was possible for a job to run/complete before a
COMMIT; resulting in it either producing an error, or producing no
new data.
2. When scheduling jobs the code would not wait until completion. This
could lead to a user creating a project and then immediately trying
to push to it. Usually this will work fine, but given enough load it
might take a few seconds before a user has access.
The first one is problematic, the second one is mostly just annoying
(but annoying enough to warrant a solution).
This commit changes two things to deal with this:
1. Sidekiq scheduling now takes places after a COMMIT, this is ensured
by scheduling using Rails' after_commit hook instead of doing so in
an arbitrary method.
2. When scheduling jobs the calling thread now waits for all jobs to
complete.
Solution 2 requires tracking of job completions. Sidekiq provides a way
to find a job by its ID, but this involves scanning over the entire
queue; something that is very in-efficient for large queues. As such a
more efficient solution is necessary. There are two main Gems that can
do this in a more efficient manner:
* sidekiq-status
* sidekiq_status
No, this is not a joke. Both Gems do a similar thing (but slightly
different), and the only difference in their name is a dash vs an
underscore. Both Gems however provide far more than just checking if a
job has been completed, and both have their problems. sidekiq-status
does not appear to be actively maintained, with the last release being
in 2015. It also has some issues during testing as API calls are not
stubbed in any way. sidekiq_status on the other hand does not appear to
be very popular, and introduces a similar amount of code.
Because of this I opted to write a simple home grown solution. After
all, all we need is storing a job ID somewhere so we can efficiently
look it up; we don't need extra web UIs (as provided by sidekiq-status)
or complex APIs to update progress, etc.
This is where Gitlab::SidekiqStatus comes in handy. This namespace
contains some code used for tracking, removing, and looking up job IDs;
all without having to scan over an entire queue. Data is removed
explicitly, but also expires automatically just in case.
Using this API we can now schedule jobs in a fork-join like manner: we
schedule the jobs in Sidekiq, process them in parallel, then wait for
completion. By using Sidekiq we can leverage all the benefits such as
being able to scale across multiple cores and hosts, retrying failed
jobs, etc.
The one downside is that we need to make sure we can deal with
unexpected increases in job processing timings. To deal with this the
class Gitlab::JobWaiter (used for waiting for jobs to complete) will
only wait a number of seconds (30 by default). Once this timeout is
reached it will simply return.
For GitLab.com almost all AuthorizedProjectWorker jobs complete in
seconds, only very rarely do we spike to job timings of around a minute.
These in turn seem to be the result of external factors (e.g. deploys),
in which case a user is most likely not able to use the system anyway.
In short, this new solution should ensure that jobs are processed
properly and that in almost all cases a user has access to their
resources whenever they need to have access.
2017-01-22 12:22:02 -05:00
config . client_middleware do | chain |
chain . add Gitlab :: SidekiqStatus :: ClientMiddleware
2018-12-05 15:54:40 -05:00
chain . add Gitlab :: SidekiqMiddleware :: CorrelationInjector
2014-07-03 10:23:41 -04:00
end
2015-12-03 18:24:39 -05:00
2017-03-07 12:01:20 -05:00
config . on :startup do
# Clear any connections that might have been obtained before starting
# Sidekiq (e.g. in an initializer).
ActiveRecord :: Base . clear_all_connections!
end
2019-04-25 11:59:53 -04:00
if enable_reliable_fetch?
config . options [ :semi_reliable_fetch ] = enable_semi_reliable_fetch_mode?
2018-12-14 07:14:46 -05:00
Sidekiq :: ReliableFetch . setup_reliable_fetch! ( config )
2018-09-12 04:53:42 -04:00
end
2015-12-12 20:06:23 -05:00
# Sidekiq-cron: load recurring jobs from gitlab.yml
2015-12-22 09:42:39 -05:00
# UGLY Hack to get nested hash from settingslogic
cron_jobs = JSON . parse ( Gitlab . config . cron_jobs . to_json )
# UGLY hack: Settingslogic doesn't allow 'class' key
2016-07-18 06:57:57 -04:00
cron_jobs_required_keys = %w( job_class cron )
cron_jobs . each do | k , v |
if cron_jobs [ k ] && cron_jobs_required_keys . all? { | s | cron_jobs [ k ] . key? ( s ) }
cron_jobs [ k ] [ 'class' ] = cron_jobs [ k ] . delete ( 'job_class' )
else
2016-07-20 06:21:38 -04:00
cron_jobs . delete ( k )
2019-07-10 15:26:47 -04:00
Rails . logger . error ( " Invalid cron_jobs config key: ' #{ k } '. Check your gitlab config file. " ) # rubocop:disable Gitlab/RailsLogger
2016-07-18 06:57:57 -04:00
end
end
2015-12-22 09:42:39 -05:00
Sidekiq :: Cron :: Job . load_from_hash! cron_jobs
2015-12-10 12:45:36 -05:00
2017-11-28 11:16:50 -05:00
Gitlab :: SidekiqVersioning . install!
2018-09-12 04:53:42 -04:00
db_config = Gitlab :: Database . config ||
2016-12-15 17:14:20 -05:00
Rails . application . config . database_configuration [ Rails . env ]
2018-09-12 04:53:42 -04:00
db_config [ 'pool' ] = Sidekiq . options [ :concurrency ]
ActiveRecord :: Base . establish_connection ( db_config )
2019-07-10 15:26:47 -04:00
Rails . logger . debug ( " Connection Pool size for Sidekiq Server is now: #{ ActiveRecord :: Base . connection . pool . instance_variable_get ( '@size' ) } " ) # rubocop:disable Gitlab/RailsLogger
2016-06-20 06:43:29 -04:00
2019-07-08 15:47:16 -04:00
Gitlab . ee do
Gitlab :: Mirror . configure_cron_job!
Gitlab :: Geo . configure_cron_jobs!
if Gitlab :: Geo . geo_database_configured?
Rails . configuration . geo_database [ 'pool' ] = Sidekiq . options [ :concurrency ]
Geo :: TrackingBase . establish_connection ( Rails . configuration . geo_database )
2019-07-10 15:26:47 -04:00
Rails . logger . debug ( " Connection Pool size for Sidekiq Server is now: #{ Geo :: TrackingBase . connection_pool . size } (Geo tracking database) " ) # rubocop:disable Gitlab/RailsLogger
2019-07-08 15:47:16 -04:00
end
end
2016-06-20 06:43:29 -04:00
# Avoid autoload issue such as 'Mail::Parsers::AddressStruct'
# https://github.com/mikel/mail/issues/912#issuecomment-214850355
Mail . eager_autoload!
2019-02-28 18:25:37 -05:00
# Ensure the whole process group is terminated if possible
Gitlab :: SidekiqSignals . install! ( Sidekiq :: CLI :: SIGNAL_HANDLERS )
2012-09-22 11:23:12 -04:00
end
2013-01-09 00:14:05 -05:00
Sidekiq . configure_client do | config |
2017-07-10 23:35:47 -04:00
config . redis = queues_config_hash
Fix race conditions for AuthorizedProjectsWorker
There were two cases that could be problematic:
1. Because sometimes AuthorizedProjectsWorker would be scheduled in a
transaction it was possible for a job to run/complete before a
COMMIT; resulting in it either producing an error, or producing no
new data.
2. When scheduling jobs the code would not wait until completion. This
could lead to a user creating a project and then immediately trying
to push to it. Usually this will work fine, but given enough load it
might take a few seconds before a user has access.
The first one is problematic, the second one is mostly just annoying
(but annoying enough to warrant a solution).
This commit changes two things to deal with this:
1. Sidekiq scheduling now takes places after a COMMIT, this is ensured
by scheduling using Rails' after_commit hook instead of doing so in
an arbitrary method.
2. When scheduling jobs the calling thread now waits for all jobs to
complete.
Solution 2 requires tracking of job completions. Sidekiq provides a way
to find a job by its ID, but this involves scanning over the entire
queue; something that is very in-efficient for large queues. As such a
more efficient solution is necessary. There are two main Gems that can
do this in a more efficient manner:
* sidekiq-status
* sidekiq_status
No, this is not a joke. Both Gems do a similar thing (but slightly
different), and the only difference in their name is a dash vs an
underscore. Both Gems however provide far more than just checking if a
job has been completed, and both have their problems. sidekiq-status
does not appear to be actively maintained, with the last release being
in 2015. It also has some issues during testing as API calls are not
stubbed in any way. sidekiq_status on the other hand does not appear to
be very popular, and introduces a similar amount of code.
Because of this I opted to write a simple home grown solution. After
all, all we need is storing a job ID somewhere so we can efficiently
look it up; we don't need extra web UIs (as provided by sidekiq-status)
or complex APIs to update progress, etc.
This is where Gitlab::SidekiqStatus comes in handy. This namespace
contains some code used for tracking, removing, and looking up job IDs;
all without having to scan over an entire queue. Data is removed
explicitly, but also expires automatically just in case.
Using this API we can now schedule jobs in a fork-join like manner: we
schedule the jobs in Sidekiq, process them in parallel, then wait for
completion. By using Sidekiq we can leverage all the benefits such as
being able to scale across multiple cores and hosts, retrying failed
jobs, etc.
The one downside is that we need to make sure we can deal with
unexpected increases in job processing timings. To deal with this the
class Gitlab::JobWaiter (used for waiting for jobs to complete) will
only wait a number of seconds (30 by default). Once this timeout is
reached it will simply return.
For GitLab.com almost all AuthorizedProjectWorker jobs complete in
seconds, only very rarely do we spike to job timings of around a minute.
These in turn seem to be the result of external factors (e.g. deploys),
in which case a user is most likely not able to use the system anyway.
In short, this new solution should ensure that jobs are processed
properly and that in almost all cases a user has access to their
resources whenever they need to have access.
2017-01-22 12:22:02 -05:00
config . client_middleware do | chain |
2018-12-05 15:54:40 -05:00
chain . add Gitlab :: SidekiqMiddleware :: CorrelationInjector
Fix race conditions for AuthorizedProjectsWorker
There were two cases that could be problematic:
1. Because sometimes AuthorizedProjectsWorker would be scheduled in a
transaction it was possible for a job to run/complete before a
COMMIT; resulting in it either producing an error, or producing no
new data.
2. When scheduling jobs the code would not wait until completion. This
could lead to a user creating a project and then immediately trying
to push to it. Usually this will work fine, but given enough load it
might take a few seconds before a user has access.
The first one is problematic, the second one is mostly just annoying
(but annoying enough to warrant a solution).
This commit changes two things to deal with this:
1. Sidekiq scheduling now takes places after a COMMIT, this is ensured
by scheduling using Rails' after_commit hook instead of doing so in
an arbitrary method.
2. When scheduling jobs the calling thread now waits for all jobs to
complete.
Solution 2 requires tracking of job completions. Sidekiq provides a way
to find a job by its ID, but this involves scanning over the entire
queue; something that is very in-efficient for large queues. As such a
more efficient solution is necessary. There are two main Gems that can
do this in a more efficient manner:
* sidekiq-status
* sidekiq_status
No, this is not a joke. Both Gems do a similar thing (but slightly
different), and the only difference in their name is a dash vs an
underscore. Both Gems however provide far more than just checking if a
job has been completed, and both have their problems. sidekiq-status
does not appear to be actively maintained, with the last release being
in 2015. It also has some issues during testing as API calls are not
stubbed in any way. sidekiq_status on the other hand does not appear to
be very popular, and introduces a similar amount of code.
Because of this I opted to write a simple home grown solution. After
all, all we need is storing a job ID somewhere so we can efficiently
look it up; we don't need extra web UIs (as provided by sidekiq-status)
or complex APIs to update progress, etc.
This is where Gitlab::SidekiqStatus comes in handy. This namespace
contains some code used for tracking, removing, and looking up job IDs;
all without having to scan over an entire queue. Data is removed
explicitly, but also expires automatically just in case.
Using this API we can now schedule jobs in a fork-join like manner: we
schedule the jobs in Sidekiq, process them in parallel, then wait for
completion. By using Sidekiq we can leverage all the benefits such as
being able to scale across multiple cores and hosts, retrying failed
jobs, etc.
The one downside is that we need to make sure we can deal with
unexpected increases in job processing timings. To deal with this the
class Gitlab::JobWaiter (used for waiting for jobs to complete) will
only wait a number of seconds (30 by default). Once this timeout is
reached it will simply return.
For GitLab.com almost all AuthorizedProjectWorker jobs complete in
seconds, only very rarely do we spike to job timings of around a minute.
These in turn seem to be the result of external factors (e.g. deploys),
in which case a user is most likely not able to use the system anyway.
In short, this new solution should ensure that jobs are processed
properly and that in almost all cases a user has access to their
resources whenever they need to have access.
2017-01-22 12:22:02 -05:00
chain . add Gitlab :: SidekiqStatus :: ClientMiddleware
end
2013-01-09 00:14:05 -05:00
end