deal uncached prepare qps

This commit is contained in:
hebo
2019-09-24 22:40:17 +08:00
parent 92e592ed73
commit a9f0ee309e
6 changed files with 159 additions and 89 deletions

View File

@@ -0,0 +1,77 @@
package model
import (
"github.com/pingcap/tidb/util/hack"
"time"
)
// MysqlQueryPiece 查询信息
type MysqlQueryPiece struct {
BaseQueryPiece
SessionID *string `json:"cid"`
ClientHost *string `json:"-"`
ClientPort int `json:"-"`
VisitUser *string `json:"user"`
VisitDB *string `json:"db"`
QuerySQL *string `json:"sql"`
CostTimeInMS int64 `json:"cms"`
}
type PooledMysqlQueryPiece struct {
MysqlQueryPiece
recoverPool *mysqlQueryPiecePool
sliceBufferPool *sliceBufferPool
}
func NewPooledMysqlQueryPiece(
sessionID, clientIP, visitUser, visitDB, clientHost, serverIP *string,
clientPort, serverPort int, throwPacketRate float64, stmtBeginTime int64) (
mqp *PooledMysqlQueryPiece) {
mqp = mqpp.Dequeue()
nowInMS := time.Now().UnixNano() / millSecondUnit
mqp.SessionID = sessionID
mqp.ClientHost = clientIP
mqp.ClientPort = clientPort
mqp.ClientHost = clientHost
mqp.ServerIP = serverIP
mqp.ServerPort = serverPort
mqp.VisitUser = visitUser
mqp.VisitDB = visitDB
mqp.SyncSend = false
mqp.ThrowPacketRate = throwPacketRate
mqp.BeginTime = stmtBeginTime
mqp.CostTimeInMS = nowInMS - stmtBeginTime
mqp.recoverPool = mqpp
mqp.sliceBufferPool = localSliceBufferPool
return
}
func (mqp *MysqlQueryPiece) String() (*string) {
content := mqp.Bytes()
contentStr := hack.String(content)
return &contentStr
}
func (mqp *MysqlQueryPiece) Bytes() (content []byte) {
// content, err := json.Marshal(mqp)
if len(mqp.jsonContent) > 0 {
return mqp.jsonContent
}
mqp.jsonContent = marsharQueryPiece(mqp)
return mqp.jsonContent
}
func (mqp *MysqlQueryPiece) GetSQL() (str *string) {
return mqp.QuerySQL
}
func (pmqp *PooledMysqlQueryPiece) Recovery() {
pmqp.recoverPool.Enqueue(pmqp)
pmqp.sliceBufferPool.Enqueue(pmqp.jsonContent[:0])
pmqp.jsonContent = nil
}

View File

@@ -1,13 +1,11 @@
package model
import (
"bytes"
"encoding/json"
// "github.com/json-iterator/go"
"time"
"bytes"
"encoding/json"
"github.com/pingcap/tidb/util/hack"
"time"
)
type QueryPiece interface {
@@ -18,28 +16,14 @@ type QueryPiece interface {
Recovery()
}
// MysqlQueryPiece 查询信息
type MysqlQueryPiece struct {
SessionID *string `json:"cid"`
ClientHost *string `json:"-"`
ClientPort int `json:"-"`
SyncSend bool `json:"-"`
ServerIP *string `json:"sip"`
ServerPort int `json:"sport"`
VisitUser *string `json:"user"`
VisitDB *string `json:"db"`
QuerySQL *string `json:"sql"`
ThrowPacketRate float64 `json:"tpr"`
BeginTime int64 `json:"bt"`
CostTimeInMS int64 `json:"cms"`
jsonContent []byte `json:"-"`
}
type PooledMysqlQueryPiece struct {
MysqlQueryPiece
recoverPool *mysqlQueryPiecePool
sliceBufferPool *sliceBufferPool
// BaseQueryPiece 查询信息
type BaseQueryPiece struct {
SyncSend bool `json:"-"`
ServerIP *string `json:"sip"`
ServerPort int `json:"sport"`
ThrowPacketRate float64 `json:"tpr"`
BeginTime int64 `json:"bt"`
jsonContent []byte `json:"-"`
}
const (
@@ -47,74 +31,63 @@ const (
)
var (
mqpp = NewMysqlQueryPiecePool()
mqpp = NewMysqlQueryPiecePool()
localSliceBufferPool = NewSliceBufferPool("json cache", 2*1024*1024)
)
func NewPooledMysqlQueryPiece(
sessionID, clientIP, visitUser, visitDB, clientHost, serverIP *string,
clientPort, serverPort int, throwPacketRate float64, stmtBeginTime int64) (
mqp *PooledMysqlQueryPiece) {
mqp = mqpp.Dequeue()
var commonBaseQueryPiece = &BaseQueryPiece{}
nowInMS := time.Now().UnixNano() / millSecondUnit
mqp.SessionID = sessionID
mqp.ClientHost = clientIP
mqp.ClientPort = clientPort
mqp.ClientHost = clientHost
mqp.ServerIP = serverIP
mqp.ServerPort = serverPort
mqp.VisitUser = visitUser
mqp.VisitDB = visitDB
mqp.SyncSend = false
mqp.ThrowPacketRate = throwPacketRate
mqp.BeginTime = stmtBeginTime
mqp.CostTimeInMS = nowInMS - stmtBeginTime
mqp.recoverPool = mqpp
mqp.sliceBufferPool = localSliceBufferPool
func NewBaseQueryPiece(
serverIP *string, serverPort int, throwPacketRate float64) (
bqp *BaseQueryPiece) {
bqp = commonBaseQueryPiece
bqp.ServerIP = serverIP
bqp.ServerPort = serverPort
bqp.SyncSend = false
bqp.ThrowPacketRate = throwPacketRate
bqp.BeginTime = time.Now().UnixNano() / millSecondUnit
return
}
func (mqp *MysqlQueryPiece) String() (*string) {
content := mqp.Bytes()
func (bqp *BaseQueryPiece) NeedSyncSend() (bool) {
return bqp.SyncSend
}
func (bqp *BaseQueryPiece) SetNeedSyncSend(syncSend bool) {
bqp.SyncSend = syncSend
}
func (bqp *BaseQueryPiece) String() (*string) {
content := bqp.Bytes()
contentStr := hack.String(content)
return &contentStr
}
func (mqp *MysqlQueryPiece) Bytes() (content []byte) {
// content, err := json.Marshal(mqp)
if len(mqp.jsonContent) > 0 {
return mqp.jsonContent
func (bqp *BaseQueryPiece) Bytes() (content []byte) {
// content, err := json.Marshal(bqp)
if bqp.jsonContent != nil && len(bqp.jsonContent) > 0 {
return bqp.jsonContent
}
bqp.jsonContent = marsharQueryPiece(bqp)
return bqp.jsonContent
}
func (bqp *BaseQueryPiece) GetSQL() (*string) {
return nil
}
func (bqp *BaseQueryPiece) Recovery() {
}
func marsharQueryPiece(qp interface{}) []byte {
var cacheBuffer = localSliceBufferPool.Dequeue()
buffer := bytes.NewBuffer(cacheBuffer)
err := json.NewEncoder(buffer).Encode(mqp)
err := json.NewEncoder(buffer).Encode(qp)
if err != nil {
mqp.jsonContent = []byte(err.Error())
} else {
mqp.jsonContent = buffer.Bytes()
return []byte(err.Error())
}
return mqp.jsonContent
}
func (mqp *MysqlQueryPiece) GetSQL() (str *string) {
return mqp.QuerySQL
}
func (mqp *MysqlQueryPiece) NeedSyncSend() (bool) {
return mqp.SyncSend
}
func (mqp *MysqlQueryPiece) SetNeedSyncSend(syncSend bool) {
mqp.SyncSend = syncSend
}
func (pmqp *PooledMysqlQueryPiece) Recovery() {
pmqp.recoverPool.Enqueue(pmqp)
pmqp.sliceBufferPool.Enqueue(pmqp.jsonContent[:0])
pmqp.jsonContent = nil
return buffer.Bytes()
}