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))
USE_CHAR_ARRAY_SLOT =

less memory consumption, but slow

Integer(ENV.fetch('REDIS_CLIENT_USE_CHAR_ARRAY_SLOT', 1)) == 1
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.



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

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



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
136
# File 'lib/redis_client/cluster/node.rb', line 92

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



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

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

#any_replica_node_key(seed: nil) ⇒ Object



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

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

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



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

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

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



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

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



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

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

#clients_for_scanning(seed: nil) ⇒ Object



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

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



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

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

#find_by(node_key) ⇒ Object

Raises:



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

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



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

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



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

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



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

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

#node_keysObject



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

def node_keys
  @topology.clients.keys.sort
end

#sampleObject



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

def sample
  @topology.clients.values.sample
end

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

Raises:



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

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



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

def shuffled_nodes
  @topology.clients.values.shuffle
end

#update_slot(slot, node_key) ⇒ Object



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

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