Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 26 additions & 12 deletions cluster.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
Expand Down Expand Up @@ -55,24 +55,28 @@ def set_node_name!(n)
# Contact the startup nodes and try to fetch the hash slots -> instances
# map in order to initialize the @slots hash.
def initialize_slots_cache
@startup_nodes.each{|n|
@startup_nodes.each do |n|
begin
@slots = {}
@nodes = []

r = get_redis_link(n[:host],n[:port])
r.cluster("slots").each {|r|
(r[0]..r[1]).each{|slot|
ip,port = r[2]
redis_link = get_redis_link(n[:host],n[:port])
@cluster_slots = redis_link.cluster("slots")

@cluster_slots.each do |item|
(item[0]..item[1]).each do |slot|
ip, port = item[2]
name = "#{ip}:#{port}"
node = {
:host => ip, :port => port,
:host => ip,
:port => port,
:name => name
}
@nodes << node
@slots[slot] = node
}
}
end
end

populate_startup_nodes
@refresh_table_asap = false
rescue
Expand All @@ -81,7 +85,7 @@ def initialize_slots_cache
end
# Exit the loop as long as the first node replies
break
}
end
end

# Use @nodes to populate @startup_nodes, so that we have more chances
Expand All @@ -90,7 +94,17 @@ def populate_startup_nodes
# Make sure every node has already a name, so that later the
# Array uniq! method will work reliably.
@startup_nodes.each{|n| set_node_name! n}
@nodes.each{|n| @startup_nodes << n}
@cluster_slots.each do |item|
ip, port = item[2]
name = "#{ip}:#{port}"
node = {
:host => ip,
:port => port,
:name => name
}
@startup_nodes << node
end

@startup_nodes.uniq!
end

Expand Down
5 changes: 3 additions & 2 deletions example.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

if ARGV.length != 2
startup_nodes = [
{:host => "127.0.0.1", :port => 6379},
{:host => "127.0.0.1", :port => 6380}
{:host => "127.0.0.1", :port => 7000},
{:host => "127.0.0.1", :port => 7001},
{:host => "127.0.0.1", :port => 7002}
]
else
startup_nodes = [
Expand Down