diff --git a/server/api/config.go b/server/api/config.go index 0c895ce21c1..af47b8bf18e 100644 --- a/server/api/config.go +++ b/server/api/config.go @@ -49,6 +49,16 @@ func (h *confHandler) Get(w http.ResponseWriter, r *http.Request) { h.rd.JSON(w, http.StatusOK, h.svr.GetConfig()) } +func (h *confHandler) GetDefault(w http.ResponseWriter, r *http.Request) { + config := config.NewConfig() + err := config.Adjust(nil) + if err != nil { + h.rd.JSON(w, http.StatusInternalServerError, err.Error()) + } + + h.rd.JSON(w, http.StatusOK, config) +} + func (h *confHandler) Post(w http.ResponseWriter, r *http.Request) { if h.svr.GetConfig().EnableDynamicConfig { cm := h.svr.GetConfigManager() diff --git a/server/api/config_test.go b/server/api/config_test.go index 7aac2c666a3..bd381ef0b0e 100644 --- a/server/api/config_test.go +++ b/server/api/config_test.go @@ -19,6 +19,7 @@ import ( "time" . "github.com/pingcap/check" + "github.com/pingcap/pd/v4/pkg/typeutil" "github.com/pingcap/pd/v4/server" "github.com/pingcap/pd/v4/server/config" ) @@ -177,3 +178,40 @@ func (s *testConfigSuite) TestConfigLabelProperty(c *C) { c.Assert(cfg, HasLen, 1) c.Assert(cfg["foo"], DeepEquals, []config.StoreLabel{{Key: "zone", Value: "cn2"}}) } + +func (s *testConfigSuite) TestConfigDefault(c *C) { + addr := fmt.Sprintf("%s/config", s.urlPrefix) + + r := map[string]int{"max-replicas": 5} + postData, err := json.Marshal(r) + c.Assert(err, IsNil) + err = postJSON(addr, postData) + c.Assert(err, IsNil) + l := map[string]interface{}{ + "location-labels": "zone,rack", + "region-schedule-limit": 10, + } + postData, err = json.Marshal(l) + c.Assert(err, IsNil) + err = postJSON(addr, postData) + c.Assert(err, IsNil) + + l = map[string]interface{}{ + "metric-storage": "http://127.0.0.1:9090", + } + postData, err = json.Marshal(l) + c.Assert(err, IsNil) + err = postJSON(addr, postData) + c.Assert(err, IsNil) + + time.Sleep(20 * time.Millisecond) + addr = fmt.Sprintf("%s/config/default", s.urlPrefix) + defaultCfg := &config.Config{} + err = readJSON(addr, defaultCfg) + c.Assert(err, IsNil) + + c.Assert(defaultCfg.Replication.MaxReplicas, Equals, uint64(3)) + c.Assert(defaultCfg.Replication.LocationLabels, DeepEquals, typeutil.StringSlice([]string{})) + c.Assert(defaultCfg.Schedule.RegionScheduleLimit, Equals, uint64(2048)) + c.Assert(defaultCfg.PDServerCfg.MetricStorage, Equals, "") +} diff --git a/server/api/router.go b/server/api/router.go index 40bdec760c8..4501855a74d 100644 --- a/server/api/router.go +++ b/server/api/router.go @@ -78,6 +78,7 @@ func createRouter(ctx context.Context, prefix string, svr *server.Server) (*mux. confHandler := newConfHandler(svr, rd) apiRouter.HandleFunc("/config", confHandler.Get).Methods("GET") apiRouter.HandleFunc("/config", confHandler.Post).Methods("POST") + apiRouter.HandleFunc("/config/default", confHandler.GetDefault).Methods("GET") apiRouter.HandleFunc("/config/schedule", confHandler.GetSchedule).Methods("GET") apiRouter.HandleFunc("/config/schedule", confHandler.SetSchedule).Methods("POST") apiRouter.HandleFunc("/config/replicate", confHandler.GetReplication).Methods("GET")