Skip to content

Commit

Permalink
support cluster version and label property
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Leung <rleungx@gmail.com>
  • Loading branch information
rleungx committed Apr 17, 2020
1 parent ad324b3 commit fddc382
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 1 deletion.
58 changes: 57 additions & 1 deletion server/api/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,17 @@ func (h *confHandler) updateWithoutPrefix(w http.ResponseWriter, config *config.
h.rd.JSON(w, http.StatusBadRequest, err.Error())
return
}
if !found1 && !found2 && !found3 && !found4 {
found5, err := h.updateClusterVersion(data, config)
if err != nil {
h.rd.JSON(w, http.StatusBadRequest, err.Error())
return
}
found6, err := h.updateLabelProperty(data, config)
if err != nil {
h.rd.JSON(w, http.StatusBadRequest, err.Error())
return
}
if !found1 && !found2 && !found3 && !found4 && !found5 && !found6 {
h.rd.JSON(w, http.StatusBadRequest, "config item not found")
return
}
Expand Down Expand Up @@ -207,6 +217,52 @@ func (h *confHandler) updateLogLevel(data []byte, config *config.Config) (bool,
return false, err
}

func (h *confHandler) updateClusterVersion(data []byte, config *config.Config) (bool, error) {
cfg := make(map[string]interface{})
err := json.Unmarshal(data, &cfg)
if err != nil {
return false, err
}

if version, ok := cfg["cluster-version"].(string); ok {
err := h.svr.SetClusterVersion(version)
if err != nil {
return true, err
}
return true, nil
}
return false, err
}

func (h *confHandler) updateLabelProperty(data []byte, config *config.Config) (bool, error) {
cfg := make(map[string]interface{})
err := json.Unmarshal(data, &cfg)
if err != nil {
return false, err
}

if lp, ok := cfg["label-property"].(string); ok {
input := make(map[string]string)
err = json.Unmarshal([]byte(lp), &input)
if err != nil {
return true, err
}
switch input["action"] {
case "set":
err = h.svr.SetLabelProperty(input["type"], input["label-key"], input["label-value"])
case "delete":
err = h.svr.DeleteLabelProperty(input["type"], input["label-key"], input["label-value"])
default:
err = errors.Errorf("unknown action %v", input["action"])
}
if err != nil {
return true, err
}
return true, nil
}
return false, err
}

func (h *confHandler) mergeConfig(v interface{}, data []byte) (updated bool, found bool, err error) {
old, _ := json.Marshal(v)
if err := json.Unmarshal(data, v); err != nil {
Expand Down
17 changes: 17 additions & 0 deletions server/api/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
. "github.com/pingcap/check"
"github.com/pingcap/pd/v4/pkg/typeutil"
"github.com/pingcap/pd/v4/server"
"github.com/pingcap/pd/v4/server/cluster"
"github.com/pingcap/pd/v4/server/config"
)

Expand Down Expand Up @@ -89,6 +90,8 @@ func (s *testConfigSuite) TestConfigAll(c *C) {
"replication.location-labels": "idc,host",
"pd-server.metric-storage": "http://127.0.0.1:1234",
"log.level": "warn",
"cluster-version": "v4.0.0-beta",
"label-property": `{"type": "foo", "action": "set", "label-key": "zone", "label-value": "cn1"}`,
}
postData, err = json.Marshal(l)
c.Assert(err, IsNil)
Expand All @@ -101,8 +104,22 @@ func (s *testConfigSuite) TestConfigAll(c *C) {
cfg.Replication.LocationLabels = []string{"idc", "host"}
cfg.PDServerCfg.MetricStorage = "http://127.0.0.1:1234"
cfg.Log.Level = "warn"
v, err := cluster.ParseVersion("v4.0.0-beta")
c.Assert(err, IsNil)
cfg.ClusterVersion = *v
cfg.LabelProperty = map[string][]config.StoreLabel{
"foo": {{Key: "zone", Value: "cn1"}},
}
c.Assert(newCfg1, DeepEquals, cfg)

l = map[string]interface{}{
"label-property": `{"type": "foo", "action": "delete", "label-key": "zone", "label-value": "cn1"}`,
}
postData, err = json.Marshal(l)
c.Assert(err, IsNil)
err = postJSON(addr, postData)
c.Assert(err, IsNil)

// illegal prefix
l = map[string]interface{}{
"replicate.max-replicas": 1,
Expand Down

0 comments on commit fddc382

Please sign in to comment.