-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
id: make id allocator general purpose (tikv#5284)
close tikv#5294 make id allocator general purpose Signed-off-by: David <8039876+AmoebaProtozoa@users.noreply.github.com> Co-authored-by: Ti Chi Robot <ti-community-prow-bot@tidb.io>
- Loading branch information
Showing
4 changed files
with
144 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// Copyright 2022 TiKV Project Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package id | ||
|
||
import ( | ||
"context" | ||
"strconv" | ||
"sync" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"github.com/tikv/pd/pkg/etcdutil" | ||
"go.etcd.io/etcd/clientv3" | ||
"go.etcd.io/etcd/embed" | ||
) | ||
|
||
const ( | ||
rootPath = "/pd" | ||
leaderPath = "/pd/leader" | ||
allocPath = "alloc_id" | ||
label = "idalloc" | ||
memberVal = "member" | ||
step = uint64(500) | ||
) | ||
|
||
// TestMultipleAllocator tests situation where multiple allocators that | ||
// share rootPath and member val update their ids concurrently. | ||
func TestMultipleAllocator(t *testing.T) { | ||
re := require.New(t) | ||
cfg := etcdutil.NewTestSingleConfig(t) | ||
etcd, err := embed.StartEtcd(cfg) | ||
defer func() { | ||
etcd.Close() | ||
}() | ||
re.NoError(err) | ||
|
||
ep := cfg.LCUrls[0].String() | ||
client, err := clientv3.New(clientv3.Config{ | ||
Endpoints: []string{ep}, | ||
}) | ||
re.NoError(err) | ||
|
||
<-etcd.Server.ReadyNotify() | ||
|
||
// Put memberValue to leaderPath to simulate an election success. | ||
_, err = client.Put(context.Background(), leaderPath, memberVal) | ||
re.NoError(err) | ||
|
||
wg := sync.WaitGroup{} | ||
for i := 0; i < 3; i++ { | ||
iStr := strconv.Itoa(i) | ||
wg.Add(1) | ||
// All allocators share rootPath and memberVal, but they have different allocPaths, labels and steps. | ||
allocator := NewAllocator(&AllocatorParams{ | ||
Client: client, | ||
RootPath: rootPath, | ||
AllocPath: allocPath + iStr, | ||
Label: label + iStr, | ||
Member: memberVal, | ||
Step: step * uint64(i), // allocator 0, 1, 2 should have step size 1000 (default), 500, 1000 respectively. | ||
}) | ||
go func(re *require.Assertions, allocator Allocator) { | ||
defer wg.Done() | ||
testAllocator(re, allocator) | ||
}(re, allocator) | ||
} | ||
wg.Wait() | ||
} | ||
|
||
// testAllocator sequentially updates given allocator and check if values are expected. | ||
func testAllocator(re *require.Assertions, allocator Allocator) { | ||
startID, err := allocator.Alloc() | ||
re.NoError(err) | ||
for i := startID + 1; i < startID+step*20; i++ { | ||
id, err := allocator.Alloc() | ||
re.NoError(err) | ||
re.Equal(i, id) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters