Skip to content

Commit

Permalink
chore: enhance MySQL get role accuracy (#8662)
Browse files Browse the repository at this point in the history
  • Loading branch information
xuriwuyun authored Dec 17, 2024
1 parent 3559596 commit 93ac774
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 9 deletions.
5 changes: 5 additions & 0 deletions pkg/lorry/dcs/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,10 @@ func (store *KubernetesStore) GetLeader() (*Leader, error) {
ttl = viper.GetInt(constant.KBEnvTTL)
}
leader := annotations["leader"]
holder := annotations["holder"]
if holder == "" {
holder = leader
}
stateStr, ok := annotations["dbstate"]
var dbState *DBState
if ok {
Expand All @@ -380,6 +384,7 @@ func (store *KubernetesStore) GetLeader() (*Leader, error) {
return &Leader{
Index: configmap.ResourceVersion,
Name: leader,
Holder: holder,
AcquireTime: acquireTime,
RenewTime: renewTime,
TTL: ttl,
Expand Down
1 change: 1 addition & 0 deletions pkg/lorry/dcs/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ type Leader struct {
DBState *DBState
Index string
Name string
Holder string
AcquireTime int64
RenewTime int64
TTL int
Expand Down
10 changes: 1 addition & 9 deletions pkg/lorry/engines/mysql/get_replica_role.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,13 @@ func (mgr *Manager) GetReplicaRole(ctx context.Context, cluster *dcs.Cluster) (s
if cluster == nil {
return "", errors.New("cluster not found")
}
if cluster.HaConfig != nil && !cluster.HaConfig.IsEnable() {
return mgr.GetReplicaRoleFromDB(ctx)
}

leader, err := dcs.GetStore().GetLeader()
if err != nil {
mgr.Logger.Info("get leader failed", "error", err.Error())
}
cluster.Leader = leader
if !cluster.IsLocked() {
mgr.Logger.Info("cluster has no leader lease")
return models.SECONDARY, nil
}

if cluster.Leader.Name != mgr.CurrentMemberName {
if leader == nil || leader.Holder != mgr.CurrentMemberName {
return models.SECONDARY, nil
}

Expand Down

0 comments on commit 93ac774

Please sign in to comment.