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.

resource_manager.go 4.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 rm
  18. import (
  19. "context"
  20. "fmt"
  21. "sync"
  22. )
  23. import (
  24. "github.com/seata/seata-go/pkg/protocol/branch"
  25. "github.com/seata/seata-go/pkg/protocol/resource"
  26. )
  27. var (
  28. // BranchType -> ResourceManager
  29. resourceManagerMap sync.Map
  30. // singletone ResourceManager
  31. rmFacadeInstance *ResourceManager
  32. onceRMFacade = &sync.Once{}
  33. )
  34. func GetResourceManagerInstance() *ResourceManager {
  35. if rmFacadeInstance == nil {
  36. onceRMFacade.Do(func() {
  37. rmFacadeInstance = &ResourceManager{}
  38. })
  39. }
  40. return rmFacadeInstance
  41. }
  42. type ResourceManager struct {
  43. }
  44. // 将事务管理器注册到这里
  45. func RegisterResourceManager(resourceManager resource.ResourceManager) {
  46. resourceManagerMap.Store(resourceManager.GetBranchType(), resourceManager)
  47. }
  48. func (*ResourceManager) GetResourceManager(branchType branch.BranchType) resource.ResourceManager {
  49. rm, ok := resourceManagerMap.Load(branchType)
  50. if !ok {
  51. panic(fmt.Sprintf("No ResourceManager for BranchType: %v", branchType))
  52. }
  53. return rm.(resource.ResourceManager)
  54. }
  55. // Commit a branch transaction
  56. func (d *ResourceManager) BranchCommit(ctx context.Context, branchType branch.BranchType, xid string, branchId int64, resourceId string, applicationData []byte) (branch.BranchStatus, error) {
  57. return d.GetResourceManager(branchType).BranchCommit(ctx, branchType, xid, branchId, resourceId, applicationData)
  58. }
  59. // Rollback a branch transaction
  60. func (d *ResourceManager) BranchRollback(ctx context.Context, branchType branch.BranchType, xid string, branchId int64, resourceId string, applicationData []byte) (branch.BranchStatus, error) {
  61. return d.GetResourceManager(branchType).BranchRollback(ctx, branchType, xid, branchId, resourceId, applicationData)
  62. }
  63. // Branch register long
  64. func (d *ResourceManager) BranchRegister(ctx context.Context, branchType branch.BranchType, resourceId, clientId, xid, applicationData, lockKeys string) (int64, error) {
  65. return d.GetResourceManager(branchType).BranchRegister(ctx, branchType, resourceId, clientId, xid, applicationData, lockKeys)
  66. }
  67. // Branch report
  68. func (d *ResourceManager) BranchReport(ctx context.Context, branchType branch.BranchType, xid string, branchId int64, status branch.BranchStatus, applicationData string) error {
  69. return d.GetResourceManager(branchType).BranchReport(ctx, branchType, xid, branchId, status, applicationData)
  70. }
  71. // Lock query boolean
  72. func (d *ResourceManager) LockQuery(ctx context.Context, branchType branch.BranchType, resourceId, xid, lockKeys string) (bool, error) {
  73. return d.GetResourceManager(branchType).LockQuery(ctx, branchType, resourceId, xid, lockKeys)
  74. }
  75. // Register a model.Resource to be managed by model.Resource Manager
  76. func (d *ResourceManager) RegisterResource(resource resource.Resource) error {
  77. return d.GetResourceManager(resource.GetBranchType()).RegisterResource(resource)
  78. }
  79. // Unregister a model.Resource from the model.Resource Manager
  80. func (d *ResourceManager) UnregisterResource(resource resource.Resource) error {
  81. return d.GetResourceManager(resource.GetBranchType()).UnregisterResource(resource)
  82. }
  83. // Get all resources managed by this manager
  84. func (d *ResourceManager) GetManagedResources() sync.Map {
  85. return resourceManagerMap
  86. }
  87. // Get the model.BranchType
  88. func (d *ResourceManager) GetBranchType() branch.BranchType {
  89. panic("DefaultResourceManager isn't a real ResourceManager")
  90. }

Go Implementation For Seata