From 0a32ac884da04da79473b4bdcc404056d2cd1102 Mon Sep 17 00:00:00 2001 From: matchge <74505524+matchge-ca@users.noreply.github.com> Date: Wed, 13 Oct 2021 01:19:27 -0400 Subject: [PATCH] api, pd-ctl : support config show server sub-command (#4194) * implement config show server sub-command for pd-ctl Signed-off-by: Hua Lu * Rename func NewShowServerConfigCommand and add test in tests/pdctl/config Signed-off-by: Hua Lu Co-authored-by: Hua Lu Co-authored-by: Ti Chi Robot --- server/api/config.go | 9 +++++ server/api/config_test.go | 23 +++++++++++++ server/api/router.go | 1 + tests/pdctl/config/config_test.go | 36 ++++++++++++++++++++ tools/pd-ctl/pdctl/command/config_command.go | 20 +++++++++++ 5 files changed, 89 insertions(+) diff --git a/server/api/config.go b/server/api/config.go index 0adf980cb8e..146c88fea39 100644 --- a/server/api/config.go +++ b/server/api/config.go @@ -507,3 +507,12 @@ func (h *confHandler) SetReplicationMode(w http.ResponseWriter, r *http.Request) } h.rd.JSON(w, http.StatusOK, "The replication mode config is updated.") } + +// @Tags config +// @Summary Get PD server config. +// @Produce json +// @Success 200 {object} config.PDServerConfig +// @Router /config/pd-server [get] +func (h *confHandler) GetPDServer(w http.ResponseWriter, r *http.Request) { + h.rd.JSON(w, http.StatusOK, h.svr.GetPDServerConfig()) +} diff --git a/server/api/config_test.go b/server/api/config_test.go index 4e972219f62..10f1228ca3e 100644 --- a/server/api/config_test.go +++ b/server/api/config_test.go @@ -279,6 +279,29 @@ func (s *testConfigSuite) TestConfigDefault(c *C) { c.Assert(defaultCfg.PDServerCfg.MetricStorage, Equals, "") } +func (s *testConfigSuite) TestConfigPDServer(c *C) { + addr := fmt.Sprintf("%s/config", s.urlPrefix) + + ms := map[string]interface{}{ + "metric-storage": "", + } + postData, err := json.Marshal(ms) + c.Assert(err, IsNil) + c.Assert(postJSON(testDialClient, addr, postData), IsNil) + + addr = fmt.Sprintf("%s/config/pd-server", s.urlPrefix) + sc := &config.PDServerConfig{} + c.Assert(readJSON(testDialClient, addr, sc), IsNil) + + c.Assert(sc.UseRegionStorage, Equals, bool(true)) + c.Assert(sc.KeyType, Equals, "table") + c.Assert(sc.RuntimeServices, DeepEquals, typeutil.StringSlice([]string{})) + c.Assert(sc.MetricStorage, Equals, "") + c.Assert(sc.DashboardAddress, Equals, "auto") + c.Assert(sc.FlowRoundByDigit, Equals, int(3)) + c.Assert(sc.MaxResetTSGap.Duration, Equals, time.Duration(24*time.Hour)) +} + var ttlConfig = map[string]interface{}{ "schedule.max-snapshot-count": 999, "schedule.enable-location-replacement": false, diff --git a/server/api/router.go b/server/api/router.go index 75db8b2306d..aa132ea16a4 100644 --- a/server/api/router.go +++ b/server/api/router.go @@ -90,6 +90,7 @@ func createRouter(prefix string, svr *server.Server) *mux.Router { 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/pd-server", confHandler.GetPDServer).Methods("GET") apiRouter.HandleFunc("/config/replicate", confHandler.GetReplication).Methods("GET") apiRouter.HandleFunc("/config/replicate", confHandler.SetReplication).Methods("POST") apiRouter.HandleFunc("/config/label-property", confHandler.GetLabelProperty).Methods("GET") diff --git a/tests/pdctl/config/config_test.go b/tests/pdctl/config/config_test.go index 6b0f4413499..593db90b63a 100644 --- a/tests/pdctl/config/config_test.go +++ b/tests/pdctl/config/config_test.go @@ -702,6 +702,42 @@ func (s *configTestSuite) TestUpdateDefaultReplicaConfig(c *C) { checkRuleLocationLabels(1) } +func (s *configTestSuite) TestPDServerConfig(c *C) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + cluster, err := tests.NewTestCluster(ctx, 1) + c.Assert(err, IsNil) + err = cluster.RunInitialServers() + c.Assert(err, IsNil) + cluster.WaitLeader() + pdAddr := cluster.GetConfig().GetClientURL() + cmd := pdctlCmd.GetRootCmd() + + store := &metapb.Store{ + Id: 1, + State: metapb.StoreState_Up, + LastHeartbeat: time.Now().UnixNano(), + } + leaderServer := cluster.GetServer(cluster.GetLeader()) + c.Assert(leaderServer.BootstrapCluster(), IsNil) + svr := leaderServer.GetServer() + pdctl.MustPutStore(c, svr, store) + defer cluster.Destroy() + + output, err := pdctl.ExecuteCommand(cmd, "-u", pdAddr, "config", "show", "server") + c.Assert(err, IsNil) + var conf config.PDServerConfig + json.Unmarshal(output, &conf) + + c.Assert(conf.UseRegionStorage, Equals, bool(true)) + c.Assert(conf.MaxResetTSGap.Duration, Equals, time.Duration(24*time.Hour)) + c.Assert(conf.KeyType, Equals, "table") + c.Assert(conf.RuntimeServices, DeepEquals, typeutil.StringSlice([]string{})) + c.Assert(conf.MetricStorage, Equals, "") + c.Assert(conf.DashboardAddress, Equals, "auto") + c.Assert(conf.FlowRoundByDigit, Equals, int(3)) +} + func assertBundles(a, b []placement.GroupBundle, c *C) { c.Assert(len(a), Equals, len(b)) for i := 0; i < len(a); i++ { diff --git a/tools/pd-ctl/pdctl/command/config_command.go b/tools/pd-ctl/pdctl/command/config_command.go index 03f8f1dbd99..3fa3b0ac879 100644 --- a/tools/pd-ctl/pdctl/command/config_command.go +++ b/tools/pd-ctl/pdctl/command/config_command.go @@ -43,6 +43,7 @@ var ( ruleGroupsPrefix = "pd/api/v1/config/rule_groups" replicationModePrefix = "pd/api/v1/config/replication-mode" ruleBundlePrefix = "pd/api/v1/config/placement-rule" + pdServerPrefix = "pd/api/v1/config/pd-server" ) // NewConfigCommand return a config subcommand of rootCmd @@ -71,6 +72,7 @@ func NewShowConfigCommand() *cobra.Command { sc.AddCommand(NewShowLabelPropertyCommand()) sc.AddCommand(NewShowClusterVersionCommand()) sc.AddCommand(newShowReplicationModeCommand()) + sc.AddCommand(NewShowServerConfigCommand()) return sc } @@ -132,6 +134,15 @@ func newShowReplicationModeCommand() *cobra.Command { } } +// NewShowServerConfigCommand returns a server configuration of show subcommand. +func NewShowServerConfigCommand() *cobra.Command { + return &cobra.Command{ + Use: "server", + Short: "show PD server config", + Run: showServerCommandFunc, + } +} + // NewSetConfigCommand return a set subcommand of configCmd func NewSetConfigCommand() *cobra.Command { sc := &cobra.Command{ @@ -302,6 +313,15 @@ func showReplicationModeCommandFunc(cmd *cobra.Command, args []string) { cmd.Println(r) } +func showServerCommandFunc(cmd *cobra.Command, args []string) { + r, err := doRequest(cmd, pdServerPrefix, http.MethodGet) + if err != nil { + cmd.Printf("Failed to get server config: %s\n", err) + return + } + cmd.Println(r) +} + func postConfigDataWithPath(cmd *cobra.Command, key, value, path string) error { var val interface{} data := make(map[string]interface{})