2017-06-16 12:42:41 -04:00
|
|
|
require 'base64'
|
|
|
|
|
2017-02-05 13:04:23 -05:00
|
|
|
require 'gitaly'
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module GitalyClient
|
2017-05-31 17:06:01 -04:00
|
|
|
module MigrationStatus
|
|
|
|
DISABLED = 1
|
|
|
|
OPT_IN = 2
|
|
|
|
OPT_OUT = 3
|
|
|
|
end
|
|
|
|
|
2017-03-13 10:04:46 -04:00
|
|
|
SERVER_VERSION_FILE = 'GITALY_SERVER_VERSION'.freeze
|
|
|
|
|
2017-05-10 08:18:59 -04:00
|
|
|
MUTEX = Mutex.new
|
|
|
|
private_constant :MUTEX
|
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
|
|
|
|
klass = Gitaly.const_get(name.to_s.camelcase.to_sym).const_get(:Stub)
|
|
|
|
addr = address(storage)
|
|
|
|
addr = addr.sub(%r{^tcp://}, '') if URI(addr).scheme == 'tcp'
|
|
|
|
klass.new(addr, :this_channel_is_insecure)
|
2017-03-30 10:26:30 -04:00
|
|
|
end
|
|
|
|
end
|
2017-03-22 13:23:40 -04: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
|
|
|
|
|
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
|
|
|
|
2017-05-10 08:18:59 -04:00
|
|
|
unless URI(address).scheme.in?(%w(tcp unix))
|
|
|
|
raise "Unsupported Gitaly address: #{address.inspect} does not use URL scheme 'tcp' or 'unix'"
|
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-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-06-16 12:42:41 -04:00
|
|
|
def self.call(storage, service, rpc, request)
|
|
|
|
metadata = request_metadata(storage)
|
|
|
|
metadata = yield(metadata) if block_given?
|
|
|
|
stub(service, storage).send(rpc, request, metadata)
|
|
|
|
end
|
2017-07-21 03:36:31 -04:00
|
|
|
|
2017-06-16 12:42:41 -04:00
|
|
|
def self.request_metadata(storage)
|
|
|
|
encoded_token = Base64.strict_encode64(token(storage).to_s)
|
|
|
|
{ metadata: { 'authorization' => "Bearer #{encoded_token}" } }
|
|
|
|
end
|
|
|
|
|
|
|
|
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-05-31 17:06:01 -04:00
|
|
|
def self.feature_enabled?(feature, status: MigrationStatus::OPT_IN)
|
2017-07-06 08:45:29 -04:00
|
|
|
return false if status == MigrationStatus::DISABLED
|
2017-05-31 17:06:01 -04:00
|
|
|
|
|
|
|
feature = Feature.get("gitaly_#{feature}")
|
|
|
|
|
|
|
|
# If the feature hasn't been set, turn it on if it's opt-out
|
|
|
|
return status == MigrationStatus::OPT_OUT unless Feature.persisted?(feature)
|
|
|
|
|
|
|
|
if feature.percentage_of_time_value > 0
|
|
|
|
# Probabilistically enable this feature
|
|
|
|
return Random.rand() * 100 < feature.percentage_of_time_value
|
|
|
|
end
|
|
|
|
|
|
|
|
feature.enabled?
|
2017-02-24 10:53:44 -05:00
|
|
|
end
|
|
|
|
|
2017-07-19 11:40:25 -04:00
|
|
|
def self.migrate(feature, status: MigrationStatus::OPT_IN)
|
|
|
|
is_enabled = feature_enabled?(feature, status: status)
|
2017-02-24 10:53:44 -05:00
|
|
|
metric_name = feature.to_s
|
|
|
|
metric_name += "_gitaly" if is_enabled
|
|
|
|
|
|
|
|
Gitlab::Metrics.measure(metric_name) do
|
|
|
|
yield is_enabled
|
|
|
|
end
|
|
|
|
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
|
|
|
|
|
|
|
def self.encode(s)
|
|
|
|
s.dup.force_encoding(Encoding::ASCII_8BIT)
|
|
|
|
end
|
2017-02-05 13:04:23 -05:00
|
|
|
end
|
|
|
|
end
|