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.

tcc_service.go 3.9 kB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 tcc
  18. import (
  19. "context"
  20. "encoding/json"
  21. "fmt"
  22. "time"
  23. )
  24. import (
  25. "github.com/pkg/errors"
  26. )
  27. import (
  28. "github.com/seata/seata-go/pkg/common"
  29. "github.com/seata/seata-go/pkg/common/log"
  30. "github.com/seata/seata-go/pkg/common/net"
  31. "github.com/seata/seata-go/pkg/protocol/branch"
  32. "github.com/seata/seata-go/pkg/protocol/seatactx"
  33. context2 "github.com/seata/seata-go/pkg/protocol/transaction"
  34. "github.com/seata/seata-go/pkg/rm"
  35. api2 "github.com/seata/seata-go/pkg/rm/tcc/api"
  36. )
  37. type TCCService interface {
  38. Prepare(ctx context.Context, params interface{}) error
  39. Commit(ctx context.Context, businessActionContext api2.BusinessActionContext) error
  40. Rollback(ctx context.Context, businessActionContext api2.BusinessActionContext) error
  41. GetActionName() string
  42. //GetRemoteType() remoting.RemoteType
  43. //GetServiceType() remoting.ServiceType
  44. }
  45. type TCCServiceProxy struct {
  46. TCCService
  47. }
  48. func NewTCCServiceProxy(tccService TCCService) TCCService {
  49. if tccService == nil {
  50. panic("param tccService should not be nil")
  51. }
  52. // register resource
  53. tccResource := TCCResource{
  54. TCCServiceBean: tccService,
  55. ResourceGroupId: "DEFAULT",
  56. AppName: "",
  57. ActionName: tccService.GetActionName(),
  58. }
  59. err := rm.GetResourceManagerInstance().GetResourceManager(branch.BranchTypeTCC).RegisterResource(&tccResource)
  60. if err != nil {
  61. panic(fmt.Sprintf("NewTCCServiceProxy registerResource error: {%#v}", err.Error()))
  62. }
  63. return &TCCServiceProxy{
  64. TCCService: tccService,
  65. }
  66. }
  67. func (t *TCCServiceProxy) Prepare(ctx context.Context, param interface{}) error {
  68. if seatactx.HasXID(ctx) {
  69. err := t.RegisteBranch(ctx, param)
  70. if err != nil {
  71. return err
  72. }
  73. }
  74. return t.TCCService.Prepare(ctx, param)
  75. }
  76. func (t *TCCServiceProxy) RegisteBranch(ctx context.Context, param interface{}) error {
  77. // register transaction branch
  78. if !seatactx.HasXID(ctx) {
  79. err := errors.New("BranchRegister error, xid should not be nil")
  80. log.Errorf(err.Error())
  81. return err
  82. }
  83. tccContext := make(map[string]interface{}, 0)
  84. tccContext[common.StartTime] = time.Now().UnixNano() / 1e6
  85. tccContext[common.HostName] = net.GetLocalIp()
  86. tccContextStr, _ := json.Marshal(tccContext)
  87. branchId, err := rm.GetResourceManagerInstance().GetResourceManager(branch.BranchTypeTCC).BranchRegister(
  88. ctx, branch.BranchTypeTCC, t.GetActionName(), "", seatactx.GetXID(ctx), string(tccContextStr), "")
  89. if err != nil {
  90. err = errors.New(fmt.Sprintf("BranchRegister error: %v", err.Error()))
  91. log.Error(err.Error())
  92. return err
  93. }
  94. actionContext := &api2.BusinessActionContext{
  95. Xid: seatactx.GetXID(ctx),
  96. BranchId: string(branchId),
  97. ActionName: t.GetActionName(),
  98. ActionContext: param,
  99. }
  100. seatactx.SetBusinessActionContext(ctx, actionContext)
  101. return nil
  102. }
  103. func (t *TCCServiceProxy) GetTransactionInfo() context2.TransactionInfo {
  104. // todo replace with config
  105. return context2.TransactionInfo{
  106. TimeOut: 10000,
  107. Name: t.GetActionName(),
  108. //Propagation, Propagation
  109. //LockRetryInternal, int64
  110. //LockRetryTimes int64
  111. }
  112. }

Go Implementation For Seata