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

PCP: /api/v1/regions optimization #1970

Merged
merged 14 commits into from
Dec 2, 2019
Merged
Show file tree
Hide file tree
Changes from 5 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
51 changes: 33 additions & 18 deletions server/api/region.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,32 @@ func NewRegionInfo(r *core.RegionInfo) *RegionInfo {
if r == nil {
ekalinin marked this conversation as resolved.
Show resolved Hide resolved
return nil
}
return &RegionInfo{
ID: r.GetID(),
StartKey: string(core.HexRegionKey(r.GetStartKey())),
EndKey: string(core.HexRegionKey(r.GetEndKey())),
RegionEpoch: r.GetRegionEpoch(),
Peers: r.GetPeers(),
Leader: r.GetLeader(),
DownPeers: r.GetDownPeers(),
PendingPeers: r.GetPendingPeers(),
WrittenBytes: r.GetBytesWritten(),
WrittenKeys: r.GetKeysWritten(),
ReadBytes: r.GetBytesRead(),
ReadKeys: r.GetKeysRead(),
ApproximateSize: r.GetApproximateSize(),
ApproximateKeys: r.GetApproximateKeys(),
ri := &RegionInfo{}
return InitRegion(r, ri)
}

// InitRegion init a new api RegionInfo from the core.RegionInfo.
func InitRegion(r *core.RegionInfo, s *RegionInfo) *RegionInfo {
if r == nil {
return nil
}

s.ID = r.GetID()
s.StartKey = core.HexRegionKeyStr(r.GetStartKey())
s.EndKey = core.HexRegionKeyStr(r.GetEndKey())
s.RegionEpoch = r.GetRegionEpoch()
s.Peers = r.GetPeers()
s.Leader = r.GetLeader()
s.DownPeers = r.GetDownPeers()
s.PendingPeers = r.GetPendingPeers()
s.WrittenBytes = r.GetBytesWritten()
s.WrittenKeys = r.GetKeysWritten()
s.ReadBytes = r.GetBytesRead()
s.ReadKeys = r.GetKeysRead()
s.ApproximateSize = r.GetApproximateSize()
s.ApproximateKeys = r.GetApproximateKeys()

return s
}

// RegionsInfo contains some regions with the detailed region info.
Expand Down Expand Up @@ -122,13 +132,18 @@ func newRegionsHandler(svr *server.Server, rd *render.Render) *regionsHandler {
}

func convertToAPIRegions(regions []*core.RegionInfo) *RegionsInfo {
regionInfos := make([]*RegionInfo, len(regions))
regionInfos := make([]RegionInfo, len(regions))
regionInfosRefs := make([]*RegionInfo, len(regions))

for i := 0; i < len(regions); i++ {
regionInfosRefs[i] = &regionInfos[i]
}
for i, r := range regions {
regionInfos[i] = NewRegionInfo(r)
regionInfosRefs[i] = InitRegion(r, regionInfosRefs[i])
}
return &RegionsInfo{
Count: len(regions),
Regions: regionInfos,
Regions: regionInfosRefs,
}
}

Expand Down
71 changes: 71 additions & 0 deletions server/api/region_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@
package api

import (
"bytes"
"fmt"
"math/rand"
"net/url"
"sort"
"testing"

. "github.com/pingcap/check"
"github.com/pingcap/kvproto/pkg/metapb"
Expand Down Expand Up @@ -331,3 +333,72 @@ func (s *testGetRegionSuite) TestScanRegionByKey(c *C) {
c.Assert(v, Equals, regions.Regions[i].ID)
}
}

// Create n regions (0..n) of n stores (0..n).
// Each region contains np peers, the first peer is the leader.
// (copied from server/cluster_test.go)
func newTestRegions(n, np uint64) []*core.RegionInfo {
ekalinin marked this conversation as resolved.
Show resolved Hide resolved
regions := make([]*core.RegionInfo, 0, n)
for i := uint64(0); i < n; i++ {
peers := make([]*metapb.Peer, 0, np)
for j := uint64(0); j < np; j++ {
peer := &metapb.Peer{
Id: i*np + j,
}
peer.StoreId = (i + j) % n
peers = append(peers, peer)
}
region := &metapb.Region{
Id: i,
Peers: peers,
StartKey: []byte{byte(i)},
ekalinin marked this conversation as resolved.
Show resolved Hide resolved
EndKey: []byte{byte(i + 1)},
RegionEpoch: &metapb.RegionEpoch{ConfVer: 2, Version: 2},
}
regions = append(regions, core.NewRegionInfo(region, peers[0]))
}
return regions
}

func BenchmarkRenderJSON(b *testing.B) {
regionsCount := uint64(10000)
storeCount := uint64(100)
ekalinin marked this conversation as resolved.
Show resolved Hide resolved
regionInfos := newTestRegions(regionsCount, storeCount)

rd := createRender()
regions := convertToAPIRegions(regionInfos)

b.ResetTimer()
for i := 0; i < b.N; i++ {
var buffer bytes.Buffer
rd.JSON(&buffer, 200, regions)
}
}

func BenchmarkConvertToAPIRegions(b *testing.B) {
regionsCount := uint64(10000)
storeCount := uint64(100)
regionInfos := newTestRegions(regionsCount, storeCount)

b.ResetTimer()
for i := 0; i < b.N; i++ {
regions := convertToAPIRegions(regionInfos)
_ = regions.Count
}
}

func BenchmarkHexRegionKey(b *testing.B) {
key := []byte("region_number_infinity")
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = string(core.HexRegionKey(key))
}
}

func BenchmarkHexRegionKeyStr(b *testing.B) {
key := []byte("region_number_infinity")
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = core.HexRegionKeyStr(key)
}
}
10 changes: 7 additions & 3 deletions server/api/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,14 @@ import (
"github.com/unrolled/render"
)

func createRouter(prefix string, svr *server.Server) *mux.Router {
rd := render.New(render.Options{
IndentJSON: true,
func createRender() *render.Render {
return render.New(render.Options{
StreamingJSON: true,
ekalinin marked this conversation as resolved.
Show resolved Hide resolved
})
}

func createRouter(prefix string, svr *server.Server) *mux.Router {
rd := createRender()

rootRouter := mux.NewRouter().PathPrefix(prefix).Subrouter()
handler := svr.GetHandler()
Expand Down
8 changes: 7 additions & 1 deletion server/core/region.go
Original file line number Diff line number Diff line change
Expand Up @@ -891,7 +891,13 @@ func isInvolved(region *RegionInfo, startKey, endKey []byte) bool {
// HexRegionKey converts region key to hex format. Used for formating region in
// logs.
func HexRegionKey(key []byte) []byte {
return []byte(strings.ToUpper(hex.EncodeToString(key)))
return []byte(HexRegionKeyStr(key))
}

// HexRegionKeyStr converts region key to hex format. Used for formating region in
// logs.
func HexRegionKeyStr(key []byte) string {
return strings.ToUpper(hex.EncodeToString(key))
}

// RegionToHexMeta converts a region meta's keys to hex format. Used for formating
Expand Down