-
Notifications
You must be signed in to change notification settings - Fork 711
/
allocator_manager.go
1314 lines (1227 loc) · 47.2 KB
/
allocator_manager.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2020 TiKV Project Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package tso
import (
"context"
"fmt"
"math"
"path"
"strconv"
"strings"
"sync"
"time"
"github.com/pingcap/errors"
"github.com/pingcap/failpoint"
"github.com/pingcap/kvproto/pkg/pdpb"
"github.com/pingcap/log"
"github.com/tikv/pd/pkg/election"
"github.com/tikv/pd/pkg/errs"
mcsutils "github.com/tikv/pd/pkg/mcs/utils"
"github.com/tikv/pd/pkg/member"
"github.com/tikv/pd/pkg/slice"
"github.com/tikv/pd/pkg/storage/endpoint"
"github.com/tikv/pd/pkg/storage/kv"
"github.com/tikv/pd/pkg/utils/etcdutil"
"github.com/tikv/pd/pkg/utils/grpcutil"
"github.com/tikv/pd/pkg/utils/logutil"
"github.com/tikv/pd/pkg/utils/syncutil"
"go.etcd.io/etcd/clientv3"
"go.uber.org/zap"
"google.golang.org/grpc"
)
const (
// GlobalDCLocation is the Global TSO Allocator's DC location label.
GlobalDCLocation = "global"
checkStep = time.Minute
patrolStep = time.Second
defaultAllocatorLeaderLease = 3
localTSOAllocatorEtcdPrefix = "lta"
localTSOSuffixEtcdPrefix = "lts"
)
var (
// PriorityCheck exported is only for test.
PriorityCheck = time.Minute
)
// AllocatorGroupFilter is used to select AllocatorGroup.
type AllocatorGroupFilter func(ag *allocatorGroup) bool
type allocatorGroup struct {
dcLocation string
// ctx is built with cancel from a parent context when set up which can be different
// in order to receive Done() signal correctly.
// cancel would be call when allocatorGroup is deleted to stop background loop.
ctx context.Context
cancel context.CancelFunc
// For the Global TSO Allocator, leadership is a PD leader's
// leadership, and for the Local TSO Allocator, leadership
// is a DC-level certificate to allow an allocator to generate
// TSO for local transactions in its DC.
leadership *election.Leadership
allocator Allocator
}
// DCLocationInfo is used to record some dc-location related info,
// such as suffix sign and server IDs in this dc-location.
type DCLocationInfo struct {
// dc-location/global (string) -> Member IDs
ServerIDs []uint64
// dc-location (string) -> Suffix sign. It is collected and maintained by the PD leader.
Suffix int32
}
func (info *DCLocationInfo) clone() DCLocationInfo {
copiedInfo := DCLocationInfo{
Suffix: info.Suffix,
}
// Make a deep copy here for the slice
copiedInfo.ServerIDs = make([]uint64, len(info.ServerIDs))
copy(copiedInfo.ServerIDs, info.ServerIDs)
return copiedInfo
}
// ElectionMember defines the interface for the election related logic.
type ElectionMember interface {
// ID returns the unique ID in the election group. For example, it can be unique
// server id of a cluster or the unique keyspace group replica id of the election
// group comprised of the replicas of a keyspace group.
ID() uint64
// ID returns the unique Name in the election group.
Name() string
// MemberValue returns the member value.
MemberValue() string
// GetMember() returns the current member
GetMember() interface{}
// Client returns the etcd client.
Client() *clientv3.Client
// IsLeader returns whether the participant is the leader or not by checking its
// leadership's lease and leader info.
IsLeader() bool
// IsLeaderElected returns true if the leader exists; otherwise false.
IsLeaderElected() bool
// CheckLeader checks if someone else is taking the leadership. If yes, returns the leader;
// otherwise returns a bool which indicates if it is needed to check later.
CheckLeader() (leader member.ElectionLeader, checkAgain bool)
// EnableLeader declares the member itself to be the leader.
EnableLeader()
// KeepLeader is used to keep the leader's leadership.
KeepLeader(ctx context.Context)
// CampaignLeader is used to campaign the leadership and make it become a leader in an election group.
CampaignLeader(leaseTimeout int64) error
// ResetLeader is used to reset the member's current leadership.
// Basically it will reset the leader lease and unset leader info.
ResetLeader()
// GetLeaderListenUrls returns current leader's listen urls
GetLeaderListenUrls() []string
// GetLeaderID returns current leader's member ID.
GetLeaderID() uint64
// GetLeaderPath returns the path of the leader.
GetLeaderPath() string
// GetLeadership returns the leadership of the PD member.
GetLeadership() *election.Leadership
// GetLastLeaderUpdatedTime returns the last time when the leader is updated.
GetLastLeaderUpdatedTime() time.Time
// GetDCLocationPathPrefix returns the dc-location path prefix of the cluster.
GetDCLocationPathPrefix() string
// GetDCLocationPath returns the dc-location path of a member with the given member ID.
GetDCLocationPath(id uint64) string
// PreCheckLeader does some pre-check before checking whether it's the leader.
PreCheckLeader() error
}
// AllocatorManager is used to manage the TSO Allocators a PD server holds.
// It is in charge of maintaining TSO allocators' leadership, checking election
// priority, and forwarding TSO allocation requests to correct TSO Allocators.
type AllocatorManager struct {
mu struct {
syncutil.RWMutex
// There are two kinds of TSO Allocators:
// 1. Global TSO Allocator, as a global single point to allocate
// TSO for global transactions, such as cross-region cases.
// 2. Local TSO Allocator, servers for DC-level transactions.
// dc-location/global (string) -> TSO Allocator
allocatorGroups map[string]*allocatorGroup
clusterDCLocations map[string]*DCLocationInfo
// The max suffix sign we have so far, it will be used to calculate
// the number of suffix bits we need in the TSO logical part.
maxSuffix int32
}
// for the synchronization purpose of the allocator update checks
wg sync.WaitGroup
// for the synchronization purpose of the service loops
svcLoopWG sync.WaitGroup
ctx context.Context
cancel context.CancelFunc
// kgID is the keyspace group ID
kgID uint32
// member is for election use
member ElectionMember
// TSO config
rootPath string
storage endpoint.TSOStorage
enableLocalTSO bool
saveInterval time.Duration
updatePhysicalInterval time.Duration
// leaderLease defines the time within which a TSO primary/leader must update its TTL
// in etcd, otherwise etcd will expire the leader key and other servers can campaign
// the primary/leader again. Etcd only supports seconds TTL, so here is second too.
leaderLease int64
maxResetTSGap func() time.Duration
securityConfig *grpcutil.TLSConfig
// for gRPC use
localAllocatorConn struct {
syncutil.RWMutex
clientConns map[string]*grpc.ClientConn
}
}
// NewAllocatorManager creates a new TSO Allocator Manager.
func NewAllocatorManager(
ctx context.Context,
keyspaceGroupID uint32,
member ElectionMember,
rootPath string,
storage endpoint.TSOStorage,
cfg Config,
startGlobalLeaderLoop bool,
) *AllocatorManager {
ctx, cancel := context.WithCancel(ctx)
am := &AllocatorManager{
ctx: ctx,
cancel: cancel,
kgID: keyspaceGroupID,
member: member,
rootPath: rootPath,
storage: storage,
enableLocalTSO: cfg.IsLocalTSOEnabled(),
saveInterval: cfg.GetTSOSaveInterval(),
updatePhysicalInterval: cfg.GetTSOUpdatePhysicalInterval(),
leaderLease: cfg.GetLeaderLease(),
maxResetTSGap: cfg.GetMaxResetTSGap,
securityConfig: cfg.GetTLSConfig(),
}
am.mu.allocatorGroups = make(map[string]*allocatorGroup)
am.mu.clusterDCLocations = make(map[string]*DCLocationInfo)
am.localAllocatorConn.clientConns = make(map[string]*grpc.ClientConn)
// Set up the Global TSO Allocator here, it will be initialized once the member campaigns leader successfully.
am.SetUpGlobalAllocator(am.ctx, am.member.GetLeadership(), startGlobalLeaderLoop)
am.svcLoopWG.Add(1)
go am.tsoAllocatorLoop()
return am
}
// SetUpGlobalAllocator is used to set up the global allocator, which will initialize the allocator and put it into
// an allocator daemon. An TSO Allocator should only be set once, and may be initialized and reset multiple times
// depending on the election.
func (am *AllocatorManager) SetUpGlobalAllocator(ctx context.Context, leadership *election.Leadership, startGlobalLeaderLoop bool) {
am.mu.Lock()
defer am.mu.Unlock()
allocator := NewGlobalTSOAllocator(ctx, am, startGlobalLeaderLoop)
// Create a new allocatorGroup
ctx, cancel := context.WithCancel(ctx)
am.mu.allocatorGroups[GlobalDCLocation] = &allocatorGroup{
dcLocation: GlobalDCLocation,
ctx: ctx,
cancel: cancel,
leadership: leadership,
allocator: allocator,
}
}
// setUpLocalAllocator is used to set up an allocator, which will initialize the allocator and put it into allocator daemon.
// One TSO Allocator should only be set once, and may be initialized and reset multiple times depending on the election.
func (am *AllocatorManager) setUpLocalAllocator(parentCtx context.Context, dcLocation string, leadership *election.Leadership) {
am.mu.Lock()
defer am.mu.Unlock()
if _, exist := am.mu.allocatorGroups[dcLocation]; exist {
return
}
allocator := NewLocalTSOAllocator(am, leadership, dcLocation)
// Create a new allocatorGroup
ctx, cancel := context.WithCancel(parentCtx)
am.mu.allocatorGroups[dcLocation] = &allocatorGroup{
dcLocation: dcLocation,
ctx: ctx,
cancel: cancel,
leadership: leadership,
allocator: allocator,
}
// Start election of the Local TSO Allocator here
localTSOAllocator, _ := allocator.(*LocalTSOAllocator)
go am.allocatorLeaderLoop(parentCtx, localTSOAllocator)
}
// tsoAllocatorLoop is used to run the TSO Allocator updating daemon.
func (am *AllocatorManager) tsoAllocatorLoop() {
defer logutil.LogPanic()
defer am.svcLoopWG.Done()
am.AllocatorDaemon(am.ctx)
log.Info("exit allocator loop")
}
// close is used to shutdown TSO Allocator updating daemon.
// tso service call this function to shutdown the loop here, but pd manages its own loop.
func (am *AllocatorManager) close() {
log.Info("closing the allocator manager")
if allocatorGroup, exist := am.getAllocatorGroup(GlobalDCLocation); exist {
allocatorGroup.allocator.(*GlobalTSOAllocator).close()
}
am.cancel()
am.svcLoopWG.Wait()
log.Info("closed the allocator manager")
}
func (am *AllocatorManager) getMember() ElectionMember {
return am.member
}
// SetLocalTSOConfig receives the zone label of this PD server and write it into etcd as dc-location
// to make the whole cluster know the DC-level topology for later Local TSO Allocator campaign.
func (am *AllocatorManager) SetLocalTSOConfig(dcLocation string) error {
serverName := am.member.Name()
serverID := am.member.ID()
if err := am.checkDCLocationUpperLimit(dcLocation); err != nil {
log.Error("check dc-location upper limit failed",
zap.Int("upper-limit", int(math.Pow(2, MaxSuffixBits))-1),
zap.String("dc-location", dcLocation),
zap.String("server-name", serverName),
zap.Uint64("server-id", serverID),
errs.ZapError(err))
return err
}
// The key-value pair in etcd will be like: serverID -> dcLocation
dcLocationKey := am.member.GetDCLocationPath(serverID)
resp, err := kv.
NewSlowLogTxn(am.member.Client()).
Then(clientv3.OpPut(dcLocationKey, dcLocation)).
Commit()
if err != nil {
return errs.ErrEtcdTxnInternal.Wrap(err).GenWithStackByCause()
}
if !resp.Succeeded {
log.Warn("write dc-location configuration into etcd failed",
zap.String("dc-location", dcLocation),
zap.String("server-name", serverName),
zap.Uint64("server-id", serverID))
return errs.ErrEtcdTxnConflict.FastGenByArgs()
}
log.Info("write dc-location configuration into etcd",
zap.String("dc-location", dcLocation),
zap.String("server-name", serverName),
zap.Uint64("server-id", serverID))
go am.ClusterDCLocationChecker()
return nil
}
func (am *AllocatorManager) checkDCLocationUpperLimit(dcLocation string) error {
clusterDCLocations, err := am.GetClusterDCLocationsFromEtcd()
if err != nil {
return err
}
// It's ok to add a new PD to the old dc-location.
if _, ok := clusterDCLocations[dcLocation]; ok {
return nil
}
// Check whether the dc-location number meets the upper limit 2**(LogicalBits-1)-1,
// which includes 1 global and 2**(LogicalBits-1) local
if len(clusterDCLocations) == int(math.Pow(2, MaxSuffixBits))-1 {
return errs.ErrSetLocalTSOConfig.FastGenByArgs("the number of dc-location meets the upper limit")
}
return nil
}
// GetClusterDCLocationsFromEtcd fetches dcLocation topology from etcd
func (am *AllocatorManager) GetClusterDCLocationsFromEtcd() (clusterDCLocations map[string][]uint64, err error) {
resp, err := etcdutil.EtcdKVGet(
am.member.Client(),
am.member.GetDCLocationPathPrefix(),
clientv3.WithPrefix())
if err != nil {
return clusterDCLocations, err
}
clusterDCLocations = make(map[string][]uint64)
for _, kv := range resp.Kvs {
// The key will contain the member ID and the value is its dcLocation
serverPath := strings.Split(string(kv.Key), "/")
// Get serverID from serverPath, e.g, /pd/dc-location/1232143243253 -> 1232143243253
serverID, err := strconv.ParseUint(serverPath[len(serverPath)-1], 10, 64)
dcLocation := string(kv.Value)
if err != nil {
log.Warn("get server id and dcLocation from etcd failed, invalid server id",
zap.Any("splitted-serverPath", serverPath),
zap.String("dc-location", dcLocation),
errs.ZapError(err))
continue
}
clusterDCLocations[dcLocation] = append(clusterDCLocations[dcLocation], serverID)
}
return clusterDCLocations, nil
}
// GetDCLocationInfo returns a copy of DCLocationInfo of the given dc-location,
func (am *AllocatorManager) GetDCLocationInfo(dcLocation string) (DCLocationInfo, bool) {
am.mu.RLock()
defer am.mu.RUnlock()
infoPtr, ok := am.mu.clusterDCLocations[dcLocation]
if !ok {
return DCLocationInfo{}, false
}
return infoPtr.clone(), true
}
// CleanUpDCLocation cleans up certain server's DCLocationInfo
func (am *AllocatorManager) CleanUpDCLocation() error {
serverID := am.member.ID()
dcLocationKey := am.member.GetDCLocationPath(serverID)
// remove dcLocationKey from etcd
if resp, err := kv.
NewSlowLogTxn(am.member.Client()).
Then(clientv3.OpDelete(dcLocationKey)).
Commit(); err != nil {
return errs.ErrEtcdTxnInternal.Wrap(err).GenWithStackByCause()
} else if !resp.Succeeded {
return errs.ErrEtcdTxnConflict.FastGenByArgs()
}
log.Info("delete the dc-location key previously written in etcd", zap.Uint64("server-id", serverID))
go am.ClusterDCLocationChecker()
return nil
}
// GetClusterDCLocations returns all dc-locations of a cluster with a copy of map,
// which satisfies dcLocation -> DCLocationInfo.
func (am *AllocatorManager) GetClusterDCLocations() map[string]DCLocationInfo {
am.mu.RLock()
defer am.mu.RUnlock()
dcLocationMap := make(map[string]DCLocationInfo)
for dcLocation, info := range am.mu.clusterDCLocations {
dcLocationMap[dcLocation] = info.clone()
}
return dcLocationMap
}
// GetClusterDCLocationsNumber returns the number of cluster dc-locations.
func (am *AllocatorManager) GetClusterDCLocationsNumber() int {
am.mu.RLock()
defer am.mu.RUnlock()
return len(am.mu.clusterDCLocations)
}
// compareAndSetMaxSuffix sets the max suffix sign if suffix is greater than am.mu.maxSuffix.
func (am *AllocatorManager) compareAndSetMaxSuffix(suffix int32) {
am.mu.Lock()
defer am.mu.Unlock()
if suffix > am.mu.maxSuffix {
am.mu.maxSuffix = suffix
}
}
// GetSuffixBits calculates the bits of suffix sign
// by the max number of suffix so far,
// which will be used in the TSO logical part.
func (am *AllocatorManager) GetSuffixBits() int {
am.mu.RLock()
defer am.mu.RUnlock()
return CalSuffixBits(am.mu.maxSuffix)
}
// CalSuffixBits calculates the bits of suffix by the max suffix sign.
func CalSuffixBits(maxSuffix int32) int {
// maxSuffix + 1 because we have the Global TSO holds 0 as the suffix sign
return int(math.Ceil(math.Log2(float64(maxSuffix + 1))))
}
func (am *AllocatorManager) getAllocatorPath(dcLocation string) string {
// For backward compatibility, the global timestamp's store path will still use the old one
if dcLocation == GlobalDCLocation {
return am.rootPath
}
return path.Join(am.getLocalTSOAllocatorPath(), dcLocation)
}
// Add a prefix to the root path to prevent being conflicted
// with other system key paths such as leader, member, alloc_id, raft, etc.
func (am *AllocatorManager) getLocalTSOAllocatorPath() string {
return path.Join(am.rootPath, localTSOAllocatorEtcdPrefix)
}
// similar logic with leaderLoop in server/server.go
func (am *AllocatorManager) allocatorLeaderLoop(ctx context.Context, allocator *LocalTSOAllocator) {
defer logutil.LogPanic()
defer log.Info("server is closed, return local tso allocator leader loop",
zap.String("dc-location", allocator.GetDCLocation()),
zap.String("local-tso-allocator-name", am.member.Name()))
for {
select {
case <-ctx.Done():
return
default:
}
// Check whether the Local TSO Allocator has the leader already
allocatorLeader, rev, checkAgain := allocator.CheckAllocatorLeader()
if checkAgain {
continue
}
if allocatorLeader != nil {
log.Info("start to watch allocator leader",
zap.Stringer(fmt.Sprintf("%s-allocator-leader", allocator.GetDCLocation()), allocatorLeader),
zap.String("local-tso-allocator-name", am.member.Name()))
// WatchAllocatorLeader will keep looping and never return unless the Local TSO Allocator leader has changed.
allocator.WatchAllocatorLeader(ctx, allocatorLeader, rev)
log.Info("local tso allocator leader has changed, try to re-campaign a local tso allocator leader",
zap.String("dc-location", allocator.GetDCLocation()))
}
// Check the next-leader key
nextLeader, err := am.getNextLeaderID(allocator.GetDCLocation())
if err != nil {
log.Error("get next leader from etcd failed",
zap.String("dc-location", allocator.GetDCLocation()),
errs.ZapError(err))
time.Sleep(200 * time.Millisecond)
continue
}
isNextLeader := false
if nextLeader != 0 {
if nextLeader != am.member.ID() {
log.Info("skip campaigning of the local tso allocator leader and check later",
zap.String("server-name", am.member.Name()),
zap.Uint64("server-id", am.member.ID()),
zap.Uint64("next-leader-id", nextLeader))
time.Sleep(200 * time.Millisecond)
continue
}
isNextLeader = true
}
// Make sure the leader is aware of this new dc-location in order to make the
// Global TSO synchronization can cover up this dc-location.
ok, dcLocationInfo, err := am.getDCLocationInfoFromLeader(ctx, allocator.GetDCLocation())
if err != nil {
log.Error("get dc-location info from pd leader failed",
zap.String("dc-location", allocator.GetDCLocation()),
errs.ZapError(err))
// PD leader hasn't been elected out, wait for the campaign
if !longSleep(ctx, time.Second) {
return
}
continue
}
if !ok || dcLocationInfo.Suffix <= 0 || dcLocationInfo.MaxTs == nil {
log.Warn("pd leader is not aware of dc-location during allocatorLeaderLoop, wait next round",
zap.String("dc-location", allocator.GetDCLocation()),
zap.Any("dc-location-info", dcLocationInfo),
zap.String("wait-duration", checkStep.String()))
// Because the checkStep is long, we use select here to check whether the ctx is done
// to prevent the leak of goroutine.
if !longSleep(ctx, checkStep) {
return
}
continue
}
am.campaignAllocatorLeader(ctx, allocator, dcLocationInfo, isNextLeader)
}
}
// longSleep is used to sleep the long wait duration while also watching the
// ctx.Done() to prevent the goroutine from leaking. This function returns
// true if the sleep is over, false if the ctx is done.
func longSleep(ctx context.Context, waitStep time.Duration) bool {
waitTicker := time.NewTicker(waitStep)
defer waitTicker.Stop()
select {
case <-ctx.Done():
return false
case <-waitTicker.C:
return true
}
}
func (am *AllocatorManager) campaignAllocatorLeader(
loopCtx context.Context,
allocator *LocalTSOAllocator,
dcLocationInfo *pdpb.GetDCLocationInfoResponse,
isNextLeader bool,
) {
log.Info("start to campaign local tso allocator leader",
zap.String("dc-location", allocator.GetDCLocation()),
zap.Any("dc-location-info", dcLocationInfo),
zap.String("name", am.member.Name()))
cmps := make([]clientv3.Cmp, 0)
nextLeaderKey := am.nextLeaderKey(allocator.GetDCLocation())
if !isNextLeader {
cmps = append(cmps, clientv3.Compare(clientv3.CreateRevision(nextLeaderKey), "=", 0))
} else {
nextLeaderValue := fmt.Sprintf("%v", am.member.ID())
cmps = append(cmps, clientv3.Compare(clientv3.Value(nextLeaderKey), "=", nextLeaderValue))
}
failpoint.Inject("injectNextLeaderKey", func(val failpoint.Value) {
if val.(bool) {
// In order not to campaign leader too often in tests
time.Sleep(5 * time.Second)
cmps = []clientv3.Cmp{
clientv3.Compare(clientv3.Value(nextLeaderKey), "=", "mockValue"),
}
}
})
if err := allocator.CampaignAllocatorLeader(am.leaderLease, cmps...); err != nil {
if err.Error() == errs.ErrEtcdTxnConflict.Error() {
log.Info("failed to campaign local tso allocator leader due to txn conflict, another allocator may campaign successfully",
zap.String("dc-location", allocator.GetDCLocation()),
zap.Any("dc-location-info", dcLocationInfo),
zap.String("name", am.member.Name()))
} else {
log.Error("failed to campaign local tso allocator leader due to etcd error",
zap.String("dc-location", allocator.GetDCLocation()),
zap.Any("dc-location-info", dcLocationInfo),
zap.String("name", am.member.Name()),
errs.ZapError(err))
}
return
}
// Start keepalive the Local TSO Allocator leadership and enable Local TSO service.
ctx, cancel := context.WithCancel(loopCtx)
defer cancel()
defer am.ResetAllocatorGroup(allocator.GetDCLocation())
// Maintain the Local TSO Allocator leader
go allocator.KeepAllocatorLeader(ctx)
log.Info("campaign local tso allocator leader ok",
zap.String("dc-location", allocator.GetDCLocation()),
zap.Any("dc-location-info", dcLocationInfo),
zap.String("name", am.member.Name()))
log.Info("initialize the local TSO allocator",
zap.String("dc-location", allocator.GetDCLocation()),
zap.Any("dc-location-info", dcLocationInfo),
zap.String("name", am.member.Name()))
if err := allocator.Initialize(int(dcLocationInfo.Suffix)); err != nil {
log.Error("failed to initialize the local TSO allocator",
zap.String("dc-location", allocator.GetDCLocation()),
zap.Any("dc-location-info", dcLocationInfo),
errs.ZapError(err))
return
}
if dcLocationInfo.GetMaxTs().GetPhysical() != 0 {
if err := allocator.WriteTSO(dcLocationInfo.GetMaxTs()); err != nil {
log.Error("failed to write the max local TSO after member changed",
zap.String("dc-location", allocator.GetDCLocation()),
zap.Any("dc-location-info", dcLocationInfo),
errs.ZapError(err))
return
}
}
am.compareAndSetMaxSuffix(dcLocationInfo.Suffix)
allocator.EnableAllocatorLeader()
// The next leader is me, delete it to finish campaigning
am.deleteNextLeaderID(allocator.GetDCLocation())
log.Info("local tso allocator leader is ready to serve",
zap.String("dc-location", allocator.GetDCLocation()),
zap.Any("dc-location-info", dcLocationInfo),
zap.String("name", am.member.Name()))
leaderTicker := time.NewTicker(mcsutils.LeaderTickInterval)
defer leaderTicker.Stop()
for {
select {
case <-leaderTicker.C:
if !allocator.IsAllocatorLeader() {
log.Info("no longer a local tso allocator leader because lease has expired, local tso allocator leader will step down",
zap.String("dc-location", allocator.GetDCLocation()),
zap.Any("dc-location-info", dcLocationInfo),
zap.String("name", am.member.Name()))
return
}
case <-ctx.Done():
// Server is closed and it should return nil.
log.Info("server is closed, reset the local tso allocator",
zap.String("dc-location", allocator.GetDCLocation()),
zap.Any("dc-location-info", dcLocationInfo),
zap.String("name", am.member.Name()))
return
}
}
}
// AllocatorDaemon is used to update every allocator's TSO and check whether we have
// any new local allocator that needs to be set up.
func (am *AllocatorManager) AllocatorDaemon(ctx context.Context) {
log.Info("entering into allocator daemon")
// allocatorPatroller should only work when enableLocalTSO is true to
// set up the new Local TSO Allocator in time.
var patrolTicker = &time.Ticker{}
if am.enableLocalTSO {
patrolTicker = time.NewTicker(patrolStep)
defer patrolTicker.Stop()
}
tsTicker := time.NewTicker(am.updatePhysicalInterval)
failpoint.Inject("fastUpdatePhysicalInterval", func() {
tsTicker.Stop()
tsTicker = time.NewTicker(time.Millisecond)
})
defer tsTicker.Stop()
checkerTicker := time.NewTicker(PriorityCheck)
defer checkerTicker.Stop()
for {
select {
case <-patrolTicker.C:
// Inspect the cluster dc-location info and set up the new Local TSO Allocator in time.
am.allocatorPatroller(ctx)
case <-tsTicker.C:
// Update the initialized TSO Allocator to advance TSO.
am.allocatorUpdater()
case <-checkerTicker.C:
// Check and maintain the cluster's meta info about dc-location distribution.
go am.ClusterDCLocationChecker()
// We won't have any Local TSO Allocator set up in PD without enabling Local TSO.
if am.enableLocalTSO {
// Check the election priority of every Local TSO Allocator this PD is holding.
go am.PriorityChecker()
}
// PS: ClusterDCLocationChecker and PriorityChecker are time consuming and low frequent to run,
// we should run them concurrently to speed up the progress.
case <-ctx.Done():
log.Info("exit allocator daemon")
return
}
}
}
// Update the Local TSO Allocator leaders TSO in memory concurrently.
func (am *AllocatorManager) allocatorUpdater() {
// Filter out allocators without leadership and uninitialized
allocatorGroups := am.getAllocatorGroups(FilterUninitialized(), FilterUnavailableLeadership())
// Update each allocator concurrently
for _, ag := range allocatorGroups {
am.wg.Add(1)
go am.updateAllocator(ag)
}
am.wg.Wait()
}
// updateAllocator is used to update the allocator in the group.
func (am *AllocatorManager) updateAllocator(ag *allocatorGroup) {
defer logutil.LogPanic()
defer am.wg.Done()
select {
case <-ag.ctx.Done():
// Resetting the allocator will clear TSO in memory
ag.allocator.Reset()
log.Info("exit the allocator update loop")
return
default:
}
if !ag.leadership.Check() {
log.Info("allocator doesn't campaign leadership yet", zap.String("dc-location", ag.dcLocation))
time.Sleep(200 * time.Millisecond)
return
}
if err := ag.allocator.UpdateTSO(); err != nil {
log.Warn("failed to update allocator's timestamp",
zap.String("dc-location", ag.dcLocation), errs.ZapError(err))
am.ResetAllocatorGroup(ag.dcLocation)
return
}
}
// Check if we have any new dc-location configured, if yes,
// then set up the corresponding local allocator.
func (am *AllocatorManager) allocatorPatroller(serverCtx context.Context) {
// Collect all dc-locations
dcLocations := am.GetClusterDCLocations()
// Get all Local TSO Allocators
allocatorGroups := am.getAllocatorGroups(FilterDCLocation(GlobalDCLocation))
// Set up the new one
for dcLocation := range dcLocations {
if slice.NoneOf(allocatorGroups, func(i int) bool {
return allocatorGroups[i].dcLocation == dcLocation
}) {
am.setUpLocalAllocator(serverCtx, dcLocation, election.NewLeadership(
am.member.Client(),
am.getAllocatorPath(dcLocation),
fmt.Sprintf("%s local allocator leader election", dcLocation),
))
}
}
// Clean up the unused one
for _, ag := range allocatorGroups {
if _, exist := dcLocations[ag.dcLocation]; !exist {
am.deleteAllocatorGroup(ag.dcLocation)
}
}
}
// ClusterDCLocationChecker collects all dc-locations of a cluster, computes some related info
// and stores them into the DCLocationInfo, then finally writes them into am.mu.clusterDCLocations.
func (am *AllocatorManager) ClusterDCLocationChecker() {
defer logutil.LogPanic()
// Wait for the group leader to be elected out.
if !am.member.IsLeaderElected() {
return
}
newClusterDCLocations, err := am.GetClusterDCLocationsFromEtcd()
if err != nil {
log.Error("get cluster dc-locations from etcd failed", errs.ZapError(err))
return
}
am.mu.Lock()
// Clean up the useless dc-locations
for dcLocation := range am.mu.clusterDCLocations {
if _, ok := newClusterDCLocations[dcLocation]; !ok {
delete(am.mu.clusterDCLocations, dcLocation)
}
}
// May be used to rollback the updating after
newDCLocations := make([]string, 0)
// Update the new dc-locations
for dcLocation, serverIDs := range newClusterDCLocations {
if _, ok := am.mu.clusterDCLocations[dcLocation]; !ok {
am.mu.clusterDCLocations[dcLocation] = &DCLocationInfo{
ServerIDs: serverIDs,
Suffix: -1,
}
newDCLocations = append(newDCLocations, dcLocation)
}
}
// Only leader can write the TSO suffix to etcd in order to make it consistent in the cluster
if am.member.IsLeader() {
for dcLocation, info := range am.mu.clusterDCLocations {
if info.Suffix > 0 {
continue
}
suffix, err := am.getOrCreateLocalTSOSuffix(dcLocation)
if err != nil {
log.Warn("get or create the local tso suffix failed",
zap.String("dc-location", dcLocation), errs.ZapError(err))
continue
}
if suffix > am.mu.maxSuffix {
am.mu.maxSuffix = suffix
}
am.mu.clusterDCLocations[dcLocation].Suffix = suffix
}
} else {
// Follower should check and update the am.mu.maxSuffix
maxSuffix, err := am.getMaxLocalTSOSuffix()
if err != nil {
log.Error("get the max local tso suffix from etcd failed", errs.ZapError(err))
// Rollback the new dc-locations we update before
for _, dcLocation := range newDCLocations {
delete(am.mu.clusterDCLocations, dcLocation)
}
} else if maxSuffix > am.mu.maxSuffix {
am.mu.maxSuffix = maxSuffix
}
}
am.mu.Unlock()
}
// getOrCreateLocalTSOSuffix will check whether we have the Local TSO suffix written into etcd.
// If not, it will write a number into etcd according to the its joining order.
// If yes, it will just return the previous persisted one.
func (am *AllocatorManager) getOrCreateLocalTSOSuffix(dcLocation string) (int32, error) {
// Try to get the suffix from etcd
dcLocationSuffix, err := am.getDCLocationSuffixMapFromEtcd()
if err != nil {
return -1, nil
}
var maxSuffix int32
for curDCLocation, suffix := range dcLocationSuffix {
// If we already have the suffix persisted in etcd before,
// just use it as the result directly.
if curDCLocation == dcLocation {
return suffix, nil
}
if suffix > maxSuffix {
maxSuffix = suffix
}
}
maxSuffix++
localTSOSuffixKey := am.GetLocalTSOSuffixPath(dcLocation)
// The Local TSO suffix is determined by the joining order of this dc-location.
localTSOSuffixValue := strconv.FormatInt(int64(maxSuffix), 10)
txnResp, err := kv.NewSlowLogTxn(am.member.Client()).
If(clientv3.Compare(clientv3.CreateRevision(localTSOSuffixKey), "=", 0)).
Then(clientv3.OpPut(localTSOSuffixKey, localTSOSuffixValue)).
Commit()
if err != nil {
return -1, errs.ErrEtcdTxnInternal.Wrap(err).GenWithStackByCause()
}
if !txnResp.Succeeded {
log.Warn("write local tso suffix into etcd failed",
zap.String("dc-location", dcLocation),
zap.String("local-tso-suffix", localTSOSuffixValue),
zap.String("server-name", am.member.Name()),
zap.Uint64("server-id", am.member.ID()))
return -1, errs.ErrEtcdTxnConflict.FastGenByArgs()
}
return maxSuffix, nil
}
func (am *AllocatorManager) getDCLocationSuffixMapFromEtcd() (map[string]int32, error) {
resp, err := etcdutil.EtcdKVGet(
am.member.Client(),
am.GetLocalTSOSuffixPathPrefix(),
clientv3.WithPrefix())
if err != nil {
return nil, err
}
dcLocationSuffix := make(map[string]int32)
for _, kv := range resp.Kvs {
suffix, err := strconv.ParseInt(string(kv.Value), 10, 32)
if err != nil {
return nil, err
}
splittedKey := strings.Split(string(kv.Key), "/")
dcLocation := splittedKey[len(splittedKey)-1]
dcLocationSuffix[dcLocation] = int32(suffix)
}
return dcLocationSuffix, nil
}
func (am *AllocatorManager) getMaxLocalTSOSuffix() (int32, error) {
// Try to get the suffix from etcd
dcLocationSuffix, err := am.getDCLocationSuffixMapFromEtcd()
if err != nil {
return -1, err
}
var maxSuffix int32
for _, suffix := range dcLocationSuffix {
if suffix > maxSuffix {
maxSuffix = suffix
}
}
return maxSuffix, nil
}
// GetLocalTSOSuffixPathPrefix returns the etcd key prefix of the Local TSO suffix for the given dc-location.
func (am *AllocatorManager) GetLocalTSOSuffixPathPrefix() string {
return path.Join(am.rootPath, localTSOSuffixEtcdPrefix)
}
// GetLocalTSOSuffixPath returns the etcd key of the Local TSO suffix for the given dc-location.
func (am *AllocatorManager) GetLocalTSOSuffixPath(dcLocation string) string {
return path.Join(am.GetLocalTSOSuffixPathPrefix(), dcLocation)
}
// PriorityChecker is used to check the election priority of a Local TSO Allocator.
// In the normal case, if we want to elect a Local TSO Allocator for a certain DC,
// such as dc-1, we need to make sure the follow priority rules:
// 1. The PD server with dc-location="dc-1" needs to be elected as the allocator
// leader with the highest priority.
// 2. If all PD servers with dc-location="dc-1" are down, then the other PD servers
// of DC could be elected.
func (am *AllocatorManager) PriorityChecker() {
defer logutil.LogPanic()
serverID := am.member.ID()
myServerDCLocation := am.getServerDCLocation(serverID)
// Check all Local TSO Allocator followers to see if their priorities is higher than the leaders
// Filter out allocators with leadership and initialized
allocatorGroups := am.getAllocatorGroups(FilterDCLocation(GlobalDCLocation), FilterAvailableLeadership())
for _, allocatorGroup := range allocatorGroups {
localTSOAllocator, _ := allocatorGroup.allocator.(*LocalTSOAllocator)
leaderServerID := localTSOAllocator.GetAllocatorLeader().GetMemberId()
// No leader, maybe the leader is not been watched yet
if leaderServerID == 0 {
continue
}
leaderServerDCLocation := am.getServerDCLocation(leaderServerID)
// For example, an allocator leader for dc-1 is elected by a server of dc-2, then the server of dc-1 will
// find this allocator's dc-location isn't the same with server of dc-2 but is same with itself.
if allocatorGroup.dcLocation != leaderServerDCLocation && allocatorGroup.dcLocation == myServerDCLocation {
log.Info("try to move the local tso allocator",
zap.Uint64("old-leader-id", leaderServerID),
zap.String("old-dc-location", leaderServerDCLocation),
zap.Uint64("next-leader-id", serverID),
zap.String("next-dc-location", myServerDCLocation))
if err := am.transferLocalAllocator(allocatorGroup.dcLocation, am.member.ID()); err != nil {
log.Error("move the local tso allocator failed",
zap.Uint64("old-leader-id", leaderServerID),
zap.String("old-dc-location", leaderServerDCLocation),
zap.Uint64("next-leader-id", serverID),
zap.String("next-dc-location", myServerDCLocation),
errs.ZapError(err))
continue
}
}
}
// Check next leader and resign
// Filter out allocators with leadership
allocatorGroups = am.getAllocatorGroups(FilterDCLocation(GlobalDCLocation), FilterUnavailableLeadership())
for _, allocatorGroup := range allocatorGroups {
nextLeader, err := am.getNextLeaderID(allocatorGroup.dcLocation)
if err != nil {
log.Error("get next leader from etcd failed",
zap.String("dc-location", allocatorGroup.dcLocation), errs.ZapError(err))
continue
}
// nextLeader is not empty and isn't same with the server ID, resign the leader
if nextLeader != 0 && nextLeader != serverID {
log.Info("next leader key found, resign current leader", zap.Uint64("nextLeaderID", nextLeader))
am.ResetAllocatorGroup(allocatorGroup.dcLocation)
}
}
}
// TransferAllocatorForDCLocation transfer local tso allocator to the target member for the given dcLocation
func (am *AllocatorManager) TransferAllocatorForDCLocation(dcLocation string, memberID uint64) error {
if dcLocation == GlobalDCLocation {
return fmt.Errorf("dc-location %v should be transferred by transfer leader", dcLocation)