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_resource.go 5.8 kB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. "fmt"
  21. "sync"
  22. )
  23. import (
  24. "github.com/seata/seata-go/pkg/common/log"
  25. "github.com/seata/seata-go/pkg/protocol/branch"
  26. "github.com/seata/seata-go/pkg/protocol/message"
  27. "github.com/seata/seata-go/pkg/protocol/resource"
  28. "github.com/seata/seata-go/pkg/remoting/getty"
  29. "github.com/seata/seata-go/pkg/rm"
  30. "github.com/seata/seata-go/pkg/rm/common/remoting"
  31. "github.com/seata/seata-go/pkg/rm/tcc/api"
  32. )
  33. var (
  34. tCCResourceManager *TCCResourceManager
  35. onceTCCResourceManager = &sync.Once{}
  36. )
  37. type TCCResource struct {
  38. TCCServiceBean TCCService
  39. ResourceGroupId string `default:"DEFAULT"`
  40. AppName string
  41. ActionName string
  42. }
  43. func (t *TCCResource) GetResourceGroupId() string {
  44. return t.ResourceGroupId
  45. }
  46. func (t *TCCResource) GetResourceId() string {
  47. return t.ActionName
  48. }
  49. func (t *TCCResource) GetBranchType() branch.BranchType {
  50. return branch.BranchTypeTCC
  51. }
  52. func init() {
  53. rm.RegisterResourceManager(GetTCCResourceManagerInstance())
  54. }
  55. func GetTCCResourceManagerInstance() *TCCResourceManager {
  56. if tCCResourceManager == nil {
  57. onceTCCResourceManager.Do(func() {
  58. tCCResourceManager = &TCCResourceManager{
  59. resourceManagerMap: sync.Map{},
  60. rmRemoting: remoting.GetRMRemotingInstance(),
  61. }
  62. })
  63. }
  64. return tCCResourceManager
  65. }
  66. type TCCResourceManager struct {
  67. rmRemoting *remoting.RMRemoting
  68. // resourceID -> resource
  69. resourceManagerMap sync.Map
  70. }
  71. // register transaction branch
  72. func (t *TCCResourceManager) BranchRegister(ctx context.Context, branchType branch.BranchType, resourceId, clientId, xid, applicationData, lockKeys string) (int64, error) {
  73. request := message.BranchRegisterRequest{
  74. Xid: xid,
  75. BranchType: t.GetBranchType(),
  76. ResourceId: resourceId,
  77. LockKey: lockKeys,
  78. ApplicationData: []byte(applicationData),
  79. }
  80. resp, err := getty.GetGettyRemotingClient().SendSyncRequest(request)
  81. if err != nil || resp == nil {
  82. log.Errorf("BranchRegister error: %v, res %v", err.Error(), resp)
  83. return 0, err
  84. }
  85. return resp.(message.BranchRegisterResponse).BranchId, nil
  86. }
  87. func (t *TCCResourceManager) BranchReport(ctx context.Context, ranchType branch.BranchType, xid string, branchId int64, status branch.BranchStatus, applicationData string) error {
  88. //TODO implement me
  89. panic("implement me")
  90. }
  91. func (t *TCCResourceManager) LockQuery(ctx context.Context, ranchType branch.BranchType, resourceId, xid, lockKeys string) (bool, error) {
  92. //TODO implement me
  93. panic("implement me")
  94. }
  95. func (t *TCCResourceManager) UnregisterResource(resource resource.Resource) error {
  96. //TODO implement me
  97. panic("implement me")
  98. }
  99. func (t *TCCResourceManager) RegisterResource(resource resource.Resource) error {
  100. if _, ok := resource.(*TCCResource); !ok {
  101. panic(fmt.Sprintf("register tcc resource error, TCCResource is needed, param %v", resource))
  102. }
  103. t.resourceManagerMap.Store(resource.GetResourceId(), resource)
  104. return t.rmRemoting.RegisterResource(resource)
  105. }
  106. func (t *TCCResourceManager) GetManagedResources() sync.Map {
  107. return t.resourceManagerMap
  108. }
  109. // Commit a branch transaction
  110. func (t *TCCResourceManager) BranchCommit(ctx context.Context, ranchType branch.BranchType, xid string, branchID int64, resourceID string, applicationData []byte) (branch.BranchStatus, error) {
  111. var tccResource *TCCResource
  112. if resource, ok := t.resourceManagerMap.Load(resourceID); !ok {
  113. err := fmt.Errorf("TCC resource is not exist, resourceId: %s", resourceID)
  114. return 0, err
  115. } else {
  116. tccResource, _ = resource.(*TCCResource)
  117. }
  118. err := tccResource.TCCServiceBean.Commit(ctx, t.getBusinessActionContext(xid, branchID, resourceID, applicationData))
  119. if err != nil {
  120. return branch.BranchStatusPhasetwoCommitFailedRetryable, err
  121. }
  122. return branch.BranchStatusPhasetwoCommitted, err
  123. }
  124. func (t *TCCResourceManager) getBusinessActionContext(xid string, branchID int64, resourceID string, applicationData []byte) api.BusinessActionContext {
  125. return api.BusinessActionContext{
  126. Xid: xid,
  127. BranchId: string(branchID),
  128. ActionName: resourceID,
  129. // todo get ActionContext
  130. //ActionContext:,
  131. }
  132. }
  133. // Rollback a branch transaction
  134. func (t *TCCResourceManager) BranchRollback(ctx context.Context, ranchType branch.BranchType, xid string, branchID int64, resourceID string, applicationData []byte) (branch.BranchStatus, error) {
  135. var tccResource *TCCResource
  136. if resource, ok := t.resourceManagerMap.Load(resourceID); !ok {
  137. err := fmt.Errorf("CC resource is not exist, resourceId: %s", resourceID)
  138. return 0, err
  139. } else {
  140. tccResource, _ = resource.(*TCCResource)
  141. }
  142. err := tccResource.TCCServiceBean.Rollback(ctx, t.getBusinessActionContext(xid, branchID, resourceID, applicationData))
  143. if err != nil {
  144. return branch.BranchStatusPhasetwoRollbacked, err
  145. }
  146. return branch.BranchStatusPhasetwoRollbackFailedRetryable, err
  147. }
  148. func (t *TCCResourceManager) GetBranchType() branch.BranchType {
  149. return branch.BranchTypeTCC
  150. }

Go Implementation For Seata