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.



98
99
100
101
102
103
104
105
106
107
108
# 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
  @next_reload_time = nil
  @random = Random.new
end

Instance Method Details

#any_primary_node_key(seed: nil) ⇒ Object



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

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

#any_replica_node_key(seed: nil) ⇒ Object



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

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

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



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

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

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



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

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



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

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

#clientsObject



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

def clients
  @topology.clients.values
end

#clients_for_scanning(seed: nil) ⇒ Object



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

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



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

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

#find_by(node_key) ⇒ Object

Raises:



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

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



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

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



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

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



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

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

#node_keysObject



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

def node_keys
  @topology.clients.keys.sort
end

#primary_clientsObject



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

def primary_clients
  @topology.primary_clients.values
end

#replica_clientsObject



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

def replica_clients
  @topology.replica_clients.values
end

#sampleObject



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

def sample
  @topology.clients.values.sample
end

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

Raises:



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

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



202
203
204
205
206
207
208
209
210
# File 'lib/redis_client/cluster/node.rb', line 202

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



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

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