Skip to content

Commit

Permalink
*: add backend supports for tiproxy & ticdc
Browse files Browse the repository at this point in the history
Signed-off-by: mornyx <[email protected]>
  • Loading branch information
mornyx committed Dec 25, 2023
1 parent 1f39ee0 commit a97a763
Show file tree
Hide file tree
Showing 18 changed files with 464 additions and 3 deletions.
11 changes: 10 additions & 1 deletion pkg/apiserver/apiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,17 @@ import (
"github.com/pingcap/tidb-dashboard/pkg/apiserver/user/sso"
"github.com/pingcap/tidb-dashboard/pkg/apiserver/user/sso/ssoauth"
"github.com/pingcap/tidb-dashboard/pkg/apiserver/visualplan"
"github.com/pingcap/tidb-dashboard/pkg/ticdc"
"github.com/pingcap/tidb-dashboard/pkg/tiflash"
"github.com/pingcap/tidb-dashboard/pkg/tiproxy"
"github.com/pingcap/tidb-dashboard/pkg/utils/version"
"github.com/pingcap/tidb-dashboard/util/client/httpclient"
"github.com/pingcap/tidb-dashboard/util/client/pdclient"
"github.com/pingcap/tidb-dashboard/util/client/ticdcclient"
"github.com/pingcap/tidb-dashboard/util/client/tidbclient"
"github.com/pingcap/tidb-dashboard/util/client/tiflashclient"
"github.com/pingcap/tidb-dashboard/util/client/tikvclient"
"github.com/pingcap/tidb-dashboard/util/client/tiproxyclient"
"github.com/pingcap/tidb-dashboard/util/featureflag"
"github.com/pingcap/tidb-dashboard/util/rest"

Expand Down Expand Up @@ -113,6 +117,8 @@ var Modules = fx.Options(
tidb.NewTiDBClient,
tikv.NewTiKVClient,
tiflash.NewTiFlashClient,
ticdc.NewTiCDCClient,
tiproxy.NewTiProxyClient,
utils.ProvideSysSchema,
apiutils.NewNgmProxy,
info.NewService,
Expand Down Expand Up @@ -191,6 +197,8 @@ func newClients(lc fx.Lifecycle, config *config.Config) (
kvClient *tikvclient.StatusClient,
csClient *tiflashclient.StatusClient,
pdClient *pdclient.APIClient,
ticdcClient *ticdcclient.StatusClient,
tiproxyClient *tiproxyclient.StatusClient,
) {
httpConfig := httpclient.Config{
TLSConfig: config.ClusterTLSConfig,
Expand All @@ -199,7 +207,8 @@ func newClients(lc fx.Lifecycle, config *config.Config) (
kvClient = tikvclient.NewStatusClient(httpConfig)
csClient = tiflashclient.NewStatusClient(httpConfig)
pdClient = pdclient.NewAPIClient(httpConfig)
// cdcClient = ticdcclient.NewStatusClient(httpConfig)
ticdcClient = ticdcclient.NewStatusClient(httpConfig)
tiproxyClient = tiproxyclient.NewStatusClient(httpConfig)
lc.Append(fx.Hook{
OnStart: func(ctx context.Context) error {
dbClient.SetDefaultCtx(ctx)
Expand Down
12 changes: 10 additions & 2 deletions pkg/apiserver/clusterinfo/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,19 @@ func (s *Service) fetchAllInstanceHosts() ([]string, error) {
allHostsMap[i.IP] = struct{}{}
}

ticdcIndo, err := topology.FetchTiCDCTopology(s.lifecycleCtx, s.params.EtcdClient)
ticdcInfo, err := topology.FetchTiCDCTopology(s.lifecycleCtx, s.params.EtcdClient)

Check warning on line 47 in pkg/apiserver/clusterinfo/host.go

View check run for this annotation

Codecov / codecov/patch

pkg/apiserver/clusterinfo/host.go#L47

Added line #L47 was not covered by tests
if err != nil {
return nil, err
}
for _, i := range ticdcIndo {
for _, i := range ticdcInfo {
allHostsMap[i.IP] = struct{}{}
}

Check warning on line 53 in pkg/apiserver/clusterinfo/host.go

View check run for this annotation

Codecov / codecov/patch

pkg/apiserver/clusterinfo/host.go#L51-L53

Added lines #L51 - L53 were not covered by tests

tiproxyInfo, err := topology.FetchTiProxyTopology(s.lifecycleCtx, s.params.EtcdClient)
if err != nil {
return nil, err
}
for _, i := range tiproxyInfo {

Check warning on line 59 in pkg/apiserver/clusterinfo/host.go

View check run for this annotation

Codecov / codecov/patch

pkg/apiserver/clusterinfo/host.go#L55-L59

Added lines #L55 - L59 were not covered by tests
allHostsMap[i.IP] = struct{}{}
}

Expand Down
16 changes: 16 additions & 0 deletions pkg/apiserver/clusterinfo/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func RegisterRouter(r *gin.RouterGroup, auth *user.AuthService, s *Service) {
endpoint.Use(auth.MWAuthRequired())
endpoint.GET("/tidb", s.getTiDBTopology)
endpoint.GET("/ticdc", s.getTiCDCTopology)
endpoint.GET("/tiproxy", s.getTiProxyTopology)

Check warning on line 58 in pkg/apiserver/clusterinfo/service.go

View check run for this annotation

Codecov / codecov/patch

pkg/apiserver/clusterinfo/service.go#L58

Added line #L58 was not covered by tests
endpoint.DELETE("/tidb/:address", s.deleteTiDBTopology)
endpoint.GET("/store", s.getStoreTopology)
endpoint.GET("/pd", s.getPDTopology)
Expand Down Expand Up @@ -140,6 +141,21 @@ func (s *Service) getTiCDCTopology(c *gin.Context) {
c.JSON(http.StatusOK, instances)
}

// @ID getTiProxyTopology
// @Summary Get all TiProxy instances
// @Success 200 {array} topology.TiProxyInfo
// @Router /topology/tiproxy [get]
// @Security JwtAuth
// @Failure 401 {object} rest.ErrorResponse
func (s *Service) getTiProxyTopology(c *gin.Context) {
instances, err := topology.FetchTiProxyTopology(s.lifecycleCtx, s.params.EtcdClient)
if err != nil {
rest.Error(c, err)
return
}
c.JSON(http.StatusOK, instances)

Check warning on line 156 in pkg/apiserver/clusterinfo/service.go

View check run for this annotation

Codecov / codecov/patch

pkg/apiserver/clusterinfo/service.go#L150-L156

Added lines #L150 - L156 were not covered by tests
}

type StoreTopologyResponse struct {
TiKV []topology.StoreInfo `json:"tikv"`
TiFlash []topology.StoreInfo `json:"tiflash"`
Expand Down
18 changes: 18 additions & 0 deletions pkg/apiserver/clusterinfo/statistics.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ func (s *Service) calculateStatistics(db *gorm.DB) (*ClusterStatistics, error) {
infoByIk["tikv"] = newInstanceKindImmediateInfo()
infoByIk["tiflash"] = newInstanceKindImmediateInfo()
infoByIk["ticdc"] = newInstanceKindImmediateInfo()
infoByIk["tiproxy"] = newInstanceKindImmediateInfo()

Check warning on line 79 in pkg/apiserver/clusterinfo/statistics.go

View check run for this annotation

Codecov / codecov/patch

pkg/apiserver/clusterinfo/statistics.go#L79

Added line #L79 was not covered by tests

// Fill from topology info
pdInfo, err := topology.FetchPDTopology(s.params.PDClient)
Expand Down Expand Up @@ -124,6 +125,16 @@ func (s *Service) calculateStatistics(db *gorm.DB) (*ClusterStatistics, error) {
globalInfo.instances[net.JoinHostPort(i.IP, strconv.Itoa(int(i.Port)))] = struct{}{}
infoByIk["ticdc"].instances[net.JoinHostPort(i.IP, strconv.Itoa(int(i.Port)))] = struct{}{}
}
tiproxyInfo, err := topology.FetchTiProxyTopology(s.lifecycleCtx, s.params.EtcdClient)
if err != nil {
return nil, err
}
for _, i := range tiproxyInfo {
globalHostsSet[i.IP] = struct{}{}
globalVersionsSet[i.Version] = struct{}{}
globalInfo.instances[net.JoinHostPort(i.IP, strconv.Itoa(int(i.Port)))] = struct{}{}
infoByIk["tiproxy"].instances[net.JoinHostPort(i.IP, strconv.Itoa(int(i.Port)))] = struct{}{}
}

Check warning on line 137 in pkg/apiserver/clusterinfo/statistics.go

View check run for this annotation

Codecov / codecov/patch

pkg/apiserver/clusterinfo/statistics.go#L128-L137

Added lines #L128 - L137 were not covered by tests

// Fill from hardware info
allHostsInfoMap := make(map[string]*hostinfo.Info)
Expand Down Expand Up @@ -180,6 +191,13 @@ func (s *Service) calculateStatistics(db *gorm.DB) (*ClusterStatistics, error) {
globalFailureHostsSet[i.IP] = struct{}{}
}
}
for _, i := range tiproxyInfo {
if v, ok := globalInfo.hosts[i.IP]; ok {
infoByIk["tiproxy"].hosts[i.IP] = v
} else {
globalFailureHostsSet[i.IP] = struct{}{}
}

Check warning on line 199 in pkg/apiserver/clusterinfo/statistics.go

View check run for this annotation

Codecov / codecov/patch

pkg/apiserver/clusterinfo/statistics.go#L194-L199

Added lines #L194 - L199 were not covered by tests
}

// Generate result..
versions := lo.Keys(globalVersionsSet)
Expand Down
29 changes: 29 additions & 0 deletions pkg/apiserver/debugapi/endpoint/payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ import (
"github.com/pingcap/tidb-dashboard/pkg/utils/topology"
"github.com/pingcap/tidb-dashboard/util/client/httpclient"
"github.com/pingcap/tidb-dashboard/util/client/pdclient"
"github.com/pingcap/tidb-dashboard/util/client/ticdcclient"
"github.com/pingcap/tidb-dashboard/util/client/tidbclient"
"github.com/pingcap/tidb-dashboard/util/client/tiflashclient"
"github.com/pingcap/tidb-dashboard/util/client/tikvclient"
"github.com/pingcap/tidb-dashboard/util/client/tiproxyclient"
"github.com/pingcap/tidb-dashboard/util/rest"
"github.com/pingcap/tidb-dashboard/util/topo"
)
Expand All @@ -40,6 +42,8 @@ type HTTPClients struct {
TiDBStatusClient *tidbclient.StatusClient
TiKVStatusClient *tikvclient.StatusClient
TiFlashStatusClient *tiflashclient.StatusClient
TiCDCStatusClient *ticdcclient.StatusClient
TiProxyStatusClient *tiproxyclient.StatusClient
}

func (c HTTPClients) GetHTTPClientByNodeKind(kind topo.Kind) *httpclient.Client {
Expand All @@ -64,6 +68,16 @@ func (c HTTPClients) GetHTTPClientByNodeKind(kind topo.Kind) *httpclient.Client
return nil
}
return c.TiFlashStatusClient.Client
case topo.KindTiCDC:
if c.TiCDCStatusClient == nil {
return nil
}
return c.TiCDCStatusClient.Client
case topo.KindTiProxy:
if c.TiProxyStatusClient == nil {
return nil
}
return c.TiProxyStatusClient.Client

Check warning on line 80 in pkg/apiserver/debugapi/endpoint/payload.go

View check run for this annotation

Codecov / codecov/patch

pkg/apiserver/debugapi/endpoint/payload.go#L71-L80

Added lines #L71 - L80 were not covered by tests
default:
return nil
}
Expand Down Expand Up @@ -277,6 +291,21 @@ func (p *ResolvedRequestPayload) verifyEndpoint(ctx context.Context, etcdClient
if !matched {
return ErrInvalidEndpoint.New("invalid endpoint '%s:%d'", p.host, p.port)
}
case topo.KindTiProxy:
infos, err := topology.FetchTiProxyTopology(ctx, etcdClient)
if err != nil {
return ErrInvalidEndpoint.Wrap(err, "failed to fetch tiproxy topology")
}
matched := false
for _, info := range infos {
if info.IP == p.host && info.Port == uint(p.port) {
matched = true
break

Check warning on line 303 in pkg/apiserver/debugapi/endpoint/payload.go

View check run for this annotation

Codecov / codecov/patch

pkg/apiserver/debugapi/endpoint/payload.go#L294-L303

Added lines #L294 - L303 were not covered by tests
}
}
if !matched {
return ErrInvalidEndpoint.New("invalid endpoint '%s:%d'", p.host, p.port)
}

Check warning on line 308 in pkg/apiserver/debugapi/endpoint/payload.go

View check run for this annotation

Codecov / codecov/patch

pkg/apiserver/debugapi/endpoint/payload.go#L306-L308

Added lines #L306 - L308 were not covered by tests
default:
return ErrUnknownComponent.New("Unknown component '%s'", p.api.Component)
}
Expand Down
6 changes: 6 additions & 0 deletions pkg/apiserver/debugapi/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ import (
"github.com/pingcap/tidb-dashboard/pkg/apiserver/user"
"github.com/pingcap/tidb-dashboard/pkg/pd"
"github.com/pingcap/tidb-dashboard/util/client/pdclient"
"github.com/pingcap/tidb-dashboard/util/client/ticdcclient"
"github.com/pingcap/tidb-dashboard/util/client/tidbclient"
"github.com/pingcap/tidb-dashboard/util/client/tiflashclient"
"github.com/pingcap/tidb-dashboard/util/client/tikvclient"
"github.com/pingcap/tidb-dashboard/util/client/tiproxyclient"
"github.com/pingcap/tidb-dashboard/util/rest"
"github.com/pingcap/tidb-dashboard/util/rest/fileswap"
)
Expand All @@ -39,6 +41,8 @@ type ServiceParams struct {
TiDBStatusClient *tidbclient.StatusClient
TiKVStatusClient *tikvclient.StatusClient
TiFlashStatusClient *tiflashclient.StatusClient
TiCDCStatusClient *ticdcclient.StatusClient
TiProxyStatusClient *tiproxyclient.StatusClient
EtcdClient *clientv3.Client
PDClient *pd.Client
}
Expand All @@ -57,6 +61,8 @@ func newService(p ServiceParams) *Service {
TiDBStatusClient: p.TiDBStatusClient,
TiKVStatusClient: p.TiKVStatusClient,
TiFlashStatusClient: p.TiFlashStatusClient,
TiCDCStatusClient: p.TiCDCStatusClient,
TiProxyStatusClient: p.TiProxyStatusClient,
}
return &Service{
httpClients: httpClients,
Expand Down
4 changes: 4 additions & 0 deletions pkg/apiserver/model/common_models.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const (
NodeKindPD NodeKind = "pd"
NodeKindTiFlash NodeKind = "tiflash"
NodeKindTiCDC NodeKind = "ticdc"
NodeKindTiProxy NodeKind = "tiproxy"
)

type RequestTargetNode struct {
Expand All @@ -39,6 +40,7 @@ type RequestTargetStatistics struct {
NumPDNodes int `json:"num_pd_nodes"`
NumTiFlashNodes int `json:"num_tiflash_nodes"`
NumTiCDCNodes int `json:"num_ticdc_nodes"`
NumTiProxyNodes int `json:"num_tiproxy_nodes"`
}

func NewRequestTargetStatisticsFromArray(arr *[]RequestTargetNode) RequestTargetStatistics {
Expand All @@ -55,6 +57,8 @@ func NewRequestTargetStatisticsFromArray(arr *[]RequestTargetNode) RequestTarget
stats.NumTiFlashNodes++
case NodeKindTiCDC:
stats.NumTiCDCNodes++
case NodeKindTiProxy:
stats.NumTiProxyNodes++

Check warning on line 61 in pkg/apiserver/model/common_models.go

View check run for this annotation

Codecov / codecov/patch

pkg/apiserver/model/common_models.go#L60-L61

Added lines #L60 - L61 were not covered by tests
}
}
return stats
Expand Down
28 changes: 28 additions & 0 deletions pkg/apiserver/profiling/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ import (

"github.com/pingcap/tidb-dashboard/pkg/config"
"github.com/pingcap/tidb-dashboard/pkg/pd"
"github.com/pingcap/tidb-dashboard/pkg/ticdc"
"github.com/pingcap/tidb-dashboard/pkg/tidb"
"github.com/pingcap/tidb-dashboard/pkg/tiflash"
"github.com/pingcap/tidb-dashboard/pkg/tikv"
"github.com/pingcap/tidb-dashboard/pkg/tiproxy"
)

const (
Expand All @@ -41,13 +43,17 @@ type fetchers struct {
tiflash profileFetcher
tidb profileFetcher
pd profileFetcher
ticdc profileFetcher
tiproxy profileFetcher
}

var newFetchers = fx.Provide(func(
tikvClient *tikv.Client,
tidbClient *tidb.Client,
pdClient *pd.Client,
tiflashClient *tiflash.Client,
ticdcClient *ticdc.Client,
tiproxyClient *tiproxy.Client,
config *config.Config,
) *fetchers {
return &fetchers{
Expand All @@ -64,6 +70,12 @@ var newFetchers = fx.Provide(func(
client: pdClient,
statusAPIHTTPScheme: config.GetClusterHTTPScheme(),
},
ticdc: &ticdcFecther{
client: ticdcClient,
},
tiproxy: &tiproxyFecther{
client: tiproxyClient,
},

Check warning on line 78 in pkg/apiserver/profiling/fetcher.go

View check run for this annotation

Codecov / codecov/patch

pkg/apiserver/profiling/fetcher.go#L73-L78

Added lines #L73 - L78 were not covered by tests
}
})

Expand Down Expand Up @@ -146,3 +158,19 @@ func (f *pdFetcher) fetch(op *fetchOptions) ([]byte, error) {
WithoutPrefix(). // pprof API does not have /pd/api/v1 prefix
SendGetRequest(op.path)
}

type ticdcFecther struct {
client *ticdc.Client
}

func (f *ticdcFecther) fetch(op *fetchOptions) ([]byte, error) {
return f.client.WithTimeout(maxProfilingTimeout).SendGetRequest(op.ip, op.port, op.path)

Check warning on line 167 in pkg/apiserver/profiling/fetcher.go

View check run for this annotation

Codecov / codecov/patch

pkg/apiserver/profiling/fetcher.go#L166-L167

Added lines #L166 - L167 were not covered by tests
}

type tiproxyFecther struct {
client *tiproxy.Client
}

func (f *tiproxyFecther) fetch(op *fetchOptions) ([]byte, error) {
return f.client.WithTimeout(maxProfilingTimeout).SendGetRequest(op.ip, op.port, op.path)

Check warning on line 175 in pkg/apiserver/profiling/fetcher.go

View check run for this annotation

Codecov / codecov/patch

pkg/apiserver/profiling/fetcher.go#L174-L175

Added lines #L174 - L175 were not covered by tests
}
4 changes: 4 additions & 0 deletions pkg/apiserver/profiling/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ func profileAndWritePprof(ctx context.Context, fts *fetchers, target *model.Requ
return fetchPprof(&pprofOptions{duration: profileDurationSecs, fileNameWithoutExt: fileNameWithoutExt, target: target, fetcher: &fts.tidb, profilingType: profilingType})
case model.NodeKindPD:
return fetchPprof(&pprofOptions{duration: profileDurationSecs, fileNameWithoutExt: fileNameWithoutExt, target: target, fetcher: &fts.pd, profilingType: profilingType})
case model.NodeKindTiCDC:
return fetchPprof(&pprofOptions{duration: profileDurationSecs, fileNameWithoutExt: fileNameWithoutExt, target: target, fetcher: &fts.ticdc, profilingType: profilingType})
case model.NodeKindTiProxy:
return fetchPprof(&pprofOptions{duration: profileDurationSecs, fileNameWithoutExt: fileNameWithoutExt, target: target, fetcher: &fts.tiproxy, profilingType: profilingType})

Check warning on line 32 in pkg/apiserver/profiling/profile.go

View check run for this annotation

Codecov / codecov/patch

pkg/apiserver/profiling/profile.go#L29-L32

Added lines #L29 - L32 were not covered by tests
default:
return "", "", ErrUnsupportedProfilingTarget.New(target.String())
}
Expand Down
Loading

0 comments on commit a97a763

Please sign in to comment.