File: random_replica_or_primary.rb

package info (click to toggle)
ruby-redis-cluster-client 0.13.6-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 224 kB
  • sloc: ruby: 2,498; makefile: 4
file content (53 lines) | stat: -rw-r--r-- 1,887 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# frozen_string_literal: true

require 'redis_client/cluster/node/base_topology'

class RedisClient
  class Cluster
    class Node
      class RandomReplicaOrPrimary < BaseTopology
        def replica_clients
          keys = @replications.values.filter_map(&:sample)
          @clients.select { |k, _| keys.include?(k) }
        end

        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

        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

        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

        private

        # Randomly equally likely choose node to read between primary and all replicas
        # e.g. 1 primary + 1 replica = 50% probability to read from primary
        # e.g. 1 primary + 2 replica = 33% probability to read from primary
        # e.g. 1 primary + 0 replica = 100% probability to read from primary
        def decide_use_primary?(random, replica_nodes)
          primary_nodes = 1.0
          total = primary_nodes + replica_nodes
          random.rand < primary_nodes / total
        end
      end
    end
  end
end