Skip to content
Browse files

RedisCloud -> DistRedis

  • Loading branch information...
1 parent df243e2 commit 98d287d90d86842c39e09beac80bd19989b02dc2 Ezra Zygmuntowicz committed
Showing with 53 additions and 9 deletions.
  1. +50 −4 lib/{redis_cloud.rb → dist_redis.rb}
  2. +2 −3 lib/hash_ring.rb
  3. +1 −2 lib/redis.rb
View
54 lib/redis_cloud.rb → lib/dist_redis.rb
@@ -1,6 +1,6 @@
require 'redis'
require 'hash_ring'
-class RedisCloud
+class DistRedis
attr_reader :ring
def initialize(*servers)
srvs = []
@@ -11,6 +11,11 @@ def initialize(*servers)
@ring = HashRing.new srvs, 3
end
+ def add_server(server)
+ server, port = server.split(':')
+ @ring.add_node Redis.new(:host => server, :port => port)
+ end
+
def method_missing(sym, *args, &blk)
if redis = @ring.get_node(args.first)
redis.send sym, *args, &blk
@@ -57,7 +62,7 @@ def delete_cloud!
if __FILE__ == $0
-r = RedisCloud.new 'localhost:6379', 'localhost:6380', 'localhost:6381','localhost:6382'
+r = DistRedis.new 'localhost:6379', 'localhost:6380', 'localhost:6381'
r['urmom'] = 'urmom'
r['urdad'] = 'urdad'
r['urmom1'] = 'urmom1'
@@ -94,6 +99,47 @@ def delete_cloud!
end
p r.keys('*')
- #r.delete_cloud!
- #p r.keys('*')
+
+ r.add_server 'localhost:6382'
+
+ r['urmom'] = 'urmom'
+ r['urdad'] = 'urdad'
+ r['urmom1'] = 'urmom1'
+ r['urdad1'] = 'urdad1'
+ r['urmom2'] = 'urmom2'
+ r['urdad2'] = 'urdad2'
+ r['urmom3'] = 'urmom3'
+ r['urdad3'] = 'urdad3'
+ p r['urmom']
+ p r['urdad']
+ p r['urmom1']
+ p r['urdad1']
+ p r['urmom2']
+ p r['urdad2']
+ p r['urmom3']
+ p r['urdad3']
+
+ r.push_tail 'listor', 'foo1'
+ r.push_tail 'listor', 'foo2'
+ r.push_tail 'listor', 'foo3'
+ r.push_tail 'listor', 'foo4'
+ r.push_tail 'listor', 'foo5'
+
+ p r.pop_tail 'listor'
+ p r.pop_tail 'listor'
+ p r.pop_tail 'listor'
+ p r.pop_tail 'listor'
+ p r.pop_tail 'listor'
+
+ puts "key distribution:"
+
+ r.ring.nodes.each do |red|
+ p [red.port, red.keys("*")]
+ end
+
+ p r.keys('*')
+
+ r.delete_cloud!
+ p r.keys('*')
+
end
View
5 lib/hash_ring.rb
@@ -7,7 +7,7 @@ class HashRing
def initialize(nodes=[], replicas=3)
@replicas = replicas
@ring = {}
- @nodes = nodes
+ @nodes = []
@sorted_keys = []
nodes.each do |node|
add_node(node)
@@ -16,6 +16,7 @@ def initialize(nodes=[], replicas=3)
# Adds a `node` to the hash ring (including a number of replicas).
def add_node(node)
+ @nodes << node
@replicas.times do |i|
key = gen_key("#{node}:#{i}")
@ring[key] = node
@@ -66,5 +67,3 @@ def gen_key(key)
#ring = HashRing.new ['server1', 'server2', 'server3']
#
#p ring.get_node "kjhjkjlkjlkkh"
-#
-#ring.get_nodes("fff") do |n| p n end
View
3 lib/redis.rb
@@ -6,7 +6,6 @@ class RedisError < StandardError
class Redis
OK = "+OK".freeze
- ERROR = "-ERR".freeze
ERRCODE = "-".freeze
NIL = 'nil'.freeze
CTRLF = "\r\n".freeze
@@ -637,7 +636,7 @@ def bulk_reply
def multi_bulk_reply
res = read_proto
- if res.index(ERROR) == 0
+ if res.index(ERRCODE) == 0
err = read(res.to_i.abs)
nibble_end
raise RedisError, err

0 comments on commit 98d287d

Please sign in to comment.
Something went wrong with that request. Please try again.