From 0f22ecbb6fd1187335dc45ee3b2a244a34066693 Mon Sep 17 00:00:00 2001 From: Wenxuan Date: Thu, 12 Nov 2020 11:46:42 +0800 Subject: [PATCH] *: Fix slow query and start_ts not working in some cases (#793) Signed-off-by: Breezewish --- pkg/apiserver/slowquery/queries.go | 53 ++++++++++--------- pkg/apiserver/slowquery/service.go | 8 +-- ui/lib/apps/SlowQuery/pages/Detail/index.tsx | 2 +- .../utils/useSlowQueryTableController.ts | 4 +- 4 files changed, 35 insertions(+), 32 deletions(-) diff --git a/pkg/apiserver/slowquery/queries.go b/pkg/apiserver/slowquery/queries.go index 0ac04185bd..28141c9f43 100644 --- a/pkg/apiserver/slowquery/queries.go +++ b/pkg/apiserver/slowquery/queries.go @@ -24,27 +24,29 @@ import ( const ( SlowQueryTable = "INFORMATION_SCHEMA.CLUSTER_SLOW_QUERY" - SelectStmt = "*, (unix_timestamp(Time) + 0E0) as timestamp" + SelectStmt = "*, (UNIX_TIMESTAMP(Time) + 0E0) AS timestamp" ) type SlowQuery struct { Digest string `gorm:"column:Digest" json:"digest"` Query string `gorm:"column:Query" json:"query"` - Instance string `gorm:"column:INSTANCE" json:"instance"` - DB string `gorm:"column:DB" json:"db"` - ConnectionID uint `gorm:"column:Conn_ID" json:"connection_id"` + Instance string `gorm:"column:INSTANCE" json:"instance"` + DB string `gorm:"column:DB" json:"db"` + // TODO: Switch back to uint64 when modern browser as well as Swagger handles BigInt well. + ConnectionID string `gorm:"column:Conn_ID" json:"connection_id"` Success int `gorm:"column:Succ" json:"success"` - Timestamp float64 `gorm:"column:timestamp" proj:"(unix_timestamp(Time) + 0E0)" json:"timestamp"` // finish time + Timestamp float64 `gorm:"column:timestamp" proj:"(UNIX_TIMESTAMP(Time) + 0E0)" json:"timestamp"` // finish time QueryTime float64 `gorm:"column:Query_time" json:"query_time"` // latency ParseTime float64 `gorm:"column:Parse_time" json:"parse_time"` CompileTime float64 `gorm:"column:Compile_time" json:"compile_time"` ProcessTime float64 `gorm:"column:Process_time" json:"process_time"` - MemoryMax int `gorm:"column:Mem_max" json:"memory_max"` - DiskMax int `gorm:"column:Disk_max" json:"disk_max"` - TxnStartTS uint `gorm:"column:Txn_start_ts" json:"txn_start_ts"` + MemoryMax int `gorm:"column:Mem_max" json:"memory_max"` + DiskMax int `gorm:"column:Disk_max" json:"disk_max"` + // TODO: Switch back to uint64 when modern browser as well as Swagger handles BigInt well. + TxnStartTS string `gorm:"column:Txn_start_ts" json:"txn_start_ts"` // Detail PrevStmt string `gorm:"column:Prev_stmt" json:"prev_stmt"` @@ -91,13 +93,13 @@ type SlowQuery struct { } type GetListRequest struct { - LogStartTS int64 `json:"logStartTS" form:"logStartTS"` - LogEndTS int64 `json:"logEndTS" form:"logEndTS"` - DB []string `json:"db" form:"db"` - Limit int `json:"limit" form:"limit"` - Text string `json:"text" form:"text"` - OrderBy string `json:"orderBy" form:"orderBy"` - DESC bool `json:"desc" form:"desc"` + RangeBeginTs uint `json:"rangeBeginTs" form:"rangeBeginTs"` + RangeEndTs uint `json:"rangeEndTs" form:"rangeEndTs"` + DB []string `json:"db" form:"db"` + Limit uint `json:"limit" form:"limit"` + Text string `json:"text" form:"text"` + OrderBy string `json:"orderBy" form:"orderBy"` + IsDesc bool `json:"desc" form:"desc"` // for showing slow queries in the statement detail page Plans []string `json:"plans" form:"plans"` @@ -136,7 +138,8 @@ func getProjectionsByFields(jsonFields ...string) ([]string, error) { type GetDetailRequest struct { Digest string `json:"digest" form:"digest"` Timestamp float64 `json:"timestamp" form:"timestamp"` - ConnectID int64 `json:"connect_id" form:"connect_id"` + // TODO: Switch back to uint64 when modern browser as well as Swagger handles BigInt well. + ConnectID string `json:"connect_id" form:"connect_id"` } func QuerySlowLogList(db *gorm.DB, req *GetListRequest) ([]SlowQuery, error) { @@ -153,7 +156,7 @@ func QuerySlowLogList(db *gorm.DB, req *GetListRequest) ([]SlowQuery, error) { tx := db. Table(SlowQueryTable). Select(strings.Join(projections, ", ")). - Where("Time between from_unixtime(?) and from_unixtime(?)", req.LogStartTS, req.LogEndTS). + Where("Time BETWEEN FROM_UNIXTIME(?) AND FROM_UNIXTIME(?)", req.RangeBeginTs, req.RangeEndTs). Limit(req.Limit) if req.Text != "" { @@ -161,10 +164,10 @@ func QuerySlowLogList(db *gorm.DB, req *GetListRequest) ([]SlowQuery, error) { arr := strings.Fields(lowerStr) for _, v := range arr { tx = tx.Where( - `txn_start_ts REGEXP ? - OR LOWER(digest) REGEXP ? - OR LOWER(CONVERT(prev_stmt USING utf8)) REGEXP ? - OR LOWER(CONVERT(query USING utf8)) REGEXP ?`, + `Txn_start_ts REGEXP ? + OR LOWER(Digest) REGEXP ? + OR LOWER(CONVERT(Prev_stmt USING utf8)) REGEXP ? + OR LOWER(CONVERT(Query USING utf8)) REGEXP ?`, v, v, v, v, ) } @@ -183,10 +186,10 @@ func QuerySlowLogList(db *gorm.DB, req *GetListRequest) ([]SlowQuery, error) { if strings.Contains(order[0], " AS ") { order[0] = req.OrderBy } - if req.DESC { - tx = tx.Order(fmt.Sprintf("%s desc", order[0])) + if req.IsDesc { + tx = tx.Order(fmt.Sprintf("%s DESC", order[0])) } else { - tx = tx.Order(fmt.Sprintf("%s asc", order[0])) + tx = tx.Order(fmt.Sprintf("%s ASC", order[0])) } if len(req.Plans) > 0 { @@ -211,7 +214,7 @@ func QuerySlowLogDetail(db *gorm.DB, req *GetDetailRequest) (*SlowQuery, error) Table(SlowQueryTable). Select(SelectStmt). Where("Digest = ?", req.Digest). - Where("Time = from_unixtime(?)", req.Timestamp). + Where("Time = FROM_UNIXTIME(?)", req.Timestamp). Where("Conn_id = ?", req.ConnectID). First(&result).Error if err != nil { diff --git a/pkg/apiserver/slowquery/service.go b/pkg/apiserver/slowquery/service.go index 9f04fcd304..235609a744 100644 --- a/pkg/apiserver/slowquery/service.go +++ b/pkg/apiserver/slowquery/service.go @@ -41,8 +41,8 @@ func Register(r *gin.RouterGroup, auth *user.AuthService, s *Service) { endpoint := r.Group("/slow_query") endpoint.Use(auth.MWAuthRequired()) endpoint.Use(utils.MWConnectTiDB(s.params.TiDBClient)) - endpoint.GET("/list", s.listHandler) - endpoint.GET("/detail", s.detailhandler) + endpoint.GET("/list", s.getList) + endpoint.GET("/detail", s.getDetails) } // @Summary List all slow queries @@ -51,7 +51,7 @@ func Register(r *gin.RouterGroup, auth *user.AuthService, s *Service) { // @Router /slow_query/list [get] // @Security JwtAuth // @Failure 401 {object} utils.APIError "Unauthorized failure" -func (s *Service) listHandler(c *gin.Context) { +func (s *Service) getList(c *gin.Context) { var req GetListRequest if err := c.ShouldBindQuery(&req); err != nil { utils.MakeInvalidRequestErrorFromError(c, err) @@ -73,7 +73,7 @@ func (s *Service) listHandler(c *gin.Context) { // @Router /slow_query/detail [get] // @Security JwtAuth // @Failure 401 {object} utils.APIError "Unauthorized failure" -func (s *Service) detailhandler(c *gin.Context) { +func (s *Service) getDetails(c *gin.Context) { var req GetDetailRequest if err := c.ShouldBindQuery(&req); err != nil { utils.MakeInvalidRequestErrorFromError(c, err) diff --git a/ui/lib/apps/SlowQuery/pages/Detail/index.tsx b/ui/lib/apps/SlowQuery/pages/Detail/index.tsx index 1f47b2e777..358d23cfd0 100644 --- a/ui/lib/apps/SlowQuery/pages/Detail/index.tsx +++ b/ui/lib/apps/SlowQuery/pages/Detail/index.tsx @@ -27,7 +27,7 @@ import TabCopr from './DetailTabCopr' import TabTxn from './DetailTabTxn' export interface IPageQuery { - connectId?: number + connectId?: string digest?: string timestamp?: number } diff --git a/ui/lib/apps/SlowQuery/utils/useSlowQueryTableController.ts b/ui/lib/apps/SlowQuery/utils/useSlowQueryTableController.ts index dcb4370bc7..7e575ed2ea 100644 --- a/ui/lib/apps/SlowQuery/utils/useSlowQueryTableController.ts +++ b/ui/lib/apps/SlowQuery/utils/useSlowQueryTableController.ts @@ -145,10 +145,10 @@ export default function useSlowQueryTableController( queryOptions.digest, selectedFields, queryOptions.limit, - queryTimeRange.endTime, - queryTimeRange.beginTime, orderOptions.orderBy, queryOptions.plans, + queryTimeRange.beginTime, + queryTimeRange.endTime, queryOptions.searchText, { errorStrategy: ErrorStrategy.Custom,