redis-cluster-client 0.4.2 → 0.4.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5bd4bae00bd7df3abda8f68a53f08746abae7c818f22466e8ec3e52c3e978b5b
4
- data.tar.gz: c78d5b8600d131655d8fcf63744928a9f62718d78aaa3ddb5fd73908b1716314
3
+ metadata.gz: 16cd5b517f21a96b53fcaa47eb44e28b0c8d12aef23e3d2d239b2568d5ba61cd
4
+ data.tar.gz: ec4a1ebf3e548fdd7bfb184a910511f98919eecda1987674bef8d135bfc8b9da
5
5
  SHA512:
6
- metadata.gz: c78d564a0398b6adfc0217568fd22ff7227215db616f8052142ebf45999a70930ce0f4b76363d6f0ac1030bffa9673bc25566815d6eac8bacf84247fd9e162f2
7
- data.tar.gz: c992c9a5faf59fc2929351e99d6e112cd0fd464397fad0d4952a5997abebdf0b2fdfd8d691c4a96aea0856639423c99519d538bc422ce66943d34292a242224d
6
+ metadata.gz: 92a61bcbb6964cb41ed791768d346f65b48d1943627fbea13b86cf995e921990dcc41651553f7c5200e60cebc7c5cc7e519795e02a9173e37dd1ef4da1201006
7
+ data.tar.gz: a27605d3438d40da8be910776db98fd6ede734c9ac8824c3bcedfc599d0d1f3f6f00187a7e785f96d0da8d1f480b0c6f012fa4670e31edf1c152f189a59bcf73
@@ -10,6 +10,7 @@ class RedisClient
10
10
  EMPTY_STRING = ''
11
11
  LEFT_BRACKET = '{'
12
12
  RIGHT_BRACKET = '}'
13
+ EMPTY_HASH = {}.freeze
13
14
 
14
15
  Detail = Struct.new(
15
16
  'RedisCommand',
@@ -21,15 +22,15 @@ class RedisClient
21
22
 
22
23
  class << self
23
24
  def load(nodes)
24
- errors = []
25
- cmd = nil
26
- nodes&.each do |node|
27
- break unless cmd.nil?
25
+ cmd = errors = nil
28
26
 
27
+ nodes&.each do |node|
29
28
  reply = node.call('COMMAND')
30
29
  commands = parse_command_reply(reply)
31
30
  cmd = ::RedisClient::Cluster::Command.new(commands)
31
+ break
32
32
  rescue ::RedisClient::Error => e
33
+ errors ||= []
33
34
  errors << e
34
35
  end
35
36
 
@@ -41,21 +42,20 @@ class RedisClient
41
42
  private
42
43
 
43
44
  def parse_command_reply(rows)
44
- rows&.reject { |row| row[0].nil? }.to_h do |row|
45
- [
46
- row[0].downcase,
47
- ::RedisClient::Cluster::Command::Detail.new(
48
- first_key_position: row[3],
49
- write?: row[2].include?('write'),
50
- readonly?: row[2].include?('readonly')
51
- )
52
- ]
53
- end
45
+ rows&.each_with_object({}) do |row, acc|
46
+ next if row[0].nil?
47
+
48
+ acc[row[0].downcase] = ::RedisClient::Cluster::Command::Detail.new(
49
+ first_key_position: row[3],
50
+ write?: row[2].include?('write'),
51
+ readonly?: row[2].include?('readonly')
52
+ )
53
+ end.freeze || EMPTY_HASH
54
54
  end
55
55
  end
56
56
 
57
57
  def initialize(commands)
58
- @commands = commands || {}
58
+ @commands = commands || EMPTY_HASH
59
59
  end
60
60
 
61
61
  def extract_first_key(command)
@@ -10,7 +10,7 @@ class RedisClient
10
10
 
11
11
  attr_reader :replica_clients
12
12
 
13
- DUMMY_LATENCY_NSEC = 100 * 1000 * 1000 * 1000
13
+ DUMMY_LATENCY_MSEC = 100 * 1000 * 1000
14
14
  MEASURE_ATTEMPT_COUNT = 10
15
15
 
16
16
  def initialize(replications, options, pool, **kwargs)
@@ -45,7 +45,7 @@ class RedisClient
45
45
  Thread.new(k, v) do |node_key, client|
46
46
  Thread.current[:node_key] = node_key
47
47
 
48
- min = DUMMY_LATENCY_NSEC
48
+ min = DUMMY_LATENCY_MSEC
49
49
  MEASURE_ATTEMPT_COUNT.times do
50
50
  starting = Process.clock_gettime(Process::CLOCK_MONOTONIC, :microsecond)
51
51
  client.call_once('PING')
@@ -55,7 +55,7 @@ class RedisClient
55
55
 
56
56
  Thread.current[:latency] = min
57
57
  rescue StandardError
58
- Thread.current[:latency] = DUMMY_LATENCY_NSEC
58
+ Thread.current[:latency] = DUMMY_LATENCY_MSEC
59
59
  end
60
60
  end
61
61
 
@@ -18,6 +18,9 @@ class RedisClient
18
18
  MAX_STARTUP_SAMPLE = 37
19
19
  MAX_THREADS = Integer(ENV.fetch('REDIS_CLIENT_MAX_THREADS', 5))
20
20
  IGNORE_GENERIC_CONFIG_KEYS = %i[url host port path].freeze
21
+ DEAD_FLAGS = %w[fail? fail handshake noaddr noflags].freeze
22
+ ROLE_FLAGS = %w[master slave].freeze
23
+ EMPTY_ARRAY = [].freeze
21
24
 
22
25
  ReloadNeeded = Class.new(::RedisClient::Error)
23
26
 
@@ -118,13 +121,13 @@ class RedisClient
118
121
  raise ::RedisClient::Cluster::InitialSetupError, errors if node_info_list.nil?
119
122
 
120
123
  grouped = node_info_list.compact.group_by do |info_list|
121
- info_list
122
- .sort_by(&:id)
123
- .map { |i| "#{i.id}#{i.node_key}#{i.role}#{i.primary_id}#{i.config_epoch}" }
124
- .join
124
+ info_list.sort_by!(&:id)
125
+ info_list.each_with_object(String.new(capacity: 128 * info_list.size)) do |e, a|
126
+ a << e.id << e.node_key << e.role << e.primary_id << e.config_epoch
127
+ end
125
128
  end
126
129
 
127
- grouped.max_by { |_, v| v.size }[1].first
130
+ grouped.max_by { |_, v| v.size }[1].first.freeze
128
131
  end
129
132
 
130
133
  private
@@ -132,27 +135,47 @@ class RedisClient
132
135
  # @see https://redis.io/commands/cluster-nodes/
133
136
  # @see https://github.com/redis/redis/blob/78960ad57b8a5e6af743d789ed8fd767e37d42b8/src/cluster.c#L4660-L4683
134
137
  def parse_cluster_node_reply(reply) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
135
- rows = reply.split("\n").map(&:split)
136
- rows.each { |arr| arr[2] = arr[2].split(',') }
137
- rows.select! { |arr| arr[7] == 'connected' && (arr[2] & %w[fail? fail handshake noaddr noflags]).empty? }
138
- rows.each do |arr|
139
- arr[1] = arr[1].split('@').first
140
- arr[2] = (arr[2] & %w[master slave]).first
141
- if arr[8].nil?
142
- arr[8] = []
143
- next
144
- end
145
- arr[8] = arr[8..].filter_map { |str| str.start_with?('[') ? nil : str.split('-').map { |s| Integer(s) } }
146
- .map { |a| a.size == 1 ? a << a.first : a }.map(&:sort)
147
- end
138
+ reply.each_line("\n", chomp: true).filter_map do |line|
139
+ fields = line.split
140
+ flags = fields[2].split(',')
141
+ next unless fields[7] == 'connected' && (flags & DEAD_FLAGS).empty?
142
+
143
+ slots = if fields[8].nil?
144
+ EMPTY_ARRAY
145
+ else
146
+ fields[8..].reject { |str| str.start_with?('[') }
147
+ .map { |str| str.split('-').map { |s| Integer(s) } }
148
+ .map { |a| a.size == 1 ? a << a.first : a }
149
+ .map(&:sort)
150
+ end
148
151
 
149
- rows.map do |arr|
150
152
  ::RedisClient::Cluster::Node::Info.new(
151
- id: arr[0], node_key: arr[1], role: arr[2], primary_id: arr[3], ping_sent: arr[4],
152
- pong_recv: arr[5], config_epoch: arr[6], link_state: arr[7], slots: arr[8]
153
+ id: fields[0],
154
+ node_key: parse_node_key(fields[1]),
155
+ role: (flags & ROLE_FLAGS).first,
156
+ primary_id: fields[3],
157
+ ping_sent: fields[4],
158
+ pong_recv: fields[5],
159
+ config_epoch: fields[6],
160
+ link_state: fields[7],
161
+ slots: slots
153
162
  )
154
163
  end
155
164
  end
165
+
166
+ # As redirection node_key is dependent on `cluster-preferred-endpoint-type` config,
167
+ # node_key should use hostname if present in CLUSTER NODES output.
168
+ #
169
+ # See https://redis.io/commands/cluster-nodes/ for details on the output format.
170
+ # node_address matches fhe format: <ip:port@cport[,hostname[,auxiliary_field=value]*]>
171
+ def parse_node_key(node_address)
172
+ ip_chunk, hostname, _auxiliaries = node_address.split(',')
173
+ ip_port_string = ip_chunk.split('@').first
174
+ return ip_port_string if hostname.nil? || hostname.empty?
175
+
176
+ port = ip_port_string.split(':')[1]
177
+ "#{hostname}:#{port}"
178
+ end
156
179
  end
157
180
 
158
181
  def initialize(
@@ -3,33 +3,61 @@
3
3
  class RedisClient
4
4
  class Cluster
5
5
  class PubSub
6
+ MAX_THREADS = Integer(ENV.fetch('REDIS_CLIENT_MAX_THREADS', 5))
7
+
6
8
  def initialize(router, command_builder)
7
9
  @router = router
8
10
  @command_builder = command_builder
9
- @pubsub = nil
11
+ @pubsub_states = {}
10
12
  end
11
13
 
12
14
  def call(*args, **kwargs)
13
- close
14
- command = @command_builder.generate(args, kwargs)
15
- @pubsub = @router.assign_node(command).pubsub
16
- @pubsub.call_v(command)
15
+ _call(@command_builder.generate(args, kwargs))
17
16
  end
18
17
 
19
18
  def call_v(command)
20
- close
21
- command = @command_builder.generate(command)
22
- @pubsub = @router.assign_node(command).pubsub
23
- @pubsub.call_v(command)
19
+ _call(@command_builder.generate(command))
24
20
  end
25
21
 
26
22
  def close
27
- @pubsub&.close
28
- @pubsub = nil
23
+ @pubsub_states.each_value(&:close)
24
+ @pubsub_states.clear
29
25
  end
30
26
 
31
27
  def next_event(timeout = nil)
32
- @pubsub&.next_event(timeout)
28
+ msgs = collect_messages(timeout).compact
29
+ return msgs.first if msgs.size == 1
30
+
31
+ msgs
32
+ end
33
+
34
+ private
35
+
36
+ def _call(command)
37
+ node_key = @router.find_node_key(command)
38
+ pubsub = if @pubsub_states.key?(node_key)
39
+ @pubsub_states[node_key]
40
+ else
41
+ @pubsub_states[node_key] = @router.find_node(node_key).pubsub
42
+ end
43
+ pubsub.call_v(command)
44
+ end
45
+
46
+ def collect_messages(timeout)
47
+ @pubsub_states.each_slice(MAX_THREADS).each_with_object([]) do |chuncked_pubsub_states, acc|
48
+ threads = chuncked_pubsub_states.map do |_, v|
49
+ Thread.new(v) do |pubsub|
50
+ Thread.current[:reply] = pubsub.next_event(timeout)
51
+ rescue StandardError => e
52
+ Thread.current[:reply] = e
53
+ end
54
+ end
55
+
56
+ threads.each do |t|
57
+ t.join
58
+ acc << t[:reply]
59
+ end
60
+ end
33
61
  end
34
62
  end
35
63
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: redis-cluster-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.2
4
+ version: 0.4.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Taishi Kasuga
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-02-03 00:00:00.000000000 Z
11
+ date: 2023-08-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: redis-client
@@ -24,7 +24,7 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0.12'
27
- description:
27
+ description:
28
28
  email:
29
29
  - proxy0721@gmail.com
30
30
  executables: []
@@ -54,7 +54,7 @@ licenses:
54
54
  metadata:
55
55
  rubygems_mfa_required: 'true'
56
56
  allowed_push_host: https://rubygems.org
57
- post_install_message:
57
+ post_install_message:
58
58
  rdoc_options: []
59
59
  require_paths:
60
60
  - lib
@@ -69,8 +69,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
69
69
  - !ruby/object:Gem::Version
70
70
  version: '0'
71
71
  requirements: []
72
- rubygems_version: 3.3.23
73
- signing_key:
72
+ rubygems_version: 3.4.13
73
+ signing_key:
74
74
  specification_version: 4
75
75
  summary: A Redis cluster client for Ruby
76
76
  test_files: []