debian-mirror-gitlab/lib/gitlab/database/load_balancing/sidekiq_server_middleware.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

102 lines
3.2 KiB
Ruby
Raw Normal View History

2021-09-04 01:27:46 +05:30
# frozen_string_literal: true
module Gitlab
module Database
module LoadBalancing
class SidekiqServerMiddleware
2023-07-09 08:55:56 +05:30
include WalTrackingReceiver
2023-01-13 00:05:48 +05:30
JobReplicaNotUpToDate = Class.new(::Gitlab::SidekiqMiddleware::RetryError)
2021-09-04 01:27:46 +05:30
2023-06-20 00:43:36 +05:30
REPLICA_WAIT_SLEEP_SECONDS = 0.5
2022-01-26 12:08:38 +05:30
2021-09-04 01:27:46 +05:30
def call(worker, job, _queue)
2023-07-09 08:55:56 +05:30
# ActiveJobs have wrapped class stored in 'wrapped' key
resolved_class = job['wrapped']&.safe_constantize || worker.class
strategy = select_load_balancing_strategy(resolved_class, job)
2021-09-30 23:02:18 +05:30
job['load_balancing_strategy'] = strategy.to_s
if use_primary?(strategy)
2021-12-11 22:18:48 +05:30
::Gitlab::Database::LoadBalancing::Session.current.use_primary!
2021-09-30 23:02:18 +05:30
elsif strategy == :retry
2023-07-09 08:55:56 +05:30
raise JobReplicaNotUpToDate, "Sidekiq job #{resolved_class} JID-#{job['jid']} couldn't use the replica."\
2023-06-20 00:43:36 +05:30
" Replica was not up to date."
2021-09-30 23:02:18 +05:30
else
# this means we selected an up-to-date replica, but there is nothing to do in this case.
2021-09-04 01:27:46 +05:30
end
yield
ensure
clear
end
private
def clear
2021-12-11 22:18:48 +05:30
::Gitlab::Database::LoadBalancing.release_hosts
::Gitlab::Database::LoadBalancing::Session.clear_session
2021-09-04 01:27:46 +05:30
end
2021-09-30 23:02:18 +05:30
def use_primary?(strategy)
strategy.start_with?('primary')
end
2021-09-04 01:27:46 +05:30
2021-09-30 23:02:18 +05:30
def select_load_balancing_strategy(worker_class, job)
return :primary unless load_balancing_available?(worker_class)
2021-09-04 01:27:46 +05:30
2021-11-11 11:23:49 +05:30
wal_locations = get_wal_locations(job)
2022-01-26 12:08:38 +05:30
return :primary_no_wal if wal_locations.blank?
# Happy case: we can read from a replica.
return replica_strategy(worker_class, job) if databases_in_sync?(wal_locations)
2023-06-20 00:43:36 +05:30
3.times do
sleep REPLICA_WAIT_SLEEP_SECONDS
break if databases_in_sync?(wal_locations)
end
2021-09-04 01:27:46 +05:30
2021-11-18 22:05:49 +05:30
if databases_in_sync?(wal_locations)
2022-01-26 12:08:38 +05:30
replica_strategy(worker_class, job)
2021-09-30 23:02:18 +05:30
elsif can_retry?(worker_class, job)
# Optimistic case: The worker allows retries and we have retries left.
:retry
2021-09-04 01:27:46 +05:30
else
2021-09-30 23:02:18 +05:30
# Sad case: we need to fall back to the primary.
:primary
2021-09-04 01:27:46 +05:30
end
end
2022-01-26 12:08:38 +05:30
def get_wal_locations(job)
job['dedup_wal_locations'] || job['wal_locations']
2021-11-11 11:23:49 +05:30
end
2021-09-30 23:02:18 +05:30
def load_balancing_available?(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
def can_retry?(worker_class, job)
2023-06-20 00:43:36 +05:30
worker_class.get_data_consistency == :delayed && not_yet_requeued?(job)
2021-09-30 23:02:18 +05:30
end
2022-01-26 12:08:38 +05:30
def replica_strategy(worker_class, job)
retried_before?(worker_class, job) ? :replica_retried : :replica
end
2021-09-30 23:02:18 +05:30
def retried_before?(worker_class, job)
2023-06-20 00:43:36 +05:30
worker_class.get_data_consistency == :delayed && !not_yet_requeued?(job)
2021-09-30 23:02:18 +05:30
end
2023-06-20 00:43:36 +05:30
def not_yet_requeued?(job)
2021-09-04 01:27:46 +05:30
# if `retry_count` is `nil` it indicates that this job was never retried
# the `0` indicates that this is a first retry
job['retry_count'].nil?
end
end
end
end
end