From b34289e4fde3b0ec51cc944384f2915b94c7d540 Mon Sep 17 00:00:00 2001 From: Connor1996 Date: Mon, 8 Jan 2018 18:48:51 +0800 Subject: [PATCH 1/9] add debug log --- pdctl/command/debug_command.go | 67 +++++++++++++++++++++++++++++ pdctl/ctl.go | 1 + pkg/logutil/log.go | 4 +- pkg/logutil/log_test.go | 14 +++--- server/api/debug.go | 61 ++++++++++++++++++++++++++ server/api/router.go | 3 ++ server/schedule/filters.go | 3 ++ server/schedulers/balance_leader.go | 3 ++ server/schedulers/balance_region.go | 3 ++ server/schedulers/utils.go | 8 +++- server/server.go | 4 ++ 11 files changed, 161 insertions(+), 10 deletions(-) create mode 100644 pdctl/command/debug_command.go create mode 100644 server/api/debug.go diff --git a/pdctl/command/debug_command.go b/pdctl/command/debug_command.go new file mode 100644 index 00000000000..5153682daa3 --- /dev/null +++ b/pdctl/command/debug_command.go @@ -0,0 +1,67 @@ +// Copyright 2018 PingCAP, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// See the License for the specific language governing permissions and +// limitations under the License. + +package command + +import ( + "bytes" + "encoding/json" + "fmt" + "net/http" + "strconv" + + "github.com/spf13/cobra" +) + +var ( + debugPrefix = "pd/api/v1/debug" +) + +// NewDebugCommand New a debug subcommand of the rootCmd +func NewDebugCommand() *cobra.Command { + conf := &cobra.Command{ + Use: "debug ", + Short: "enable debug info", + Run: debugCommandFunc, + } + return conf +} + +func debugCommandFunc(cmd *cobra.Command, args []string) { + var debug bool + var err error + if len(args) != 1 { + fmt.Println(cmd.UsageString()) + return + } else if debug, err = strconv.ParseBool(args[0]); err != nil { + fmt.Println("argument should be a bool var") + return + } + + data, err := json.Marshal(debug) + if err != nil { + fmt.Printf("Failed to set debug mode: %s\n", err) + return + } + req, err := getRequest(cmd, debugPrefix, http.MethodPost, "application/json", bytes.NewBuffer(data)) + if err != nil { + fmt.Printf("Failed to set debug mode: %s\n", err) + return + } + _, err = dail(req) + if err != nil { + fmt.Printf("Failed to set debug mode: %s\n", err) + return + } + fmt.Println("Success!") +} diff --git a/pdctl/ctl.go b/pdctl/ctl.go index fd0a24d707a..b50fbedb5df 100644 --- a/pdctl/ctl.go +++ b/pdctl/ctl.go @@ -56,6 +56,7 @@ func init() { command.NewClusterCommand(), command.NewTableNamespaceCommand(), command.NewHealthCommand(), + command.NewDebugCommand(), ) cobra.EnablePrefixMatching = true } diff --git a/pkg/logutil/log.go b/pkg/logutil/log.go index 6bb8a720a81..b59856a19e6 100644 --- a/pkg/logutil/log.go +++ b/pkg/logutil/log.go @@ -124,7 +124,7 @@ func (hook *contextHook) Levels() []log.Level { return log.AllLevels } -func stringToLogLevel(level string) log.Level { +func StringToLogLevel(level string) log.Level { switch strings.ToLower(level) { case "fatal": return log.FatalLevel @@ -221,7 +221,7 @@ var once sync.Once func InitLogger(cfg *LogConfig) error { var err error once.Do(func() { - log.SetLevel(stringToLogLevel(cfg.Level)) + log.SetLevel(StringToLogLevel(cfg.Level)) log.AddHook(&contextHook{}) if cfg.Format == "" { diff --git a/pkg/logutil/log_test.go b/pkg/logutil/log_test.go index cf216a36a48..07b8815997e 100644 --- a/pkg/logutil/log_test.go +++ b/pkg/logutil/log_test.go @@ -42,13 +42,13 @@ func (s *testLogSuite) SetUpSuite(c *C) { } func (s *testLogSuite) TestStringToLogLevel(c *C) { - c.Assert(stringToLogLevel("fatal"), Equals, log.FatalLevel) - c.Assert(stringToLogLevel("ERROR"), Equals, log.ErrorLevel) - c.Assert(stringToLogLevel("warn"), Equals, log.WarnLevel) - c.Assert(stringToLogLevel("warning"), Equals, log.WarnLevel) - c.Assert(stringToLogLevel("debug"), Equals, log.DebugLevel) - c.Assert(stringToLogLevel("info"), Equals, log.InfoLevel) - c.Assert(stringToLogLevel("whatever"), Equals, log.InfoLevel) + c.Assert(StringToLogLevel("fatal"), Equals, log.FatalLevel) + c.Assert(StringToLogLevel("ERROR"), Equals, log.ErrorLevel) + c.Assert(StringToLogLevel("warn"), Equals, log.WarnLevel) + c.Assert(StringToLogLevel("warning"), Equals, log.WarnLevel) + c.Assert(StringToLogLevel("debug"), Equals, log.DebugLevel) + c.Assert(StringToLogLevel("info"), Equals, log.InfoLevel) + c.Assert(StringToLogLevel("whatever"), Equals, log.InfoLevel) } // TestLogging assure log format and log redirection works. diff --git a/server/api/debug.go b/server/api/debug.go new file mode 100644 index 00000000000..0b3b6b74aa8 --- /dev/null +++ b/server/api/debug.go @@ -0,0 +1,61 @@ +// Copyright 2018 PingCAP, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// See the License for the specific language governing permissions and +// limitations under the License. + +package api + +import ( + "encoding/json" + "io/ioutil" + "net/http" + + "github.com/pingcap/pd/pkg/logutil" + "github.com/pingcap/pd/server" + log "github.com/sirupsen/logrus" + "github.com/unrolled/render" +) + +type debugHandler struct { + svr *server.Server + rd *render.Render +} + +func newDebugHandler(svr *server.Server, rd *render.Render) *debugHandler { + return &debugHandler{ + svr: svr, + rd: rd, + } +} + +func (h *debugHandler) Handle(w http.ResponseWriter, r *http.Request) { + var debug bool + data, err := ioutil.ReadAll(r.Body) + r.Body.Close() + if err != nil { + h.rd.JSON(w, http.StatusInternalServerError, err.Error()) + return + } + err = json.Unmarshal(data, &debug) + if err != nil { + h.rd.JSON(w, http.StatusInternalServerError, err.Error()) + return + } + + level := "info" + if debug { + level = "debug" + } + h.svr.SetLogLevel(level) + log.SetLevel(logutil.StringToLogLevel(level)) + + h.rd.JSON(w, http.StatusOK, nil) +} diff --git a/server/api/router.go b/server/api/router.go index e6414b2cf4c..071e9005e31 100644 --- a/server/api/router.go +++ b/server/api/router.go @@ -100,6 +100,9 @@ func createRouter(prefix string, svr *server.Server) *mux.Router { trendHandler := newTrendHandler(svr, rd) router.HandleFunc("/api/v1/trend", trendHandler.Handle).Methods("GET") + debugHanler := newDebugHandler(svr, rd) + router.HandleFunc("/api/v1/debug", debugHanler.Handle).Methods("POST") + router.HandleFunc("/ping", func(w http.ResponseWriter, r *http.Request) {}).Methods("GET") return router } diff --git a/server/schedule/filters.go b/server/schedule/filters.go index 428609e08dd..292360c51c4 100644 --- a/server/schedule/filters.go +++ b/server/schedule/filters.go @@ -17,6 +17,7 @@ import ( "github.com/pingcap/pd/server/cache" "github.com/pingcap/pd/server/core" "github.com/pingcap/pd/server/namespace" + log "github.com/sirupsen/logrus" ) // Filter is an interface to filter source and target store. @@ -31,6 +32,7 @@ type Filter interface { func FilterSource(opt Options, store *core.StoreInfo, filters []Filter) bool { for _, filter := range filters { if filter.FilterSource(opt, store) { + log.Debugf("filter %T filtes store %v from source", filter, store) return true } } @@ -41,6 +43,7 @@ func FilterSource(opt Options, store *core.StoreInfo, filters []Filter) bool { func FilterTarget(opt Options, store *core.StoreInfo, filters []Filter) bool { for _, filter := range filters { if filter.FilterTarget(opt, store) { + log.Debugf("filter %T filtes store %v from target", filter, store) return true } } diff --git a/server/schedulers/balance_leader.go b/server/schedulers/balance_leader.go index 5a299701bbf..1c567693a9e 100644 --- a/server/schedulers/balance_leader.go +++ b/server/schedulers/balance_leader.go @@ -16,6 +16,7 @@ package schedulers import ( "github.com/pingcap/pd/server/core" "github.com/pingcap/pd/server/schedule" + log "github.com/sirupsen/logrus" ) func init() { @@ -74,6 +75,8 @@ func (l *balanceLeaderScheduler) Schedule(cluster schedule.Cluster, opInfluence source := cluster.GetStore(region.Leader.GetStoreId()) target := cluster.GetStore(newLeader.GetStoreId()) + log.Debugf("source store is %v", source) + log.Debugf("target store is %v", target) avgScore := cluster.GetStoresAverageScore(core.LeaderKind) if !shouldBalance(source, target, avgScore, core.LeaderKind, region, opInfluence, cluster.GetTolerantSizeRatio()) { schedulerCounter.WithLabelValues(l.GetName(), "skip").Inc() diff --git a/server/schedulers/balance_region.go b/server/schedulers/balance_region.go index 65c93f601b0..39a22610bf7 100644 --- a/server/schedulers/balance_region.go +++ b/server/schedulers/balance_region.go @@ -20,6 +20,7 @@ import ( "github.com/pingcap/pd/server/cache" "github.com/pingcap/pd/server/core" "github.com/pingcap/pd/server/schedule" + log "github.com/sirupsen/logrus" ) func init() { @@ -117,6 +118,8 @@ func (s *balanceRegionScheduler) transferPeer(cluster schedule.Cluster, region * target := cluster.GetStore(newPeer.GetStoreId()) avgScore := cluster.GetStoresAverageScore(core.RegionKind) + log.Debugf("[balance region] source store is %v", source) + log.Debugf("[balance region] target store is %v", target) if !shouldBalance(source, target, avgScore, core.RegionKind, region, opInfluence, cluster.GetTolerantSizeRatio()) { schedulerCounter.WithLabelValues(s.GetName(), "skip").Inc() return nil diff --git a/server/schedulers/utils.go b/server/schedulers/utils.go index c2599282965..34823bbc38f 100644 --- a/server/schedulers/utils.go +++ b/server/schedulers/utils.go @@ -39,6 +39,8 @@ func scheduleTransferLeader(cluster schedule.Cluster, schedulerName string, s sc mostLeaderStore := s.SelectSource(cluster, stores, filters...) leastLeaderStore := s.SelectTarget(cluster, stores, filters...) + log.Debugf("mostLeaderStore is %v", mostLeaderStore) + log.Debugf("leastLeaderStore is %v", leastLeaderStore) var mostLeaderDistance, leastLeaderDistance float64 if mostLeaderStore != nil { @@ -47,6 +49,8 @@ func scheduleTransferLeader(cluster schedule.Cluster, schedulerName string, s sc if leastLeaderStore != nil { leastLeaderDistance = math.Abs(leastLeaderStore.LeaderScore() - averageLeader) } + log.Debugf("mostLeaderDistance is %v", mostLeaderDistance) + log.Debugf("leastLeaderDistance is %v", leastLeaderDistance) if mostLeaderDistance == 0 && leastLeaderDistance == 0 { schedulerCounter.WithLabelValues(schedulerName, "already_balanced").Inc() return nil, nil @@ -63,7 +67,7 @@ func scheduleTransferLeader(cluster schedule.Cluster, schedulerName string, s sc region, peer = scheduleRemoveLeader(cluster, schedulerName, mostLeaderStore.GetId(), s) } } - + log.Debugf("transfer leader select %v to be new leader for region %v", peer, region) return region, peer } @@ -173,6 +177,7 @@ func shouldBalance(source, target *core.StoreInfo, avgScore float64, kind core.R sourceScore := source.ResourceScore(kind) targetScore := target.ResourceScore(kind) if targetScore >= sourceScore { + log.Debugf("should balance return false cause targetScore %v >= sourceScore %v", targetScore, sourceScore) return false } @@ -183,6 +188,7 @@ func shouldBalance(source, target *core.StoreInfo, avgScore float64, kind core.R sourceSizeDiff := (sourceScore - avgScore) * source.ResourceWeight(kind) targetSizeDiff := (avgScore - targetScore) * target.ResourceWeight(kind) + log.Debugf("size diff is %v and region size is %v", math.Min(sourceSizeDiff, targetSizeDiff), float64(region.ApproximateSize)*tolerantRatio) return math.Min(sourceSizeDiff, targetSizeDiff) >= float64(region.ApproximateSize)*tolerantRatio } diff --git a/server/server.go b/server/server.go index e7503b5bd3d..63798c5e32a 100644 --- a/server/server.go +++ b/server/server.go @@ -558,3 +558,7 @@ func (s *Server) GetClusterStatus() (*ClusterStatus, error) { func (s *Server) getAllocIDPath() string { return path.Join(s.rootPath, "alloc_id") } + +func (s *Server) SetLogLevel(level string) { + s.cfg.Log.Level = level +} From 0a2255a661f812aec7dad40c2f5c6ef28285501d Mon Sep 17 00:00:00 2001 From: Connor1996 Date: Tue, 9 Jan 2018 12:58:13 +0800 Subject: [PATCH 2/9] fix ci --- pkg/logutil/log.go | 1 + server/server.go | 1 + 2 files changed, 2 insertions(+) diff --git a/pkg/logutil/log.go b/pkg/logutil/log.go index b59856a19e6..4d17e9a4f6a 100644 --- a/pkg/logutil/log.go +++ b/pkg/logutil/log.go @@ -124,6 +124,7 @@ func (hook *contextHook) Levels() []log.Level { return log.AllLevels } +// StringToLogLevel converts string to relative log.Level. func StringToLogLevel(level string) log.Level { switch strings.ToLower(level) { case "fatal": diff --git a/server/server.go b/server/server.go index 63798c5e32a..3816c6716bc 100644 --- a/server/server.go +++ b/server/server.go @@ -559,6 +559,7 @@ func (s *Server) getAllocIDPath() string { return path.Join(s.rootPath, "alloc_id") } +// SetLogLevel sets log level. func (s *Server) SetLogLevel(level string) { s.cfg.Log.Level = level } From a3b2e5bd65634b5db4b856709f5c95e9cd3de38e Mon Sep 17 00:00:00 2001 From: Connor1996 Date: Tue, 9 Jan 2018 13:34:49 +0800 Subject: [PATCH 3/9] tiny change --- server/schedule/filters.go | 4 ++-- server/schedulers/balance_leader.go | 3 +-- server/schedulers/balance_region.go | 3 +-- server/schedulers/utils.go | 8 +++----- 4 files changed, 7 insertions(+), 11 deletions(-) diff --git a/server/schedule/filters.go b/server/schedule/filters.go index 292360c51c4..6b12399d7a3 100644 --- a/server/schedule/filters.go +++ b/server/schedule/filters.go @@ -32,7 +32,7 @@ type Filter interface { func FilterSource(opt Options, store *core.StoreInfo, filters []Filter) bool { for _, filter := range filters { if filter.FilterSource(opt, store) { - log.Debugf("filter %T filtes store %v from source", filter, store) + log.Debugf("[filter %T] filtes store %v from source", filter, store) return true } } @@ -43,7 +43,7 @@ func FilterSource(opt Options, store *core.StoreInfo, filters []Filter) bool { func FilterTarget(opt Options, store *core.StoreInfo, filters []Filter) bool { for _, filter := range filters { if filter.FilterTarget(opt, store) { - log.Debugf("filter %T filtes store %v from target", filter, store) + log.Debugf("[filter %T] filtes store %v from target", filter, store) return true } } diff --git a/server/schedulers/balance_leader.go b/server/schedulers/balance_leader.go index 1c567693a9e..e0c5d00525b 100644 --- a/server/schedulers/balance_leader.go +++ b/server/schedulers/balance_leader.go @@ -75,8 +75,7 @@ func (l *balanceLeaderScheduler) Schedule(cluster schedule.Cluster, opInfluence source := cluster.GetStore(region.Leader.GetStoreId()) target := cluster.GetStore(newLeader.GetStoreId()) - log.Debugf("source store is %v", source) - log.Debugf("target store is %v", target) + log.Debugf("source store is %v, target store is %v", source, target) avgScore := cluster.GetStoresAverageScore(core.LeaderKind) if !shouldBalance(source, target, avgScore, core.LeaderKind, region, opInfluence, cluster.GetTolerantSizeRatio()) { schedulerCounter.WithLabelValues(l.GetName(), "skip").Inc() diff --git a/server/schedulers/balance_region.go b/server/schedulers/balance_region.go index 39a22610bf7..c1be09f17e9 100644 --- a/server/schedulers/balance_region.go +++ b/server/schedulers/balance_region.go @@ -118,8 +118,7 @@ func (s *balanceRegionScheduler) transferPeer(cluster schedule.Cluster, region * target := cluster.GetStore(newPeer.GetStoreId()) avgScore := cluster.GetStoresAverageScore(core.RegionKind) - log.Debugf("[balance region] source store is %v", source) - log.Debugf("[balance region] target store is %v", target) + log.Debugf("source store is %v, target store is %v", source, target) if !shouldBalance(source, target, avgScore, core.RegionKind, region, opInfluence, cluster.GetTolerantSizeRatio()) { schedulerCounter.WithLabelValues(s.GetName(), "skip").Inc() return nil diff --git a/server/schedulers/utils.go b/server/schedulers/utils.go index 34823bbc38f..ae8e28023b5 100644 --- a/server/schedulers/utils.go +++ b/server/schedulers/utils.go @@ -39,8 +39,7 @@ func scheduleTransferLeader(cluster schedule.Cluster, schedulerName string, s sc mostLeaderStore := s.SelectSource(cluster, stores, filters...) leastLeaderStore := s.SelectTarget(cluster, stores, filters...) - log.Debugf("mostLeaderStore is %v", mostLeaderStore) - log.Debugf("leastLeaderStore is %v", leastLeaderStore) + log.Debugf("[%v] mostLeaderStore is %v, leastLeaderStore is %v", schedulerName, mostLeaderStore, leastLeaderStore) var mostLeaderDistance, leastLeaderDistance float64 if mostLeaderStore != nil { @@ -49,8 +48,7 @@ func scheduleTransferLeader(cluster schedule.Cluster, schedulerName string, s sc if leastLeaderStore != nil { leastLeaderDistance = math.Abs(leastLeaderStore.LeaderScore() - averageLeader) } - log.Debugf("mostLeaderDistance is %v", mostLeaderDistance) - log.Debugf("leastLeaderDistance is %v", leastLeaderDistance) + log.Debugf("[%v] mostLeaderDistance is %v, leastLeaderDistance is %v", schedulerName, mostLeaderDistance, leastLeaderDistance) if mostLeaderDistance == 0 && leastLeaderDistance == 0 { schedulerCounter.WithLabelValues(schedulerName, "already_balanced").Inc() return nil, nil @@ -67,7 +65,7 @@ func scheduleTransferLeader(cluster schedule.Cluster, schedulerName string, s sc region, peer = scheduleRemoveLeader(cluster, schedulerName, mostLeaderStore.GetId(), s) } } - log.Debugf("transfer leader select %v to be new leader for region %v", peer, region) + log.Debugf("[%v][region %v] select %v to be new leader", schedulerName, region.GetId(), peer) return region, peer } From 3ec9aba946822e15301b103551b02fdb6bdec2c4 Mon Sep 17 00:00:00 2001 From: Connor1996 Date: Tue, 9 Jan 2018 15:34:52 +0800 Subject: [PATCH 4/9] fix test --- server/schedulers/utils.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/server/schedulers/utils.go b/server/schedulers/utils.go index ae8e28023b5..8b761370dc7 100644 --- a/server/schedulers/utils.go +++ b/server/schedulers/utils.go @@ -65,7 +65,11 @@ func scheduleTransferLeader(cluster schedule.Cluster, schedulerName string, s sc region, peer = scheduleRemoveLeader(cluster, schedulerName, mostLeaderStore.GetId(), s) } } - log.Debugf("[%v][region %v] select %v to be new leader", schedulerName, region.GetId(), peer) + if region == nil { + log.Debugf("[%v] select no region", schedulerName) + } else { + log.Debugf("[%v][region %v] select %v to be new leader", schedulerName, region.GetId(), peer) + } return region, peer } From 9f92af191af72646a602829f9ffb3188df375151 Mon Sep 17 00:00:00 2001 From: Connor1996 Date: Tue, 9 Jan 2018 16:06:01 +0800 Subject: [PATCH 5/9] set log level --- .../{debug_command.go => log_command.go} | 29 ++++++++----------- pdctl/ctl.go | 2 +- server/api/{debug.go => log.go} | 16 ++++------ server/api/router.go | 4 +-- 4 files changed, 21 insertions(+), 30 deletions(-) rename pdctl/command/{debug_command.go => log_command.go} (55%) rename server/api/{debug.go => log.go} (79%) diff --git a/pdctl/command/debug_command.go b/pdctl/command/log_command.go similarity index 55% rename from pdctl/command/debug_command.go rename to pdctl/command/log_command.go index 5153682daa3..a84cd58bb36 100644 --- a/pdctl/command/debug_command.go +++ b/pdctl/command/log_command.go @@ -18,49 +18,44 @@ import ( "encoding/json" "fmt" "net/http" - "strconv" "github.com/spf13/cobra" ) var ( - debugPrefix = "pd/api/v1/debug" + logPrefix = "pd/api/v1/log" ) -// NewDebugCommand New a debug subcommand of the rootCmd -func NewDebugCommand() *cobra.Command { +// NewLogCommand New a log subcommand of the rootCmd +func NewLogCommand() *cobra.Command { conf := &cobra.Command{ - Use: "debug ", - Short: "enable debug info", - Run: debugCommandFunc, + Use: "log [fatal|error|warn|info|debug]", + Short: "set log level", + Run: logCommandFunc, } return conf } -func debugCommandFunc(cmd *cobra.Command, args []string) { - var debug bool +func logCommandFunc(cmd *cobra.Command, args []string) { var err error if len(args) != 1 { fmt.Println(cmd.UsageString()) return - } else if debug, err = strconv.ParseBool(args[0]); err != nil { - fmt.Println("argument should be a bool var") - return } - data, err := json.Marshal(debug) + data, err := json.Marshal(args[0]) if err != nil { - fmt.Printf("Failed to set debug mode: %s\n", err) + fmt.Printf("Failed to set log level: %s\n", err) return } - req, err := getRequest(cmd, debugPrefix, http.MethodPost, "application/json", bytes.NewBuffer(data)) + req, err := getRequest(cmd, logPrefix, http.MethodPost, "application/json", bytes.NewBuffer(data)) if err != nil { - fmt.Printf("Failed to set debug mode: %s\n", err) + fmt.Printf("Failed to set log level: %s\n", err) return } _, err = dail(req) if err != nil { - fmt.Printf("Failed to set debug mode: %s\n", err) + fmt.Printf("Failed to set log level: %s\n", err) return } fmt.Println("Success!") diff --git a/pdctl/ctl.go b/pdctl/ctl.go index b50fbedb5df..a319dc7a72a 100644 --- a/pdctl/ctl.go +++ b/pdctl/ctl.go @@ -56,7 +56,7 @@ func init() { command.NewClusterCommand(), command.NewTableNamespaceCommand(), command.NewHealthCommand(), - command.NewDebugCommand(), + command.NewLogCommand(), ) cobra.EnablePrefixMatching = true } diff --git a/server/api/debug.go b/server/api/log.go similarity index 79% rename from server/api/debug.go rename to server/api/log.go index 0b3b6b74aa8..cab2e8abce4 100644 --- a/server/api/debug.go +++ b/server/api/log.go @@ -24,36 +24,32 @@ import ( "github.com/unrolled/render" ) -type debugHandler struct { +type logHandler struct { svr *server.Server rd *render.Render } -func newDebugHandler(svr *server.Server, rd *render.Render) *debugHandler { - return &debugHandler{ +func newlogHandler(svr *server.Server, rd *render.Render) *logHandler { + return &logHandler{ svr: svr, rd: rd, } } -func (h *debugHandler) Handle(w http.ResponseWriter, r *http.Request) { - var debug bool +func (h *logHandler) Handle(w http.ResponseWriter, r *http.Request) { + var level string data, err := ioutil.ReadAll(r.Body) r.Body.Close() if err != nil { h.rd.JSON(w, http.StatusInternalServerError, err.Error()) return } - err = json.Unmarshal(data, &debug) + err = json.Unmarshal(data, &level) if err != nil { h.rd.JSON(w, http.StatusInternalServerError, err.Error()) return } - level := "info" - if debug { - level = "debug" - } h.svr.SetLogLevel(level) log.SetLevel(logutil.StringToLogLevel(level)) diff --git a/server/api/router.go b/server/api/router.go index 071e9005e31..ccbc93c15bc 100644 --- a/server/api/router.go +++ b/server/api/router.go @@ -100,8 +100,8 @@ func createRouter(prefix string, svr *server.Server) *mux.Router { trendHandler := newTrendHandler(svr, rd) router.HandleFunc("/api/v1/trend", trendHandler.Handle).Methods("GET") - debugHanler := newDebugHandler(svr, rd) - router.HandleFunc("/api/v1/debug", debugHanler.Handle).Methods("POST") + logHanler := newlogHandler(svr, rd) + router.HandleFunc("/api/v1/log", logHanler.Handle).Methods("POST") router.HandleFunc("/ping", func(w http.ResponseWriter, r *http.Request) {}).Methods("GET") return router From 9b7754efacbc20d53383f3d0c9af51bd0a851a74 Mon Sep 17 00:00:00 2001 From: Connor1996 Date: Tue, 9 Jan 2018 16:35:31 +0800 Subject: [PATCH 6/9] address comment --- server/schedule/filters.go | 4 ++-- server/schedulers/utils.go | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/server/schedule/filters.go b/server/schedule/filters.go index 6b12399d7a3..3ae4c22d2e6 100644 --- a/server/schedule/filters.go +++ b/server/schedule/filters.go @@ -32,7 +32,7 @@ type Filter interface { func FilterSource(opt Options, store *core.StoreInfo, filters []Filter) bool { for _, filter := range filters { if filter.FilterSource(opt, store) { - log.Debugf("[filter %T] filtes store %v from source", filter, store) + log.Debugf("[filter %T] filters store %v from source", filter, store) return true } } @@ -43,7 +43,7 @@ func FilterSource(opt Options, store *core.StoreInfo, filters []Filter) bool { func FilterTarget(opt Options, store *core.StoreInfo, filters []Filter) bool { for _, filter := range filters { if filter.FilterTarget(opt, store) { - log.Debugf("[filter %T] filtes store %v from target", filter, store) + log.Debugf("[filter %T] filters store %v from target", filter, store) return true } } diff --git a/server/schedulers/utils.go b/server/schedulers/utils.go index 8b761370dc7..95402c4868e 100644 --- a/server/schedulers/utils.go +++ b/server/schedulers/utils.go @@ -68,7 +68,7 @@ func scheduleTransferLeader(cluster schedule.Cluster, schedulerName string, s sc if region == nil { log.Debugf("[%v] select no region", schedulerName) } else { - log.Debugf("[%v][region %v] select %v to be new leader", schedulerName, region.GetId(), peer) + log.Debugf("[region %v][%v] select %v to be new leader", schedulerName, region.GetId(), peer) } return region, peer } From bd4b5a84ac95de2306cb4fffaa17efc894d443d1 Mon Sep 17 00:00:00 2001 From: Connor1996 Date: Tue, 9 Jan 2018 18:44:55 +0800 Subject: [PATCH 7/9] address comment --- server/schedulers/balance_leader.go | 2 +- server/schedulers/balance_region.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/server/schedulers/balance_leader.go b/server/schedulers/balance_leader.go index e0c5d00525b..4aa99626e16 100644 --- a/server/schedulers/balance_leader.go +++ b/server/schedulers/balance_leader.go @@ -75,7 +75,7 @@ func (l *balanceLeaderScheduler) Schedule(cluster schedule.Cluster, opInfluence source := cluster.GetStore(region.Leader.GetStoreId()) target := cluster.GetStore(newLeader.GetStoreId()) - log.Debugf("source store is %v, target store is %v", source, target) + log.Debugf("source store id is %v, target store id is %v", source.GetId(), target.GetId()) avgScore := cluster.GetStoresAverageScore(core.LeaderKind) if !shouldBalance(source, target, avgScore, core.LeaderKind, region, opInfluence, cluster.GetTolerantSizeRatio()) { schedulerCounter.WithLabelValues(l.GetName(), "skip").Inc() diff --git a/server/schedulers/balance_region.go b/server/schedulers/balance_region.go index c1be09f17e9..00901bfc0b6 100644 --- a/server/schedulers/balance_region.go +++ b/server/schedulers/balance_region.go @@ -118,7 +118,7 @@ func (s *balanceRegionScheduler) transferPeer(cluster schedule.Cluster, region * target := cluster.GetStore(newPeer.GetStoreId()) avgScore := cluster.GetStoresAverageScore(core.RegionKind) - log.Debugf("source store is %v, target store is %v", source, target) + log.Debugf("source store id is %v, target store id is %v", source.GetId(), target.GetId()) if !shouldBalance(source, target, avgScore, core.RegionKind, region, opInfluence, cluster.GetTolerantSizeRatio()) { schedulerCounter.WithLabelValues(s.GetName(), "skip").Inc() return nil From 0858b9536212f51f45312e091cdfa004bdad7fe6 Mon Sep 17 00:00:00 2001 From: Connor1996 Date: Wed, 10 Jan 2018 10:43:16 +0800 Subject: [PATCH 8/9] address comment --- server/schedulers/balance_leader.go | 2 +- server/schedulers/balance_region.go | 2 +- server/schedulers/utils.go | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/server/schedulers/balance_leader.go b/server/schedulers/balance_leader.go index 4aa99626e16..6592a9edf48 100644 --- a/server/schedulers/balance_leader.go +++ b/server/schedulers/balance_leader.go @@ -75,7 +75,7 @@ func (l *balanceLeaderScheduler) Schedule(cluster schedule.Cluster, opInfluence source := cluster.GetStore(region.Leader.GetStoreId()) target := cluster.GetStore(newLeader.GetStoreId()) - log.Debugf("source store id is %v, target store id is %v", source.GetId(), target.GetId()) + log.Debugf("[region %d] source store id is %v, target store id is %v", region.GetId(), source.GetId(), target.GetId()) avgScore := cluster.GetStoresAverageScore(core.LeaderKind) if !shouldBalance(source, target, avgScore, core.LeaderKind, region, opInfluence, cluster.GetTolerantSizeRatio()) { schedulerCounter.WithLabelValues(l.GetName(), "skip").Inc() diff --git a/server/schedulers/balance_region.go b/server/schedulers/balance_region.go index 00901bfc0b6..4450f794614 100644 --- a/server/schedulers/balance_region.go +++ b/server/schedulers/balance_region.go @@ -118,7 +118,7 @@ func (s *balanceRegionScheduler) transferPeer(cluster schedule.Cluster, region * target := cluster.GetStore(newPeer.GetStoreId()) avgScore := cluster.GetStoresAverageScore(core.RegionKind) - log.Debugf("source store id is %v, target store id is %v", source.GetId(), target.GetId()) + log.Debugf("[region %d] source store id is %v, target store id is %v", region.GetId(), source.GetId(), target.GetId()) if !shouldBalance(source, target, avgScore, core.RegionKind, region, opInfluence, cluster.GetTolerantSizeRatio()) { schedulerCounter.WithLabelValues(s.GetName(), "skip").Inc() return nil diff --git a/server/schedulers/utils.go b/server/schedulers/utils.go index 95402c4868e..d959ee313c4 100644 --- a/server/schedulers/utils.go +++ b/server/schedulers/utils.go @@ -39,7 +39,7 @@ func scheduleTransferLeader(cluster schedule.Cluster, schedulerName string, s sc mostLeaderStore := s.SelectSource(cluster, stores, filters...) leastLeaderStore := s.SelectTarget(cluster, stores, filters...) - log.Debugf("[%v] mostLeaderStore is %v, leastLeaderStore is %v", schedulerName, mostLeaderStore, leastLeaderStore) + log.Debugf("[%s] mostLeaderStore is %v, leastLeaderStore is %v", schedulerName, mostLeaderStore, leastLeaderStore) var mostLeaderDistance, leastLeaderDistance float64 if mostLeaderStore != nil { @@ -48,7 +48,7 @@ func scheduleTransferLeader(cluster schedule.Cluster, schedulerName string, s sc if leastLeaderStore != nil { leastLeaderDistance = math.Abs(leastLeaderStore.LeaderScore() - averageLeader) } - log.Debugf("[%v] mostLeaderDistance is %v, leastLeaderDistance is %v", schedulerName, mostLeaderDistance, leastLeaderDistance) + log.Debugf("[%s] mostLeaderDistance is %v, leastLeaderDistance is %v", schedulerName, mostLeaderDistance, leastLeaderDistance) if mostLeaderDistance == 0 && leastLeaderDistance == 0 { schedulerCounter.WithLabelValues(schedulerName, "already_balanced").Inc() return nil, nil From ff975d684b9e9e1f6ee57e6b8376b355b27c36ee Mon Sep 17 00:00:00 2001 From: Connor1996 Date: Wed, 10 Jan 2018 14:15:12 +0800 Subject: [PATCH 9/9] address comment --- server/schedulers/utils.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/server/schedulers/utils.go b/server/schedulers/utils.go index d959ee313c4..88906319da7 100644 --- a/server/schedulers/utils.go +++ b/server/schedulers/utils.go @@ -68,7 +68,7 @@ func scheduleTransferLeader(cluster schedule.Cluster, schedulerName string, s sc if region == nil { log.Debugf("[%v] select no region", schedulerName) } else { - log.Debugf("[region %v][%v] select %v to be new leader", schedulerName, region.GetId(), peer) + log.Debugf("[region %v][%v] select %v to be new leader", region.GetId(), schedulerName, peer) } return region, peer } @@ -190,7 +190,7 @@ func shouldBalance(source, target *core.StoreInfo, avgScore float64, kind core.R sourceSizeDiff := (sourceScore - avgScore) * source.ResourceWeight(kind) targetSizeDiff := (avgScore - targetScore) * target.ResourceWeight(kind) - log.Debugf("size diff is %v and region size is %v", math.Min(sourceSizeDiff, targetSizeDiff), float64(region.ApproximateSize)*tolerantRatio) + log.Debugf("[region %d] size diff is %v and tolerant size is %v", region.GetId(), math.Min(sourceSizeDiff, targetSizeDiff), float64(region.ApproximateSize)*tolerantRatio) return math.Min(sourceSizeDiff, targetSizeDiff) >= float64(region.ApproximateSize)*tolerantRatio }