Skip to content

Commit

Permalink
election: fix the data race of the leadership resetting (#4102)
Browse files Browse the repository at this point in the history
* Fix the data race of the leadership resetting

Signed-off-by: JmPotato <[email protected]>

* Fix the test

Signed-off-by: JmPotato <[email protected]>

* Add a comment

Signed-off-by: JmPotato <[email protected]>

Co-authored-by: Ti Chi Robot <[email protected]>
  • Loading branch information
JmPotato and ti-chi-bot authored Sep 13, 2021
1 parent 740fac1 commit 3f53f1e
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
3 changes: 2 additions & 1 deletion server/election/lease.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/pingcap/log"
"github.com/tikv/pd/pkg/errs"
"github.com/tikv/pd/pkg/etcdutil"
"github.com/tikv/pd/pkg/typeutil"
"go.etcd.io/etcd/clientv3"
"go.uber.org/zap"
)
Expand Down Expand Up @@ -71,7 +72,7 @@ func (l *lease) Close() error {
return nil
}
// Reset expire time.
l.expireTime.Store(time.Time{})
l.expireTime.Store(typeutil.ZeroTime)
// Try to revoke lease to make subsequent elections faster.
ctx, cancel := context.WithTimeout(l.client.Ctx(), revokeLeaseTimeout)
defer cancel()
Expand Down
5 changes: 4 additions & 1 deletion server/tso/allocator_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -933,7 +933,10 @@ func (am *AllocatorManager) ResetAllocatorGroup(dcLocation string) {
defer am.mu.Unlock()
if allocatorGroup, exist := am.mu.allocatorGroups[dcLocation]; exist {
allocatorGroup.allocator.Reset()
allocatorGroup.leadership.Reset()
// Reset if it still has the leadership. Otherwise the data race may occur because of the re-campaigning.
if allocatorGroup.leadership.Check() {
allocatorGroup.leadership.Reset()
}
}
}

Expand Down

0 comments on commit 3f53f1e

Please sign in to comment.