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 2a647ac
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 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)
}
6 changes: 3 additions & 3 deletions pkg/mcs/utils/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package utils

import (
"context"
"github.com/tikv/pd/pkg/storage/kv"
"net"
"net/http"
"os"
Expand All @@ -33,6 +32,7 @@ import (
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/soheilhy/cmux"
"github.com/tikv/pd/pkg/errs"
"github.com/tikv/pd/pkg/storage/kv"
"github.com/tikv/pd/pkg/utils/apiutil"
"github.com/tikv/pd/pkg/utils/apiutil/multiservicesapi"
"github.com/tikv/pd/pkg/utils/etcdutil"
Expand Down 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 2a647ac

Please sign in to comment.