Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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.
  • Loading branch information...
commit 2e5c394fa8dbc0fc09811fb2075271d68d05ad62 1 parent e26f448
@antirez authored
Showing with 22 additions and 16 deletions.
  1. +22 −16 src/redis-trib.rb
View
38 src/redis-trib.rb
@@ -553,22 +553,28 @@ def alloc_slots
# 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
Please sign in to comment.
Something went wrong with that request. Please try again.