debian-mirror-gitlab/lib/gitlab/shard_health_cache.rb

48 lines
1.4 KiB
Ruby
Raw Permalink Normal View History

2018-12-13 13:39:08 +05:30
# frozen_string_literal: true
2018-11-08 19:23:39 +05:30
module Gitlab
class ShardHealthCache
2019-12-04 20:38:33 +05:30
HEALTHY_SHARDS_KEY = 'gitlab-healthy-shards'
2018-11-08 19:23:39 +05:30
HEALTHY_SHARDS_TIMEOUT = 300
# Clears the Redis set storing the list of healthy shards
def self.clear
2023-01-13 00:05:48 +05:30
with_redis { |redis| redis.del(HEALTHY_SHARDS_KEY) }
2018-11-08 19:23:39 +05:30
end
# Updates the list of healthy shards using a Redis set
#
# shards - An array of shard names to store
def self.update(shards)
2023-01-13 00:05:48 +05:30
with_redis do |redis|
2018-11-08 19:23:39 +05:30
redis.multi do |m|
m.del(HEALTHY_SHARDS_KEY)
2023-01-13 00:05:48 +05:30
m.sadd(HEALTHY_SHARDS_KEY, shards) unless shards.blank?
2018-11-08 19:23:39 +05:30
m.expire(HEALTHY_SHARDS_KEY, HEALTHY_SHARDS_TIMEOUT)
end
end
end
# Returns an array of strings of healthy shards
def self.cached_healthy_shards
2023-01-13 00:05:48 +05:30
with_redis { |redis| redis.smembers(HEALTHY_SHARDS_KEY) }
2018-11-08 19:23:39 +05:30
end
# Checks whether the given shard name is in the list of healthy shards.
#
# shard_name - The string to check
def self.healthy_shard?(shard_name)
2023-01-13 00:05:48 +05:30
with_redis { |redis| redis.sismember(HEALTHY_SHARDS_KEY, shard_name) }
2018-11-08 19:23:39 +05:30
end
# Returns the number of healthy shards in the Redis set
def self.healthy_shard_count
2023-01-13 00:05:48 +05:30
with_redis { |redis| redis.scard(HEALTHY_SHARDS_KEY) }
end
def self.with_redis(&block)
Gitlab::Redis::Cache.with(&block) # rubocop:disable CodeReuse/ActiveRecord
2018-11-08 19:23:39 +05:30
end
end
end