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.

at_resource_manager.go 4.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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"
  21. "fmt"
  22. "sync"
  23. "github.com/prometheus/client_golang/prometheus"
  24. "seata.apache.org/seata-go/pkg/datasource/sql/datasource"
  25. "seata.apache.org/seata-go/pkg/datasource/sql/types"
  26. "seata.apache.org/seata-go/pkg/datasource/sql/undo"
  27. "seata.apache.org/seata-go/pkg/protocol/branch"
  28. "seata.apache.org/seata-go/pkg/rm"
  29. serr "seata.apache.org/seata-go/pkg/util/errors"
  30. )
  31. func InitAT(cfg undo.Config, asyncCfg AsyncWorkerConfig) {
  32. atSourceManager := &ATSourceManager{
  33. resourceCache: sync.Map{},
  34. basic: datasource.NewBasicSourceManager(),
  35. rmRemoting: rm.GetRMRemotingInstance(),
  36. }
  37. undo.InitUndoConfig(cfg)
  38. atSourceManager.worker = NewAsyncWorker(prometheus.DefaultRegisterer, asyncCfg, atSourceManager)
  39. rm.GetRmCacheInstance().RegisterResourceManager(atSourceManager)
  40. }
  41. type ATSourceManager struct {
  42. resourceCache sync.Map
  43. worker *AsyncWorker
  44. basic *datasource.BasicSourceManager
  45. rmRemoting *rm.RMRemoting
  46. }
  47. func (a *ATSourceManager) GetBranchType() branch.BranchType {
  48. return branch.BranchTypeAT
  49. }
  50. // GetCachedResources get all resources managed by this manager
  51. func (a *ATSourceManager) GetCachedResources() *sync.Map {
  52. return &a.resourceCache
  53. }
  54. // RegisterResource register a Resource to be managed by Resource Manager
  55. func (a *ATSourceManager) RegisterResource(res rm.Resource) error {
  56. a.resourceCache.Store(res.GetResourceId(), res)
  57. return a.basic.RegisterResource(res)
  58. }
  59. // UnregisterResource unregister a Resource from the Resource Manager
  60. func (a *ATSourceManager) UnregisterResource(res rm.Resource) error {
  61. return a.basic.UnregisterResource(res)
  62. }
  63. // BranchRollback rollback a branch transaction
  64. func (a *ATSourceManager) BranchRollback(ctx context.Context, branchResource rm.BranchResource) (branch.BranchStatus, error) {
  65. var dbResource *DBResource
  66. if resource, ok := a.resourceCache.Load(branchResource.ResourceId); !ok {
  67. err := fmt.Errorf("DB resource is not exist, resourceId: %s", branchResource.ResourceId)
  68. return branch.BranchStatusUnknown, err
  69. } else {
  70. dbResource, _ = resource.(*DBResource)
  71. }
  72. undoMgr, err := undo.GetUndoLogManager(dbResource.dbType)
  73. if err != nil {
  74. return branch.BranchStatusUnknown, err
  75. }
  76. if err := undoMgr.RunUndo(ctx, branchResource.Xid, branchResource.BranchId, dbResource.db, dbResource.dbName); err != nil {
  77. transErr, ok := err.(*serr.SeataError)
  78. if !ok {
  79. return branch.BranchStatusPhaseoneFailed, err
  80. }
  81. if transErr.Code == serr.TransactionErrorCodeBranchRollbackFailedUnretriable {
  82. return branch.BranchStatusPhasetwoRollbackFailedUnretryable, nil
  83. }
  84. return branch.BranchStatusPhasetwoRollbackFailedRetryable, nil
  85. }
  86. return branch.BranchStatusPhasetwoRollbacked, nil
  87. }
  88. // BranchCommit commit the branch transaction
  89. func (a *ATSourceManager) BranchCommit(ctx context.Context, resource rm.BranchResource) (branch.BranchStatus, error) {
  90. a.worker.BranchCommit(ctx, resource)
  91. return branch.BranchStatusPhasetwoCommitted, nil
  92. }
  93. func (a *ATSourceManager) LockQuery(ctx context.Context, param rm.LockQueryParam) (bool, error) {
  94. return a.rmRemoting.LockQuery(param)
  95. }
  96. // BranchRegister branch transaction register
  97. func (a *ATSourceManager) BranchRegister(ctx context.Context, req rm.BranchRegisterParam) (int64, error) {
  98. return a.rmRemoting.BranchRegister(req)
  99. }
  100. // BranchReport Report status of transaction branch
  101. func (a *ATSourceManager) BranchReport(ctx context.Context, param rm.BranchReportParam) error {
  102. return a.rmRemoting.BranchReport(param)
  103. }
  104. func (a *ATSourceManager) CreateTableMetaCache(ctx context.Context, resID string, dbType types.DBType,
  105. db *sql.DB) (datasource.TableMetaCache, error) {
  106. return a.basic.CreateTableMetaCache(ctx, resID, dbType, db)
  107. }