Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add:msg_topic #4

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions docs/guide/e-msg.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,34 @@ app.GET(BusiAPI+"/QueryPreparedB", dtmutil.WrapHandler2(func(c *gin.Context) int

At this point, a complete two-stage message distributed transaction is written.

## Topic message

You can also make the 2-phase message transaction call by topic message. "topic" has the same semantics which is in message queue.

Firstly we subscribe a topic named `TransIn` for our business api:

> We can also manage topics in dtm's console.

```go
resp, err := dtmcli.GetRestyClient().R().SetQueryParams(map[string]string{
"topic": "TransIn",
"url": busi.Busi+"/SagaBTransIn",
"remark": "trans in api",
}).Get(dtmutil.DefaultHTTPServer + "/subscribe")
```

Then open the 2-phase message transaction and send message to the topic `TransIn`

```go
msg := dtmcli.NewMsg(DtmServer, shortuuid.New()).
AddTopic("TransIn", &TransReq{ Amount: 30 })
err := msg.DoAndSubmitDB(busi.Busi+"/QueryPreparedB", dbGet(), func(tx *sql.Tx) error {
return busi.SagaAdjustBalance(tx, busi.TransOutUID, -req.Amount)
})
```

Note that the change of topic needs to take a few seconds to take effect, which depends on the `ConfigUpdateInterval` configuration parameter.

## Run the example
If you want to run a successful example in its entirety, the steps are as follows.
1. run dtm
Expand Down
10 changes: 10 additions & 0 deletions docs/practice/msg.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,16 @@ This problem is completely solved by dtm's 2-phase message solution. dtm's 2-pha

So how do 2-phase message distinguish between in-progress and rolled back messages? The trick lies in the data inserted during the checkback. If the database transaction is still in progress at the time of the checkback, then the insert operation will be blocked by the in-progress transaction, because the insert operation will wait for the row lock held by the transaction. If the insert operation returns normally, then the local transaction in the database, which must have ended.

## Topic message principle

As mentioned in previous article, the 2-phase message can also implement branch transaction call by topic message.

The dtm server maintains a mapping of `topic -> APIs ` and stores it in database. The server reads the mapping information regularly and loads it into memory for cache.

When dtm server processes the 2-phase message request which includes topic message, it will use the cached mapping information to find corresponding APIs for each topic, and then call the APIs one by one.

Topic message decouples the distributed transaction caller and external services, which means we don't need to modify caller's code when external services' APIs change, and we can configure APIs dynamically.

## Common messages
2-phase messages can replace not only the local message table scheme, but also the normal message scheme. If you call Submit directly, then it is similar to the normal message scheme, but provides a more flexible and simple interface.

Expand Down