Skip to content

Commit

Permalink
server/api/admin: change the tso parameter of ResetTS to string (#1896)
Browse files Browse the repository at this point in the history
Signed-off-by: 5kbpers <tangminghua@pingcap.com>
  • Loading branch information
5kbpers authored and sre-bot committed Nov 7, 2019
1 parent a256162 commit d96c622
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
11 changes: 8 additions & 3 deletions server/api/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,18 @@ func (h *adminHandler) ResetTS(w http.ResponseWriter, r *http.Request) {
if err := apiutil.ReadJSONRespondError(h.rd, w, r.Body, &input); err != nil {
return
}
ts, ok := input["tso"].(float64)
if !ok || ts < 0 {
tsValue, ok := input["tso"].(string)
if !ok || len(tsValue) == 0 {
h.rd.JSON(w, http.StatusBadRequest, "invalid tso value")
return
}
ts, err := strconv.ParseUint(tsValue, 10, 64)
if err != nil {
h.rd.JSON(w, http.StatusBadRequest, "invalid tso value")
return
}

if err := handler.ResetTS(uint64(ts)); err != nil {
if err = handler.ResetTS(ts); err != nil {
if err == server.ErrServerNotStarted {
h.rd.JSON(w, http.StatusInternalServerError, err.Error())
} else {
Expand Down
21 changes: 13 additions & 8 deletions server/api/admin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package api
import (
"encoding/json"
"fmt"
"math"
"net/http"
"strings"
"time"
Expand Down Expand Up @@ -92,9 +91,9 @@ type testTSOSuite struct {
urlPrefix string
}

func makeTS(offset time.Duration) int64 {
func makeTS(offset time.Duration) uint64 {
physical := time.Now().Add(offset).UnixNano() / int64(time.Millisecond)
return physical << 18
return uint64(physical << 18)
}

func (s *testTSOSuite) SetUpSuite(c *C) {
Expand All @@ -115,7 +114,7 @@ func (s *testTSOSuite) TestResetTS(c *C) {
args := make(map[string]interface{})
t1 := makeTS(time.Hour)
url := s.urlPrefix
args["tso"] = t1
args["tso"] = fmt.Sprintf("%d", t1)
values, err := json.Marshal(args)
c.Assert(err, IsNil)
err = postJSON(url, values,
Expand All @@ -126,7 +125,7 @@ func (s *testTSOSuite) TestResetTS(c *C) {

c.Assert(err, IsNil)
t2 := makeTS(32 * time.Hour)
args["tso"] = t2
args["tso"] = fmt.Sprintf("%d", t2)
values, err = json.Marshal(args)
c.Assert(err, IsNil)
err = postJSON(url, values,
Expand All @@ -135,21 +134,27 @@ func (s *testTSOSuite) TestResetTS(c *C) {
c.Assert(strings.Contains(err.Error(), "too large"), IsTrue)

t3 := makeTS(-2 * time.Hour)
args["tso"] = t3
args["tso"] = fmt.Sprintf("%d", t3)
values, err = json.Marshal(args)
c.Assert(err, IsNil)
err = postJSON(url, values,
func(_ []byte, code int) { c.Assert(code, Equals, http.StatusForbidden) })
c.Assert(err, NotNil)
c.Assert(strings.Contains(err.Error(), "small"), IsTrue)

t4 := math.MinInt64
args["tso"] = t4
args["tso"] = ""
values, err = json.Marshal(args)
c.Assert(err, IsNil)
err = postJSON(url, values,
func(_ []byte, code int) { c.Assert(code, Equals, http.StatusBadRequest) })
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, "\"invalid tso value\"\n")

args["tso"] = "test"
values, err = json.Marshal(args)
c.Assert(err, IsNil)
err = postJSON(url, values,
func(_ []byte, code int) { c.Assert(code, Equals, http.StatusBadRequest) })
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, "\"invalid tso value\"\n")
}

0 comments on commit d96c622

Please sign in to comment.