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

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
# 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
  @next_reload_time = nil
  @random = Random.new
end

Instance Method Details

#any_primary_node_key(seed: nil) ⇒ Object



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

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

#any_replica_node_key(seed: nil) ⇒ Object



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

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

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



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

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

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



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

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



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

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

#clientsObject



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

def clients
  @topology.clients.values
end

#clients_for_scanning(seed: nil) ⇒ Object



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

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



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

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

#find_by(node_key) ⇒ Object

Raises:



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

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



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

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



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

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



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

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

#node_keysObject



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

def node_keys
  @topology.clients.keys.sort
end

#primary_clientsObject



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

def primary_clients
  @topology.primary_clients.values
end

#replica_clientsObject



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

def replica_clients
  @topology.replica_clients.values
end

#sampleObject



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

def sample
  @topology.clients.values.sample
end

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

Raises:



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

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

#try_reload!Object



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

def try_reload!
  with_reload_lock do
    with_reload_jitter do
      with_startup_clients(@config.max_startup_sample) do |clients|
        reload!(clients)
      end
    end
  end
end

#update_slot(slot, node_key) ⇒ Object



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

def update_slot(slot, node_key)
  return unless @mutex.try_lock

  @slots[slot] = node_key
rescue RangeError
  @slots = Array.new(SLOT_SIZE) { |i| @slots[i] }
  @slots[slot] = node_key
ensure
  @mutex.unlock if @mutex.owned?
end