Skip to content

Commit

Permalink
autoid_service: fix potential autoid decrease when leader change or c…
Browse files Browse the repository at this point in the history
…lose for AUTO_ID_CACHE=1 (#52602) (#52999)

close #52600
  • Loading branch information
ti-chi-bot authored May 23, 2024
1 parent 99478c8 commit c9ed8f4
Showing 1 changed file with 33 additions and 5 deletions.
38 changes: 33 additions & 5 deletions pkg/autoid_service/autoid.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,14 @@ func (alloc *autoIDValue) alloc4Unsigned(ctx context.Context, store kv.Storage,
if uint64(newBase) == math.MaxUint64 {
return 0, 0, errAutoincReadFailed
}
logutil.BgLogger().Info("alloc4Unsigned from",
zap.String("category", "autoid service"),
zap.Int64("dbID", dbID),
zap.Int64("tblID", tblID),
zap.Int64("from base", alloc.base),
zap.Int64("from end", alloc.end),
zap.Int64("to base", newBase),
zap.Int64("to end", newEnd))
alloc.base, alloc.end = newBase, newEnd
}
min = alloc.base
Expand Down Expand Up @@ -168,6 +176,14 @@ func (alloc *autoIDValue) alloc4Signed(ctx context.Context,
if newBase == math.MaxInt64 {
return 0, 0, errAutoincReadFailed
}
logutil.BgLogger().Info("alloc4Signed from",
zap.String("category", "autoid service"),
zap.Int64("dbID", dbID),
zap.Int64("tblID", tblID),
zap.Int64("from base", alloc.base),
zap.Int64("from end", alloc.end),
zap.Int64("to base", newBase),
zap.Int64("to end", newEnd))
alloc.base, alloc.end = newBase, newEnd
}
min = alloc.base
Expand Down Expand Up @@ -301,7 +317,19 @@ func New(selfAddr string, etcdAddr []string, store kv.Storage, tlsConfig *tls.Co

func newWithCli(selfAddr string, cli *clientv3.Client, store kv.Storage) *Service {
l := owner.NewOwnerManager(context.Background(), cli, "autoid", selfAddr, autoIDLeaderPath)
service := &Service{
autoIDMap: make(map[autoIDKey]*autoIDValue),
leaderShip: l,
store: store,
}
l.SetBeOwnerHook(func() {
// Reset the map to avoid a case that a node lose leadership and regain it, then
// improperly use the stale map to serve the autoid requests.
// See https://github.com/pingcap/tidb/issues/52600
service.autoIDLock.Lock()
clear(service.autoIDMap)
service.autoIDLock.Unlock()

logutil.BgLogger().Info("leader change of autoid service, this node become owner",
zap.String("addr", selfAddr),
zap.String("category", "autoid service"))
Expand All @@ -312,11 +340,7 @@ func newWithCli(selfAddr string, cli *clientv3.Client, store kv.Storage) *Servic
panic(err)
}

return &Service{
autoIDMap: make(map[autoIDKey]*autoIDValue),
leaderShip: l,
store: store,
}
return service
}

type mockClient struct {
Expand Down Expand Up @@ -353,7 +377,10 @@ func MockForTest(store kv.Storage) autoid.AutoIDAllocClient {
// Close closes the Service and clean up resource.
func (s *Service) Close() {
if s.leaderShip != nil && s.leaderShip.IsOwner() {
s.autoIDLock.Lock()
defer s.autoIDLock.Unlock()
for k, v := range s.autoIDMap {
v.Lock()
if v.base > 0 {
err := v.forceRebase(context.Background(), s.store, k.dbID, k.tblID, v.base, v.isUnsigned)
if err != nil {
Expand All @@ -364,6 +391,7 @@ func (s *Service) Close() {
zap.Error(err))
}
}
v.Unlock()
}
s.leaderShip.Cancel()
}
Expand Down

0 comments on commit c9ed8f4

Please sign in to comment.