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/replica_mixin.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

Modules: ReplicaMixin Classes: CharArray, Config, Info, LatencyReplica, PrimaryOnly, RandomReplica, RandomReplicaOrPrimary

Constant Summary collapse

SLOT_SIZE =
16_384
MIN_SLOT =
0
MAX_SLOT =
SLOT_SIZE - 1
MAX_STARTUP_SAMPLE =
Integer(ENV.fetch('REDIS_CLIENT_MAX_STARTUP_SAMPLE', 3))
IGNORE_GENERIC_CONFIG_KEYS =
i[url host port path].freeze
DEAD_FLAGS =
%w[fail? fail handshake noaddr noflags].freeze
ROLE_FLAGS =
%w[master slave].freeze
EMPTY_ARRAY =
[].freeze
ReloadNeeded =
Class.new(::RedisClient::Error)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options, concurrent_worker, node_info_list: [], with_replica: false, replica_affinity: :random, pool: nil, **kwargs) ⇒ Node

Returns a new instance of Node.



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/redis_client/cluster/node.rb', line 185

def initialize(
  options,
  concurrent_worker,
  node_info_list: [],
  with_replica: false,
  replica_affinity: :random,
  pool: nil,
  **kwargs
)

  @concurrent_worker = concurrent_worker
  @slots = build_slot_node_mappings(node_info_list)
  @replications = build_replication_mappings(node_info_list)
  klass = make_topology_class(with_replica, replica_affinity)
  @topology = klass.new(@replications, options, pool, @concurrent_worker, **kwargs)
  @mutex = Mutex.new
end

Class Method Details

.load_info(options, concurrent_worker, **kwargs) ⇒ Object

rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/redis_client/cluster/node.rb', line 91

def load_info(options, concurrent_worker, **kwargs) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
  raise ::RedisClient::Cluster::InitialSetupError, [] if options.nil? || options.empty?

  startup_size = options.size > MAX_STARTUP_SAMPLE ? MAX_STARTUP_SAMPLE : options.size
  startup_options = options.to_a.sample(startup_size).to_h
  startup_nodes = ::RedisClient::Cluster::Node.new(startup_options, concurrent_worker, **kwargs)
  work_group = concurrent_worker.new_group(size: startup_size)

  startup_nodes.each_with_index do |raw_client, i|
    work_group.push(i, raw_client) do |client|
      reply = client.call('CLUSTER', 'NODES')
      parse_cluster_node_reply(reply)
    rescue StandardError => e
      e
    ensure
      client&.close
    end
  end

  node_info_list = errors = nil

  work_group.each do |i, v|
    case v
    when StandardError
      errors ||= Array.new(startup_size)
      errors[i] = v
    else
      node_info_list ||= Array.new(startup_size)
      node_info_list[i] = v
    end
  end

  work_group.close

  raise ::RedisClient::Cluster::InitialSetupError, errors if node_info_list.nil?

  grouped = node_info_list.compact.group_by do |info_list|
    info_list.sort_by!(&:id)
    info_list.each_with_object(String.new(capacity: 128 * info_list.size)) do |e, a|
      a << e.id << e.node_key << e.role << e.primary_id << e.config_epoch
    end
  end

  grouped.max_by { |_, v| v.size }[1].first.freeze
end

Instance Method Details

#any_primary_node_key(seed: nil) ⇒ Object



268
269
270
# File 'lib/redis_client/cluster/node.rb', line 268

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

#any_replica_node_key(seed: nil) ⇒ Object



272
273
274
# File 'lib/redis_client/cluster/node.rb', line 272

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

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



229
230
231
# File 'lib/redis_client/cluster/node.rb', line 229

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

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



233
234
235
# File 'lib/redis_client/cluster/node.rb', line 233

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



237
238
239
# File 'lib/redis_client/cluster/node.rb', line 237

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

#clients_for_scanning(seed: nil) ⇒ Object



250
251
252
# File 'lib/redis_client/cluster/node.rb', line 250

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



207
208
209
# File 'lib/redis_client/cluster/node.rb', line 207

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

#find_by(node_key) ⇒ Object

Raises:



223
224
225
226
227
# File 'lib/redis_client/cluster/node.rb', line 223

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



254
255
256
257
258
259
260
261
# File 'lib/redis_client/cluster/node.rb', line 254

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



263
264
265
266
# File 'lib/redis_client/cluster/node.rb', line 263

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



203
204
205
# File 'lib/redis_client/cluster/node.rb', line 203

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

#node_keysObject



219
220
221
# File 'lib/redis_client/cluster/node.rb', line 219

def node_keys
  @topology.clients.keys.sort
end

#sampleObject



215
216
217
# File 'lib/redis_client/cluster/node.rb', line 215

def sample
  @topology.clients.values.sample
end

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

Raises:



241
242
243
244
245
246
247
248
# File 'lib/redis_client/cluster/node.rb', line 241

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

#shuffled_nodesObject



211
212
213
# File 'lib/redis_client/cluster/node.rb', line 211

def shuffled_nodes
  @topology.clients.values.shuffle
end

#update_slot(slot, node_key) ⇒ Object



276
277
278
279
280
281
282
283
284
285
# File 'lib/redis_client/cluster/node.rb', line 276

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