Skip to content

Commit

Permalink
server: add a http interface to change labels (#36845)
Browse files Browse the repository at this point in the history
ref #35926
  • Loading branch information
glorv authored Aug 4, 2022
1 parent a6475a1 commit 95e4df8
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
32 changes: 32 additions & 0 deletions server/http_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,9 @@ type ddlHookHandler struct {
type valueHandler struct {
}

// labelHandler is the handler for set labels
type labelHandler struct{}

const (
opTableRegions = "regions"
opTableRanges = "ranges"
Expand Down Expand Up @@ -2158,3 +2161,32 @@ func (h ddlHookHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
ctx := req.Context()
logutil.Logger(ctx).Info("change ddl hook success", zap.String("to_ddl_hook", req.FormValue("ddl_hook")))
}

// ServeHTTP handles request of set server labels.
func (h labelHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
if req.Method != http.MethodPost {
writeError(w, errors.Errorf("This api only support POST method"))
return
}

labels := make(map[string]string)
err := json.NewDecoder(req.Body).Decode(&labels)
if err != nil {
writeError(w, err)
return
}

if len(labels) > 0 {
cfg := *config.GetGlobalConfig()
if cfg.Labels == nil {
cfg.Labels = make(map[string]string, len(labels))
}
for k, v := range labels {
cfg.Labels[k] = v
}
config.StoreGlobalConfig(&cfg)
logutil.BgLogger().Info("update server labels", zap.Any("labels", cfg.Labels))
}

writeData(w, config.GetGlobalConfig().Labels)
}
33 changes: 33 additions & 0 deletions server/http_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1127,3 +1127,36 @@ func TestWriteDBTablesData(t *testing.T) {
require.Equal(t, ti[0].Name.String(), tbs[0].Meta().Name.String())
require.Equal(t, ti[1].Name.String(), tbs[1].Meta().Name.String())
}

func TestSetLabels(t *testing.T) {
ts := createBasicHTTPHandlerTestSuite()

ts.startServer(t)
defer ts.stopServer(t)

testUpdateLabels := func(labels, expected map[string]string) {
buffer := bytes.NewBuffer([]byte{})
require.Nil(t, json.NewEncoder(buffer).Encode(labels))
resp, err := ts.postStatus("/labels", "application/json", buffer)
require.NoError(t, err)
require.NotNil(t, resp)
require.Equal(t, http.StatusOK, resp.StatusCode)
newLabels := config.GetGlobalConfig().Labels
require.Equal(t, newLabels, expected)
}

labels := map[string]string{
"zone": "us-west-1",
"test": "123",
}
testUpdateLabels(labels, labels)

updated := map[string]string{
"zone": "bj-1",
}
labels["zone"] = "bj-1"
testUpdateLabels(updated, labels)

// reset the global variable
config.GetGlobalConfig().Labels = map[string]string{}
}
1 change: 1 addition & 0 deletions server/http_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ func (s *Server) startHTTPServer() {
router.Handle("/config", fn.Wrap(func() (*config.Config, error) {
return config.GetGlobalConfig(), nil
}))
router.Handle("/labels", labelHandler{}).Name("Labels")

// HTTP path for get server info.
router.Handle("/info", serverInfoHandler{tikvHandlerTool}).Name("Info")
Expand Down

0 comments on commit 95e4df8

Please sign in to comment.