Class: Gitlab::ShardHealthCache

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/shard_health_cache.rb

Constant Summary collapse

HEALTHY_SHARDS_KEY =
'gitlab-healthy-shards'
HEALTHY_SHARDS_TIMEOUT =
300

Class Method Summary collapse

Class Method Details

.cached_healthy_shardsObject

Returns an array of strings of healthy shards



27
28
29
# File 'lib/gitlab/shard_health_cache.rb', line 27

def self.cached_healthy_shards
  with_redis { |redis| redis.smembers(HEALTHY_SHARDS_KEY) }
end

.clearObject

Clears the Redis set storing the list of healthy shards



9
10
11
# File 'lib/gitlab/shard_health_cache.rb', line 9

def self.clear
  with_redis { |redis| redis.del(HEALTHY_SHARDS_KEY) }
end

.healthy_shard?(shard_name) ⇒ Boolean

Checks whether the given shard name is in the list of healthy shards.

shard_name - The string to check

Returns:

  • (Boolean)


34
35
36
# File 'lib/gitlab/shard_health_cache.rb', line 34

def self.healthy_shard?(shard_name)
  with_redis { |redis| redis.sismember(HEALTHY_SHARDS_KEY, shard_name) }
end

.healthy_shard_countObject

Returns the number of healthy shards in the Redis set



39
40
41
# File 'lib/gitlab/shard_health_cache.rb', line 39

def self.healthy_shard_count
  with_redis { |redis| redis.scard(HEALTHY_SHARDS_KEY) }
end

.update(shards) ⇒ Object

Updates the list of healthy shards using a Redis set

shards - An array of shard names to store



16
17
18
19
20
21
22
23
24
# File 'lib/gitlab/shard_health_cache.rb', line 16

def self.update(shards)
  with_redis do |redis|
    redis.multi do |m|
      m.del(HEALTHY_SHARDS_KEY)
      m.sadd(HEALTHY_SHARDS_KEY, shards) unless shards.blank?
      m.expire(HEALTHY_SHARDS_KEY, HEALTHY_SHARDS_TIMEOUT)
    end
  end
end

.with_redis(&block) ⇒ Object



43
44
45
# File 'lib/gitlab/shard_health_cache.rb', line 43

def self.with_redis(&block)
  Gitlab::Redis::Cache.with(&block) # rubocop:disable CodeReuse/ActiveRecord
end