Skip to content

Commit

Permalink
fix: tryFixReplicationRelationships error (#2991)
Browse files Browse the repository at this point in the history
* fix invalid arguments error when dashboard send slveof force

Co-authored-by: wangshaoyi <[email protected]>
  • Loading branch information
wangshao1 and wangshaoyi authored Jan 16, 2025
1 parent f0704c3 commit fdcdd8f
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 14 deletions.
19 changes: 7 additions & 12 deletions codis/pkg/topom/topom_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ func (s *Topom) GroupPromoteServer(gid int, addr string) error {
}
}

func (s *Topom) tryFixReplicationRelationships(ctx *context, recoveredGroupServers []*redis.ReplicationState, masterOffGroupLen int) {
func (s *Topom) tryFixReplicationRelationships(ctx *context, recoveredGroupServers []*redis.ReplicationState) {
for _, state := range recoveredGroupServers {
log.Infof("group-[%d] try to fix server[%v-%v] replication relationship", state.GroupID, state.Index, state.Addr)
group, err := ctx.getGroup(state.GroupID)
Expand All @@ -346,7 +346,7 @@ func (s *Topom) tryFixReplicationRelationships(ctx *context, recoveredGroupServe
continue
}

err = s.tryFixReplicationRelationship(group, state.Server, state, masterOffGroupLen)
err = s.tryFixReplicationRelationship(group, state.Server, state)
if err != nil {
log.Warnf("group-[%d] fix server[%v] replication relationship failed, err: %v", group.Id, state.Addr, err)
continue
Expand All @@ -371,19 +371,14 @@ func (s *Topom) tryFixReplicationRelationships(ctx *context, recoveredGroupServe
// only fix which the old state of GroupServer is GroupServerStateOffline.
// It will only update the state of GroupServer to GroupServerStateNormal, If the GroupServer have right
// master-slave replication relationship.
func (s *Topom) tryFixReplicationRelationship(group *models.Group, groupServer *models.GroupServer, state *redis.ReplicationState, masterOffGroupLen int) (err error) {
func (s *Topom) tryFixReplicationRelationship(group *models.Group, groupServer *models.GroupServer, state *redis.ReplicationState) (err error) {
curMasterAddr := group.Servers[0].Addr
if isGroupMaster(state, group) {
// current server is master,
if models.GroupServerRole(state.Replication.Role) == models.RoleMaster {
if masterOffGroupLen > 0 {
return nil
}
}

// execute the command `slaveof no one`
if err = promoteServerToNewMaster(state.Addr, s.config.ProductAuth); err != nil {
return err
if models.GroupServerRole(state.Replication.Role) != models.RoleMaster {
if err = promoteServerToNewMaster(state.Addr, s.config.ProductAuth); err != nil {
return err
}
}
} else {
// skip if it has right replication relationship
Expand Down
2 changes: 1 addition & 1 deletion codis/pkg/topom/topom_sentinel.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (s *Topom) CheckStateAndSwitchSlavesAndMasters(filter func(index int, g *mo

if len(recoveredGroupServersState) > 0 {
// offline GroupServer's service has recovered, check and fix it's master-slave replication relationship
s.tryFixReplicationRelationships(ctx, recoveredGroupServersState, len(masterOfflineGroups))
s.tryFixReplicationRelationships(ctx, recoveredGroupServersState)
}

return nil
Expand Down
2 changes: 1 addition & 1 deletion codis/pkg/utils/redis/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ func (c *Client) SetMaster(master string, force bool) error {
}

if force {
if _, err := c.Do("SLAVEOF", host, port, "-f"); err != nil {
if _, err := c.Do("SLAVEOF", host, port, "force"); err != nil {
return err
}
} else {
Expand Down

0 comments on commit fdcdd8f

Please sign in to comment.