redis 4.1.0 → 4.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -5
- data/CHANGELOG.md +107 -0
- data/README.md +81 -17
- data/lib/redis/client.rb +137 -82
- data/lib/redis/cluster/command_loader.rb +6 -7
- data/lib/redis/cluster/node.rb +5 -1
- data/lib/redis/cluster/node_key.rb +3 -7
- data/lib/redis/cluster/option.rb +30 -14
- data/lib/redis/cluster/slot.rb +30 -13
- data/lib/redis/cluster/slot_loader.rb +4 -4
- data/lib/redis/cluster.rb +22 -17
- data/lib/redis/connection/command_helper.rb +5 -2
- data/lib/redis/connection/hiredis.rb +4 -3
- data/lib/redis/connection/registry.rb +2 -1
- data/lib/redis/connection/ruby.rb +121 -105
- data/lib/redis/connection/synchrony.rb +9 -4
- data/lib/redis/connection.rb +2 -0
- data/lib/redis/distributed.rb +170 -68
- data/lib/redis/errors.rb +2 -0
- data/lib/redis/hash_ring.rb +15 -14
- data/lib/redis/pipeline.rb +46 -8
- data/lib/redis/subscribe.rb +11 -12
- data/lib/redis/version.rb +3 -1
- data/lib/redis.rb +808 -476
- metadata +15 -25
@@ -1,9 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require_relative "command_helper"
|
2
4
|
require_relative "registry"
|
3
5
|
require_relative "../errors"
|
4
6
|
require "em-synchrony"
|
5
7
|
require "hiredis/reader"
|
6
8
|
|
9
|
+
Kernel.warn(
|
10
|
+
"The redis synchrony driver is deprecated and will be removed in redis-rb 5.0. " \
|
11
|
+
"We're looking for people to maintain it as a separate gem, see https://github.com/redis/redis-rb/issues/915"
|
12
|
+
)
|
13
|
+
|
7
14
|
class Redis
|
8
15
|
module Connection
|
9
16
|
class RedisClient < EventMachine::Connection
|
@@ -46,9 +53,7 @@ class Redis
|
|
46
53
|
|
47
54
|
def read
|
48
55
|
@req = EventMachine::DefaultDeferrable.new
|
49
|
-
if @timeout > 0
|
50
|
-
@req.timeout(@timeout, :timeout)
|
51
|
-
end
|
56
|
+
@req.timeout(@timeout, :timeout) if @timeout > 0
|
52
57
|
EventMachine::Synchrony.sync @req
|
53
58
|
end
|
54
59
|
|
@@ -105,7 +110,7 @@ class Redis
|
|
105
110
|
end
|
106
111
|
|
107
112
|
def connected?
|
108
|
-
@connection
|
113
|
+
@connection&.connected?
|
109
114
|
end
|
110
115
|
|
111
116
|
def timeout=(timeout)
|
data/lib/redis/connection.rb
CHANGED
data/lib/redis/distributed.rb
CHANGED
@@ -1,15 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require_relative "hash_ring"
|
2
4
|
|
3
5
|
class Redis
|
4
6
|
class Distributed
|
5
|
-
|
6
7
|
class CannotDistribute < RuntimeError
|
7
8
|
def initialize(command)
|
8
9
|
@command = command
|
9
10
|
end
|
10
11
|
|
11
12
|
def message
|
12
|
-
"#{@command.to_s.upcase} cannot be used in Redis::Distributed because the keys involved need
|
13
|
+
"#{@command.to_s.upcase} cannot be used in Redis::Distributed because the keys involved need " \
|
14
|
+
"to be on the same server or because we cannot guarantee that the operation will be atomic."
|
13
15
|
end
|
14
16
|
end
|
15
17
|
|
@@ -22,10 +24,14 @@ class Redis
|
|
22
24
|
@default_options = options.dup
|
23
25
|
node_configs.each { |node_config| add_node(node_config) }
|
24
26
|
@subscribed_node = nil
|
27
|
+
@watch_key = nil
|
25
28
|
end
|
26
29
|
|
27
30
|
def node_for(key)
|
28
|
-
|
31
|
+
key = key_tag(key.to_s) || key.to_s
|
32
|
+
raise CannotDistribute, :watch if @watch_key && @watch_key != key
|
33
|
+
|
34
|
+
@ring.get_node(key)
|
29
35
|
end
|
30
36
|
|
31
37
|
def nodes
|
@@ -33,9 +39,9 @@ class Redis
|
|
33
39
|
end
|
34
40
|
|
35
41
|
def add_node(options)
|
36
|
-
options = { :
|
42
|
+
options = { url: options } if options.is_a?(String)
|
37
43
|
options = @default_options.merge(options)
|
38
|
-
@ring.add_node Redis.new(
|
44
|
+
@ring.add_node Redis.new(options)
|
39
45
|
end
|
40
46
|
|
41
47
|
# Change the selected database for the current connection.
|
@@ -144,12 +150,12 @@ class Redis
|
|
144
150
|
end
|
145
151
|
|
146
152
|
# Create a key using the serialized value, previously obtained using DUMP.
|
147
|
-
def restore(key, ttl, serialized_value, options
|
148
|
-
node_for(key).restore(key, ttl, serialized_value, options)
|
153
|
+
def restore(key, ttl, serialized_value, **options)
|
154
|
+
node_for(key).restore(key, ttl, serialized_value, **options)
|
149
155
|
end
|
150
156
|
|
151
157
|
# Transfer a key from the connected instance to another instance.
|
152
|
-
def migrate(
|
158
|
+
def migrate(_key, _options)
|
153
159
|
raise CannotDistribute, :migrate
|
154
160
|
end
|
155
161
|
|
@@ -170,8 +176,33 @@ class Redis
|
|
170
176
|
end
|
171
177
|
|
172
178
|
# Determine if a key exists.
|
173
|
-
def exists(
|
174
|
-
|
179
|
+
def exists(*args)
|
180
|
+
if !Redis.exists_returns_integer && args.size == 1
|
181
|
+
message = "`Redis#exists(key)` will return an Integer in redis-rb 4.3, if you want to keep the old behavior, " \
|
182
|
+
"use `exists?` instead. To opt-in to the new behavior now you can set Redis.exists_returns_integer = true. " \
|
183
|
+
"(#{::Kernel.caller(1, 1).first})\n"
|
184
|
+
|
185
|
+
if defined?(::Warning)
|
186
|
+
::Warning.warn(message)
|
187
|
+
else
|
188
|
+
warn(message)
|
189
|
+
end
|
190
|
+
exists?(*args)
|
191
|
+
else
|
192
|
+
keys_per_node = args.group_by { |key| node_for(key) }
|
193
|
+
keys_per_node.inject(0) do |sum, (node, keys)|
|
194
|
+
sum + node._exists(*keys)
|
195
|
+
end
|
196
|
+
end
|
197
|
+
end
|
198
|
+
|
199
|
+
# Determine if any of the keys exists.
|
200
|
+
def exists?(*args)
|
201
|
+
keys_per_node = args.group_by { |key| node_for(key) }
|
202
|
+
keys_per_node.each do |node, keys|
|
203
|
+
return true if node.exists?(*keys)
|
204
|
+
end
|
205
|
+
false
|
175
206
|
end
|
176
207
|
|
177
208
|
# Find all keys matching the given pattern.
|
@@ -204,11 +235,11 @@ class Redis
|
|
204
235
|
end
|
205
236
|
|
206
237
|
# Sort the elements in a list, set or sorted set.
|
207
|
-
def sort(key, options
|
238
|
+
def sort(key, **options)
|
208
239
|
keys = [key, options[:by], options[:store], *Array(options[:get])].compact
|
209
240
|
|
210
241
|
ensure_same_node(:sort, keys) do |node|
|
211
|
-
node.sort(key, options)
|
242
|
+
node.sort(key, **options)
|
212
243
|
end
|
213
244
|
end
|
214
245
|
|
@@ -243,8 +274,8 @@ class Redis
|
|
243
274
|
end
|
244
275
|
|
245
276
|
# Set the string value of a key.
|
246
|
-
def set(key, value, options
|
247
|
-
node_for(key).set(key, value, options)
|
277
|
+
def set(key, value, **options)
|
278
|
+
node_for(key).set(key, value, **options)
|
248
279
|
end
|
249
280
|
|
250
281
|
# Set the time to live in seconds of a key.
|
@@ -263,20 +294,20 @@ class Redis
|
|
263
294
|
end
|
264
295
|
|
265
296
|
# Set multiple keys to multiple values.
|
266
|
-
def mset(*
|
297
|
+
def mset(*_args)
|
267
298
|
raise CannotDistribute, :mset
|
268
299
|
end
|
269
300
|
|
270
|
-
def mapped_mset(
|
301
|
+
def mapped_mset(_hash)
|
271
302
|
raise CannotDistribute, :mapped_mset
|
272
303
|
end
|
273
304
|
|
274
305
|
# Set multiple keys to multiple values, only if none of the keys exist.
|
275
|
-
def msetnx(*
|
306
|
+
def msetnx(*_args)
|
276
307
|
raise CannotDistribute, :msetnx
|
277
308
|
end
|
278
309
|
|
279
|
-
def mapped_msetnx(
|
310
|
+
def mapped_msetnx(_hash)
|
280
311
|
raise CannotDistribute, :mapped_msetnx
|
281
312
|
end
|
282
313
|
|
@@ -285,6 +316,16 @@ class Redis
|
|
285
316
|
node_for(key).get(key)
|
286
317
|
end
|
287
318
|
|
319
|
+
# Get the value of a key and delete it.
|
320
|
+
def getdel(key)
|
321
|
+
node_for(key).getdel(key)
|
322
|
+
end
|
323
|
+
|
324
|
+
# Get the value of a key and sets its time to live based on options.
|
325
|
+
def getex(key, **options)
|
326
|
+
node_for(key).getex(key, **options)
|
327
|
+
end
|
328
|
+
|
288
329
|
# Get the values of all the given keys as an Array.
|
289
330
|
def mget(*keys)
|
290
331
|
mapped_mget(*keys).values_at(*keys)
|
@@ -335,7 +376,7 @@ class Redis
|
|
335
376
|
end
|
336
377
|
|
337
378
|
# Return the position of the first bit set to 1 or 0 in a string.
|
338
|
-
def bitpos(key, bit, start=nil, stop=nil)
|
379
|
+
def bitpos(key, bit, start = nil, stop = nil)
|
339
380
|
node_for(key).bitpos(key, bit, start, stop)
|
340
381
|
end
|
341
382
|
|
@@ -353,7 +394,7 @@ class Redis
|
|
353
394
|
get(key)
|
354
395
|
end
|
355
396
|
|
356
|
-
def []=(key,value)
|
397
|
+
def []=(key, value)
|
357
398
|
set(key, value)
|
358
399
|
end
|
359
400
|
|
@@ -362,6 +403,21 @@ class Redis
|
|
362
403
|
node_for(key).llen(key)
|
363
404
|
end
|
364
405
|
|
406
|
+
# Remove the first/last element in a list, append/prepend it to another list and return it.
|
407
|
+
def lmove(source, destination, where_source, where_destination)
|
408
|
+
ensure_same_node(:lmove, [source, destination]) do |node|
|
409
|
+
node.lmove(source, destination, where_source, where_destination)
|
410
|
+
end
|
411
|
+
end
|
412
|
+
|
413
|
+
# Remove the first/last element in a list and append/prepend it
|
414
|
+
# to another list and return it, or block until one is available.
|
415
|
+
def blmove(source, destination, where_source, where_destination, timeout: 0)
|
416
|
+
ensure_same_node(:lmove, [source, destination]) do |node|
|
417
|
+
node.blmove(source, destination, where_source, where_destination, timeout: timeout)
|
418
|
+
end
|
419
|
+
end
|
420
|
+
|
365
421
|
# Prepend one or more values to a list.
|
366
422
|
def lpush(key, value)
|
367
423
|
node_for(key).lpush(key, value)
|
@@ -382,14 +438,14 @@ class Redis
|
|
382
438
|
node_for(key).rpushx(key, value)
|
383
439
|
end
|
384
440
|
|
385
|
-
# Remove and get the first
|
386
|
-
def lpop(key)
|
387
|
-
node_for(key).lpop(key)
|
441
|
+
# Remove and get the first elements in a list.
|
442
|
+
def lpop(key, count = nil)
|
443
|
+
node_for(key).lpop(key, count)
|
388
444
|
end
|
389
445
|
|
390
|
-
# Remove and get the last
|
391
|
-
def rpop(key)
|
392
|
-
node_for(key).rpop(key)
|
446
|
+
# Remove and get the last elements in a list.
|
447
|
+
def rpop(key, count = nil)
|
448
|
+
node_for(key).rpop(key, count)
|
393
449
|
end
|
394
450
|
|
395
451
|
# Remove the last element in a list, append it to another list and return
|
@@ -401,13 +457,12 @@ class Redis
|
|
401
457
|
end
|
402
458
|
|
403
459
|
def _bpop(cmd, args)
|
404
|
-
|
405
|
-
|
406
|
-
if args.last.is_a?(Hash)
|
460
|
+
timeout = if args.last.is_a?(Hash)
|
407
461
|
options = args.pop
|
462
|
+
options[:timeout]
|
408
463
|
elsif args.last.respond_to?(:to_int)
|
409
464
|
# Issue deprecation notice in obnoxious mode...
|
410
|
-
|
465
|
+
args.pop.to_int
|
411
466
|
end
|
412
467
|
|
413
468
|
if args.size > 1
|
@@ -417,7 +472,11 @@ class Redis
|
|
417
472
|
keys = args.flatten
|
418
473
|
|
419
474
|
ensure_same_node(cmd, keys) do |node|
|
420
|
-
|
475
|
+
if timeout
|
476
|
+
node.__send__(cmd, keys, timeout: timeout)
|
477
|
+
else
|
478
|
+
node.__send__(cmd, keys)
|
479
|
+
end
|
421
480
|
end
|
422
481
|
end
|
423
482
|
|
@@ -435,15 +494,9 @@ class Redis
|
|
435
494
|
|
436
495
|
# Pop a value from a list, push it to another list and return it; or block
|
437
496
|
# until one is available.
|
438
|
-
def brpoplpush(source, destination,
|
439
|
-
case options
|
440
|
-
when Integer
|
441
|
-
# Issue deprecation notice in obnoxious mode...
|
442
|
-
options = { :timeout => options }
|
443
|
-
end
|
444
|
-
|
497
|
+
def brpoplpush(source, destination, deprecated_timeout = 0, **options)
|
445
498
|
ensure_same_node(:brpoplpush, [source, destination]) do |node|
|
446
|
-
node.brpoplpush(source, destination, options)
|
499
|
+
node.brpoplpush(source, destination, deprecated_timeout, **options)
|
447
500
|
end
|
448
501
|
end
|
449
502
|
|
@@ -514,19 +567,24 @@ class Redis
|
|
514
567
|
node_for(key).sismember(key, member)
|
515
568
|
end
|
516
569
|
|
570
|
+
# Determine if multiple values are members of a set.
|
571
|
+
def smismember(key, *members)
|
572
|
+
node_for(key).smismember(key, *members)
|
573
|
+
end
|
574
|
+
|
517
575
|
# Get all the members in a set.
|
518
576
|
def smembers(key)
|
519
577
|
node_for(key).smembers(key)
|
520
578
|
end
|
521
579
|
|
522
580
|
# Scan a set
|
523
|
-
def sscan(key, cursor, options
|
524
|
-
node_for(key).sscan(key, cursor, options)
|
581
|
+
def sscan(key, cursor, **options)
|
582
|
+
node_for(key).sscan(key, cursor, **options)
|
525
583
|
end
|
526
584
|
|
527
585
|
# Scan a set and return an enumerator
|
528
|
-
def sscan_each(key, options
|
529
|
-
node_for(key).sscan_each(key, options, &block)
|
586
|
+
def sscan_each(key, **options, &block)
|
587
|
+
node_for(key).sscan_each(key, **options, &block)
|
530
588
|
end
|
531
589
|
|
532
590
|
# Subtract multiple sets.
|
@@ -581,6 +639,7 @@ class Redis
|
|
581
639
|
def zadd(key, *args)
|
582
640
|
node_for(key).zadd(key, *args)
|
583
641
|
end
|
642
|
+
ruby2_keywords(:zadd) if respond_to?(:ruby2_keywords, true)
|
584
643
|
|
585
644
|
# Increment the score of a member in a sorted set.
|
586
645
|
def zincrby(key, increment, member)
|
@@ -597,15 +656,25 @@ class Redis
|
|
597
656
|
node_for(key).zscore(key, member)
|
598
657
|
end
|
599
658
|
|
659
|
+
# Get one or more random members from a sorted set.
|
660
|
+
def zrandmember(key, count = nil, **options)
|
661
|
+
node_for(key).zrandmember(key, count, **options)
|
662
|
+
end
|
663
|
+
|
664
|
+
# Get the scores associated with the given members in a sorted set.
|
665
|
+
def zmscore(key, *members)
|
666
|
+
node_for(key).zmscore(key, *members)
|
667
|
+
end
|
668
|
+
|
600
669
|
# Return a range of members in a sorted set, by index.
|
601
|
-
def zrange(key, start, stop, options
|
602
|
-
node_for(key).zrange(key, start, stop, options)
|
670
|
+
def zrange(key, start, stop, **options)
|
671
|
+
node_for(key).zrange(key, start, stop, **options)
|
603
672
|
end
|
604
673
|
|
605
674
|
# Return a range of members in a sorted set, by index, with scores ordered
|
606
675
|
# from high to low.
|
607
|
-
def zrevrange(key, start, stop, options
|
608
|
-
node_for(key).zrevrange(key, start, stop, options)
|
676
|
+
def zrevrange(key, start, stop, **options)
|
677
|
+
node_for(key).zrevrange(key, start, stop, **options)
|
609
678
|
end
|
610
679
|
|
611
680
|
# Determine the index of a member in a sorted set.
|
@@ -625,14 +694,14 @@ class Redis
|
|
625
694
|
end
|
626
695
|
|
627
696
|
# Return a range of members in a sorted set, by score.
|
628
|
-
def zrangebyscore(key, min, max, options
|
629
|
-
node_for(key).zrangebyscore(key, min, max, options)
|
697
|
+
def zrangebyscore(key, min, max, **options)
|
698
|
+
node_for(key).zrangebyscore(key, min, max, **options)
|
630
699
|
end
|
631
700
|
|
632
701
|
# Return a range of members in a sorted set, by score, with scores ordered
|
633
702
|
# from high to low.
|
634
|
-
def zrevrangebyscore(key, max, min, options
|
635
|
-
node_for(key).zrevrangebyscore(key, max, min, options)
|
703
|
+
def zrevrangebyscore(key, max, min, **options)
|
704
|
+
node_for(key).zrevrangebyscore(key, max, min, **options)
|
636
705
|
end
|
637
706
|
|
638
707
|
# Remove all members in a sorted set within the given scores.
|
@@ -645,18 +714,25 @@ class Redis
|
|
645
714
|
node_for(key).zcount(key, min, max)
|
646
715
|
end
|
647
716
|
|
717
|
+
# Get the intersection of multiple sorted sets
|
718
|
+
def zinter(*keys, **options)
|
719
|
+
ensure_same_node(:zinter, keys) do |node|
|
720
|
+
node.zinter(*keys, **options)
|
721
|
+
end
|
722
|
+
end
|
723
|
+
|
648
724
|
# Intersect multiple sorted sets and store the resulting sorted set in a new
|
649
725
|
# key.
|
650
|
-
def zinterstore(destination, keys, options
|
726
|
+
def zinterstore(destination, keys, **options)
|
651
727
|
ensure_same_node(:zinterstore, [destination] + keys) do |node|
|
652
|
-
node.zinterstore(destination, keys, options)
|
728
|
+
node.zinterstore(destination, keys, **options)
|
653
729
|
end
|
654
730
|
end
|
655
731
|
|
656
732
|
# Add multiple sorted sets and store the resulting sorted set in a new key.
|
657
|
-
def zunionstore(destination, keys, options
|
733
|
+
def zunionstore(destination, keys, **options)
|
658
734
|
ensure_same_node(:zunionstore, [destination] + keys) do |node|
|
659
|
-
node.zunionstore(destination, keys, options)
|
735
|
+
node.zunionstore(destination, keys, **options)
|
660
736
|
end
|
661
737
|
end
|
662
738
|
|
@@ -665,9 +741,9 @@ class Redis
|
|
665
741
|
node_for(key).hlen(key)
|
666
742
|
end
|
667
743
|
|
668
|
-
# Set
|
669
|
-
def hset(key,
|
670
|
-
node_for(key).hset(key,
|
744
|
+
# Set multiple hash fields to multiple values.
|
745
|
+
def hset(key, *attrs)
|
746
|
+
node_for(key).hset(key, *attrs)
|
671
747
|
end
|
672
748
|
|
673
749
|
# Set the value of a hash field, only if the field does not exist.
|
@@ -739,7 +815,7 @@ class Redis
|
|
739
815
|
end
|
740
816
|
|
741
817
|
def subscribed?
|
742
|
-
|
818
|
+
!!@subscribed_node
|
743
819
|
end
|
744
820
|
|
745
821
|
# Listen for messages published to the given channels.
|
@@ -757,7 +833,8 @@ class Redis
|
|
757
833
|
|
758
834
|
# Stop listening for messages posted to the given channels.
|
759
835
|
def unsubscribe(*channels)
|
760
|
-
raise
|
836
|
+
raise "Can't unsubscribe if not subscribed." unless subscribed?
|
837
|
+
|
761
838
|
@subscribed_node.unsubscribe(*channels)
|
762
839
|
end
|
763
840
|
|
@@ -773,13 +850,26 @@ class Redis
|
|
773
850
|
end
|
774
851
|
|
775
852
|
# Watch the given keys to determine execution of the MULTI/EXEC block.
|
776
|
-
def watch(*keys)
|
777
|
-
|
853
|
+
def watch(*keys, &block)
|
854
|
+
ensure_same_node(:watch, keys) do |node|
|
855
|
+
@watch_key = key_tag(keys.first) || keys.first.to_s
|
856
|
+
|
857
|
+
begin
|
858
|
+
node.watch(*keys, &block)
|
859
|
+
rescue StandardError
|
860
|
+
@watch_key = nil
|
861
|
+
raise
|
862
|
+
end
|
863
|
+
end
|
778
864
|
end
|
779
865
|
|
780
866
|
# Forget about all watched keys.
|
781
867
|
def unwatch
|
782
|
-
raise CannotDistribute, :unwatch
|
868
|
+
raise CannotDistribute, :unwatch unless @watch_key
|
869
|
+
|
870
|
+
result = node_for(@watch_key).unwatch
|
871
|
+
@watch_key = nil
|
872
|
+
result
|
783
873
|
end
|
784
874
|
|
785
875
|
def pipelined
|
@@ -787,18 +877,30 @@ class Redis
|
|
787
877
|
end
|
788
878
|
|
789
879
|
# Mark the start of a transaction block.
|
790
|
-
def multi
|
791
|
-
raise CannotDistribute, :multi
|
880
|
+
def multi(&block)
|
881
|
+
raise CannotDistribute, :multi unless @watch_key
|
882
|
+
|
883
|
+
result = node_for(@watch_key).multi(&block)
|
884
|
+
@watch_key = nil if block_given?
|
885
|
+
result
|
792
886
|
end
|
793
887
|
|
794
888
|
# Execute all commands issued after MULTI.
|
795
889
|
def exec
|
796
|
-
raise CannotDistribute, :exec
|
890
|
+
raise CannotDistribute, :exec unless @watch_key
|
891
|
+
|
892
|
+
result = node_for(@watch_key).exec
|
893
|
+
@watch_key = nil
|
894
|
+
result
|
797
895
|
end
|
798
896
|
|
799
897
|
# Discard all commands issued after MULTI.
|
800
898
|
def discard
|
801
|
-
raise CannotDistribute, :discard
|
899
|
+
raise CannotDistribute, :discard unless @watch_key
|
900
|
+
|
901
|
+
result = node_for(@watch_key).discard
|
902
|
+
@watch_key = nil
|
903
|
+
result
|
802
904
|
end
|
803
905
|
|
804
906
|
# Control remote script registry.
|
@@ -857,7 +959,7 @@ class Redis
|
|
857
959
|
self.class.new(@node_configs, @default_options)
|
858
960
|
end
|
859
961
|
|
860
|
-
|
962
|
+
protected
|
861
963
|
|
862
964
|
def on_each_node(command, *args)
|
863
965
|
nodes.map do |node|
|
data/lib/redis/errors.rb
CHANGED
data/lib/redis/hash_ring.rb
CHANGED
@@ -1,8 +1,9 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'zlib'
|
2
4
|
|
3
5
|
class Redis
|
4
6
|
class HashRing
|
5
|
-
|
6
7
|
POINTS_PER_SERVER = 160 # this is the default in libmemcached
|
7
8
|
|
8
9
|
attr_reader :ring, :sorted_keys, :replicas, :nodes
|
@@ -10,7 +11,7 @@ class Redis
|
|
10
11
|
# nodes is a list of objects that have a proper to_s representation.
|
11
12
|
# replicas indicates how many virtual points should be used pr. node,
|
12
13
|
# replicas are required to improve the distribution.
|
13
|
-
def initialize(nodes=[], replicas=POINTS_PER_SERVER)
|
14
|
+
def initialize(nodes = [], replicas = POINTS_PER_SERVER)
|
14
15
|
@replicas = replicas
|
15
16
|
@ring = {}
|
16
17
|
@nodes = []
|
@@ -32,11 +33,11 @@ class Redis
|
|
32
33
|
end
|
33
34
|
|
34
35
|
def remove_node(node)
|
35
|
-
@nodes.reject!{|n| n.id == node.id}
|
36
|
+
@nodes.reject! { |n| n.id == node.id }
|
36
37
|
@replicas.times do |i|
|
37
38
|
key = Zlib.crc32("#{node.id}:#{i}")
|
38
39
|
@ring.delete(key)
|
39
|
-
@sorted_keys.reject! {|k| k == key}
|
40
|
+
@sorted_keys.reject! { |k| k == key }
|
40
41
|
end
|
41
42
|
end
|
42
43
|
|
@@ -46,27 +47,29 @@ class Redis
|
|
46
47
|
end
|
47
48
|
|
48
49
|
def get_node_pos(key)
|
49
|
-
return [nil,nil] if @ring.
|
50
|
+
return [nil, nil] if @ring.empty?
|
51
|
+
|
50
52
|
crc = Zlib.crc32(key)
|
51
53
|
idx = HashRing.binary_search(@sorted_keys, crc)
|
52
|
-
|
54
|
+
[@ring[@sorted_keys[idx]], idx]
|
53
55
|
end
|
54
56
|
|
55
57
|
def iter_nodes(key)
|
56
|
-
return [nil,nil] if @ring.
|
58
|
+
return [nil, nil] if @ring.empty?
|
59
|
+
|
57
60
|
_, pos = get_node_pos(key)
|
58
61
|
@ring.size.times do |n|
|
59
|
-
yield @ring[@sorted_keys[(pos+n) % @ring.size]]
|
62
|
+
yield @ring[@sorted_keys[(pos + n) % @ring.size]]
|
60
63
|
end
|
61
64
|
end
|
62
65
|
|
63
66
|
# Find the closest index in HashRing with value <= the given value
|
64
|
-
def self.binary_search(ary, value
|
67
|
+
def self.binary_search(ary, value)
|
65
68
|
upper = ary.size - 1
|
66
69
|
lower = 0
|
67
70
|
idx = 0
|
68
71
|
|
69
|
-
while
|
72
|
+
while lower <= upper
|
70
73
|
idx = (lower + upper) / 2
|
71
74
|
comp = ary[idx] <=> value
|
72
75
|
|
@@ -79,10 +82,8 @@ class Redis
|
|
79
82
|
end
|
80
83
|
end
|
81
84
|
|
82
|
-
if upper < 0
|
83
|
-
|
84
|
-
end
|
85
|
-
return upper
|
85
|
+
upper = ary.size - 1 if upper < 0
|
86
|
+
upper
|
86
87
|
end
|
87
88
|
end
|
88
89
|
end
|