Class: RedisClient::Cluster::Router

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

Constant Summary collapse

ZERO_CURSOR_FOR_SCAN =
'0'
METHODS_FOR_BLOCKING_CMD =
%i[blocking_call_v blocking_call].freeze
TSF =
->(f, x) { f.nil? ? x : f.call(x) }.curry

Instance Method Summary collapse

Constructor Details

#initialize(config, concurrent_worker, pool: nil, **kwargs) ⇒ Router

Returns a new instance of Router.



19
20
21
22
23
24
25
26
27
28
# File 'lib/redis_client/cluster/router.rb', line 19

def initialize(config, concurrent_worker, pool: nil, **kwargs)
  @config = config.dup
  @concurrent_worker = concurrent_worker
  @pool = pool
  @client_kwargs = kwargs
  @node = fetch_cluster_info(@config, @concurrent_worker, pool: @pool, **@client_kwargs)
  @command = ::RedisClient::Cluster::Command.load(@node.shuffled_nodes)
  @mutex = Mutex.new
  @command_builder = @config.command_builder
end

Instance Method Details

#assign_asking_node(err_msg) ⇒ Object



209
210
211
212
# File 'lib/redis_client/cluster/router.rb', line 209

def assign_asking_node(err_msg)
  _, _, node_key = err_msg.split
  find_node(node_key)
end

#assign_node(command) ⇒ Object



166
167
168
169
# File 'lib/redis_client/cluster/router.rb', line 166

def assign_node(command)
  node_key = find_node_key(command)
  find_node(node_key)
end

#assign_redirection_node(err_msg) ⇒ Object



202
203
204
205
206
207
# File 'lib/redis_client/cluster/router.rb', line 202

def assign_redirection_node(err_msg)
  _, slot, node_key = err_msg.split
  slot = slot.to_i
  @node.update_slot(slot, node_key)
  find_node(node_key)
end

#closeObject



218
219
220
# File 'lib/redis_client/cluster/router.rb', line 218

def close
  @node.each(&:close)
end

#command_exists?(name) ⇒ Boolean

Returns:

  • (Boolean)


198
199
200
# File 'lib/redis_client/cluster/router.rb', line 198

def command_exists?(name)
  @command.exists?(name)
end

#find_node(node_key, retry_count: 3) ⇒ Object



188
189
190
191
192
193
194
195
196
# File 'lib/redis_client/cluster/router.rb', line 188

def find_node(node_key, retry_count: 3)
  @node.find_by(node_key)
rescue ::RedisClient::Cluster::Node::ReloadNeeded
  raise ::RedisClient::Cluster::NodeMightBeDown if retry_count <= 0

  update_cluster_info!
  retry_count -= 1
  retry
end

#find_node_key(command, seed: nil) ⇒ Object



171
172
173
174
175
176
177
178
179
180
# File 'lib/redis_client/cluster/router.rb', line 171

def find_node_key(command, seed: nil)
  key = @command.extract_first_key(command)
  slot = key.empty? ? nil : ::RedisClient::Cluster::KeySlotConverter.convert(key)

  if @command.should_send_to_primary?(command)
    @node.find_node_key_of_primary(slot) || @node.any_primary_node_key(seed: seed)
  else
    @node.find_node_key_of_replica(slot, seed: seed) || @node.any_replica_node_key(seed: seed)
  end
end

#find_primary_node_key(command) ⇒ Object



182
183
184
185
186
# File 'lib/redis_client/cluster/router.rb', line 182

def find_primary_node_key(command)
  key = @command.extract_first_key(command)
  slot = key.empty? ? nil : ::RedisClient::Cluster::KeySlotConverter.convert(key)
  @node.find_node_key_of_primary(slot)
end

#node_keysObject



214
215
216
# File 'lib/redis_client/cluster/router.rb', line 214

def node_keys
  @node.node_keys
end

#scan(*command, seed: nil, **kwargs) ⇒ Object

rubocop:disable Metrics/AbcSize



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/redis_client/cluster/router.rb', line 142

def scan(*command, seed: nil, **kwargs) # rubocop:disable Metrics/AbcSize
  command = @command_builder.generate(command, kwargs)

  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]
end

#send_command(method, command, *args, &block) ⇒ Object

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



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/redis_client/cluster/router.rb', line 30

def send_command(method, command, *args, &block) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
  cmd = ::RedisClient::Cluster::NormalizedCmdName.instance.get_by_command(command)
  case cmd
  when 'ping'     then @node.send_ping(method, command, args).first.then(&TSF.call(block))
  when 'wait'     then send_wait_command(method, command, args, &block)
  when 'keys'     then @node.call_replicas(method, command, args).flatten.sort_by(&:to_s).then(&TSF.call(block))
  when 'dbsize'   then @node.call_replicas(method, command, args).select { |e| e.is_a?(Integer) }.sum.then(&TSF.call(block))
  when 'scan'     then scan(command, seed: 1)
  when 'lastsave' then @node.call_all(method, command, args).sort_by(&:to_i).then(&TSF.call(block))
  when 'role'     then @node.call_all(method, command, args, &block)
  when 'config'   then send_config_command(method, command, args, &block)
  when 'client'   then send_client_command(method, command, args, &block)
  when 'cluster'  then send_cluster_command(method, command, args, &block)
  when 'memory'   then send_memory_command(method, command, args, &block)
  when 'script'   then send_script_command(method, command, args, &block)
  when 'pubsub'   then send_pubsub_command(method, command, args, &block)
  when 'acl', 'auth', 'bgrewriteaof', 'bgsave', 'quit', 'save'
    @node.call_all(method, command, args).first.then(&TSF.call(block))
  when 'flushall', 'flushdb'
    @node.call_primaries(method, command, args).first.then(&TSF.call(block))
  when 'readonly', 'readwrite', 'shutdown'
    raise ::RedisClient::Cluster::OrchestrationCommandNotSupported, cmd
  when 'discard', 'exec', 'multi', 'unwatch'
    raise ::RedisClient::Cluster::AmbiguousNodeError, cmd
  else
    node = assign_node(command)
    try_send(node, method, command, args, &block)
  end
rescue ::RedisClient::CircuitBreaker::OpenCircuitError
  raise
rescue ::RedisClient::Cluster::Node::ReloadNeeded
  update_cluster_info!
  raise ::RedisClient::Cluster::NodeMightBeDown
rescue ::RedisClient::Cluster::ErrorCollection => e
  raise if e.errors.any?(::RedisClient::CircuitBreaker::OpenCircuitError)

  update_cluster_info! if e.errors.values.any? do |err|
    err.message.start_with?('CLUSTERDOWN Hash slot not served')
  end

  raise
end

#try_delegate(node, method, *args, retry_count: 3, **kwargs, &block) ⇒ Object

rubocop:disable Metrics/AbcSize



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/redis_client/cluster/router.rb', line 111

def try_delegate(node, method, *args, retry_count: 3, **kwargs, &block) # rubocop:disable Metrics/AbcSize
  node.public_send(method, *args, **kwargs, &block)
rescue ::RedisClient::CircuitBreaker::OpenCircuitError
  raise
rescue ::RedisClient::CommandError => e
  raise if retry_count <= 0

  if e.message.start_with?('MOVED')
    node = assign_redirection_node(e.message)
    retry_count -= 1
    retry
  elsif e.message.start_with?('ASK')
    node = assign_asking_node(e.message)
    node.call('ASKING')
    retry_count -= 1
    retry
  elsif e.message.start_with?('CLUSTERDOWN Hash slot not served')
    update_cluster_info!
    retry_count -= 1
    retry
  else
    raise
  end
rescue ::RedisClient::ConnectionError
  raise if retry_count <= 0

  update_cluster_info!
  retry_count -= 1
  retry
end

#try_send(node, method, command, args, retry_count: 3, &block) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/redis_client/cluster/router.rb', line 74

def try_send(node, method, command, args, retry_count: 3, &block) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
  if args.empty?
    # prevent memory allocation for variable-length args
    node.public_send(method, command, &block)
  else
    node.public_send(method, *args, command, &block)
  end
rescue ::RedisClient::CircuitBreaker::OpenCircuitError
  raise
rescue ::RedisClient::CommandError => e
  raise if retry_count <= 0

  if e.message.start_with?('MOVED')
    node = assign_redirection_node(e.message)
    retry_count -= 1
    retry
  elsif e.message.start_with?('ASK')
    node = assign_asking_node(e.message)
    node.call('ASKING')
    retry_count -= 1
    retry
  elsif e.message.start_with?('CLUSTERDOWN Hash slot not served')
    update_cluster_info!
    retry_count -= 1
    retry
  else
    raise
  end
rescue ::RedisClient::ConnectionError => e
  raise if METHODS_FOR_BLOCKING_CMD.include?(method) && e.is_a?(RedisClient::ReadTimeoutError)
  raise if retry_count <= 0

  update_cluster_info!
  retry_count -= 1
  retry
end