Module: Gitlab::SidekiqMiddleware

Defined in:
lib/gitlab/sidekiq_middleware.rb,
lib/gitlab/sidekiq_middleware/monitor.rb,
lib/gitlab/sidekiq_middleware/skip_jobs.rb,
lib/gitlab/sidekiq_middleware/retry_error.rb,
lib/gitlab/sidekiq_middleware/batch_loader.rb,
lib/gitlab/sidekiq_middleware/pause_control.rb,
lib/gitlab/sidekiq_middleware/client_metrics.rb,
lib/gitlab/sidekiq_middleware/metrics_helper.rb,
lib/gitlab/sidekiq_middleware/query_analyzer.rb,
lib/gitlab/sidekiq_middleware/server_metrics.rb,
lib/gitlab/sidekiq_middleware/set_ip_address.rb,
lib/gitlab/sidekiq_middleware/worker_context.rb,
lib/gitlab/sidekiq_middleware/arguments_logger.rb,
lib/gitlab/sidekiq_middleware/identity/restore.rb,
lib/gitlab/sidekiq_middleware/admin_mode/client.rb,
lib/gitlab/sidekiq_middleware/admin_mode/server.rb,
lib/gitlab/sidekiq_middleware/size_limiter/client.rb,
lib/gitlab/sidekiq_middleware/size_limiter/server.rb,
lib/gitlab/sidekiq_middleware/identity/passthrough.rb,
lib/gitlab/sidekiq_middleware/pause_control/client.rb,
lib/gitlab/sidekiq_middleware/pause_control/server.rb,
lib/gitlab/sidekiq_middleware/duplicate_jobs/client.rb,
lib/gitlab/sidekiq_middleware/duplicate_jobs/cookie.rb,
lib/gitlab/sidekiq_middleware/duplicate_jobs/server.rb,
lib/gitlab/sidekiq_middleware/worker_context/client.rb,
lib/gitlab/sidekiq_middleware/worker_context/server.rb,
lib/gitlab/sidekiq_middleware/instrumentation_logger.rb,
lib/gitlab/sidekiq_middleware/size_limiter/validator.rb,
lib/gitlab/sidekiq_middleware/extra_done_log_metadata.rb,
lib/gitlab/sidekiq_middleware/size_limiter/compressor.rb,
lib/gitlab/sidekiq_middleware/concurrency_limit/client.rb,
lib/gitlab/sidekiq_middleware/concurrency_limit/resume.rb,
lib/gitlab/sidekiq_middleware/concurrency_limit/server.rb,
lib/gitlab/sidekiq_middleware/request_store_middleware.rb,
lib/gitlab/sidekiq_middleware/duplicate_jobs/strategies.rb,
lib/gitlab/sidekiq_middleware/pause_control/workers_map.rb,
lib/gitlab/sidekiq_middleware/shard_awareness_validator.rb,
lib/gitlab/sidekiq_middleware/resource_usage_limit/server.rb,
lib/gitlab/sidekiq_middleware/concurrency_limit/middleware.rb,
lib/gitlab/sidekiq_middleware/duplicate_jobs/duplicate_job.rb,
lib/gitlab/sidekiq_middleware/concurrency_limit/workers_map.rb,
lib/gitlab/sidekiq_middleware/pause_control/strategies/base.rb,
lib/gitlab/sidekiq_middleware/pause_control/strategies/none.rb,
lib/gitlab/sidekiq_middleware/duplicate_jobs/strategies/base.rb,
lib/gitlab/sidekiq_middleware/duplicate_jobs/strategies/none.rb,
lib/gitlab/sidekiq_middleware/pause_control/strategies/zoekt.rb,
lib/gitlab/sidekiq_middleware/pause_control/strategy_handler.rb,
lib/gitlab/sidekiq_middleware/concurrency_limit/queue_manager.rb,
lib/gitlab/sidekiq_middleware/resource_usage_limit/middleware.rb,
lib/gitlab/sidekiq_middleware/size_limiter/exceed_limit_error.rb,
lib/gitlab/sidekiq_middleware/pause_control/pause_control_service.rb,
lib/gitlab/sidekiq_middleware/pause_control/strategies/deprecated.rb,
lib/gitlab/sidekiq_middleware/duplicate_jobs/strategies/until_executed.rb,
lib/gitlab/sidekiq_middleware/pause_control/strategies/advanced_search.rb,
lib/gitlab/sidekiq_middleware/duplicate_jobs/strategies/until_executing.rb,
lib/gitlab/sidekiq_middleware/concurrency_limit/worker_execution_tracker.rb,
lib/gitlab/sidekiq_middleware/concurrency_limit/concurrency_limit_service.rb,
lib/gitlab/sidekiq_middleware/pause_control/strategies/click_house_migration.rb,
lib/gitlab/sidekiq_middleware/duplicate_jobs/strategies/deduplicates_when_scheduling.rb

Overview

The SidekiqMiddleware class is responsible for configuring the middleware stacks used in the client and server middlewares

Defined Under Namespace

Modules: AdminMode, ConcurrencyLimit, DuplicateJobs, Identity, MetricsHelper, PauseControl, ResourceUsageLimit, SizeLimiter, WorkerContext Classes: ArgumentsLogger, BatchLoader, ClientMetrics, ExtraDoneLogMetadata, InstrumentationLogger, Monitor, QueryAnalyzer, RequestStoreMiddleware, ServerMetrics, SetIpAddress, ShardAwarenessValidator, SkipJobs

Constant Summary collapse

RetryError =

Sidekiq retry error that won’t be reported to Sentry Use it when a job retry is an expected behavior

Class.new(StandardError)

Class Method Summary collapse

Class Method Details

.client_configuratorObject

The result of this method should be passed to Sidekiq’s ‘config.client_middleware` method eg: `config.client_middleware(&Gitlab::SidekiqMiddleware.client_configurator)`



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/gitlab/sidekiq_middleware.rb', line 10

def self.client_configurator
  ->(chain) do
    # ConcurrencyLimit::Resume needs to be first and before Labkit and ConcurrencyLimit::Client
    chain.add ::Gitlab::SidekiqMiddleware::ConcurrencyLimit::Resume
    chain.add ::Gitlab::SidekiqMiddleware::WorkerContext::Client # needs to be before the Labkit middleware
    chain.add ::Labkit::Middleware::Sidekiq::Client
    # Sidekiq Client Middleware should be placed before DuplicateJobs::Client middleware,
    # so we can store WAL location before we deduplicate the job.
    chain.add ::Gitlab::Database::LoadBalancing::SidekiqClientMiddleware
    chain.add ::Gitlab::SidekiqMiddleware::PauseControl::Client
    chain.add ::Gitlab::SidekiqMiddleware::ConcurrencyLimit::Client
    # NOTE: Everything from DuplicateJobs::Client to DuplicateJobs::Server must yield
    # no returning or job interception as it will leave the duplicate job redis key
    # dangling and errorneously deduplicating future jobs until key expires.
    chain.add ::Gitlab::SidekiqMiddleware::DuplicateJobs::Client
    chain.add ::Gitlab::SidekiqStatus::ClientMiddleware
    chain.add ::Gitlab::SidekiqMiddleware::AdminMode::Client
    # Size limiter should be placed at the bottom, but before the metrics middleware
    # NOTE: this raises error after the duplicate jobs middleware but is acceptable
    # since jobs that are too large should not be enqueued.
    chain.add ::Gitlab::SidekiqMiddleware::SizeLimiter::Client
    chain.add ::Gitlab::SidekiqMiddleware::ClientMetrics
    chain.add ::Gitlab::SidekiqMiddleware::Identity::Passthrough
  end
end

.server_configurator(metrics: true, arguments_logger: true, skip_jobs: true) ⇒ Object

The result of this method should be passed to Sidekiq’s ‘config.server_middleware` method eg: `config.server_middleware(&Gitlab::SidekiqMiddleware.server_configurator)`



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/gitlab/sidekiq_middleware.rb', line 39

def self.server_configurator(metrics: true, arguments_logger: true, skip_jobs: true)
  ->(chain) do
    # Size limiter should be placed at the top
    chain.add ::Gitlab::SidekiqMiddleware::SizeLimiter::Server
    chain.add ::Gitlab::SidekiqMiddleware::ShardAwarenessValidator
    chain.add ::Gitlab::SidekiqMiddleware::Monitor

    # Labkit wraps the job in the `Labkit::Context` resurrected from
    # the job-hash. We need properties from the context for
    # recording metrics, so this needs to be before
    # `::Gitlab::SidekiqMiddleware::ServerMetrics` (if we're using
    # that).
    chain.add ::Labkit::Middleware::Sidekiq::Server
    chain.add ::Gitlab::SidekiqMiddleware::RequestStoreMiddleware

    chain.add ::Gitlab::QueryLimiting::SidekiqMiddleware if ::Gitlab::QueryLimiting.enabled_for_env?

    if metrics
      chain.add ::Gitlab::SidekiqMiddleware::ServerMetrics

      ::Gitlab::SidekiqMiddleware::ServerMetrics.initialize_process_metrics
    end

    chain.add ::Gitlab::SidekiqMiddleware::ArgumentsLogger if arguments_logger
    chain.add ::Gitlab::SidekiqMiddleware::ExtraDoneLogMetadata
    chain.add ::Gitlab::SidekiqMiddleware::BatchLoader
    chain.add ::Gitlab::SidekiqMiddleware::InstrumentationLogger
    chain.add ::Gitlab::SidekiqMiddleware::SetIpAddress
    chain.add ::Gitlab::SidekiqMiddleware::AdminMode::Server
    chain.add ::Gitlab::SidekiqMiddleware::QueryAnalyzer
    chain.add ::Gitlab::SidekiqVersioning::Middleware
    chain.add ::Gitlab::SidekiqStatus::ServerMiddleware
    chain.add ::Gitlab::SidekiqMiddleware::WorkerContext::Server
    # TODO: pause control intercepts job into sorted set.
    # See https://gitlab.com/gitlab-org/gitlab/-/issues/508229
    chain.add ::Gitlab::SidekiqMiddleware::PauseControl::Server
    # TODO: migrate support middleware could raise lock error
    # See https://gitlab.com/gitlab-org/gitlab/-/issues/508229
    chain.add ::ClickHouse::MigrationSupport::SidekiqMiddleware
    # DuplicateJobs::Server should be placed at the bottom, but before the SidekiqServerMiddleware,
    # so we can compare the latest WAL location against replica
    # NOTE: Everything from DuplicateJobs::Client to DuplicateJobs::Server must yield
    # no returning or job interception as it will leave the duplicate job redis key
    # dangling and errorneously deduplicating future jobs until key expires.
    # Any middlewares after DuplicateJobs::Server can return/intercept jobs.
    chain.add ::Gitlab::SidekiqMiddleware::DuplicateJobs::Server
    chain.add ::Gitlab::SidekiqMiddleware::ConcurrencyLimit::Server
    chain.add ::Gitlab::Database::LoadBalancing::SidekiqServerMiddleware
    chain.add ::Gitlab::SidekiqMiddleware::ResourceUsageLimit::Server
    chain.add ::Gitlab::SidekiqMiddleware::SkipJobs if skip_jobs
    chain.add ::Gitlab::SidekiqMiddleware::Identity::Restore
  end
end