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

pdctl: escape the key before query the corresponding region #1299

Merged
merged 3 commits into from
Oct 29, 2018
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
73 changes: 54 additions & 19 deletions server/api/region_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package api
import (
"fmt"
"math/rand"
"net/url"
"sort"

. "github.com/pingcap/check"
Expand Down Expand Up @@ -109,25 +110,6 @@ func (s *testRegionSuite) TestRegions(c *C) {
}
}

func (s *testRegionSuite) TestScanRegionByKey(c *C) {
r1 := newTestRegionInfo(2, 1, []byte("a"), []byte("b"))
r2 := newTestRegionInfo(3, 1, []byte("b"), []byte("c"))
r3 := newTestRegionInfo(4, 2, []byte("c"), []byte("d"))
mustRegionHeartbeat(c, s.svr, r1)
mustRegionHeartbeat(c, s.svr, r2)
mustRegionHeartbeat(c, s.svr, r3)

url := fmt.Sprintf("%s/regions/key/%s", s.urlPrefix, "b")
regionIds := []uint64{3, 4}
regions := &regionsInfo{}
err := readJSONWithURL(url, regions)
c.Assert(err, IsNil)
c.Assert(len(regionIds), Equals, regions.Count)
for i, v := range regionIds {
c.Assert(v, Equals, regions.Regions[i].ID)
}
}

func (s *testRegionSuite) TestStoreRegions(c *C) {
r1 := newTestRegionInfo(2, 1, []byte("a"), []byte("b"))
r2 := newTestRegionInfo(3, 1, []byte("b"), []byte("c"))
Expand Down Expand Up @@ -225,3 +207,56 @@ func (s *testRegionSuite) TestTopN(c *C) {
}
}
}

var _ = Suite(&testGetRegionSuite{})

type testGetRegionSuite struct {
svr *server.Server
cleanup cleanUpFunc
urlPrefix string
}

func (s *testGetRegionSuite) SetUpSuite(c *C) {
s.svr, s.cleanup = mustNewServer(c)
mustWaitLeader(c, []*server.Server{s.svr})

addr := s.svr.GetAddr()
s.urlPrefix = fmt.Sprintf("%s%s/api/v1", addr, apiPrefix)

mustBootstrapCluster(c, s.svr)
}

func (s *testGetRegionSuite) TearDownSuite(c *C) {
s.cleanup()
}

func (s *testGetRegionSuite) TestRegionKey(c *C) {
r := newTestRegionInfo(99, 1, []byte{0xFF, 0xFF, 0xAA}, []byte{0xFF, 0xFF, 0xCC}, core.SetWrittenBytes(500), core.SetReadBytes(800), core.SetRegionConfVer(3), core.SetRegionVersion(2))
mustRegionHeartbeat(c, s.svr, r)
url := fmt.Sprintf("%s/region/key/%s", s.urlPrefix, url.QueryEscape(string([]byte{0xFF, 0xFF, 0xBB})))
regionInfo := &regionInfo{}
err := readJSONWithURL(url, regionInfo)
c.Assert(err, IsNil)
c.Assert(r.GetID(), Equals, regionInfo.ID)
}

func (s *testGetRegionSuite) TestScanRegionByKey(c *C) {
r1 := newTestRegionInfo(2, 1, []byte("a"), []byte("b"))
r2 := newTestRegionInfo(3, 1, []byte("b"), []byte("c"))
r3 := newTestRegionInfo(4, 2, []byte("c"), []byte("d"))
r := newTestRegionInfo(99, 1, []byte{0xFF, 0xFF, 0xAA}, []byte{0xFF, 0xFF, 0xCC}, core.SetWrittenBytes(500), core.SetReadBytes(800), core.SetRegionConfVer(3), core.SetRegionVersion(2))
mustRegionHeartbeat(c, s.svr, r1)
mustRegionHeartbeat(c, s.svr, r2)
mustRegionHeartbeat(c, s.svr, r3)
mustRegionHeartbeat(c, s.svr, r)

url := fmt.Sprintf("%s/regions/key/%s", s.urlPrefix, "b")
regionIds := []uint64{3, 4, 99}
regions := &regionsInfo{}
err := readJSONWithURL(url, regions)
c.Assert(err, IsNil)
c.Assert(len(regionIds), Equals, regions.Count)
for i, v := range regionIds {
c.Assert(v, Equals, regions.Regions[i].ID)
}
}
6 changes: 4 additions & 2 deletions tools/pd-ctl/pdctl/command/region_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"fmt"
"io"
"net/http"
"net/url"
"os/exec"
"strconv"

Expand Down Expand Up @@ -233,7 +234,8 @@ func showRegionWithTableCommandFunc(cmd *cobra.Command, args []string) {
fmt.Println("Error: unknown format")
return
}
// TODO: Deal with path escaped

key = url.QueryEscape(key)
prefix := regionKeyPrefix + "/" + key
r, err := doRequest(cmd, prefix, http.MethodGet)
if err != nil {
Expand Down Expand Up @@ -324,7 +326,7 @@ func showRegionsFromStartKeyCommandFunc(cmd *cobra.Command, args []string) {
fmt.Println("Error: unknown format")
return
}
// TODO: Deal with path escaped
key = url.QueryEscape(key)
prefix := regionKeyPrefix + "/" + key
if len(args) == 2 {
if _, err = strconv.Atoi(args[1]); err != nil {
Expand Down