Skip to content

Commit

Permalink
make test happy
Browse files Browse the repository at this point in the history
Signed-off-by: husharp <[email protected]>
  • Loading branch information
HuSharp committed May 9, 2024
1 parent 7fa19d3 commit 480b075
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
27 changes: 20 additions & 7 deletions pkg/election/leadership.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ type Leadership struct {
leaderKey string
leaderValue string

LeaderWatch bool
leaderWatch struct {
syncutil.RWMutex
val bool
}

keepAliveCtx context.Context
keepAliveCancelFunc context.CancelFunc
Expand All @@ -74,10 +77,6 @@ type Leadership struct {
campaignTimes []time.Time
}

func (ls *Leadership) SetLeaderWatch(val bool) {
ls.LeaderWatch = val
}

func (ls *Leadership) GetLeaderValue() string {
return ls.leaderValue
}
Expand Down Expand Up @@ -123,6 +122,20 @@ func (ls *Leadership) GetLeaderKey() string {
return ls.leaderKey
}

// SetLeaderWatch sets the leader watch flag.
func (ls *Leadership) SetLeaderWatch(val bool) {
ls.leaderWatch.Lock()
ls.leaderWatch.val = val
ls.leaderWatch.Unlock()
}

// GetLeaderWatch gets the leader watch flag.
func (ls *Leadership) GetLeaderWatch() bool {
ls.leaderWatch.RLock()
defer ls.leaderWatch.RUnlock()
return ls.leaderWatch.val
}

// GetCampaignTimesNum is used to get the campaign times of the leader within `campaignTimesRecordTimeout`.
func (ls *Leadership) GetCampaignTimesNum() int {
if ls == nil {
Expand Down Expand Up @@ -386,7 +399,7 @@ func (ls *Leadership) Watch(serverCtx context.Context, revision int64) {
return
}
// only API update the leader key to transfer the leader will meet
if ev.Type == mvccpb.PUT && ls.LeaderWatch {
if ev.Type == mvccpb.PUT && ls.GetLeaderWatch() {
log.Info("[LeaderWatch] current leadership is updated", zap.Int64("watchRevision", revision),
zap.Int64("revision", wresp.Header.Revision), zap.String("leader-key", ls.leaderKey), zap.String("purpose", ls.purpose))
return
Expand All @@ -409,5 +422,5 @@ func (ls *Leadership) Reset() {
}
ls.keepAliveCancelFuncLock.Unlock()
ls.getLease().Close()
ls.LeaderWatch = false
ls.SetLeaderWatch(false)
}
4 changes: 2 additions & 2 deletions pkg/mcs/utils/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func RemoveExpectedPrimary(client *clientv3.Client, leaderPath string) {
resp, err := kv.NewSlowLogTxn(client).
Then(clientv3.OpDelete(strings.Join([]string{leaderPath, ExpectedPrimary}, "/"))).
Commit()
if err != nil && !resp.Succeeded {
if err != nil || !resp.Succeeded {
log.Error("change primary error", errs.ZapError(err))
return
}
Expand All @@ -113,7 +113,7 @@ func SetExpectedPrimary(client *clientv3.Client, leaderPath string) {
// indicate the current primary has exited
clientv3.OpDelete(leaderPath)).
Commit()
if err != nil && !resp.Succeeded {
if err != nil || !resp.Succeeded {
log.Error("change primary error", errs.ZapError(err))
return
}
Expand Down

0 comments on commit 480b075

Please sign in to comment.