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

store/tikv: support debug PB in client. #10038

Merged
merged 11 commits into from
Apr 4, 2019
12 changes: 12 additions & 0 deletions store/tikv/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/grpc-ecosystem/go-grpc-prometheus"
"github.com/pingcap/errors"
"github.com/pingcap/kvproto/pkg/coprocessor"
"github.com/pingcap/kvproto/pkg/debugpb"
"github.com/pingcap/kvproto/pkg/tikvpb"
"github.com/pingcap/parser/terror"
"github.com/pingcap/tidb/config"
Expand Down Expand Up @@ -579,6 +580,17 @@ func (c *rpcClient) SendRequest(ctx context.Context, addr string, req *tikvrpc.R
}
}

if req.IsDebugReq() {
client := debugpb.NewDebugClient(connArray.Get())
ctx1, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
resp, err := tikvrpc.CallDebugRPC(ctx1, client, req)
lzmhhh123 marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return nil, errors.Trace(err)
lzmhhh123 marked this conversation as resolved.
Show resolved Hide resolved
}
return resp, nil
}

client := tikvpb.NewTikvClient(connArray.Get())

if req.Type != tikvrpc.CmdCopStream {
Expand Down
76 changes: 76 additions & 0 deletions store/tikv/tikvrpc/tikvrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

"github.com/pingcap/errors"
"github.com/pingcap/kvproto/pkg/coprocessor"
"github.com/pingcap/kvproto/pkg/debugpb"
"github.com/pingcap/kvproto/pkg/errorpb"
"github.com/pingcap/kvproto/pkg/kvrpcpb"
"github.com/pingcap/kvproto/pkg/metapb"
Expand Down Expand Up @@ -61,6 +62,20 @@ const (
CmdMvccGetByKey CmdType = 1024 + iota
CmdMvccGetByStartTs
CmdSplitRegion

CmdDebugGet CmdType = 2048 + iota
lzmhhh123 marked this conversation as resolved.
Show resolved Hide resolved
CmdDebugRaftLog
CmdDebugRegionInfo
CmdDebugRegionSize
CmdDebugScanMvcc
CmdDebugCompact
CmdDebugInjectFailPoint
CmdDebugRecoverFailPoint
CmdDebugListFailPoints
CmdDebugGetMetrics
CmdDebugCheckRegionConsistency
CmdDebugModifyTikvConfig
CmdDebugGetRegionProperties
)

func (t CmdType) String() string {
Expand Down Expand Up @@ -115,6 +130,32 @@ func (t CmdType) String() string {
return "MvccGetByStartTS"
case CmdSplitRegion:
return "SplitRegion"
case CmdDebugGet:
return "DebugGet"
case CmdDebugRaftLog:
return "DebugRaftLog"
case CmdDebugRegionInfo:
return "DebugRegionInfo"
case CmdDebugRegionSize:
return "DebugRegionSize"
case CmdDebugScanMvcc:
return "DebugScanMvcc"
case CmdDebugCompact:
return "DebugCompact"
case CmdDebugInjectFailPoint:
return "DebugInjectFailPoint"
case CmdDebugRecoverFailPoint:
return "DebugRecoverFailPoint"
case CmdDebugListFailPoints:
return "DebugListFailPoints"
case CmdDebugGetMetrics:
return "DebugGetMetics"
case CmdDebugCheckRegionConsistency:
return "DebugCheckRegionConsistency"
case CmdDebugModifyTikvConfig:
return "DebugModifyTikvConfig"
case CmdDebugGetRegionProperties:
return "DebugGetRegionProperties"
}
return "Unknown"
}
Expand Down Expand Up @@ -147,6 +188,8 @@ type Request struct {
MvccGetByKey *kvrpcpb.MvccGetByKeyRequest
MvccGetByStartTs *kvrpcpb.MvccGetByStartTsRequest
SplitRegion *kvrpcpb.SplitRegionRequest

DebugGetRegionProperties *debugpb.GetRegionPropertiesRequest
}

// ToBatchCommandsRequest converts the request to an entry in BatchCommands request.
Expand Down Expand Up @@ -196,6 +239,20 @@ func (req *Request) ToBatchCommandsRequest() *tikvpb.BatchCommandsRequest_Reques
return nil
}

// IsDebugReq check whether the req is debug req.
func (req *Request) IsDebugReq() bool {
switch req.Type {
case CmdDebugGet, CmdDebugRaftLog, CmdDebugRegionInfo,
CmdDebugRegionSize, CmdDebugScanMvcc, CmdDebugCompact,
CmdDebugInjectFailPoint, CmdDebugRecoverFailPoint,
CmdDebugListFailPoints, CmdDebugGetMetrics,
CmdDebugCheckRegionConsistency, CmdDebugModifyTikvConfig,
CmdDebugGetRegionProperties:
return true
}
return false
}

// Response wraps all kv/coprocessor responses.
type Response struct {
Type CmdType
Expand Down Expand Up @@ -224,6 +281,8 @@ type Response struct {
MvccGetByKey *kvrpcpb.MvccGetByKeyResponse
MvccGetByStartTS *kvrpcpb.MvccGetByStartTsResponse
SplitRegion *kvrpcpb.SplitRegionResponse

DebugGetRegionProperties *debugpb.GetRegionPropertiesResponse
}

// FromBatchCommandsResponse converts a BatchCommands response to Response.
Expand Down Expand Up @@ -592,6 +651,23 @@ func CallRPC(ctx context.Context, client tikvpb.TikvClient, req *Request) (*Resp
return resp, nil
}

// CallDebugRPC launches a debug rpc call.
func CallDebugRPC(ctx context.Context, client debugpb.DebugClient, req *Request) (*Response, error) {
resp := &Response{}
lzmhhh123 marked this conversation as resolved.
Show resolved Hide resolved
resp.Type = req.Type
var err error
switch req.Type {
case CmdDebugGetRegionProperties:
resp.DebugGetRegionProperties, err = client.GetRegionProperties(ctx, req.DebugGetRegionProperties)
default:
return nil, errors.Errorf("invalid request type: %v", req.Type)
}
if err != nil {
lzmhhh123 marked this conversation as resolved.
Show resolved Hide resolved
return nil, errors.Trace(err)
lzmhhh123 marked this conversation as resolved.
Show resolved Hide resolved
}
return resp, nil
}

// Lease is used to implement grpc stream timeout.
type Lease struct {
Cancel context.CancelFunc
Expand Down