diff options
author | antirez <antirez@gmail.com> | 2014-03-11 14:17:28 +0100 |
---|---|---|
committer | antirez <antirez@gmail.com> | 2014-03-11 14:17:28 +0100 |
commit | 2e5c394fa8dbc0fc09811fb2075271d68d05ad62 (patch) | |
tree | af11eb3667f7d10281331473926151c91bd8944d /src/redis-trib.rb | |
parent | e26f4486b07070d795644a16e50429917b86eee0 (diff) | |
download | redis-2e5c394fa8dbc0fc09811fb2075271d68d05ad62.tar.gz |
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.
Diffstat (limited to 'src/redis-trib.rb')
-rwxr-xr-x | src/redis-trib.rb | 38 |
1 files changed, 22 insertions, 16 deletions
diff --git a/src/redis-trib.rb b/src/redis-trib.rb index ec3c416bb..1fc73917b 100755 --- a/src/redis-trib.rb +++ b/src/redis-trib.rb @@ -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 |