Skip to content

Commit ed2bb7a

Browse files
Raise errors from #pipelined instead of returning them (#297)
1 parent c00ee01 commit ed2bb7a

File tree

2 files changed

+16
-25
lines changed

2 files changed

+16
-25
lines changed

lib/redis_client/cluster/pipeline.rb

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,28 +50,31 @@ def get_block(inner_index)
5050
end
5151

5252
::RedisClient::ConnectionMixin.module_eval do
53-
def call_pipelined_aware_of_redirection(commands, timeouts) # rubocop:disable Metrics/AbcSize
53+
def call_pipelined_aware_of_redirection(commands, timeouts) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
5454
size = commands.size
5555
results = Array.new(commands.size)
5656
@pending_reads += size
5757
write_multi(commands)
5858

5959
redirection_indices = nil
60+
exception = nil
6061
size.times do |index|
6162
timeout = timeouts && timeouts[index]
6263
result = read(timeout)
6364
@pending_reads -= 1
64-
if result.is_a?(CommandError)
65+
if result.is_a?(::RedisClient::Error)
6566
result._set_command(commands[index])
66-
if result.message.start_with?('MOVED', 'ASK')
67+
if result.is_a?(::RedisClient::CommandError) && result.message.start_with?('MOVED', 'ASK')
6768
redirection_indices ||= []
6869
redirection_indices << index
70+
else
71+
exception ||= result
6972
end
7073
end
71-
7274
results[index] = result
7375
end
7476

77+
raise exception if exception
7578
return results if redirection_indices.nil?
7679

7780
err = ::RedisClient::Cluster::Pipeline::RedirectionNeeded.new
@@ -217,21 +220,15 @@ def handle_redirection(err, pipeline, inner_index)
217220

218221
if err.message.start_with?('MOVED')
219222
node = @router.assign_redirection_node(err.message)
220-
try_redirection(node, pipeline, inner_index)
223+
redirect_command(node, pipeline, inner_index)
221224
elsif err.message.start_with?('ASK')
222225
node = @router.assign_asking_node(err.message)
223-
try_asking(node) ? try_redirection(node, pipeline, inner_index) : err
226+
try_asking(node) ? redirect_command(node, pipeline, inner_index) : err
224227
else
225228
err
226229
end
227230
end
228231

229-
def try_redirection(node, pipeline, inner_index)
230-
redirect_command(node, pipeline, inner_index)
231-
rescue StandardError => e
232-
e
233-
end
234-
235232
def redirect_command(node, pipeline, inner_index)
236233
method = pipeline.get_callee_method(inner_index)
237234
command = pipeline.get_command(inner_index)

test/redis_client/test_cluster.rb

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -153,22 +153,16 @@ def test_pipelined
153153
end
154154

155155
def test_pipelined_with_errors
156-
got = @client.pipelined do |pipeline|
157-
10.times do |i|
158-
pipeline.call('SET', "string#{i}", i)
159-
pipeline.call('SET', "string#{i}", i, 'too many args')
160-
pipeline.call('SET', "string#{i}", i + 10)
156+
assert_raises(RedisClient::Cluster::ErrorCollection) do
157+
@client.pipelined do |pipeline|
158+
10.times do |i|
159+
pipeline.call('SET', "string#{i}", i)
160+
pipeline.call('SET', "string#{i}", i, 'too many args')
161+
pipeline.call('SET', "string#{i}", i + 10)
162+
end
161163
end
162164
end
163165

164-
assert_equal(30, got.size)
165-
166-
10.times do |i|
167-
assert_equal('OK', got[(3 * i) + 0])
168-
assert_instance_of(::RedisClient::CommandError, got[(3 * i) + 1])
169-
assert_equal('OK', got[(3 * i) + 2])
170-
end
171-
172166
wait_for_replication
173167

174168
10.times { |i| assert_equal((i + 10).to_s, @client.call('GET', "string#{i}")) }

0 commit comments

Comments
 (0)