From c1804bee4b0bb02787afae9fe962f936c8b985a4 Mon Sep 17 00:00:00 2001 From: tiancaiamao Date: Mon, 9 Oct 2017 15:38:53 +0800 Subject: [PATCH] store/tikv/mocktikv: rpc handler should put error in response instead of return it --- store/tikv/mock-tikv/analyze.go | 20 +++++++++++++------- store/tikv/mock-tikv/cop_handler_dag.go | 24 ++++++++++++++---------- store/tikv/mock-tikv/rpc.go | 8 ++------ 3 files changed, 29 insertions(+), 23 deletions(-) diff --git a/store/tikv/mock-tikv/analyze.go b/store/tikv/mock-tikv/analyze.go index c933f3c6643fc..f2eb1660bf3ec 100644 --- a/store/tikv/mock-tikv/analyze.go +++ b/store/tikv/mock-tikv/analyze.go @@ -27,27 +27,33 @@ import ( "github.com/pingcap/tipb/go-tipb" ) -func (h *rpcHandler) handleCopAnalyzeRequest(req *coprocessor.Request) (*coprocessor.Response, error) { +func (h *rpcHandler) handleCopAnalyzeRequest(req *coprocessor.Request) *coprocessor.Response { resp := &coprocessor.Response{} if len(req.Ranges) == 0 { - return resp, nil + return resp } if req.GetTp() != kv.ReqTypeAnalyze { - return resp, nil + return resp } if err := h.checkRequestContext(req.GetContext()); err != nil { resp.RegionError = err - return resp, nil + return resp } analyzeReq := new(tipb.AnalyzeReq) err := proto.Unmarshal(req.Data, analyzeReq) if err != nil { - return nil, errors.Trace(err) + resp.OtherError = err.Error() + return resp } if analyzeReq.Tp == tipb.AnalyzeType_TypeIndex { - return h.handleAnalyzeIndexReq(req, analyzeReq) + resp, err = h.handleAnalyzeIndexReq(req, analyzeReq) + } else { + resp, err = h.handleAnalyzeColumnsReq(req, analyzeReq) + } + if err != nil { + resp.OtherError = err.Error() } - return h.handleAnalyzeColumnsReq(req, analyzeReq) + return resp } func (h *rpcHandler) handleAnalyzeIndexReq(req *coprocessor.Request, analyzeReq *tipb.AnalyzeReq) (*coprocessor.Response, error) { diff --git a/store/tikv/mock-tikv/cop_handler_dag.go b/store/tikv/mock-tikv/cop_handler_dag.go index 57ca00dd0710c..0abab17607056 100644 --- a/store/tikv/mock-tikv/cop_handler_dag.go +++ b/store/tikv/mock-tikv/cop_handler_dag.go @@ -41,23 +41,24 @@ type dagContext struct { evalCtx *evalContext } -func (h *rpcHandler) handleCopDAGRequest(req *coprocessor.Request) (*coprocessor.Response, error) { +func (h *rpcHandler) handleCopDAGRequest(req *coprocessor.Request) *coprocessor.Response { resp := &coprocessor.Response{} if len(req.Ranges) == 0 { - return resp, nil + return resp } if req.GetTp() != kv.ReqTypeDAG { - return resp, nil + return resp } if err := h.checkRequestContext(req.GetContext()); err != nil { resp.RegionError = err - return resp, nil + return resp } dagReq := new(tipb.DAGRequest) err := proto.Unmarshal(req.Data, dagReq) if err != nil { - return nil, errors.Trace(err) + resp.OtherError = err.Error() + return resp } sc := flagsToStatementContext(dagReq.Flags) timeZone := time.FixedZone("UTC", int(dagReq.TimeZoneOffset)) @@ -68,7 +69,9 @@ func (h *rpcHandler) handleCopDAGRequest(req *coprocessor.Request) (*coprocessor } e, err := h.buildDAG(ctx, dagReq.Executors) if err != nil { - return nil, errors.Trace(err) + return &coprocessor.Response{ + OtherError: err.Error(), + } } var ( chunks []tipb.Chunk @@ -78,7 +81,7 @@ func (h *rpcHandler) handleCopDAGRequest(req *coprocessor.Request) (*coprocessor var row [][]byte row, err = e.Next() if err != nil { - return nil, errors.Trace(err) + break } if row == nil { break @@ -323,7 +326,7 @@ func flagsToStatementContext(flags uint64) *variable.StatementContext { return sc } -func buildResp(chunks []tipb.Chunk, err error) (*coprocessor.Response, error) { +func buildResp(chunks []tipb.Chunk, err error) *coprocessor.Response { resp := &coprocessor.Response{} selResp := &tipb.SelectResponse{ Error: toPBError(err), @@ -343,10 +346,11 @@ func buildResp(chunks []tipb.Chunk, err error) (*coprocessor.Response, error) { } data, err := proto.Marshal(selResp) if err != nil { - return nil, errors.Trace(err) + resp.OtherError = err.Error() + return resp } resp.Data = data - return resp, nil + return resp } func toPBError(err error) *tipb.Error { diff --git a/store/tikv/mock-tikv/rpc.go b/store/tikv/mock-tikv/rpc.go index 993f3ba4147e9..36d41b8379bc9 100644 --- a/store/tikv/mock-tikv/rpc.go +++ b/store/tikv/mock-tikv/rpc.go @@ -581,14 +581,10 @@ func (c *RPCClient) SendReq(ctx goctx.Context, addr string, req *tikvrpc.Request handler.rawStartKey = MvccKey(handler.startKey).Raw() handler.rawEndKey = MvccKey(handler.endKey).Raw() var res *coprocessor.Response - var err error if r.GetTp() == kv.ReqTypeDAG { - res, err = handler.handleCopDAGRequest(r) + res = handler.handleCopDAGRequest(r) } else { - res, err = handler.handleCopAnalyzeRequest(r) - } - if err != nil { - return nil, err + res = handler.handleCopAnalyzeRequest(r) } resp.Cop = res case tikvrpc.CmdMvccGetByKey: