From d1377e82d9bccd6b49c08aa67f6776b7981295ae Mon Sep 17 00:00:00 2001 From: GitLab Bot Date: Thu, 15 Jul 2021 21:09:09 +0000 Subject: Add latest changes from gitlab-org/gitlab@master --- app/workers/all_queues.yml | 2 +- .../user_refresh_from_replica_worker.rb | 35 ++++++++++++---------- 2 files changed, 21 insertions(+), 16 deletions(-) (limited to 'app/workers') diff --git a/app/workers/all_queues.yml b/app/workers/all_queues.yml index de1b17b588f..8d08beb56aa 100644 --- a/app/workers/all_queues.yml +++ b/app/workers/all_queues.yml @@ -37,7 +37,7 @@ :urgency: :low :resource_boundary: :unknown :weight: 1 - :idempotent: + :idempotent: true :tags: [] - :name: authorized_project_update:authorized_project_update_user_refresh_over_user_range :worker_name: AuthorizedProjectUpdate::UserRefreshOverUserRangeWorker diff --git a/app/workers/authorized_project_update/user_refresh_from_replica_worker.rb b/app/workers/authorized_project_update/user_refresh_from_replica_worker.rb index 7ce3f5453d2..10f7cb20df0 100644 --- a/app/workers/authorized_project_update/user_refresh_from_replica_worker.rb +++ b/app/workers/authorized_project_update/user_refresh_from_replica_worker.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true module AuthorizedProjectUpdate - class UserRefreshFromReplicaWorker # rubocop:disable Scalability/IdempotentWorker + class UserRefreshFromReplicaWorker include ApplicationWorker sidekiq_options retry: 3 @@ -9,31 +9,36 @@ module AuthorizedProjectUpdate urgency :low queue_namespace :authorized_project_update - # This job will not be deduplicated since it is marked with - # `data_consistency :delayed` and not `idempotent!` - # See https://gitlab.com/gitlab-org/gitlab/-/issues/325291 + idempotent! deduplicate :until_executing, including_scheduled: true - data_consistency :delayed - def perform(user_id) - user = User.find_by_id(user_id) - return unless user - if Feature.enabled?(:user_refresh_from_replica_worker_uses_replica_db) - enqueue_project_authorizations_refresh(user) if project_authorizations_needs_refresh?(user) + use_replica_if_available do + user = User.find_by_id(user_id) + + if user && project_authorizations_needs_refresh?(user) + enqueue_project_authorizations_refresh(user) + end + end else - use_primary_database + user = User.find_by_id(user_id) + return unless user + user.refresh_authorized_projects(source: self.class.name) end end private - def use_primary_database - if ::Gitlab::Database::LoadBalancing.enable? - ::Gitlab::Database::LoadBalancing::Session.current.use_primary! - end + # We use this approach instead of specifying `data_consistency :delayed` because these jobs + # are enqueued in large numbers, and using `data_consistency :delayed` + # does not allow us to deduplicate these jobs. + # https://gitlab.com/gitlab-org/gitlab/-/issues/325291 + def use_replica_if_available(&block) + return yield unless ::Gitlab::Database::LoadBalancing.enable? + + ::Gitlab::Database::LoadBalancing::Session.current.use_replicas_for_read_queries(&block) end def project_authorizations_needs_refresh?(user) -- cgit v1.2.1