redis_cluster 0.2.1 → 0.2.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1c41d685d5abe6bef5a4df7003ef79e2b38c14c3
4
- data.tar.gz: 6b4394d762352ad8985b4d2d37b61a7085286758
3
+ metadata.gz: f73539f72f1afc171f22acfd295dbf491f745ec2
4
+ data.tar.gz: 25c3ede9fa06f5402d21d3aa5b3aff8df64f77a7
5
5
  SHA512:
6
- metadata.gz: 4ce9f3781fc3b9cdd11fc0cb59940e3781f7ca6338dd922bf0738f238b3242b62fc2875b48a41ad3c0fe64a4ffc3be776887b41d4fe65581a48ce1f4b44e9c6d
7
- data.tar.gz: d9127f2e680ab9481365aa3b831551294d57ee1e152ae3a9098af5baa7b5d81ce178f7fd0daccf0431ff07eac5220f5d3f09cfe5a51b56eb073d19a3339dba5a
6
+ metadata.gz: 10437f66a1ca4fe5190d388fddc2e1619ca0fdb1db0c12506ce7cdeb43cd7fe37dba92b70b0a5e98ae323bb96143ec10162c9ef4d13bddd094af56387f250df5
7
+ data.tar.gz: 66e1268bfbd524926b61474c13d98bfb249eb7a2bfd71b4c1e6bac0c1ce59bd8b84721ce5a3e520f55779cbcd216e200f37b0418d4eecd9458cccd048dcd690a
data/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  RedisCluster for ruby is rewrited from [https://github.com/antirez/redis-rb-cluster](https://github.com/antirez/redis-rb-cluster)
4
4
 
5
- Now is developing, and not use in any production environments. Welcome to test and contribute codes!
5
+ Now is developing, only support single node methods, and not use in any production environments.
6
6
 
7
7
 
8
8
  ## Installation
@@ -26,12 +26,39 @@ Or install it yourself as:
26
26
  Doing!
27
27
 
28
28
  ```ruby
29
- hosts = [{host: 127.0.0.1, port: 7000}, {host: 127.0.0.1, port: 7001}]
29
+ hosts = [{host: '127.0.0.1', port: 7000}, {host: '127.0.0.1', port: 7001}] # don't need all, gem can auto detect all nodes, and process failover if some master nodes down
30
30
  rs = RedisCluster.new hosts
31
31
  rs.set "test", 1
32
32
  rs.get "test"
33
33
  ```
34
34
 
35
+ ## Benchmark test
36
+
37
+ A simple benchmark at my macbook, start 4 master nodes (and 4 cold slave nodes), running with one ruby process.
38
+ This only testing redis_cluster can work, not for redis Performance. When I fork 8 ruby process same time and run get command,redis can run 80,000 - 110,000 times per second at my macbook.
39
+
40
+
41
+ ```ruby
42
+ Benchmark.bm do |x|
43
+ x.report do
44
+ 1.upto(100_000).each do |i|
45
+ redis.get "test#{i}"
46
+ end
47
+ end
48
+ x.report do
49
+ 1.upto(100_000).each do |i|
50
+ redis.set "test#{i}", i
51
+ end
52
+ end
53
+ x.report do
54
+ 1.upto(100_000).each do |i|
55
+ redis.del "test#{i}"
56
+ end
57
+ end
58
+ end
59
+ ```
60
+
61
+
35
62
  ## Development
36
63
 
37
64
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
data/lib/redis_cluster.rb CHANGED
@@ -23,3 +23,4 @@ require "redis_cluster/node"
23
23
  require "redis_cluster/pool"
24
24
  require "redis_cluster/slot"
25
25
  require "redis_cluster/crc16"
26
+ require "redis_cluster/errors"
@@ -1,3 +1,5 @@
1
+ require "thread"
2
+
1
3
  module RedisCluster
2
4
 
3
5
  class Client
@@ -5,7 +7,7 @@ module RedisCluster
5
7
  def initialize(startup_hosts, global_configs = {})
6
8
  @startup_hosts = startup_hosts
7
9
  @pool = Pool.new
8
- @locking = false
10
+ @mutex = Mutex.new
9
11
  reload_pool_nodes
10
12
  end
11
13
 
@@ -48,24 +50,23 @@ module RedisCluster
48
50
  private
49
51
 
50
52
  def reload_pool_nodes
51
- return if @locking
52
- @locking = true
53
- @startup_hosts.each do |options|
54
- begin
55
- redis = Node.redis(options)
56
- slots_mapping = redis.cluster("slots").group_by{|x| x[2]}
57
- @pool.delete_except!(slots_mapping.keys)
58
- slots_mapping.each do |host, infos|
59
- slots_ranges = infos.map {|x| x[0]..x[1] }
60
- @pool.add_node!({host: host[0], port: host[1]}, slots_ranges)
53
+ @mutex.synchronize do
54
+ @startup_hosts.each do |options|
55
+ begin
56
+ redis = Node.redis(options)
57
+ slots_mapping = redis.cluster("slots").group_by{|x| x[2]}
58
+ @pool.delete_except!(slots_mapping.keys)
59
+ slots_mapping.each do |host, infos|
60
+ slots_ranges = infos.map {|x| x[0]..x[1] }
61
+ @pool.add_node!({host: host[0], port: host[1]}, slots_ranges)
62
+ end
63
+ rescue
64
+ next
61
65
  end
62
- rescue
63
- next
66
+ break
64
67
  end
65
- break
68
+ fresh_startup_nodes
66
69
  end
67
- @locking = false
68
- fresh_startup_nodes
69
70
  end
70
71
 
71
72
  def fresh_startup_nodes
@@ -12,7 +12,7 @@ module RedisCluster
12
12
  hmset mapped_hmset hget hmget mapped_hmget hdel hexists hincrby hincrbyfloat hkeys hvals hgetall publish pfadd
13
13
  )
14
14
 
15
- # TODO: methods for each node
15
+ SUPPORT_MULTI_NODE_METHODS = %w(keys)
16
16
  end
17
17
 
18
18
  end
@@ -0,0 +1,5 @@
1
+ module RedisCluster
2
+
3
+ NotSupportError = Class.new StandardError
4
+
5
+ end
@@ -24,14 +24,20 @@ module RedisCluster
24
24
  # asking
25
25
  # random_node
26
26
  def execute(method, args, other_options)
27
+ return keys(args.first) if Configuration::SUPPORT_MULTI_NODE_METHODS.include?(method.to_s)
28
+
27
29
  key = key_by_command(method, args)
28
- raise "not usable" if key.nil?
30
+ raise NotSupportError if key.nil?
29
31
 
30
32
  node = other_options[:random_node] ? random_node : node_by(key)
31
33
  node.asking if other_options[:asking]
32
34
  node.execute(method, args)
33
35
  end
34
36
 
37
+ def keys(glob = "*")
38
+ on_each_node(:keys, glob).flatten
39
+ end
40
+
35
41
  private
36
42
 
37
43
  def node_by(key)
@@ -52,6 +58,12 @@ module RedisCluster
52
58
  end
53
59
  end
54
60
 
61
+ def on_each_node(method, *args)
62
+ @nodes.map do |node|
63
+ node.execute(method, args)
64
+ end
65
+ end
66
+
55
67
  end # end pool
56
68
 
57
69
  end
@@ -1,3 +1,3 @@
1
1
  module RedisCluster
2
- VERSION = "0.2.1"
2
+ VERSION = "0.2.3"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: redis_cluster
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - wangzc
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-05-11 00:00:00.000000000 Z
11
+ date: 2016-05-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: redis
@@ -99,6 +99,7 @@ files:
99
99
  - lib/redis_cluster/client.rb
100
100
  - lib/redis_cluster/configuration.rb
101
101
  - lib/redis_cluster/crc16.rb
102
+ - lib/redis_cluster/errors.rb
102
103
  - lib/redis_cluster/node.rb
103
104
  - lib/redis_cluster/pool.rb
104
105
  - lib/redis_cluster/slot.rb