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.
# 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.
masters.each{|m|
i = 0
while i < @replicas
ips.each{|ip,nodes_list|
next if nodes_list.length == 0
# Skip instances with the same IP as the master if we
# have some more IPs available.
next if ip == m.info[:host] && nodes_count > nodes_list.length
slave = nodes_list.shift
slave.set_as_replica(m.info[:name])
nodes_count -= 1
i += 1
puts "#{m} replica ##{i} is #{slave}"
break if masters.length == masters_count
}
end
#
# Note that we loop two times, the first with spare_loop set to false,
# the second with the var set to true. The second loop changes the
# while condition so to assign remaining slaves.
[false,true].each{|spare_loop|
masters.each{|m|
i = 0
while (!spare_loop && i < @replicas) || \
(spare_loop && nodes_count > 0)
ips.each{|ip,nodes_list|
next if nodes_list.length == 0
# Skip instances with the same IP as the master if we
# have some more IPs available.
next if ip == m.info[:host] && nodes_count > nodes_list.length
slave = nodes_list.shift
slave.set_as_replica(m.info[:name])
nodes_count -= 1
i += 1
puts "Adding replica #{slave} to #{m}"
}
end
}
}
end