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

refactor: Switch to use ziputil, netutil, reflectutil and fileswap #1067

Merged
merged 2 commits into from
Nov 24, 2021
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
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ run:
skip-dirs:
- swaggerspec
- pkg/uiserver
- ui
timeout: 2m

issues:
Expand Down
4 changes: 2 additions & 2 deletions pkg/apiserver/clusterinfo/hostinfo/cluster_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

"gorm.io/gorm"

"github.com/pingcap/tidb-dashboard/pkg/utils/host"
"github.com/pingcap/tidb-dashboard/util/netutil"
)

type clusterConfigModel struct {
Expand All @@ -29,7 +29,7 @@ func FillInstances(db *gorm.DB, m InfoMap) error {
}

for _, row := range rows {
hostname, _, err := host.ParseHostAndPortFromAddress(row.Instance)
hostname, _, err := netutil.ParseHostAndPortFromAddress(row.Instance)
if err != nil {
continue
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/apiserver/clusterinfo/hostinfo/cluster_hardware.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

"gorm.io/gorm"

"github.com/pingcap/tidb-dashboard/pkg/utils/host"
"github.com/pingcap/tidb-dashboard/util/netutil"
)

// Used to deserialize from JSON_VALUE.
Expand Down Expand Up @@ -46,7 +46,7 @@ func FillFromClusterHardwareTable(db *gorm.DB, m InfoMap) error {
tiFlashDisks := make([]clusterTableModel, 0)

for _, row := range rows {
hostname, _, err := host.ParseHostAndPortFromAddress(row.Instance)
hostname, _, err := netutil.ParseHostAndPortFromAddress(row.Instance)
if err != nil {
continue
}
Expand Down Expand Up @@ -126,7 +126,7 @@ func FillFromClusterHardwareTable(db *gorm.DB, m InfoMap) error {
}
// Back fill TiFlash instances
for instance, de := range tiFlashDiskInfo {
hostname, _, err := host.ParseHostAndPortFromAddress(instance)
hostname, _, err := netutil.ParseHostAndPortFromAddress(instance)
if err != nil {
panic(err)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/apiserver/clusterinfo/hostinfo/cluster_load.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

"gorm.io/gorm"

"github.com/pingcap/tidb-dashboard/pkg/utils/host"
"github.com/pingcap/tidb-dashboard/util/netutil"
)

// Used to deserialize from JSON_VALUE.
Expand Down Expand Up @@ -40,7 +40,7 @@ func FillFromClusterLoadTable(db *gorm.DB, m InfoMap) error {
}

for _, row := range rows {
hostname, _, err := host.ParseHostAndPortFromAddress(row.Instance)
hostname, _, err := netutil.ParseHostAndPortFromAddress(row.Instance)
if err != nil {
continue
}
Expand Down
37 changes: 19 additions & 18 deletions pkg/apiserver/debugapi/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,8 @@ import (

"github.com/pingcap/tidb-dashboard/pkg/apiserver/debugapi/endpoint"
"github.com/pingcap/tidb-dashboard/pkg/apiserver/user"
"github.com/pingcap/tidb-dashboard/pkg/apiserver/utils"
"github.com/pingcap/tidb-dashboard/util/rest"
)

const (
tokenIssuer = "debugAPI"
"github.com/pingcap/tidb-dashboard/util/rest/fileswap"
)

func registerRouter(r *gin.RouterGroup, auth *user.AuthService, s *Service) {
Expand All @@ -34,11 +30,13 @@ func registerRouter(r *gin.RouterGroup, auth *user.AuthService, s *Service) {

type Service struct {
Client *endpoint.Client
fSwap *fileswap.Handler
}

func newService(hp httpClientParam) *Service {
return &Service{
Client: endpoint.NewClient(newHTTPClient(hp), endpointDefs),
fSwap: fileswap.New(),
}
}

Expand Down Expand Up @@ -82,27 +80,31 @@ func (s *Service) RequestEndpoint(c *gin.Context) {
}
defer res.Response.Body.Close() //nolint:errcheck

ext := getExtFromContentTypeHeader(res.Header.Get("Content-Type"))
fileName := fmt.Sprintf("%s_%d%s", req.EndpointID, time.Now().Unix(), ext)

writer, token, err := utils.FSPersist(utils.FSPersistConfig{
TokenIssuer: tokenIssuer,
TokenExpire: time.Minute * 5, // Note: the expire time should include request time.
TempFilePattern: "debug_api",
DownloadFileName: fileName,
})
writer, err := s.fSwap.NewFileWriter("debug_api")
if err != nil {
_ = c.Error(err)
return
}
defer writer.Close() //nolint:errcheck
defer func() {
_ = writer.Close()
}()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

knowledge++


_, err = io.Copy(writer, res.Response.Body)
if err != nil {
_ = c.Error(err)
return
}

c.String(http.StatusOK, token)
ext := getExtFromContentTypeHeader(res.Header.Get("Content-Type"))
fileName := fmt.Sprintf("%s_%d%s", req.EndpointID, time.Now().Unix(), ext)
downloadToken, err := writer.GetDownloadToken(fileName, time.Minute*5)
if err != nil {
// This shall never happen
_ = c.Error(err)
return
}

c.String(http.StatusOK, downloadToken)
}

// @Summary Download a finished request result
Expand All @@ -112,8 +114,7 @@ func (s *Service) RequestEndpoint(c *gin.Context) {
// @Failure 500 {object} rest.ErrorResponse
// @Router /debug_api/download [get]
func (s *Service) Download(c *gin.Context) {
token := c.Query("token")
utils.FSServe(c, token, tokenIssuer)
s.fSwap.HandleDownloadRequest(c)
}

// @Summary Get all endpoints
Expand Down
4 changes: 2 additions & 2 deletions pkg/apiserver/logsearch/pack.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"github.com/pingcap/log"
"go.uber.org/zap"

"github.com/pingcap/tidb-dashboard/pkg/apiserver/utils"
"github.com/pingcap/tidb-dashboard/util/rest"
"github.com/pingcap/tidb-dashboard/util/ziputil"
)

func serveTaskForDownload(task *TaskModel, c *gin.Context) {
Expand Down Expand Up @@ -41,7 +41,7 @@ func serveMultipleTaskForDownload(tasks []*TaskModel, c *gin.Context) {

c.Writer.Header().Set("Content-type", "application/octet-stream")
c.Writer.Header().Set("Content-Disposition", "attachment; filename=\"logs.zip\"")
err := utils.StreamZipPack(c.Writer, filePaths, false)
err := ziputil.WriteZipFromFiles(c.Writer, filePaths, false)
if err != nil {
log.Error("Stream zip pack failed", zap.Error(err))
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/apiserver/profiling/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/pingcap/tidb-dashboard/pkg/apiserver/utils"
"github.com/pingcap/tidb-dashboard/pkg/config"
"github.com/pingcap/tidb-dashboard/util/rest"
"github.com/pingcap/tidb-dashboard/util/ziputil"
)

// Register register the handlers to the service.
Expand Down Expand Up @@ -223,7 +224,7 @@ func (s *Service) downloadGroup(c *gin.Context) {
fileName := fmt.Sprintf("profiling_pack_%d.zip", taskGroupID)
c.Writer.Header().Set("Content-type", "application/octet-stream")
c.Writer.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", fileName))
err = utils.StreamZipPack(c.Writer, filePathes, true)
err = ziputil.WriteZipFromFiles(c.Writer, filePathes, true)
if err != nil {
log.Error("Stream zip pack failed", zap.Error(err))
}
Expand Down Expand Up @@ -262,7 +263,7 @@ func (s *Service) downloadSingle(c *gin.Context) {
fileName := fmt.Sprintf("profiling_%d.zip", taskID)
c.Writer.Header().Set("Content-type", "application/octet-stream")
c.Writer.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", fileName))
err = utils.StreamZipPack(c.Writer, []string{task.FilePath}, true)
err = ziputil.WriteZipFromFiles(c.Writer, []string{task.FilePath}, true)
if err != nil {
log.Error("Stream zip pack failed", zap.Error(err))
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/apiserver/slowquery/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/thoas/go-funk"

"github.com/pingcap/tidb-dashboard/pkg/apiserver/utils"
"github.com/pingcap/tidb-dashboard/util/reflectutil"
)

type Model struct {
Expand Down Expand Up @@ -96,7 +97,7 @@ type Field struct {
}

func getFieldsAndTags() (slowQueryFields []Field) {
fields := utils.GetFieldsAndTags(Model{}, []string{"gorm", "proj", "json"})
fields := reflectutil.GetFieldsAndTags(Model{}, []string{"gorm", "proj", "json"})

for _, f := range fields {
sqf := Field{
Expand Down
3 changes: 2 additions & 1 deletion pkg/apiserver/statement/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"gorm.io/gorm/schema"

"github.com/pingcap/tidb-dashboard/pkg/apiserver/utils"
"github.com/pingcap/tidb-dashboard/util/reflectutil"
)

// TimeRange represents a range of time.
Expand Down Expand Up @@ -135,7 +136,7 @@ type Field struct {
var gormDefaultNamingStrategy = schema.NamingStrategy{}

func getFieldsAndTags() (stmtFields []Field) {
fields := utils.GetFieldsAndTags(Model{}, []string{"related", "agg", "json"})
fields := reflectutil.GetFieldsAndTags(Model{}, []string{"related", "agg", "json"})

for _, f := range fields {
sf := Field{
Expand Down
119 changes: 0 additions & 119 deletions pkg/apiserver/utils/fs_stream.go

This file was deleted.

32 changes: 0 additions & 32 deletions pkg/apiserver/utils/reflections.go

This file was deleted.

Loading