You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

tx.go 4.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package sql
  18. import (
  19. "context"
  20. "database/sql/driver"
  21. "sync"
  22. "github.com/seata/seata-go/pkg/datasource/sql/datasource"
  23. "github.com/seata/seata-go/pkg/protocol/branch"
  24. "github.com/seata/seata-go/pkg/protocol/message"
  25. "github.com/seata/seata-go/pkg/util/log"
  26. "github.com/seata/seata-go/pkg/datasource/sql/types"
  27. )
  28. const REPORT_RETRY_COUNT = 5
  29. var (
  30. hl sync.RWMutex
  31. txHooks []txHook
  32. )
  33. func RegisterTxHook(h txHook) {
  34. hl.Lock()
  35. defer hl.Unlock()
  36. txHooks = append(txHooks, h)
  37. }
  38. func CleanTxHooks() {
  39. hl.Lock()
  40. defer hl.Unlock()
  41. txHooks = make([]txHook, 0, 4)
  42. }
  43. type (
  44. txOption func(tx *Tx)
  45. txHook interface {
  46. BeforeCommit(tx *Tx)
  47. BeforeRollback(tx *Tx)
  48. }
  49. )
  50. func newTx(opts ...txOption) (driver.Tx, error) {
  51. tx := new(Tx)
  52. for i := range opts {
  53. opts[i](tx)
  54. }
  55. if err := tx.init(); err != nil {
  56. return nil, err
  57. }
  58. return tx, nil
  59. }
  60. // withDriverConn
  61. func withDriverConn(conn *Conn) txOption {
  62. return func(t *Tx) {
  63. t.conn = conn
  64. }
  65. }
  66. // withOriginTx
  67. func withOriginTx(tx driver.Tx) txOption {
  68. return func(t *Tx) {
  69. t.target = tx
  70. }
  71. }
  72. // withTxCtx
  73. func withTxCtx(ctx *types.TransactionContext) txOption {
  74. return func(t *Tx) {
  75. t.ctx = ctx
  76. }
  77. }
  78. // Tx
  79. type Tx struct {
  80. conn *Conn
  81. ctx *types.TransactionContext
  82. target driver.Tx
  83. }
  84. // Commit do commit action
  85. func (tx *Tx) Commit() error {
  86. tx.beforeCommit()
  87. return tx.commitOnLocal()
  88. }
  89. func (tx *Tx) beforeCommit() {
  90. if len(txHooks) != 0 {
  91. hl.RLock()
  92. defer hl.RUnlock()
  93. for i := range txHooks {
  94. txHooks[i].BeforeCommit(tx)
  95. }
  96. }
  97. }
  98. func (tx *Tx) Rollback() error {
  99. if len(txHooks) != 0 {
  100. hl.RLock()
  101. defer hl.RUnlock()
  102. for i := range txHooks {
  103. txHooks[i].BeforeRollback(tx)
  104. }
  105. }
  106. return tx.target.Rollback()
  107. }
  108. // init
  109. func (tx *Tx) init() error {
  110. return nil
  111. }
  112. // commitOnLocal
  113. func (tx *Tx) commitOnLocal() error {
  114. return tx.target.Commit()
  115. }
  116. // register
  117. func (tx *Tx) register(ctx *types.TransactionContext) error {
  118. if !ctx.HasUndoLog() || !ctx.HasLockKey() {
  119. return nil
  120. }
  121. lockKey := ""
  122. for _, v := range ctx.LockKeys {
  123. lockKey += v + ";"
  124. }
  125. request := message.BranchRegisterRequest{
  126. Xid: ctx.XaID,
  127. BranchType: branch.BranchType(ctx.TransType),
  128. ResourceId: ctx.ResourceID,
  129. LockKey: lockKey,
  130. ApplicationData: nil,
  131. }
  132. dataSourceManager := datasource.GetDataSourceManager(branch.BranchType(ctx.TransType))
  133. branchId, err := dataSourceManager.BranchRegister(context.Background(), "", request)
  134. if err != nil {
  135. log.Infof("Failed to report branch status: %s", err.Error())
  136. return err
  137. }
  138. ctx.BranchID = uint64(branchId)
  139. return nil
  140. }
  141. // report
  142. func (tx *Tx) report(success bool) error {
  143. if tx.ctx.BranchID == 0 {
  144. return nil
  145. }
  146. status := getStatus(success)
  147. request := message.BranchReportRequest{
  148. Xid: tx.ctx.XaID,
  149. BranchId: int64(tx.ctx.BranchID),
  150. ResourceId: tx.ctx.ResourceID,
  151. Status: status,
  152. }
  153. dataSourceManager := datasource.GetDataSourceManager(branch.BranchType(tx.ctx.TransType))
  154. retry := REPORT_RETRY_COUNT
  155. for retry > 0 {
  156. err := dataSourceManager.BranchReport(context.Background(), request)
  157. if err != nil {
  158. retry--
  159. log.Infof("Failed to report [%s / %s] commit done [%s] Retry Countdown: %s", tx.ctx.BranchID, tx.ctx.XaID, success, retry)
  160. if retry == 0 {
  161. log.Infof("Failed to report branch status: %s", err.Error())
  162. return err
  163. }
  164. } else {
  165. return nil
  166. }
  167. }
  168. return nil
  169. }
  170. func getStatus(success bool) branch.BranchStatus {
  171. if success {
  172. return branch.BranchStatusPhaseoneDone
  173. } else {
  174. return branch.BranchStatusPhaseoneFailed
  175. }
  176. }