summaryrefslogtreecommitdiff
path: root/src/redis-trib.rb
diff options
context:
space:
mode:
authorantirez <antirez@gmail.com>2014-03-25 17:44:39 +0100
committerantirez <antirez@gmail.com>2014-03-25 17:44:39 +0100
commit0064b1a583f2feb4852a22d84248548dc7e66a3a (patch)
tree2e3fccb63645de24921a4af87f1a5fc6174e7a16 /src/redis-trib.rb
parent6c527a89a06ad93d7e250de78d3fecb39f13a6a3 (diff)
downloadredis-0064b1a583f2feb4852a22d84248548dc7e66a3a.tar.gz
Cluster: redis-trib cluster allocation more even across nodes.
redis-trib used to allocate slots not considering fractions of nodes when computing the slots_per_node amount. So the fractional part was carried over till the end of the allocation, where the last node received a few more slots than any other (or a lot more if the cluster was composed of many nodes). The computation was changed to allocate slots more evenly when they are not exactly divisible for the number of masters we have.
Diffstat (limited to 'src/redis-trib.rb')
-rwxr-xr-xsrc/redis-trib.rb16
1 files changed, 10 insertions, 6 deletions
diff --git a/src/redis-trib.rb b/src/redis-trib.rb
index 85139ebb9..743c87442 100755
--- a/src/redis-trib.rb
+++ b/src/redis-trib.rb
@@ -510,7 +510,6 @@ class RedisTrib
def alloc_slots
nodes_count = @nodes.length
masters_count = @nodes.length / (@replicas+1)
- slots_per_node = ClusterHashSlots / masters_count
masters = []
slaves = []
@@ -541,13 +540,18 @@ class RedisTrib
end
# Alloc slots on masters
- i = 0
+ slots_per_node = ClusterHashSlots.to_f / masters_count
+ first = 0
+ cursor = 0.0
masters.each_with_index{|n,masternum|
- first = i*slots_per_node
- last = first+slots_per_node-1
- last = ClusterHashSlots-1 if masternum == masters.length-1
+ last = (cursor+slots_per_node-1).round
+ if last > ClusterHashSlots || masternum == masters.length-1
+ last = ClusterHashSlots-1
+ end
+ last = first if last < first # Min step is 1.
n.add_slots first..last
- i += 1
+ first = last+1
+ cursor += slots_per_node
}
# Select N replicas for every master.