Class: RedisClient::Cluster::Node

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/redis_client/cluster/node.rb,
lib/redis_client/cluster/node/primary_only.rb,
lib/redis_client/cluster/node/base_topology.rb,
lib/redis_client/cluster/node/random_replica.rb,
lib/redis_client/cluster/node/latency_replica.rb,
lib/redis_client/cluster/node/random_replica_or_primary.rb

Defined Under Namespace

Classes: BaseTopology, CharArray, Config, Info, LatencyReplica, PrimaryOnly, RandomReplica, RandomReplicaOrPrimary

Constant Summary collapse

STATE_REFRESH_INTERVAL =
(3..10).freeze
ReloadNeeded =
Class.new(::RedisClient::Error)

Instance Method Summary collapse

Constructor Details

#initialize(concurrent_worker, config:, pool: nil, **kwargs) ⇒ Node

Returns a new instance of Node.



98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/redis_client/cluster/node.rb', line 98

def initialize(concurrent_worker, config:, pool: nil, **kwargs)
  @concurrent_worker = concurrent_worker
  @slots = build_slot_node_mappings(EMPTY_ARRAY)
  @replications = build_replication_mappings(EMPTY_ARRAY)
  klass = make_topology_class(config.use_replica?, config.replica_affinity)
  @topology = klass.new(pool, @concurrent_worker, **kwargs)
  @config = config
  @mutex = Mutex.new
  @last_reloaded_at = nil
  @reload_times = 0
  @random = Random.new
end

Instance Method Details

#any_primary_node_key(seed: nil) ⇒ Object



184
185
186
# File 'lib/redis_client/cluster/node.rb', line 184

def any_primary_node_key(seed: nil)
  @topology.any_primary_node_key(seed: seed)
end

#any_replica_node_key(seed: nil) ⇒ Object



188
189
190
# File 'lib/redis_client/cluster/node.rb', line 188

def any_replica_node_key(seed: nil)
  @topology.any_replica_node_key(seed: seed)
end

#call_all(method, command, args, &block) ⇒ Object



133
134
135
# File 'lib/redis_client/cluster/node.rb', line 133

def call_all(method, command, args, &block)
  call_multiple_nodes!(@topology.clients, method, command, args, &block)
end

#call_primaries(method, command, args, &block) ⇒ Object



137
138
139
# File 'lib/redis_client/cluster/node.rb', line 137

def call_primaries(method, command, args, &block)
  call_multiple_nodes!(@topology.primary_clients, method, command, args, &block)
end

#call_replicas(method, command, args, &block) ⇒ Object



141
142
143
# File 'lib/redis_client/cluster/node.rb', line 141

def call_replicas(method, command, args, &block)
  call_multiple_nodes!(@topology.replica_clients, method, command, args, &block)
end

#clientsObject



158
159
160
# File 'lib/redis_client/cluster/node.rb', line 158

def clients
  @topology.clients.values
end

#clients_for_scanning(seed: nil) ⇒ Object



154
155
156
# File 'lib/redis_client/cluster/node.rb', line 154

def clients_for_scanning(seed: nil)
  @topology.clients_for_scanning(seed: seed).values.sort_by { |c| "#{c.config.host}-#{c.config.port}" }
end

#each(&block) ⇒ Object



115
116
117
# File 'lib/redis_client/cluster/node.rb', line 115

def each(&block)
  @topology.clients.each_value(&block)
end

#find_by(node_key) ⇒ Object

Raises:



127
128
129
130
131
# File 'lib/redis_client/cluster/node.rb', line 127

def find_by(node_key)
  raise ReloadNeeded if node_key.nil? || !@topology.clients.key?(node_key)

  @topology.clients.fetch(node_key)
end

#find_node_key_of_primary(slot) ⇒ Object



170
171
172
173
174
175
176
177
# File 'lib/redis_client/cluster/node.rb', line 170

def find_node_key_of_primary(slot)
  return if slot.nil?

  slot = Integer(slot)
  return if slot < MIN_SLOT || slot > MAX_SLOT

  @slots[slot]
end

#find_node_key_of_replica(slot, seed: nil) ⇒ Object



179
180
181
182
# File 'lib/redis_client/cluster/node.rb', line 179

def find_node_key_of_replica(slot, seed: nil)
  primary_node_key = find_node_key_of_primary(slot)
  @topology.find_node_key_of_replica(primary_node_key, seed: seed)
end

#inspectObject



111
112
113
# File 'lib/redis_client/cluster/node.rb', line 111

def inspect
  "#<#{self.class.name} #{node_keys.join(', ')}>"
end

#node_keysObject



123
124
125
# File 'lib/redis_client/cluster/node.rb', line 123

def node_keys
  @topology.clients.keys.sort
end

#primary_clientsObject



162
163
164
# File 'lib/redis_client/cluster/node.rb', line 162

def primary_clients
  @topology.primary_clients.values
end

#reload!Object



203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/redis_client/cluster/node.rb', line 203

def reload!
  with_reload_lock do
    with_startup_clients(@config.max_startup_sample) do |startup_clients|
      @node_info = refetch_node_info_list(startup_clients)
      @node_configs = @node_info.to_h do |node_info|
        [node_info.node_key, @config.client_config_for_node(node_info.node_key)]
      end
      @slots = build_slot_node_mappings(@node_info)
      @replications = build_replication_mappings(@node_info)
      @topology.process_topology_update!(@replications, @node_configs)
    end
  end
end

#replica_clientsObject



166
167
168
# File 'lib/redis_client/cluster/node.rb', line 166

def replica_clients
  @topology.replica_clients.values
end

#sampleObject



119
120
121
# File 'lib/redis_client/cluster/node.rb', line 119

def sample
  @topology.clients.values.sample
end

#send_ping(method, command, args, &block) ⇒ Object

Raises:



145
146
147
148
149
150
151
152
# File 'lib/redis_client/cluster/node.rb', line 145

def send_ping(method, command, args, &block)
  result_values, errors = call_multiple_nodes(@topology.clients, method, command, args, &block)
  return result_values if errors.nil? || errors.empty?

  raise ReloadNeeded if errors.values.any?(::RedisClient::ConnectionError)

  raise ::RedisClient::Cluster::ErrorCollection, errors
end

#update_slot(slot, node_key) ⇒ Object



192
193
194
195
196
197
198
199
200
201
# File 'lib/redis_client/cluster/node.rb', line 192

def update_slot(slot, node_key)
  return if @mutex.locked?

  @mutex.synchronize do
    @slots[slot] = node_key
  rescue RangeError
    @slots = Array.new(SLOT_SIZE) { |i| @slots[i] }
    @slots[slot] = node_key
  end
end