From 72b8e25f6e6653d5469b36a05271df35596a4ac0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20Mart=C3=ADnez=20del=20Horno?= Date: Mon, 23 Jun 2025 11:56:28 +0200 Subject: [PATCH] fix: balance cluster if node has less slots than expected --- internal/rediscluster/cluster.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/internal/rediscluster/cluster.go b/internal/rediscluster/cluster.go index e89d6d7..5448b61 100644 --- a/internal/rediscluster/cluster.go +++ b/internal/rediscluster/cluster.go @@ -552,6 +552,8 @@ func (rc *RedisCluster) ResetNode(node *redis.RedisNode) error { func (rc *RedisCluster) IsBalanced() bool { masters := rc.GetMasterNodes() slotsPerMaster := int(math.Ceil(float64(RedisClusterTotalSlots) / float64(len(masters)))) + maximumSlots := slotsPerMaster + (slotsPerMaster * RedisNodesUnbalancedThreshold / 100) + minimumSlots := slotsPerMaster - (slotsPerMaster * RedisNodesUnbalancedThreshold / 100) for _, master := range masters { masterSlots := master.GetNumberOfSlots() @@ -562,10 +564,9 @@ func (rc *RedisCluster) IsBalanced() bool { return false } - // Cluster is not balanced if the number of slots is greater than the maximum - maximumSlots := slotsPerMaster + (slotsPerMaster * RedisNodesUnbalancedThreshold / 100) - if masterSlots > maximumSlots { - rc.logger.Info("Cluster needs rebalance: node has more slots than the maximum", "node", master.Name, "slots", masterSlots, "maximumSlots", maximumSlots) + // Cluster is not balanced if the number of slots is not in the expected range + if masterSlots < minimumSlots || masterSlots > maximumSlots { + rc.logger.Info("Cluster needs rebalance: node slots are not in the expected range", "node", master.Name, "slots", masterSlots, "minimumSlots", minimumSlots, "maximumSlots", maximumSlots) return false } }