mirror of
https://github.com/fluencelabs/redis
synced 2025-06-20 20:46:31 +00:00
Ruby client library updated. Important changes in this new version!
This commit is contained in:
@ -163,6 +163,9 @@ end
|
||||
# Another name for Timeout::Error, defined for backwards compatibility with
|
||||
# earlier versions of timeout.rb.
|
||||
|
||||
class Object
|
||||
remove_const(:TimeoutError) if const_defined?(:TimeoutError)
|
||||
end
|
||||
TimeoutError = Timeout::Error # :nodoc:
|
||||
|
||||
if __FILE__ == $0
|
||||
|
@ -24,7 +24,7 @@ class DistRedis
|
||||
end
|
||||
|
||||
def method_missing(sym, *args, &blk)
|
||||
if redis = node_for_key(args.first)
|
||||
if redis = node_for_key(args.first.to_s)
|
||||
redis.send sym, *args, &blk
|
||||
else
|
||||
super
|
||||
@ -94,11 +94,11 @@ r = DistRedis.new 'localhost:6379', 'localhost:6380', 'localhost:6381', 'localho
|
||||
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'
|
||||
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:"
|
||||
|
||||
|
@ -1,10 +1,15 @@
|
||||
require 'digest/md5'
|
||||
require 'zlib'
|
||||
|
||||
class HashRing
|
||||
|
||||
POINTS_PER_SERVER = 160 # this is the default in libmemcached
|
||||
|
||||
attr_reader :ring, :sorted_keys, :replicas, :nodes
|
||||
|
||||
# nodes is a list of objects that have a proper to_s representation.
|
||||
# replicas indicates how many virtual points should be used pr. node,
|
||||
# replicas are required to improve the distribution.
|
||||
def initialize(nodes=[], replicas=3)
|
||||
def initialize(nodes=[], replicas=POINTS_PER_SERVER)
|
||||
@replicas = replicas
|
||||
@ring = {}
|
||||
@nodes = []
|
||||
@ -18,7 +23,7 @@ class HashRing
|
||||
def add_node(node)
|
||||
@nodes << node
|
||||
@replicas.times do |i|
|
||||
key = gen_key("#{node}:#{i}")
|
||||
key = Zlib.crc32("#{node}:#{i}")
|
||||
@ring[key] = node
|
||||
@sorted_keys << key
|
||||
end
|
||||
@ -27,7 +32,7 @@ class HashRing
|
||||
|
||||
def remove_node(node)
|
||||
@replicas.times do |i|
|
||||
key = gen_key("#{node}:#{count}")
|
||||
key = Zlib.crc32("#{node}:#{count}")
|
||||
@ring.delete(key)
|
||||
@sorted_keys.reject! {|k| k == key}
|
||||
end
|
||||
@ -40,15 +45,9 @@ class HashRing
|
||||
|
||||
def get_node_pos(key)
|
||||
return [nil,nil] if @ring.size == 0
|
||||
key = gen_key(key)
|
||||
nodes = @sorted_keys
|
||||
nodes.size.times do |i|
|
||||
node = nodes[i]
|
||||
if key <= node
|
||||
return [@ring[node], i]
|
||||
end
|
||||
end
|
||||
[@ring[nodes[0]], 0]
|
||||
crc = Zlib.crc32(key)
|
||||
idx = HashRing.binary_search(@sorted_keys, crc)
|
||||
return [@ring[@sorted_keys[idx]], idx]
|
||||
end
|
||||
|
||||
def iter_nodes(key)
|
||||
@ -59,11 +58,66 @@ class HashRing
|
||||
end
|
||||
end
|
||||
|
||||
def gen_key(key)
|
||||
key = Digest::MD5.hexdigest(key)
|
||||
((key[3] << 24) | (key[2] << 16) | (key[1] << 8) | key[0])
|
||||
class << self
|
||||
|
||||
# gem install RubyInline to use this code
|
||||
# Native extension to perform the binary search within the hashring.
|
||||
# There's a pure ruby version below so this is purely optional
|
||||
# for performance. In testing 20k gets and sets, the native
|
||||
# binary search shaved about 12% off the runtime (9sec -> 8sec).
|
||||
begin
|
||||
require 'inline'
|
||||
inline do |builder|
|
||||
builder.c <<-EOM
|
||||
int binary_search(VALUE ary, unsigned int r) {
|
||||
int upper = RARRAY_LEN(ary) - 1;
|
||||
int lower = 0;
|
||||
int idx = 0;
|
||||
|
||||
while (lower <= upper) {
|
||||
idx = (lower + upper) / 2;
|
||||
|
||||
VALUE continuumValue = RARRAY_PTR(ary)[idx];
|
||||
unsigned int l = NUM2UINT(continuumValue);
|
||||
if (l == r) {
|
||||
return idx;
|
||||
}
|
||||
else if (l > r) {
|
||||
upper = idx - 1;
|
||||
}
|
||||
else {
|
||||
lower = idx + 1;
|
||||
}
|
||||
}
|
||||
return upper;
|
||||
}
|
||||
EOM
|
||||
end
|
||||
rescue Exception => e
|
||||
# Find the closest index in HashRing with value <= the given value
|
||||
def binary_search(ary, value, &block)
|
||||
upper = ary.size - 1
|
||||
lower = 0
|
||||
idx = 0
|
||||
|
||||
while(lower <= upper) do
|
||||
idx = (lower + upper) / 2
|
||||
comp = ary[idx] <=> value
|
||||
|
||||
if comp == 0
|
||||
return idx
|
||||
elsif comp > 0
|
||||
upper = idx - 1
|
||||
else
|
||||
lower = idx + 1
|
||||
end
|
||||
end
|
||||
return upper
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
end
|
||||
|
||||
# ring = HashRing.new ['server1', 'server2', 'server3']
|
||||
|
File diff suppressed because it is too large
Load Diff
131
client-libraries/ruby/lib/server.rb
Normal file
131
client-libraries/ruby/lib/server.rb
Normal file
@ -0,0 +1,131 @@
|
||||
##
|
||||
# This class represents a redis server instance.
|
||||
|
||||
class Server
|
||||
|
||||
##
|
||||
# The amount of time to wait before attempting to re-establish a
|
||||
# connection with a server that is marked dead.
|
||||
|
||||
RETRY_DELAY = 30.0
|
||||
|
||||
##
|
||||
# The host the redis server is running on.
|
||||
|
||||
attr_reader :host
|
||||
|
||||
##
|
||||
# The port the redis server is listening on.
|
||||
|
||||
attr_reader :port
|
||||
|
||||
##
|
||||
#
|
||||
|
||||
attr_reader :replica
|
||||
|
||||
##
|
||||
# The time of next retry if the connection is dead.
|
||||
|
||||
attr_reader :retry
|
||||
|
||||
##
|
||||
# A text status string describing the state of the server.
|
||||
|
||||
attr_reader :status
|
||||
|
||||
##
|
||||
# Create a new Redis::Server object for the redis instance
|
||||
# listening on the given host and port.
|
||||
|
||||
def initialize(host, port = DEFAULT_PORT)
|
||||
raise ArgumentError, "No host specified" if host.nil? or host.empty?
|
||||
raise ArgumentError, "No port specified" if port.nil? or port.to_i.zero?
|
||||
|
||||
@host = host
|
||||
@port = port.to_i
|
||||
|
||||
@sock = nil
|
||||
@retry = nil
|
||||
@status = 'NOT CONNECTED'
|
||||
@timeout = 1
|
||||
end
|
||||
|
||||
##
|
||||
# Return a string representation of the server object.
|
||||
def inspect
|
||||
"<Redis::Server: %s:%d (%s)>" % [@host, @port, @status]
|
||||
end
|
||||
|
||||
##
|
||||
# Try to connect to the redis server targeted by this object.
|
||||
# Returns the connected socket object on success or nil on failure.
|
||||
|
||||
def socket
|
||||
return @sock if @sock and not @sock.closed?
|
||||
|
||||
@sock = nil
|
||||
|
||||
# If the host was dead, don't retry for a while.
|
||||
return if @retry and @retry > Time.now
|
||||
|
||||
# Attempt to connect if not already connected.
|
||||
begin
|
||||
@sock = connect_to(@host, @port, @timeout)
|
||||
@sock.setsockopt Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1
|
||||
@retry = nil
|
||||
@status = 'CONNECTED'
|
||||
rescue Errno::EPIPE, Errno::ECONNREFUSED => e
|
||||
puts "Socket died... socket: #{@sock.inspect}\n" if $debug
|
||||
@sock.close
|
||||
retry
|
||||
rescue SocketError, SystemCallError, IOError => err
|
||||
puts "Unable to open socket: #{err.class.name}, #{err.message}" if $debug
|
||||
mark_dead err
|
||||
end
|
||||
|
||||
return @sock
|
||||
end
|
||||
|
||||
def connect_to(host, port, timeout=nil)
|
||||
addrs = Socket.getaddrinfo('localhost', nil)
|
||||
addr = addrs.detect { |ad| ad[0] == 'AF_INET' }
|
||||
sock = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
|
||||
#addr = Socket.getaddrinfo(host, nil)
|
||||
#sock = Socket.new(Socket.const_get(addr[0][0]), Socket::SOCK_STREAM, 0)
|
||||
|
||||
if timeout
|
||||
secs = Integer(timeout)
|
||||
usecs = Integer((timeout - secs) * 1_000_000)
|
||||
optval = [secs, usecs].pack("l_2")
|
||||
sock.setsockopt Socket::SOL_SOCKET, Socket::SO_RCVTIMEO, optval
|
||||
sock.setsockopt Socket::SOL_SOCKET, Socket::SO_SNDTIMEO, optval
|
||||
end
|
||||
sock.connect(Socket.pack_sockaddr_in('6379', addr[3]))
|
||||
sock
|
||||
end
|
||||
|
||||
##
|
||||
# Close the connection to the redis server targeted by this
|
||||
# object. The server is not considered dead.
|
||||
|
||||
def close
|
||||
@sock.close if @sock && !@sock.closed?
|
||||
@sock = nil
|
||||
@retry = nil
|
||||
@status = "NOT CONNECTED"
|
||||
end
|
||||
|
||||
##
|
||||
# Mark the server as dead and close its socket.
|
||||
def mark_dead(error)
|
||||
@sock.close if @sock && !@sock.closed?
|
||||
@sock = nil
|
||||
@retry = Time.now #+ RETRY_DELAY
|
||||
|
||||
reason = "#{error.class.name}: #{error.message}"
|
||||
@status = sprintf "%s:%s DEAD (%s), will retry at %s", @host, @port, reason, @retry
|
||||
puts @status
|
||||
end
|
||||
|
||||
end
|
Reference in New Issue
Block a user