From b68f1980cd1ec7845b983b8e78f2ad57d7ece5f8 Mon Sep 17 00:00:00 2001 From: husharp Date: Thu, 5 Sep 2024 16:02:22 +0800 Subject: [PATCH] refine priority Signed-off-by: husharp --- pkg/tso/keyspace_group_manager.go | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/pkg/tso/keyspace_group_manager.go b/pkg/tso/keyspace_group_manager.go index 0e62fc0346c..65e6de57d04 100644 --- a/pkg/tso/keyspace_group_manager.go +++ b/pkg/tso/keyspace_group_manager.go @@ -33,6 +33,7 @@ import ( "github.com/tikv/pd/pkg/election" "github.com/tikv/pd/pkg/errs" "github.com/tikv/pd/pkg/mcs/discovery" + "github.com/tikv/pd/pkg/mcs/utils" "github.com/tikv/pd/pkg/mcs/utils/constant" "github.com/tikv/pd/pkg/member" "github.com/tikv/pd/pkg/slice" @@ -647,11 +648,28 @@ func (kgm *KeyspaceGroupManager) primaryPriorityCheckLoop() { select { case <-ctx.Done(): default: - member.ResetLeader() - log.Info("reset primary", + allocator, err := kgm.GetAllocatorManager(kg.ID) + if err != nil { + continue + } + globalAllocator, err := allocator.GetAllocator(GlobalDCLocation) + if err != nil { + continue + } + // only members of specific group are valid primary candidates. + group := kgm.GetKeyspaceGroups()[kg.ID] + memberMap := make(map[string]bool, len(group.Members)) + for _, m := range group.Members { + memberMap[m.Address] = true + } + log.Info("tso priority checker moves primary", zap.String("local-address", kgm.tsoServiceID.ServiceAddr), zap.Uint32("keyspace-group-id", kg.ID), zap.Int("local-priority", localPriority)) + if err := utils.TransferPrimary(kgm.etcdClient, globalAllocator.(*GlobalTSOAllocator).GetExpectedPrimaryLease(), + constant.TSOServiceName, kgm.GetServiceConfig().GetName(), "", kg.ID, memberMap); err != nil { + continue + } } } else { log.Warn("no need to reset primary as the replicas with higher priority are offline",