Skip to content

Commit

Permalink
*: address a comment
Browse files Browse the repository at this point in the history
  • Loading branch information
zimulala committed Aug 15, 2023
1 parent 757c19e commit 6049691
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 12 deletions.
3 changes: 2 additions & 1 deletion server/handler/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ go_library(
"//tablecodec",
"//types",
"//util/codec",
"@com_github_gorilla_mux//:mux",
"//util/logutil",
"@com_github_pingcap_errors//:errors",
"@com_github_pingcap_failpoint//:failpoint",
"@com_github_pingcap_kvproto//pkg/kvrpcpb",
"@com_github_pingcap_kvproto//pkg/metapb",
"@com_github_tikv_client_go_v2//tikv",
"@org_uber_go_zap//:zap",
],
)
19 changes: 13 additions & 6 deletions server/handler/tests/http_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1247,8 +1247,15 @@ func TestUpgrade(t *testing.T) {
ts.startServer(t)
defer ts.stopServer(t)

// test /upgrade/start
resp, err := ts.FetchStatus("/upgrade/start")
resp, err := ts.FetchStatus("/upgrade")
require.NoError(t, err)
require.Equal(t, http.StatusBadRequest, resp.StatusCode)
require.NoError(t, resp.Body.Close())

require.NoError(t, err)
require.NotNil(t, resp)
// test upgrade start
resp, err = ts.PostStatus("/upgrade", "application/x-www-form-urlencoded", bytes.NewBuffer([]byte(`op=start`)))
require.NoError(t, err)
require.Equal(t, http.StatusOK, resp.StatusCode)
b, err := httputil.DumpResponse(resp, true)
Expand All @@ -1266,7 +1273,7 @@ func TestUpgrade(t *testing.T) {
require.True(t, isUpgrading)

// Do start upgrade again.
resp, err = ts.FetchStatus("/upgrade/start")
resp, err = ts.PostStatus("/upgrade", "application/x-www-form-urlencoded", bytes.NewBuffer([]byte(`op=start`)))
require.NoError(t, err)
require.Equal(t, http.StatusOK, resp.StatusCode)
b, err = httputil.DumpResponse(resp, true)
Expand All @@ -1283,8 +1290,8 @@ func TestUpgrade(t *testing.T) {
require.NoError(t, err)
require.True(t, isUpgrading)

// test /upgrade/finish
resp, err = ts.FetchStatus("/upgrade/finish")
// test upgrade finish
resp, err = ts.PostStatus("/upgrade", "application/x-www-form-urlencoded", bytes.NewBuffer([]byte(`op=finish`)))
require.NoError(t, err)
require.Equal(t, http.StatusOK, resp.StatusCode)
b, err = httputil.DumpResponse(resp, true)
Expand All @@ -1302,7 +1309,7 @@ func TestUpgrade(t *testing.T) {
require.False(t, isUpgrading)

// Do finish upgrade again.
resp, err = ts.FetchStatus("/upgrade/finish")
resp, err = ts.PostStatus("/upgrade", "application/x-www-form-urlencoded", bytes.NewBuffer([]byte(`op=finish`)))
require.NoError(t, err)
require.Equal(t, http.StatusOK, resp.StatusCode)
b, err = httputil.DumpResponse(resp, true)
Expand Down
13 changes: 9 additions & 4 deletions server/handler/upgradehandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ package handler
import (
"net/http"

"github.com/gorilla/mux"
"github.com/pingcap/errors"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/session"
"github.com/pingcap/tidb/util/logutil"
"go.uber.org/zap"
)

// ClusterUpgradeHandler is the handler for upgrading cluster.
Expand All @@ -35,12 +36,14 @@ func NewClusterUpgradeHandler(store kv.Storage) *ClusterUpgradeHandler {

// ServeHTTP handles request of ddl server info.
func (h ClusterUpgradeHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
// parse params
params := mux.Vars(req)
if req.Method != http.MethodPost {
WriteError(w, errors.Errorf("This API only support POST method"))
return
}

var err error
var hasDone bool
op := params[Operation]
op := req.FormValue("op")
switch op {
case "start":
hasDone, err = h.startUpgrade()
Expand All @@ -64,6 +67,8 @@ func (h ClusterUpgradeHandler) ServeHTTP(w http.ResponseWriter, req *http.Reques
}
}
WriteData(w, "success!")
logutil.Logger(req.Context()).Info("[upgrading] upgrade op success",
zap.String("op", req.FormValue("op")), zap.Bool("hasDone", hasDone))
}

func (h ClusterUpgradeHandler) startUpgrade() (hasDone bool, err error) {
Expand Down
2 changes: 1 addition & 1 deletion server/http_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ func (s *Server) startHTTPServer() {
router.Handle("/tiflash/replica-deprecated", tikvhandler.NewFlashReplicaHandler(tikvHandlerTool))

// HTTP path for upgrade operations.
router.Handle("/upgrade/{op}", handler.NewClusterUpgradeHandler(tikvHandlerTool.Store.(kv.Storage))).Name("upgrade operations")
router.Handle("/upgrade", handler.NewClusterUpgradeHandler(tikvHandlerTool.Store.(kv.Storage))).Name("upgrade operations")

if s.cfg.Store == "tikv" {
// HTTP path for tikv.
Expand Down

0 comments on commit 6049691

Please sign in to comment.