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

Refine profiling #874

Closed
wants to merge 11 commits into from
1 change: 0 additions & 1 deletion pkg/apiserver/apiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ func (s *Service) Start(ctx context.Context) error {
user.RegisterRouter,
info.RegisterRouter,
clusterinfo.RegisterRouter,
profiling.RegisterRouter,
logsearch.RegisterRouter,
slowquery.RegisterRouter,
statement.RegisterRouter,
Expand Down
115 changes: 115 additions & 0 deletions pkg/apiserver/profiling/clientmap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
// Copyright 2021 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.

package profiling

import (
"fmt"
"net/http"
"time"

"github.com/pingcap/tidb-dashboard/pkg/apiserver/model"
"github.com/pingcap/tidb-dashboard/pkg/apiserver/profiling/fetcher"
"github.com/pingcap/tidb-dashboard/pkg/config"
"github.com/pingcap/tidb-dashboard/pkg/pd"
"github.com/pingcap/tidb-dashboard/pkg/tidb"
"github.com/pingcap/tidb-dashboard/pkg/tiflash"
"github.com/pingcap/tidb-dashboard/pkg/tikv"
)

const (
maxProfilingTimeout = time.Minute * 5
)

type clientMap map[model.NodeKind]fetcher.Client

func (fm *clientMap) Get(kind model.NodeKind) (fetcher.Client, error) {
f, ok := (*fm)[kind]
if !ok {
return nil, fmt.Errorf("unsupported target %s", kind)
}
return f, nil
}

func newClientMap(
tikvHTTPClient *tikv.Client,
tiflashHTTPClient *tiflash.Client,
tidbHTTPClient *tidb.Client,
pdHTTPClient *pd.Client,
config *config.Config,
) *clientMap {
return &clientMap{
model.NodeKindTiKV: &tikvClient{
client: tikvHTTPClient,
},
model.NodeKindTiFlash: &tiflashClient{
client: tiflashHTTPClient,
},
model.NodeKindTiDB: &tidbClient{
client: tidbHTTPClient,
},
model.NodeKindPD: &pdClient{
client: pdHTTPClient,
statusAPIHTTPScheme: config.GetClusterHTTPScheme(),
},
}
}

// tikv
var _ fetcher.Client = (*tikvClient)(nil)

type tikvClient struct {
client *tikv.Client
}

func (f *tikvClient) Fetch(op *fetcher.ClientFetchOptions) ([]byte, error) {
return f.client.WithTimeout(maxProfilingTimeout).SendGetRequest(op.IP, op.Port, op.Path)
}

// tiflash
var _ fetcher.Client = (*tiflashClient)(nil)

type tiflashClient struct {
client *tiflash.Client
}

func (f *tiflashClient) Fetch(op *fetcher.ClientFetchOptions) ([]byte, error) {
return f.client.WithTimeout(maxProfilingTimeout).SendGetRequest(op.IP, op.Port, op.Path)
}

// tidb
var _ fetcher.Client = (*tidbClient)(nil)

type tidbClient struct {
client *tidb.Client
}

func (f *tidbClient) Fetch(op *fetcher.ClientFetchOptions) ([]byte, error) {
return f.client.WithStatusAPIAddress(op.IP, op.Port).WithStatusAPITimeout(maxProfilingTimeout).SendGetRequest(op.Path)
}

// pd
var _ fetcher.Client = (*pdClient)(nil)

type pdClient struct {
client *pd.Client
statusAPIHTTPScheme string
}

func (f *pdClient) Fetch(op *fetcher.ClientFetchOptions) ([]byte, error) {
baseURL := fmt.Sprintf("%s://%s:%d", f.statusAPIHTTPScheme, op.IP, op.Port)
f.client.WithBeforeRequest(func(req *http.Request) {
req.Header.Add("PD-Allow-follower-handle", "true")
})
return f.client.WithTimeout(maxProfilingTimeout).WithBaseURL(baseURL).SendGetRequest(op.Path)
}
110 changes: 0 additions & 110 deletions pkg/apiserver/profiling/fetcher.go

This file was deleted.

24 changes: 24 additions & 0 deletions pkg/apiserver/profiling/fetcher/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright 2021 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.

package fetcher

type ClientFetchOptions struct {
IP string
Port int
Path string
}

type Client interface {
Fetch(op *ClientFetchOptions) ([]byte, error)
}
59 changes: 59 additions & 0 deletions pkg/apiserver/profiling/fetcher/fetcher.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright 2021 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.

package fetcher

import (
"time"

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

type ProfileFetchOptions struct {
Duration time.Duration
}

type ProfilerFetcher interface {
Fetch(op *ProfileFetchOptions) ([]byte, error)
}

type Fetcher interface {
Fetch(client Client, target *model.RequestTargetNode, op *ProfileFetchOptions) ([]byte, error)
}

type profilerFetcher struct {
fetcher Fetcher
client Client
target *model.RequestTargetNode
}

func (p *profilerFetcher) Fetch(op *ProfileFetchOptions) ([]byte, error) {
return p.fetcher.Fetch(p.client, p.target, op)
}

type Factory struct {
client Client
target *model.RequestTargetNode
}

func (ff *Factory) Create(fetcher Fetcher) ProfilerFetcher {
return &profilerFetcher{
fetcher: fetcher,
client: ff.client,
target: ff.target,
}
}

func NewFetcherFactory(client Client, target *model.RequestTargetNode) *Factory {
return &Factory{client: client, target: target}
}
29 changes: 29 additions & 0 deletions pkg/apiserver/profiling/fetcher/flamegraph.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2021 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.

package fetcher

import (
"fmt"

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

var _ Fetcher = (*FlameGraph)(nil)

type FlameGraph struct{}

func (f *FlameGraph) Fetch(client Client, target *model.RequestTargetNode, op *ProfileFetchOptions) ([]byte, error) {
path := fmt.Sprintf("/debug/pprof/profile?seconds=%d", op.Duration)
return client.Fetch(&ClientFetchOptions{IP: target.IP, Port: target.Port, Path: path})
}
Loading