From fb7eafde829457d96271e0e1767620a5f96fa296 Mon Sep 17 00:00:00 2001 From: Breezewish Date: Fri, 28 May 2021 01:58:16 +0800 Subject: [PATCH] Grumble --- pkg/apiserver/debugapi/client.go | 32 ++++++++++++++++++--- pkg/apiserver/debugapi/endpoint/endpoint.go | 13 ++++----- pkg/apiserver/debugapi/service.go | 6 ++-- pkg/pd/client.go | 12 ++++---- pkg/tidb/client.go | 9 +++--- pkg/tiflash/client.go | 12 ++++---- pkg/tikv/client.go | 12 ++++---- ui/lib/apps/DebugAPI/apilist/ApiForm.tsx | 2 +- ui/lib/apps/DebugAPI/apilist/ApiList.tsx | 29 +++++++++---------- ui/lib/apps/DebugAPI/translations/en.yaml | 2 ++ ui/lib/apps/DebugAPI/translations/zh.yaml | 4 +++ 11 files changed, 81 insertions(+), 52 deletions(-) diff --git a/pkg/apiserver/debugapi/client.go b/pkg/apiserver/debugapi/client.go index 644c99ed24..4a1514a86d 100644 --- a/pkg/apiserver/debugapi/client.go +++ b/pkg/apiserver/debugapi/client.go @@ -15,6 +15,7 @@ package debugapi import ( "fmt" + "time" "go.uber.org/fx" @@ -27,6 +28,10 @@ import ( "github.com/pingcap/tidb-dashboard/pkg/tikv" ) +const ( + defaultTimeout = time.Second * 45 // Default profiling can be as long as 30s. +) + type Client interface { Send(request *endpoint.Request) (*httpc.Response, error) Get(request *endpoint.Request) (*httpc.Response, error) @@ -53,13 +58,24 @@ func defaultSendRequest(client Client, req *endpoint.Request) (*httpc.Response, } } +func buildRelativeUri(path string, query string) string { + if len(query) == 0 { + return path + } else { + return fmt.Sprintf("%s?%s", path, query) + } +} + type tidbImplement struct { fx.In Client *tidb.Client } func (impl *tidbImplement) Get(req *endpoint.Request) (*httpc.Response, error) { - return impl.Client.WithEnforcedStatusAPIAddress(req.Host, req.Port).Get(req.Path) + return impl.Client. + WithEnforcedStatusAPIAddress(req.Host, req.Port). + WithStatusAPITimeout(defaultTimeout). + Get(buildRelativeUri(req.Path, req.Query)) } func (impl *tidbImplement) Send(req *endpoint.Request) (*httpc.Response, error) { @@ -72,9 +88,12 @@ type tikvImplement struct { } func (impl *tikvImplement) Get(req *endpoint.Request) (*httpc.Response, error) { - return impl.Client.Get(req.Host, req.Port, req.Path) + return impl.Client. + WithTimeout(defaultTimeout). + Get(req.Host, req.Port, buildRelativeUri(req.Path, req.Query)) } +// FIXME: Deduplicate default implementation. func (impl *tikvImplement) Send(req *endpoint.Request) (*httpc.Response, error) { return defaultSendRequest(impl, req) } @@ -85,7 +104,9 @@ type tiflashImplement struct { } func (impl *tiflashImplement) Get(req *endpoint.Request) (*httpc.Response, error) { - return impl.Client.Get(req.Host, req.Port, req.Path) + return impl.Client. + WithTimeout(defaultTimeout). + Get(req.Host, req.Port, buildRelativeUri(req.Path, req.Query)) } func (impl *tiflashImplement) Send(req *endpoint.Request) (*httpc.Response, error) { @@ -98,7 +119,10 @@ type pdImplement struct { } func (impl *pdImplement) Get(req *endpoint.Request) (*httpc.Response, error) { - return impl.Client.WithAddress(req.Host, req.Port).Get(req.Path) + return impl.Client. + WithAddress(req.Host, req.Port). + WithTimeout(defaultTimeout). + Get(buildRelativeUri(req.Path, req.Query)) } func (impl *pdImplement) Send(req *endpoint.Request) (*httpc.Response, error) { diff --git a/pkg/apiserver/debugapi/endpoint/endpoint.go b/pkg/apiserver/debugapi/endpoint/endpoint.go index 185462d5c9..651c43a8fb 100644 --- a/pkg/apiserver/debugapi/endpoint/endpoint.go +++ b/pkg/apiserver/debugapi/endpoint/endpoint.go @@ -19,7 +19,6 @@ import ( "regexp" "github.com/joomcode/errorx" - "github.com/pingcap/tidb-dashboard/pkg/apiserver/model" ) @@ -70,28 +69,28 @@ const ( MethodGet Method = http.MethodGet ) -func (e *APIModel) NewRequest(host string, port int, data map[string]string) (*Request, error) { +func (m *APIModel) NewRequest(host string, port int, data map[string]string) (*Request, error) { req := &Request{ - Method: e.Method, + Method: m.Method, Host: host, Port: port, } - pathValues, err := transformValues(e.PathParams, data, true) + pathValues, err := transformValues(m.PathParams, data, true) if err != nil { return nil, err } - path, err := populatePath(e.Path, pathValues) + path, err := populatePath(m.Path, pathValues) if err != nil { return nil, err } req.Path = path - queryValues, err := transformValues(e.QueryParams, data, false) + queryValues, err := transformValues(m.QueryParams, data, false) if err != nil { return nil, err } - query, err := encodeQuery(e.QueryParams, queryValues) + query, err := encodeQuery(m.QueryParams, queryValues) if err != nil { return nil, err } diff --git a/pkg/apiserver/debugapi/service.go b/pkg/apiserver/debugapi/service.go index e52535046e..d72bc113c1 100644 --- a/pkg/apiserver/debugapi/service.go +++ b/pkg/apiserver/debugapi/service.go @@ -84,18 +84,18 @@ func (s *Service) RequestEndpoint(c *gin.Context) { return } - endpoint, ok := s.endpointMap[req.ID] + ep, ok := s.endpointMap[req.ID] if !ok { _ = c.Error(ErrEndpointConfig.New("invalid endpoint id: %s", req.ID)) return } - endpointReq, err := endpoint.NewRequest(req.Host, req.Port, req.Params) + endpointReq, err := ep.NewRequest(req.Host, req.Port, req.Params) if err != nil { _ = c.Error(err) return } - res, err := endpoint.Client.Send(endpointReq) + res, err := ep.Client.Send(endpointReq) if err != nil { _ = c.Error(err) return diff --git a/pkg/pd/client.go b/pkg/pd/client.go index 59b621f1cc..31fb03130b 100644 --- a/pkg/pd/client.go +++ b/pkg/pd/client.go @@ -81,20 +81,20 @@ func (c Client) WithBeforeRequest(callback func(req *http.Request)) *Client { return &c } -func (c *Client) Get(path string) (*httpc.Response, error) { - uri := fmt.Sprintf("%s/pd/api/v1%s", c.baseURL, path) +func (c *Client) Get(relativeUri string) (*httpc.Response, error) { + uri := fmt.Sprintf("%s/pd/api/v1%s", c.baseURL, relativeUri) return c.httpClient.WithTimeout(c.timeout).Send(c.lifecycleCtx, uri, http.MethodGet, nil, ErrPDClientRequestFailed, "PD") } -func (c *Client) SendGetRequest(path string) ([]byte, error) { - res, err := c.Get(path) +func (c *Client) SendGetRequest(relativeUri string) ([]byte, error) { + res, err := c.Get(relativeUri) if err != nil { return nil, err } return res.Body() } -func (c *Client) SendPostRequest(path string, body io.Reader) ([]byte, error) { - uri := fmt.Sprintf("%s/pd/api/v1%s", c.baseURL, path) +func (c *Client) SendPostRequest(relativeUri string, body io.Reader) ([]byte, error) { + uri := fmt.Sprintf("%s/pd/api/v1%s", c.baseURL, relativeUri) return c.httpClient.WithTimeout(c.timeout).SendRequest(c.lifecycleCtx, uri, http.MethodPost, body, ErrPDClientRequestFailed, "PD") } diff --git a/pkg/tidb/client.go b/pkg/tidb/client.go index b1ae81af56..ad762d8135 100644 --- a/pkg/tidb/client.go +++ b/pkg/tidb/client.go @@ -159,7 +159,7 @@ func (c *Client) OpenSQLConn(user string, pass string) (*gorm.DB, error) { return db, nil } -func (c *Client) Get(path string) (*httpc.Response, error) { +func (c *Client) Get(relativeUri string) (*httpc.Response, error) { var err error overrideEndpoint := os.Getenv(tidbOverrideStatusEndpointEnvVar) @@ -184,7 +184,7 @@ func (c *Client) Get(path string) (*httpc.Response, error) { } } - uri := fmt.Sprintf("%s://%s%s", c.statusAPIHTTPScheme, addr, path) + uri := fmt.Sprintf("%s://%s%s", c.statusAPIHTTPScheme, addr, relativeUri) res, err := c.statusAPIHTTPClient. WithTimeout(c.statusAPITimeout). Send(c.lifecycleCtx, uri, http.MethodGet, nil, ErrTiDBClientRequestFailed, "TiDB") @@ -194,8 +194,9 @@ func (c *Client) Get(path string) (*httpc.Response, error) { return res, err } -func (c *Client) SendGetRequest(path string) ([]byte, error) { - res, err := c.Get(path) +// FIXME: SendGetRequest should be extracted, as a common method. +func (c *Client) SendGetRequest(relativeUri string) ([]byte, error) { + res, err := c.Get(relativeUri) if err != nil { return nil, err } diff --git a/pkg/tiflash/client.go b/pkg/tiflash/client.go index 99b8df6b34..e888931e68 100644 --- a/pkg/tiflash/client.go +++ b/pkg/tiflash/client.go @@ -64,20 +64,20 @@ func (c Client) WithTimeout(timeout time.Duration) *Client { return &c } -func (c *Client) Get(host string, statusPort int, path string) (*httpc.Response, error) { - uri := fmt.Sprintf("%s://%s:%d%s", c.httpScheme, host, statusPort, path) +func (c *Client) Get(host string, statusPort int, relativeUri string) (*httpc.Response, error) { + uri := fmt.Sprintf("%s://%s:%d%s", c.httpScheme, host, statusPort, relativeUri) return c.httpClient.WithTimeout(c.timeout).Send(c.lifecycleCtx, uri, http.MethodGet, nil, ErrFlashClientRequestFailed, "TiFlash") } -func (c *Client) SendGetRequest(host string, statusPort int, path string) ([]byte, error) { - res, err := c.Get(host, statusPort, path) +func (c *Client) SendGetRequest(host string, statusPort int, relativeUri string) ([]byte, error) { + res, err := c.Get(host, statusPort, relativeUri) if err != nil { return nil, err } return res.Body() } -func (c *Client) SendPostRequest(host string, statusPort int, path string, body io.Reader) ([]byte, error) { - uri := fmt.Sprintf("%s://%s:%d%s", c.httpScheme, host, statusPort, path) +func (c *Client) SendPostRequest(host string, statusPort int, relativeUri string, body io.Reader) ([]byte, error) { + uri := fmt.Sprintf("%s://%s:%d%s", c.httpScheme, host, statusPort, relativeUri) return c.httpClient.WithTimeout(c.timeout).SendRequest(c.lifecycleCtx, uri, http.MethodPost, body, ErrFlashClientRequestFailed, "TiFlash") } diff --git a/pkg/tikv/client.go b/pkg/tikv/client.go index 0aa7158449..b8d2808793 100644 --- a/pkg/tikv/client.go +++ b/pkg/tikv/client.go @@ -64,20 +64,20 @@ func (c Client) WithTimeout(timeout time.Duration) *Client { return &c } -func (c *Client) Get(host string, statusPort int, path string) (*httpc.Response, error) { - uri := fmt.Sprintf("%s://%s:%d%s", c.httpScheme, host, statusPort, path) +func (c *Client) Get(host string, statusPort int, relativeUri string) (*httpc.Response, error) { + uri := fmt.Sprintf("%s://%s:%d%s", c.httpScheme, host, statusPort, relativeUri) return c.httpClient.WithTimeout(c.timeout).Send(c.lifecycleCtx, uri, http.MethodGet, nil, ErrTiKVClientRequestFailed, "TiKV") } -func (c *Client) SendGetRequest(host string, statusPort int, path string) ([]byte, error) { - res, err := c.Get(host, statusPort, path) +func (c *Client) SendGetRequest(host string, statusPort int, relativeUri string) ([]byte, error) { + res, err := c.Get(host, statusPort, relativeUri) if err != nil { return nil, err } return res.Body() } -func (c *Client) SendPostRequest(host string, statusPort int, path string, body io.Reader) ([]byte, error) { - uri := fmt.Sprintf("%s://%s:%d%s", c.httpScheme, host, statusPort, path) +func (c *Client) SendPostRequest(host string, statusPort int, relativeUri string, body io.Reader) ([]byte, error) { + uri := fmt.Sprintf("%s://%s:%d%s", c.httpScheme, host, statusPort, relativeUri) return c.httpClient.WithTimeout(c.timeout).SendRequest(c.lifecycleCtx, uri, http.MethodPost, body, ErrTiKVClientRequestFailed, "TiKV") } diff --git a/ui/lib/apps/DebugAPI/apilist/ApiForm.tsx b/ui/lib/apps/DebugAPI/apilist/ApiForm.tsx index 50352410e0..d9f392897f 100644 --- a/ui/lib/apps/DebugAPI/apilist/ApiForm.tsx +++ b/ui/lib/apps/DebugAPI/apilist/ApiForm.tsx @@ -99,7 +99,7 @@ export default function ApiForm({ endpoint={endpoint} param={endpointParam} topology={topology} - > + /> ) return ( diff --git a/ui/lib/apps/DebugAPI/apilist/ApiList.tsx b/ui/lib/apps/DebugAPI/apilist/ApiList.tsx index ac7d8eefb9..ea81311b19 100644 --- a/ui/lib/apps/DebugAPI/apilist/ApiList.tsx +++ b/ui/lib/apps/DebugAPI/apilist/ApiList.tsx @@ -129,7 +129,7 @@ export default function Page() { > {descExists && ( + + +
- - - } - onChange={(e) => filterBy(e.target.value)} - /> - + } + onChange={(e) => filterBy(e.target.value)} + />
diff --git a/ui/lib/apps/DebugAPI/translations/en.yaml b/ui/lib/apps/DebugAPI/translations/en.yaml index 4dd9ff8927..04a92b1f66 100644 --- a/ui/lib/apps/DebugAPI/translations/en.yaml +++ b/ui/lib/apps/DebugAPI/translations/en.yaml @@ -36,6 +36,7 @@ debug_api: tidb_table_regions: Region - by Database + Table tidb_hot_regions: Hot Regions tidb_pprof: TiDB pprof + tidb_pprof_desc: The `seconds` parameter is only effective to `kind=profile` and `kind=trace`. pd: name: PD endpoints: @@ -67,3 +68,4 @@ debug_api: pd_stores: Stores - All (pd-ctl store) pd_store_id: Store - by StoreID (pd-ctl store [id]) pd_pprof: PD pprof + pd_pprof_desc: The `seconds` parameter is only effective to `kind=profile` and `kind=trace`. diff --git a/ui/lib/apps/DebugAPI/translations/zh.yaml b/ui/lib/apps/DebugAPI/translations/zh.yaml index ac60bc5e52..fc5b37425b 100644 --- a/ui/lib/apps/DebugAPI/translations/zh.yaml +++ b/ui/lib/apps/DebugAPI/translations/zh.yaml @@ -20,3 +20,7 @@ debug_api: tidb: endpoints: tidb_stats_dump_timestamp_desc: 时间戳应当在 GC Safe Point 以后 + tidb_pprof_desc: seconds 参数仅对 kind=profile 和 kind=trace 生效 + pd: + endpoints: + pd_pprof_desc: seconds 参数仅对 kind=profile 和 kind=trace 生效