From ff9166508b135bb2554516bbb2e45d94a2c827d7 Mon Sep 17 00:00:00 2001 From: Shubham Gupta <69793468+shubham-cmyk@users.noreply.github.com> Date: Sun, 24 Mar 2024 11:40:38 +0530 Subject: [PATCH] chore: use redis client library (#840) * chore: use appropriate way to calulate slots Signed-off-by: Shubham Gupta * chore: remove unncessary conversion Signed-off-by: Shubham Gupta * chore: improve slaveid calculation Signed-off-by: Shubham Gupta --------- Signed-off-by: Shubham Gupta --- k8sutils/cluster-scaling.go | 48 ++++++++++--------------------------- 1 file changed, 12 insertions(+), 36 deletions(-) diff --git a/k8sutils/cluster-scaling.go b/k8sutils/cluster-scaling.go index e5e86726c..f95163758 100644 --- a/k8sutils/cluster-scaling.go +++ b/k8sutils/cluster-scaling.go @@ -86,33 +86,22 @@ func getRedisClusterSlots(ctx context.Context, client kubernetes.Interface, logg redisClient := configureRedisClient(client, logger, cr, cr.ObjectMeta.Name+"-leader-0") defer redisClient.Close() - redisClusterInfo, err := redisClient.ClusterNodes(ctx).Result() + + redisSlots, err := redisClient.ClusterSlots(ctx).Result() if err != nil { - logger.Error(err, "Failed to Get Cluster Info") + logger.Error(err, "Failed to Get Cluster Slots") return "" } - - // Split the Redis cluster info into lines - lines := strings.Split(redisClusterInfo, "\n") - // Iterate through all lines - for _, line := range lines { - if strings.Contains(line, "master") && strings.Contains(line, "connected") { // Check if this line is a master node - parts := strings.Fields(line) - if parts[0] == nodeID { // Check if this is the node we're interested in - for _, conn := range parts[8:] { - slotRange := strings.Split(conn, "-") - if len(slotRange) < 2 { - totalSlots = totalSlots + 1 - } else { - start, _ := strconv.Atoi(slotRange[0]) - end, _ := strconv.Atoi(slotRange[1]) - totalSlots = totalSlots + end - start + 1 - } - } + for _, slot := range redisSlots { + for _, node := range slot.Nodes { + if node.ID == nodeID { + // Each slot range is a continuous block managed by the node + totalSlots += slot.End - slot.Start + 1 break } } } + logger.V(1).Info("Total cluster slots to be transferred from", "node", nodeID, "is", totalSlots) return strconv.Itoa(totalSlots) } @@ -270,25 +259,12 @@ func AddRedisNodeToCluster(ctx context.Context, client kubernetes.Interface, log func getAttachedFollowerNodeIDs(ctx context.Context, client kubernetes.Interface, logger logr.Logger, cr *redisv1beta2.RedisCluster, masterNodeID string) []string { redisClient := configureRedisClient(client, logger, cr, cr.ObjectMeta.Name+"-leader-0") defer redisClient.Close() - redisClusterInfo, err := redisClient.ClusterNodes(ctx).Result() + + slaveIDs, err := redisClient.ClusterSlaves(ctx, masterNodeID).Result() if err != nil { - logger.Error(err, "Failed to Get Cluster Info") + logger.Error(err, "Failed to get attached follower node IDs", "masterNodeID", masterNodeID) return nil } - - slaveIDs := []string{} - // Split the Redis cluster info into lines - lines := strings.Split(redisClusterInfo, "\n") - - for _, line := range lines { - if strings.Contains(line, "slave") && strings.Contains(line, "connected") { - parts := strings.Fields(line) - if len(parts) >= 3 && parts[3] == masterNodeID { - slaveIDs = append(slaveIDs, parts[0]) - } - } - } - logger.V(1).Info("Slaves Nodes attached to", "node", masterNodeID, "are", slaveIDs) return slaveIDs }