redis 3.3.5 → 4.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (126) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +54 -2
  3. data/README.md +77 -76
  4. data/lib/redis.rb +779 -63
  5. data/lib/redis/client.rb +41 -20
  6. data/lib/redis/cluster.rb +286 -0
  7. data/lib/redis/cluster/command.rb +81 -0
  8. data/lib/redis/cluster/command_loader.rb +34 -0
  9. data/lib/redis/cluster/key_slot_converter.rb +72 -0
  10. data/lib/redis/cluster/node.rb +104 -0
  11. data/lib/redis/cluster/node_key.rb +35 -0
  12. data/lib/redis/cluster/node_loader.rb +37 -0
  13. data/lib/redis/cluster/option.rb +77 -0
  14. data/lib/redis/cluster/slot.rb +69 -0
  15. data/lib/redis/cluster/slot_loader.rb +49 -0
  16. data/lib/redis/connection.rb +2 -2
  17. data/lib/redis/connection/command_helper.rb +2 -8
  18. data/lib/redis/connection/hiredis.rb +2 -2
  19. data/lib/redis/connection/ruby.rb +13 -30
  20. data/lib/redis/connection/synchrony.rb +12 -4
  21. data/lib/redis/distributed.rb +32 -12
  22. data/lib/redis/errors.rb +46 -0
  23. data/lib/redis/hash_ring.rb +20 -64
  24. data/lib/redis/pipeline.rb +9 -7
  25. data/lib/redis/version.rb +1 -1
  26. metadata +53 -196
  27. data/.gitignore +0 -16
  28. data/.travis.yml +0 -89
  29. data/.travis/Gemfile +0 -11
  30. data/.yardopts +0 -3
  31. data/Gemfile +0 -4
  32. data/Rakefile +0 -87
  33. data/benchmarking/logging.rb +0 -71
  34. data/benchmarking/pipeline.rb +0 -51
  35. data/benchmarking/speed.rb +0 -21
  36. data/benchmarking/suite.rb +0 -24
  37. data/benchmarking/worker.rb +0 -71
  38. data/examples/basic.rb +0 -15
  39. data/examples/consistency.rb +0 -114
  40. data/examples/dist_redis.rb +0 -43
  41. data/examples/incr-decr.rb +0 -17
  42. data/examples/list.rb +0 -26
  43. data/examples/pubsub.rb +0 -37
  44. data/examples/sentinel.rb +0 -41
  45. data/examples/sentinel/start +0 -49
  46. data/examples/sets.rb +0 -36
  47. data/examples/unicorn/config.ru +0 -3
  48. data/examples/unicorn/unicorn.rb +0 -20
  49. data/redis.gemspec +0 -44
  50. data/test/bitpos_test.rb +0 -69
  51. data/test/blocking_commands_test.rb +0 -42
  52. data/test/client_test.rb +0 -59
  53. data/test/command_map_test.rb +0 -30
  54. data/test/commands_on_hashes_test.rb +0 -21
  55. data/test/commands_on_hyper_log_log_test.rb +0 -21
  56. data/test/commands_on_lists_test.rb +0 -20
  57. data/test/commands_on_sets_test.rb +0 -77
  58. data/test/commands_on_sorted_sets_test.rb +0 -137
  59. data/test/commands_on_strings_test.rb +0 -101
  60. data/test/commands_on_value_types_test.rb +0 -133
  61. data/test/connection_handling_test.rb +0 -277
  62. data/test/connection_test.rb +0 -57
  63. data/test/distributed_blocking_commands_test.rb +0 -46
  64. data/test/distributed_commands_on_hashes_test.rb +0 -10
  65. data/test/distributed_commands_on_hyper_log_log_test.rb +0 -33
  66. data/test/distributed_commands_on_lists_test.rb +0 -22
  67. data/test/distributed_commands_on_sets_test.rb +0 -83
  68. data/test/distributed_commands_on_sorted_sets_test.rb +0 -18
  69. data/test/distributed_commands_on_strings_test.rb +0 -59
  70. data/test/distributed_commands_on_value_types_test.rb +0 -95
  71. data/test/distributed_commands_requiring_clustering_test.rb +0 -164
  72. data/test/distributed_connection_handling_test.rb +0 -23
  73. data/test/distributed_internals_test.rb +0 -79
  74. data/test/distributed_key_tags_test.rb +0 -52
  75. data/test/distributed_persistence_control_commands_test.rb +0 -26
  76. data/test/distributed_publish_subscribe_test.rb +0 -92
  77. data/test/distributed_remote_server_control_commands_test.rb +0 -66
  78. data/test/distributed_scripting_test.rb +0 -102
  79. data/test/distributed_sorting_test.rb +0 -20
  80. data/test/distributed_test.rb +0 -58
  81. data/test/distributed_transactions_test.rb +0 -32
  82. data/test/encoding_test.rb +0 -18
  83. data/test/error_replies_test.rb +0 -59
  84. data/test/fork_safety_test.rb +0 -65
  85. data/test/helper.rb +0 -232
  86. data/test/helper_test.rb +0 -24
  87. data/test/internals_test.rb +0 -417
  88. data/test/lint/blocking_commands.rb +0 -150
  89. data/test/lint/hashes.rb +0 -162
  90. data/test/lint/hyper_log_log.rb +0 -60
  91. data/test/lint/lists.rb +0 -143
  92. data/test/lint/sets.rb +0 -140
  93. data/test/lint/sorted_sets.rb +0 -316
  94. data/test/lint/strings.rb +0 -260
  95. data/test/lint/value_types.rb +0 -122
  96. data/test/persistence_control_commands_test.rb +0 -26
  97. data/test/pipelining_commands_test.rb +0 -242
  98. data/test/publish_subscribe_test.rb +0 -282
  99. data/test/remote_server_control_commands_test.rb +0 -118
  100. data/test/scanning_test.rb +0 -413
  101. data/test/scripting_test.rb +0 -78
  102. data/test/sentinel_command_test.rb +0 -80
  103. data/test/sentinel_test.rb +0 -255
  104. data/test/sorting_test.rb +0 -59
  105. data/test/ssl_test.rb +0 -73
  106. data/test/support/connection/hiredis.rb +0 -1
  107. data/test/support/connection/ruby.rb +0 -1
  108. data/test/support/connection/synchrony.rb +0 -17
  109. data/test/support/redis_mock.rb +0 -130
  110. data/test/support/ssl/gen_certs.sh +0 -31
  111. data/test/support/ssl/trusted-ca.crt +0 -25
  112. data/test/support/ssl/trusted-ca.key +0 -27
  113. data/test/support/ssl/trusted-cert.crt +0 -81
  114. data/test/support/ssl/trusted-cert.key +0 -28
  115. data/test/support/ssl/untrusted-ca.crt +0 -26
  116. data/test/support/ssl/untrusted-ca.key +0 -27
  117. data/test/support/ssl/untrusted-cert.crt +0 -82
  118. data/test/support/ssl/untrusted-cert.key +0 -28
  119. data/test/support/wire/synchrony.rb +0 -24
  120. data/test/support/wire/thread.rb +0 -5
  121. data/test/synchrony_driver.rb +0 -88
  122. data/test/test.conf.erb +0 -9
  123. data/test/thread_safety_test.rb +0 -62
  124. data/test/transactions_test.rb +0 -264
  125. data/test/unknown_commands_test.rb +0 -14
  126. data/test/url_param_test.rb +0 -138
@@ -1,71 +0,0 @@
1
- # Run with
2
- #
3
- # $ ruby -Ilib benchmarking/logging.rb
4
- #
5
-
6
- begin
7
- require "bench"
8
- rescue LoadError
9
- $stderr.puts "`gem install bench` and try again."
10
- exit 1
11
- end
12
-
13
- require "redis"
14
- require "logger"
15
-
16
- def log(level, namespace = nil)
17
- logger = (namespace || Kernel).const_get(:Logger).new("/dev/null")
18
- logger.level = (namespace || Logger).const_get(level)
19
- logger
20
- end
21
-
22
- def stress(redis)
23
- redis.flushdb
24
-
25
- n = (ARGV.shift || 2000).to_i
26
-
27
- n.times do |i|
28
- key = "foo:#{i}"
29
- redis.set key, i
30
- redis.get key
31
- end
32
- end
33
-
34
- default = Redis.new
35
-
36
- logging_redises = [
37
- Redis.new(:logger => log(:DEBUG)),
38
- Redis.new(:logger => log(:INFO)),
39
- ]
40
-
41
- begin
42
- require "log4r"
43
-
44
- logging_redises += [
45
- Redis.new(:logger => log(:DEBUG, Log4r)),
46
- Redis.new(:logger => log(:INFO, Log4r)),
47
- ]
48
- rescue LoadError
49
- $stderr.puts "Log4r not installed. `gem install log4r` if you want to compare it against Ruby's Logger (spoiler: it's much faster)."
50
- end
51
-
52
- benchmark "Default options (no logger)" do
53
- stress(default)
54
- end
55
-
56
- logging_redises.each do |redis|
57
- logger = redis.client.logger
58
-
59
- case logger
60
- when Logger
61
- level = Logger::SEV_LABEL[logger.level]
62
- when Log4r::Logger
63
- level = logger.levels[logger.level]
64
- end
65
-
66
- benchmark "#{logger.class} on #{level}" do
67
- stress(redis)
68
- end
69
- end
70
-
71
- run 10
@@ -1,51 +0,0 @@
1
- require "benchmark"
2
-
3
- $:.push File.join(File.dirname(__FILE__), 'lib')
4
-
5
- require 'redis'
6
-
7
- ITERATIONS = 10000
8
-
9
- @r = Redis.new
10
-
11
- Benchmark.bmbm do |benchmark|
12
- benchmark.report("set") do
13
- @r.flushdb
14
-
15
- ITERATIONS.times do |i|
16
- @r.set("foo#{i}", "Hello world!")
17
- @r.get("foo#{i}")
18
- end
19
- end
20
-
21
- benchmark.report("set (pipelined)") do
22
- @r.flushdb
23
-
24
- @r.pipelined do
25
- ITERATIONS.times do |i|
26
- @r.set("foo#{i}", "Hello world!")
27
- @r.get("foo#{i}")
28
- end
29
- end
30
- end
31
-
32
- benchmark.report("lpush+ltrim") do
33
- @r.flushdb
34
-
35
- ITERATIONS.times do |i|
36
- @r.lpush "lpush#{i}", i
37
- @r.ltrim "ltrim#{i}", 0, 30
38
- end
39
- end
40
-
41
- benchmark.report("lpush+ltrim (pipelined)") do
42
- @r.flushdb
43
-
44
- @r.pipelined do
45
- ITERATIONS.times do |i|
46
- @r.lpush "lpush#{i}", i
47
- @r.ltrim "ltrim#{i}", 0, 30
48
- end
49
- end
50
- end
51
- end
@@ -1,21 +0,0 @@
1
- # Run with
2
- #
3
- # $ ruby -Ilib benchmarking/speed.rb
4
- #
5
-
6
- require "benchmark"
7
- require "redis"
8
-
9
- r = Redis.new
10
- n = (ARGV.shift || 20000).to_i
11
-
12
- elapsed = Benchmark.realtime do
13
- # n sets, n gets
14
- n.times do |i|
15
- key = "foo#{i}"
16
- r[key] = key * 10
17
- r[key]
18
- end
19
- end
20
-
21
- puts '%.2f Kops' % (2 * n / 1000 / elapsed)
@@ -1,24 +0,0 @@
1
- require 'fileutils'
2
-
3
- def run_in_background(command)
4
- fork { system command }
5
- end
6
-
7
- def with_all_segments(&block)
8
- 0.upto(9) do |segment_number|
9
- block_size = 100000
10
- start_index = segment_number * block_size
11
- end_index = start_index + block_size - 1
12
- block.call(start_index, end_index)
13
- end
14
- end
15
-
16
- #with_all_segments do |start_index, end_index|
17
- # puts "Initializing keys from #{start_index} to #{end_index}"
18
- # system "ruby worker.rb initialize #{start_index} #{end_index} 0"
19
- #end
20
-
21
- with_all_segments do |start_index, end_index|
22
- run_in_background "ruby worker.rb write #{start_index} #{end_index} 10"
23
- run_in_background "ruby worker.rb read #{start_index} #{end_index} 1"
24
- end
@@ -1,71 +0,0 @@
1
- BENCHMARK_ROOT = File.dirname(__FILE__)
2
- REDIS_ROOT = File.join(BENCHMARK_ROOT, "..", "lib")
3
-
4
- $: << REDIS_ROOT
5
- require 'redis'
6
- require 'benchmark'
7
-
8
- def show_usage
9
- puts <<-EOL
10
- Usage: worker.rb [read:write] <start_index> <end_index> <sleep_msec>
11
- EOL
12
- end
13
-
14
- def shift_from_argv
15
- value = ARGV.shift
16
- unless value
17
- show_usage
18
- exit -1
19
- end
20
- value
21
- end
22
-
23
- operation = shift_from_argv.to_sym
24
- start_index = shift_from_argv.to_i
25
- end_index = shift_from_argv.to_i
26
- sleep_msec = shift_from_argv.to_i
27
- sleep_duration = sleep_msec/1000.0
28
-
29
- redis = Redis.new
30
-
31
- case operation
32
- when :initialize
33
-
34
- start_index.upto(end_index) do |i|
35
- redis[i] = 0
36
- end
37
-
38
- when :clear
39
-
40
- start_index.upto(end_index) do |i|
41
- redis.delete(i)
42
- end
43
-
44
- when :read, :write
45
-
46
- puts "Starting to #{operation} at segment #{end_index + 1}"
47
-
48
- loop do
49
- t1 = Time.now
50
- start_index.upto(end_index) do |i|
51
- case operation
52
- when :read
53
- redis.get(i)
54
- when :write
55
- redis.incr(i)
56
- else
57
- raise "Unknown operation: #{operation}"
58
- end
59
- sleep sleep_duration
60
- end
61
- t2 = Time.now
62
-
63
- requests_processed = end_index - start_index
64
- time = t2 - t1
65
- puts "#{t2.strftime("%H:%M")} [segment #{end_index + 1}] : Processed #{requests_processed} requests in #{time} seconds - #{(requests_processed/time).round} requests/sec"
66
- end
67
-
68
- else
69
- raise "Unknown operation: #{operation}"
70
- end
71
-
@@ -1,15 +0,0 @@
1
- require 'redis'
2
-
3
- r = Redis.new
4
-
5
- r.del('foo')
6
-
7
- puts
8
-
9
- p'set foo to "bar"'
10
- r['foo'] = 'bar'
11
-
12
- puts
13
-
14
- p 'value of foo'
15
- p r['foo']
@@ -1,114 +0,0 @@
1
- # This file implements a simple consistency test for Redis-rb (or any other
2
- # Redis environment if you pass a different client object) where a client
3
- # writes to the database using INCR in order to increment keys, but actively
4
- # remember the value the key should have. Before every write a read is performed
5
- # to check if the value in the database matches the value expected.
6
- #
7
- # In this way this program can check for lost writes, or acknowledged writes
8
- # that were executed.
9
- #
10
- # Copyright (C) 2013-2014 Salvatore Sanfilippo <antirez@gmail.com>
11
- #
12
- # Permission is hereby granted, free of charge, to any person obtaining
13
- # a copy of this software and associated documentation files (the
14
- # "Software"), to deal in the Software without restriction, including
15
- # without limitation the rights to use, copy, modify, merge, publish,
16
- # distribute, sublicense, and/or sell copies of the Software, and to
17
- # permit persons to whom the Software is furnished to do so, subject to
18
- # the following conditions:
19
- #
20
- # The above copyright notice and this permission notice shall be
21
- # included in all copies or substantial portions of the Software.
22
- #
23
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24
- # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25
- # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26
- # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27
- # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28
- # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29
- # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30
-
31
- require 'redis'
32
-
33
- class ConsistencyTester
34
- def initialize(redis)
35
- @r = redis
36
- @working_set = 10000
37
- @keyspace = 100000
38
- @writes = 0
39
- @reads = 0
40
- @failed_writes = 0
41
- @failed_reads = 0
42
- @lost_writes = 0
43
- @not_ack_writes = 0
44
- @delay = 0
45
- @cached = {} # We take our view of data stored in the DB.
46
- @prefix = [Process.pid.to_s,Time.now.usec,@r.object_id,""].join("|")
47
- @errtime = {}
48
- end
49
-
50
- def genkey
51
- # Write more often to a small subset of keys
52
- ks = rand() > 0.5 ? @keyspace : @working_set
53
- @prefix+"key_"+rand(ks).to_s
54
- end
55
-
56
- def check_consistency(key,value)
57
- expected = @cached[key]
58
- return if !expected # We lack info about previous state.
59
- if expected > value
60
- @lost_writes += expected-value
61
- elsif expected < value
62
- @not_ack_writes += value-expected
63
- end
64
- end
65
-
66
- def puterr(msg)
67
- if !@errtime[msg] || Time.now.to_i != @errtime[msg]
68
- puts msg
69
- end
70
- @errtime[msg] = Time.now.to_i
71
- end
72
-
73
- def test
74
- last_report = Time.now.to_i
75
- while true
76
- # Read
77
- key = genkey
78
- begin
79
- val = @r.get(key)
80
- check_consistency(key,val.to_i)
81
- @reads += 1
82
- rescue => e
83
- puterr "Reading: #{e.class}: #{e.message} (#{e.backtrace.first})"
84
- @failed_reads += 1
85
- end
86
-
87
- # Write
88
- begin
89
- @cached[key] = @r.incr(key).to_i
90
- @writes += 1
91
- rescue => e
92
- puterr "Writing: #{e.class}: #{e.message} (#{e.backtrace.first})"
93
- @failed_writes += 1
94
- end
95
-
96
- # Report
97
- sleep @delay
98
- if Time.now.to_i != last_report
99
- report = "#{@reads} R (#{@failed_reads} err) | " +
100
- "#{@writes} W (#{@failed_writes} err) | "
101
- report += "#{@lost_writes} lost | " if @lost_writes > 0
102
- report += "#{@not_ack_writes} noack | " if @not_ack_writes > 0
103
- last_report = Time.now.to_i
104
- puts report
105
- end
106
- end
107
- end
108
- end
109
-
110
- Sentinels = [{:host => "127.0.0.1", :port => 26379},
111
- {:host => "127.0.0.1", :port => 26380}]
112
- r = Redis.new(:url => "redis://master1", :sentinels => Sentinels, :role => :master)
113
- tester = ConsistencyTester.new(r)
114
- tester.test
@@ -1,43 +0,0 @@
1
- require "redis"
2
- require "redis/distributed"
3
-
4
- r = Redis::Distributed.new %w[redis://localhost:6379 redis://localhost:6380 redis://localhost:6381 redis://localhost:6382]
5
-
6
- r.flushdb
7
-
8
- r['urmom'] = 'urmom'
9
- r['urdad'] = 'urdad'
10
- r['urmom1'] = 'urmom1'
11
- r['urdad1'] = 'urdad1'
12
- r['urmom2'] = 'urmom2'
13
- r['urdad2'] = 'urdad2'
14
- r['urmom3'] = 'urmom3'
15
- r['urdad3'] = 'urdad3'
16
- p r['urmom']
17
- p r['urdad']
18
- p r['urmom1']
19
- p r['urdad1']
20
- p r['urmom2']
21
- p r['urdad2']
22
- p r['urmom3']
23
- p r['urdad3']
24
-
25
- r.rpush 'listor', 'foo1'
26
- r.rpush 'listor', 'foo2'
27
- r.rpush 'listor', 'foo3'
28
- r.rpush 'listor', 'foo4'
29
- r.rpush 'listor', 'foo5'
30
-
31
- p r.rpop('listor')
32
- p r.rpop('listor')
33
- p r.rpop('listor')
34
- p r.rpop('listor')
35
- p r.rpop('listor')
36
-
37
- puts "key distribution:"
38
-
39
- r.ring.nodes.each do |node|
40
- p [node.client, node.keys("*")]
41
- end
42
- r.flushdb
43
- p r.keys('*')
@@ -1,17 +0,0 @@
1
- require 'redis'
2
-
3
- r = Redis.new
4
-
5
- puts
6
- p 'incr'
7
- r.del 'counter'
8
-
9
- p r.incr('counter')
10
- p r.incr('counter')
11
- p r.incr('counter')
12
-
13
- puts
14
- p 'decr'
15
- p r.decr('counter')
16
- p r.decr('counter')
17
- p r.decr('counter')
@@ -1,26 +0,0 @@
1
- require 'rubygems'
2
- require 'redis'
3
-
4
- r = Redis.new
5
-
6
- r.del 'logs'
7
-
8
- puts
9
-
10
- p "pushing log messages into a LIST"
11
- r.rpush 'logs', 'some log message'
12
- r.rpush 'logs', 'another log message'
13
- r.rpush 'logs', 'yet another log message'
14
- r.rpush 'logs', 'also another log message'
15
-
16
- puts
17
- p 'contents of logs LIST'
18
-
19
- p r.lrange('logs', 0, -1)
20
-
21
- puts
22
- p 'Trim logs LIST to last 2 elements(easy circular buffer)'
23
-
24
- r.ltrim('logs', -2, -1)
25
-
26
- p r.lrange('logs', 0, -1)