Commit 0064b1a5 authored by antirez's avatar antirez
Browse files

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.
parent 6c527a89
...@@ -510,7 +510,6 @@ class RedisTrib ...@@ -510,7 +510,6 @@ class RedisTrib
def alloc_slots def alloc_slots
nodes_count = @nodes.length nodes_count = @nodes.length
masters_count = @nodes.length / (@replicas+1) masters_count = @nodes.length / (@replicas+1)
slots_per_node = ClusterHashSlots / masters_count
masters = [] masters = []
slaves = [] slaves = []
...@@ -541,13 +540,18 @@ class RedisTrib ...@@ -541,13 +540,18 @@ class RedisTrib
end end
# Alloc slots on masters # 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| masters.each_with_index{|n,masternum|
first = i*slots_per_node last = (cursor+slots_per_node-1).round
last = first+slots_per_node-1 if last > ClusterHashSlots || masternum == masters.length-1
last = ClusterHashSlots-1 if masternum == masters.length-1 last = ClusterHashSlots-1
end
last = first if last < first # Min step is 1.
n.add_slots first..last n.add_slots first..last
i += 1 first = last+1
cursor += slots_per_node
} }
# Select N replicas for every master. # Select N replicas for every master.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment