redis-trib: create subcommand is now able to assign spare slaves.

Example: if the user will try to configure a cluster with 9 nodes,
asking for 1 slave for master, redis-trib will configure a 4 masters
cluster with 1 slave each as usually, but this time will assign the
spare node as a slave of one of the masters.
This commit is contained in:
antirez 2014-03-11 14:17:28 +01:00
parent e26f4486b0
commit 2e5c394fa8

View File

@ -553,22 +553,28 @@ class RedisTrib
# Select N replicas for every master. # Select N replicas for every master.
# We try to split the replicas among all the IPs with spare nodes # We try to split the replicas among all the IPs with spare nodes
# trying to avoid the host where the master is running, if possible. # trying to avoid the host where the master is running, if possible.
masters.each{|m| #
i = 0 # Note that we loop two times, the first with spare_loop set to false,
while i < @replicas # the second with the var set to true. The second loop changes the
ips.each{|ip,nodes_list| # while condition so to assign remaining slaves.
next if nodes_list.length == 0 [false,true].each{|spare_loop|
# Skip instances with the same IP as the master if we masters.each{|m|
# have some more IPs available. i = 0
next if ip == m.info[:host] && nodes_count > nodes_list.length while (!spare_loop && i < @replicas) || \
slave = nodes_list.shift (spare_loop && nodes_count > 0)
slave.set_as_replica(m.info[:name]) ips.each{|ip,nodes_list|
nodes_count -= 1 next if nodes_list.length == 0
i += 1 # Skip instances with the same IP as the master if we
puts "#{m} replica ##{i} is #{slave}" # have some more IPs available.
break if masters.length == masters_count next if ip == m.info[:host] && nodes_count > nodes_list.length
} slave = nodes_list.shift
end slave.set_as_replica(m.info[:name])
nodes_count -= 1
i += 1
puts "Adding replica #{slave} to #{m}"
}
end
}
} }
end end