Skip to content

Commit

Permalink
Grumble
Browse files Browse the repository at this point in the history
  • Loading branch information
breezewish committed May 27, 2021
1 parent a5915d1 commit fb7eafd
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 52 deletions.
32 changes: 28 additions & 4 deletions pkg/apiserver/debugapi/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package debugapi

import (
"fmt"
"time"

"go.uber.org/fx"

Expand All @@ -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)
Expand All @@ -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) {
Expand All @@ -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)
}
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down
13 changes: 6 additions & 7 deletions pkg/apiserver/debugapi/endpoint/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"regexp"

"github.com/joomcode/errorx"

"github.com/pingcap/tidb-dashboard/pkg/apiserver/model"
)

Expand Down Expand Up @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/apiserver/debugapi/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions pkg/pd/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
9 changes: 5 additions & 4 deletions pkg/tidb/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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")
Expand All @@ -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
}
Expand Down
12 changes: 6 additions & 6 deletions pkg/tiflash/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
12 changes: 6 additions & 6 deletions pkg/tikv/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
2 changes: 1 addition & 1 deletion ui/lib/apps/DebugAPI/apilist/ApiForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export default function ApiForm({
endpoint={endpoint}
param={endpointParam}
topology={topology}
></ApiFormItem>
/>
)

return (
Expand Down
29 changes: 14 additions & 15 deletions ui/lib/apps/DebugAPI/apilist/ApiList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ export default function Page() {
>
{descExists && (
<Alert
style={{ marginBottom: 8 }}
style={{ marginBottom: 16 }}
message={t(descTranslationKey)}
type="info"
showIcon
Expand All @@ -147,23 +147,22 @@ export default function Page() {
return (
<Root>
<ScrollablePane style={{ height: '100vh' }}>
<Card noMarginBottom>
<Alert
message={t(`debug_api.warning_header.title`)}
description={t(`debug_api.warning_header.body`)}
type="warning"
showIcon
/>
</Card>
<Sticky stickyPosition={StickyPositionType.Header} isScrollSynced>
<div style={{ display: 'flow-root' }}>
<Card>
<Space direction="vertical" size="large">
<Alert
message={t(`debug_api.warning_header.title`)}
description={t(`debug_api.warning_header.body`)}
type="warning"
showIcon
/>
<Input
style={{ maxWidth: 450 }}
placeholder={t(`debug_api.keyword_search`)}
prefix={<SearchOutlined />}
onChange={(e) => filterBy(e.target.value)}
/>
</Space>
<Input
placeholder={t(`debug_api.keyword_search`)}
prefix={<SearchOutlined />}
onChange={(e) => filterBy(e.target.value)}
/>
</Card>
</div>
</Sticky>
Expand Down
2 changes: 2 additions & 0 deletions ui/lib/apps/DebugAPI/translations/en.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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`.
4 changes: 4 additions & 0 deletions ui/lib/apps/DebugAPI/translations/zh.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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 生效

0 comments on commit fb7eafd

Please sign in to comment.