1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
# frozen_string_literal: true
module Gitlab
module GitalyClient
# This module is responsible for collecting feature flag actors in Gitaly Client. Unlike normal feature flags used
# in Gitlab development, feature flags passed to Gitaly are pre-evaluated at Rails side before being passed to
# Gitaly. As a result, we need to collect all possible actors for the evaluation before issue any RPC. At this
# layer, the only parameter we have is raw repository. We need to infer other actors from the repository. Adding
# extra SQL queries before any RPC are not good for the performance. We applied some quirky optimizations here to
# avoid issuing SQL queries. However, in some less common code paths, a couple of queries are expected.
module WithFeatureFlagActors
include Gitlab::Utils::StrongMemoize
attr_accessor :repository_actor
# gitaly_client_call performs Gitaly calls including collected feature flag actors. The actors are retrieved
# from repository actor and memoized. The service must set `self.repository_actor = a_repository` beforehand.
def gitaly_client_call(*args, **kargs)
unless repository_actor
Gitlab::ErrorTracking.track_and_raise_for_dev_exception(
Feature::InvalidFeatureFlagError.new("gitaly_client_call called without setting repository_actor")
)
end
GitalyClient.with_feature_flag_actors(
repository: repository_actor,
user: user_actor,
project: project_actor,
group: group_actor
) do
GitalyClient.call(*args, **kargs)
end
end
# gitaly_feature_flag_actors returns a hash of actors implied from input repository.
def gitaly_feature_flag_actors(repository)
container = find_repository_container(repository)
{
repository: repository,
user: Feature::Gitaly.user_actor,
project: Feature::Gitaly.project_actor(container),
group: Feature::Gitaly.group_actor(container)
}
end
# Use actor here means the user who originally perform the action. It is collected from ApplicationContext. As
# this information is widely propagated in all entry points, User actor should be available everywhere, even in
# background jobs.
def user_actor
strong_memoize(:user_actor) do
Feature::Gitaly.user_actor
end
end
# TODO: replace this project actor by Repo actor
def project_actor
strong_memoize(:project_actor) do
Feature::Gitaly.project_actor(repository_container)
end
end
def group_actor
strong_memoize(:group_actor) do
Feature::Gitaly.group_actor(repository_container)
end
end
private
def repository_container
strong_memoize(:repository_container) do
find_repository_container(repository_actor)
end
end
def find_repository_container(repository)
return if repository&.gl_repository.blank?
if repository.container.nil?
begin
identifier = Gitlab::GlRepository::Identifier.parse(repository.gl_repository)
identifier.container
rescue Gitlab::GlRepository::Identifier::InvalidIdentifier
nil
end
else
repository.container
end
end
end
end
end
Gitlab::GitalyClient::WithFeatureFlagActors.prepend_mod_with('Gitlab::GitalyClient::WithFeatureFlagActors')
|