Class: RedisClient::Cluster::Node::LatencyReplica

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

Constant Summary collapse

DUMMY_LATENCY_MSEC =
100 * 1000 * 1000
MEASURE_ATTEMPT_COUNT =
10

Constants included from ReplicaMixin

ReplicaMixin::EMPTY_ARRAY

Instance Attribute Summary collapse

Attributes included from ReplicaMixin

#clients, #primary_clients

Instance Method Summary collapse

Methods included from ReplicaMixin

#any_primary_node_key

Constructor Details

#initialize(replications, options, pool, concurrent_worker, **kwargs) ⇒ LatencyReplica

Returns a new instance of LatencyReplica.



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

def initialize(replications, options, pool, concurrent_worker, **kwargs)
  super

  all_replica_clients = @clients.select { |k, _| @replica_node_keys.include?(k) }
  latencies = measure_latencies(all_replica_clients, concurrent_worker)
  @replications.each_value { |keys| keys.sort_by! { |k| latencies.fetch(k) } }
  @replica_clients = select_replica_clients(@replications, @clients)
  @clients_for_scanning = select_clients_for_scanning(@replications, @clients)
  @existed_replicas = @replications.values.reject(&:empty?)
end

Instance Attribute Details

#replica_clientsObject (readonly)

Returns the value of attribute replica_clients.



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

def replica_clients
  @replica_clients
end

Instance Method Details

#any_replica_node_key(seed: nil) ⇒ Object



35
36
37
38
# File 'lib/redis_client/cluster/node/latency_replica.rb', line 35

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

#clients_for_scanning(seed: nil) ⇒ Object

rubocop:disable Lint/UnusedMethodArgument



27
28
29
# File 'lib/redis_client/cluster/node/latency_replica.rb', line 27

def clients_for_scanning(seed: nil) # rubocop:disable Lint/UnusedMethodArgument
  @clients_for_scanning
end

#find_node_key_of_replica(primary_node_key, seed: nil) ⇒ Object

rubocop:disable Lint/UnusedMethodArgument



31
32
33
# File 'lib/redis_client/cluster/node/latency_replica.rb', line 31

def find_node_key_of_replica(primary_node_key, seed: nil) # rubocop:disable Lint/UnusedMethodArgument
  @replications.fetch(primary_node_key, EMPTY_ARRAY).first || primary_node_key
end