From cbb42755d38b84be408fd81c9de26b08ff9cfb47 Mon Sep 17 00:00:00 2001 From: pandaLIU <563883861@qq.com> Date: Tue, 22 Feb 2022 22:14:32 +0800 Subject: [PATCH 01/16] Added json rpc http --- conf.sample.yml | 1 + dtmsvr/api_json_rpc_http.go | 110 ++++++++++++++++++++++++++++++++++++ dtmsvr/config/config.go | 1 + dtmsvr/svr.go | 12 ++++ dtmsvr/trans_class.go | 11 ++++ 5 files changed, 135 insertions(+) create mode 100644 dtmsvr/api_json_rpc_http.go diff --git a/conf.sample.yml b/conf.sample.yml index a4e869c..d9052a5 100644 --- a/conf.sample.yml +++ b/conf.sample.yml @@ -56,6 +56,7 @@ # HttpPort: 36789 # GrpcPort: 36790 +# JsonRpcHttp: 36791 ### advanced options # UpdateBranchAsyncGoroutineNum: 1 # num of async goroutine to update branch status diff --git a/dtmsvr/api_json_rpc_http.go b/dtmsvr/api_json_rpc_http.go new file mode 100644 index 0000000..4f174e4 --- /dev/null +++ b/dtmsvr/api_json_rpc_http.go @@ -0,0 +1,110 @@ +package dtmsvr + +import ( + "encoding/json" + "fmt" + "github.com/dtm-labs/dtm/dtmcli" + "github.com/dtm-labs/dtm/dtmcli/logger" + "github.com/gin-gonic/gin" + "net/http" +) + +type jsonRpcHttpReq struct { + Method string `json:"method"` + Jsonrpc string `json:"jsonrpc"` + Params interface{} `json:"params"` + Id string `json:"id"` +} + +func addJsonRpcHttpRouter(engine *gin.Engine) { + engine.POST("/", dispatcher) +} + +func dispatcher(c *gin.Context) { + req := new(jsonRpcHttpReq) + err := c.BindJSON(req) + logger.Infof("request:%s\n", req) + if err != nil { + c.JSON(http.StatusOK, gin.H{"id": req.Id, "result": nil, "error": map[string]interface{}{"code": -32700, "message": "Parse error"}}) + return + } + if req.Method == "dtmserver.NewGid" { + res, err := jsonRpcHttpNewGid() + c.JSON(http.StatusOK, gin.H{"id": req.Id, "result": res, "error": err}) + return + } + + if req.Method == "dtmserver.Prepare" { + res := jsonRpcHttpPrepare(req.Params) + c.JSON(http.StatusOK, gin.H{"id": req.Id, "result": res, "error": nil}) + return + } + + if req.Method == "dtmserver.Submit" { + res := jsonRpcHttpSubmit(req.Params) + c.JSON(http.StatusOK, gin.H{"id": req.Id, "result": res, "error": nil}) + return + } + + if req.Method == "dtmserver.Abort" { + res := jsonRpcHttpAbort(req.Params) + c.JSON(http.StatusOK, gin.H{"id": req.Id, "result": res, "error": nil}) + return + } + + if req.Method == "dtmserver.RegisterBranch" { + res := jsonRpcHttpRegisterBranch(req.Params) + c.JSON(http.StatusOK, gin.H{"id": req.Id, "result": res, "error": nil}) + return + } + c.JSON(http.StatusOK, gin.H{"id": req.Id, "result": nil, "error": map[string]interface{}{"code": -32601, "message": "Method not found"}}) + return +} + +func jsonRpcHttpNewGid() (interface{}, error) { + return map[string]interface{}{"gid": GenGid(), "dtm_result": dtmcli.ResultSuccess}, nil +} + +func jsonRpcHttpPrepare(params interface{}) interface{} { + res := svcPrepare(TransFromJsonRpcHttpContext(params)) + if res == nil { + return map[string]string{"dtm_result": "SUCCESS"} + } + return map[string]string{"dtm_result": "FAILURE", "message": fmt.Sprintf("%v", res)} +} + +func jsonRpcHttpSubmit(params interface{}) interface{} { + res := svcSubmit(TransFromJsonRpcHttpContext(params)) + if res == nil { + return map[string]string{"dtm_result": "SUCCESS"} + } + return map[string]string{"dtm_result": "FAILURE", "message": fmt.Sprintf("%v", res)} +} + +func jsonRpcHttpAbort(params interface{}) interface{} { + res := svcAbort(TransFromJsonRpcHttpContext(params)) + if res == nil { + return map[string]string{"dtm_result": "SUCCESS"} + } + return map[string]string{"dtm_result": "FAILURE", "message": fmt.Sprintf("%v", res)} +} + +func jsonRpcHttpRegisterBranch(params interface{}) interface{} { + data := map[string]string{} + paramsJson, _ := json.Marshal(params) + err := json.Unmarshal(paramsJson, &data) + if err != nil { + return map[string]string{"dtm_result": "FAILURE", "message": err.Error()} + } + branch := TransBranch{ + Gid: data["gid"], + BranchID: data["branch_id"], + Status: dtmcli.StatusPrepared, + BinData: []byte(data["data"]), + } + res := svcRegisterBranch(data["trans_type"], &branch, data) + if res == nil { + return map[string]string{"dtm_result": "SUCCESS"} + } + return map[string]string{"dtm_result": "FAILURE", "message": res.Error()} +} diff --git a/dtmsvr/config/config.go b/dtmsvr/config/config.go index 06a8fda..4f9ce1c 100644 --- a/dtmsvr/config/config.go +++ b/dtmsvr/config/config.go @@ -76,6 +76,7 @@ type configType struct { RequestTimeout int64 `yaml:"RequestTimeout" default:"3"` HTTPPort int64 `yaml:"HttpPort" default:"36789"` GrpcPort int64 `yaml:"GrpcPort" default:"36790"` + JsonRpcHttpPort int64 `yaml:"JsonRpcHttpPort" default:"36791"` MicroService MicroService `yaml:"MicroService"` UpdateBranchSync int64 `yaml:"UpdateBranchSync"` UpdateBranchAsyncGoroutineNum int64 `yaml:"UpdateBranchAsyncGoroutineNum" default:"1"` diff --git a/dtmsvr/svr.go b/dtmsvr/svr.go index 1816087..7989986 100644 --- a/dtmsvr/svr.go +++ b/dtmsvr/svr.go @@ -66,6 +66,18 @@ func StartSvr() { logger.FatalIfError(err) err = dtmdriver.GetDriver().RegisterGrpcService(conf.MicroService.Target, conf.MicroService.EndPoint) logger.FatalIfError(err) + + // start json-rpc server + jsonRpcHttpApp := dtmutil.GetGinApp() + jsonRpcHttpApp = httpMetrics(jsonRpcHttpApp) + addJsonRpcHttpRouter(jsonRpcHttpApp) + logger.Infof("dtmsvr listen at: %d", conf.JsonRpcHttpPort) + go func() { + err := jsonRpcHttpApp.Run(fmt.Sprintf(":%d", conf.JsonRpcHttpPort)) + if err != nil { + logger.Errorf("start server err: %v", err) + } + }() } // PopulateDB setup mysql data diff --git a/dtmsvr/trans_class.go b/dtmsvr/trans_class.go index b6e080b..8b039b9 100644 --- a/dtmsvr/trans_class.go +++ b/dtmsvr/trans_class.go @@ -8,6 +8,7 @@ package dtmsvr import ( "context" + "encoding/json" "time" "github.com/dtm-labs/dtm/dtmcli" @@ -84,6 +85,16 @@ func TransFromContext(c *gin.Context) *TransGlobal { return &m } +func TransFromJsonRpcHttpContext(params interface{}) *TransGlobal { + jsonStr, _ := json.Marshal(params) + m := TransGlobal{} + err := json.Unmarshal(jsonStr, &m) + if err != nil { + return nil + } + return &m +} + // TransFromDtmRequest TransFromContext func TransFromDtmRequest(ctx context.Context, c *dtmgpb.DtmRequest) *TransGlobal { o := &dtmgpb.DtmTransOptions{} From ad5dbd906ffc4e8a78190d48edf38ba488be46a1 Mon Sep 17 00:00:00 2001 From: yedf2 <120050102@qq.com> Date: Wed, 23 Feb 2022 20:06:23 +0800 Subject: [PATCH 02/16] storeFac use single sqlFac --- dtmsvr/storage/registry/registry.go | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/dtmsvr/storage/registry/registry.go b/dtmsvr/storage/registry/registry.go index 003fd69..e699bd3 100644 --- a/dtmsvr/storage/registry/registry.go +++ b/dtmsvr/storage/registry/registry.go @@ -18,6 +18,12 @@ type StorageFactory interface { GetStorage() storage.Store } +var sqlFac = &SingletonFactory{ + creatorFunction: func() storage.Store { + return &sql.Store{} + }, +} + var storeFactorys = map[string]StorageFactory{ "boltdb": &SingletonFactory{ creatorFunction: func() storage.Store { @@ -29,16 +35,8 @@ var storeFactorys = map[string]StorageFactory{ return &redis.Store{} }, }, - "mysql": &SingletonFactory{ - creatorFunction: func() storage.Store { - return &sql.Store{} - }, - }, - "postgres": &SingletonFactory{ - creatorFunction: func() storage.Store { - return &sql.Store{} - }, - }, + "mysql": sqlFac, + "postgres": sqlFac, } // GetStore returns storage.Store From ec72eed9545836bc0b584ccd54ff4f4bd5570da1 Mon Sep 17 00:00:00 2001 From: yedf2 <120050102@qq.com> Date: Thu, 24 Feb 2022 10:45:11 +0800 Subject: [PATCH 03/16] fix lint --- conf.sample.yml | 2 +- dtmsvr/api_json_rpc_http.go | 58 ++++++++++++++++++------------------- dtmsvr/config/config.go | 2 +- dtmsvr/svr.go | 10 +++---- dtmsvr/trans_class.go | 3 +- 5 files changed, 38 insertions(+), 37 deletions(-) diff --git a/conf.sample.yml b/conf.sample.yml index d9052a5..1fd557e 100644 --- a/conf.sample.yml +++ b/conf.sample.yml @@ -56,7 +56,7 @@ # HttpPort: 36789 # GrpcPort: 36790 -# JsonRpcHttp: 36791 +# JSONRPC: 36791 ### advanced options # UpdateBranchAsyncGoroutineNum: 1 # num of async goroutine to update branch status diff --git a/dtmsvr/api_json_rpc_http.go b/dtmsvr/api_json_rpc_http.go index 4f174e4..edfc426 100644 --- a/dtmsvr/api_json_rpc_http.go +++ b/dtmsvr/api_json_rpc_http.go @@ -3,96 +3,96 @@ package dtmsvr import ( "encoding/json" "fmt" + "net/http" + "github.com/dtm-labs/dtm/dtmcli" "github.com/dtm-labs/dtm/dtmcli/logger" "github.com/gin-gonic/gin" - "net/http" ) -type jsonRpcHttpReq struct { +type jsonRPCReq struct { Method string `json:"method"` Jsonrpc string `json:"jsonrpc"` Params interface{} `json:"params"` - Id string `json:"id"` + ID string `json:"id"` } -func addJsonRpcHttpRouter(engine *gin.Engine) { +func addJSONRPCRouter(engine *gin.Engine) { engine.POST("/", dispatcher) } func dispatcher(c *gin.Context) { - req := new(jsonRpcHttpReq) + req := new(jsonRPCReq) err := c.BindJSON(req) logger.Infof("request:%s\n", req) if err != nil { - c.JSON(http.StatusOK, gin.H{"id": req.Id, "result": nil, "error": map[string]interface{}{"code": -32700, "message": "Parse error"}}) + c.JSON(http.StatusOK, gin.H{"id": req.ID, "result": nil, "error": map[string]interface{}{"code": -32700, "message": "Parse error"}}) return } if req.Method == "dtmserver.NewGid" { - res, err := jsonRpcHttpNewGid() - c.JSON(http.StatusOK, gin.H{"id": req.Id, "result": res, "error": err}) + res := jsonRPCNewGid() + c.JSON(http.StatusOK, gin.H{"id": req.ID, "result": res, "error": err}) return } if req.Method == "dtmserver.Prepare" { - res := jsonRpcHttpPrepare(req.Params) - c.JSON(http.StatusOK, gin.H{"id": req.Id, "result": res, "error": nil}) + res := jsonRPCPrepare(req.Params) + c.JSON(http.StatusOK, gin.H{"id": req.ID, "result": res, "error": nil}) return } if req.Method == "dtmserver.Submit" { - res := jsonRpcHttpSubmit(req.Params) - c.JSON(http.StatusOK, gin.H{"id": req.Id, "result": res, "error": nil}) + res := jsonRPCSubmit(req.Params) + c.JSON(http.StatusOK, gin.H{"id": req.ID, "result": res, "error": nil}) return } if req.Method == "dtmserver.Abort" { - res := jsonRpcHttpAbort(req.Params) - c.JSON(http.StatusOK, gin.H{"id": req.Id, "result": res, "error": nil}) + res := jsonRPCAbort(req.Params) + c.JSON(http.StatusOK, gin.H{"id": req.ID, "result": res, "error": nil}) return } if req.Method == "dtmserver.RegisterBranch" { - res := jsonRpcHttpRegisterBranch(req.Params) - c.JSON(http.StatusOK, gin.H{"id": req.Id, "result": res, "error": nil}) + res := jsonRPCRegisterBranch(req.Params) + c.JSON(http.StatusOK, gin.H{"id": req.ID, "result": res, "error": nil}) return } - c.JSON(http.StatusOK, gin.H{"id": req.Id, "result": nil, "error": map[string]interface{}{"code": -32601, "message": "Method not found"}}) - return + c.JSON(http.StatusOK, gin.H{"id": req.ID, "result": nil, "error": map[string]interface{}{"code": -32601, "message": "Method not found"}}) } -func jsonRpcHttpNewGid() (interface{}, error) { - return map[string]interface{}{"gid": GenGid(), "dtm_result": dtmcli.ResultSuccess}, nil +func jsonRPCNewGid() interface{} { + return map[string]interface{}{"gid": GenGid(), "dtm_result": dtmcli.ResultSuccess} } -func jsonRpcHttpPrepare(params interface{}) interface{} { - res := svcPrepare(TransFromJsonRpcHttpContext(params)) +func jsonRPCPrepare(params interface{}) interface{} { + res := svcPrepare(TransFromJSONRPCContext(params)) if res == nil { return map[string]string{"dtm_result": "SUCCESS"} } return map[string]string{"dtm_result": "FAILURE", "message": fmt.Sprintf("%v", res)} } -func jsonRpcHttpSubmit(params interface{}) interface{} { - res := svcSubmit(TransFromJsonRpcHttpContext(params)) +func jsonRPCSubmit(params interface{}) interface{} { + res := svcSubmit(TransFromJSONRPCContext(params)) if res == nil { return map[string]string{"dtm_result": "SUCCESS"} } return map[string]string{"dtm_result": "FAILURE", "message": fmt.Sprintf("%v", res)} } -func jsonRpcHttpAbort(params interface{}) interface{} { - res := svcAbort(TransFromJsonRpcHttpContext(params)) +func jsonRPCAbort(params interface{}) interface{} { + res := svcAbort(TransFromJSONRPCContext(params)) if res == nil { return map[string]string{"dtm_result": "SUCCESS"} } return map[string]string{"dtm_result": "FAILURE", "message": fmt.Sprintf("%v", res)} } -func jsonRpcHttpRegisterBranch(params interface{}) interface{} { +func jsonRPCRegisterBranch(params interface{}) interface{} { data := map[string]string{} - paramsJson, _ := json.Marshal(params) - err := json.Unmarshal(paramsJson, &data) + paramsJSON, _ := json.Marshal(params) + err := json.Unmarshal(paramsJSON, &data) if err != nil { return map[string]string{"dtm_result": "FAILURE", "message": err.Error()} } diff --git a/dtmsvr/config/config.go b/dtmsvr/config/config.go index 4f9ce1c..7f69588 100644 --- a/dtmsvr/config/config.go +++ b/dtmsvr/config/config.go @@ -76,7 +76,7 @@ type configType struct { RequestTimeout int64 `yaml:"RequestTimeout" default:"3"` HTTPPort int64 `yaml:"HttpPort" default:"36789"` GrpcPort int64 `yaml:"GrpcPort" default:"36790"` - JsonRpcHttpPort int64 `yaml:"JsonRpcHttpPort" default:"36791"` + JSONRPCPort int64 `yaml:"JSONRPCPort" default:"36791"` MicroService MicroService `yaml:"MicroService"` UpdateBranchSync int64 `yaml:"UpdateBranchSync"` UpdateBranchAsyncGoroutineNum int64 `yaml:"UpdateBranchAsyncGoroutineNum" default:"1"` diff --git a/dtmsvr/svr.go b/dtmsvr/svr.go index 7989986..62b06d6 100644 --- a/dtmsvr/svr.go +++ b/dtmsvr/svr.go @@ -68,12 +68,12 @@ func StartSvr() { logger.FatalIfError(err) // start json-rpc server - jsonRpcHttpApp := dtmutil.GetGinApp() - jsonRpcHttpApp = httpMetrics(jsonRpcHttpApp) - addJsonRpcHttpRouter(jsonRpcHttpApp) - logger.Infof("dtmsvr listen at: %d", conf.JsonRpcHttpPort) + jsonRPCApp := dtmutil.GetGinApp() + jsonRPCApp = httpMetrics(jsonRPCApp) + addJSONRPCRouter(jsonRPCApp) + logger.Infof("dtmsvr listen at: %d", conf.JSONRPCPort) go func() { - err := jsonRpcHttpApp.Run(fmt.Sprintf(":%d", conf.JsonRpcHttpPort)) + err := jsonRPCApp.Run(fmt.Sprintf(":%d", conf.JSONRPCPort)) if err != nil { logger.Errorf("start server err: %v", err) } diff --git a/dtmsvr/trans_class.go b/dtmsvr/trans_class.go index 8b039b9..48538cc 100644 --- a/dtmsvr/trans_class.go +++ b/dtmsvr/trans_class.go @@ -85,7 +85,8 @@ func TransFromContext(c *gin.Context) *TransGlobal { return &m } -func TransFromJsonRpcHttpContext(params interface{}) *TransGlobal { +// TransFromJSONRPCContext 1 +func TransFromJSONRPCContext(params interface{}) *TransGlobal { jsonStr, _ := json.Marshal(params) m := TransGlobal{} err := json.Unmarshal(jsonStr, &m) From d020368bd1ab7e98ed8a01a10cef227025983759 Mon Sep 17 00:00:00 2001 From: liulei Date: Thu, 24 Feb 2022 12:56:25 +0800 Subject: [PATCH 04/16] feat: transOption support requstTimeout option for grpc request --- dtmgrpc/dtmgimp/utils.go | 11 +++ dtmgrpc/dtmgpb/dtmgimp.pb.go | 150 ++++++++++++++++-------------- dtmgrpc/dtmgpb/dtmgimp.proto | 5 +- dtmgrpc/dtmgpb/dtmgimp_grpc.pb.go | 4 + dtmsvr/trans_class.go | 1 + dtmsvr/trans_status.go | 11 +++ 6 files changed, 110 insertions(+), 72 deletions(-) diff --git a/dtmgrpc/dtmgimp/utils.go b/dtmgrpc/dtmgimp/utils.go index fb3eadf..12c7e8d 100644 --- a/dtmgrpc/dtmgimp/utils.go +++ b/dtmgrpc/dtmgimp/utils.go @@ -8,6 +8,9 @@ package dtmgimp import ( context "context" + "time" + + "google.golang.org/grpc" "github.com/dtm-labs/dtm/dtmcli/dtmimp" "github.com/dtm-labs/dtm/dtmcli/logger" @@ -26,6 +29,13 @@ func MustProtoMarshal(msg proto.Message) []byte { // DtmGrpcCall make a convenient call to dtm func DtmGrpcCall(s *dtmimp.TransBase, operation string) error { + if s.RequestTimeout != 0 { + ClientInterceptors = append(ClientInterceptors, func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error { + ctx2, cancel := context.WithTimeout(ctx, time.Duration(s.RequestTimeout)*time.Second) + defer cancel() + return invoker(ctx2, method, req, reply, cc, opts...) + }) + } reply := emptypb.Empty{} return MustGetGrpcConn(s.Dtm, false).Invoke(context.Background(), "/dtmgimp.Dtm/"+operation, &dtmgpb.DtmRequest{ Gid: s.Gid, @@ -36,6 +46,7 @@ func DtmGrpcCall(s *dtmimp.TransBase, operation string) error { RetryInterval: s.RetryInterval, PassthroughHeaders: s.PassthroughHeaders, BranchHeaders: s.BranchHeaders, + RequestTimeout: s.RequestTimeout, }, QueryPrepared: s.QueryPrepared, CustomedData: s.CustomData, diff --git a/dtmgrpc/dtmgpb/dtmgimp.pb.go b/dtmgrpc/dtmgpb/dtmgimp.pb.go index 9247260..ab8f440 100644 --- a/dtmgrpc/dtmgpb/dtmgimp.pb.go +++ b/dtmgrpc/dtmgpb/dtmgimp.pb.go @@ -1,7 +1,7 @@ // Code generated by protoc-gen-go. DO NOT EDIT. // versions: // protoc-gen-go v1.27.1 -// protoc v3.17.3 +// protoc v3.19.4 // source: dtmgrpc/dtmgpb/dtmgimp.proto package dtmgpb @@ -29,8 +29,9 @@ type DtmTransOptions struct { WaitResult bool `protobuf:"varint,1,opt,name=WaitResult,proto3" json:"WaitResult,omitempty"` TimeoutToFail int64 `protobuf:"varint,2,opt,name=TimeoutToFail,proto3" json:"TimeoutToFail,omitempty"` RetryInterval int64 `protobuf:"varint,3,opt,name=RetryInterval,proto3" json:"RetryInterval,omitempty"` - PassthroughHeaders []string `protobuf:"bytes,4,rep,name=PassthroughHeaders,proto3" json:"PassthroughHeaders,omitempty"` - BranchHeaders map[string]string `protobuf:"bytes,5,rep,name=BranchHeaders,proto3" json:"BranchHeaders,omitempty" protobuf_key:"bytes,1,opt,name=key,proto3" protobuf_val:"bytes,2,opt,name=value,proto3"` + RequestTimeout int64 `protobuf:"varint,4,opt,name=RequestTimeout,proto3" json:"RequestTimeout,omitempty"` + PassthroughHeaders []string `protobuf:"bytes,5,rep,name=PassthroughHeaders,proto3" json:"PassthroughHeaders,omitempty"` + BranchHeaders map[string]string `protobuf:"bytes,6,rep,name=BranchHeaders,proto3" json:"BranchHeaders,omitempty" protobuf_key:"bytes,1,opt,name=key,proto3" protobuf_val:"bytes,2,opt,name=value,proto3"` } func (x *DtmTransOptions) Reset() { @@ -86,6 +87,13 @@ func (x *DtmTransOptions) GetRetryInterval() int64 { return 0 } +func (x *DtmTransOptions) GetRequestTimeout() int64 { + if x != nil { + return x.RequestTimeout + } + return 0 +} + func (x *DtmTransOptions) GetPassthroughHeaders() []string { if x != nil { return x.PassthroughHeaders @@ -337,7 +345,7 @@ var file_dtmgrpc_dtmgpb_dtmgimp_proto_rawDesc = []byte{ 0x2f, 0x64, 0x74, 0x6d, 0x67, 0x69, 0x6d, 0x70, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x07, 0x64, 0x74, 0x6d, 0x67, 0x69, 0x6d, 0x70, 0x1a, 0x1b, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2f, 0x65, 0x6d, 0x70, 0x74, 0x79, 0x2e, 0x70, - 0x72, 0x6f, 0x74, 0x6f, 0x22, 0xc2, 0x02, 0x0a, 0x0f, 0x44, 0x74, 0x6d, 0x54, 0x72, 0x61, 0x6e, + 0x72, 0x6f, 0x74, 0x6f, 0x22, 0xea, 0x02, 0x0a, 0x0f, 0x44, 0x74, 0x6d, 0x54, 0x72, 0x61, 0x6e, 0x73, 0x4f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x12, 0x1e, 0x0a, 0x0a, 0x57, 0x61, 0x69, 0x74, 0x52, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x18, 0x01, 0x20, 0x01, 0x28, 0x08, 0x52, 0x0a, 0x57, 0x61, 0x69, 0x74, 0x52, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x12, 0x24, 0x0a, 0x0d, 0x54, 0x69, 0x6d, 0x65, @@ -345,74 +353,76 @@ var file_dtmgrpc_dtmgpb_dtmgimp_proto_rawDesc = []byte{ 0x0d, 0x54, 0x69, 0x6d, 0x65, 0x6f, 0x75, 0x74, 0x54, 0x6f, 0x46, 0x61, 0x69, 0x6c, 0x12, 0x24, 0x0a, 0x0d, 0x52, 0x65, 0x74, 0x72, 0x79, 0x49, 0x6e, 0x74, 0x65, 0x72, 0x76, 0x61, 0x6c, 0x18, 0x03, 0x20, 0x01, 0x28, 0x03, 0x52, 0x0d, 0x52, 0x65, 0x74, 0x72, 0x79, 0x49, 0x6e, 0x74, 0x65, - 0x72, 0x76, 0x61, 0x6c, 0x12, 0x2e, 0x0a, 0x12, 0x50, 0x61, 0x73, 0x73, 0x74, 0x68, 0x72, 0x6f, - 0x75, 0x67, 0x68, 0x48, 0x65, 0x61, 0x64, 0x65, 0x72, 0x73, 0x18, 0x04, 0x20, 0x03, 0x28, 0x09, - 0x52, 0x12, 0x50, 0x61, 0x73, 0x73, 0x74, 0x68, 0x72, 0x6f, 0x75, 0x67, 0x68, 0x48, 0x65, 0x61, - 0x64, 0x65, 0x72, 0x73, 0x12, 0x51, 0x0a, 0x0d, 0x42, 0x72, 0x61, 0x6e, 0x63, 0x68, 0x48, 0x65, - 0x61, 0x64, 0x65, 0x72, 0x73, 0x18, 0x05, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x2b, 0x2e, 0x64, 0x74, - 0x6d, 0x67, 0x69, 0x6d, 0x70, 0x2e, 0x44, 0x74, 0x6d, 0x54, 0x72, 0x61, 0x6e, 0x73, 0x4f, 0x70, - 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x2e, 0x42, 0x72, 0x61, 0x6e, 0x63, 0x68, 0x48, 0x65, 0x61, 0x64, - 0x65, 0x72, 0x73, 0x45, 0x6e, 0x74, 0x72, 0x79, 0x52, 0x0d, 0x42, 0x72, 0x61, 0x6e, 0x63, 0x68, - 0x48, 0x65, 0x61, 0x64, 0x65, 0x72, 0x73, 0x1a, 0x40, 0x0a, 0x12, 0x42, 0x72, 0x61, 0x6e, 0x63, - 0x68, 0x48, 0x65, 0x61, 0x64, 0x65, 0x72, 0x73, 0x45, 0x6e, 0x74, 0x72, 0x79, 0x12, 0x10, 0x0a, - 0x03, 0x6b, 0x65, 0x79, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x6b, 0x65, 0x79, 0x12, - 0x14, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, - 0x76, 0x61, 0x6c, 0x75, 0x65, 0x3a, 0x02, 0x38, 0x01, 0x22, 0xfc, 0x01, 0x0a, 0x0a, 0x44, 0x74, - 0x6d, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x10, 0x0a, 0x03, 0x47, 0x69, 0x64, 0x18, - 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x47, 0x69, 0x64, 0x12, 0x1c, 0x0a, 0x09, 0x54, 0x72, - 0x61, 0x6e, 0x73, 0x54, 0x79, 0x70, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x09, 0x54, - 0x72, 0x61, 0x6e, 0x73, 0x54, 0x79, 0x70, 0x65, 0x12, 0x3c, 0x0a, 0x0c, 0x54, 0x72, 0x61, 0x6e, - 0x73, 0x4f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x18, - 0x2e, 0x64, 0x74, 0x6d, 0x67, 0x69, 0x6d, 0x70, 0x2e, 0x44, 0x74, 0x6d, 0x54, 0x72, 0x61, 0x6e, - 0x73, 0x4f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x52, 0x0c, 0x54, 0x72, 0x61, 0x6e, 0x73, 0x4f, - 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x12, 0x22, 0x0a, 0x0c, 0x43, 0x75, 0x73, 0x74, 0x6f, 0x6d, - 0x65, 0x64, 0x44, 0x61, 0x74, 0x61, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0c, 0x43, 0x75, - 0x73, 0x74, 0x6f, 0x6d, 0x65, 0x64, 0x44, 0x61, 0x74, 0x61, 0x12, 0x20, 0x0a, 0x0b, 0x42, 0x69, - 0x6e, 0x50, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64, 0x73, 0x18, 0x05, 0x20, 0x03, 0x28, 0x0c, 0x52, - 0x0b, 0x42, 0x69, 0x6e, 0x50, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64, 0x73, 0x12, 0x24, 0x0a, 0x0d, - 0x51, 0x75, 0x65, 0x72, 0x79, 0x50, 0x72, 0x65, 0x70, 0x61, 0x72, 0x65, 0x64, 0x18, 0x06, 0x20, - 0x01, 0x28, 0x09, 0x52, 0x0d, 0x51, 0x75, 0x65, 0x72, 0x79, 0x50, 0x72, 0x65, 0x70, 0x61, 0x72, - 0x65, 0x64, 0x12, 0x14, 0x0a, 0x05, 0x53, 0x74, 0x65, 0x70, 0x73, 0x18, 0x07, 0x20, 0x01, 0x28, - 0x09, 0x52, 0x05, 0x53, 0x74, 0x65, 0x70, 0x73, 0x22, 0x1f, 0x0a, 0x0b, 0x44, 0x74, 0x6d, 0x47, - 0x69, 0x64, 0x52, 0x65, 0x70, 0x6c, 0x79, 0x12, 0x10, 0x0a, 0x03, 0x47, 0x69, 0x64, 0x18, 0x01, - 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x47, 0x69, 0x64, 0x22, 0x82, 0x02, 0x0a, 0x10, 0x44, 0x74, - 0x6d, 0x42, 0x72, 0x61, 0x6e, 0x63, 0x68, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x10, - 0x0a, 0x03, 0x47, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x47, 0x69, 0x64, - 0x12, 0x1c, 0x0a, 0x09, 0x54, 0x72, 0x61, 0x6e, 0x73, 0x54, 0x79, 0x70, 0x65, 0x18, 0x02, 0x20, - 0x01, 0x28, 0x09, 0x52, 0x09, 0x54, 0x72, 0x61, 0x6e, 0x73, 0x54, 0x79, 0x70, 0x65, 0x12, 0x1a, - 0x0a, 0x08, 0x42, 0x72, 0x61, 0x6e, 0x63, 0x68, 0x49, 0x44, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, - 0x52, 0x08, 0x42, 0x72, 0x61, 0x6e, 0x63, 0x68, 0x49, 0x44, 0x12, 0x0e, 0x0a, 0x02, 0x4f, 0x70, - 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, 0x02, 0x4f, 0x70, 0x12, 0x37, 0x0a, 0x04, 0x44, 0x61, - 0x74, 0x61, 0x18, 0x05, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x23, 0x2e, 0x64, 0x74, 0x6d, 0x67, 0x69, - 0x6d, 0x70, 0x2e, 0x44, 0x74, 0x6d, 0x42, 0x72, 0x61, 0x6e, 0x63, 0x68, 0x52, 0x65, 0x71, 0x75, - 0x65, 0x73, 0x74, 0x2e, 0x44, 0x61, 0x74, 0x61, 0x45, 0x6e, 0x74, 0x72, 0x79, 0x52, 0x04, 0x44, - 0x61, 0x74, 0x61, 0x12, 0x20, 0x0a, 0x0b, 0x42, 0x75, 0x73, 0x69, 0x50, 0x61, 0x79, 0x6c, 0x6f, - 0x61, 0x64, 0x18, 0x06, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x0b, 0x42, 0x75, 0x73, 0x69, 0x50, 0x61, - 0x79, 0x6c, 0x6f, 0x61, 0x64, 0x1a, 0x37, 0x0a, 0x09, 0x44, 0x61, 0x74, 0x61, 0x45, 0x6e, 0x74, - 0x72, 0x79, 0x12, 0x10, 0x0a, 0x03, 0x6b, 0x65, 0x79, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, - 0x03, 0x6b, 0x65, 0x79, 0x12, 0x14, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x02, 0x20, - 0x01, 0x28, 0x09, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x3a, 0x02, 0x38, 0x01, 0x32, 0xb1, - 0x02, 0x0a, 0x03, 0x44, 0x74, 0x6d, 0x12, 0x38, 0x0a, 0x06, 0x4e, 0x65, 0x77, 0x47, 0x69, 0x64, - 0x12, 0x16, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, - 0x75, 0x66, 0x2e, 0x45, 0x6d, 0x70, 0x74, 0x79, 0x1a, 0x14, 0x2e, 0x64, 0x74, 0x6d, 0x67, 0x69, - 0x6d, 0x70, 0x2e, 0x44, 0x74, 0x6d, 0x47, 0x69, 0x64, 0x52, 0x65, 0x70, 0x6c, 0x79, 0x22, 0x00, - 0x12, 0x37, 0x0a, 0x06, 0x53, 0x75, 0x62, 0x6d, 0x69, 0x74, 0x12, 0x13, 0x2e, 0x64, 0x74, 0x6d, - 0x67, 0x69, 0x6d, 0x70, 0x2e, 0x44, 0x74, 0x6d, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, - 0x16, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, - 0x66, 0x2e, 0x45, 0x6d, 0x70, 0x74, 0x79, 0x22, 0x00, 0x12, 0x38, 0x0a, 0x07, 0x50, 0x72, 0x65, - 0x70, 0x61, 0x72, 0x65, 0x12, 0x13, 0x2e, 0x64, 0x74, 0x6d, 0x67, 0x69, 0x6d, 0x70, 0x2e, 0x44, - 0x74, 0x6d, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x16, 0x2e, 0x67, 0x6f, 0x6f, 0x67, - 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x45, 0x6d, 0x70, 0x74, - 0x79, 0x22, 0x00, 0x12, 0x36, 0x0a, 0x05, 0x41, 0x62, 0x6f, 0x72, 0x74, 0x12, 0x13, 0x2e, 0x64, + 0x72, 0x76, 0x61, 0x6c, 0x12, 0x26, 0x0a, 0x0e, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x54, + 0x69, 0x6d, 0x65, 0x6f, 0x75, 0x74, 0x18, 0x04, 0x20, 0x01, 0x28, 0x03, 0x52, 0x0e, 0x52, 0x65, + 0x71, 0x75, 0x65, 0x73, 0x74, 0x54, 0x69, 0x6d, 0x65, 0x6f, 0x75, 0x74, 0x12, 0x2e, 0x0a, 0x12, + 0x50, 0x61, 0x73, 0x73, 0x74, 0x68, 0x72, 0x6f, 0x75, 0x67, 0x68, 0x48, 0x65, 0x61, 0x64, 0x65, + 0x72, 0x73, 0x18, 0x05, 0x20, 0x03, 0x28, 0x09, 0x52, 0x12, 0x50, 0x61, 0x73, 0x73, 0x74, 0x68, + 0x72, 0x6f, 0x75, 0x67, 0x68, 0x48, 0x65, 0x61, 0x64, 0x65, 0x72, 0x73, 0x12, 0x51, 0x0a, 0x0d, + 0x42, 0x72, 0x61, 0x6e, 0x63, 0x68, 0x48, 0x65, 0x61, 0x64, 0x65, 0x72, 0x73, 0x18, 0x06, 0x20, + 0x03, 0x28, 0x0b, 0x32, 0x2b, 0x2e, 0x64, 0x74, 0x6d, 0x67, 0x69, 0x6d, 0x70, 0x2e, 0x44, 0x74, + 0x6d, 0x54, 0x72, 0x61, 0x6e, 0x73, 0x4f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x2e, 0x42, 0x72, + 0x61, 0x6e, 0x63, 0x68, 0x48, 0x65, 0x61, 0x64, 0x65, 0x72, 0x73, 0x45, 0x6e, 0x74, 0x72, 0x79, + 0x52, 0x0d, 0x42, 0x72, 0x61, 0x6e, 0x63, 0x68, 0x48, 0x65, 0x61, 0x64, 0x65, 0x72, 0x73, 0x1a, + 0x40, 0x0a, 0x12, 0x42, 0x72, 0x61, 0x6e, 0x63, 0x68, 0x48, 0x65, 0x61, 0x64, 0x65, 0x72, 0x73, + 0x45, 0x6e, 0x74, 0x72, 0x79, 0x12, 0x10, 0x0a, 0x03, 0x6b, 0x65, 0x79, 0x18, 0x01, 0x20, 0x01, + 0x28, 0x09, 0x52, 0x03, 0x6b, 0x65, 0x79, 0x12, 0x14, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, + 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x3a, 0x02, 0x38, + 0x01, 0x22, 0xfc, 0x01, 0x0a, 0x0a, 0x44, 0x74, 0x6d, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, + 0x12, 0x10, 0x0a, 0x03, 0x47, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x47, + 0x69, 0x64, 0x12, 0x1c, 0x0a, 0x09, 0x54, 0x72, 0x61, 0x6e, 0x73, 0x54, 0x79, 0x70, 0x65, 0x18, + 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x09, 0x54, 0x72, 0x61, 0x6e, 0x73, 0x54, 0x79, 0x70, 0x65, + 0x12, 0x3c, 0x0a, 0x0c, 0x54, 0x72, 0x61, 0x6e, 0x73, 0x4f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, + 0x18, 0x03, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x18, 0x2e, 0x64, 0x74, 0x6d, 0x67, 0x69, 0x6d, 0x70, + 0x2e, 0x44, 0x74, 0x6d, 0x54, 0x72, 0x61, 0x6e, 0x73, 0x4f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, + 0x52, 0x0c, 0x54, 0x72, 0x61, 0x6e, 0x73, 0x4f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x12, 0x22, + 0x0a, 0x0c, 0x43, 0x75, 0x73, 0x74, 0x6f, 0x6d, 0x65, 0x64, 0x44, 0x61, 0x74, 0x61, 0x18, 0x04, + 0x20, 0x01, 0x28, 0x09, 0x52, 0x0c, 0x43, 0x75, 0x73, 0x74, 0x6f, 0x6d, 0x65, 0x64, 0x44, 0x61, + 0x74, 0x61, 0x12, 0x20, 0x0a, 0x0b, 0x42, 0x69, 0x6e, 0x50, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64, + 0x73, 0x18, 0x05, 0x20, 0x03, 0x28, 0x0c, 0x52, 0x0b, 0x42, 0x69, 0x6e, 0x50, 0x61, 0x79, 0x6c, + 0x6f, 0x61, 0x64, 0x73, 0x12, 0x24, 0x0a, 0x0d, 0x51, 0x75, 0x65, 0x72, 0x79, 0x50, 0x72, 0x65, + 0x70, 0x61, 0x72, 0x65, 0x64, 0x18, 0x06, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0d, 0x51, 0x75, 0x65, + 0x72, 0x79, 0x50, 0x72, 0x65, 0x70, 0x61, 0x72, 0x65, 0x64, 0x12, 0x14, 0x0a, 0x05, 0x53, 0x74, + 0x65, 0x70, 0x73, 0x18, 0x07, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x53, 0x74, 0x65, 0x70, 0x73, + 0x22, 0x1f, 0x0a, 0x0b, 0x44, 0x74, 0x6d, 0x47, 0x69, 0x64, 0x52, 0x65, 0x70, 0x6c, 0x79, 0x12, + 0x10, 0x0a, 0x03, 0x47, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x47, 0x69, + 0x64, 0x22, 0x82, 0x02, 0x0a, 0x10, 0x44, 0x74, 0x6d, 0x42, 0x72, 0x61, 0x6e, 0x63, 0x68, 0x52, + 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x10, 0x0a, 0x03, 0x47, 0x69, 0x64, 0x18, 0x01, 0x20, + 0x01, 0x28, 0x09, 0x52, 0x03, 0x47, 0x69, 0x64, 0x12, 0x1c, 0x0a, 0x09, 0x54, 0x72, 0x61, 0x6e, + 0x73, 0x54, 0x79, 0x70, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x09, 0x54, 0x72, 0x61, + 0x6e, 0x73, 0x54, 0x79, 0x70, 0x65, 0x12, 0x1a, 0x0a, 0x08, 0x42, 0x72, 0x61, 0x6e, 0x63, 0x68, + 0x49, 0x44, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x42, 0x72, 0x61, 0x6e, 0x63, 0x68, + 0x49, 0x44, 0x12, 0x0e, 0x0a, 0x02, 0x4f, 0x70, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, 0x02, + 0x4f, 0x70, 0x12, 0x37, 0x0a, 0x04, 0x44, 0x61, 0x74, 0x61, 0x18, 0x05, 0x20, 0x03, 0x28, 0x0b, + 0x32, 0x23, 0x2e, 0x64, 0x74, 0x6d, 0x67, 0x69, 0x6d, 0x70, 0x2e, 0x44, 0x74, 0x6d, 0x42, 0x72, + 0x61, 0x6e, 0x63, 0x68, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x2e, 0x44, 0x61, 0x74, 0x61, + 0x45, 0x6e, 0x74, 0x72, 0x79, 0x52, 0x04, 0x44, 0x61, 0x74, 0x61, 0x12, 0x20, 0x0a, 0x0b, 0x42, + 0x75, 0x73, 0x69, 0x50, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64, 0x18, 0x06, 0x20, 0x01, 0x28, 0x0c, + 0x52, 0x0b, 0x42, 0x75, 0x73, 0x69, 0x50, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64, 0x1a, 0x37, 0x0a, + 0x09, 0x44, 0x61, 0x74, 0x61, 0x45, 0x6e, 0x74, 0x72, 0x79, 0x12, 0x10, 0x0a, 0x03, 0x6b, 0x65, + 0x79, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x6b, 0x65, 0x79, 0x12, 0x14, 0x0a, 0x05, + 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x76, 0x61, 0x6c, + 0x75, 0x65, 0x3a, 0x02, 0x38, 0x01, 0x32, 0xb1, 0x02, 0x0a, 0x03, 0x44, 0x74, 0x6d, 0x12, 0x38, + 0x0a, 0x06, 0x4e, 0x65, 0x77, 0x47, 0x69, 0x64, 0x12, 0x16, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, + 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x45, 0x6d, 0x70, 0x74, 0x79, + 0x1a, 0x14, 0x2e, 0x64, 0x74, 0x6d, 0x67, 0x69, 0x6d, 0x70, 0x2e, 0x44, 0x74, 0x6d, 0x47, 0x69, + 0x64, 0x52, 0x65, 0x70, 0x6c, 0x79, 0x22, 0x00, 0x12, 0x37, 0x0a, 0x06, 0x53, 0x75, 0x62, 0x6d, + 0x69, 0x74, 0x12, 0x13, 0x2e, 0x64, 0x74, 0x6d, 0x67, 0x69, 0x6d, 0x70, 0x2e, 0x44, 0x74, 0x6d, + 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x16, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, + 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x45, 0x6d, 0x70, 0x74, 0x79, 0x22, + 0x00, 0x12, 0x38, 0x0a, 0x07, 0x50, 0x72, 0x65, 0x70, 0x61, 0x72, 0x65, 0x12, 0x13, 0x2e, 0x64, 0x74, 0x6d, 0x67, 0x69, 0x6d, 0x70, 0x2e, 0x44, 0x74, 0x6d, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x16, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, - 0x62, 0x75, 0x66, 0x2e, 0x45, 0x6d, 0x70, 0x74, 0x79, 0x22, 0x00, 0x12, 0x45, 0x0a, 0x0e, 0x52, - 0x65, 0x67, 0x69, 0x73, 0x74, 0x65, 0x72, 0x42, 0x72, 0x61, 0x6e, 0x63, 0x68, 0x12, 0x19, 0x2e, - 0x64, 0x74, 0x6d, 0x67, 0x69, 0x6d, 0x70, 0x2e, 0x44, 0x74, 0x6d, 0x42, 0x72, 0x61, 0x6e, 0x63, - 0x68, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x16, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, - 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x45, 0x6d, 0x70, 0x74, 0x79, - 0x22, 0x00, 0x42, 0x0a, 0x5a, 0x08, 0x2e, 0x2f, 0x64, 0x74, 0x6d, 0x67, 0x70, 0x62, 0x62, 0x06, - 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x62, 0x75, 0x66, 0x2e, 0x45, 0x6d, 0x70, 0x74, 0x79, 0x22, 0x00, 0x12, 0x36, 0x0a, 0x05, 0x41, + 0x62, 0x6f, 0x72, 0x74, 0x12, 0x13, 0x2e, 0x64, 0x74, 0x6d, 0x67, 0x69, 0x6d, 0x70, 0x2e, 0x44, + 0x74, 0x6d, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x16, 0x2e, 0x67, 0x6f, 0x6f, 0x67, + 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x45, 0x6d, 0x70, 0x74, + 0x79, 0x22, 0x00, 0x12, 0x45, 0x0a, 0x0e, 0x52, 0x65, 0x67, 0x69, 0x73, 0x74, 0x65, 0x72, 0x42, + 0x72, 0x61, 0x6e, 0x63, 0x68, 0x12, 0x19, 0x2e, 0x64, 0x74, 0x6d, 0x67, 0x69, 0x6d, 0x70, 0x2e, + 0x44, 0x74, 0x6d, 0x42, 0x72, 0x61, 0x6e, 0x63, 0x68, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, + 0x1a, 0x16, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, + 0x75, 0x66, 0x2e, 0x45, 0x6d, 0x70, 0x74, 0x79, 0x22, 0x00, 0x42, 0x0a, 0x5a, 0x08, 0x2e, 0x2f, + 0x64, 0x74, 0x6d, 0x67, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( diff --git a/dtmgrpc/dtmgpb/dtmgimp.proto b/dtmgrpc/dtmgpb/dtmgimp.proto index f97b277..5fc3aa7 100644 --- a/dtmgrpc/dtmgpb/dtmgimp.proto +++ b/dtmgrpc/dtmgpb/dtmgimp.proto @@ -18,8 +18,9 @@ message DtmTransOptions { bool WaitResult = 1; int64 TimeoutToFail = 2; int64 RetryInterval = 3; - repeated string PassthroughHeaders = 4; - map BranchHeaders = 5; + int64 RequestTimeout = 4; + repeated string PassthroughHeaders = 5; + map BranchHeaders = 6; } // DtmRequest request sent to dtm server diff --git a/dtmgrpc/dtmgpb/dtmgimp_grpc.pb.go b/dtmgrpc/dtmgpb/dtmgimp_grpc.pb.go index 8381443..db22f19 100644 --- a/dtmgrpc/dtmgpb/dtmgimp_grpc.pb.go +++ b/dtmgrpc/dtmgpb/dtmgimp_grpc.pb.go @@ -1,4 +1,8 @@ // Code generated by protoc-gen-go-grpc. DO NOT EDIT. +// versions: +// - protoc-gen-go-grpc v1.2.0 +// - protoc v3.19.4 +// source: dtmgrpc/dtmgpb/dtmgimp.proto package dtmgpb diff --git a/dtmsvr/trans_class.go b/dtmsvr/trans_class.go index b6e080b..8aa1b60 100644 --- a/dtmsvr/trans_class.go +++ b/dtmsvr/trans_class.go @@ -103,6 +103,7 @@ func TransFromDtmRequest(ctx context.Context, c *dtmgpb.DtmRequest) *TransGlobal RetryInterval: o.RetryInterval, PassthroughHeaders: o.PassthroughHeaders, BranchHeaders: o.BranchHeaders, + RequestTimeout: o.RequestTimeout, }, }} if c.Steps != "" { diff --git a/dtmsvr/trans_status.go b/dtmsvr/trans_status.go index d2fd8dc..430df7c 100644 --- a/dtmsvr/trans_status.go +++ b/dtmsvr/trans_status.go @@ -7,11 +7,14 @@ package dtmsvr import ( + "context" "errors" "fmt" "strings" "time" + "google.golang.org/grpc" + "github.com/dtm-labs/dtm/dtmcli" "github.com/dtm-labs/dtm/dtmcli/dtmimp" "github.com/dtm-labs/dtm/dtmcli/logger" @@ -102,6 +105,14 @@ func (t *TransGlobal) getURLResult(url string, branchID, op string, branchPayloa if err != nil { return err } + if t.RequestTimeout != 0 { + // use ClientInterceptors[1:] for remove default request setting + dtmgimp.ClientInterceptors = append(dtmgimp.ClientInterceptors[1:], func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error { + ctx2, cancel := context.WithTimeout(ctx, time.Duration(t.RequestTimeout)*time.Second) + defer cancel() + return invoker(ctx2, method, req, reply, cc, opts...) + }) + } conn := dtmgimp.MustGetGrpcConn(server, true) ctx := dtmgimp.TransInfo2Ctx(t.Gid, t.TransType, branchID, op, "") kvs := dtmgimp.Map2Kvs(t.Ext.Headers) From 153fd442744d82a93320e60c905e59f30c6caf2a Mon Sep 17 00:00:00 2001 From: liulei Date: Thu, 24 Feb 2022 12:57:20 +0800 Subject: [PATCH 05/16] test: add test for grpc request support request timeout option --- test/busi/base_grpc.go | 4 ++ test/busi/busi.pb.go | 143 ++++++++++++++++++++------------------ test/busi/busi.proto | 1 + test/busi/busi_grpc.pb.go | 40 +++++++++++ test/saga_grpc_test.go | 11 +++ 5 files changed, 131 insertions(+), 68 deletions(-) diff --git a/test/busi/base_grpc.go b/test/busi/base_grpc.go index 682c37d..fe5d255 100644 --- a/test/busi/base_grpc.go +++ b/test/busi/base_grpc.go @@ -128,6 +128,10 @@ func (s *busiServer) XaNotify(ctx context.Context, in *emptypb.Empty) (*emptypb. return XaGrpcClient.HandleCallback(ctx) } +func (s *busiServer) TransOutWithGlobalRequestTimeout(ctx context.Context, in *BusiReq) (*emptypb.Empty, error) { + return &emptypb.Empty{}, handleGrpcBusiness(in, MainSwitch.TransOutResult.Fetch(), in.TransOutResult, dtmimp.GetFuncName()) +} + func (s *busiServer) TransOutHeaderYes(ctx context.Context, in *BusiReq) (*emptypb.Empty, error) { meta := dtmgimp.GetMetaFromContext(ctx, "test_header") if meta == "" { diff --git a/test/busi/busi.pb.go b/test/busi/busi.pb.go index 09b8d85..e9c31f6 100644 --- a/test/busi/busi.pb.go +++ b/test/busi/busi.pb.go @@ -1,7 +1,7 @@ // Code generated by protoc-gen-go. DO NOT EDIT. // versions: // protoc-gen-go v1.27.1 -// protoc v3.17.3 +// protoc v3.19.4 // source: test/busi/busi.proto package busi @@ -148,7 +148,7 @@ var file_test_busi_busi_proto_rawDesc = []byte{ 0x6e, 0x73, 0x49, 0x6e, 0x52, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x22, 0x25, 0x0a, 0x09, 0x42, 0x75, 0x73, 0x69, 0x52, 0x65, 0x70, 0x6c, 0x79, 0x12, 0x18, 0x0a, 0x07, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, - 0x65, 0x32, 0xbe, 0x0b, 0x0a, 0x04, 0x42, 0x75, 0x73, 0x69, 0x12, 0x32, 0x0a, 0x07, 0x54, 0x72, + 0x65, 0x32, 0x8b, 0x0c, 0x0a, 0x04, 0x42, 0x75, 0x73, 0x69, 0x12, 0x32, 0x0a, 0x07, 0x54, 0x72, 0x61, 0x6e, 0x73, 0x49, 0x6e, 0x12, 0x0d, 0x2e, 0x62, 0x75, 0x73, 0x69, 0x2e, 0x42, 0x75, 0x73, 0x69, 0x52, 0x65, 0x71, 0x1a, 0x16, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x45, 0x6d, 0x70, 0x74, 0x79, 0x22, 0x00, 0x12, 0x33, @@ -206,42 +206,47 @@ var file_test_busi_busi_proto_rawDesc = []byte{ 0x73, 0x4f, 0x75, 0x74, 0x52, 0x65, 0x76, 0x65, 0x72, 0x74, 0x42, 0x53, 0x61, 0x67, 0x61, 0x12, 0x0d, 0x2e, 0x62, 0x75, 0x73, 0x69, 0x2e, 0x42, 0x75, 0x73, 0x69, 0x52, 0x65, 0x71, 0x1a, 0x16, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, - 0x2e, 0x45, 0x6d, 0x70, 0x74, 0x79, 0x22, 0x00, 0x12, 0x3c, 0x0a, 0x11, 0x54, 0x72, 0x61, 0x6e, - 0x73, 0x4f, 0x75, 0x74, 0x48, 0x65, 0x61, 0x64, 0x65, 0x72, 0x59, 0x65, 0x73, 0x12, 0x0d, 0x2e, - 0x62, 0x75, 0x73, 0x69, 0x2e, 0x42, 0x75, 0x73, 0x69, 0x52, 0x65, 0x71, 0x1a, 0x16, 0x2e, 0x67, - 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x45, - 0x6d, 0x70, 0x74, 0x79, 0x22, 0x00, 0x12, 0x3b, 0x0a, 0x10, 0x54, 0x72, 0x61, 0x6e, 0x73, 0x4f, - 0x75, 0x74, 0x48, 0x65, 0x61, 0x64, 0x65, 0x72, 0x4e, 0x6f, 0x12, 0x0d, 0x2e, 0x62, 0x75, 0x73, - 0x69, 0x2e, 0x42, 0x75, 0x73, 0x69, 0x52, 0x65, 0x71, 0x1a, 0x16, 0x2e, 0x67, 0x6f, 0x6f, 0x67, - 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x45, 0x6d, 0x70, 0x74, - 0x79, 0x22, 0x00, 0x12, 0x37, 0x0a, 0x0c, 0x54, 0x72, 0x61, 0x6e, 0x73, 0x49, 0x6e, 0x52, 0x65, - 0x64, 0x69, 0x73, 0x12, 0x0d, 0x2e, 0x62, 0x75, 0x73, 0x69, 0x2e, 0x42, 0x75, 0x73, 0x69, 0x52, - 0x65, 0x71, 0x1a, 0x16, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, - 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x45, 0x6d, 0x70, 0x74, 0x79, 0x22, 0x00, 0x12, 0x38, 0x0a, 0x0d, - 0x54, 0x72, 0x61, 0x6e, 0x73, 0x4f, 0x75, 0x74, 0x52, 0x65, 0x64, 0x69, 0x73, 0x12, 0x0d, 0x2e, - 0x62, 0x75, 0x73, 0x69, 0x2e, 0x42, 0x75, 0x73, 0x69, 0x52, 0x65, 0x71, 0x1a, 0x16, 0x2e, 0x67, - 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x45, - 0x6d, 0x70, 0x74, 0x79, 0x22, 0x00, 0x12, 0x3d, 0x0a, 0x12, 0x54, 0x72, 0x61, 0x6e, 0x73, 0x49, - 0x6e, 0x52, 0x65, 0x76, 0x65, 0x72, 0x74, 0x52, 0x65, 0x64, 0x69, 0x73, 0x12, 0x0d, 0x2e, 0x62, - 0x75, 0x73, 0x69, 0x2e, 0x42, 0x75, 0x73, 0x69, 0x52, 0x65, 0x71, 0x1a, 0x16, 0x2e, 0x67, 0x6f, - 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x45, 0x6d, - 0x70, 0x74, 0x79, 0x22, 0x00, 0x12, 0x3e, 0x0a, 0x13, 0x54, 0x72, 0x61, 0x6e, 0x73, 0x4f, 0x75, - 0x74, 0x52, 0x65, 0x76, 0x65, 0x72, 0x74, 0x52, 0x65, 0x64, 0x69, 0x73, 0x12, 0x0d, 0x2e, 0x62, + 0x2e, 0x45, 0x6d, 0x70, 0x74, 0x79, 0x22, 0x00, 0x12, 0x4b, 0x0a, 0x20, 0x54, 0x72, 0x61, 0x6e, + 0x73, 0x4f, 0x75, 0x74, 0x57, 0x69, 0x74, 0x68, 0x47, 0x6c, 0x6f, 0x62, 0x61, 0x6c, 0x52, 0x65, + 0x71, 0x75, 0x65, 0x73, 0x74, 0x54, 0x69, 0x6d, 0x65, 0x6f, 0x75, 0x74, 0x12, 0x0d, 0x2e, 0x62, 0x75, 0x73, 0x69, 0x2e, 0x42, 0x75, 0x73, 0x69, 0x52, 0x65, 0x71, 0x1a, 0x16, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x45, 0x6d, - 0x70, 0x74, 0x79, 0x22, 0x00, 0x12, 0x31, 0x0a, 0x0d, 0x51, 0x75, 0x65, 0x72, 0x79, 0x50, 0x72, - 0x65, 0x70, 0x61, 0x72, 0x65, 0x64, 0x12, 0x0d, 0x2e, 0x62, 0x75, 0x73, 0x69, 0x2e, 0x42, 0x75, - 0x73, 0x69, 0x52, 0x65, 0x71, 0x1a, 0x0f, 0x2e, 0x62, 0x75, 0x73, 0x69, 0x2e, 0x42, 0x75, 0x73, - 0x69, 0x52, 0x65, 0x70, 0x6c, 0x79, 0x22, 0x00, 0x12, 0x39, 0x0a, 0x0e, 0x51, 0x75, 0x65, 0x72, - 0x79, 0x50, 0x72, 0x65, 0x70, 0x61, 0x72, 0x65, 0x64, 0x42, 0x12, 0x0d, 0x2e, 0x62, 0x75, 0x73, + 0x70, 0x74, 0x79, 0x22, 0x00, 0x12, 0x3c, 0x0a, 0x11, 0x54, 0x72, 0x61, 0x6e, 0x73, 0x4f, 0x75, + 0x74, 0x48, 0x65, 0x61, 0x64, 0x65, 0x72, 0x59, 0x65, 0x73, 0x12, 0x0d, 0x2e, 0x62, 0x75, 0x73, 0x69, 0x2e, 0x42, 0x75, 0x73, 0x69, 0x52, 0x65, 0x71, 0x1a, 0x16, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x45, 0x6d, 0x70, 0x74, - 0x79, 0x22, 0x00, 0x12, 0x3d, 0x0a, 0x12, 0x51, 0x75, 0x65, 0x72, 0x79, 0x50, 0x72, 0x65, 0x70, - 0x61, 0x72, 0x65, 0x64, 0x52, 0x65, 0x64, 0x69, 0x73, 0x12, 0x0d, 0x2e, 0x62, 0x75, 0x73, 0x69, + 0x79, 0x22, 0x00, 0x12, 0x3b, 0x0a, 0x10, 0x54, 0x72, 0x61, 0x6e, 0x73, 0x4f, 0x75, 0x74, 0x48, + 0x65, 0x61, 0x64, 0x65, 0x72, 0x4e, 0x6f, 0x12, 0x0d, 0x2e, 0x62, 0x75, 0x73, 0x69, 0x2e, 0x42, + 0x75, 0x73, 0x69, 0x52, 0x65, 0x71, 0x1a, 0x16, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, + 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x45, 0x6d, 0x70, 0x74, 0x79, 0x22, 0x00, + 0x12, 0x37, 0x0a, 0x0c, 0x54, 0x72, 0x61, 0x6e, 0x73, 0x49, 0x6e, 0x52, 0x65, 0x64, 0x69, 0x73, + 0x12, 0x0d, 0x2e, 0x62, 0x75, 0x73, 0x69, 0x2e, 0x42, 0x75, 0x73, 0x69, 0x52, 0x65, 0x71, 0x1a, + 0x16, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, + 0x66, 0x2e, 0x45, 0x6d, 0x70, 0x74, 0x79, 0x22, 0x00, 0x12, 0x38, 0x0a, 0x0d, 0x54, 0x72, 0x61, + 0x6e, 0x73, 0x4f, 0x75, 0x74, 0x52, 0x65, 0x64, 0x69, 0x73, 0x12, 0x0d, 0x2e, 0x62, 0x75, 0x73, + 0x69, 0x2e, 0x42, 0x75, 0x73, 0x69, 0x52, 0x65, 0x71, 0x1a, 0x16, 0x2e, 0x67, 0x6f, 0x6f, 0x67, + 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x45, 0x6d, 0x70, 0x74, + 0x79, 0x22, 0x00, 0x12, 0x3d, 0x0a, 0x12, 0x54, 0x72, 0x61, 0x6e, 0x73, 0x49, 0x6e, 0x52, 0x65, + 0x76, 0x65, 0x72, 0x74, 0x52, 0x65, 0x64, 0x69, 0x73, 0x12, 0x0d, 0x2e, 0x62, 0x75, 0x73, 0x69, 0x2e, 0x42, 0x75, 0x73, 0x69, 0x52, 0x65, 0x71, 0x1a, 0x16, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x45, 0x6d, 0x70, 0x74, 0x79, - 0x22, 0x00, 0x42, 0x08, 0x5a, 0x06, 0x2e, 0x2f, 0x62, 0x75, 0x73, 0x69, 0x62, 0x06, 0x70, 0x72, - 0x6f, 0x74, 0x6f, 0x33, + 0x22, 0x00, 0x12, 0x3e, 0x0a, 0x13, 0x54, 0x72, 0x61, 0x6e, 0x73, 0x4f, 0x75, 0x74, 0x52, 0x65, + 0x76, 0x65, 0x72, 0x74, 0x52, 0x65, 0x64, 0x69, 0x73, 0x12, 0x0d, 0x2e, 0x62, 0x75, 0x73, 0x69, + 0x2e, 0x42, 0x75, 0x73, 0x69, 0x52, 0x65, 0x71, 0x1a, 0x16, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, + 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x45, 0x6d, 0x70, 0x74, 0x79, + 0x22, 0x00, 0x12, 0x31, 0x0a, 0x0d, 0x51, 0x75, 0x65, 0x72, 0x79, 0x50, 0x72, 0x65, 0x70, 0x61, + 0x72, 0x65, 0x64, 0x12, 0x0d, 0x2e, 0x62, 0x75, 0x73, 0x69, 0x2e, 0x42, 0x75, 0x73, 0x69, 0x52, + 0x65, 0x71, 0x1a, 0x0f, 0x2e, 0x62, 0x75, 0x73, 0x69, 0x2e, 0x42, 0x75, 0x73, 0x69, 0x52, 0x65, + 0x70, 0x6c, 0x79, 0x22, 0x00, 0x12, 0x39, 0x0a, 0x0e, 0x51, 0x75, 0x65, 0x72, 0x79, 0x50, 0x72, + 0x65, 0x70, 0x61, 0x72, 0x65, 0x64, 0x42, 0x12, 0x0d, 0x2e, 0x62, 0x75, 0x73, 0x69, 0x2e, 0x42, + 0x75, 0x73, 0x69, 0x52, 0x65, 0x71, 0x1a, 0x16, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, + 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x45, 0x6d, 0x70, 0x74, 0x79, 0x22, 0x00, + 0x12, 0x3d, 0x0a, 0x12, 0x51, 0x75, 0x65, 0x72, 0x79, 0x50, 0x72, 0x65, 0x70, 0x61, 0x72, 0x65, + 0x64, 0x52, 0x65, 0x64, 0x69, 0x73, 0x12, 0x0d, 0x2e, 0x62, 0x75, 0x73, 0x69, 0x2e, 0x42, 0x75, + 0x73, 0x69, 0x52, 0x65, 0x71, 0x1a, 0x16, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, + 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x45, 0x6d, 0x70, 0x74, 0x79, 0x22, 0x00, 0x42, + 0x08, 0x5a, 0x06, 0x2e, 0x2f, 0x62, 0x75, 0x73, 0x69, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, + 0x33, } var ( @@ -279,42 +284,44 @@ var file_test_busi_busi_proto_depIdxs = []int32{ 0, // 13: busi.Busi.TransOutBSaga:input_type -> busi.BusiReq 0, // 14: busi.Busi.TransInRevertBSaga:input_type -> busi.BusiReq 0, // 15: busi.Busi.TransOutRevertBSaga:input_type -> busi.BusiReq - 0, // 16: busi.Busi.TransOutHeaderYes:input_type -> busi.BusiReq - 0, // 17: busi.Busi.TransOutHeaderNo:input_type -> busi.BusiReq - 0, // 18: busi.Busi.TransInRedis:input_type -> busi.BusiReq - 0, // 19: busi.Busi.TransOutRedis:input_type -> busi.BusiReq - 0, // 20: busi.Busi.TransInRevertRedis:input_type -> busi.BusiReq - 0, // 21: busi.Busi.TransOutRevertRedis:input_type -> busi.BusiReq - 0, // 22: busi.Busi.QueryPrepared:input_type -> busi.BusiReq - 0, // 23: busi.Busi.QueryPreparedB:input_type -> busi.BusiReq - 0, // 24: busi.Busi.QueryPreparedRedis:input_type -> busi.BusiReq - 2, // 25: busi.Busi.TransIn:output_type -> google.protobuf.Empty - 2, // 26: busi.Busi.TransOut:output_type -> google.protobuf.Empty - 2, // 27: busi.Busi.TransInRevert:output_type -> google.protobuf.Empty - 2, // 28: busi.Busi.TransOutRevert:output_type -> google.protobuf.Empty - 2, // 29: busi.Busi.TransInConfirm:output_type -> google.protobuf.Empty - 2, // 30: busi.Busi.TransOutConfirm:output_type -> google.protobuf.Empty - 2, // 31: busi.Busi.XaNotify:output_type -> google.protobuf.Empty - 2, // 32: busi.Busi.TransInXa:output_type -> google.protobuf.Empty - 2, // 33: busi.Busi.TransOutXa:output_type -> google.protobuf.Empty - 2, // 34: busi.Busi.TransInTcc:output_type -> google.protobuf.Empty - 2, // 35: busi.Busi.TransOutTcc:output_type -> google.protobuf.Empty - 2, // 36: busi.Busi.TransInTccNested:output_type -> google.protobuf.Empty - 2, // 37: busi.Busi.TransInBSaga:output_type -> google.protobuf.Empty - 2, // 38: busi.Busi.TransOutBSaga:output_type -> google.protobuf.Empty - 2, // 39: busi.Busi.TransInRevertBSaga:output_type -> google.protobuf.Empty - 2, // 40: busi.Busi.TransOutRevertBSaga:output_type -> google.protobuf.Empty - 2, // 41: busi.Busi.TransOutHeaderYes:output_type -> google.protobuf.Empty - 2, // 42: busi.Busi.TransOutHeaderNo:output_type -> google.protobuf.Empty - 2, // 43: busi.Busi.TransInRedis:output_type -> google.protobuf.Empty - 2, // 44: busi.Busi.TransOutRedis:output_type -> google.protobuf.Empty - 2, // 45: busi.Busi.TransInRevertRedis:output_type -> google.protobuf.Empty - 2, // 46: busi.Busi.TransOutRevertRedis:output_type -> google.protobuf.Empty - 1, // 47: busi.Busi.QueryPrepared:output_type -> busi.BusiReply - 2, // 48: busi.Busi.QueryPreparedB:output_type -> google.protobuf.Empty - 2, // 49: busi.Busi.QueryPreparedRedis:output_type -> google.protobuf.Empty - 25, // [25:50] is the sub-list for method output_type - 0, // [0:25] is the sub-list for method input_type + 0, // 16: busi.Busi.TransOutWithGlobalRequestTimeout:input_type -> busi.BusiReq + 0, // 17: busi.Busi.TransOutHeaderYes:input_type -> busi.BusiReq + 0, // 18: busi.Busi.TransOutHeaderNo:input_type -> busi.BusiReq + 0, // 19: busi.Busi.TransInRedis:input_type -> busi.BusiReq + 0, // 20: busi.Busi.TransOutRedis:input_type -> busi.BusiReq + 0, // 21: busi.Busi.TransInRevertRedis:input_type -> busi.BusiReq + 0, // 22: busi.Busi.TransOutRevertRedis:input_type -> busi.BusiReq + 0, // 23: busi.Busi.QueryPrepared:input_type -> busi.BusiReq + 0, // 24: busi.Busi.QueryPreparedB:input_type -> busi.BusiReq + 0, // 25: busi.Busi.QueryPreparedRedis:input_type -> busi.BusiReq + 2, // 26: busi.Busi.TransIn:output_type -> google.protobuf.Empty + 2, // 27: busi.Busi.TransOut:output_type -> google.protobuf.Empty + 2, // 28: busi.Busi.TransInRevert:output_type -> google.protobuf.Empty + 2, // 29: busi.Busi.TransOutRevert:output_type -> google.protobuf.Empty + 2, // 30: busi.Busi.TransInConfirm:output_type -> google.protobuf.Empty + 2, // 31: busi.Busi.TransOutConfirm:output_type -> google.protobuf.Empty + 2, // 32: busi.Busi.XaNotify:output_type -> google.protobuf.Empty + 2, // 33: busi.Busi.TransInXa:output_type -> google.protobuf.Empty + 2, // 34: busi.Busi.TransOutXa:output_type -> google.protobuf.Empty + 2, // 35: busi.Busi.TransInTcc:output_type -> google.protobuf.Empty + 2, // 36: busi.Busi.TransOutTcc:output_type -> google.protobuf.Empty + 2, // 37: busi.Busi.TransInTccNested:output_type -> google.protobuf.Empty + 2, // 38: busi.Busi.TransInBSaga:output_type -> google.protobuf.Empty + 2, // 39: busi.Busi.TransOutBSaga:output_type -> google.protobuf.Empty + 2, // 40: busi.Busi.TransInRevertBSaga:output_type -> google.protobuf.Empty + 2, // 41: busi.Busi.TransOutRevertBSaga:output_type -> google.protobuf.Empty + 2, // 42: busi.Busi.TransOutWithGlobalRequestTimeout:output_type -> google.protobuf.Empty + 2, // 43: busi.Busi.TransOutHeaderYes:output_type -> google.protobuf.Empty + 2, // 44: busi.Busi.TransOutHeaderNo:output_type -> google.protobuf.Empty + 2, // 45: busi.Busi.TransInRedis:output_type -> google.protobuf.Empty + 2, // 46: busi.Busi.TransOutRedis:output_type -> google.protobuf.Empty + 2, // 47: busi.Busi.TransInRevertRedis:output_type -> google.protobuf.Empty + 2, // 48: busi.Busi.TransOutRevertRedis:output_type -> google.protobuf.Empty + 1, // 49: busi.Busi.QueryPrepared:output_type -> busi.BusiReply + 2, // 50: busi.Busi.QueryPreparedB:output_type -> google.protobuf.Empty + 2, // 51: busi.Busi.QueryPreparedRedis:output_type -> google.protobuf.Empty + 26, // [26:52] is the sub-list for method output_type + 0, // [0:26] is the sub-list for method input_type 0, // [0:0] is the sub-list for extension type_name 0, // [0:0] is the sub-list for extension extendee 0, // [0:0] is the sub-list for field type_name diff --git a/test/busi/busi.proto b/test/busi/busi.proto index 72a92b6..3c5ecd9 100644 --- a/test/busi/busi.proto +++ b/test/busi/busi.proto @@ -35,6 +35,7 @@ service Busi { rpc TransOutBSaga(BusiReq) returns (google.protobuf.Empty) {} rpc TransInRevertBSaga(BusiReq) returns (google.protobuf.Empty) {} rpc TransOutRevertBSaga(BusiReq) returns (google.protobuf.Empty) {} + rpc TransOutWithGlobalRequestTimeout(BusiReq) returns (google.protobuf.Empty) {} rpc TransOutHeaderYes(BusiReq) returns (google.protobuf.Empty) {} rpc TransOutHeaderNo(BusiReq) returns (google.protobuf.Empty) {} diff --git a/test/busi/busi_grpc.pb.go b/test/busi/busi_grpc.pb.go index 9235453..6de70fc 100644 --- a/test/busi/busi_grpc.pb.go +++ b/test/busi/busi_grpc.pb.go @@ -1,4 +1,8 @@ // Code generated by protoc-gen-go-grpc. DO NOT EDIT. +// versions: +// - protoc-gen-go-grpc v1.2.0 +// - protoc v3.19.4 +// source: test/busi/busi.proto package busi @@ -35,6 +39,7 @@ type BusiClient interface { TransOutBSaga(ctx context.Context, in *BusiReq, opts ...grpc.CallOption) (*emptypb.Empty, error) TransInRevertBSaga(ctx context.Context, in *BusiReq, opts ...grpc.CallOption) (*emptypb.Empty, error) TransOutRevertBSaga(ctx context.Context, in *BusiReq, opts ...grpc.CallOption) (*emptypb.Empty, error) + TransOutWithGlobalRequestTimeout(ctx context.Context, in *BusiReq, opts ...grpc.CallOption) (*emptypb.Empty, error) TransOutHeaderYes(ctx context.Context, in *BusiReq, opts ...grpc.CallOption) (*emptypb.Empty, error) TransOutHeaderNo(ctx context.Context, in *BusiReq, opts ...grpc.CallOption) (*emptypb.Empty, error) TransInRedis(ctx context.Context, in *BusiReq, opts ...grpc.CallOption) (*emptypb.Empty, error) @@ -198,6 +203,15 @@ func (c *busiClient) TransOutRevertBSaga(ctx context.Context, in *BusiReq, opts return out, nil } +func (c *busiClient) TransOutWithGlobalRequestTimeout(ctx context.Context, in *BusiReq, opts ...grpc.CallOption) (*emptypb.Empty, error) { + out := new(emptypb.Empty) + err := c.cc.Invoke(ctx, "/busi.Busi/TransOutWithGlobalRequestTimeout", in, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + func (c *busiClient) TransOutHeaderYes(ctx context.Context, in *BusiReq, opts ...grpc.CallOption) (*emptypb.Empty, error) { out := new(emptypb.Empty) err := c.cc.Invoke(ctx, "/busi.Busi/TransOutHeaderYes", in, out, opts...) @@ -299,6 +313,7 @@ type BusiServer interface { TransOutBSaga(context.Context, *BusiReq) (*emptypb.Empty, error) TransInRevertBSaga(context.Context, *BusiReq) (*emptypb.Empty, error) TransOutRevertBSaga(context.Context, *BusiReq) (*emptypb.Empty, error) + TransOutWithGlobalRequestTimeout(context.Context, *BusiReq) (*emptypb.Empty, error) TransOutHeaderYes(context.Context, *BusiReq) (*emptypb.Empty, error) TransOutHeaderNo(context.Context, *BusiReq) (*emptypb.Empty, error) TransInRedis(context.Context, *BusiReq) (*emptypb.Empty, error) @@ -363,6 +378,9 @@ func (UnimplementedBusiServer) TransInRevertBSaga(context.Context, *BusiReq) (*e func (UnimplementedBusiServer) TransOutRevertBSaga(context.Context, *BusiReq) (*emptypb.Empty, error) { return nil, status.Errorf(codes.Unimplemented, "method TransOutRevertBSaga not implemented") } +func (UnimplementedBusiServer) TransOutWithGlobalRequestTimeout(context.Context, *BusiReq) (*emptypb.Empty, error) { + return nil, status.Errorf(codes.Unimplemented, "method TransOutWithGlobalRequestTimeout not implemented") +} func (UnimplementedBusiServer) TransOutHeaderYes(context.Context, *BusiReq) (*emptypb.Empty, error) { return nil, status.Errorf(codes.Unimplemented, "method TransOutHeaderYes not implemented") } @@ -691,6 +709,24 @@ func _Busi_TransOutRevertBSaga_Handler(srv interface{}, ctx context.Context, dec return interceptor(ctx, in, info, handler) } +func _Busi_TransOutWithGlobalRequestTimeout_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(BusiReq) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(BusiServer).TransOutWithGlobalRequestTimeout(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: "/busi.Busi/TransOutWithGlobalRequestTimeout", + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(BusiServer).TransOutWithGlobalRequestTimeout(ctx, req.(*BusiReq)) + } + return interceptor(ctx, in, info, handler) +} + func _Busi_TransOutHeaderYes_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { in := new(BusiReq) if err := dec(in); err != nil { @@ -924,6 +960,10 @@ var Busi_ServiceDesc = grpc.ServiceDesc{ MethodName: "TransOutRevertBSaga", Handler: _Busi_TransOutRevertBSaga_Handler, }, + { + MethodName: "TransOutWithGlobalRequestTimeout", + Handler: _Busi_TransOutWithGlobalRequestTimeout_Handler, + }, { MethodName: "TransOutHeaderYes", Handler: _Busi_TransOutHeaderYes_Handler, diff --git a/test/saga_grpc_test.go b/test/saga_grpc_test.go index ee181ce..70fd731 100644 --- a/test/saga_grpc_test.go +++ b/test/saga_grpc_test.go @@ -110,6 +110,17 @@ func TestSagaGrpcPassthroughHeadersYes(t *testing.T) { waitTransProcessed(gidYes) } +func TestSagaGrpcWithGlobalTransRequestTimeout(t *testing.T) { + gid := dtmimp.GetFuncName() + saga := dtmgrpc.NewSagaGrpc(dtmutil.DefaultGrpcServer, gid) + saga.WaitResult = true + saga.Add(busi.BusiGrpc+"/busi.Busi/TransOutWithGlobalRequestTimeout", "", nil) + saga.WithGlobalTransRequestTimeout(6) + err := saga.Submit() + assert.Nil(t, err) + waitTransProcessed(gid) +} + func TestSagaGrpcCronPassthroughHeadersYes(t *testing.T) { gidYes := dtmimp.GetFuncName() sagaYes := dtmgrpc.NewSagaGrpc(dtmutil.DefaultGrpcServer, gidYes) From 10b1b898ec3be752b2d7b44b0d7577944bb6e4ef Mon Sep 17 00:00:00 2001 From: yedf2 <120050102@qq.com> Date: Thu, 24 Feb 2022 13:02:29 +0800 Subject: [PATCH 06/16] json rpc refactored --- conf.sample.yml | 2 +- dtmsvr/api_json_rpc.go | 137 ++++++++++++++++++++++++++++++++++++ dtmsvr/api_json_rpc_http.go | 110 ----------------------------- dtmsvr/config/config.go | 2 +- dtmsvr/svr.go | 26 +++---- dtmsvr/trans_class.go | 12 ---- dtmutil/utils.go | 79 +++++++++++++++++++++ 7 files changed, 231 insertions(+), 137 deletions(-) create mode 100644 dtmsvr/api_json_rpc.go delete mode 100644 dtmsvr/api_json_rpc_http.go diff --git a/conf.sample.yml b/conf.sample.yml index 1fd557e..673c9e9 100644 --- a/conf.sample.yml +++ b/conf.sample.yml @@ -56,7 +56,7 @@ # HttpPort: 36789 # GrpcPort: 36790 -# JSONRPC: 36791 +# JsonRpcPort: 36791 ### advanced options # UpdateBranchAsyncGoroutineNum: 1 # num of async goroutine to update branch status diff --git a/dtmsvr/api_json_rpc.go b/dtmsvr/api_json_rpc.go new file mode 100644 index 0000000..7ced4e6 --- /dev/null +++ b/dtmsvr/api_json_rpc.go @@ -0,0 +1,137 @@ +package dtmsvr + +import ( + "encoding/json" + "errors" + "fmt" + "time" + + "github.com/dtm-labs/dtm/dtmcli" + "github.com/dtm-labs/dtm/dtmcli/dtmimp" + "github.com/dtm-labs/dtm/dtmcli/logger" + "github.com/gin-gonic/gin" +) + +const jrpcCodeFailure = -32901 +const jrpcCodeOngoing = -32902 + +type jrpcReq struct { + Method string `json:"method"` + Jsonrpc string `json:"jsonrpc"` + Params interface{} `json:"params"` + ID string `json:"id"` +} + +func addJrpcRouter(engine *gin.Engine) { + type jrpcFunc = func(interface{}) interface{} + handlers := map[string]jrpcFunc{ + "dtmserver.NewGid": jrpcNewGid, + "dtmserver.Prepare": jrpcPrepare, + "dtmserver.Submit": jrpcSubmit, + "dtmserver.Abort": jrpcAbort, + "dtmserver.RegisterBranch": jrpcRegisterBranch, + } + engine.POST("/", func(c *gin.Context) { + began := time.Now() + var err error + var req jrpcReq + var jerr map[string]interface{} + r := func() interface{} { + defer dtmimp.P2E(&err) + err2 := c.BindJSON(&req) + if err2 != nil { + jerr = map[string]interface{}{ + "code": -32700, + "message": fmt.Sprintf("Parse json error: %s", err2.Error()), + } + } else if req.ID == "" || req.Jsonrpc != "2.0" { + jerr = map[string]interface{}{ + "code": -32600, + "message": fmt.Sprintf("Bad json request: %s", dtmimp.MustMarshalString(req)), + } + } else if handlers[req.Method] == nil { + jerr = map[string]interface{}{ + "code": -32601, + "message": fmt.Sprintf("Method not found: %s", req.Method), + } + } else if handlers[req.Method] != nil { + return handlers[req.Method](req.Params) + } + return nil + }() + + // error maybe returned in r, assign it to err + if ne, ok := r.(error); ok && err == nil { + err = ne + } + + if err != nil { + if errors.Is(err, dtmcli.ErrFailure) { + jerr = map[string]interface{}{ + "code": jrpcCodeFailure, + "message": err.Error(), + } + } else if errors.Is(err, dtmcli.ErrOngoing) { + jerr = map[string]interface{}{ + "code": jrpcCodeOngoing, + "message": err.Error(), + } + } else if jerr == nil { + jerr = map[string]interface{}{ + "code": -32603, + "message": err.Error(), + } + } + } + + result := map[string]interface{}{ + "jsonrpc": "2.0", + "id": req.ID, + "error": jerr, + "result": r, + } + b, _ := json.Marshal(result) + cont := string(b) + if jerr == nil || jerr["code"] == jrpcCodeOngoing { + logger.Infof("%2dms %d %s %s %s", time.Since(began).Milliseconds(), 200, c.Request.Method, c.Request.RequestURI, cont) + } else { + logger.Errorf("%2dms %d %s %s %s", time.Since(began).Milliseconds(), 200, c.Request.Method, c.Request.RequestURI, cont) + } + c.JSON(200, result) + }) +} + +// TransFromJrpcParams construct TransGlobal from jrpc params +func TransFromJrpcParams(params interface{}) *TransGlobal { + t := TransGlobal{} + dtmimp.MustRemarshal(params, &t) + return &t +} + +func jrpcNewGid(interface{}) interface{} { + return map[string]interface{}{"gid": GenGid()} +} + +func jrpcPrepare(params interface{}) interface{} { + return svcPrepare(TransFromJrpcParams(params)) +} + +func jrpcSubmit(params interface{}) interface{} { + return svcSubmit(TransFromJrpcParams(params)) +} + +func jrpcAbort(params interface{}) interface{} { + return svcAbort(TransFromJrpcParams(params)) +} + +func jrpcRegisterBranch(params interface{}) interface{} { + data := map[string]string{} + dtmimp.MustRemarshal(params, &data) + branch := TransBranch{ + Gid: data["gid"], + BranchID: data["branch_id"], + Status: dtmcli.StatusPrepared, + BinData: []byte(data["data"]), + } + return svcRegisterBranch(data["trans_type"], &branch, data) +} diff --git a/dtmsvr/api_json_rpc_http.go b/dtmsvr/api_json_rpc_http.go deleted file mode 100644 index edfc426..0000000 --- a/dtmsvr/api_json_rpc_http.go +++ /dev/null @@ -1,110 +0,0 @@ -package dtmsvr - -import ( - "encoding/json" - "fmt" - "net/http" - - "github.com/dtm-labs/dtm/dtmcli" - "github.com/dtm-labs/dtm/dtmcli/logger" - "github.com/gin-gonic/gin" -) - -type jsonRPCReq struct { - Method string `json:"method"` - Jsonrpc string `json:"jsonrpc"` - Params interface{} `json:"params"` - ID string `json:"id"` -} - -func addJSONRPCRouter(engine *gin.Engine) { - engine.POST("/", dispatcher) -} - -func dispatcher(c *gin.Context) { - req := new(jsonRPCReq) - err := c.BindJSON(req) - logger.Infof("request:%s\n", req) - if err != nil { - c.JSON(http.StatusOK, gin.H{"id": req.ID, "result": nil, "error": map[string]interface{}{"code": -32700, "message": "Parse error"}}) - return - } - if req.Method == "dtmserver.NewGid" { - res := jsonRPCNewGid() - c.JSON(http.StatusOK, gin.H{"id": req.ID, "result": res, "error": err}) - return - } - - if req.Method == "dtmserver.Prepare" { - res := jsonRPCPrepare(req.Params) - c.JSON(http.StatusOK, gin.H{"id": req.ID, "result": res, "error": nil}) - return - } - - if req.Method == "dtmserver.Submit" { - res := jsonRPCSubmit(req.Params) - c.JSON(http.StatusOK, gin.H{"id": req.ID, "result": res, "error": nil}) - return - } - - if req.Method == "dtmserver.Abort" { - res := jsonRPCAbort(req.Params) - c.JSON(http.StatusOK, gin.H{"id": req.ID, "result": res, "error": nil}) - return - } - - if req.Method == "dtmserver.RegisterBranch" { - res := jsonRPCRegisterBranch(req.Params) - c.JSON(http.StatusOK, gin.H{"id": req.ID, "result": res, "error": nil}) - return - } - c.JSON(http.StatusOK, gin.H{"id": req.ID, "result": nil, "error": map[string]interface{}{"code": -32601, "message": "Method not found"}}) -} - -func jsonRPCNewGid() interface{} { - return map[string]interface{}{"gid": GenGid(), "dtm_result": dtmcli.ResultSuccess} -} - -func jsonRPCPrepare(params interface{}) interface{} { - res := svcPrepare(TransFromJSONRPCContext(params)) - if res == nil { - return map[string]string{"dtm_result": "SUCCESS"} - } - return map[string]string{"dtm_result": "FAILURE", "message": fmt.Sprintf("%v", res)} -} - -func jsonRPCSubmit(params interface{}) interface{} { - res := svcSubmit(TransFromJSONRPCContext(params)) - if res == nil { - return map[string]string{"dtm_result": "SUCCESS"} - } - return map[string]string{"dtm_result": "FAILURE", "message": fmt.Sprintf("%v", res)} -} - -func jsonRPCAbort(params interface{}) interface{} { - res := svcAbort(TransFromJSONRPCContext(params)) - if res == nil { - return map[string]string{"dtm_result": "SUCCESS"} - } - return map[string]string{"dtm_result": "FAILURE", "message": fmt.Sprintf("%v", res)} -} - -func jsonRPCRegisterBranch(params interface{}) interface{} { - data := map[string]string{} - paramsJSON, _ := json.Marshal(params) - err := json.Unmarshal(paramsJSON, &data) - if err != nil { - return map[string]string{"dtm_result": "FAILURE", "message": err.Error()} - } - branch := TransBranch{ - Gid: data["gid"], - BranchID: data["branch_id"], - Status: dtmcli.StatusPrepared, - BinData: []byte(data["data"]), - } - res := svcRegisterBranch(data["trans_type"], &branch, data) - if res == nil { - return map[string]string{"dtm_result": "SUCCESS"} - } - return map[string]string{"dtm_result": "FAILURE", "message": res.Error()} -} diff --git a/dtmsvr/config/config.go b/dtmsvr/config/config.go index 7f69588..750af5e 100644 --- a/dtmsvr/config/config.go +++ b/dtmsvr/config/config.go @@ -76,7 +76,7 @@ type configType struct { RequestTimeout int64 `yaml:"RequestTimeout" default:"3"` HTTPPort int64 `yaml:"HttpPort" default:"36789"` GrpcPort int64 `yaml:"GrpcPort" default:"36790"` - JSONRPCPort int64 `yaml:"JSONRPCPort" default:"36791"` + JSONRPCPort int64 `yaml:"JsonRpcPort" default:"36791"` MicroService MicroService `yaml:"MicroService"` UpdateBranchSync int64 `yaml:"UpdateBranchSync"` UpdateBranchAsyncGoroutineNum int64 `yaml:"UpdateBranchAsyncGoroutineNum" default:"1"` diff --git a/dtmsvr/svr.go b/dtmsvr/svr.go index 62b06d6..c4b1aa0 100644 --- a/dtmsvr/svr.go +++ b/dtmsvr/svr.go @@ -38,7 +38,7 @@ func StartSvr() { app := dtmutil.GetGinApp() app = httpMetrics(app) addRoute(app) - logger.Infof("dtmsvr listen at: %d", conf.HTTPPort) + logger.Infof("dtmsvr http listen at: %d", conf.HTTPPort) go func() { err := app.Run(fmt.Sprintf(":%d", conf.HTTPPort)) if err != nil { @@ -46,6 +46,18 @@ func StartSvr() { } }() + // start json-rpc server + jrpcApp := dtmutil.GetGinApp() + jrpcApp = httpMetrics(jrpcApp) + addJrpcRouter(jrpcApp) + logger.Infof("dtmsvr json-rpc listen at: %d", conf.JSONRPCPort) + go func() { + err := jrpcApp.Run(fmt.Sprintf(":%d", conf.JSONRPCPort)) + if err != nil { + logger.Errorf("start server err: %v", err) + } + }() + // start grpc server lis, err := net.Listen("tcp", fmt.Sprintf(":%d", conf.GrpcPort)) logger.FatalIfError(err) @@ -66,18 +78,6 @@ func StartSvr() { logger.FatalIfError(err) err = dtmdriver.GetDriver().RegisterGrpcService(conf.MicroService.Target, conf.MicroService.EndPoint) logger.FatalIfError(err) - - // start json-rpc server - jsonRPCApp := dtmutil.GetGinApp() - jsonRPCApp = httpMetrics(jsonRPCApp) - addJSONRPCRouter(jsonRPCApp) - logger.Infof("dtmsvr listen at: %d", conf.JSONRPCPort) - go func() { - err := jsonRPCApp.Run(fmt.Sprintf(":%d", conf.JSONRPCPort)) - if err != nil { - logger.Errorf("start server err: %v", err) - } - }() } // PopulateDB setup mysql data diff --git a/dtmsvr/trans_class.go b/dtmsvr/trans_class.go index 48538cc..b6e080b 100644 --- a/dtmsvr/trans_class.go +++ b/dtmsvr/trans_class.go @@ -8,7 +8,6 @@ package dtmsvr import ( "context" - "encoding/json" "time" "github.com/dtm-labs/dtm/dtmcli" @@ -85,17 +84,6 @@ func TransFromContext(c *gin.Context) *TransGlobal { return &m } -// TransFromJSONRPCContext 1 -func TransFromJSONRPCContext(params interface{}) *TransGlobal { - jsonStr, _ := json.Marshal(params) - m := TransGlobal{} - err := json.Unmarshal(jsonStr, &m) - if err != nil { - return nil - } - return &m -} - // TransFromDtmRequest TransFromContext func TransFromDtmRequest(ctx context.Context, c *dtmgpb.DtmRequest) *TransGlobal { o := &dtmgpb.DtmTransOptions{} diff --git a/dtmutil/utils.go b/dtmutil/utils.go index 849b539..718e6d7 100644 --- a/dtmutil/utils.go +++ b/dtmutil/utils.go @@ -10,6 +10,7 @@ import ( "bytes" "encoding/json" "errors" + "fmt" "io/ioutil" "net/http" "os" @@ -105,6 +106,84 @@ func WrapHandler2(fn func(*gin.Context) interface{}) gin.HandlerFunc { } } +const jrpcCodeFailure = -32901 +const jrpcCodeOngoing = -32902 + +// JrpcReq json-rpc request +type JrpcReq struct { + Method string `json:"method"` + Jsonrpc string `json:"jsonrpc"` + Params interface{} `json:"params"` + ID string `json:"id"` +} + +// WrapJrpcHandler wrap a gin func to be a gin handler func +func WrapJrpcHandler(fn func(*JrpcReq) interface{}) gin.HandlerFunc { + return func(c *gin.Context) { + began := time.Now() + var err error + var req JrpcReq + var jerr map[string]interface{} + r := func() interface{} { + defer dtmimp.P2E(&err) + err2 := c.BindJSON(&req) + if err2 != nil { + jerr = map[string]interface{}{ + "code": -32700, + "message": fmt.Sprintf("Parse json error: %s", err2.Error()), + } + } else if req.ID == "" || req.Jsonrpc != "2.0" { + jerr = map[string]interface{}{ + "code": -32600, + "message": fmt.Sprintf("Bad json request: %s", dtmimp.MustMarshalString(req)), + } + } else { + return fn(&req) + } + return nil + }() + + // error maybe returned in r, assign it to err + if ne, ok := r.(error); ok && err == nil { + err = ne + } + + if err != nil { + if errors.Is(err, dtmcli.ErrFailure) { + jerr = map[string]interface{}{ + "code": jrpcCodeFailure, + "message": err.Error(), + } + } else if errors.Is(err, dtmcli.ErrOngoing) { + jerr = map[string]interface{}{ + "code": jrpcCodeOngoing, + "message": err.Error(), + } + } else if jerr == nil { + jerr = map[string]interface{}{ + "code": -32603, + "message": err.Error(), + } + } + } + + result := map[string]interface{}{ + "jsonrpc": "2.0", + "id": req.ID, + "error": jerr, + "result": r, + } + b, _ := json.Marshal(result) + cont := string(b) + if jerr == nil || jerr["code"] == jrpcCodeOngoing { + logger.Infof("%2dms %d %s %s %s", time.Since(began).Milliseconds(), 200, c.Request.Method, c.Request.RequestURI, cont) + } else { + logger.Errorf("%2dms %d %s %s %s", time.Since(began).Milliseconds(), 200, c.Request.Method, c.Request.RequestURI, cont) + } + c.JSON(200, result) + } +} + // MustGetwd must version of os.Getwd func MustGetwd() string { wd, err := os.Getwd() From c040ff0c78d3e8d29e368f33487dcda3c196eca2 Mon Sep 17 00:00:00 2001 From: yedf2 <120050102@qq.com> Date: Thu, 24 Feb 2022 13:30:20 +0800 Subject: [PATCH 07/16] json-rpc add first test case --- dtmsvr/api_json_rpc.go | 10 +++++----- dtmutil/consts.go | 2 ++ 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/dtmsvr/api_json_rpc.go b/dtmsvr/api_json_rpc.go index 7ced4e6..8f9a571 100644 --- a/dtmsvr/api_json_rpc.go +++ b/dtmsvr/api_json_rpc.go @@ -25,11 +25,11 @@ type jrpcReq struct { func addJrpcRouter(engine *gin.Engine) { type jrpcFunc = func(interface{}) interface{} handlers := map[string]jrpcFunc{ - "dtmserver.NewGid": jrpcNewGid, - "dtmserver.Prepare": jrpcPrepare, - "dtmserver.Submit": jrpcSubmit, - "dtmserver.Abort": jrpcAbort, - "dtmserver.RegisterBranch": jrpcRegisterBranch, + "dtmserver.newGid": jrpcNewGid, + "dtmserver.prepare": jrpcPrepare, + "dtmserver.submit": jrpcSubmit, + "dtmserver.abort": jrpcAbort, + "dtmserver.registerBranch": jrpcRegisterBranch, } engine.POST("/", func(c *gin.Context) { began := time.Now() diff --git a/dtmutil/consts.go b/dtmutil/consts.go index 9f1afe4..08388e2 100644 --- a/dtmutil/consts.go +++ b/dtmutil/consts.go @@ -11,4 +11,6 @@ const ( DefaultHTTPServer = "http://localhost:36789/api/dtmsvr" // DefaultGrpcServer default url for grpc server. used by test and examples DefaultGrpcServer = "localhost:36790" + // DefaultJrpcServer default url for http json-rpc server. used by test and examples + DefaultJrpcServer = "http://localhost:36791" ) From 6e1fe644ed58d448c2f8618cbead8d779baee596 Mon Sep 17 00:00:00 2001 From: yedf2 <120050102@qq.com> Date: Thu, 24 Feb 2022 13:30:25 +0800 Subject: [PATCH 08/16] json-rpc add first test case --- test/msg_jrpc_test.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 test/msg_jrpc_test.go diff --git a/test/msg_jrpc_test.go b/test/msg_jrpc_test.go new file mode 100644 index 0000000..16d04bb --- /dev/null +++ b/test/msg_jrpc_test.go @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2021 yedf. All rights reserved. + * Use of this source code is governed by a BSD-style + * license that can be found in the LICENSE file. + */ + +package test + +import ( + "testing" + + "github.com/dtm-labs/dtm/dtmcli" + "github.com/dtm-labs/dtm/dtmutil" + "github.com/stretchr/testify/assert" +) + +func TestMsgJrpcNormal(t *testing.T) { + resp, err := dtmcli.GetRestyClient().R().SetBody(map[string]string{ + "jsonrpc": "2.0", + "method": "dtmserver.newGid", + "params": "", + "id": "TestMsgJrpcNormal", + }).Post(dtmutil.DefaultJrpcServer) + assert.Nil(t, err) + assert.Contains(t, resp.String(), "gid") +} From c507acee37ccd5141bdf4939e1dd92498a9519bb Mon Sep 17 00:00:00 2001 From: liulei Date: Thu, 24 Feb 2022 15:06:33 +0800 Subject: [PATCH 09/16] feat: change requestTimeout proto define sequence for old compatible and optimize grpc interceptor set request timeout --- dtmgrpc/dtmgpb/dtmgimp.pb.go | 42 ++++++++++++++++++------------------ dtmgrpc/dtmgpb/dtmgimp.proto | 6 +++--- dtmsvr/svr.go | 12 +++++------ dtmsvr/trans_status.go | 17 ++++++++------- 4 files changed, 38 insertions(+), 39 deletions(-) diff --git a/dtmgrpc/dtmgpb/dtmgimp.pb.go b/dtmgrpc/dtmgpb/dtmgimp.pb.go index ab8f440..671c1c6 100644 --- a/dtmgrpc/dtmgpb/dtmgimp.pb.go +++ b/dtmgrpc/dtmgpb/dtmgimp.pb.go @@ -29,9 +29,9 @@ type DtmTransOptions struct { WaitResult bool `protobuf:"varint,1,opt,name=WaitResult,proto3" json:"WaitResult,omitempty"` TimeoutToFail int64 `protobuf:"varint,2,opt,name=TimeoutToFail,proto3" json:"TimeoutToFail,omitempty"` RetryInterval int64 `protobuf:"varint,3,opt,name=RetryInterval,proto3" json:"RetryInterval,omitempty"` - RequestTimeout int64 `protobuf:"varint,4,opt,name=RequestTimeout,proto3" json:"RequestTimeout,omitempty"` - PassthroughHeaders []string `protobuf:"bytes,5,rep,name=PassthroughHeaders,proto3" json:"PassthroughHeaders,omitempty"` - BranchHeaders map[string]string `protobuf:"bytes,6,rep,name=BranchHeaders,proto3" json:"BranchHeaders,omitempty" protobuf_key:"bytes,1,opt,name=key,proto3" protobuf_val:"bytes,2,opt,name=value,proto3"` + PassthroughHeaders []string `protobuf:"bytes,4,rep,name=PassthroughHeaders,proto3" json:"PassthroughHeaders,omitempty"` + BranchHeaders map[string]string `protobuf:"bytes,5,rep,name=BranchHeaders,proto3" json:"BranchHeaders,omitempty" protobuf_key:"bytes,1,opt,name=key,proto3" protobuf_val:"bytes,2,opt,name=value,proto3"` + RequestTimeout int64 `protobuf:"varint,6,opt,name=RequestTimeout,proto3" json:"RequestTimeout,omitempty"` } func (x *DtmTransOptions) Reset() { @@ -87,13 +87,6 @@ func (x *DtmTransOptions) GetRetryInterval() int64 { return 0 } -func (x *DtmTransOptions) GetRequestTimeout() int64 { - if x != nil { - return x.RequestTimeout - } - return 0 -} - func (x *DtmTransOptions) GetPassthroughHeaders() []string { if x != nil { return x.PassthroughHeaders @@ -108,6 +101,13 @@ func (x *DtmTransOptions) GetBranchHeaders() map[string]string { return nil } +func (x *DtmTransOptions) GetRequestTimeout() int64 { + if x != nil { + return x.RequestTimeout + } + return 0 +} + // DtmRequest request sent to dtm server type DtmRequest struct { state protoimpl.MessageState @@ -353,17 +353,17 @@ var file_dtmgrpc_dtmgpb_dtmgimp_proto_rawDesc = []byte{ 0x0d, 0x54, 0x69, 0x6d, 0x65, 0x6f, 0x75, 0x74, 0x54, 0x6f, 0x46, 0x61, 0x69, 0x6c, 0x12, 0x24, 0x0a, 0x0d, 0x52, 0x65, 0x74, 0x72, 0x79, 0x49, 0x6e, 0x74, 0x65, 0x72, 0x76, 0x61, 0x6c, 0x18, 0x03, 0x20, 0x01, 0x28, 0x03, 0x52, 0x0d, 0x52, 0x65, 0x74, 0x72, 0x79, 0x49, 0x6e, 0x74, 0x65, - 0x72, 0x76, 0x61, 0x6c, 0x12, 0x26, 0x0a, 0x0e, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x54, - 0x69, 0x6d, 0x65, 0x6f, 0x75, 0x74, 0x18, 0x04, 0x20, 0x01, 0x28, 0x03, 0x52, 0x0e, 0x52, 0x65, - 0x71, 0x75, 0x65, 0x73, 0x74, 0x54, 0x69, 0x6d, 0x65, 0x6f, 0x75, 0x74, 0x12, 0x2e, 0x0a, 0x12, - 0x50, 0x61, 0x73, 0x73, 0x74, 0x68, 0x72, 0x6f, 0x75, 0x67, 0x68, 0x48, 0x65, 0x61, 0x64, 0x65, - 0x72, 0x73, 0x18, 0x05, 0x20, 0x03, 0x28, 0x09, 0x52, 0x12, 0x50, 0x61, 0x73, 0x73, 0x74, 0x68, - 0x72, 0x6f, 0x75, 0x67, 0x68, 0x48, 0x65, 0x61, 0x64, 0x65, 0x72, 0x73, 0x12, 0x51, 0x0a, 0x0d, - 0x42, 0x72, 0x61, 0x6e, 0x63, 0x68, 0x48, 0x65, 0x61, 0x64, 0x65, 0x72, 0x73, 0x18, 0x06, 0x20, - 0x03, 0x28, 0x0b, 0x32, 0x2b, 0x2e, 0x64, 0x74, 0x6d, 0x67, 0x69, 0x6d, 0x70, 0x2e, 0x44, 0x74, - 0x6d, 0x54, 0x72, 0x61, 0x6e, 0x73, 0x4f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x2e, 0x42, 0x72, - 0x61, 0x6e, 0x63, 0x68, 0x48, 0x65, 0x61, 0x64, 0x65, 0x72, 0x73, 0x45, 0x6e, 0x74, 0x72, 0x79, - 0x52, 0x0d, 0x42, 0x72, 0x61, 0x6e, 0x63, 0x68, 0x48, 0x65, 0x61, 0x64, 0x65, 0x72, 0x73, 0x1a, + 0x72, 0x76, 0x61, 0x6c, 0x12, 0x2e, 0x0a, 0x12, 0x50, 0x61, 0x73, 0x73, 0x74, 0x68, 0x72, 0x6f, + 0x75, 0x67, 0x68, 0x48, 0x65, 0x61, 0x64, 0x65, 0x72, 0x73, 0x18, 0x04, 0x20, 0x03, 0x28, 0x09, + 0x52, 0x12, 0x50, 0x61, 0x73, 0x73, 0x74, 0x68, 0x72, 0x6f, 0x75, 0x67, 0x68, 0x48, 0x65, 0x61, + 0x64, 0x65, 0x72, 0x73, 0x12, 0x51, 0x0a, 0x0d, 0x42, 0x72, 0x61, 0x6e, 0x63, 0x68, 0x48, 0x65, + 0x61, 0x64, 0x65, 0x72, 0x73, 0x18, 0x05, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x2b, 0x2e, 0x64, 0x74, + 0x6d, 0x67, 0x69, 0x6d, 0x70, 0x2e, 0x44, 0x74, 0x6d, 0x54, 0x72, 0x61, 0x6e, 0x73, 0x4f, 0x70, + 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x2e, 0x42, 0x72, 0x61, 0x6e, 0x63, 0x68, 0x48, 0x65, 0x61, 0x64, + 0x65, 0x72, 0x73, 0x45, 0x6e, 0x74, 0x72, 0x79, 0x52, 0x0d, 0x42, 0x72, 0x61, 0x6e, 0x63, 0x68, + 0x48, 0x65, 0x61, 0x64, 0x65, 0x72, 0x73, 0x12, 0x26, 0x0a, 0x0e, 0x52, 0x65, 0x71, 0x75, 0x65, + 0x73, 0x74, 0x54, 0x69, 0x6d, 0x65, 0x6f, 0x75, 0x74, 0x18, 0x06, 0x20, 0x01, 0x28, 0x03, 0x52, + 0x0e, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x54, 0x69, 0x6d, 0x65, 0x6f, 0x75, 0x74, 0x1a, 0x40, 0x0a, 0x12, 0x42, 0x72, 0x61, 0x6e, 0x63, 0x68, 0x48, 0x65, 0x61, 0x64, 0x65, 0x72, 0x73, 0x45, 0x6e, 0x74, 0x72, 0x79, 0x12, 0x10, 0x0a, 0x03, 0x6b, 0x65, 0x79, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x6b, 0x65, 0x79, 0x12, 0x14, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, diff --git a/dtmgrpc/dtmgpb/dtmgimp.proto b/dtmgrpc/dtmgpb/dtmgimp.proto index 5fc3aa7..0a76d53 100644 --- a/dtmgrpc/dtmgpb/dtmgimp.proto +++ b/dtmgrpc/dtmgpb/dtmgimp.proto @@ -18,9 +18,9 @@ message DtmTransOptions { bool WaitResult = 1; int64 TimeoutToFail = 2; int64 RetryInterval = 3; - int64 RequestTimeout = 4; - repeated string PassthroughHeaders = 5; - map BranchHeaders = 6; + repeated string PassthroughHeaders = 4; + map BranchHeaders = 5; + int64 RequestTimeout = 6; } // DtmRequest request sent to dtm server diff --git a/dtmsvr/svr.go b/dtmsvr/svr.go index 62b06d6..b897ad9 100644 --- a/dtmsvr/svr.go +++ b/dtmsvr/svr.go @@ -7,14 +7,12 @@ package dtmsvr import ( - "context" "fmt" "net" "time" "github.com/dtm-labs/dtm/dtmcli" "github.com/dtm-labs/dtm/dtmcli/logger" - "github.com/dtm-labs/dtm/dtmgrpc" "github.com/dtm-labs/dtm/dtmgrpc/dtmgimp" "github.com/dtm-labs/dtm/dtmgrpc/dtmgpb" "github.com/dtm-labs/dtm/dtmutil" @@ -28,11 +26,11 @@ func StartSvr() { setServerInfoMetrics() dtmcli.GetRestyClient().SetTimeout(time.Duration(conf.RequestTimeout) * time.Second) - dtmgrpc.AddUnaryInterceptor(func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error { - ctx2, cancel := context.WithTimeout(ctx, time.Duration(conf.RequestTimeout)*time.Second) - defer cancel() - return invoker(ctx2, method, req, reply, cc, opts...) - }) + //dtmgrpc.AddUnaryInterceptor(func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error { + // ctx2, cancel := context.WithTimeout(ctx, time.Duration(conf.RequestTimeout)*time.Second) + // defer cancel() + // return invoker(ctx2, method, req, reply, cc, opts...) + //}) // start gin server app := dtmutil.GetGinApp() diff --git a/dtmsvr/trans_status.go b/dtmsvr/trans_status.go index 01c2ab0..bda6330 100644 --- a/dtmsvr/trans_status.go +++ b/dtmsvr/trans_status.go @@ -122,14 +122,15 @@ func (t *TransGlobal) getURLResult(url string, branchID, op string, branchPayloa if err != nil { return err } - if t.RequestTimeout != 0 { - // use ClientInterceptors[1:] for remove default request setting - dtmgimp.ClientInterceptors = append(dtmgimp.ClientInterceptors[1:], func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error { - ctx2, cancel := context.WithTimeout(ctx, time.Duration(t.RequestTimeout)*time.Second) - defer cancel() - return invoker(ctx2, method, req, reply, cc, opts...) - }) - } + dtmgimp.ClientInterceptors = append(dtmgimp.ClientInterceptors, func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error { + timeout := conf.RequestTimeout + if t.RequestTimeout != 0 { + timeout = conf.RequestTimeout + } + ctx2, cancel := context.WithTimeout(ctx, time.Duration(timeout)*time.Second) + defer cancel() + return invoker(ctx2, method, req, reply, cc, opts...) + }) conn := dtmgimp.MustGetGrpcConn(server, true) ctx := dtmgimp.TransInfo2Ctx(t.Gid, t.TransType, branchID, op, "") kvs := dtmgimp.Map2Kvs(t.Ext.Headers) From b23195a9a35b33461804ff6b5bf911f12c464bcd Mon Sep 17 00:00:00 2001 From: xyctruth <398041993@qq.com> Date: Thu, 24 Feb 2022 15:55:46 +0800 Subject: [PATCH 10/16] fix release workflows trigger events --- .github/workflows/release.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 17c6e77..d9fb0d6 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -1,6 +1,6 @@ name: Release on: - create: + push: tags: - 'v*.*.*' From cbf1b2cfde97a77d266d5d0c604f8845d32af142 Mon Sep 17 00:00:00 2001 From: yedf2 <120050102@qq.com> Date: Thu, 24 Feb 2022 17:40:38 +0800 Subject: [PATCH 11/16] remove utils --- dtmutil/utils.go | 79 ------------------------------------------------ 1 file changed, 79 deletions(-) diff --git a/dtmutil/utils.go b/dtmutil/utils.go index 718e6d7..849b539 100644 --- a/dtmutil/utils.go +++ b/dtmutil/utils.go @@ -10,7 +10,6 @@ import ( "bytes" "encoding/json" "errors" - "fmt" "io/ioutil" "net/http" "os" @@ -106,84 +105,6 @@ func WrapHandler2(fn func(*gin.Context) interface{}) gin.HandlerFunc { } } -const jrpcCodeFailure = -32901 -const jrpcCodeOngoing = -32902 - -// JrpcReq json-rpc request -type JrpcReq struct { - Method string `json:"method"` - Jsonrpc string `json:"jsonrpc"` - Params interface{} `json:"params"` - ID string `json:"id"` -} - -// WrapJrpcHandler wrap a gin func to be a gin handler func -func WrapJrpcHandler(fn func(*JrpcReq) interface{}) gin.HandlerFunc { - return func(c *gin.Context) { - began := time.Now() - var err error - var req JrpcReq - var jerr map[string]interface{} - r := func() interface{} { - defer dtmimp.P2E(&err) - err2 := c.BindJSON(&req) - if err2 != nil { - jerr = map[string]interface{}{ - "code": -32700, - "message": fmt.Sprintf("Parse json error: %s", err2.Error()), - } - } else if req.ID == "" || req.Jsonrpc != "2.0" { - jerr = map[string]interface{}{ - "code": -32600, - "message": fmt.Sprintf("Bad json request: %s", dtmimp.MustMarshalString(req)), - } - } else { - return fn(&req) - } - return nil - }() - - // error maybe returned in r, assign it to err - if ne, ok := r.(error); ok && err == nil { - err = ne - } - - if err != nil { - if errors.Is(err, dtmcli.ErrFailure) { - jerr = map[string]interface{}{ - "code": jrpcCodeFailure, - "message": err.Error(), - } - } else if errors.Is(err, dtmcli.ErrOngoing) { - jerr = map[string]interface{}{ - "code": jrpcCodeOngoing, - "message": err.Error(), - } - } else if jerr == nil { - jerr = map[string]interface{}{ - "code": -32603, - "message": err.Error(), - } - } - } - - result := map[string]interface{}{ - "jsonrpc": "2.0", - "id": req.ID, - "error": jerr, - "result": r, - } - b, _ := json.Marshal(result) - cont := string(b) - if jerr == nil || jerr["code"] == jrpcCodeOngoing { - logger.Infof("%2dms %d %s %s %s", time.Since(began).Milliseconds(), 200, c.Request.Method, c.Request.RequestURI, cont) - } else { - logger.Errorf("%2dms %d %s %s %s", time.Since(began).Milliseconds(), 200, c.Request.Method, c.Request.RequestURI, cont) - } - c.JSON(200, result) - } -} - // MustGetwd must version of os.Getwd func MustGetwd() string { wd, err := os.Getwd() From 47b7e2f48c2e8588ec74e76298fd5b8144e2ef8c Mon Sep 17 00:00:00 2001 From: huileihe Date: Thu, 24 Feb 2022 21:07:01 +0800 Subject: [PATCH 12/16] add Copyright --- dtmsvr/storage/sql/sql.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/dtmsvr/storage/sql/sql.go b/dtmsvr/storage/sql/sql.go index 566d2f8..d87297a 100644 --- a/dtmsvr/storage/sql/sql.go +++ b/dtmsvr/storage/sql/sql.go @@ -1,3 +1,9 @@ +/* + * Copyright (c) 2021 yedf. All rights reserved. + * Use of this source code is governed by a BSD-style + * license that can be found in the LICENSE file. + */ + package sql import ( From aa144f0464005555df62129e0506f2ee278d231f Mon Sep 17 00:00:00 2001 From: liulei Date: Thu, 24 Feb 2022 21:12:24 +0800 Subject: [PATCH 13/16] feat: grpc request with global trans request timeout option --- dtmgrpc/dtmgimp/utils.go | 26 ++++++++++++++++---------- dtmsvr/svr.go | 17 ++++++++++++----- dtmsvr/trans_status.go | 14 ++------------ 3 files changed, 30 insertions(+), 27 deletions(-) diff --git a/dtmgrpc/dtmgimp/utils.go b/dtmgrpc/dtmgimp/utils.go index 12c7e8d..17aaa11 100644 --- a/dtmgrpc/dtmgimp/utils.go +++ b/dtmgrpc/dtmgimp/utils.go @@ -8,9 +8,6 @@ package dtmgimp import ( context "context" - "time" - - "google.golang.org/grpc" "github.com/dtm-labs/dtm/dtmcli/dtmimp" "github.com/dtm-labs/dtm/dtmcli/logger" @@ -29,13 +26,6 @@ func MustProtoMarshal(msg proto.Message) []byte { // DtmGrpcCall make a convenient call to dtm func DtmGrpcCall(s *dtmimp.TransBase, operation string) error { - if s.RequestTimeout != 0 { - ClientInterceptors = append(ClientInterceptors, func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error { - ctx2, cancel := context.WithTimeout(ctx, time.Duration(s.RequestTimeout)*time.Second) - defer cancel() - return invoker(ctx2, method, req, reply, cc, opts...) - }) - } reply := emptypb.Empty{} return MustGetGrpcConn(s.Dtm, false).Invoke(context.Background(), "/dtmgimp.Dtm/"+operation, &dtmgpb.DtmRequest{ Gid: s.Gid, @@ -111,3 +101,19 @@ func GetMetaFromContext(ctx context.Context, name string) string { md, _ := metadata.FromIncomingContext(ctx) return mdGet(md, name) } + +type requestTimeoutKey struct{} + +// RequestTimeoutFromContext returns requestTime of transOption option +func RequestTimeoutFromContext(ctx context.Context) int64 { + if v, ok := ctx.Value(requestTimeoutKey{}).(int64); ok { + return v + } + + return 0 +} + +// RequestTimeoutNewContext sets requestTimeout of transOption option to context +func RequestTimeoutNewContext(ctx context.Context, requestTimeout int64) context.Context { + return context.WithValue(ctx, requestTimeoutKey{}, requestTimeout) +} diff --git a/dtmsvr/svr.go b/dtmsvr/svr.go index b897ad9..5d97fc2 100644 --- a/dtmsvr/svr.go +++ b/dtmsvr/svr.go @@ -7,10 +7,13 @@ package dtmsvr import ( + "context" "fmt" "net" "time" + "github.com/dtm-labs/dtm/dtmgrpc" + "github.com/dtm-labs/dtm/dtmcli" "github.com/dtm-labs/dtm/dtmcli/logger" "github.com/dtm-labs/dtm/dtmgrpc/dtmgimp" @@ -26,11 +29,15 @@ func StartSvr() { setServerInfoMetrics() dtmcli.GetRestyClient().SetTimeout(time.Duration(conf.RequestTimeout) * time.Second) - //dtmgrpc.AddUnaryInterceptor(func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error { - // ctx2, cancel := context.WithTimeout(ctx, time.Duration(conf.RequestTimeout)*time.Second) - // defer cancel() - // return invoker(ctx2, method, req, reply, cc, opts...) - //}) + dtmgrpc.AddUnaryInterceptor(func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error { + timeout := conf.RequestTimeout + if v := dtmgimp.RequestTimeoutFromContext(ctx); v != 0 { + timeout = v + } + ctx2, cancel := context.WithTimeout(ctx, time.Duration(timeout)*time.Second) + defer cancel() + return invoker(ctx2, method, req, reply, cc, opts...) + }) // start gin server app := dtmutil.GetGinApp() diff --git a/dtmsvr/trans_status.go b/dtmsvr/trans_status.go index bda6330..00c0c58 100644 --- a/dtmsvr/trans_status.go +++ b/dtmsvr/trans_status.go @@ -7,14 +7,11 @@ package dtmsvr import ( - "context" "errors" "fmt" "strings" "time" - "google.golang.org/grpc" - "github.com/dtm-labs/dtm/dtmcli" "github.com/dtm-labs/dtm/dtmcli/dtmimp" "github.com/dtm-labs/dtm/dtmcli/logger" @@ -122,20 +119,13 @@ func (t *TransGlobal) getURLResult(url string, branchID, op string, branchPayloa if err != nil { return err } - dtmgimp.ClientInterceptors = append(dtmgimp.ClientInterceptors, func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error { - timeout := conf.RequestTimeout - if t.RequestTimeout != 0 { - timeout = conf.RequestTimeout - } - ctx2, cancel := context.WithTimeout(ctx, time.Duration(timeout)*time.Second) - defer cancel() - return invoker(ctx2, method, req, reply, cc, opts...) - }) + conn := dtmgimp.MustGetGrpcConn(server, true) ctx := dtmgimp.TransInfo2Ctx(t.Gid, t.TransType, branchID, op, "") kvs := dtmgimp.Map2Kvs(t.Ext.Headers) kvs = append(kvs, dtmgimp.Map2Kvs(t.BranchHeaders)...) ctx = metadata.AppendToOutgoingContext(ctx, kvs...) + ctx = dtmgimp.RequestTimeoutNewContext(ctx, t.RequestTimeout) err = conn.Invoke(ctx, method, branchPayload, &[]byte{}) if err == nil { return nil From a7026d8a46fac6b877d8a52799449bdc423c2ae4 Mon Sep 17 00:00:00 2001 From: liulei Date: Fri, 25 Feb 2022 11:23:42 +0800 Subject: [PATCH 14/16] revert: undo request timeout test pb define --- test/busi/busi.pb.go | 141 ++++++++++++++++++-------------------- test/busi/busi.proto | 1 - test/busi/busi_grpc.pb.go | 36 ---------- test/saga_grpc_test.go | 2 +- 4 files changed, 68 insertions(+), 112 deletions(-) diff --git a/test/busi/busi.pb.go b/test/busi/busi.pb.go index e9c31f6..ee64b5a 100644 --- a/test/busi/busi.pb.go +++ b/test/busi/busi.pb.go @@ -148,7 +148,7 @@ var file_test_busi_busi_proto_rawDesc = []byte{ 0x6e, 0x73, 0x49, 0x6e, 0x52, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x22, 0x25, 0x0a, 0x09, 0x42, 0x75, 0x73, 0x69, 0x52, 0x65, 0x70, 0x6c, 0x79, 0x12, 0x18, 0x0a, 0x07, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, - 0x65, 0x32, 0x8b, 0x0c, 0x0a, 0x04, 0x42, 0x75, 0x73, 0x69, 0x12, 0x32, 0x0a, 0x07, 0x54, 0x72, + 0x65, 0x32, 0xbe, 0x0b, 0x0a, 0x04, 0x42, 0x75, 0x73, 0x69, 0x12, 0x32, 0x0a, 0x07, 0x54, 0x72, 0x61, 0x6e, 0x73, 0x49, 0x6e, 0x12, 0x0d, 0x2e, 0x62, 0x75, 0x73, 0x69, 0x2e, 0x42, 0x75, 0x73, 0x69, 0x52, 0x65, 0x71, 0x1a, 0x16, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x45, 0x6d, 0x70, 0x74, 0x79, 0x22, 0x00, 0x12, 0x33, @@ -206,47 +206,42 @@ var file_test_busi_busi_proto_rawDesc = []byte{ 0x73, 0x4f, 0x75, 0x74, 0x52, 0x65, 0x76, 0x65, 0x72, 0x74, 0x42, 0x53, 0x61, 0x67, 0x61, 0x12, 0x0d, 0x2e, 0x62, 0x75, 0x73, 0x69, 0x2e, 0x42, 0x75, 0x73, 0x69, 0x52, 0x65, 0x71, 0x1a, 0x16, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, - 0x2e, 0x45, 0x6d, 0x70, 0x74, 0x79, 0x22, 0x00, 0x12, 0x4b, 0x0a, 0x20, 0x54, 0x72, 0x61, 0x6e, - 0x73, 0x4f, 0x75, 0x74, 0x57, 0x69, 0x74, 0x68, 0x47, 0x6c, 0x6f, 0x62, 0x61, 0x6c, 0x52, 0x65, - 0x71, 0x75, 0x65, 0x73, 0x74, 0x54, 0x69, 0x6d, 0x65, 0x6f, 0x75, 0x74, 0x12, 0x0d, 0x2e, 0x62, - 0x75, 0x73, 0x69, 0x2e, 0x42, 0x75, 0x73, 0x69, 0x52, 0x65, 0x71, 0x1a, 0x16, 0x2e, 0x67, 0x6f, - 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x45, 0x6d, - 0x70, 0x74, 0x79, 0x22, 0x00, 0x12, 0x3c, 0x0a, 0x11, 0x54, 0x72, 0x61, 0x6e, 0x73, 0x4f, 0x75, - 0x74, 0x48, 0x65, 0x61, 0x64, 0x65, 0x72, 0x59, 0x65, 0x73, 0x12, 0x0d, 0x2e, 0x62, 0x75, 0x73, + 0x2e, 0x45, 0x6d, 0x70, 0x74, 0x79, 0x22, 0x00, 0x12, 0x3c, 0x0a, 0x11, 0x54, 0x72, 0x61, 0x6e, + 0x73, 0x4f, 0x75, 0x74, 0x48, 0x65, 0x61, 0x64, 0x65, 0x72, 0x59, 0x65, 0x73, 0x12, 0x0d, 0x2e, + 0x62, 0x75, 0x73, 0x69, 0x2e, 0x42, 0x75, 0x73, 0x69, 0x52, 0x65, 0x71, 0x1a, 0x16, 0x2e, 0x67, + 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x45, + 0x6d, 0x70, 0x74, 0x79, 0x22, 0x00, 0x12, 0x3b, 0x0a, 0x10, 0x54, 0x72, 0x61, 0x6e, 0x73, 0x4f, + 0x75, 0x74, 0x48, 0x65, 0x61, 0x64, 0x65, 0x72, 0x4e, 0x6f, 0x12, 0x0d, 0x2e, 0x62, 0x75, 0x73, 0x69, 0x2e, 0x42, 0x75, 0x73, 0x69, 0x52, 0x65, 0x71, 0x1a, 0x16, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x45, 0x6d, 0x70, 0x74, - 0x79, 0x22, 0x00, 0x12, 0x3b, 0x0a, 0x10, 0x54, 0x72, 0x61, 0x6e, 0x73, 0x4f, 0x75, 0x74, 0x48, - 0x65, 0x61, 0x64, 0x65, 0x72, 0x4e, 0x6f, 0x12, 0x0d, 0x2e, 0x62, 0x75, 0x73, 0x69, 0x2e, 0x42, - 0x75, 0x73, 0x69, 0x52, 0x65, 0x71, 0x1a, 0x16, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, - 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x45, 0x6d, 0x70, 0x74, 0x79, 0x22, 0x00, - 0x12, 0x37, 0x0a, 0x0c, 0x54, 0x72, 0x61, 0x6e, 0x73, 0x49, 0x6e, 0x52, 0x65, 0x64, 0x69, 0x73, - 0x12, 0x0d, 0x2e, 0x62, 0x75, 0x73, 0x69, 0x2e, 0x42, 0x75, 0x73, 0x69, 0x52, 0x65, 0x71, 0x1a, - 0x16, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, - 0x66, 0x2e, 0x45, 0x6d, 0x70, 0x74, 0x79, 0x22, 0x00, 0x12, 0x38, 0x0a, 0x0d, 0x54, 0x72, 0x61, - 0x6e, 0x73, 0x4f, 0x75, 0x74, 0x52, 0x65, 0x64, 0x69, 0x73, 0x12, 0x0d, 0x2e, 0x62, 0x75, 0x73, + 0x79, 0x22, 0x00, 0x12, 0x37, 0x0a, 0x0c, 0x54, 0x72, 0x61, 0x6e, 0x73, 0x49, 0x6e, 0x52, 0x65, + 0x64, 0x69, 0x73, 0x12, 0x0d, 0x2e, 0x62, 0x75, 0x73, 0x69, 0x2e, 0x42, 0x75, 0x73, 0x69, 0x52, + 0x65, 0x71, 0x1a, 0x16, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, + 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x45, 0x6d, 0x70, 0x74, 0x79, 0x22, 0x00, 0x12, 0x38, 0x0a, 0x0d, + 0x54, 0x72, 0x61, 0x6e, 0x73, 0x4f, 0x75, 0x74, 0x52, 0x65, 0x64, 0x69, 0x73, 0x12, 0x0d, 0x2e, + 0x62, 0x75, 0x73, 0x69, 0x2e, 0x42, 0x75, 0x73, 0x69, 0x52, 0x65, 0x71, 0x1a, 0x16, 0x2e, 0x67, + 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x45, + 0x6d, 0x70, 0x74, 0x79, 0x22, 0x00, 0x12, 0x3d, 0x0a, 0x12, 0x54, 0x72, 0x61, 0x6e, 0x73, 0x49, + 0x6e, 0x52, 0x65, 0x76, 0x65, 0x72, 0x74, 0x52, 0x65, 0x64, 0x69, 0x73, 0x12, 0x0d, 0x2e, 0x62, + 0x75, 0x73, 0x69, 0x2e, 0x42, 0x75, 0x73, 0x69, 0x52, 0x65, 0x71, 0x1a, 0x16, 0x2e, 0x67, 0x6f, + 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x45, 0x6d, + 0x70, 0x74, 0x79, 0x22, 0x00, 0x12, 0x3e, 0x0a, 0x13, 0x54, 0x72, 0x61, 0x6e, 0x73, 0x4f, 0x75, + 0x74, 0x52, 0x65, 0x76, 0x65, 0x72, 0x74, 0x52, 0x65, 0x64, 0x69, 0x73, 0x12, 0x0d, 0x2e, 0x62, + 0x75, 0x73, 0x69, 0x2e, 0x42, 0x75, 0x73, 0x69, 0x52, 0x65, 0x71, 0x1a, 0x16, 0x2e, 0x67, 0x6f, + 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x45, 0x6d, + 0x70, 0x74, 0x79, 0x22, 0x00, 0x12, 0x31, 0x0a, 0x0d, 0x51, 0x75, 0x65, 0x72, 0x79, 0x50, 0x72, + 0x65, 0x70, 0x61, 0x72, 0x65, 0x64, 0x12, 0x0d, 0x2e, 0x62, 0x75, 0x73, 0x69, 0x2e, 0x42, 0x75, + 0x73, 0x69, 0x52, 0x65, 0x71, 0x1a, 0x0f, 0x2e, 0x62, 0x75, 0x73, 0x69, 0x2e, 0x42, 0x75, 0x73, + 0x69, 0x52, 0x65, 0x70, 0x6c, 0x79, 0x22, 0x00, 0x12, 0x39, 0x0a, 0x0e, 0x51, 0x75, 0x65, 0x72, + 0x79, 0x50, 0x72, 0x65, 0x70, 0x61, 0x72, 0x65, 0x64, 0x42, 0x12, 0x0d, 0x2e, 0x62, 0x75, 0x73, 0x69, 0x2e, 0x42, 0x75, 0x73, 0x69, 0x52, 0x65, 0x71, 0x1a, 0x16, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x45, 0x6d, 0x70, 0x74, - 0x79, 0x22, 0x00, 0x12, 0x3d, 0x0a, 0x12, 0x54, 0x72, 0x61, 0x6e, 0x73, 0x49, 0x6e, 0x52, 0x65, - 0x76, 0x65, 0x72, 0x74, 0x52, 0x65, 0x64, 0x69, 0x73, 0x12, 0x0d, 0x2e, 0x62, 0x75, 0x73, 0x69, + 0x79, 0x22, 0x00, 0x12, 0x3d, 0x0a, 0x12, 0x51, 0x75, 0x65, 0x72, 0x79, 0x50, 0x72, 0x65, 0x70, + 0x61, 0x72, 0x65, 0x64, 0x52, 0x65, 0x64, 0x69, 0x73, 0x12, 0x0d, 0x2e, 0x62, 0x75, 0x73, 0x69, 0x2e, 0x42, 0x75, 0x73, 0x69, 0x52, 0x65, 0x71, 0x1a, 0x16, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x45, 0x6d, 0x70, 0x74, 0x79, - 0x22, 0x00, 0x12, 0x3e, 0x0a, 0x13, 0x54, 0x72, 0x61, 0x6e, 0x73, 0x4f, 0x75, 0x74, 0x52, 0x65, - 0x76, 0x65, 0x72, 0x74, 0x52, 0x65, 0x64, 0x69, 0x73, 0x12, 0x0d, 0x2e, 0x62, 0x75, 0x73, 0x69, - 0x2e, 0x42, 0x75, 0x73, 0x69, 0x52, 0x65, 0x71, 0x1a, 0x16, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, - 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x45, 0x6d, 0x70, 0x74, 0x79, - 0x22, 0x00, 0x12, 0x31, 0x0a, 0x0d, 0x51, 0x75, 0x65, 0x72, 0x79, 0x50, 0x72, 0x65, 0x70, 0x61, - 0x72, 0x65, 0x64, 0x12, 0x0d, 0x2e, 0x62, 0x75, 0x73, 0x69, 0x2e, 0x42, 0x75, 0x73, 0x69, 0x52, - 0x65, 0x71, 0x1a, 0x0f, 0x2e, 0x62, 0x75, 0x73, 0x69, 0x2e, 0x42, 0x75, 0x73, 0x69, 0x52, 0x65, - 0x70, 0x6c, 0x79, 0x22, 0x00, 0x12, 0x39, 0x0a, 0x0e, 0x51, 0x75, 0x65, 0x72, 0x79, 0x50, 0x72, - 0x65, 0x70, 0x61, 0x72, 0x65, 0x64, 0x42, 0x12, 0x0d, 0x2e, 0x62, 0x75, 0x73, 0x69, 0x2e, 0x42, - 0x75, 0x73, 0x69, 0x52, 0x65, 0x71, 0x1a, 0x16, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, - 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x45, 0x6d, 0x70, 0x74, 0x79, 0x22, 0x00, - 0x12, 0x3d, 0x0a, 0x12, 0x51, 0x75, 0x65, 0x72, 0x79, 0x50, 0x72, 0x65, 0x70, 0x61, 0x72, 0x65, - 0x64, 0x52, 0x65, 0x64, 0x69, 0x73, 0x12, 0x0d, 0x2e, 0x62, 0x75, 0x73, 0x69, 0x2e, 0x42, 0x75, - 0x73, 0x69, 0x52, 0x65, 0x71, 0x1a, 0x16, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, - 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x45, 0x6d, 0x70, 0x74, 0x79, 0x22, 0x00, 0x42, - 0x08, 0x5a, 0x06, 0x2e, 0x2f, 0x62, 0x75, 0x73, 0x69, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, - 0x33, + 0x22, 0x00, 0x42, 0x08, 0x5a, 0x06, 0x2e, 0x2f, 0x62, 0x75, 0x73, 0x69, 0x62, 0x06, 0x70, 0x72, + 0x6f, 0x74, 0x6f, 0x33, } var ( @@ -284,44 +279,42 @@ var file_test_busi_busi_proto_depIdxs = []int32{ 0, // 13: busi.Busi.TransOutBSaga:input_type -> busi.BusiReq 0, // 14: busi.Busi.TransInRevertBSaga:input_type -> busi.BusiReq 0, // 15: busi.Busi.TransOutRevertBSaga:input_type -> busi.BusiReq - 0, // 16: busi.Busi.TransOutWithGlobalRequestTimeout:input_type -> busi.BusiReq - 0, // 17: busi.Busi.TransOutHeaderYes:input_type -> busi.BusiReq - 0, // 18: busi.Busi.TransOutHeaderNo:input_type -> busi.BusiReq - 0, // 19: busi.Busi.TransInRedis:input_type -> busi.BusiReq - 0, // 20: busi.Busi.TransOutRedis:input_type -> busi.BusiReq - 0, // 21: busi.Busi.TransInRevertRedis:input_type -> busi.BusiReq - 0, // 22: busi.Busi.TransOutRevertRedis:input_type -> busi.BusiReq - 0, // 23: busi.Busi.QueryPrepared:input_type -> busi.BusiReq - 0, // 24: busi.Busi.QueryPreparedB:input_type -> busi.BusiReq - 0, // 25: busi.Busi.QueryPreparedRedis:input_type -> busi.BusiReq - 2, // 26: busi.Busi.TransIn:output_type -> google.protobuf.Empty - 2, // 27: busi.Busi.TransOut:output_type -> google.protobuf.Empty - 2, // 28: busi.Busi.TransInRevert:output_type -> google.protobuf.Empty - 2, // 29: busi.Busi.TransOutRevert:output_type -> google.protobuf.Empty - 2, // 30: busi.Busi.TransInConfirm:output_type -> google.protobuf.Empty - 2, // 31: busi.Busi.TransOutConfirm:output_type -> google.protobuf.Empty - 2, // 32: busi.Busi.XaNotify:output_type -> google.protobuf.Empty - 2, // 33: busi.Busi.TransInXa:output_type -> google.protobuf.Empty - 2, // 34: busi.Busi.TransOutXa:output_type -> google.protobuf.Empty - 2, // 35: busi.Busi.TransInTcc:output_type -> google.protobuf.Empty - 2, // 36: busi.Busi.TransOutTcc:output_type -> google.protobuf.Empty - 2, // 37: busi.Busi.TransInTccNested:output_type -> google.protobuf.Empty - 2, // 38: busi.Busi.TransInBSaga:output_type -> google.protobuf.Empty - 2, // 39: busi.Busi.TransOutBSaga:output_type -> google.protobuf.Empty - 2, // 40: busi.Busi.TransInRevertBSaga:output_type -> google.protobuf.Empty - 2, // 41: busi.Busi.TransOutRevertBSaga:output_type -> google.protobuf.Empty - 2, // 42: busi.Busi.TransOutWithGlobalRequestTimeout:output_type -> google.protobuf.Empty - 2, // 43: busi.Busi.TransOutHeaderYes:output_type -> google.protobuf.Empty - 2, // 44: busi.Busi.TransOutHeaderNo:output_type -> google.protobuf.Empty - 2, // 45: busi.Busi.TransInRedis:output_type -> google.protobuf.Empty - 2, // 46: busi.Busi.TransOutRedis:output_type -> google.protobuf.Empty - 2, // 47: busi.Busi.TransInRevertRedis:output_type -> google.protobuf.Empty - 2, // 48: busi.Busi.TransOutRevertRedis:output_type -> google.protobuf.Empty - 1, // 49: busi.Busi.QueryPrepared:output_type -> busi.BusiReply - 2, // 50: busi.Busi.QueryPreparedB:output_type -> google.protobuf.Empty - 2, // 51: busi.Busi.QueryPreparedRedis:output_type -> google.protobuf.Empty - 26, // [26:52] is the sub-list for method output_type - 0, // [0:26] is the sub-list for method input_type + 0, // 16: busi.Busi.TransOutHeaderYes:input_type -> busi.BusiReq + 0, // 17: busi.Busi.TransOutHeaderNo:input_type -> busi.BusiReq + 0, // 18: busi.Busi.TransInRedis:input_type -> busi.BusiReq + 0, // 19: busi.Busi.TransOutRedis:input_type -> busi.BusiReq + 0, // 20: busi.Busi.TransInRevertRedis:input_type -> busi.BusiReq + 0, // 21: busi.Busi.TransOutRevertRedis:input_type -> busi.BusiReq + 0, // 22: busi.Busi.QueryPrepared:input_type -> busi.BusiReq + 0, // 23: busi.Busi.QueryPreparedB:input_type -> busi.BusiReq + 0, // 24: busi.Busi.QueryPreparedRedis:input_type -> busi.BusiReq + 2, // 25: busi.Busi.TransIn:output_type -> google.protobuf.Empty + 2, // 26: busi.Busi.TransOut:output_type -> google.protobuf.Empty + 2, // 27: busi.Busi.TransInRevert:output_type -> google.protobuf.Empty + 2, // 28: busi.Busi.TransOutRevert:output_type -> google.protobuf.Empty + 2, // 29: busi.Busi.TransInConfirm:output_type -> google.protobuf.Empty + 2, // 30: busi.Busi.TransOutConfirm:output_type -> google.protobuf.Empty + 2, // 31: busi.Busi.XaNotify:output_type -> google.protobuf.Empty + 2, // 32: busi.Busi.TransInXa:output_type -> google.protobuf.Empty + 2, // 33: busi.Busi.TransOutXa:output_type -> google.protobuf.Empty + 2, // 34: busi.Busi.TransInTcc:output_type -> google.protobuf.Empty + 2, // 35: busi.Busi.TransOutTcc:output_type -> google.protobuf.Empty + 2, // 36: busi.Busi.TransInTccNested:output_type -> google.protobuf.Empty + 2, // 37: busi.Busi.TransInBSaga:output_type -> google.protobuf.Empty + 2, // 38: busi.Busi.TransOutBSaga:output_type -> google.protobuf.Empty + 2, // 39: busi.Busi.TransInRevertBSaga:output_type -> google.protobuf.Empty + 2, // 40: busi.Busi.TransOutRevertBSaga:output_type -> google.protobuf.Empty + 2, // 41: busi.Busi.TransOutHeaderYes:output_type -> google.protobuf.Empty + 2, // 42: busi.Busi.TransOutHeaderNo:output_type -> google.protobuf.Empty + 2, // 43: busi.Busi.TransInRedis:output_type -> google.protobuf.Empty + 2, // 44: busi.Busi.TransOutRedis:output_type -> google.protobuf.Empty + 2, // 45: busi.Busi.TransInRevertRedis:output_type -> google.protobuf.Empty + 2, // 46: busi.Busi.TransOutRevertRedis:output_type -> google.protobuf.Empty + 1, // 47: busi.Busi.QueryPrepared:output_type -> busi.BusiReply + 2, // 48: busi.Busi.QueryPreparedB:output_type -> google.protobuf.Empty + 2, // 49: busi.Busi.QueryPreparedRedis:output_type -> google.protobuf.Empty + 25, // [25:50] is the sub-list for method output_type + 0, // [0:25] is the sub-list for method input_type 0, // [0:0] is the sub-list for extension type_name 0, // [0:0] is the sub-list for extension extendee 0, // [0:0] is the sub-list for field type_name diff --git a/test/busi/busi.proto b/test/busi/busi.proto index 3c5ecd9..72a92b6 100644 --- a/test/busi/busi.proto +++ b/test/busi/busi.proto @@ -35,7 +35,6 @@ service Busi { rpc TransOutBSaga(BusiReq) returns (google.protobuf.Empty) {} rpc TransInRevertBSaga(BusiReq) returns (google.protobuf.Empty) {} rpc TransOutRevertBSaga(BusiReq) returns (google.protobuf.Empty) {} - rpc TransOutWithGlobalRequestTimeout(BusiReq) returns (google.protobuf.Empty) {} rpc TransOutHeaderYes(BusiReq) returns (google.protobuf.Empty) {} rpc TransOutHeaderNo(BusiReq) returns (google.protobuf.Empty) {} diff --git a/test/busi/busi_grpc.pb.go b/test/busi/busi_grpc.pb.go index 6de70fc..21828f1 100644 --- a/test/busi/busi_grpc.pb.go +++ b/test/busi/busi_grpc.pb.go @@ -39,7 +39,6 @@ type BusiClient interface { TransOutBSaga(ctx context.Context, in *BusiReq, opts ...grpc.CallOption) (*emptypb.Empty, error) TransInRevertBSaga(ctx context.Context, in *BusiReq, opts ...grpc.CallOption) (*emptypb.Empty, error) TransOutRevertBSaga(ctx context.Context, in *BusiReq, opts ...grpc.CallOption) (*emptypb.Empty, error) - TransOutWithGlobalRequestTimeout(ctx context.Context, in *BusiReq, opts ...grpc.CallOption) (*emptypb.Empty, error) TransOutHeaderYes(ctx context.Context, in *BusiReq, opts ...grpc.CallOption) (*emptypb.Empty, error) TransOutHeaderNo(ctx context.Context, in *BusiReq, opts ...grpc.CallOption) (*emptypb.Empty, error) TransInRedis(ctx context.Context, in *BusiReq, opts ...grpc.CallOption) (*emptypb.Empty, error) @@ -203,15 +202,6 @@ func (c *busiClient) TransOutRevertBSaga(ctx context.Context, in *BusiReq, opts return out, nil } -func (c *busiClient) TransOutWithGlobalRequestTimeout(ctx context.Context, in *BusiReq, opts ...grpc.CallOption) (*emptypb.Empty, error) { - out := new(emptypb.Empty) - err := c.cc.Invoke(ctx, "/busi.Busi/TransOutWithGlobalRequestTimeout", in, out, opts...) - if err != nil { - return nil, err - } - return out, nil -} - func (c *busiClient) TransOutHeaderYes(ctx context.Context, in *BusiReq, opts ...grpc.CallOption) (*emptypb.Empty, error) { out := new(emptypb.Empty) err := c.cc.Invoke(ctx, "/busi.Busi/TransOutHeaderYes", in, out, opts...) @@ -313,7 +303,6 @@ type BusiServer interface { TransOutBSaga(context.Context, *BusiReq) (*emptypb.Empty, error) TransInRevertBSaga(context.Context, *BusiReq) (*emptypb.Empty, error) TransOutRevertBSaga(context.Context, *BusiReq) (*emptypb.Empty, error) - TransOutWithGlobalRequestTimeout(context.Context, *BusiReq) (*emptypb.Empty, error) TransOutHeaderYes(context.Context, *BusiReq) (*emptypb.Empty, error) TransOutHeaderNo(context.Context, *BusiReq) (*emptypb.Empty, error) TransInRedis(context.Context, *BusiReq) (*emptypb.Empty, error) @@ -378,9 +367,6 @@ func (UnimplementedBusiServer) TransInRevertBSaga(context.Context, *BusiReq) (*e func (UnimplementedBusiServer) TransOutRevertBSaga(context.Context, *BusiReq) (*emptypb.Empty, error) { return nil, status.Errorf(codes.Unimplemented, "method TransOutRevertBSaga not implemented") } -func (UnimplementedBusiServer) TransOutWithGlobalRequestTimeout(context.Context, *BusiReq) (*emptypb.Empty, error) { - return nil, status.Errorf(codes.Unimplemented, "method TransOutWithGlobalRequestTimeout not implemented") -} func (UnimplementedBusiServer) TransOutHeaderYes(context.Context, *BusiReq) (*emptypb.Empty, error) { return nil, status.Errorf(codes.Unimplemented, "method TransOutHeaderYes not implemented") } @@ -709,24 +695,6 @@ func _Busi_TransOutRevertBSaga_Handler(srv interface{}, ctx context.Context, dec return interceptor(ctx, in, info, handler) } -func _Busi_TransOutWithGlobalRequestTimeout_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { - in := new(BusiReq) - if err := dec(in); err != nil { - return nil, err - } - if interceptor == nil { - return srv.(BusiServer).TransOutWithGlobalRequestTimeout(ctx, in) - } - info := &grpc.UnaryServerInfo{ - Server: srv, - FullMethod: "/busi.Busi/TransOutWithGlobalRequestTimeout", - } - handler := func(ctx context.Context, req interface{}) (interface{}, error) { - return srv.(BusiServer).TransOutWithGlobalRequestTimeout(ctx, req.(*BusiReq)) - } - return interceptor(ctx, in, info, handler) -} - func _Busi_TransOutHeaderYes_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { in := new(BusiReq) if err := dec(in); err != nil { @@ -960,10 +928,6 @@ var Busi_ServiceDesc = grpc.ServiceDesc{ MethodName: "TransOutRevertBSaga", Handler: _Busi_TransOutRevertBSaga_Handler, }, - { - MethodName: "TransOutWithGlobalRequestTimeout", - Handler: _Busi_TransOutWithGlobalRequestTimeout_Handler, - }, { MethodName: "TransOutHeaderYes", Handler: _Busi_TransOutHeaderYes_Handler, diff --git a/test/saga_grpc_test.go b/test/saga_grpc_test.go index 70fd731..94f1732 100644 --- a/test/saga_grpc_test.go +++ b/test/saga_grpc_test.go @@ -114,7 +114,7 @@ func TestSagaGrpcWithGlobalTransRequestTimeout(t *testing.T) { gid := dtmimp.GetFuncName() saga := dtmgrpc.NewSagaGrpc(dtmutil.DefaultGrpcServer, gid) saga.WaitResult = true - saga.Add(busi.BusiGrpc+"/busi.Busi/TransOutWithGlobalRequestTimeout", "", nil) + saga.Add(busi.BusiGrpc+"/busi.Busi/TransOutHeaderNo", "", nil) saga.WithGlobalTransRequestTimeout(6) err := saga.Submit() assert.Nil(t, err) From 795e5232a8d82ae6bc8411047450b43cc5abeb28 Mon Sep 17 00:00:00 2001 From: liulei Date: Fri, 25 Feb 2022 11:31:47 +0800 Subject: [PATCH 15/16] revert: undo base_grpc of requestTimeout test logic --- test/busi/base_grpc.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/test/busi/base_grpc.go b/test/busi/base_grpc.go index fe5d255..682c37d 100644 --- a/test/busi/base_grpc.go +++ b/test/busi/base_grpc.go @@ -128,10 +128,6 @@ func (s *busiServer) XaNotify(ctx context.Context, in *emptypb.Empty) (*emptypb. return XaGrpcClient.HandleCallback(ctx) } -func (s *busiServer) TransOutWithGlobalRequestTimeout(ctx context.Context, in *BusiReq) (*emptypb.Empty, error) { - return &emptypb.Empty{}, handleGrpcBusiness(in, MainSwitch.TransOutResult.Fetch(), in.TransOutResult, dtmimp.GetFuncName()) -} - func (s *busiServer) TransOutHeaderYes(ctx context.Context, in *BusiReq) (*emptypb.Empty, error) { meta := dtmgimp.GetMetaFromContext(ctx, "test_header") if meta == "" { From 623eef5ae1fe330e85ad9c61d6858054f401fc38 Mon Sep 17 00:00:00 2001 From: yedf2 <120050102@qq.com> Date: Fri, 25 Feb 2022 15:33:15 +0800 Subject: [PATCH 16/16] add jrpc tests --- dtmcli/dtmimp/trans_base.go | 23 ++++++++++++++++ dtmsvr/api_json_rpc.go | 13 ++++----- dtmsvr/svr.go | 13 +-------- dtmsvr/trans_class.go | 28 ++++++++++++-------- dtmutil/consts.go | 4 +-- test/msg_jrpc_test.go | 53 ++++++++++++++++++++++++++++++++++--- 6 files changed, 99 insertions(+), 35 deletions(-) diff --git a/dtmcli/dtmimp/trans_base.go b/dtmcli/dtmimp/trans_base.go index 879ffc2..4ec247b 100644 --- a/dtmcli/dtmimp/trans_base.go +++ b/dtmcli/dtmimp/trans_base.go @@ -65,6 +65,7 @@ type TransBase struct { Op string `json:"-"` // used in XA/TCC QueryPrepared string `json:"query_prepared,omitempty"` // used in MSG + Protocol string `json:"protocol"` } // NewTransBase new a TransBase @@ -93,6 +94,25 @@ func TransCallDtm(tb *TransBase, body interface{}, operation string) error { if tb.RequestTimeout != 0 { RestyClient.SetTimeout(time.Duration(tb.RequestTimeout) * time.Second) } + if tb.Protocol == "json-rpc" { + var result map[string]interface{} + resp, err := RestyClient.R(). + SetBody(map[string]interface{}{ + "jsonrpc": "2.0", + "id": "no-use", + "method": operation, + "params": body, + }). + SetResult(&result). + Post(tb.Dtm) + if err != nil { + return err + } + if resp.StatusCode() != http.StatusOK || result["error"] != nil { + return errors.New(resp.String()) + } + return nil + } resp, err := RestyClient.R(). SetBody(body).Post(fmt.Sprintf("%s/%s", tb.Dtm, operation)) if err != nil { @@ -118,6 +138,9 @@ func TransRegisterBranch(tb *TransBase, added map[string]string, operation strin // TransRequestBranch TransBase request branch result func TransRequestBranch(t *TransBase, method string, body interface{}, branchID string, op string, url string) (*resty.Response, error) { + if url == "" { + return nil, nil + } resp, err := RestyClient.R(). SetBody(body). SetQueryParams(map[string]string{ diff --git a/dtmsvr/api_json_rpc.go b/dtmsvr/api_json_rpc.go index 8f9a571..d9f3936 100644 --- a/dtmsvr/api_json_rpc.go +++ b/dtmsvr/api_json_rpc.go @@ -25,13 +25,13 @@ type jrpcReq struct { func addJrpcRouter(engine *gin.Engine) { type jrpcFunc = func(interface{}) interface{} handlers := map[string]jrpcFunc{ - "dtmserver.newGid": jrpcNewGid, - "dtmserver.prepare": jrpcPrepare, - "dtmserver.submit": jrpcSubmit, - "dtmserver.abort": jrpcAbort, - "dtmserver.registerBranch": jrpcRegisterBranch, + "newGid": jrpcNewGid, + "prepare": jrpcPrepare, + "submit": jrpcSubmit, + "abort": jrpcAbort, + "registerBranch": jrpcRegisterBranch, } - engine.POST("/", func(c *gin.Context) { + engine.POST("/api/json-rpc", func(c *gin.Context) { began := time.Now() var err error var req jrpcReq @@ -105,6 +105,7 @@ func addJrpcRouter(engine *gin.Engine) { func TransFromJrpcParams(params interface{}) *TransGlobal { t := TransGlobal{} dtmimp.MustRemarshal(params, &t) + t.setupPayloads() return &t } diff --git a/dtmsvr/svr.go b/dtmsvr/svr.go index 49d5583..4fafd89 100644 --- a/dtmsvr/svr.go +++ b/dtmsvr/svr.go @@ -43,6 +43,7 @@ func StartSvr() { app := dtmutil.GetGinApp() app = httpMetrics(app) addRoute(app) + addJrpcRouter(app) logger.Infof("dtmsvr http listen at: %d", conf.HTTPPort) go func() { err := app.Run(fmt.Sprintf(":%d", conf.HTTPPort)) @@ -51,18 +52,6 @@ func StartSvr() { } }() - // start json-rpc server - jrpcApp := dtmutil.GetGinApp() - jrpcApp = httpMetrics(jrpcApp) - addJrpcRouter(jrpcApp) - logger.Infof("dtmsvr json-rpc listen at: %d", conf.JSONRPCPort) - go func() { - err := jrpcApp.Run(fmt.Sprintf(":%d", conf.JSONRPCPort)) - if err != nil { - logger.Errorf("start server err: %v", err) - } - }() - // start grpc server lis, err := net.Listen("tcp", fmt.Sprintf(":%d", conf.GrpcPort)) logger.FatalIfError(err) diff --git a/dtmsvr/trans_class.go b/dtmsvr/trans_class.go index 8aa1b60..2eae58c 100644 --- a/dtmsvr/trans_class.go +++ b/dtmsvr/trans_class.go @@ -26,6 +26,22 @@ type TransGlobal struct { updateBranchSync bool } +func (t *TransGlobal) setupPayloads() { + // Payloads will be store in BinPayloads, Payloads is only used to Unmarshal + for _, p := range t.Payloads { + t.BinPayloads = append(t.BinPayloads, []byte(p)) + } + for _, d := range t.Steps { + if d["data"] != "" { + t.BinPayloads = append(t.BinPayloads, []byte(d["data"])) + } + } + if t.Protocol == "" { + t.Protocol = "http" + } + +} + // TransBranch branch transaction type TransBranch = storage.TransBranchStore @@ -61,17 +77,7 @@ func TransFromContext(c *gin.Context) *TransGlobal { m := TransGlobal{} dtmimp.MustUnmarshal(b, &m) logger.Debugf("creating trans in prepare") - // Payloads will be store in BinPayloads, Payloads is only used to Unmarshal - for _, p := range m.Payloads { - m.BinPayloads = append(m.BinPayloads, []byte(p)) - } - for _, d := range m.Steps { - if d["data"] != "" { - m.BinPayloads = append(m.BinPayloads, []byte(d["data"])) - } - } - m.Protocol = "http" - + m.setupPayloads() m.Ext.Headers = map[string]string{} if len(m.PassthroughHeaders) > 0 { for _, h := range m.PassthroughHeaders { diff --git a/dtmutil/consts.go b/dtmutil/consts.go index 08388e2..d8c4345 100644 --- a/dtmutil/consts.go +++ b/dtmutil/consts.go @@ -9,8 +9,8 @@ package dtmutil const ( // DefaultHTTPServer default url for http server. used by test and examples DefaultHTTPServer = "http://localhost:36789/api/dtmsvr" + // DefaultJrpcServer default url for http json-rpc server. used by test and examples + DefaultJrpcServer = "http://localhost:36789/api/json-rpc" // DefaultGrpcServer default url for grpc server. used by test and examples DefaultGrpcServer = "localhost:36790" - // DefaultJrpcServer default url for http json-rpc server. used by test and examples - DefaultJrpcServer = "http://localhost:36791" ) diff --git a/test/msg_jrpc_test.go b/test/msg_jrpc_test.go index 16d04bb..f94c2c8 100644 --- a/test/msg_jrpc_test.go +++ b/test/msg_jrpc_test.go @@ -10,17 +10,62 @@ import ( "testing" "github.com/dtm-labs/dtm/dtmcli" + "github.com/dtm-labs/dtm/dtmcli/dtmimp" "github.com/dtm-labs/dtm/dtmutil" + "github.com/dtm-labs/dtm/test/busi" "github.com/stretchr/testify/assert" ) func TestMsgJrpcNormal(t *testing.T) { - resp, err := dtmcli.GetRestyClient().R().SetBody(map[string]string{ + msg := genJrpcMsg(dtmimp.GetFuncName()) + msg.Submit() + assert.Equal(t, StatusSubmitted, getTransStatus(msg.Gid)) + waitTransProcessed(msg.Gid) + assert.Equal(t, []string{StatusSucceed, StatusSucceed}, getBranchesStatus(msg.Gid)) + assert.Equal(t, StatusSucceed, getTransStatus(msg.Gid)) +} + +func TestMsgJrpcRepeated(t *testing.T) { + msg := genJrpcMsg(dtmimp.GetFuncName()) + msg.Submit() + assert.Equal(t, StatusSubmitted, getTransStatus(msg.Gid)) + waitTransProcessed(msg.Gid) + assert.Equal(t, []string{StatusSucceed, StatusSucceed}, getBranchesStatus(msg.Gid)) + assert.Equal(t, StatusSucceed, getTransStatus(msg.Gid)) + err := msg.Submit() + assert.Error(t, err) +} +func TestMsgJprcAbnormal(t *testing.T) { + id := "no-use" + resp, err := dtmcli.GetRestyClient().R().SetBody("hello").Post(dtmutil.DefaultJrpcServer) + assert.Nil(t, err) + assert.Contains(t, resp.String(), "-32700") + + resp, err = dtmcli.GetRestyClient().R().SetBody(map[string]string{ + "jsonrpc": "1.0", + "method": "newGid", + "params": "", + "id": id, + }).Post(dtmutil.DefaultJrpcServer) + assert.Nil(t, err) + assert.Contains(t, resp.String(), "-32600") + + resp, err = dtmcli.GetRestyClient().R().SetBody(map[string]string{ "jsonrpc": "2.0", - "method": "dtmserver.newGid", + "method": "not-exists", "params": "", - "id": "TestMsgJrpcNormal", + "id": id, }).Post(dtmutil.DefaultJrpcServer) assert.Nil(t, err) - assert.Contains(t, resp.String(), "gid") + assert.Contains(t, resp.String(), "-32601") +} + +func genJrpcMsg(gid string) *dtmcli.Msg { + req := busi.GenTransReq(30, false, false) + msg := dtmcli.NewMsg(dtmutil.DefaultJrpcServer, gid). + Add(busi.Busi+"/TransOut", &req). + Add(busi.Busi+"/TransIn", &req) + msg.QueryPrepared = busi.Busi + "/QueryPrepared" + msg.Protocol = "json-rpc" + return msg }