Skip to content
This repository has been archived by the owner on Dec 8, 2021. It is now read-only.

lightning: support dynamically modifying the log level #393

Merged
merged 2 commits into from
Sep 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ github.com/cznic/sortutil v0.0.0-20181122101858-f5f958428db8 h1:LpMLYGyy67BoAFGd
github.com/cznic/sortutil v0.0.0-20181122101858-f5f958428db8/go.mod h1:q2w6Bg5jeox1B+QkJ6Wp/+Vn0G/bo3f1uY7Fn3vivIQ=
github.com/cznic/strutil v0.0.0-20171016134553-529a34b1c186/go.mod h1:AHHPPPXTw0h6pVabbcbyGRK1DckRn7r/STdZEeIDzZc=
github.com/cznic/y v0.0.0-20170802143616-045f81c6662a/go.mod h1:1rk5VM7oSnA4vjp+hrLQ3HWHa+Y4yPCa3/CsJrcNnvs=
github.com/danjacques/gofslock v0.0.0-20191023191349-0a45f885bc37 h1:X6mKGhCFOxrKeeHAjv/3UvT6e5RRxW6wRdlqlV6/H4w=
github.com/danjacques/gofslock v0.0.0-20191023191349-0a45f885bc37/go.mod h1:DC3JtzuG7kxMvJ6dZmf2ymjNyoXwgtklr7FN+Um2B0U=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
Expand Down
32 changes: 32 additions & 0 deletions lightning/lightning.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/shurcooL/httpgzip"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"golang.org/x/net/http/httpproxy"

"github.com/pingcap/tidb-lightning/lightning/backend"
Expand Down Expand Up @@ -108,6 +109,7 @@ func (l *Lightning) GoServe() error {
mux.HandleFunc("/progress/table", handleProgressTable)
mux.HandleFunc("/pause", handlePause)
mux.HandleFunc("/resume", handleResume)
mux.HandleFunc("/loglevel", handleLogLevel)

mux.Handle("/web/", http.StripPrefix("/web", httpgzip.FileServer(web.Res, httpgzip.FileServerOptions{
IndexHTML: true,
Expand Down Expand Up @@ -554,6 +556,36 @@ func handleResume(w http.ResponseWriter, req *http.Request) {
}
}

func handleLogLevel(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "application/json")

var logLevel struct {
Level zapcore.Level `json:"level"`
}

switch req.Method {
case http.MethodGet:
logLevel.Level = log.Level()
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(logLevel)

case http.MethodPut, http.MethodPost:
if err := json.NewDecoder(req.Body).Decode(&logLevel); err != nil {
writeJSONError(w, http.StatusBadRequest, "invalid log level", err)
return
}
oldLevel := log.SetLevel(zapcore.InfoLevel)
log.L().Info("changed log level", zap.Stringer("old", oldLevel), zap.Stringer("new", logLevel.Level))
log.SetLevel(logLevel.Level)
w.WriteHeader(http.StatusOK)
w.Write([]byte("{}"))

default:
w.Header().Set("Allow", http.MethodGet+", "+http.MethodPut+", "+http.MethodPost)
writeJSONError(w, http.StatusMethodNotAllowed, "only GET, PUT and POST are allowed", nil)
}
}

func checkSystemRequirement(cfg *config.Config, dbsMeta []*mydump.MDDatabaseMeta) error {
// in local mode, we need to read&write a lot of L0 sst files, so we need to check system max open files limit
if cfg.TikvImporter.Backend == config.BackendLocal {
Expand Down
5 changes: 5 additions & 0 deletions lightning/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ func L() Logger {
return appLogger
}

// Level returns the current global log level.
func Level() zapcore.Level {
return appLevel.Level()
}

// SetLevel modifies the log level of the global logger. Returns the previous
// level.
func SetLevel(level zapcore.Level) zapcore.Level {
Expand Down
56 changes: 55 additions & 1 deletion web/docs/api.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
openapi: 3.0.2
info:
title: TiDB Lightning web interface
version: 3.0.0
version: 4.0.6
servers:
- url: http://127.0.0.1:8289/
tags:
Expand All @@ -11,6 +11,8 @@ tags:
description: Task progress
- name: Pause
description: Pause/resume tasks
- name: Log
description: Logging
components:
schemas:
Error:
Expand Down Expand Up @@ -236,6 +238,23 @@ components:
properties:
paused:
type: boolean
LogLevel:
type: object
required:
- level
additionalProperties: false
properties:
level:
type: string
description: Log level
enum:
- debug
- info
- warn
- error
- dpanic
- panic
- fatal
parameters:
TaskId:
name: taskId
Expand All @@ -255,6 +274,13 @@ components:
example: |
[mydumper]
data-source-dir = '/data/db1'
LogLevel:
description: Log level
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/LogLevel'
responses:
serverModeDisabled:
description: Server mode disabled
Expand Down Expand Up @@ -465,3 +491,31 @@ paths:
responses:
200:
description: The program is resumed
/loglevel:
get:
summary: Get the current log level
operationId: GetLogLevel
tags: [Log]
responses:
200:
description: Current log level
content:
application/json:
schema:
$ref: '#/components/schemas/LogLevel'
put:
summary: Change the current log level
operationId: PutLogLevel
tags: [Log]
requestBody:
$ref: '#/components/requestBodies/LogLevel'
responses:
200:
description: Log level is updated
400:
description: Invalid log level
content:
application/json:
schema:
$ref: '#/components/schemas/Error'