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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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/pkg/errors"
  23. "github.com/seata/seata-go/pkg/datasource/sql/datasource"
  24. "github.com/seata/seata-go/pkg/protocol/branch"
  25. "github.com/seata/seata-go/pkg/rm"
  26. "github.com/seata/seata-go/pkg/util/log"
  27. "github.com/seata/seata-go/pkg/datasource/sql/types"
  28. )
  29. const REPORT_RETRY_COUNT = 5
  30. var (
  31. hl sync.RWMutex
  32. txHooks []txHook
  33. )
  34. func RegisterTxHook(h txHook) {
  35. hl.Lock()
  36. defer hl.Unlock()
  37. txHooks = append(txHooks, h)
  38. }
  39. func CleanTxHooks() {
  40. hl.Lock()
  41. defer hl.Unlock()
  42. txHooks = make([]txHook, 0, 4)
  43. }
  44. type (
  45. txOption func(tx *Tx)
  46. txHook interface {
  47. BeforeCommit(tx *Tx)
  48. BeforeRollback(tx *Tx)
  49. }
  50. )
  51. func newTx(opts ...txOption) (driver.Tx, error) {
  52. tx := new(Tx)
  53. for i := range opts {
  54. opts[i](tx)
  55. }
  56. if err := tx.init(); err != nil {
  57. return nil, err
  58. }
  59. return tx, nil
  60. }
  61. // withDriverConn
  62. func withDriverConn(conn *Conn) txOption {
  63. return func(t *Tx) {
  64. t.conn = conn
  65. }
  66. }
  67. // withOriginTx
  68. func withOriginTx(tx driver.Tx) txOption {
  69. return func(t *Tx) {
  70. t.target = tx
  71. }
  72. }
  73. // withTxCtx
  74. func withTxCtx(ctx *types.TransactionContext) txOption {
  75. return func(t *Tx) {
  76. t.tranCtx = ctx
  77. }
  78. }
  79. // Tx
  80. type Tx struct {
  81. conn *Conn
  82. tranCtx *types.TransactionContext
  83. target driver.Tx
  84. }
  85. // Commit do commit action
  86. func (tx *Tx) Commit() error {
  87. tx.beforeCommit()
  88. return tx.commitOnLocal()
  89. }
  90. func (tx *Tx) beforeCommit() {
  91. if len(txHooks) != 0 {
  92. hl.RLock()
  93. defer hl.RUnlock()
  94. for i := range txHooks {
  95. txHooks[i].BeforeCommit(tx)
  96. }
  97. }
  98. }
  99. func (tx *Tx) Rollback() error {
  100. if len(txHooks) != 0 {
  101. hl.RLock()
  102. defer hl.RUnlock()
  103. for i := range txHooks {
  104. txHooks[i].BeforeRollback(tx)
  105. }
  106. }
  107. return tx.target.Rollback()
  108. }
  109. // init
  110. func (tx *Tx) init() error {
  111. return nil
  112. }
  113. // commitOnLocal
  114. func (tx *Tx) commitOnLocal() error {
  115. return tx.target.Commit()
  116. }
  117. // register
  118. func (tx *Tx) register(ctx *types.TransactionContext) error {
  119. if !ctx.HasUndoLog() || !ctx.HasLockKey() {
  120. return nil
  121. }
  122. lockKey := ""
  123. for k, _ := range ctx.LockKeys {
  124. lockKey += k + ";"
  125. }
  126. request := rm.BranchRegisterParam{
  127. Xid: ctx.XID,
  128. BranchType: ctx.TransactionMode.BranchType(),
  129. ResourceId: ctx.ResourceID,
  130. LockKeys: lockKey,
  131. }
  132. dataSourceManager := datasource.GetDataSourceManager(ctx.TransactionMode.BranchType())
  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.tranCtx.BranchID == 0 {
  144. return nil
  145. }
  146. status := getStatus(success)
  147. request := rm.BranchReportParam{
  148. Xid: tx.tranCtx.XID,
  149. BranchId: int64(tx.tranCtx.BranchID),
  150. Status: status,
  151. }
  152. dataSourceManager := datasource.GetDataSourceManager(tx.tranCtx.TransactionMode.BranchType())
  153. if dataSourceManager == nil {
  154. return errors.New("get dataSourceManager failed")
  155. }
  156. retry := REPORT_RETRY_COUNT
  157. for retry > 0 {
  158. err := dataSourceManager.BranchReport(context.Background(), request)
  159. if err != nil {
  160. retry--
  161. log.Infof("Failed to report [%s / %s] commit done [%s] Retry Countdown: %s", tx.tranCtx.BranchID, tx.tranCtx.XID, success, retry)
  162. if retry == 0 {
  163. log.Infof("Failed to report branch status: %s", err.Error())
  164. return err
  165. }
  166. } else {
  167. return nil
  168. }
  169. }
  170. return nil
  171. }
  172. func getStatus(success bool) branch.BranchStatus {
  173. if success {
  174. return branch.BranchStatusPhaseoneDone
  175. } else {
  176. return branch.BranchStatusPhaseoneFailed
  177. }
  178. }