Class: RedisClient::Cluster::Node::RandomReplicaOrPrimary

Inherits:
Object
  • Object
show all
Includes:
ReplicaMixin
Defined in:
lib/redis_client/cluster/node/random_replica_or_primary.rb

Constant Summary

Constants included from ReplicaMixin

RedisClient::Cluster::Node::ReplicaMixin::EMPTY_ARRAY

Instance Attribute Summary

Attributes included from ReplicaMixin

#clients, #primary_clients

Instance Method Summary collapse

Methods included from ReplicaMixin

#any_primary_node_key, #initialize

Instance Method Details

#any_replica_node_key(seed: nil) ⇒ Object



36
37
38
39
# File 'lib/redis_client/cluster/node/random_replica_or_primary.rb', line 36

def any_replica_node_key(seed: nil)
  random = seed.nil? ? Random : Random.new(seed)
  @replica_node_keys.sample(random: random) || any_primary_node_key(seed: seed)
end

#clients_for_scanning(seed: nil) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/redis_client/cluster/node/random_replica_or_primary.rb', line 16

def clients_for_scanning(seed: nil)
  random = seed.nil? ? Random : Random.new(seed)
  keys = @replications.map do |primary_node_key, replica_node_keys|
    decide_use_primary?(random, replica_node_keys.size) ? primary_node_key : replica_node_keys.sample(random: random)
  end

  clients.select { |k, _| keys.include?(k) }
end

#find_node_key_of_replica(primary_node_key, seed: nil) ⇒ Object



25
26
27
28
29
30
31
32
33
34
# File 'lib/redis_client/cluster/node/random_replica_or_primary.rb', line 25

def find_node_key_of_replica(primary_node_key, seed: nil)
  random = seed.nil? ? Random : Random.new(seed)

  replica_node_keys = @replications.fetch(primary_node_key, EMPTY_ARRAY)
  if decide_use_primary?(random, replica_node_keys.size)
    primary_node_key
  else
    replica_node_keys.sample(random: random) || primary_node_key
  end
end

#replica_clientsObject



11
12
13
14
# File 'lib/redis_client/cluster/node/random_replica_or_primary.rb', line 11

def replica_clients
  keys = @replications.values.filter_map(&:sample)
  @clients.select { |k, _| keys.include?(k) }
end