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::Cluster::Error)

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Node.



103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/redis_client/cluster/node.rb', line 103

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



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

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

#any_replica_node_key(seed: nil) ⇒ Object



193
194
195
# File 'lib/redis_client/cluster/node.rb', line 193

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

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



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

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

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



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

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



146
147
148
# File 'lib/redis_client/cluster/node.rb', line 146

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

#clientsObject



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

def clients
  @topology.clients.values
end

#clients_for_scanning(seed: nil) ⇒ Object



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

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



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

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

#find_by(node_key) ⇒ Object

Raises:



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

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



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

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



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

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



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

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

#node_keysObject



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

def node_keys
  @topology.clients.keys.sort
end

#primary_clientsObject



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

def primary_clients
  @topology.primary_clients.values
end

#reload!Object



208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/redis_client/cluster/node.rb', line 208

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



171
172
173
# File 'lib/redis_client/cluster/node.rb', line 171

def replica_clients
  @topology.replica_clients.values
end

#sampleObject



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

def sample
  @topology.clients.values.sample
end

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

Raises:



150
151
152
153
154
155
156
157
# File 'lib/redis_client/cluster/node.rb', line 150

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.with_errors(errors)
end

#update_slot(slot, node_key) ⇒ Object



197
198
199
200
201
202
203
204
205
206
# File 'lib/redis_client/cluster/node.rb', line 197

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