2018-10-22 03:00:50 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-06-16 12:42:41 -04:00
|
|
|
require 'base64'
|
|
|
|
|
2017-02-05 13:04:23 -05:00
|
|
|
require 'gitaly'
|
2018-01-21 18:34:09 -05:00
|
|
|
require 'grpc/health/v1/health_pb'
|
|
|
|
require 'grpc/health/v1/health_services_pb'
|
2017-02-05 13:04:23 -05:00
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module GitalyClient
|
2017-09-19 06:55:37 -04:00
|
|
|
class TooManyInvocationsError < StandardError
|
|
|
|
attr_reader :call_site, :invocation_count, :max_call_stack
|
|
|
|
|
|
|
|
def initialize(call_site, invocation_count, max_call_stack, most_invoked_stack)
|
|
|
|
@call_site = call_site
|
|
|
|
@invocation_count = invocation_count
|
|
|
|
@max_call_stack = max_call_stack
|
|
|
|
stacks = most_invoked_stack.join('\n') if most_invoked_stack
|
|
|
|
|
|
|
|
msg = "GitalyClient##{call_site} called #{invocation_count} times from single request. Potential n+1?"
|
2018-10-22 03:00:50 -04:00
|
|
|
msg = "#{msg}\nThe following call site called into Gitaly #{max_call_stack} times:\n#{stacks}\n" if stacks
|
2017-09-19 06:55:37 -04:00
|
|
|
|
|
|
|
super(msg)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-05-05 06:19:14 -04:00
|
|
|
PEM_REGEX = /\-+BEGIN CERTIFICATE\-+.+?\-+END CERTIFICATE\-+/m.freeze
|
2018-11-20 07:29:56 -05:00
|
|
|
SERVER_VERSION_FILE = 'GITALY_SERVER_VERSION'
|
2019-02-22 09:51:27 -05:00
|
|
|
MAXIMUM_GITALY_CALLS = 30
|
2019-12-22 04:07:51 -05:00
|
|
|
CLIENT_NAME = (Gitlab::Runtime.sidekiq? ? 'gitlab-sidekiq' : 'gitlab-web').freeze
|
2019-06-14 21:22:26 -04:00
|
|
|
GITALY_METADATA_FILENAME = '.gitaly-metadata'
|
2017-03-13 10:04:46 -04:00
|
|
|
|
2017-05-10 08:18:59 -04:00
|
|
|
MUTEX = Mutex.new
|
2017-03-30 10:26:30 -04:00
|
|
|
|
2017-05-10 08:18:59 -04:00
|
|
|
def self.stub(name, storage)
|
|
|
|
MUTEX.synchronize do
|
|
|
|
@stubs ||= {}
|
|
|
|
@stubs[storage] ||= {}
|
|
|
|
@stubs[storage][name] ||= begin
|
2018-01-21 18:34:09 -05:00
|
|
|
klass = stub_class(name)
|
|
|
|
addr = stub_address(storage)
|
2018-10-25 10:35:04 -04:00
|
|
|
creds = stub_creds(storage)
|
2020-03-10 05:08:10 -04:00
|
|
|
klass.new(addr, creds, interceptors: interceptors, channel_args: channel_args)
|
2017-03-30 10:26:30 -04:00
|
|
|
end
|
|
|
|
end
|
2017-03-22 13:23:40 -04:00
|
|
|
end
|
|
|
|
|
2019-01-07 05:40:54 -05:00
|
|
|
def self.interceptors
|
2019-02-18 15:57:22 -05:00
|
|
|
return [] unless Labkit::Tracing.enabled?
|
2019-01-07 05:40:54 -05:00
|
|
|
|
2019-08-28 12:29:17 -04:00
|
|
|
[Labkit::Tracing::GRPC::ClientInterceptor.instance]
|
2019-01-07 05:40:54 -05:00
|
|
|
end
|
|
|
|
private_class_method :interceptors
|
|
|
|
|
2020-03-10 05:08:10 -04:00
|
|
|
def self.channel_args
|
|
|
|
# These values match the go Gitaly client
|
|
|
|
# https://gitlab.com/gitlab-org/gitaly/-/blob/bf9f52bc/client/dial.go#L78
|
|
|
|
{
|
|
|
|
'grpc.keepalive_time_ms': 20000,
|
|
|
|
'grpc.keepalive_permit_without_calls': 1
|
|
|
|
}
|
|
|
|
end
|
|
|
|
private_class_method :channel_args
|
|
|
|
|
2018-12-19 12:07:33 -05:00
|
|
|
def self.stub_cert_paths
|
2018-12-11 06:41:03 -05:00
|
|
|
cert_paths = Dir["#{OpenSSL::X509::DEFAULT_CERT_DIR}/*"]
|
|
|
|
cert_paths << OpenSSL::X509::DEFAULT_CERT_FILE if File.exist? OpenSSL::X509::DEFAULT_CERT_FILE
|
2018-12-19 12:07:33 -05:00
|
|
|
cert_paths
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.stub_certs
|
|
|
|
return @certs if @certs
|
2018-12-11 06:41:03 -05:00
|
|
|
|
2018-12-19 12:07:33 -05:00
|
|
|
@certs = stub_cert_paths.flat_map do |cert_file|
|
2018-12-19 05:36:06 -05:00
|
|
|
File.read(cert_file).scan(PEM_REGEX).map do |cert|
|
2019-03-13 09:42:43 -04:00
|
|
|
OpenSSL::X509::Certificate.new(cert).to_pem
|
|
|
|
rescue OpenSSL::OpenSSLError => e
|
2019-12-16 07:07:43 -05:00
|
|
|
Gitlab::ErrorTracking.track_and_raise_for_dev_exception(e, cert_file: cert_file)
|
2019-03-13 09:42:43 -04:00
|
|
|
nil
|
2018-12-19 05:36:06 -05:00
|
|
|
end.compact
|
|
|
|
end.uniq.join("\n")
|
2018-11-19 06:57:48 -05:00
|
|
|
end
|
|
|
|
|
2018-10-25 10:35:04 -04:00
|
|
|
def self.stub_creds(storage)
|
|
|
|
if URI(address(storage)).scheme == 'tls'
|
2018-12-19 05:36:06 -05:00
|
|
|
GRPC::Core::ChannelCredentials.new stub_certs
|
2018-10-25 10:35:04 -04:00
|
|
|
else
|
|
|
|
:this_channel_is_insecure
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-01-21 18:34:09 -05:00
|
|
|
def self.stub_class(name)
|
|
|
|
if name == :health_check
|
|
|
|
Grpc::Health::V1::Health::Stub
|
|
|
|
else
|
2019-10-15 23:06:12 -04:00
|
|
|
Gitaly.const_get(name.to_s.camelcase.to_sym, false).const_get(:Stub, false)
|
2018-01-21 18:34:09 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.stub_address(storage)
|
2018-12-19 05:36:06 -05:00
|
|
|
address(storage).sub(%r{^tcp://|^tls://}, '')
|
2018-01-21 18:34:09 -05:00
|
|
|
end
|
|
|
|
|
2017-05-10 08:18:59 -04:00
|
|
|
def self.clear_stubs!
|
|
|
|
MUTEX.synchronize do
|
|
|
|
@stubs = nil
|
|
|
|
end
|
2017-02-05 13:04:23 -05:00
|
|
|
end
|
|
|
|
|
2018-03-27 05:48:11 -04:00
|
|
|
def self.random_storage
|
|
|
|
Gitlab.config.repositories.storages.keys.sample
|
|
|
|
end
|
|
|
|
|
2017-05-10 08:18:59 -04:00
|
|
|
def self.address(storage)
|
|
|
|
params = Gitlab.config.repositories.storages[storage]
|
|
|
|
raise "storage not found: #{storage.inspect}" if params.nil?
|
2017-03-30 10:26:30 -04:00
|
|
|
|
2017-05-10 08:18:59 -04:00
|
|
|
address = params['gitaly_address']
|
|
|
|
unless address.present?
|
|
|
|
raise "storage #{storage.inspect} is missing a gitaly_address"
|
|
|
|
end
|
2017-03-24 13:22:42 -04:00
|
|
|
|
2018-10-25 10:35:04 -04:00
|
|
|
unless URI(address).scheme.in?(%w(tcp unix tls))
|
|
|
|
raise "Unsupported Gitaly address: #{address.inspect} does not use URL scheme 'tcp' or 'unix' or 'tls'"
|
2017-03-30 10:26:30 -04:00
|
|
|
end
|
|
|
|
|
2017-05-10 08:18:59 -04:00
|
|
|
address
|
2017-02-05 13:04:23 -05:00
|
|
|
end
|
|
|
|
|
2017-11-22 05:19:42 -05:00
|
|
|
def self.address_metadata(storage)
|
2020-04-30 20:09:59 -04:00
|
|
|
Base64.strict_encode64(Gitlab::Json.dump(storage => connection_data(storage)))
|
2019-01-25 07:10:04 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.connection_data(storage)
|
|
|
|
{ 'address' => address(storage), 'token' => token(storage) }
|
2017-11-22 05:19:42 -05:00
|
|
|
end
|
|
|
|
|
2017-06-20 11:31:49 -04:00
|
|
|
# All Gitaly RPC call sites should use GitalyClient.call. This method
|
|
|
|
# makes sure that per-request authentication headers are set.
|
2017-10-16 05:21:42 -04:00
|
|
|
#
|
|
|
|
# This method optionally takes a block which receives the keyword
|
|
|
|
# arguments hash 'kwargs' that will be passed to gRPC. This allows the
|
|
|
|
# caller to modify or augment the keyword arguments. The block must
|
|
|
|
# return a hash.
|
|
|
|
#
|
|
|
|
# For example:
|
|
|
|
#
|
|
|
|
# GitalyClient.call(storage, service, rpc, request) do |kwargs|
|
|
|
|
# kwargs.merge(deadline: Time.now + 10)
|
|
|
|
# end
|
|
|
|
#
|
2020-03-05 16:08:13 -05:00
|
|
|
# The optional remote_storage keyword argument is used to enable
|
|
|
|
# inter-gitaly calls. Say you have an RPC that needs to pull data from
|
|
|
|
# one repository to another. For example, to fetch a branch from a
|
|
|
|
# (non-deduplicated) fork into the fork parent. In that case you would
|
|
|
|
# send an RPC call to the Gitaly server hosting the fork parent, and in
|
|
|
|
# the request, you would tell that Gitaly server to pull Git data from
|
|
|
|
# the fork. How does that Gitaly server connect to the Gitaly server the
|
|
|
|
# forked repo lives on? This is the problem `remote_storage:` solves: it
|
|
|
|
# adds address and authentication information to the call, as gRPC
|
|
|
|
# metadata (under the `gitaly-servers` header). The request would say
|
|
|
|
# "pull from repo X on gitaly-2". In the Ruby code you pass
|
|
|
|
# `remote_storage: 'gitaly-2'`. And then the metadata would say
|
|
|
|
# "gitaly-2 is at network address tcp://10.0.1.2:8075".
|
|
|
|
#
|
2019-10-25 17:06:20 -04:00
|
|
|
def self.call(storage, service, rpc, request, remote_storage: nil, timeout: default_timeout, &block)
|
2020-06-30 20:09:02 -04:00
|
|
|
Gitlab::GitalyClient::Call.new(storage, service, rpc, request, remote_storage, timeout).call(&block)
|
2019-10-25 17:06:20 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.execute(storage, service, rpc, request, remote_storage:, timeout:)
|
2017-09-19 06:55:37 -04:00
|
|
|
enforce_gitaly_request_limits(:call)
|
2020-01-13 10:07:53 -05:00
|
|
|
Gitlab::RequestContext.instance.ensure_deadline_not_exceeded!
|
2017-09-19 06:55:37 -04:00
|
|
|
|
2019-09-19 14:06:18 -04:00
|
|
|
kwargs = request_kwargs(storage, timeout: timeout.to_f, remote_storage: remote_storage)
|
2017-10-16 05:21:42 -04:00
|
|
|
kwargs = yield(kwargs) if block_given?
|
2017-11-22 05:19:42 -05:00
|
|
|
|
2017-10-16 05:21:42 -04:00
|
|
|
stub(service, storage).__send__(rpc, request, kwargs) # rubocop:disable GitlabSecurity/PublicSend
|
2019-10-25 17:06:20 -04:00
|
|
|
end
|
|
|
|
|
2019-03-27 06:56:36 -04:00
|
|
|
def self.query_time
|
2020-05-20 20:08:06 -04:00
|
|
|
query_time = Gitlab::SafeRequestStore[:gitaly_query_time] || 0
|
2020-04-27 23:09:53 -04:00
|
|
|
query_time.round(Gitlab::InstrumentationHelper::DURATION_PRECISION)
|
2019-03-27 06:56:36 -04:00
|
|
|
end
|
|
|
|
|
2020-05-20 20:08:06 -04:00
|
|
|
def self.add_query_time(duration)
|
|
|
|
return unless Gitlab::SafeRequestStore.active?
|
|
|
|
|
|
|
|
Gitlab::SafeRequestStore[:gitaly_query_time] ||= 0
|
|
|
|
Gitlab::SafeRequestStore[:gitaly_query_time] += duration
|
2019-03-27 06:56:36 -04:00
|
|
|
end
|
|
|
|
|
2018-08-29 15:53:44 -04:00
|
|
|
# For some time related tasks we can't rely on `Time.now` since it will be
|
|
|
|
# affected by Timecop in some tests, and the clock of some gitaly-related
|
|
|
|
# components (grpc's c-core and gitaly server) use system time instead of
|
|
|
|
# timecop's time, so tests will fail.
|
|
|
|
# `Time.at(Process.clock_gettime(Process::CLOCK_REALTIME))` will circumvent
|
|
|
|
# timecop.
|
|
|
|
def self.real_time
|
|
|
|
Time.at(Process.clock_gettime(Process::CLOCK_REALTIME))
|
|
|
|
end
|
|
|
|
private_class_method :real_time
|
|
|
|
|
|
|
|
def self.authorization_token(storage)
|
|
|
|
token = token(storage).to_s
|
|
|
|
issued_at = real_time.to_i.to_s
|
|
|
|
hmac = OpenSSL::HMAC.hexdigest(OpenSSL::Digest::SHA256.new, token, issued_at)
|
|
|
|
|
|
|
|
"v2.#{hmac}.#{issued_at}"
|
|
|
|
end
|
|
|
|
private_class_method :authorization_token
|
|
|
|
|
2019-09-19 14:06:18 -04:00
|
|
|
def self.request_kwargs(storage, timeout:, remote_storage: nil)
|
2017-10-06 13:16:20 -04:00
|
|
|
metadata = {
|
2018-08-29 15:53:44 -04:00
|
|
|
'authorization' => "Bearer #{authorization_token(storage)}",
|
2017-10-06 13:16:20 -04:00
|
|
|
'client_name' => CLIENT_NAME
|
|
|
|
}
|
|
|
|
|
|
|
|
feature_stack = Thread.current[:gitaly_feature_stack]
|
|
|
|
feature = feature_stack && feature_stack[0]
|
|
|
|
metadata['call_site'] = feature.to_s if feature
|
2017-11-22 05:19:42 -05:00
|
|
|
metadata['gitaly-servers'] = address_metadata(remote_storage) if remote_storage
|
2019-02-18 15:57:22 -05:00
|
|
|
metadata['x-gitlab-correlation-id'] = Labkit::Correlation::CorrelationId.current_id if Labkit::Correlation::CorrelationId.current_id
|
2019-07-09 10:36:54 -04:00
|
|
|
metadata['gitaly-session-id'] = session_id
|
2019-06-12 09:32:29 -04:00
|
|
|
metadata.merge!(Feature::Gitaly.server_feature_flags)
|
2018-05-16 04:20:14 -04:00
|
|
|
|
2020-01-13 10:07:53 -05:00
|
|
|
deadline_info = request_deadline(timeout)
|
|
|
|
metadata.merge!(deadline_info.slice(:deadline_type))
|
2017-11-29 04:12:12 -05:00
|
|
|
|
2020-01-13 10:07:53 -05:00
|
|
|
{ metadata: metadata, deadline: deadline_info[:deadline] }
|
2017-06-16 12:42:41 -04:00
|
|
|
end
|
|
|
|
|
2020-01-13 10:07:53 -05:00
|
|
|
def self.request_deadline(timeout)
|
|
|
|
# timeout being 0 means the request is allowed to run indefinitely.
|
|
|
|
# We can't allow that inside a request, but this won't count towards Gitaly
|
|
|
|
# error budgets
|
|
|
|
regular_deadline = real_time.to_i + timeout if timeout > 0
|
|
|
|
|
|
|
|
return { deadline: regular_deadline } if Sidekiq.server?
|
|
|
|
return { deadline: regular_deadline } unless Gitlab::RequestContext.instance.request_deadline
|
|
|
|
|
|
|
|
limited_deadline = [regular_deadline, Gitlab::RequestContext.instance.request_deadline].compact.min
|
|
|
|
limited = limited_deadline < regular_deadline
|
|
|
|
|
|
|
|
{ deadline: limited_deadline, deadline_type: limited ? "limited" : "regular" }
|
|
|
|
end
|
|
|
|
private_class_method :request_deadline
|
|
|
|
|
2019-04-29 03:21:36 -04:00
|
|
|
def self.session_id
|
|
|
|
Gitlab::SafeRequestStore[:gitaly_session_id] ||= SecureRandom.uuid
|
|
|
|
end
|
2018-05-16 04:20:14 -04:00
|
|
|
|
2017-06-16 12:42:41 -04:00
|
|
|
def self.token(storage)
|
|
|
|
params = Gitlab.config.repositories.storages[storage]
|
|
|
|
raise "storage not found: #{storage.inspect}" if params.nil?
|
|
|
|
|
|
|
|
params['gitaly_token'].presence || Gitlab.config.gitaly['token']
|
|
|
|
end
|
|
|
|
|
2017-09-19 06:55:37 -04:00
|
|
|
# Ensures that Gitaly is not being abuse through n+1 misuse etc
|
|
|
|
def self.enforce_gitaly_request_limits(call_site)
|
2019-08-09 00:33:20 -04:00
|
|
|
# Only count limits in request-response environments
|
2018-09-20 18:40:15 -04:00
|
|
|
return unless Gitlab::SafeRequestStore.active?
|
2017-09-19 06:55:37 -04:00
|
|
|
|
|
|
|
# This is this actual number of times this call was made. Used for information purposes only
|
|
|
|
actual_call_count = increment_call_count("gitaly_#{call_site}_actual")
|
|
|
|
|
2019-03-11 17:15:03 -04:00
|
|
|
return unless enforce_gitaly_request_limits?
|
2017-09-19 06:55:37 -04:00
|
|
|
|
|
|
|
# Check if this call is nested within a allow_n_plus_1_calls
|
|
|
|
# block and skip check if it is
|
|
|
|
return if get_call_count(:gitaly_call_count_exception_block_depth) > 0
|
|
|
|
|
|
|
|
# This is the count of calls outside of a `allow_n_plus_1_calls` block
|
|
|
|
# It is used for enforcement but not statistics
|
|
|
|
permitted_call_count = increment_call_count("gitaly_#{call_site}_permitted")
|
|
|
|
|
|
|
|
count_stack
|
|
|
|
|
|
|
|
return if permitted_call_count <= MAXIMUM_GITALY_CALLS
|
|
|
|
|
|
|
|
raise TooManyInvocationsError.new(call_site, actual_call_count, max_call_count, max_stacks)
|
|
|
|
end
|
|
|
|
|
2019-03-11 17:15:03 -04:00
|
|
|
def self.enforce_gitaly_request_limits?
|
|
|
|
# We typically don't want to enforce request limits in production
|
|
|
|
# However, we have some production-like test environments, i.e., ones
|
|
|
|
# where `Rails.env.production?` returns `true`. We do want to be able to
|
|
|
|
# check if the limit is being exceeded while testing in those environments
|
|
|
|
# In that case we can use a feature flag to indicate that we do want to
|
|
|
|
# enforce request limits.
|
2019-06-12 09:32:29 -04:00
|
|
|
return true if Feature::Gitaly.enabled?('enforce_requests_limits')
|
2019-03-11 17:15:03 -04:00
|
|
|
|
|
|
|
!(Rails.env.production? || ENV["GITALY_DISABLE_REQUEST_LIMITS"])
|
|
|
|
end
|
|
|
|
private_class_method :enforce_gitaly_request_limits?
|
|
|
|
|
2017-09-19 06:55:37 -04:00
|
|
|
def self.allow_n_plus_1_calls
|
2018-09-20 18:40:15 -04:00
|
|
|
return yield unless Gitlab::SafeRequestStore.active?
|
2017-09-19 06:55:37 -04:00
|
|
|
|
|
|
|
begin
|
|
|
|
increment_call_count(:gitaly_call_count_exception_block_depth)
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
decrement_call_count(:gitaly_call_count_exception_block_depth)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-17 02:23:11 -04:00
|
|
|
# Normally a FindCommit RPC will cache the commit with its SHA
|
|
|
|
# instead of a ref name, since it's possible the branch is mutated
|
|
|
|
# afterwards. However, for read-only requests that never mutate the
|
|
|
|
# branch, this method allows caching of the ref name directly.
|
|
|
|
def self.allow_ref_name_caching
|
|
|
|
return yield unless Gitlab::SafeRequestStore.active?
|
2019-03-27 19:15:26 -04:00
|
|
|
return yield if ref_name_caching_allowed?
|
2019-03-17 02:23:11 -04:00
|
|
|
|
|
|
|
begin
|
|
|
|
Gitlab::SafeRequestStore[:allow_ref_name_caching] = true
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
Gitlab::SafeRequestStore[:allow_ref_name_caching] = false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.ref_name_caching_allowed?
|
|
|
|
Gitlab::SafeRequestStore[:allow_ref_name_caching]
|
|
|
|
end
|
|
|
|
|
2017-09-19 06:55:37 -04:00
|
|
|
def self.get_call_count(key)
|
2018-09-20 18:40:15 -04:00
|
|
|
Gitlab::SafeRequestStore[key] || 0
|
2017-09-19 06:55:37 -04:00
|
|
|
end
|
|
|
|
private_class_method :get_call_count
|
|
|
|
|
|
|
|
def self.increment_call_count(key)
|
2018-09-20 18:40:15 -04:00
|
|
|
Gitlab::SafeRequestStore[key] ||= 0
|
|
|
|
Gitlab::SafeRequestStore[key] += 1
|
2017-09-19 06:55:37 -04:00
|
|
|
end
|
|
|
|
private_class_method :increment_call_count
|
|
|
|
|
|
|
|
def self.decrement_call_count(key)
|
2018-09-20 18:40:15 -04:00
|
|
|
Gitlab::SafeRequestStore[key] -= 1
|
2017-09-19 06:55:37 -04:00
|
|
|
end
|
|
|
|
private_class_method :decrement_call_count
|
|
|
|
|
2018-11-20 07:29:56 -05:00
|
|
|
# Returns the of the number of Gitaly calls made for this request
|
2017-09-19 06:55:37 -04:00
|
|
|
def self.get_request_count
|
2018-11-20 07:29:56 -05:00
|
|
|
get_call_count("gitaly_call_actual")
|
2017-09-19 06:55:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.reset_counts
|
2018-09-20 18:40:15 -04:00
|
|
|
return unless Gitlab::SafeRequestStore.active?
|
2017-09-19 06:55:37 -04:00
|
|
|
|
2018-11-20 07:29:56 -05:00
|
|
|
Gitlab::SafeRequestStore["gitaly_call_actual"] = 0
|
|
|
|
Gitlab::SafeRequestStore["gitaly_call_permitted"] = 0
|
2017-02-24 10:53:44 -05:00
|
|
|
end
|
2017-03-13 10:04:46 -04:00
|
|
|
|
2019-04-13 01:54:32 -04:00
|
|
|
def self.add_call_details(details)
|
2018-11-20 07:40:45 -05:00
|
|
|
Gitlab::SafeRequestStore['gitaly_call_details'] ||= []
|
|
|
|
Gitlab::SafeRequestStore['gitaly_call_details'] << details
|
2018-03-06 09:02:18 -05:00
|
|
|
end
|
|
|
|
|
2018-03-08 07:10:59 -05:00
|
|
|
def self.list_call_details
|
2019-08-27 07:40:44 -04:00
|
|
|
return [] unless Gitlab::PerformanceBar.enabled_for_request?
|
2018-03-06 09:02:18 -05:00
|
|
|
|
2018-11-20 07:40:45 -05:00
|
|
|
Gitlab::SafeRequestStore['gitaly_call_details'] || []
|
2018-03-06 09:02:18 -05:00
|
|
|
end
|
|
|
|
|
2017-03-13 10:04:46 -04:00
|
|
|
def self.expected_server_version
|
|
|
|
path = Rails.root.join(SERVER_VERSION_FILE)
|
|
|
|
path.read.chomp
|
|
|
|
end
|
2017-07-25 17:33:06 -04:00
|
|
|
|
2018-07-04 10:02:01 -04:00
|
|
|
def self.timestamp(time)
|
|
|
|
Google::Protobuf::Timestamp.new(seconds: time.to_i)
|
2017-09-11 04:17:49 -04:00
|
|
|
end
|
|
|
|
|
2017-11-29 04:12:12 -05:00
|
|
|
# The default timeout on all Gitaly calls
|
|
|
|
def self.default_timeout
|
|
|
|
timeout(:gitaly_timeout_default)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.fast_timeout
|
|
|
|
timeout(:gitaly_timeout_fast)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.medium_timeout
|
|
|
|
timeout(:gitaly_timeout_medium)
|
|
|
|
end
|
|
|
|
|
2019-09-19 14:06:18 -04:00
|
|
|
def self.long_timeout
|
2019-12-22 04:07:51 -05:00
|
|
|
if Gitlab::Runtime.web_server?
|
2019-10-02 11:06:12 -04:00
|
|
|
default_timeout
|
2019-11-01 05:06:47 -04:00
|
|
|
else
|
|
|
|
6.hours
|
2019-09-19 14:06:18 -04:00
|
|
|
end
|
2018-07-24 07:40:35 -04:00
|
|
|
end
|
|
|
|
|
2019-06-14 21:22:26 -04:00
|
|
|
def self.storage_metadata_file_path(storage)
|
|
|
|
Gitlab::GitalyClient::StorageSettings.allow_disk_access do
|
|
|
|
File.join(
|
|
|
|
Gitlab.config.repositories.storages[storage].legacy_disk_path, GITALY_METADATA_FILENAME
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.can_use_disk?(storage)
|
2019-07-10 17:36:49 -04:00
|
|
|
cached_value = MUTEX.synchronize do
|
|
|
|
@can_use_disk ||= {}
|
|
|
|
@can_use_disk[storage]
|
|
|
|
end
|
2019-06-14 21:22:26 -04:00
|
|
|
|
2019-07-19 13:01:09 -04:00
|
|
|
return cached_value unless cached_value.nil?
|
2019-06-14 21:22:26 -04:00
|
|
|
|
2019-07-10 17:36:49 -04:00
|
|
|
gitaly_filesystem_id = filesystem_id(storage)
|
|
|
|
direct_filesystem_id = filesystem_id_from_disk(storage)
|
2019-06-14 21:22:26 -04:00
|
|
|
|
2019-07-10 17:36:49 -04:00
|
|
|
MUTEX.synchronize do
|
|
|
|
@can_use_disk[storage] = gitaly_filesystem_id.present? &&
|
|
|
|
gitaly_filesystem_id == direct_filesystem_id
|
|
|
|
end
|
2019-06-14 21:22:26 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.filesystem_id(storage)
|
2020-01-24 13:09:00 -05:00
|
|
|
Gitlab::GitalyClient::ServerService.new(storage).storage_info&.filesystem_id
|
2019-06-14 21:22:26 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.filesystem_id_from_disk(storage)
|
|
|
|
metadata_file = File.read(storage_metadata_file_path(storage))
|
2020-04-30 20:09:59 -04:00
|
|
|
metadata_hash = Gitlab::Json.parse(metadata_file)
|
2019-06-14 21:22:26 -04:00
|
|
|
metadata_hash['gitaly_filesystem_id']
|
2019-07-30 09:20:27 -04:00
|
|
|
rescue Errno::ENOENT, Errno::EACCES, JSON::ParserError
|
2019-06-14 21:22:26 -04:00
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2020-01-24 13:09:00 -05:00
|
|
|
def self.filesystem_disk_available(storage)
|
|
|
|
Gitlab::GitalyClient::ServerService.new(storage).storage_disk_statistics&.available
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.filesystem_disk_used(storage)
|
|
|
|
Gitlab::GitalyClient::ServerService.new(storage).storage_disk_statistics&.used
|
|
|
|
end
|
|
|
|
|
2017-11-29 04:12:12 -05:00
|
|
|
def self.timeout(timeout_name)
|
|
|
|
Gitlab::CurrentSettings.current_application_settings[timeout_name]
|
|
|
|
end
|
|
|
|
private_class_method :timeout
|
|
|
|
|
2017-09-19 06:55:37 -04:00
|
|
|
# Count a stack. Used for n+1 detection
|
|
|
|
def self.count_stack
|
2018-09-20 18:40:15 -04:00
|
|
|
return unless Gitlab::SafeRequestStore.active?
|
2017-09-19 06:55:37 -04:00
|
|
|
|
2019-12-27 10:08:16 -05:00
|
|
|
stack_string = Gitlab::BacktraceCleaner.clean_backtrace(caller).drop(1).join("\n")
|
2017-09-19 06:55:37 -04:00
|
|
|
|
2020-09-02 11:10:54 -04:00
|
|
|
Gitlab::SafeRequestStore[:stack_counter] ||= {}
|
2017-09-19 06:55:37 -04:00
|
|
|
|
2018-09-20 18:40:15 -04:00
|
|
|
count = Gitlab::SafeRequestStore[:stack_counter][stack_string] || 0
|
|
|
|
Gitlab::SafeRequestStore[:stack_counter][stack_string] = count + 1
|
2017-09-19 06:55:37 -04:00
|
|
|
end
|
|
|
|
private_class_method :count_stack
|
|
|
|
|
|
|
|
# Returns a count for the stack which called Gitaly the most times. Used for n+1 detection
|
|
|
|
def self.max_call_count
|
2018-09-20 18:40:15 -04:00
|
|
|
return 0 unless Gitlab::SafeRequestStore.active?
|
2017-09-19 06:55:37 -04:00
|
|
|
|
2018-09-20 18:40:15 -04:00
|
|
|
stack_counter = Gitlab::SafeRequestStore[:stack_counter]
|
2017-09-19 06:55:37 -04:00
|
|
|
return 0 unless stack_counter
|
|
|
|
|
|
|
|
stack_counter.values.max
|
|
|
|
end
|
|
|
|
private_class_method :max_call_count
|
|
|
|
|
|
|
|
# Returns the stacks that calls Gitaly the most times. Used for n+1 detection
|
|
|
|
def self.max_stacks
|
2019-02-08 07:19:53 -05:00
|
|
|
return unless Gitlab::SafeRequestStore.active?
|
2017-09-19 06:55:37 -04:00
|
|
|
|
2018-09-20 18:40:15 -04:00
|
|
|
stack_counter = Gitlab::SafeRequestStore[:stack_counter]
|
2019-02-08 07:19:53 -05:00
|
|
|
return unless stack_counter
|
2017-09-19 06:55:37 -04:00
|
|
|
|
|
|
|
max = max_call_count
|
2020-08-05 11:09:59 -04:00
|
|
|
return if max == 0
|
2017-09-19 06:55:37 -04:00
|
|
|
|
|
|
|
stack_counter.select { |_, v| v == max }.keys
|
|
|
|
end
|
|
|
|
private_class_method :max_stacks
|
2017-02-05 13:04:23 -05:00
|
|
|
end
|
|
|
|
end
|