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 bug that id may be duplicated #3322

Merged
merged 3 commits into from
Jan 4, 2021
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
3 changes: 1 addition & 2 deletions server/id/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ func (alloc *AllocatorImpl) Alloc() (uint64, error) {
if err := alloc.generateLocked(); err != nil {
return 0, err
}

alloc.base = alloc.end - allocStep
}

alloc.base++
Expand Down Expand Up @@ -116,6 +114,7 @@ func (alloc *AllocatorImpl) generateLocked() error {
log.Info("idAllocator allocates a new id", zap.Uint64("alloc-id", end))
idGauge.WithLabelValues("idalloc").Set(float64(end))
alloc.end = end
alloc.base = end - allocStep
return nil
}

Expand Down
40 changes: 34 additions & 6 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 @@ -121,10 +120,9 @@ func (s *testAllocIDSuite) TestCommand(c *C) {
}

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

err = cluster.RunInitialServers()
c.Assert(err, IsNil)
Expand Down Expand Up @@ -164,3 +162,33 @@ func (s *testAllocIDSuite) TestMonotonicID(c *C) {
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
}
}