redis-cluster-client 0.17.1 → 0.17.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5dbfbaf304e900620662da7b8b5e6af36dfda8dc62a27d3e9a1efb975f5bd07f
4
- data.tar.gz: 8080422513db9f7edcbad4d3eafc4fb0472d5a754ee2a0bc64eaacfda8e283e0
3
+ metadata.gz: fb60940c0dbbb1bbf6497debd026596a02223668a8c7fa39b48055f5461db379
4
+ data.tar.gz: ffbd5eaec4b8d29637d0a4d65053039e81a08477a28d1b9e201ddba355030eae
5
5
  SHA512:
6
- metadata.gz: 980a40227b929c64632ff903ad4334942489d3c2af13ef0ec09006be856ae7c06a2e96c1a4c8c9a976b38091f53aa2e87833a5d6d04c07a0426129da20924838
7
- data.tar.gz: b7915e5337c66a3a0423ebcb206e7be0b3794ce1ccac40a1a99e08b8f305a594c9594d6a670f3679d1df081e80527fde4a153ac6949c7ca832bbb149fc0dcfee
6
+ metadata.gz: 4d107b60e99745a2303b0c2f7fc702baa955f4f52a7873bd6bf11ef885dbbebdb461dd8c85f6ff4387e4460a511daed69f33a813d1dac851981dd34bb25a6128
7
+ data.tar.gz: bd9515f510ffe274f2e30c0fefb47729425a7a1d7a11c52f3b1ba1c331b5fd747c6a5398e553952bcf4a935f05919811e02e838ba76462b65a259e35d580ea94
@@ -155,7 +155,9 @@ class RedisClient
155
155
  result_values, errors = call_multiple_nodes(@topology.clients, method, command, args, &block)
156
156
  return result_values if errors.nil? || errors.empty?
157
157
 
158
- raise ReloadNeeded if errors.values.any?(::RedisClient::ConnectionError)
158
+ raise ReloadNeeded if errors.values.any? do |err|
159
+ err.is_a?(::RedisClient::ConnectionError) && !err.is_a?(::RedisClient::CheckoutTimeoutError)
160
+ end
159
161
 
160
162
  raise ::RedisClient::Cluster::ErrorCollection.with_errors(errors)
161
163
  end
@@ -34,6 +34,8 @@ class RedisClient
34
34
  @router.renew_cluster_state if e.message.start_with?('CLUSTERDOWN')
35
35
  raise
36
36
  end
37
+ rescue ::RedisClient::CheckoutTimeoutError
38
+ raise
37
39
  rescue ::RedisClient::ConnectionError
38
40
  @router.renew_cluster_state
39
41
  raise
@@ -52,6 +54,8 @@ class RedisClient
52
54
  times_block_executed += 1
53
55
  handle_asking_once(nd, &blk)
54
56
  end
57
+ rescue ::RedisClient::CheckoutTimeoutError
58
+ raise
55
59
  rescue ::RedisClient::ConnectionError
56
60
  # Deduct the number of retries that happened _inside_ router#handle_redirection from our remaining
57
61
  # _external_ retries. Always deduct at least one in case handle_redirection raises without trying the block.
@@ -205,7 +205,7 @@ class RedisClient
205
205
  cluster_state_errors ||= {}
206
206
  cluster_state_errors[node_key] = v
207
207
  when StandardError
208
- cluster_state_errors ||= {} if v.is_a?(::RedisClient::ConnectionError)
208
+ cluster_state_errors ||= {} if v.is_a?(::RedisClient::ConnectionError) && !v.is_a?(::RedisClient::CheckoutTimeoutError)
209
209
  errors ||= {}
210
210
  errors[node_key] = v
211
211
  else
@@ -277,10 +277,16 @@ class RedisClient
277
277
  try_redirection(node, pipeline, inner_index)
278
278
  elsif err.message.start_with?('ASK')
279
279
  node = @router.assign_asking_node(err.message)
280
- try_asking(node) ? try_redirection(node, pipeline, inner_index) : err
280
+ node.with do |client|
281
+ try_asking(client) ? try_redirection(client, pipeline, inner_index) : err
282
+ end
281
283
  else
282
284
  err
283
285
  end
286
+ rescue ::RedisClient::CheckoutTimeoutError
287
+ raise if @exception
288
+
289
+ err
284
290
  end
285
291
 
286
292
  def try_redirection(node, pipeline, inner_index)
@@ -305,6 +311,8 @@ class RedisClient
305
311
 
306
312
  def try_asking(node)
307
313
  node.call('asking') == 'OK'
314
+ rescue ::RedisClient::CheckoutTimeoutError
315
+ raise
308
316
  rescue StandardError
309
317
  false
310
318
  end
@@ -171,6 +171,8 @@ class RedisClient
171
171
 
172
172
  def handle_connection_error(node_key, ignore: false)
173
173
  yield
174
+ rescue ::RedisClient::CheckoutTimeoutError
175
+ raise
174
176
  rescue ::RedisClient::ConnectionError
175
177
  @state_dict[node_key]&.close
176
178
  @state_dict.delete(node_key)
@@ -186,6 +188,8 @@ class RedisClient
186
188
  @state_dict.clear
187
189
  @commands.each { |command| _call(command) }
188
190
  break
191
+ rescue ::RedisClient::CheckoutTimeoutError
192
+ raise
189
193
  rescue ::RedisClient::ConnectionError, ::RedisClient::Cluster::NodeMightBeDown
190
194
  attempt += 1
191
195
  raise if attempt >= 10
@@ -33,6 +33,7 @@ class RedisClient
33
33
  'keys' => RoutingAction.new(method_name: :send_command_to_replicas, reply_transformer: FLATTEN_STRINGS),
34
34
  'dbsize' => RoutingAction.new(method_name: :send_command_to_replicas, reply_transformer: SUM_NUM),
35
35
  'scan' => RoutingAction.new(method_name: :send_scan_command),
36
+ 'bless' => RoutingAction.new(method_name: :send_bless_command),
36
37
  'lastsave' => RoutingAction.new(method_name: :send_command_to_all_nodes, reply_transformer: SORT_NUMBERS),
37
38
  'role' => RoutingAction.new(method_name: :send_command_to_all_nodes),
38
39
  'config' => RoutingAction.new(method_name: :send_config_command),
@@ -39,17 +39,10 @@ class RedisClient
39
39
 
40
40
  def send_command(method, command, *args, &block) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
41
41
  action = @dedicated_actions[command.first]
42
- if action.nil?
43
- cmd_spec = @command.get_spec(command)
44
- action = find_policy_action(cmd_spec)
45
- return assign_node_and_send_command(method, command, args, cmd_spec: cmd_spec, &block) if action.nil?
46
- end
47
-
48
- return send(action.method_name, method, command, args, &block) if action.reply_transformer.nil?
42
+ return send_command_by_spec(method, command, args, &block) if action.nil?
49
43
 
50
- reply = send(action.method_name, method, command, args)
51
- action.reply_transformer.call(reply).then(&TSF.call(block))
52
- rescue ::RedisClient::CircuitBreaker::OpenCircuitError
44
+ send_command_by_action(action, method, command, args, &block)
45
+ rescue ::RedisClient::CircuitBreaker::OpenCircuitError, ::RedisClient::CheckoutTimeoutError
53
46
  raise
54
47
  rescue ::RedisClient::Cluster::Node::ReloadNeeded
55
48
  renew_cluster_state
@@ -65,6 +58,7 @@ class RedisClient
65
58
  raise if e.errors.any?(::RedisClient::CircuitBreaker::OpenCircuitError)
66
59
 
67
60
  renew_cluster_state if e.errors.values.any? do |err|
61
+ next false if err.is_a?(::RedisClient::CheckoutTimeoutError)
68
62
  next false if ::RedisClient::Cluster::ErrorIdentification.identifiable?(err) && @node.none? { |c| ::RedisClient::Cluster::ErrorIdentification.client_owns_error?(err, c) }
69
63
 
70
64
  err.message.start_with?('CLUSTERDOWN') || err.is_a?(::RedisClient::ConnectionError)
@@ -91,51 +85,61 @@ class RedisClient
91
85
  end
92
86
 
93
87
  def handle_redirection(node, command, retry_count:) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
94
- yield node
95
- rescue ::RedisClient::CircuitBreaker::OpenCircuitError
96
- raise
97
- rescue ::RedisClient::CommandError => e
98
- raise unless ::RedisClient::Cluster::ErrorIdentification.client_owns_error?(e, node)
99
-
100
- retry_count -= 1
101
- if e.message.start_with?('MOVED')
102
- node = assign_redirection_node(e.message)
103
- retry if retry_count >= 0
104
- elsif e.message.start_with?('ASK')
105
- node = assign_asking_node(e.message)
106
- if retry_count >= 0
107
- node.call('asking')
108
- retry
88
+ asking = false
89
+ begin
90
+ if asking
91
+ node.with do |client|
92
+ client.call('asking')
93
+ yield client
94
+ end
95
+ else
96
+ yield node
97
+ end
98
+ rescue ::RedisClient::CircuitBreaker::OpenCircuitError, ::RedisClient::CheckoutTimeoutError
99
+ raise
100
+ rescue ::RedisClient::CommandError => e
101
+ raise unless ::RedisClient::Cluster::ErrorIdentification.client_owns_error?(e, node)
102
+
103
+ retry_count -= 1
104
+ asking = false
105
+ if e.message.start_with?('MOVED')
106
+ node = assign_redirection_node(e.message)
107
+ retry if retry_count >= 0
108
+ elsif e.message.start_with?('ASK')
109
+ node = assign_asking_node(e.message)
110
+ asking = true
111
+ retry if retry_count >= 0
112
+ elsif e.message.start_with?('CLUSTERDOWN')
113
+ renew_cluster_state
114
+ retry if retry_count >= 0
109
115
  end
110
- elsif e.message.start_with?('CLUSTERDOWN')
111
- renew_cluster_state
112
- retry if retry_count >= 0
113
- end
114
116
 
115
- raise
116
- rescue ::RedisClient::ConnectionError => e
117
- raise unless ::RedisClient::Cluster::ErrorIdentification.client_owns_error?(e, node)
117
+ raise
118
+ rescue ::RedisClient::ConnectionError => e
119
+ raise unless ::RedisClient::Cluster::ErrorIdentification.client_owns_error?(e, node)
118
120
 
119
- renew_cluster_state
120
- raise if command.nil? || command.empty?
121
+ renew_cluster_state
122
+ raise if command.nil? || command.empty?
121
123
 
122
- retry_count -= 1
123
- raise if retry_count < 0
124
+ retry_count -= 1
125
+ raise if retry_count < 0
124
126
 
125
- # Find the node to use for this command - if this fails for some reason, though, re-use
126
- # the old node.
127
- begin
128
- node = find_node(find_node_key(command))
129
- rescue StandardError
130
- raise e
131
- end
127
+ # Find the node to use for this command - if this fails for some reason, though, re-use
128
+ # the old node.
129
+ begin
130
+ node = find_node(find_node_key(command))
131
+ rescue StandardError
132
+ raise e
133
+ end
132
134
 
133
- retry
135
+ asking = false
136
+ retry
137
+ end
134
138
  end
135
139
 
136
- def scan(command, seed: nil) # rubocop:disable Metrics/AbcSize
137
- command[1] = ZERO_CURSOR_FOR_SCAN if command.size == 1
138
- input_cursor = Integer(command[1])
140
+ def scan(command, seed: nil, cursor_index: 1) # rubocop:disable Metrics/AbcSize
141
+ command[cursor_index] = ZERO_CURSOR_FOR_SCAN if command.size == cursor_index
142
+ input_cursor = Integer(command[cursor_index])
139
143
 
140
144
  client_index = input_cursor % 256
141
145
  raw_cursor = input_cursor >> 8
@@ -145,7 +149,7 @@ class RedisClient
145
149
  client = clients[client_index]
146
150
  return [ZERO_CURSOR_FOR_SCAN, []] unless client
147
151
 
148
- command[1] = raw_cursor.to_s
152
+ command[cursor_index] = raw_cursor.to_s
149
153
 
150
154
  result_cursor, result_keys = client.call_v(command)
151
155
  result_cursor = Integer(result_cursor)
@@ -153,6 +157,8 @@ class RedisClient
153
157
  client_index += 1 if result_cursor == 0
154
158
 
155
159
  [((result_cursor << 8) + client_index).to_s, result_keys]
160
+ rescue ::RedisClient::CheckoutTimeoutError
161
+ raise
156
162
  rescue ::RedisClient::ConnectionError
157
163
  renew_cluster_state
158
164
  raise
@@ -258,6 +264,22 @@ class RedisClient
258
264
 
259
265
  private
260
266
 
267
+ # Follows the command tips which the server reports, or the routing by the key.
268
+ def send_command_by_spec(method, command, args, &block)
269
+ cmd_spec = @command.get_spec(command)
270
+ action = find_policy_action(cmd_spec)
271
+ return assign_node_and_send_command(method, command, args, cmd_spec: cmd_spec, &block) if action.nil?
272
+
273
+ send_command_by_action(action, method, command, args, &block)
274
+ end
275
+
276
+ def send_command_by_action(action, method, command, args, &block)
277
+ return send(action.method_name, method, command, args, &block) if action.reply_transformer.nil?
278
+
279
+ reply = send(action.method_name, method, command, args)
280
+ action.reply_transformer.call(reply).then(&TSF.call(block))
281
+ end
282
+
261
283
  def find_policy_action(cmd_spec)
262
284
  return if cmd_spec.nil?
263
285
 
@@ -327,13 +349,23 @@ class RedisClient
327
349
  end
328
350
  end
329
351
 
352
+ def send_bless_command(method, command, args, &block)
353
+ if command[1].casecmp('scan').zero?
354
+ scan(command, seed: 1, cursor_index: 2).then(&TSF.call(block))
355
+ else
356
+ # The other subcommands keep the default routing with the redirection handling.
357
+ send_command_by_spec(method, command, args, &block)
358
+ end
359
+ end
360
+
330
361
  def send_memory_command(method, command, args, &block)
331
362
  if command[1].casecmp('stats').zero?
332
363
  @node.call_all(method, command, args, &block)
333
364
  elsif command[1].casecmp('purge').zero?
334
365
  @node.call_all(method, command, args).first.then(&TSF.call(block))
335
366
  else
336
- assign_node(command).public_send(method, *args, command, &block)
367
+ # The other subcommands keep the default routing with the redirection handling.
368
+ send_command_by_spec(method, command, args, &block)
337
369
  end
338
370
  end
339
371
 
@@ -342,12 +374,15 @@ class RedisClient
342
374
  @node.call_all(method, command, args, &block).flatten
343
375
  elsif command[1].casecmp('pause').zero?
344
376
  @node.call_all(method, command, args).first.then(&TSF.call(block))
377
+ elsif command[1].casecmp('unpause').zero?
378
+ @node.call_all(method, command, args).first.then(&TSF.call(block))
345
379
  elsif command[1].casecmp('reply').zero?
346
380
  @node.call_all(method, command, args).first.then(&TSF.call(block))
347
381
  elsif command[1].casecmp('setname').zero?
348
382
  @node.call_all(method, command, args).first.then(&TSF.call(block))
349
383
  else
350
- assign_node(command).public_send(method, *args, command, &block)
384
+ # The other subcommands keep the default routing with the command tips.
385
+ send_command_by_spec(method, command, args, &block)
351
386
  end
352
387
  end
353
388
 
@@ -358,8 +393,15 @@ class RedisClient
358
393
  command[1].casecmp('slots').zero? ||
359
394
  command[1].casecmp('shards').zero? ||
360
395
  command[1].casecmp('count-failure-reports').zero? ||
361
- command[1].casecmp('slaves').zero?
396
+ command[1].casecmp('slaves').zero? ||
397
+ command[1].casecmp('replicas').zero? ||
398
+ command[1].casecmp('myid').zero? ||
399
+ command[1].casecmp('myshardid').zero? ||
400
+ command[1].casecmp('links').zero?
362
401
  assign_node(command).public_send(method, *args, command, &block)
402
+ elsif command[1].casecmp('slot-stats').zero?
403
+ # Each primary replies with the statistics of its own slots only.
404
+ @node.call_primaries(method, command, args).flatten(1).then(&TSF.call(block))
363
405
  elsif command[1].casecmp('saveconfig').zero?
364
406
  @node.call_all(method, command, args).first.then(&TSF.call(block))
365
407
  elsif command[1].casecmp('countkeysinslot').zero? || command[1].casecmp('getkeysinslot').zero?
@@ -376,15 +418,18 @@ class RedisClient
376
418
  if command[1].casecmp('debug').zero?
377
419
  @node.call_all(method, command, args).first.then(&TSF.call(block))
378
420
  elsif command[1].casecmp('kill').zero?
379
- @node.call_all(method, command, args).first.then(&TSF.call(block))
421
+ # The primaries without any running script reply with the NOTBUSY error.
422
+ @node.call_primaries_leniently(method, command, args).first.then(&TSF.call(block))
380
423
  elsif command[1].casecmp('flush').zero?
381
- @node.call_primaries(method, command, args).first.then(&TSF.call(block))
424
+ # The redis 7.0 or later doesn't propagate the scripts to the replicas.
425
+ @node.call_all(method, command, args).first.then(&TSF.call(block))
382
426
  elsif command[1].casecmp('load').zero?
383
- @node.call_primaries(method, command, args).first.then(&TSF.call(block))
427
+ @node.call_all(method, command, args).first.then(&TSF.call(block))
384
428
  elsif command[1].casecmp('exists').zero?
385
429
  @node.call_all(method, command, args).transpose.map { |arr| arr.any?(&:zero?) ? 0 : 1 }.then(&TSF.call(block))
386
430
  else
387
- assign_node(command).public_send(method, *args, command, &block)
431
+ # The other subcommands keep the default routing with the redirection handling.
432
+ send_command_by_spec(method, command, args, &block)
388
433
  end
389
434
  end
390
435
 
@@ -124,22 +124,27 @@ class RedisClient
124
124
  end
125
125
  end
126
126
 
127
- def send_pipeline(client, redirect:) # rubocop:disable Metrics/AbcSize
127
+ def send_pipeline(client, redirect:) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity
128
128
  replies = client.ensure_connected_cluster_scoped(retryable: @retryable) do |connection|
129
129
  commands = @pipeline._commands
130
130
  client.middlewares.call_pipelined(commands, client.config) do
131
131
  connection.call_pipelined(commands, nil)
132
132
  rescue ::RedisClient::CommandError => e
133
133
  ensure_the_same_slot!(commands)
134
- return handle_command_error!(e, redirect: redirect) unless redirect.zero?
134
+ raise if redirect.zero?
135
+ # Follow the redirection after leaving this block, so that errors from the other node don't close or retry this connection.
136
+ break e if e.message.start_with?('MOVED', 'ASK')
135
137
 
136
- raise
138
+ handle_command_error!(e, redirect: redirect)
137
139
  end
138
140
  end
139
141
 
142
+ return handle_command_error!(replies, redirect: redirect) if replies.is_a?(::RedisClient::CommandError)
140
143
  return if replies.last.nil?
141
144
 
142
145
  coerce_results!(replies.last)
146
+ rescue ::RedisClient::CheckoutTimeoutError
147
+ raise
143
148
  rescue ::RedisClient::ConnectionError
144
149
  @router.renew_cluster_state if @watching_slot.nil?
145
150
  raise
@@ -171,7 +176,9 @@ class RedisClient
171
176
  send_transaction(node, redirect: redirect - 1)
172
177
  elsif err.message.start_with?('ASK')
173
178
  node = @router.assign_asking_node(err.message)
174
- try_asking(node) ? send_transaction(node, redirect: redirect - 1) : raise(err)
179
+ node.with do |client|
180
+ try_asking(client) ? send_transaction(client, redirect: redirect - 1) : raise(err)
181
+ end
175
182
  elsif err.message.start_with?('CLUSTERDOWN')
176
183
  @router.renew_cluster_state if @watching_slot.nil?
177
184
  raise err
@@ -190,6 +197,8 @@ class RedisClient
190
197
 
191
198
  def try_asking(node)
192
199
  node.call('asking') == 'OK'
200
+ rescue ::RedisClient::CheckoutTimeoutError
201
+ raise
193
202
  rescue StandardError
194
203
  false
195
204
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: redis-cluster-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.17.1
4
+ version: 0.17.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Taishi Kasuga
@@ -75,7 +75,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
75
75
  - !ruby/object:Gem::Version
76
76
  version: '0'
77
77
  requirements: []
78
- rubygems_version: 4.0.16
78
+ rubygems_version: 4.0.20
79
79
  specification_version: 4
80
80
  summary: Redis cluster-aware client for Ruby
81
81
  test_files: []