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

*: improve region path #5636

Merged
merged 4 commits into from
Oct 27, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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: 24 additions & 1 deletion server/storage/endpoint/key_path.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ package endpoint
import (
"fmt"
"path"
"strconv"
"strings"
)

const (
Expand All @@ -38,6 +40,7 @@ const (
keyspaceMetaInfix = "meta"
keyspaceIDInfix = "id"
keyspaceAllocID = "alloc_id"
regionPathPrefix = "raft/r"
)

// AppendToRootPath appends the given key to the rootPath.
Expand Down Expand Up @@ -74,7 +77,27 @@ func storeRegionWeightPath(storeID uint64) string {

// RegionPath returns the region meta info key path with the given region ID.
func RegionPath(regionID uint64) string {
return path.Join(clusterPath, "r", fmt.Sprintf("%020d", regionID))
var buf strings.Builder
buf.WriteString(regionPathPrefix)
buf.WriteString("/")
s := strconv.FormatUint(regionID, 10)
if len(s) > 20 {
s = s[len(s)-20:]
} else {
b := make([]byte, 20)
diff := 20 - len(s)
for i := 0; i < 20; i++ {
if i < diff {
b[i] = 48
} else {
b[i] = s[i-diff]
}
}
s = string(b)
}
buf.WriteString(s)

return buf.String()
}

func ruleKeyPath(ruleKey string) string {
Expand Down
29 changes: 29 additions & 0 deletions server/storage/endpoint/key_path_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package endpoint

import (
"fmt"
"math/rand"
"path"
"testing"
"time"

"github.com/stretchr/testify/require"
)

func TestRegionPath(t *testing.T) {
re := require.New(t)
f := func(id uint64) string {
return path.Join(regionPathPrefix, fmt.Sprintf("%020d", id))
}
rand.Seed(time.Now().Unix())
for i := 0; i < 1000; i++ {
id := rand.Uint64()
re.Equal(f(id), RegionPath(id))
}
}

func BenchmarkRegionPath(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = RegionPath(uint64(i))
}
}