Commit 7daba09e authored by Douglas Barbosa Alexandre's avatar Douglas Barbosa Alexandre

Merge branch 'mk/fix-concurrent-verificationbatchworkers' into 'master'

Geo: Fix concurrent VerificationBatchWorkers

See merge request gitlab-org/gitlab!53194
parents 0f43c367 03ea02af
...@@ -86,6 +86,10 @@ module Gitlab ...@@ -86,6 +86,10 @@ module Gitlab
transition [:verification_started, :verification_succeeded, :verification_failed] => :verification_pending transition [:verification_started, :verification_succeeded, :verification_failed] => :verification_pending
end end
end end
private_class_method :start_verification_batch
private_class_method :start_verification_batch_query
private_class_method :start_verification_batch_subselect
end end
class_methods do class_methods do
...@@ -169,11 +173,25 @@ module Gitlab ...@@ -169,11 +173,25 @@ module Gitlab
UPDATE #{table_name} UPDATE #{table_name}
SET "verification_state" = #{started_enum_value}, SET "verification_state" = #{started_enum_value},
"verification_started_at" = NOW() "verification_started_at" = NOW()
WHERE #{self.verification_state_model_key} IN (#{relation.select(self.verification_state_model_key).to_sql}) WHERE #{self.verification_state_model_key} IN (#{start_verification_batch_subselect(relation).to_sql})
RETURNING #{self.verification_state_model_key} RETURNING #{self.verification_state_model_key}
SQL SQL
end end
# This query locks the rows during the transaction, and skips locked
# rows so that this query can be run concurrently, safely and reasonably
# efficiently.
# See https://gitlab.com/gitlab-org/gitlab/-/issues/300051#note_496889565
#
# @param [ActiveRecord::Relation] relation with appropriate where, order, and limit defined
# @return [String] SQL statement which selects the primary keys to update
def start_verification_batch_subselect(relation)
relation
.select(self.verification_state_model_key)
.lock('FOR UPDATE SKIP LOCKED') # rubocop:disable CodeReuse/ActiveRecord
end
# Overridden in ReplicableRegistry # Overridden in ReplicableRegistry
def verification_state_model_key def verification_state_model_key
self.primary_key self.primary_key
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment