func (lta *LocalTSOAllocator) KeepAllocatorLeader(ctx context.Context) {
	lta.leadership.Keep(ctx)
}
KeepAllocatorLeader is used to keep the PD leader's leadership.
package tso
import (
	"context"
	"fmt"
	"sync/atomic"
	"time"
	"github.com/pingcap/kvproto/pkg/pdpb"
	"github.com/pingcap/log"
	"github.com/tikv/pd/pkg/errs"
	"github.com/tikv/pd/pkg/tsoutil"
	"github.com/tikv/pd/pkg/typeutil"
	"github.com/tikv/pd/server/election"
	"go.etcd.io/etcd/clientv3"
	"go.uber.org/zap"
)
type LocalTSOAllocator struct {
	allocatorManager *AllocatorManager
	leadership      *election.Leadership
	timestampOracle *timestampOracle
	rootPath        string
	allocatorLeader atomic.Value  
}
func NewLocalTSOAllocator(
	am *AllocatorManager,
	leadership *election.Leadership,
	dcLocation string,
) Allocator {
	return &LocalTSOAllocator{
		allocatorManager: am,
		leadership:       leadership,
		timestampOracle: &timestampOracle{
			client:                 leadership.GetClient(),
			rootPath:               leadership.GetLeaderKey(),
			saveInterval:           am.saveInterval,
			updatePhysicalInterval: am.updatePhysicalInterval,
			maxResetTSGap:          am.maxResetTSGap,
			dcLocation:             dcLocation,
			tsoMux:                 &tsoObject{},
		},
		rootPath: leadership.GetLeaderKey(),
	}
}
func (lta *LocalTSOAllocator) GetDCLocation() string {
	return lta.timestampOracle.dcLocation
}
func (lta *LocalTSOAllocator) Initialize(suffix int) error {
	tsoAllocatorRole.WithLabelValues(lta.timestampOracle.dcLocation).Set(1)
	lta.timestampOracle.suffix = suffix
	return lta.timestampOracle.SyncTimestamp(lta.leadership)
}
func (lta *LocalTSOAllocator) IsInitialize() bool {
	return lta.timestampOracle.isInitialized()
}
func (lta *LocalTSOAllocator) UpdateTSO() error {
	return lta.timestampOracle.UpdateTimestamp(lta.leadership)
}
func (lta *LocalTSOAllocator) SetTSO(tso uint64) error {
	return lta.timestampOracle.resetUserTimestamp(lta.leadership, tso, false)
}
func (lta *LocalTSOAllocator) GenerateTSO(count uint32) (pdpb.Timestamp, error) {
	if !lta.leadership.Check() {
		tsoCounter.WithLabelValues("not_leader", lta.timestampOracle.dcLocation).Inc()
		return pdpb.Timestamp{}, errs.ErrGenerateTimestamp.FastGenByArgs(fmt.Sprintf("requested pd %s of %s allocator", errs.NotLeaderErr, lta.timestampOracle.dcLocation))
	}
	return lta.timestampOracle.getTS(lta.leadership, count, lta.allocatorManager.GetSuffixBits())
}
func (lta *LocalTSOAllocator) Reset() {
	tsoAllocatorRole.WithLabelValues(lta.timestampOracle.dcLocation).Set(0)
	lta.timestampOracle.ResetTimestamp()
}
func (lta *LocalTSOAllocator) setAllocatorLeader(member *pdpb.Member) {
	lta.allocatorLeader.Store(member)
}
func (lta *LocalTSOAllocator) unsetAllocatorLeader() {
	lta.allocatorLeader.Store(&pdpb.Member{})
}
func (lta *LocalTSOAllocator) GetAllocatorLeader() *pdpb.Member {
	allocatorLeader := lta.allocatorLeader.Load()
	if allocatorLeader == nil {
		return nil
	}
	return allocatorLeader.(*pdpb.Member)
}
func (lta *LocalTSOAllocator) GetMember() *pdpb.Member {
	return lta.allocatorManager.member.Member()
}
func (lta *LocalTSOAllocator) GetCurrentTSO() (*pdpb.Timestamp, error) {
	currentPhysical, currentLogical := lta.timestampOracle.getTSO()
	if currentPhysical == typeutil.ZeroTime {
		return &pdpb.Timestamp{}, errs.ErrGenerateTimestamp.FastGenByArgs("timestamp in memory isn't initialized")
	}
	return tsoutil.GenerateTimestamp(currentPhysical, uint64(currentLogical)), nil
}
func (lta *LocalTSOAllocator) WriteTSO(maxTS *pdpb.Timestamp) error {
	currentTSO, err := lta.GetCurrentTSO()
	if err != nil {
		return err
	}
	if tsoutil.CompareTimestamp(currentTSO, maxTS) >= 0 {
		return nil
	}
	return lta.timestampOracle.resetUserTimestamp(lta.leadership, tsoutil.GenerateTS(maxTS), true)
}
func (lta *LocalTSOAllocator) EnableAllocatorLeader() {
	lta.setAllocatorLeader(lta.allocatorManager.member.Member())
}
func (lta *LocalTSOAllocator) CampaignAllocatorLeader(leaseTimeout int64, cmps ...clientv3.Cmp) error {
	return lta.leadership.Campaign(leaseTimeout, lta.allocatorManager.member.MemberValue(), cmps...)
}