Class: RedisClient::Cluster::Pipeline

Inherits:
Object
  • Object
show all
Defined in:
lib/redis_client/cluster/pipeline.rb

Defined Under Namespace

Classes: Extended, RedirectionNeeded, StaleClusterState

Constant Summary collapse

ReplySizeError =
Class.new(::RedisClient::Cluster::Error)

Instance Method Summary collapse

Constructor Details

#initialize(router, command_builder, concurrent_worker, exception:, seed: Random.new_seed) ⇒ Pipeline

Returns a new instance of Pipeline.



133
134
135
136
137
138
139
140
141
# File 'lib/redis_client/cluster/pipeline.rb', line 133

def initialize(router, command_builder, concurrent_worker, exception:, seed: Random.new_seed)
  @router = router
  @command_builder = command_builder
  @concurrent_worker = concurrent_worker
  @exception = exception
  @seed = seed
  @pipelines = nil
  @size = 0
end

Instance Method Details

#blocking_call(timeout, *args, **kwargs, &block) ⇒ Object



167
168
169
170
171
# File 'lib/redis_client/cluster/pipeline.rb', line 167

def blocking_call(timeout, *args, **kwargs, &block)
  command = @command_builder.generate(args, kwargs)
  node_key = @router.find_node_key(command, seed: @seed)
  append_pipeline(node_key).blocking_call_v(timeout, command, &block)
end

#blocking_call_v(timeout, args, &block) ⇒ Object



173
174
175
176
177
# File 'lib/redis_client/cluster/pipeline.rb', line 173

def blocking_call_v(timeout, args, &block)
  command = @command_builder.generate(args)
  node_key = @router.find_node_key(command, seed: @seed)
  append_pipeline(node_key).blocking_call_v(timeout, command, &block)
end

#call(*args, **kwargs, &block) ⇒ Object



143
144
145
146
147
# File 'lib/redis_client/cluster/pipeline.rb', line 143

def call(*args, **kwargs, &block)
  command = @command_builder.generate(args, kwargs)
  node_key = @router.find_node_key(command, seed: @seed)
  append_pipeline(node_key).call_v(command, &block)
end

#call_once(*args, **kwargs, &block) ⇒ Object



155
156
157
158
159
# File 'lib/redis_client/cluster/pipeline.rb', line 155

def call_once(*args, **kwargs, &block)
  command = @command_builder.generate(args, kwargs)
  node_key = @router.find_node_key(command, seed: @seed)
  append_pipeline(node_key).call_once_v(command, &block)
end

#call_once_v(args, &block) ⇒ Object



161
162
163
164
165
# File 'lib/redis_client/cluster/pipeline.rb', line 161

def call_once_v(args, &block)
  command = @command_builder.generate(args)
  node_key = @router.find_node_key(command, seed: @seed)
  append_pipeline(node_key).call_once_v(command, &block)
end

#call_v(args, &block) ⇒ Object



149
150
151
152
153
# File 'lib/redis_client/cluster/pipeline.rb', line 149

def call_v(args, &block)
  command = @command_builder.generate(args)
  node_key = @router.find_node_key(command, seed: @seed)
  append_pipeline(node_key).call_v(command, &block)
end

#empty?Boolean

Returns:

  • (Boolean)


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

def empty?
  @size.zero?
end

#executeObject

rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity



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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/redis_client/cluster/pipeline.rb', line 183

def execute # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
  return if @pipelines.nil? || @pipelines.empty?

  work_group = @concurrent_worker.new_group(size: @pipelines.size)

  @pipelines.each do |node_key, pipeline|
    work_group.push(node_key, @router.find_node(node_key), pipeline) do |cli, pl|
      replies = do_pipelining(cli, pl)
      raise ReplySizeError, "commands: #{pl._size}, replies: #{replies.size}" if pl._size != replies.size

      replies
    end
  end

  all_replies = errors = required_redirections = cluster_state_errors = nil

  work_group.each do |node_key, v|
    case v
    when ::RedisClient::Cluster::Pipeline::RedirectionNeeded
      required_redirections ||= {}
      required_redirections[node_key] = v
    when ::RedisClient::Cluster::Pipeline::StaleClusterState
      cluster_state_errors ||= {}
      cluster_state_errors[node_key] = v
    when StandardError
      cluster_state_errors ||= {} if v.is_a?(::RedisClient::ConnectionError)
      errors ||= {}
      errors[node_key] = v
    else
      all_replies ||= Array.new(@size)
      @pipelines[node_key].outer_indices.each_with_index { |outer, inner| all_replies[outer] = v[inner] }
    end
  end

  work_group.close
  @router.renew_cluster_state if cluster_state_errors
  raise ::RedisClient::Cluster::ErrorCollection.with_errors(errors).with_config(@router.config) unless errors.nil?

  required_redirections&.each do |node_key, v|
    raise v.first_exception if v.first_exception

    all_replies ||= Array.new(@size)
    pipeline = @pipelines[node_key]
    pipeline.coerce_except!(v.replies, v.indices)
    v.indices.each { |i| v.replies[i] = handle_redirection(v.replies[i], pipeline, i) }
    pipeline.outer_indices.each_with_index { |outer, inner| all_replies[outer] = v.replies[inner] }
  end

  cluster_state_errors&.each do |node_key, v|
    raise v.first_exception if v.first_exception

    all_replies ||= Array.new(@size)
    pipeline = @pipelines[node_key]
    pipeline._coerce!(v.replies)
    pipeline.outer_indices.each_with_index { |outer, inner| all_replies[outer] = v.replies[inner] }
  end

  all_replies
end