Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: tryFixReplicationRelationships error #2991

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading