Skip to content

Commit

Permalink
chore: cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
supercaracal committed Apr 21, 2024
1 parent 06de17c commit 9a50314
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 105 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,11 @@ The following methods are able to be used like `redis-client`.
* `#pubsub`
* `#close`

The other methods are not implemented because the client cannot operate with cluster mode.
`#pipelined` method splits and sends commands to each node and aggregates replies.
The `#scan` method iterates all the nodes seamlessly.
The `#pipelined` method splits and sends commands to each node and aggregates replies.
The `#multi` method supports the transaction feature but you should use the hashtag for keys.
The `#pubsub` method supports the sharded Pub/Sub feature.
Every interfaces are handled with redirections and resharding states.

## Multiple keys and CROSSSLOT error
A subset of commands can be passed multiple keys. But it has a constraint the keys are in the same hash slot.
Expand Down
23 changes: 2 additions & 21 deletions lib/redis_client/cluster.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
require 'redis_client/cluster/pub_sub'
require 'redis_client/cluster/router'
require 'redis_client/cluster/transaction'
require 'redis_client/cluster/pinning_node'
require 'redis_client/cluster/optimistic_locking'

class RedisClient
Expand Down Expand Up @@ -118,13 +117,8 @@ def pubsub
::RedisClient::Cluster::PubSub.new(@router, @command_builder)
end

# TODO: This isn't an official public interface yet. Don't use in your production environment.
# @see https://github.com/redis-rb/redis-cluster-client/issues/299
def with(key: nil, hashtag: nil, write: true)
key = process_with_arguments(key, hashtag)
node_key = @router.find_node_key_by_key(key, primary: write)
node = @router.find_node(node_key)
node.with { |c| yield ::RedisClient::Cluster::PinningNode.new(c) }
def with(...)
raise NotImplementedError, 'No way to use'
end

def close
Expand All @@ -135,19 +129,6 @@ def close

private

def process_with_arguments(key, hashtag) # rubocop:disable Metrics/CyclomaticComplexity
raise ArgumentError, 'Only one of key or hashtag may be provided' if key && hashtag

if hashtag
# The documentation says not to wrap your hashtag in {}, but people will probably
# do it anyway and it's easy for us to fix here.
key = hashtag&.match?(/^{.*}$/) ? hashtag : "{#{hashtag}}"
end
raise ArgumentError, 'One of key or hashtag must be provided' if key.nil? || key.empty?

key
end

def method_missing(name, *args, **kwargs, &block)
if @router.command_exists?(name)
args.unshift(name)
Expand Down
8 changes: 1 addition & 7 deletions lib/redis_client/cluster/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,7 @@ def build_connection_prelude
end
end

def initialize(
concurrent_worker,
config:,
pool: nil,
**kwargs
)

def initialize(concurrent_worker, config:, pool: nil, **kwargs)
@concurrent_worker = concurrent_worker
@slots = build_slot_node_mappings(EMPTY_ARRAY)
@replications = build_replication_mappings(EMPTY_ARRAY)
Expand Down
3 changes: 3 additions & 0 deletions lib/redis_client/cluster/node/base_topology.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ class Cluster
class Node
class BaseTopology
IGNORE_GENERIC_CONFIG_KEYS = %i[url host port path].freeze
EMPTY_HASH = {}.freeze
EMPTY_ARRAY = [].freeze

attr_reader :clients, :primary_clients, :replica_clients

def initialize(pool, concurrent_worker, **kwargs)
Expand Down
35 changes: 0 additions & 35 deletions lib/redis_client/cluster/pinning_node.rb

This file was deleted.

1 change: 0 additions & 1 deletion lib/redis_client/cluster/router.rb
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,6 @@ def send_pubsub_command(method, command, args, &block) # rubocop:disable Metrics
end
end

# for redis-rb
def send_watch_command(command)
raise ::RedisClient::Cluster::Transaction::ConsistencyError, 'A block required. And you need to use the block argument as a client for the transaction.' unless block_given?

Expand Down
43 changes: 4 additions & 39 deletions test/redis_client/test_cluster.rb
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,6 @@ def test_transaction_in_race_condition
assert_equal(%w[3 4], @client.call('MGET', '{key}1', '{key}2'))
end

# for redis-rb
def test_transaction_with_dedicated_watch_command
@client.call('MSET', '{key}1', '0', '{key}2', '0')

Expand Down Expand Up @@ -683,6 +682,10 @@ def test_other_pubsub_commands
ps.close
end

def test_with_method
assert_raises(NotImplementedError) { @client.with }
end

def test_dedicated_commands # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
10.times { |i| @client.call('SET', "key#{i}", i) }
wait_for_replication
Expand Down Expand Up @@ -819,44 +822,6 @@ def test_only_reshards_own_errors
client2.close
end

def test_pinning_single_key
got = @client.with(key: 'key1') do |conn|
conn.call('SET', 'key1', 'hello')
conn.call('GET', 'key1')
end
assert_equal('hello', got)
end

def test_pinning_no_key
assert_raises(ArgumentError) do
@client.with {}
end
end

def test_pinning_empty_key
assert_raises(ArgumentError) do
@client.with(key: '') {}
end
end

def test_pinning_two_keys
got = @client.with(hashtag: 'slot') do |conn|
conn.call('SET', '{slot}key1', 'v1')
conn.call('SET', '{slot}key2', 'v2')
conn.call('MGET', '{slot}key1', '{slot}key2')
end
assert_equal(%w[v1 v2], got)
end

def test_pinning_hashtag_with_braces
got = @client.with(hashtag: '{slot}') do |conn|
conn.call('SET', '{slot}key1', 'v1')
conn.call('SET', '{slot}key2', 'v2')
conn.call('MGET', '{slot}key1', '{slot}key2')
end
assert_equal(%w[v1 v2], got)
end

private

def wait_for_replication
Expand Down

0 comments on commit 9a50314

Please sign in to comment.