From be44bcf073635b7e2312cc284626961d57b94f2b Mon Sep 17 00:00:00 2001 From: Ryan Leung Date: Mon, 28 Dec 2020 13:04:44 +0800 Subject: [PATCH] cherry pick #3305 to release-2.1 Signed-off-by: Ryan Leung --- server/id.go | 24 ++++++++++++++++-------- server/id_test.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ server/leader.go | 5 +++++ 3 files changed, 66 insertions(+), 8 deletions(-) diff --git a/server/id.go b/server/id.go index 047d96a637b8..0b0d8b3ab65c 100644 --- a/server/id.go +++ b/server/id.go @@ -39,8 +39,7 @@ func (alloc *idAllocator) 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 } @@ -53,11 +52,19 @@ func (alloc *idAllocator) Alloc() (uint64, error) { return alloc.base, nil } -func (alloc *idAllocator) generate() (uint64, error) { +// Generate synchronizes and generates id range. +func (alloc *idAllocator) Generate() error { + alloc.mu.Lock() + defer alloc.mu.Unlock() + + return alloc.generateLocked() +} + +func (alloc *idAllocator) generateLocked() error { key := alloc.s.getAllocIDPath() value, err := getValue(alloc.s.client, key) if err != nil { - return 0, err + return err } var ( @@ -72,7 +79,7 @@ func (alloc *idAllocator) generate() (uint64, error) { // update the key end, err = bytesToUint64(value) if err != nil { - return 0, err + return err } cmp = clientv3.Compare(clientv3.Value(key), "=", string(value)) @@ -82,13 +89,14 @@ func (alloc *idAllocator) generate() (uint64, error) { value = uint64ToBytes(end) resp, err := alloc.s.leaderTxn(cmp).Then(clientv3.OpPut(key, string(value))).Commit() if err != nil { - return 0, err + return err } if !resp.Succeeded { - return 0, errors.New("generate id failed, we may not leader") + return errors.New("generate id failed, we may not leader") } log.Info("idAllocator allocates a new id", zap.Uint64("alloc-id", end)) metadataGauge.WithLabelValues("idalloc").Set(float64(end)) - return end, nil + alloc.end = end + return nil } diff --git a/server/id_test.go b/server/id_test.go index 4590da04cea1..12414f708656 100644 --- a/server/id_test.go +++ b/server/id_test.go @@ -95,3 +95,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 + } +} diff --git a/server/leader.go b/server/leader.go index fcb70a24d7f1..5cdd75673ebc 100644 --- a/server/leader.go +++ b/server/leader.go @@ -274,6 +274,11 @@ func (s *Server) campaignLeader() error { physical: zeroTime, }) + log.Info("sync id from etcd") + if err = s.idAlloc.Generate(); err != nil { + return err + } + s.enableLeader() defer s.disableLeader()