|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +# Released under the MIT License. |
| 4 | +# Copyright, 2020, by David Ortiz. |
| 5 | +# Copyright, 2023-2024, by Samuel Williams. |
| 6 | + |
| 7 | +require_relative 'client' |
| 8 | +require 'io/stream' |
| 9 | + |
| 10 | +module Async |
| 11 | + module Redis |
| 12 | + class SentinelClient |
| 13 | + DEFAULT_MASTER_NAME = 'mymaster' |
| 14 | + |
| 15 | + include ::Protocol::Redis::Methods |
| 16 | + include Client::Methods |
| 17 | + |
| 18 | + # Create a new instance of the SentinelClient. |
| 19 | + # |
| 20 | + # @property endpoints [Array(Endpoint)] The list of sentinel endpoints. |
| 21 | + # @property master_name [String] The name of the master instance, defaults to 'mymaster'. |
| 22 | + # @property role [Symbol] The role of the instance that you want to connect to, either `:master` or `:slave`. |
| 23 | + # @property protocol [Protocol] The protocol to use when connecting to the actual Redis server, defaults to {Protocol::RESP2}. |
| 24 | + def initialize(endpoints, master_name: DEFAULT_MASTER_NAME, role: :master, protocol: Protocol::RESP2, **options) |
| 25 | + @endpoints = endpoints |
| 26 | + @master_name = master_name |
| 27 | + @role = role |
| 28 | + @protocol = protocol |
| 29 | + |
| 30 | + # A cache of sentinel connections. |
| 31 | + @sentinels = {} |
| 32 | + |
| 33 | + @pool = connect(**options) |
| 34 | + end |
| 35 | + |
| 36 | + # @attribute [String] The name of the master instance. |
| 37 | + attr :master_name |
| 38 | + |
| 39 | + # @attribute [Symbol] The role of the instance that you want to connect to. |
| 40 | + attr :role |
| 41 | + |
| 42 | + def resolve_address(role = @role) |
| 43 | + case role |
| 44 | + when :master |
| 45 | + resolve_master |
| 46 | + when :slave |
| 47 | + resolve_slave |
| 48 | + else |
| 49 | + raise ArgumentError, "Unknown instance role #{role}" |
| 50 | + end => address |
| 51 | + |
| 52 | + Console.debug(self, "Resolved #{@role} address: #{address}") |
| 53 | + |
| 54 | + address or raise RuntimeError, "Unable to fetch #{role} via Sentinel." |
| 55 | + end |
| 56 | + |
| 57 | + def close |
| 58 | + super |
| 59 | + |
| 60 | + @sentinels.each do |_, client| |
| 61 | + client.close |
| 62 | + end |
| 63 | + end |
| 64 | + |
| 65 | + def failover(name = @master_name) |
| 66 | + sentinels do |client| |
| 67 | + return client.call('SENTINEL', 'FAILOVER', name) |
| 68 | + end |
| 69 | + end |
| 70 | + |
| 71 | + def masters |
| 72 | + sentinels do |client| |
| 73 | + return client.call('SENTINEL', 'MASTERS').map{|fields| fields.each_slice(2).to_h} |
| 74 | + end |
| 75 | + end |
| 76 | + |
| 77 | + def master(name = @master_name) |
| 78 | + sentinels do |client| |
| 79 | + return client.call('SENTINEL', 'MASTER', name).each_slice(2).to_h |
| 80 | + end |
| 81 | + end |
| 82 | + |
| 83 | + def resolve_master |
| 84 | + sentinels do |client| |
| 85 | + begin |
| 86 | + address = client.call('SENTINEL', 'GET-MASTER-ADDR-BY-NAME', @master_name) |
| 87 | + rescue Errno::ECONNREFUSED |
| 88 | + next |
| 89 | + end |
| 90 | + |
| 91 | + return Endpoint.remote(address[0], address[1]) if address |
| 92 | + end |
| 93 | + |
| 94 | + return nil |
| 95 | + end |
| 96 | + |
| 97 | + def resolve_slave |
| 98 | + sentinels do |client| |
| 99 | + begin |
| 100 | + reply = client.call('SENTINEL', 'SLAVES', @master_name) |
| 101 | + rescue Errno::ECONNREFUSED |
| 102 | + next |
| 103 | + end |
| 104 | + |
| 105 | + slaves = available_slaves(reply) |
| 106 | + next if slaves.empty? |
| 107 | + |
| 108 | + slave = select_slave(slaves) |
| 109 | + return Endpoint.remote(slave['ip'], slave['port']) |
| 110 | + end |
| 111 | + |
| 112 | + return nil |
| 113 | + end |
| 114 | + |
| 115 | + protected |
| 116 | + |
| 117 | + # Override the parent method. The only difference is that this one needs to resolve the master/slave address. |
| 118 | + def connect(**options) |
| 119 | + Async::Pool::Controller.wrap(**options) do |
| 120 | + endpoint = resolve_address |
| 121 | + peer = endpoint.connect |
| 122 | + stream = ::IO::Stream(peer) |
| 123 | + |
| 124 | + @protocol.client(stream) |
| 125 | + end |
| 126 | + end |
| 127 | + |
| 128 | + def sentinels |
| 129 | + @endpoints.map do |endpoint| |
| 130 | + @sentinels[endpoint] ||= Client.new(endpoint) |
| 131 | + |
| 132 | + yield @sentinels[endpoint] |
| 133 | + end |
| 134 | + end |
| 135 | + |
| 136 | + def available_slaves(reply) |
| 137 | + # The reply is an array with the format: [field1, value1, field2, |
| 138 | + # value2, etc.]. |
| 139 | + # When a slave is marked as down by the sentinel, the "flags" field |
| 140 | + # (comma-separated array) contains the "s_down" value. |
| 141 | + slaves = reply.map{|fields| fields.each_slice(2).to_h} |
| 142 | + |
| 143 | + slaves.reject do |slave| |
| 144 | + slave['flags'].split(',').include?('s_down') |
| 145 | + end |
| 146 | + end |
| 147 | + |
| 148 | + def select_slave(available_slaves) |
| 149 | + available_slaves.sample |
| 150 | + end |
| 151 | + end |
| 152 | + end |
| 153 | +end |
0 commit comments