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

server: add http api to get some info of sub-optimal query (#10717) #10925

Merged
merged 1 commit into from
Jun 24, 2019
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
42 changes: 20 additions & 22 deletions server/http_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,22 @@ func (s *Server) startStatusHTTP() {
go s.startHTTPServer()
}

func serveError(w http.ResponseWriter, status int, txt string) {
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.Header().Set("X-Go-Pprof", "1")
w.Header().Del("Content-Disposition")
w.WriteHeader(status)
_, err := fmt.Fprintln(w, txt)
terror.Log(err)
}

func sleepWithCtx(ctx context.Context, d time.Duration) {
select {
case <-time.After(d):
case <-ctx.Done():
}
}

func (s *Server) startHTTPServer() {
router := mux.NewRouter()

Expand Down Expand Up @@ -123,26 +139,6 @@ func (s *Server) startHTTPServer() {
serverMux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
serverMux.HandleFunc("/debug/pprof/trace", pprof.Trace)

serveError := func(w http.ResponseWriter, status int, txt string) {
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.Header().Set("X-Go-Pprof", "1")
w.Header().Del("Content-Disposition")
w.WriteHeader(status)
_, err := fmt.Fprintln(w, txt)
terror.Log(err)
}

sleep := func(w http.ResponseWriter, d time.Duration) {
var clientGone <-chan bool
if cn, ok := w.(http.CloseNotifier); ok {
clientGone = cn.CloseNotify()
}
select {
case <-time.After(d):
case <-clientGone:
}
}

serverMux.HandleFunc("/debug/zip", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Disposition", fmt.Sprintf(`attachment; filename="tidb_debug"`+time.Now().Format("20060102150405")+".zip"))

Expand Down Expand Up @@ -191,7 +187,7 @@ func (s *Server) startHTTPServer() {
if sec <= 0 || err != nil {
sec = 10
}
sleep(w, time.Duration(sec)*time.Second)
sleepWithCtx(r.Context(), time.Duration(sec)*time.Second)
rpprof.StopCPUProfile()

// dump config
Expand Down Expand Up @@ -220,11 +216,13 @@ func (s *Server) startHTTPServer() {
err = zw.Close()
terror.Log(err)
})
fetcher := sqlInfoFetcher{store: tikvHandlerTool.Store}
serverMux.HandleFunc("/debug/sub-optimal-plan", fetcher.zipInfoForSQL)

var (
err error
httpRouterPage bytes.Buffer
pathTemplate string
err error
)
httpRouterPage.WriteString("<html><head><title>TiDB Status and Metrics Report</title></head><body><h1>TiDB Status and Metrics Report</h1><table>")
err = router.Walk(func(route *mux.Route, router *mux.Router, ancestors []*mux.Route) error {
Expand Down
Loading