diff --git a/codis/pkg/topom/topom_group.go b/codis/pkg/topom/topom_group.go index 98f2d3c537..b1ed76836b 100644 --- a/codis/pkg/topom/topom_group.go +++ b/codis/pkg/topom/topom_group.go @@ -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) @@ -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 @@ -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 diff --git a/codis/pkg/topom/topom_sentinel.go b/codis/pkg/topom/topom_sentinel.go index 807b9019a1..f1fc67fff6 100644 --- a/codis/pkg/topom/topom_sentinel.go +++ b/codis/pkg/topom/topom_sentinel.go @@ -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 diff --git a/codis/pkg/utils/redis/client.go b/codis/pkg/utils/redis/client.go index 5f751321db..9cfb47c633 100644 --- a/codis/pkg/utils/redis/client.go +++ b/codis/pkg/utils/redis/client.go @@ -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 {