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

server: add a http interface to change labels #36845

Merged
merged 4 commits into from
Aug 4, 2022
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
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