Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

id: fix the id allocator is not monotonic (#3305) #3308

Merged
merged 1 commit into from
Dec 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions server/id/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,10 @@ func (alloc *AllocatorImpl) Alloc() (uint64, error) {
defer alloc.mu.Unlock()

if alloc.base == alloc.end {
end, err := alloc.generate()
if err != nil {
if err := alloc.generateLocked(); err != nil {
return 0, err
}

alloc.end = end
alloc.base = alloc.end - allocStep
}

Expand All @@ -69,11 +67,19 @@ func (alloc *AllocatorImpl) Alloc() (uint64, error) {
return alloc.base, nil
}

func (alloc *AllocatorImpl) generate() (uint64, error) {
// Generate synchronizes and generates id range.
func (alloc *AllocatorImpl) Generate() error {
alloc.mu.Lock()
defer alloc.mu.Unlock()

return alloc.generateLocked()
}

func (alloc *AllocatorImpl) generateLocked() error {
key := alloc.getAllocIDPath()
value, err := etcdutil.GetValue(alloc.client, key)
if err != nil {
return 0, err
return err
}

var (
Expand All @@ -88,7 +94,7 @@ func (alloc *AllocatorImpl) generate() (uint64, error) {
// update the key
end, err = typeutil.BytesToUint64(value)
if err != nil {
return 0, err
return err
}

cmp = clientv3.Compare(clientv3.Value(key), "=", string(value))
Expand All @@ -101,15 +107,16 @@ func (alloc *AllocatorImpl) generate() (uint64, error) {
t := txn.If(append([]clientv3.Cmp{cmp}, clientv3.Compare(clientv3.Value(leaderPath), "=", alloc.member))...)
resp, err := t.Then(clientv3.OpPut(key, string(value))).Commit()
if err != nil {
return 0, errs.ErrEtcdTxn.Wrap(err).GenWithStackByArgs()
return errs.ErrEtcdTxn.Wrap(err).GenWithStackByArgs()
}
if !resp.Succeeded {
return 0, errs.ErrEtcdTxn.FastGenByArgs()
return errs.ErrEtcdTxn.FastGenByArgs()
}

log.Info("idAllocator allocates a new id", zap.Uint64("alloc-id", end))
idGauge.WithLabelValues("idalloc").Set(float64(end))
return end, nil
alloc.end = end
return nil
}

func (alloc *AllocatorImpl) getAllocIDPath() string {
Expand Down
4 changes: 4 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -1152,6 +1152,10 @@ func (s *Server) campaignLeader() {
log.Error("failed to load persistOptions from etcd", errs.ZapError(err))
return
}
if err := s.idAllocator.Generate(); err != nil {
log.Error("failed to sync id from etcd", errs.ZapError(err))
return
}
s.member.EnableLeader()
defer s.member.DisableLeader()

Expand Down
45 changes: 45 additions & 0 deletions tests/server/id/id_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,48 @@ func (s *testAllocIDSuite) TestCommand(c *C) {
last = resp.GetId()
}
}

func (s *testAllocIDSuite) TestMonotonicID(c *C) {
var err error
cluster, err := tests.NewTestCluster(s.ctx, 2)
defer cluster.Destroy()
c.Assert(err, IsNil)

err = cluster.RunInitialServers()
c.Assert(err, IsNil)
cluster.WaitLeader()

leaderServer := cluster.GetServer(cluster.GetLeader())
var last1 uint64
for i := uint64(0); i < 10; i++ {
id, err := leaderServer.GetAllocator().Alloc()
c.Assert(err, IsNil)
c.Assert(id, Greater, last1)
last1 = id
}
err = cluster.ResignLeader()
c.Assert(err, IsNil)
cluster.WaitLeader()
leaderServer = cluster.GetServer(cluster.GetLeader())
var last2 uint64
for i := uint64(0); i < 10; i++ {
id, err := leaderServer.GetAllocator().Alloc()
c.Assert(err, IsNil)
c.Assert(id, Greater, last2)
last2 = id
}
err = cluster.ResignLeader()
c.Assert(err, IsNil)
cluster.WaitLeader()
leaderServer = cluster.GetServer(cluster.GetLeader())
id, err := leaderServer.GetAllocator().Alloc()
c.Assert(err, IsNil)
c.Assert(id, Greater, last2)
var last3 uint64
for i := uint64(0); i < 1000; i++ {
id, err := leaderServer.GetAllocator().Alloc()
c.Assert(err, IsNil)
c.Assert(id, Greater, last3)
last3 = id
}
}