Skip to content

Commit

Permalink
In cluster mode prefer node with slots in 2 masters case
Browse files Browse the repository at this point in the history
  • Loading branch information
secwall committed May 20, 2024
1 parent c3be087 commit 7fede0c
Show file tree
Hide file tree
Showing 3 changed files with 245 additions and 130 deletions.
16 changes: 16 additions & 0 deletions internal/app/master.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,22 @@ func (app *App) getMasterHost(shardState map[string]*HostState) (string, error)
}
}
if len(masters) > 1 {
if app.mode == modeCluster {
mastersWithSlots := make([]string, 0)
for _, master := range masters {
node := app.shard.Get(master)
hasSlots, err := node.HasClusterSlots(app.ctx)
if err != nil {
return "", fmt.Errorf("unable to check slots on %s", master)
}
if hasSlots {
mastersWithSlots = append(mastersWithSlots, master)
}
}
if len(mastersWithSlots) == 1 {
return mastersWithSlots[0], nil
}
}
return "", fmt.Errorf("got more than 1 master: %s", masters)
}
if len(masters) == 0 {
Expand Down
20 changes: 20 additions & 0 deletions internal/redis/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -542,3 +542,23 @@ func (n *Node) ClusterMeet(ctx context.Context, addr string, port, clusterBusPor
cmd := n.conn.Do(ctx, n.config.Renames.Cluster, n.config.Renames.ClusterMeet, addr, strconv.Itoa(port), strconv.Itoa(clusterBusPort))
return cmd.Err()
}

// HasClusterSlots checks if node has any slot assigned
func (n *Node) HasClusterSlots(ctx context.Context) (bool, error) {
cmd := n.conn.ClusterNodes(ctx)
err := cmd.Err()
if err != nil {
return false, err
}
lines := strings.Split(cmd.Val(), "\n")
for _, line := range lines {
splitted := strings.Split(line, " ")
if len(splitted) < 3 {
continue
}
if strings.Contains(splitted[2], "myself") {
return len(splitted) > 8, nil
}
}
return false, nil
}
Loading

0 comments on commit 7fede0c

Please sign in to comment.