Skip to content

Commit 660f693

Browse files
author
KJ Tsanaktsidis
committed
keyslot
1 parent 727ce8c commit 660f693

File tree

4 files changed

+17
-13
lines changed

4 files changed

+17
-13
lines changed

lib/redis_client/cluster/command.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,7 @@ def extract_first_key(command)
6969
i = determine_first_key_position(command)
7070
return EMPTY_STRING if i == 0
7171

72-
key = (command[i].is_a?(Array) ? command[i].flatten.first : command[i]).to_s
73-
hash_tag = extract_hash_tag(key)
74-
hash_tag.empty? ? key : hash_tag
72+
(command[i].is_a?(Array) ? command[i].flatten.first : command[i]).to_s
7573
end
7674

7775
def extract_all_keys(command)

lib/redis_client/cluster/key_slot_converter.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ module KeySlotConverter
4747

4848
def convert(key)
4949
return nil if key.nil?
50+
hash_tag = extract_hash_tag(key)
51+
key = hash_tag if hash_tag.size > 0
52+
5053

5154
crc = 0
5255
key.each_byte do |b|

lib/redis_client/cluster/router.rb

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -168,21 +168,24 @@ def assign_node(command)
168168
find_node(node_key)
169169
end
170170

171-
def find_node_key(command, seed: nil)
172-
key = @command.extract_first_key(command)
173-
slot = key.empty? ? nil : ::RedisClient::Cluster::KeySlotConverter.convert(key)
174-
175-
if @command.should_send_to_primary?(command)
176-
@node.find_node_key_of_primary(slot) || @node.any_primary_node_key(seed: seed)
171+
def find_node_key_by_key(key, seed: nil, primary: false)
172+
if key && !key.empty?
173+
slot = ::RedisClient::Cluster::KeySlotConverter.convert(key)
174+
primary ? @node.find_node_key_of_primary(slot) : @node.find_node_key_of_replica(slot)
177175
else
178-
@node.find_node_key_of_replica(slot, seed: seed) || @node.any_replica_node_key(seed: seed)
176+
primary ? @node.any_primary_node_key(seed: seed) : @node.any_replica_node_key(seed: seed)
179177
end
180178
end
181179

180+
def find_node_key(command, seed: nil)
181+
key = @command.extract_first_key(command)
182+
find_node_key_by_key(key, seed: seed, primary: @command.should_send_to_primary?(command))
183+
end
184+
182185
def find_primary_node_key(command)
183186
key = @command.extract_first_key(command)
184-
slot = key.empty? ? nil : ::RedisClient::Cluster::KeySlotConverter.convert(key)
185-
@node.find_node_key_of_primary(slot)
187+
return nil unless key&.size > 0
188+
find_node_key_by_key(key, primary: true)
186189
end
187190

188191
def find_node(node_key, retry_count: 3)

test/redis_client/cluster/test_command.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def test_extract_first_key
8484
[
8585
{ command: %w[SET foo 1], want: 'foo' },
8686
{ command: %w[GET foo], want: 'foo' },
87-
{ command: %w[GET foo{bar}baz], want: 'bar' },
87+
{ command: %w[GET foo{bar}baz], want: 'foo{bar}baz' },
8888
{ command: %w[MGET foo bar baz], want: 'foo' },
8989
{ command: %w[UNKNOWN foo bar], want: '' },
9090
{ command: [['GET'], 'foo'], want: 'foo' },

0 commit comments

Comments
 (0)