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
  Gitlab::Redis::Cache.with { |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
  Gitlab::Redis::Cache.with { |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)
  Gitlab::Redis::Cache.with { |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
  Gitlab::Redis::Cache.with { |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)
  Gitlab::Redis::Cache.with do |redis|
    redis.multi do |m|
      m.del(HEALTHY_SHARDS_KEY)
      shards.each { |shard_name| m.sadd(HEALTHY_SHARDS_KEY, shard_name) }
      m.expire(HEALTHY_SHARDS_KEY, HEALTHY_SHARDS_TIMEOUT)
    end
  end
end