Class: RedisClient::Cluster::Router
- Inherits:
-
Object
- Object
- RedisClient::Cluster::Router
- Defined in:
- lib/redis_client/cluster/router.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
Instance Method Summary collapse
- #assign_asking_node(err_msg) ⇒ Object
- #assign_node(command, cmd_spec: nil) ⇒ Object
- #assign_node_and_send_command(method, command, args, retry_count: 3, cmd_spec: nil, &block) ⇒ Object
- #assign_redirection_node(err_msg) ⇒ Object
- #close ⇒ Object
- #command_exists?(name) ⇒ Boolean
- #find_node(node_key) ⇒ Object
- #find_node_key(command, seed: nil, cmd_spec: nil) ⇒ Object
- #find_node_key_by_key(key, seed: nil, primary: false) ⇒ Object
- #find_primary_node_by_slot(slot) ⇒ Object
- #find_primary_node_key(command) ⇒ Object
- #find_slot(command) ⇒ Object
- #find_slot_by_key(key) ⇒ Object
-
#handle_redirection(node, command, retry_count:) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity.
-
#initialize(config, concurrent_worker, pool: nil, **kwargs) ⇒ Router
constructor
A new instance of Router.
- #node_keys ⇒ Object
- #renew_cluster_state ⇒ Object
-
#scan(command, seed: nil) ⇒ Object
rubocop:disable Metrics/AbcSize.
- #scan_single_key(command, arity:, &block) ⇒ Object
-
#send_command(method, command, *args, &block) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity.
- #send_command_to_node(node, method, command, args, retry_count: 3, &block) ⇒ Object
Constructor Details
#initialize(config, concurrent_worker, pool: nil, **kwargs) ⇒ Router
Returns a new instance of Router.
112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/redis_client/cluster/router.rb', line 112 def initialize(config, concurrent_worker, pool: nil, **kwargs) @config = config @concurrent_worker = concurrent_worker @pool = pool @client_kwargs = kwargs @node = ::RedisClient::Cluster::Node.new(concurrent_worker, config: config, pool: pool, **kwargs) @node.try_reload! @command = ::RedisClient::Cluster::Command.load(@node.replica_clients.shuffle, slow_command_timeout: config.slow_command_timeout) @command_builder = @config.command_builder rescue ::RedisClient::Cluster::InitialSetupError => e e.with_config(config) raise end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
110 111 112 |
# File 'lib/redis_client/cluster/router.rb', line 110 def config @config end |
Instance Method Details
#assign_asking_node(err_msg) ⇒ Object
326 327 328 329 |
# File 'lib/redis_client/cluster/router.rb', line 326 def assign_asking_node(err_msg) _, _, node_key = err_msg.split handle_node_reload_error { @node.find_by(node_key) } end |
#assign_node(command, cmd_spec: nil) ⇒ Object
257 258 259 260 261 262 |
# File 'lib/redis_client/cluster/router.rb', line 257 def assign_node(command, cmd_spec: nil) handle_node_reload_error do node_key = find_node_key(command, cmd_spec: cmd_spec) @node.find_by(node_key) end end |
#assign_node_and_send_command(method, command, args, retry_count: 3, cmd_spec: nil, &block) ⇒ Object
163 164 165 166 |
# File 'lib/redis_client/cluster/router.rb', line 163 def assign_node_and_send_command(method, command, args, retry_count: 3, cmd_spec: nil, &block) node = assign_node(command, cmd_spec: cmd_spec) send_command_to_node(node, method, command, args, retry_count: retry_count, &block) end |
#assign_redirection_node(err_msg) ⇒ Object
319 320 321 322 323 324 |
# File 'lib/redis_client/cluster/router.rb', line 319 def assign_redirection_node(err_msg) _, slot, node_key = err_msg.split slot = slot.to_i @node.update_slot(slot, node_key) handle_node_reload_error { @node.find_by(node_key) } end |
#close ⇒ Object
341 342 343 |
# File 'lib/redis_client/cluster/router.rb', line 341 def close @node.each(&:close) end |
#command_exists?(name) ⇒ Boolean
315 316 317 |
# File 'lib/redis_client/cluster/router.rb', line 315 def command_exists?(name) @command.exists?(name) end |
#find_node(node_key) ⇒ Object
311 312 313 |
# File 'lib/redis_client/cluster/router.rb', line 311 def find_node(node_key) handle_node_reload_error { @node.find_by(node_key) } end |
#find_node_key(command, seed: nil, cmd_spec: nil) ⇒ Object
285 286 287 288 289 290 291 292 |
# File 'lib/redis_client/cluster/router.rb', line 285 def find_node_key(command, seed: nil, cmd_spec: nil) cmd_spec = @command.get_spec(command) if cmd_spec.nil? find_node_key_by_key( cmd_spec&.extract_first_key(command), seed: seed, primary: cmd_spec&.should_send_to_primary? ) end |
#find_node_key_by_key(key, seed: nil, primary: false) ⇒ Object
264 265 266 267 268 269 270 271 272 273 274 275 276 |
# File 'lib/redis_client/cluster/router.rb', line 264 def find_node_key_by_key(key, seed: nil, primary: false) if key && !key.empty? slot = ::RedisClient::Cluster::KeySlotConverter.convert(key) node_key = primary ? @node.find_node_key_of_primary(slot) : @node.find_node_key_of_replica(slot, seed: seed) if node_key.nil? renew_cluster_state raise ::RedisClient::Cluster::NodeMightBeDown.new.with_config(@config) end node_key else primary ? @node.any_primary_node_key(seed: seed) : @node.any_replica_node_key(seed: seed) end end |
#find_primary_node_by_slot(slot) ⇒ Object
278 279 280 281 282 283 |
# File 'lib/redis_client/cluster/router.rb', line 278 def find_primary_node_by_slot(slot) handle_node_reload_error do node_key = @node.find_node_key_of_primary(slot) @node.find_by(node_key) end end |
#find_primary_node_key(command) ⇒ Object
294 295 296 297 298 299 |
# File 'lib/redis_client/cluster/router.rb', line 294 def find_primary_node_key(command) key = @command.get_spec(command)&.extract_first_key(command) return unless key&.size&.> 0 find_node_key_by_key(key, primary: true) end |
#find_slot(command) ⇒ Object
301 302 303 |
# File 'lib/redis_client/cluster/router.rb', line 301 def find_slot(command) find_slot_by_key(@command.get_spec(command)&.extract_first_key(command)) end |
#find_slot_by_key(key) ⇒ Object
305 306 307 308 309 |
# File 'lib/redis_client/cluster/router.rb', line 305 def find_slot_by_key(key) return if key.nil? || key.empty? ::RedisClient::Cluster::KeySlotConverter.convert(key) end |
#handle_redirection(node, command, retry_count:) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/redis_client/cluster/router.rb', line 179 def handle_redirection(node, command, retry_count:) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity yield node rescue ::RedisClient::CircuitBreaker::OpenCircuitError raise rescue ::RedisClient::CommandError => e raise unless ::RedisClient::Cluster::ErrorIdentification.client_owns_error?(e, node) retry_count -= 1 if e..start_with?('MOVED') node = assign_redirection_node(e.) retry if retry_count >= 0 elsif e..start_with?('ASK') node = assign_asking_node(e.) if retry_count >= 0 node.call('asking') retry end elsif e..start_with?('CLUSTERDOWN') renew_cluster_state retry if retry_count >= 0 end raise rescue ::RedisClient::ConnectionError => e raise unless ::RedisClient::Cluster::ErrorIdentification.client_owns_error?(e, node) renew_cluster_state raise if command.nil? || command.empty? retry_count -= 1 raise if retry_count < 0 # Find the node to use for this command - if this fails for some reason, though, re-use # the old node. begin node = find_node(find_node_key(command)) rescue StandardError raise e end retry end |
#node_keys ⇒ Object
331 332 333 |
# File 'lib/redis_client/cluster/router.rb', line 331 def node_keys @node.node_keys end |
#renew_cluster_state ⇒ Object
335 336 337 338 339 |
# File 'lib/redis_client/cluster/router.rb', line 335 def renew_cluster_state @node.try_reload! rescue ::RedisClient::Cluster::InitialSetupError # ignore end |
#scan(command, seed: nil) ⇒ Object
rubocop:disable Metrics/AbcSize
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 |
# File 'lib/redis_client/cluster/router.rb', line 222 def scan(command, seed: nil) # rubocop:disable Metrics/AbcSize command[1] = ZERO_CURSOR_FOR_SCAN if command.size == 1 input_cursor = Integer(command[1]) client_index = input_cursor % 256 raw_cursor = input_cursor >> 8 clients = @node.clients_for_scanning(seed: seed) client = clients[client_index] return [ZERO_CURSOR_FOR_SCAN, []] unless client command[1] = raw_cursor.to_s result_cursor, result_keys = client.call_v(command) result_cursor = Integer(result_cursor) client_index += 1 if result_cursor == 0 [((result_cursor << 8) + client_index).to_s, result_keys] rescue ::RedisClient::ConnectionError renew_cluster_state raise end |
#scan_single_key(command, arity:, &block) ⇒ Object
247 248 249 250 251 252 253 254 255 |
# File 'lib/redis_client/cluster/router.rb', line 247 def scan_single_key(command, arity:, &block) node = assign_node(command) loop do cursor, values = handle_redirection(node, nil, retry_count: 3) { |n| n.call_v(command) } command[2] = cursor arity < 2 ? values.each(&block) : values.each_slice(arity, &block) break if cursor == ZERO_CURSOR_FOR_SCAN end end |
#send_command(method, command, *args, &block) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/redis_client/cluster/router.rb', line 126 def send_command(method, command, *args, &block) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity action = DEDICATED_ACTIONS[command.first] if action.nil? cmd_spec = @command.get_spec(command) action = find_policy_action(cmd_spec) return assign_node_and_send_command(method, command, args, cmd_spec: cmd_spec, &block) if action.nil? end return send(action.method_name, method, command, args, &block) if action.reply_transformer.nil? reply = send(action.method_name, method, command, args) action.reply_transformer.call(reply).then(&TSF.call(block)) rescue ::RedisClient::CircuitBreaker::OpenCircuitError raise rescue ::RedisClient::Cluster::Node::ReloadNeeded renew_cluster_state raise ::RedisClient::Cluster::NodeMightBeDown.new.with_config(@config) rescue ::RedisClient::ConnectionError renew_cluster_state raise rescue ::RedisClient::CommandError => e renew_cluster_state if e..start_with?('CLUSTERDOWN') raise rescue ::RedisClient::Cluster::ErrorCollection => e e.with_config(@config) raise if e.errors.any?(::RedisClient::CircuitBreaker::OpenCircuitError) renew_cluster_state if e.errors.values.any? do |err| next false if ::RedisClient::Cluster::ErrorIdentification.identifiable?(err) && @node.none? { |c| ::RedisClient::Cluster::ErrorIdentification.client_owns_error?(err, c) } err..start_with?('CLUSTERDOWN') || err.is_a?(::RedisClient::ConnectionError) end raise end |
#send_command_to_node(node, method, command, args, retry_count: 3, &block) ⇒ Object
168 169 170 171 172 173 174 175 176 177 |
# File 'lib/redis_client/cluster/router.rb', line 168 def send_command_to_node(node, method, command, args, retry_count: 3, &block) handle_redirection(node, command, retry_count: retry_count) do |on_node| if args.empty? # prevent memory allocation for variable-length args on_node.public_send(method, command, &block) else on_node.public_send(method, *args, command, &block) end end end |