debian-mirror-gitlab/lib/gitlab/git/storage/health.rb

91 lines
2.6 KiB
Ruby
Raw Normal View History

2017-09-10 17:25:29 +05:30
module Gitlab
module Git
module Storage
class Health
attr_reader :storage_name, :info
2018-03-17 18:26:18 +05:30
def self.prefix_for_storage(storage_name)
"#{Gitlab::Git::Storage::REDIS_KEY_PREFIX}#{storage_name}:"
2017-09-10 17:25:29 +05:30
end
def self.for_all_storages
storage_names = Gitlab.config.repositories.storages.keys
results_per_storage = nil
Gitlab::Git::Storage.redis.with do |redis|
keys_per_storage = all_keys_for_storages(storage_names, redis)
results_per_storage = load_for_keys(keys_per_storage, redis)
end
results_per_storage.map do |name, info|
info.each { |i| i[:failure_count] = i[:failure_count].value.to_i }
new(name, info)
end
end
2018-03-17 18:26:18 +05:30
private_class_method def self.all_keys_for_storages(storage_names, redis)
2017-09-10 17:25:29 +05:30
keys_per_storage = {}
2018-03-17 18:26:18 +05:30
all_keys = redis.zrange(Gitlab::Git::Storage::REDIS_KNOWN_KEYS, 0, -1)
2017-09-10 17:25:29 +05:30
2018-03-17 18:26:18 +05:30
storage_names.each do |storage_name|
prefix = prefix_for_storage(storage_name)
2017-09-10 17:25:29 +05:30
2018-03-17 18:26:18 +05:30
keys_per_storage[storage_name] = all_keys.select { |key| key.starts_with?(prefix) }
2017-09-10 17:25:29 +05:30
end
keys_per_storage
end
2018-03-17 18:26:18 +05:30
private_class_method def self.load_for_keys(keys_per_storage, redis)
2017-09-10 17:25:29 +05:30
info_for_keys = {}
redis.pipelined do
keys_per_storage.each do |storage_name, keys_future|
2018-03-17 18:26:18 +05:30
info_for_storage = keys_future.map do |key|
2017-09-10 17:25:29 +05:30
{ name: key, failure_count: redis.hget(key, :failure_count) }
end
info_for_keys[storage_name] = info_for_storage
end
end
info_for_keys
end
def self.for_failing_storages
for_all_storages.select(&:failing?)
end
def initialize(storage_name, info)
@storage_name = storage_name
@info = info
end
def failing_info
@failing_info ||= info.select { |info_for_host| info_for_host[:failure_count] > 0 }
end
def failing?
failing_info.any?
end
def failing_on_hosts
@failing_on_hosts ||= failing_info.map do |info_for_host|
info_for_host[:name].split(':').last
end
end
def failing_circuit_breakers
@failing_circuit_breakers ||= failing_on_hosts.map do |hostname|
2018-03-17 18:26:18 +05:30
CircuitBreaker.build(storage_name, hostname)
2017-09-10 17:25:29 +05:30
end
end
def total_failures
@total_failures ||= failing_info.sum { |info_for_host| info_for_host[:failure_count] }
end
end
end
end
end