2021-09-04 01:27:46 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module Database
|
|
|
|
module LoadBalancing
|
|
|
|
class SidekiqClientMiddleware
|
2021-11-11 11:23:49 +05:30
|
|
|
include Gitlab::Utils::StrongMemoize
|
2023-07-09 08:55:56 +05:30
|
|
|
include WalTrackingSender
|
2021-11-11 11:23:49 +05:30
|
|
|
|
2021-09-04 01:27:46 +05:30
|
|
|
def call(worker_class, job, _queue, _redis_pool)
|
2021-09-30 23:02:18 +05:30
|
|
|
# Mailers can't be constantized
|
2021-09-04 01:27:46 +05:30
|
|
|
worker_class = worker_class.to_s.safe_constantize
|
2023-07-09 08:55:56 +05:30
|
|
|
# ActiveJobs have wrapped class stored in 'wrapped' key
|
|
|
|
resolved_class = job['wrapped'].to_s.safe_constantize || worker_class
|
2021-09-04 01:27:46 +05:30
|
|
|
|
2023-07-09 08:55:56 +05:30
|
|
|
if load_balancing_enabled?(resolved_class)
|
|
|
|
job['worker_data_consistency'] = resolved_class.get_data_consistency
|
2021-11-11 11:23:49 +05:30
|
|
|
set_data_consistency_locations!(job) unless job['wal_locations']
|
2021-09-30 23:02:18 +05:30
|
|
|
else
|
|
|
|
job['worker_data_consistency'] = ::WorkerAttributes::DEFAULT_DATA_CONSISTENCY
|
|
|
|
end
|
2021-09-04 01:27:46 +05:30
|
|
|
|
|
|
|
yield
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2021-09-30 23:02:18 +05:30
|
|
|
def load_balancing_enabled?(worker_class)
|
|
|
|
worker_class &&
|
2023-07-09 08:55:56 +05:30
|
|
|
worker_class.include?(::WorkerAttributes) &&
|
2021-09-30 23:02:18 +05:30
|
|
|
worker_class.utilizes_load_balancing_capabilities? &&
|
|
|
|
worker_class.get_data_consistency_feature_flag_enabled?
|
|
|
|
end
|
2021-09-04 01:27:46 +05:30
|
|
|
|
2021-11-11 11:23:49 +05:30
|
|
|
def set_data_consistency_locations!(job)
|
2023-07-09 08:55:56 +05:30
|
|
|
job['wal_locations'] = wal_locations_by_db_name
|
2022-07-23 23:45:48 +05:30
|
|
|
job['wal_location_source'] = wal_location_source
|
|
|
|
end
|
|
|
|
|
|
|
|
def wal_location_source
|
|
|
|
if ::Gitlab::Database::LoadBalancing.primary_only? || uses_primary?
|
|
|
|
::Gitlab::Database::LoadBalancing::ROLE_PRIMARY
|
|
|
|
else
|
|
|
|
::Gitlab::Database::LoadBalancing::ROLE_REPLICA
|
|
|
|
end
|
2021-09-04 01:27:46 +05:30
|
|
|
end
|
|
|
|
|
2022-07-23 23:45:48 +05:30
|
|
|
def uses_primary?
|
|
|
|
::Gitlab::Database::LoadBalancing::Session.current.use_primary?
|
|
|
|
end
|
2021-09-04 01:27:46 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|