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

mcs: support changing log level #7172

Merged
merged 2 commits into from
Oct 8, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
31 changes: 28 additions & 3 deletions pkg/mcs/resourcemanager/server/apis/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ import (
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
rmpb "github.com/pingcap/kvproto/pkg/resource_manager"
"github.com/pingcap/log"
rmserver "github.com/tikv/pd/pkg/mcs/resourcemanager/server"
"github.com/tikv/pd/pkg/mcs/utils"
"github.com/tikv/pd/pkg/utils/apiutil"
"github.com/tikv/pd/pkg/utils/apiutil/multiservicesapi"
"github.com/tikv/pd/pkg/utils/logutil"
"github.com/tikv/pd/pkg/utils/reflectutil"
)

Expand All @@ -57,7 +59,7 @@ func init() {
// Service is the resource group service.
type Service struct {
apiHandlerEngine *gin.Engine
baseEndpoint *gin.RouterGroup
root *gin.RouterGroup

manager *rmserver.Manager
}
Expand Down Expand Up @@ -86,15 +88,22 @@ func NewService(srv *rmserver.Service) *Service {
s := &Service{
manager: manager,
apiHandlerEngine: apiHandlerEngine,
baseEndpoint: endpoint,
root: endpoint,
}
s.RegisterAdminRouter()
s.RegisterRouter()
return s
}

// RegisterAdminRouter registers the router of the TSO admin handler.
func (s *Service) RegisterAdminRouter() {
router := s.root.Group("admin")
router.PUT("/log", changeLogLevel)
}

// RegisterRouter registers the router of the service.
func (s *Service) RegisterRouter() {
configEndpoint := s.baseEndpoint.Group("/config")
configEndpoint := s.root.Group("/config")
configEndpoint.POST("/group", s.postResourceGroup)
configEndpoint.PUT("/group", s.putResourceGroup)
configEndpoint.GET("/group/:name", s.getResourceGroup)
Expand All @@ -110,6 +119,22 @@ func (s *Service) handler() http.Handler {
})
}

func changeLogLevel(c *gin.Context) {
svr := c.MustGet(multiservicesapi.ServiceContextKey).(*rmserver.Service)
var level string
if err := c.Bind(&level); err != nil {
c.String(http.StatusBadRequest, err.Error())
return
}

if err := svr.SetLogLevel(level); err != nil {
c.String(http.StatusBadRequest, err.Error())
return
}
log.SetLevel(logutil.StringToZapLogLevel(level))
c.String(http.StatusOK, "The log level is updated.")
}

// postResourceGroup
//
// @Tags ResourceManager
Expand Down
3 changes: 2 additions & 1 deletion pkg/mcs/resourcemanager/server/grpc_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ func (d dummyRestService) ServeHTTP(w http.ResponseWriter, r *http.Request) {

// Service is the gRPC service for resource manager.
type Service struct {
ctx context.Context
ctx context.Context
*Server
manager *Manager
// settings
}
Expand Down
12 changes: 12 additions & 0 deletions pkg/mcs/resourcemanager/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"time"

grpcprometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
"github.com/pingcap/errors"
"github.com/pingcap/failpoint"
"github.com/pingcap/kvproto/pkg/diagnosticspb"
"github.com/pingcap/kvproto/pkg/resource_manager"
Expand Down Expand Up @@ -86,6 +87,17 @@ func (s *Server) GetAddr() string {
return s.cfg.ListenAddr
}

// SetLogLevel sets log level.
func (s *Server) SetLogLevel(level string) error {
if !logutil.IsLevelLegal(level) {
return errors.Errorf("log level %s is illegal", level)
}
s.cfg.Log.Level = level
log.SetLevel(logutil.StringToZapLogLevel(level))
log.Warn("log level changed", zap.String("level", log.GetLevel().String()))
return nil
}

// Run runs the Resource Manager server.
func (s *Server) Run() (err error) {
skipWaitAPIServiceReady := false
Expand Down
25 changes: 25 additions & 0 deletions pkg/mcs/scheduling/server/apis/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/gin-contrib/pprof"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
"github.com/pingcap/log"
scheserver "github.com/tikv/pd/pkg/mcs/scheduling/server"
"github.com/tikv/pd/pkg/mcs/utils"
"github.com/tikv/pd/pkg/schedule"
Expand All @@ -33,6 +34,7 @@ import (
"github.com/tikv/pd/pkg/schedule/operator"
"github.com/tikv/pd/pkg/utils/apiutil"
"github.com/tikv/pd/pkg/utils/apiutil/multiservicesapi"
"github.com/tikv/pd/pkg/utils/logutil"
"github.com/unrolled/render"
)

Expand Down Expand Up @@ -110,12 +112,19 @@ func NewService(srv *scheserver.Service) *Service {
root: root,
rd: createIndentRender(),
}
s.RegisterAdminRouter()
s.RegisterOperatorsRouter()
s.RegisterSchedulersRouter()
s.RegisterCheckersRouter()
return s
}

// RegisterAdminRouter registers the router of the admin handler.
func (s *Service) RegisterAdminRouter() {
router := s.root.Group("admin")
router.PUT("/log", changeLogLevel)
}

// RegisterSchedulersRouter registers the router of the schedulers handler.
func (s *Service) RegisterSchedulersRouter() {
router := s.root.Group("schedulers")
Expand All @@ -138,6 +147,22 @@ func (s *Service) RegisterOperatorsRouter() {
router.GET("/records", getOperatorRecords)
}

func changeLogLevel(c *gin.Context) {
svr := c.MustGet(multiservicesapi.ServiceContextKey).(*scheserver.Server)
var level string
if err := c.Bind(&level); err != nil {
c.String(http.StatusBadRequest, err.Error())
return
}

if err := svr.SetLogLevel(level); err != nil {
c.String(http.StatusBadRequest, err.Error())
return
}
log.SetLevel(logutil.StringToZapLogLevel(level))
c.String(http.StatusOK, "The log level is updated.")
}

// @Tags operators
// @Summary Get an operator by ID.
// @Param region_id path int true "A Region's Id"
Expand Down
12 changes: 12 additions & 0 deletions pkg/mcs/scheduling/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"time"

grpcprometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
"github.com/pingcap/errors"
"github.com/pingcap/failpoint"
"github.com/pingcap/kvproto/pkg/diagnosticspb"
"github.com/pingcap/kvproto/pkg/pdpb"
Expand Down Expand Up @@ -119,6 +120,17 @@ func (s *Server) GetBackendEndpoints() string {
return s.cfg.BackendEndpoints
}

// SetLogLevel sets log level.
func (s *Server) SetLogLevel(level string) error {
if !logutil.IsLevelLegal(level) {
return errors.Errorf("log level %s is illegal", level)
}
s.cfg.Log.Level = level
log.SetLevel(logutil.StringToZapLogLevel(level))
log.Warn("log level changed", zap.String("level", log.GetLevel().String()))
return nil
}

// Run runs the scheduling server.
func (s *Server) Run() error {
skipWaitAPIServiceReady := false
Expand Down
18 changes: 18 additions & 0 deletions pkg/mcs/tso/server/apis/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/tikv/pd/pkg/storage/endpoint"
"github.com/tikv/pd/pkg/utils/apiutil"
"github.com/tikv/pd/pkg/utils/apiutil/multiservicesapi"
"github.com/tikv/pd/pkg/utils/logutil"
"github.com/unrolled/render"
"go.uber.org/zap"
)
Expand Down Expand Up @@ -107,6 +108,7 @@ func NewService(srv *tsoserver.Service) *Service {
func (s *Service) RegisterAdminRouter() {
router := s.root.Group("admin")
router.POST("/reset-ts", ResetTS)
router.PUT("/log", changeLogLevel)
}

// RegisterKeyspaceGroupRouter registers the router of the TSO keyspace group handler.
Expand All @@ -115,6 +117,22 @@ func (s *Service) RegisterKeyspaceGroupRouter() {
router.GET("/members", GetKeyspaceGroupMembers)
}

func changeLogLevel(c *gin.Context) {
svr := c.MustGet(multiservicesapi.ServiceContextKey).(*tsoserver.Service)
var level string
if err := c.Bind(&level); err != nil {
c.String(http.StatusBadRequest, err.Error())
return
}

if err := svr.SetLogLevel(level); err != nil {
c.String(http.StatusBadRequest, err.Error())
return
}
log.SetLevel(logutil.StringToZapLogLevel(level))
c.String(http.StatusOK, "The log level is updated.")
}

// ResetTSParams is the input json body params of ResetTS
type ResetTSParams struct {
TSO string `json:"tso"`
Expand Down
12 changes: 12 additions & 0 deletions pkg/mcs/tso/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"time"

grpcprometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
"github.com/pingcap/errors"
"github.com/pingcap/failpoint"
"github.com/pingcap/kvproto/pkg/diagnosticspb"
"github.com/pingcap/kvproto/pkg/tsopb"
Expand Down Expand Up @@ -129,6 +130,17 @@ func (s *Server) RegisterGRPCService(grpcServer *grpc.Server) {
s.service.RegisterGRPCService(grpcServer)
}

// SetLogLevel sets log level.
func (s *Server) SetLogLevel(level string) error {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will we support it in pd-ctl?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe later

if !logutil.IsLevelLegal(level) {
return errors.Errorf("log level %s is illegal", level)
}
s.cfg.Log.Level = level
log.SetLevel(logutil.StringToZapLogLevel(level))
log.Warn("log level changed", zap.String("level", log.GetLevel().String()))
return nil
}

// Run runs the TSO server.
func (s *Server) Run() error {
skipWaitAPIServiceReady := false
Expand Down
10 changes: 10 additions & 0 deletions pkg/utils/logutil/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,13 @@ func CondUint32(key string, val uint32, condition bool) zap.Field {
}
return zap.Skip()
}

// IsLevelLegal checks whether the level is legal.
func IsLevelLegal(level string) bool {
switch strings.ToLower(level) {
case "fatal", "error", "warn", "warning", "debug", "info":
return true
default:
return false
}
}
11 changes: 1 addition & 10 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -1494,7 +1494,7 @@ func (s *Server) GetClusterStatus() (*cluster.Status, error) {

// SetLogLevel sets log level.
func (s *Server) SetLogLevel(level string) error {
if !isLevelLegal(level) {
if !logutil.IsLevelLegal(level) {
return errors.Errorf("log level %s is illegal", level)
}
s.cfg.Log.Level = level
Expand All @@ -1503,15 +1503,6 @@ func (s *Server) SetLogLevel(level string) error {
return nil
}

func isLevelLegal(level string) bool {
switch strings.ToLower(level) {
case "fatal", "error", "warn", "warning", "debug", "info":
return true
default:
return false
}
}

// GetReplicationModeConfig returns the replication mode config.
func (s *Server) GetReplicationModeConfig() *config.ReplicationModeConfig {
return s.persistOptions.GetReplicationModeConfig().Clone()
Expand Down