Skip to content

Commit 7912f7e

Browse files
authored
perf: reduce load of servers in initialization as possible (#287)
1 parent 987f920 commit 7912f7e

File tree

4 files changed

+23
-7
lines changed

4 files changed

+23
-7
lines changed

lib/redis_client/cluster/command.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
class RedisClient
88
class Cluster
99
class Command
10+
SLOW_COMMAND_TIMEOUT = Float(ENV.fetch('REDIS_CLIENT_SLOW_COMMAND_TIMEOUT', -1))
11+
1012
EMPTY_STRING = ''
1113
LEFT_BRACKET = '{'
1214
RIGHT_BRACKET = '}'
@@ -25,7 +27,10 @@ def load(nodes)
2527
cmd = errors = nil
2628

2729
nodes&.each do |node|
30+
regular_timeout = node.read_timeout
31+
node.read_timeout = SLOW_COMMAND_TIMEOUT > 0.0 ? SLOW_COMMAND_TIMEOUT : regular_timeout
2832
reply = node.call('COMMAND')
33+
node.read_timeout = regular_timeout
2934
commands = parse_command_reply(reply)
3035
cmd = ::RedisClient::Cluster::Command.new(commands)
3136
break

lib/redis_client/cluster/node.rb

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,18 @@ class Cluster
1313
class Node
1414
include Enumerable
1515

16+
# It affects to strike a balance between load and stability in initialization or changed states.
17+
MAX_STARTUP_SAMPLE = Integer(ENV.fetch('REDIS_CLIENT_MAX_STARTUP_SAMPLE', 3))
18+
19+
# It's used with slow queries of fetching meta data like CLUSTER NODES, COMMAND and so on.
20+
SLOW_COMMAND_TIMEOUT = Float(ENV.fetch('REDIS_CLIENT_SLOW_COMMAND_TIMEOUT', -1))
21+
22+
# less memory consumption, but slow
23+
USE_CHAR_ARRAY_SLOT = Integer(ENV.fetch('REDIS_CLIENT_USE_CHAR_ARRAY_SLOT', 1)) == 1
24+
1625
SLOT_SIZE = 16_384
1726
MIN_SLOT = 0
1827
MAX_SLOT = SLOT_SIZE - 1
19-
MAX_STARTUP_SAMPLE = Integer(ENV.fetch('REDIS_CLIENT_MAX_STARTUP_SAMPLE', 3))
20-
USE_CHAR_ARRAY_SLOT = Integer(ENV.fetch('REDIS_CLIENT_USE_CHAR_ARRAY_SLOT', 1)) == 1 # less memory consumption, but slow
2128
IGNORE_GENERIC_CONFIG_KEYS = %i[url host port path].freeze
2229
DEAD_FLAGS = %w[fail? fail handshake noaddr noflags].freeze
2330
ROLE_FLAGS = %w[master slave].freeze
@@ -99,7 +106,10 @@ def load_info(options, concurrent_worker, **kwargs) # rubocop:disable Metrics/Ab
99106

100107
startup_nodes.each_with_index do |raw_client, i|
101108
work_group.push(i, raw_client) do |client|
109+
regular_timeout = client.read_timeout
110+
client.read_timeout = SLOW_COMMAND_TIMEOUT > 0.0 ? SLOW_COMMAND_TIMEOUT : regular_timeout
102111
reply = client.call('CLUSTER', 'NODES')
112+
client.read_timeout = regular_timeout
103113
parse_cluster_node_reply(reply)
104114
rescue StandardError => e
105115
e
@@ -209,10 +219,6 @@ def each(&block)
209219
@topology.clients.each_value(&block)
210220
end
211221

212-
def shuffled_nodes
213-
@topology.clients.values.shuffle
214-
end
215-
216222
def sample
217223
@topology.clients.values.sample
218224
end
@@ -252,6 +258,10 @@ def clients_for_scanning(seed: nil)
252258
@topology.clients_for_scanning(seed: seed).values.sort_by { |c| "#{c.config.host}-#{c.config.port}" }
253259
end
254260

261+
def replica_clients
262+
@topology.replica_clients.values
263+
end
264+
255265
def find_node_key_of_primary(slot)
256266
return if slot.nil?
257267

lib/redis_client/cluster/pub_sub.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# frozen_string_literal: true
22

33
require 'redis_client'
4+
require 'redis_client/cluster/normalized_cmd_name'
45

56
class RedisClient
67
class Cluster

lib/redis_client/cluster/router.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def initialize(config, concurrent_worker, pool: nil, **kwargs)
2222
@pool = pool
2323
@client_kwargs = kwargs
2424
@node = fetch_cluster_info(@config, @concurrent_worker, pool: @pool, **@client_kwargs)
25-
@command = ::RedisClient::Cluster::Command.load(@node.shuffled_nodes)
25+
@command = ::RedisClient::Cluster::Command.load(@node.replica_clients.shuffle)
2626
@mutex = Mutex.new
2727
@command_builder = @config.command_builder
2828
end

0 commit comments

Comments
 (0)