Skip to content

Commit

Permalink
cherry pick tikv#3322 to release-5.0-rc
Browse files Browse the repository at this point in the history
Signed-off-by: ti-srebot <ti-srebot@pingcap.com>
  • Loading branch information
HunDunDM authored and ti-srebot committed Jan 4, 2021
1 parent 27c29b7 commit ce44a91
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 4 deletions.
9 changes: 9 additions & 0 deletions server/id/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,12 @@ func (alloc *AllocatorImpl) Alloc() (uint64, error) {
if err != nil {
return 0, err
}
<<<<<<< HEAD

alloc.end = end
alloc.base = alloc.end - allocStep
=======
>>>>>>> 78842590... id: fix the bug that id may be duplicated (#3322)
}

alloc.base++
Expand Down Expand Up @@ -109,7 +112,13 @@ func (alloc *AllocatorImpl) generate() (uint64, error) {

log.Info("idAllocator allocates a new id", zap.Uint64("alloc-id", end))
idGauge.WithLabelValues("idalloc").Set(float64(end))
<<<<<<< HEAD
return end, nil
=======
alloc.end = end
alloc.base = end - allocStep
return nil
>>>>>>> 78842590... id: fix the bug that id may be duplicated (#3322)
}

func (alloc *AllocatorImpl) getAllocIDPath() string {
Expand Down
84 changes: 80 additions & 4 deletions tests/server/id/id_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ func (s *testAllocIDSuite) SetUpSuite(c *C) {
func (s *testAllocIDSuite) TearDownSuite(c *C) {
s.cancel()
}

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

err = cluster.RunInitialServers()
c.Assert(err, IsNil)
Expand Down Expand Up @@ -96,10 +96,9 @@ func (s *testAllocIDSuite) TestID(c *C) {
}

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

err = cluster.RunInitialServers()
c.Assert(err, IsNil)
Expand All @@ -119,3 +118,80 @@ func (s *testAllocIDSuite) TestCommand(c *C) {
last = resp.GetId()
}
}
<<<<<<< HEAD
=======

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

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
}
}

func (s *testAllocIDSuite) TestPDRestart(c *C) {
cluster, err := tests.NewTestCluster(s.ctx, 1)
c.Assert(err, IsNil)
defer cluster.Destroy()

err = cluster.RunInitialServers()
c.Assert(err, IsNil)
cluster.WaitLeader()
leaderServer := cluster.GetServer(cluster.GetLeader())

var last uint64
for i := uint64(0); i < 10; i++ {
id, err := leaderServer.GetAllocator().Alloc()
c.Assert(err, IsNil)
c.Assert(id, Greater, last)
last = id
}

c.Assert(leaderServer.Stop(), IsNil)
c.Assert(leaderServer.Run(), IsNil)
cluster.WaitLeader()

for i := uint64(0); i < 10; i++ {
id, err := leaderServer.GetAllocator().Alloc()
c.Assert(err, IsNil)
c.Assert(id, Greater, last)
last = id
}
}
>>>>>>> 78842590... id: fix the bug that id may be duplicated (#3322)

0 comments on commit ce44a91

Please sign in to comment.