Skip to content

Commit bb84d1f

Browse files
authored
autoid_service: fix potential autoid decrease when leader change or close for AUTO_ID_CACHE=1 (#52602)
close #52600
1 parent 2493981 commit bb84d1f

File tree

1 file changed

+33
-5
lines changed

1 file changed

+33
-5
lines changed

pkg/autoid_service/autoid.go

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,14 @@ func (alloc *autoIDValue) alloc4Unsigned(ctx context.Context, store kv.Storage,
107107
if uint64(newBase) == math.MaxUint64 {
108108
return 0, 0, errAutoincReadFailed
109109
}
110+
logutil.BgLogger().Info("alloc4Unsigned from",
111+
zap.String("category", "autoid service"),
112+
zap.Int64("dbID", dbID),
113+
zap.Int64("tblID", tblID),
114+
zap.Int64("from base", alloc.base),
115+
zap.Int64("from end", alloc.end),
116+
zap.Int64("to base", newBase),
117+
zap.Int64("to end", newEnd))
110118
alloc.base, alloc.end = newBase, newEnd
111119
}
112120
min = alloc.base
@@ -168,6 +176,14 @@ func (alloc *autoIDValue) alloc4Signed(ctx context.Context,
168176
if newBase == math.MaxInt64 {
169177
return 0, 0, errAutoincReadFailed
170178
}
179+
logutil.BgLogger().Info("alloc4Signed from",
180+
zap.String("category", "autoid service"),
181+
zap.Int64("dbID", dbID),
182+
zap.Int64("tblID", tblID),
183+
zap.Int64("from base", alloc.base),
184+
zap.Int64("from end", alloc.end),
185+
zap.Int64("to base", newBase),
186+
zap.Int64("to end", newEnd))
171187
alloc.base, alloc.end = newBase, newEnd
172188
}
173189
min = alloc.base
@@ -301,7 +317,19 @@ func New(selfAddr string, etcdAddr []string, store kv.Storage, tlsConfig *tls.Co
301317

302318
func newWithCli(selfAddr string, cli *clientv3.Client, store kv.Storage) *Service {
303319
l := owner.NewOwnerManager(context.Background(), cli, "autoid", selfAddr, autoIDLeaderPath)
320+
service := &Service{
321+
autoIDMap: make(map[autoIDKey]*autoIDValue),
322+
leaderShip: l,
323+
store: store,
324+
}
304325
l.SetBeOwnerHook(func() {
326+
// Reset the map to avoid a case that a node lose leadership and regain it, then
327+
// improperly use the stale map to serve the autoid requests.
328+
// See https://github.com/pingcap/tidb/issues/52600
329+
service.autoIDLock.Lock()
330+
clear(service.autoIDMap)
331+
service.autoIDLock.Unlock()
332+
305333
logutil.BgLogger().Info("leader change of autoid service, this node become owner",
306334
zap.String("addr", selfAddr),
307335
zap.String("category", "autoid service"))
@@ -312,11 +340,7 @@ func newWithCli(selfAddr string, cli *clientv3.Client, store kv.Storage) *Servic
312340
panic(err)
313341
}
314342

315-
return &Service{
316-
autoIDMap: make(map[autoIDKey]*autoIDValue),
317-
leaderShip: l,
318-
store: store,
319-
}
343+
return service
320344
}
321345

322346
type mockClient struct {
@@ -353,7 +377,10 @@ func MockForTest(store kv.Storage) autoid.AutoIDAllocClient {
353377
// Close closes the Service and clean up resource.
354378
func (s *Service) Close() {
355379
if s.leaderShip != nil && s.leaderShip.IsOwner() {
380+
s.autoIDLock.Lock()
381+
defer s.autoIDLock.Unlock()
356382
for k, v := range s.autoIDMap {
383+
v.Lock()
357384
if v.base > 0 {
358385
err := v.forceRebase(context.Background(), s.store, k.dbID, k.tblID, v.base, v.isUnsigned)
359386
if err != nil {
@@ -364,6 +391,7 @@ func (s *Service) Close() {
364391
zap.Error(err))
365392
}
366393
}
394+
v.Unlock()
367395
}
368396
s.leaderShip.Cancel()
369397
}

0 commit comments

Comments
 (0)