@@ -107,6 +107,14 @@ func (alloc *autoIDValue) alloc4Unsigned(ctx context.Context, store kv.Storage,
107
107
if uint64 (newBase ) == math .MaxUint64 {
108
108
return 0 , 0 , errAutoincReadFailed
109
109
}
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 ))
110
118
alloc .base , alloc .end = newBase , newEnd
111
119
}
112
120
min = alloc .base
@@ -168,6 +176,14 @@ func (alloc *autoIDValue) alloc4Signed(ctx context.Context,
168
176
if newBase == math .MaxInt64 {
169
177
return 0 , 0 , errAutoincReadFailed
170
178
}
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 ))
171
187
alloc .base , alloc .end = newBase , newEnd
172
188
}
173
189
min = alloc .base
@@ -301,7 +317,19 @@ func New(selfAddr string, etcdAddr []string, store kv.Storage, tlsConfig *tls.Co
301
317
302
318
func newWithCli (selfAddr string , cli * clientv3.Client , store kv.Storage ) * Service {
303
319
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
+ }
304
325
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
+
305
333
logutil .BgLogger ().Info ("leader change of autoid service, this node become owner" ,
306
334
zap .String ("addr" , selfAddr ),
307
335
zap .String ("category" , "autoid service" ))
@@ -312,11 +340,7 @@ func newWithCli(selfAddr string, cli *clientv3.Client, store kv.Storage) *Servic
312
340
panic (err )
313
341
}
314
342
315
- return & Service {
316
- autoIDMap : make (map [autoIDKey ]* autoIDValue ),
317
- leaderShip : l ,
318
- store : store ,
319
- }
343
+ return service
320
344
}
321
345
322
346
type mockClient struct {
@@ -353,7 +377,10 @@ func MockForTest(store kv.Storage) autoid.AutoIDAllocClient {
353
377
// Close closes the Service and clean up resource.
354
378
func (s * Service ) Close () {
355
379
if s .leaderShip != nil && s .leaderShip .IsOwner () {
380
+ s .autoIDLock .Lock ()
381
+ defer s .autoIDLock .Unlock ()
356
382
for k , v := range s .autoIDMap {
383
+ v .Lock ()
357
384
if v .base > 0 {
358
385
err := v .forceRebase (context .Background (), s .store , k .dbID , k .tblID , v .base , v .isUnsigned )
359
386
if err != nil {
@@ -364,6 +391,7 @@ func (s *Service) Close() {
364
391
zap .Error (err ))
365
392
}
366
393
}
394
+ v .Unlock ()
367
395
}
368
396
s .leaderShip .Cancel ()
369
397
}
0 commit comments