2020-07-14 09:09:02 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
#
|
|
|
|
# A Namespace::TraversalHierarchy is the collection of namespaces that descend
|
|
|
|
# from a root Namespace as defined by the Namespace#traversal_ids attributes.
|
|
|
|
#
|
|
|
|
# This class provides operations to be performed on the hierarchy itself,
|
|
|
|
# rather than individual namespaces.
|
|
|
|
#
|
|
|
|
# This includes methods for synchronizing traversal_ids attributes to a correct
|
|
|
|
# state. We use recursive methods to determine the correct state so we don't
|
|
|
|
# have to depend on the integrity of the traversal_ids attribute values
|
|
|
|
# themselves.
|
|
|
|
#
|
|
|
|
class Namespace
|
|
|
|
class TraversalHierarchy
|
|
|
|
attr_accessor :root
|
|
|
|
|
|
|
|
def self.for_namespace(namespace)
|
|
|
|
new(recursive_root_ancestor(namespace))
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(root)
|
2021-05-04 15:10:36 +00:00
|
|
|
raise StandardError, 'Must specify a root node' if root.parent_id
|
2020-07-14 09:09:02 +00:00
|
|
|
|
|
|
|
@root = root
|
|
|
|
end
|
|
|
|
|
|
|
|
# Update all traversal_ids in the current namespace hierarchy.
|
|
|
|
def sync_traversal_ids!
|
|
|
|
# An issue in Rails since 2013 prevents this kind of join based update in
|
|
|
|
# ActiveRecord. https://github.com/rails/rails/issues/13496
|
|
|
|
# Ideally it would be:
|
|
|
|
# `incorrect_traversal_ids.update_all('traversal_ids = cte.traversal_ids')`
|
2022-02-24 15:15:02 +00:00
|
|
|
sql = <<-SQL
|
|
|
|
UPDATE namespaces
|
|
|
|
SET traversal_ids = cte.traversal_ids
|
|
|
|
FROM (#{recursive_traversal_ids}) as cte
|
|
|
|
WHERE namespaces.id = cte.id
|
|
|
|
AND namespaces.traversal_ids::bigint[] <> cte.traversal_ids
|
|
|
|
SQL
|
|
|
|
|
2021-05-07 12:10:27 +00:00
|
|
|
Namespace.transaction do
|
2022-03-01 15:22:06 +00:00
|
|
|
@root.lock!("FOR NO KEY UPDATE")
|
2021-05-07 12:10:27 +00:00
|
|
|
Namespace.connection.exec_query(sql)
|
|
|
|
end
|
2021-03-19 21:08:58 +00:00
|
|
|
rescue ActiveRecord::Deadlocked
|
|
|
|
db_deadlock_counter.increment(source: 'Namespace#sync_traversal_ids!')
|
|
|
|
raise
|
2020-07-14 09:09:02 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Identify all incorrect traversal_ids in the current namespace hierarchy.
|
2021-05-07 12:10:27 +00:00
|
|
|
def incorrect_traversal_ids
|
2020-07-14 09:09:02 +00:00
|
|
|
Namespace
|
2021-05-07 12:10:27 +00:00
|
|
|
.joins("INNER JOIN (#{recursive_traversal_ids}) as cte ON namespaces.id = cte.id")
|
2021-08-03 03:08:59 +00:00
|
|
|
.where('namespaces.traversal_ids::bigint[] <> cte.traversal_ids')
|
2020-07-14 09:09:02 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
# Determine traversal_ids for the namespace hierarchy using recursive methods.
|
|
|
|
# Generate a collection of [id, traversal_ids] rows.
|
|
|
|
#
|
|
|
|
# Note that the traversal_ids represent a calculated traversal path for the
|
|
|
|
# namespace and not the value stored within the traversal_ids attribute.
|
2021-05-07 12:10:27 +00:00
|
|
|
def recursive_traversal_ids
|
2020-07-14 09:09:02 +00:00
|
|
|
root_id = Integer(@root.id)
|
|
|
|
|
2021-05-07 12:10:27 +00:00
|
|
|
<<~SQL
|
2020-07-14 09:09:02 +00:00
|
|
|
WITH RECURSIVE cte(id, traversal_ids, cycle) AS (
|
2021-08-03 03:08:59 +00:00
|
|
|
VALUES(#{root_id}::bigint, ARRAY[#{root_id}]::bigint[], false)
|
2020-07-14 09:09:02 +00:00
|
|
|
UNION ALL
|
2021-08-03 03:08:59 +00:00
|
|
|
SELECT n.id, cte.traversal_ids || n.id::bigint, n.id = ANY(cte.traversal_ids)
|
2020-07-14 09:09:02 +00:00
|
|
|
FROM namespaces n, cte
|
|
|
|
WHERE n.parent_id = cte.id AND NOT cycle
|
|
|
|
)
|
|
|
|
SELECT id, traversal_ids FROM cte
|
2021-03-19 21:08:58 +00:00
|
|
|
SQL
|
2020-07-14 09:09:02 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# This is essentially Namespace#root_ancestor which will soon be rewritten
|
|
|
|
# to use traversal_ids. We replicate here as a reliable way to find the
|
|
|
|
# root using recursive methods.
|
|
|
|
def self.recursive_root_ancestor(namespace)
|
|
|
|
Gitlab::ObjectHierarchy
|
|
|
|
.new(Namespace.where(id: namespace))
|
|
|
|
.base_and_ancestors
|
|
|
|
.reorder(nil)
|
|
|
|
.find_by(parent_id: nil)
|
|
|
|
end
|
2021-03-19 21:08:58 +00:00
|
|
|
|
|
|
|
def db_deadlock_counter
|
|
|
|
Gitlab::Metrics.counter(:db_deadlock, 'Counts the times we have deadlocked in the database')
|
|
|
|
end
|
2020-07-14 09:09:02 +00:00
|
|
|
end
|
|
|
|
end
|