Skip to content

Commit

Permalink
pd-ctl: escape the key before query the corresponding region (#1299)
Browse files Browse the repository at this point in the history
  • Loading branch information
nolouch authored Oct 29, 2018
1 parent a53e353 commit 4cdbcca
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 21 deletions.
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))

This comment has been minimized.

Copy link
@breezewish

breezewish Oct 29, 2018

Member

how about there are "/" chars in the key?

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

0 comments on commit 4cdbcca

Please sign in to comment.