From 5287e9efa18e40bc7b67b460038aaeabd3b10fd5 Mon Sep 17 00:00:00 2001 From: yedf2 Date: Tue, 26 Jul 2022 22:20:53 +0800 Subject: [PATCH 1/4] Update README.md --- README.md | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 31f8140..e1f0f4e 100644 --- a/README.md +++ b/README.md @@ -71,14 +71,14 @@ go run main.go // The address where DtmServer serves DTM, which is a url DtmServer := "http://localhost:36789/api/dtmsvr" req := &gin.H{"amount": 30} // micro-service payload - // DtmServer is the address of DTM micro-service - saga := dtmcli.NewSaga(DtmServer, shortuuid.New()). - // add a TransOut sub-transaction,forward operation with url: qsBusi+"/TransOut", reverse compensation operation with url: qsBusi+"/TransOutCom" - Add(qsBusi+"/TransOut", qsBusi+"/TransOutCom", req). - // add a TransIn sub-transaction, forward operation with url: qsBusi+"/TransIn", reverse compensation operation with url: qsBusi+"/TransInCom" - Add(qsBusi+"/TransIn", qsBusi+"/TransInCom", req) - // submit the created saga transaction,dtm ensures all sub-transactions either complete or get revoked - err := saga.Submit() + // DtmServer is the address of DTM micro-service + saga := dtmcli.NewSaga(DtmServer, shortuuid.New()). + // add a TransOut sub-transaction,forward operation with url: qsBusi+"/TransOut", reverse compensation operation with url: qsBusi+"/TransOutCom" + Add(qsBusi+"/TransOut", qsBusi+"/TransOutCom", req). + // add a TransIn sub-transaction, forward operation with url: qsBusi+"/TransIn", reverse compensation operation with url: qsBusi+"/TransInCom" + Add(qsBusi+"/TransIn", qsBusi+"/TransInCom", req) + // submit the created saga transaction,dtm ensures all sub-transactions either complete or get revoked + err := saga.Submit() ``` When the above code runs, we can see in the console that services TransOut, TransIn has been called. From a0df9010de981cc94b7713d88744530b2805d5d4 Mon Sep 17 00:00:00 2001 From: yedf2 <120050102@qq.com> Date: Mon, 1 Aug 2022 11:09:05 +0800 Subject: [PATCH 2/4] merged --- README.md | 70 +++++++++++++++++++++++++-------------------- helper/README-en.md | 70 +++++++++++++++++++++++++-------------------- 2 files changed, 78 insertions(+), 62 deletions(-) diff --git a/README.md b/README.md index e1f0f4e..8c31e82 100644 --- a/README.md +++ b/README.md @@ -26,11 +26,11 @@ DTM is a distributed transaction framework which provides cross-service eventual [More](https://en.dtm.pub/other/using.html) ## Features -* Multiple languages support: SDK for Go, Java, PHP, C#, Python, Nodejs * Support for multiple transaction modes: SAGA, TCC, XA, Workflow, Outbox +* Multiple languages support: SDK for Go, Java, PHP, C#, Python, Nodejs * Better Outbox: 2-phase messages, a more elegant solution than Outbox, support multi-databases * Multiple database transaction support: Mysql, Redis, MongoDB, Postgres, TDSQL, etc. -* Support for multiple storage engines: Mysql (common), Redis (high performance), MongoDB (under planning) +* Support for multiple storage engines: Mysql (common), Redis (high performance), BoltDB (dev&test), MongoDB (under planning) * Support for multiple microservices architectures: [go-zero](https://github.com/zeromicro/go-zero), go-kratos/kratos, polarismesh/polaris * Support for high availability and easy horizontal scaling @@ -58,57 +58,65 @@ Suppose we want to perform an inter-bank transfer. The operations of transfer ou Here is an example to illustrate a solution of dtm to this problem: ``` bash -git clone https://github.com/dtm-labs/dtmcli-go-sample && cd dtmcli-go-sample +git clone https://github.com/dtm-labs/quick-start-sample.git && cd quick-start-sample/workflow-grpc go run main.go ``` ## Code -### Use +### Usage ``` go - // business micro-service address - const qsBusi = "http://localhost:8081/api/busi_saga" - // The address where DtmServer serves DTM, which is a url - DtmServer := "http://localhost:36789/api/dtmsvr" - req := &gin.H{"amount": 30} // micro-service payload - // DtmServer is the address of DTM micro-service - saga := dtmcli.NewSaga(DtmServer, shortuuid.New()). - // add a TransOut sub-transaction,forward operation with url: qsBusi+"/TransOut", reverse compensation operation with url: qsBusi+"/TransOutCom" - Add(qsBusi+"/TransOut", qsBusi+"/TransOutCom", req). - // add a TransIn sub-transaction, forward operation with url: qsBusi+"/TransIn", reverse compensation operation with url: qsBusi+"/TransInCom" - Add(qsBusi+"/TransIn", qsBusi+"/TransInCom", req) - // submit the created saga transaction,dtm ensures all sub-transactions either complete or get revoked - err := saga.Submit() -``` +wfName := "workflow-grpc" +err = workflow.Register(wfName, func(wf *workflow.Workflow, data []byte) error { + // ... + // Define a transaction branch for TransOut + wf.NewBranch().OnRollback(func(bb *dtmcli.BranchBarrier) error { + // compensation for TransOut + _, err := busiCli.TransOutRevert(wf.Context, &req) + return err + }) + _, err = busiCli.TransOut(wf.Context, &req) + // check error + + // Define another transaction branch for TransIn + wf.NewBranch().OnRollback(func(bb *dtmcli.BranchBarrier) error { + _, err := busiCli.TransInRevert(wf.Context, &req) + return err + }) + _, err = busiCli.TransIn(wf.Context, &req) + return err +} + +// ... +req := busi.BusiReq{Amount: 30, TransInResult: ""} +data, err := proto.Marshal(&req) + +// Execute workflow +err = workflow.Execute(wfName, shortuuid.New(), data) +logger.Infof("result of workflow.Execute is: %v", err) -When the above code runs, we can see in the console that services TransOut, TransIn has been called. - -#### Timing diagram -A timing diagram for a successfully completed SAGA transaction would be as follows: +``` -saga-success +When the above code runs, we can see in the console that services `TransOut`, `TransIn` has been called. #### Rollback upon failure If any forward operation fails, DTM invokes the corresponding compensating operation of each sub-transaction to roll back, after which the transaction is successfully rolled back. -Let's purposely fail the forward operation of the second sub-transaction and watch what happens +Let's purposely trigger the failure of the second sub-transaction and watch what happens ``` go -app.POST(qsBusiAPI+"/TransIn", func(c *gin.Context) { - log.Printf("TransIn") - // c.JSON(200, "") - c.JSON(409, "") // Status 409 for Failure. Won't be retried +// req := busi.BusiReq{Amount: 30, TransInResult: ""} +req := busi.BusiReq{Amount: 30, TransInResult: "FAILURE"} }) ``` +we can see in the console that services `TransOut`, `TransIn`, `TransOutRevert` has been called The timing diagram for the intended failure is as follows: -saga-failed - ## More examples If you want more quick start examples, please refer to [dtm-labs/quick-start-sample](https://github.com/dtm-labs/quick-start-sample) -The above example mainly demonstrates the flow of a distributed transaction. More on this, including practical examples of how to interface with an actual database, how to do compensation, how to do rollback, etc. please refer to [dtm-examples](https://github.com/dtm-labs/dtm-examples) for more examples. +The above example mainly demonstrates the flow of a distributed transaction. More on this, including practical examples of how to interact with an actual database, how to do compensation, how to do rollback, etc. please refer to [dtm-examples](https://github.com/dtm-labs/dtm-examples) for more examples. ## Chat Group diff --git a/helper/README-en.md b/helper/README-en.md index 31f8140..8c31e82 100644 --- a/helper/README-en.md +++ b/helper/README-en.md @@ -26,11 +26,11 @@ DTM is a distributed transaction framework which provides cross-service eventual [More](https://en.dtm.pub/other/using.html) ## Features -* Multiple languages support: SDK for Go, Java, PHP, C#, Python, Nodejs * Support for multiple transaction modes: SAGA, TCC, XA, Workflow, Outbox +* Multiple languages support: SDK for Go, Java, PHP, C#, Python, Nodejs * Better Outbox: 2-phase messages, a more elegant solution than Outbox, support multi-databases * Multiple database transaction support: Mysql, Redis, MongoDB, Postgres, TDSQL, etc. -* Support for multiple storage engines: Mysql (common), Redis (high performance), MongoDB (under planning) +* Support for multiple storage engines: Mysql (common), Redis (high performance), BoltDB (dev&test), MongoDB (under planning) * Support for multiple microservices architectures: [go-zero](https://github.com/zeromicro/go-zero), go-kratos/kratos, polarismesh/polaris * Support for high availability and easy horizontal scaling @@ -58,57 +58,65 @@ Suppose we want to perform an inter-bank transfer. The operations of transfer ou Here is an example to illustrate a solution of dtm to this problem: ``` bash -git clone https://github.com/dtm-labs/dtmcli-go-sample && cd dtmcli-go-sample +git clone https://github.com/dtm-labs/quick-start-sample.git && cd quick-start-sample/workflow-grpc go run main.go ``` ## Code -### Use +### Usage ``` go - // business micro-service address - const qsBusi = "http://localhost:8081/api/busi_saga" - // The address where DtmServer serves DTM, which is a url - DtmServer := "http://localhost:36789/api/dtmsvr" - req := &gin.H{"amount": 30} // micro-service payload - // DtmServer is the address of DTM micro-service - saga := dtmcli.NewSaga(DtmServer, shortuuid.New()). - // add a TransOut sub-transaction,forward operation with url: qsBusi+"/TransOut", reverse compensation operation with url: qsBusi+"/TransOutCom" - Add(qsBusi+"/TransOut", qsBusi+"/TransOutCom", req). - // add a TransIn sub-transaction, forward operation with url: qsBusi+"/TransIn", reverse compensation operation with url: qsBusi+"/TransInCom" - Add(qsBusi+"/TransIn", qsBusi+"/TransInCom", req) - // submit the created saga transaction,dtm ensures all sub-transactions either complete or get revoked - err := saga.Submit() -``` +wfName := "workflow-grpc" +err = workflow.Register(wfName, func(wf *workflow.Workflow, data []byte) error { + // ... + // Define a transaction branch for TransOut + wf.NewBranch().OnRollback(func(bb *dtmcli.BranchBarrier) error { + // compensation for TransOut + _, err := busiCli.TransOutRevert(wf.Context, &req) + return err + }) + _, err = busiCli.TransOut(wf.Context, &req) + // check error + + // Define another transaction branch for TransIn + wf.NewBranch().OnRollback(func(bb *dtmcli.BranchBarrier) error { + _, err := busiCli.TransInRevert(wf.Context, &req) + return err + }) + _, err = busiCli.TransIn(wf.Context, &req) + return err +} + +// ... +req := busi.BusiReq{Amount: 30, TransInResult: ""} +data, err := proto.Marshal(&req) + +// Execute workflow +err = workflow.Execute(wfName, shortuuid.New(), data) +logger.Infof("result of workflow.Execute is: %v", err) -When the above code runs, we can see in the console that services TransOut, TransIn has been called. - -#### Timing diagram -A timing diagram for a successfully completed SAGA transaction would be as follows: +``` -saga-success +When the above code runs, we can see in the console that services `TransOut`, `TransIn` has been called. #### Rollback upon failure If any forward operation fails, DTM invokes the corresponding compensating operation of each sub-transaction to roll back, after which the transaction is successfully rolled back. -Let's purposely fail the forward operation of the second sub-transaction and watch what happens +Let's purposely trigger the failure of the second sub-transaction and watch what happens ``` go -app.POST(qsBusiAPI+"/TransIn", func(c *gin.Context) { - log.Printf("TransIn") - // c.JSON(200, "") - c.JSON(409, "") // Status 409 for Failure. Won't be retried +// req := busi.BusiReq{Amount: 30, TransInResult: ""} +req := busi.BusiReq{Amount: 30, TransInResult: "FAILURE"} }) ``` +we can see in the console that services `TransOut`, `TransIn`, `TransOutRevert` has been called The timing diagram for the intended failure is as follows: -saga-failed - ## More examples If you want more quick start examples, please refer to [dtm-labs/quick-start-sample](https://github.com/dtm-labs/quick-start-sample) -The above example mainly demonstrates the flow of a distributed transaction. More on this, including practical examples of how to interface with an actual database, how to do compensation, how to do rollback, etc. please refer to [dtm-examples](https://github.com/dtm-labs/dtm-examples) for more examples. +The above example mainly demonstrates the flow of a distributed transaction. More on this, including practical examples of how to interact with an actual database, how to do compensation, how to do rollback, etc. please refer to [dtm-examples](https://github.com/dtm-labs/dtm-examples) for more examples. ## Chat Group From b9ee002617fceb126d76d61b0fb006ec04a32008 Mon Sep 17 00:00:00 2001 From: yedf2 <120050102@qq.com> Date: Mon, 1 Aug 2022 11:14:26 +0800 Subject: [PATCH 3/4] update readme-en --- README.md | 3 +-- helper/README-en.md | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 8c31e82..2fc52d3 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,7 @@ DTM can be applied to data consistency issues in a large number of scenarios, he * [cache management](https://en.dtm.pub/app/cache.html): thoroughly guarantee the cache final consistency and strong consistency * [flash-sales to deduct inventory](https://en.dtm.pub/app/flash.html): in extreme cases, it is also possible to ensure that the precise inventory in Redis is exactly the same as the final order created, without the need for manual adjustment * [Non-monolithic order system](https://en.dtm.pub/app/order.html): Dramatically simplifies the architecture -* [Event publishing/subscription](https://en.dtm.pub/practice/msg.html): better outbox model +* [Event publishing/subscription](https://en.dtm.pub/practice/msg.html): better outbox pattern ## [Cook Book](https://en.dtm.pub) @@ -111,7 +111,6 @@ req := busi.BusiReq{Amount: 30, TransInResult: "FAILURE"} ``` we can see in the console that services `TransOut`, `TransIn`, `TransOutRevert` has been called -The timing diagram for the intended failure is as follows: ## More examples If you want more quick start examples, please refer to [dtm-labs/quick-start-sample](https://github.com/dtm-labs/quick-start-sample) diff --git a/helper/README-en.md b/helper/README-en.md index 8c31e82..2fc52d3 100644 --- a/helper/README-en.md +++ b/helper/README-en.md @@ -39,7 +39,7 @@ DTM can be applied to data consistency issues in a large number of scenarios, he * [cache management](https://en.dtm.pub/app/cache.html): thoroughly guarantee the cache final consistency and strong consistency * [flash-sales to deduct inventory](https://en.dtm.pub/app/flash.html): in extreme cases, it is also possible to ensure that the precise inventory in Redis is exactly the same as the final order created, without the need for manual adjustment * [Non-monolithic order system](https://en.dtm.pub/app/order.html): Dramatically simplifies the architecture -* [Event publishing/subscription](https://en.dtm.pub/practice/msg.html): better outbox model +* [Event publishing/subscription](https://en.dtm.pub/practice/msg.html): better outbox pattern ## [Cook Book](https://en.dtm.pub) @@ -111,7 +111,6 @@ req := busi.BusiReq{Amount: 30, TransInResult: "FAILURE"} ``` we can see in the console that services `TransOut`, `TransIn`, `TransOutRevert` has been called -The timing diagram for the intended failure is as follows: ## More examples If you want more quick start examples, please refer to [dtm-labs/quick-start-sample](https://github.com/dtm-labs/quick-start-sample) From f8928e0b850a22b6bb876eb8629ca935490c9ca8 Mon Sep 17 00:00:00 2001 From: yedf2 <120050102@qq.com> Date: Tue, 2 Aug 2022 10:54:02 +0800 Subject: [PATCH 4/4] default success --- test/busi/quick_start.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/busi/quick_start.go b/test/busi/quick_start.go index 0d40042..ce607ae 100644 --- a/test/busi/quick_start.go +++ b/test/busi/quick_start.go @@ -37,8 +37,8 @@ func QsStartSvr() { func qsAddRoute(app *gin.Engine) { app.POST(qsBusiAPI+"/TransIn", func(c *gin.Context) { log.Printf("TransIn") - // c.JSON(200, "") - c.JSON(409, "") // Status 409 for Failure. Won't be retried + c.JSON(200, "") + // c.JSON(409, "") // Status 409 for Failure. Won't be retried }) app.POST(qsBusiAPI+"/TransInCompensate", func(c *gin.Context) { log.Printf("TransInCompensate")