Skip to content

Commit

Permalink
This is an automated cherry-pick of tikv#6636
Browse files Browse the repository at this point in the history
close tikv#6635

Signed-off-by: ti-chi-bot <[email protected]>
  • Loading branch information
lhy1024 authored and ti-chi-bot committed Jun 28, 2023
1 parent bb3953a commit b4380d9
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions server/election/leadership.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package election

import (
"context"
"sync"
"sync/atomic"

"github.com/pingcap/failpoint"
Expand Down Expand Up @@ -54,8 +55,14 @@ type Leadership struct {
leaderKey string
leaderValue string

<<<<<<< HEAD:server/election/leadership.go
keepAliveCtx context.Context
keepAliceCancelFunc context.CancelFunc
=======
keepAliveCtx context.Context
keepAliveCancelFunc context.CancelFunc
keepAliveCancelFuncLock sync.Mutex
>>>>>>> 3bdfef1a3 (leadership: avoid potential data race (#6636)):pkg/election/leadership.go
}

// NewLeadership creates a new Leadership.
Expand Down Expand Up @@ -137,8 +144,15 @@ func (ls *Leadership) Keep(ctx context.Context) {
if ls == nil {
return
}
<<<<<<< HEAD:server/election/leadership.go
ls.keepAliveCtx, ls.keepAliceCancelFunc = context.WithCancel(ctx)
ls.getLease().KeepAlive(ls.keepAliveCtx)
=======
ls.keepAliveCancelFuncLock.Lock()
ls.keepAliveCtx, ls.keepAliveCancelFunc = context.WithCancel(ctx)
ls.keepAliveCancelFuncLock.Unlock()
go ls.getLease().KeepAlive(ls.keepAliveCtx)
>>>>>>> 3bdfef1a3 (leadership: avoid potential data race (#6636)):pkg/election/leadership.go
}

// Check returns whether the leadership is still available.
Expand Down Expand Up @@ -230,8 +244,15 @@ func (ls *Leadership) Reset() {
if ls == nil || ls.getLease() == nil {
return
}
<<<<<<< HEAD:server/election/leadership.go
if ls.keepAliceCancelFunc != nil {
ls.keepAliceCancelFunc()
=======
ls.keepAliveCancelFuncLock.Lock()
if ls.keepAliveCancelFunc != nil {
ls.keepAliveCancelFunc()
>>>>>>> 3bdfef1a3 (leadership: avoid potential data race (#6636)):pkg/election/leadership.go
}
ls.keepAliveCancelFuncLock.Unlock()
ls.getLease().Close()
}

0 comments on commit b4380d9

Please sign in to comment.