-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathdatasource_manager.go
157 lines (141 loc) · 4.74 KB
/
datasource_manager.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package mysql
import (
"context"
"fmt"
"sync"
"github.com/opentrx/seata-golang/v2/pkg/apis"
"github.com/opentrx/seata-golang/v2/pkg/client/base/model"
"github.com/opentrx/seata-golang/v2/pkg/util/log"
)
var dataSourceManager DataSourceManager
type DataSourceManager struct {
ResourceCache map[string]*connector
connections map[string]*mysqlConn
sync.Mutex
}
func init() {
dataSourceManager = DataSourceManager{
ResourceCache: make(map[string]*connector),
connections: make(map[string]*mysqlConn),
}
}
func GetDataSourceManager() DataSourceManager {
return dataSourceManager
}
func RegisterResource(dsn string) {
cfg, err := ParseDSN(dsn)
if err == nil {
c := &connector{
cfg: cfg,
}
dataSourceManager.ResourceCache[c.cfg.DBName] = c
InitTableMetaCache(c.cfg.DBName)
}
}
func (resourceManager DataSourceManager) GetConnection(resourceID string) *mysqlConn {
conn, ok := resourceManager.connections[resourceID]
if ok && conn.IsValid() {
return conn
}
resourceManager.Lock()
defer resourceManager.Unlock()
conn, ok = resourceManager.connections[resourceID]
if ok && conn.IsValid() {
return conn
}
db := resourceManager.ResourceCache[resourceID]
connection, err := db.Connect(context.Background())
if err != nil {
log.Error(err)
}
conn = connection.(*mysqlConn)
resourceManager.connections[resourceID] = conn
return conn
}
func (resourceManager DataSourceManager) BranchCommit(ctx context.Context, request *apis.BranchCommitRequest) (*apis.BranchCommitResponse, error) {
db := resourceManager.ResourceCache[request.ResourceID]
if db == nil {
log.Errorf("Data resource is not exist, resourceID: %s", request.ResourceID)
return &apis.BranchCommitResponse{
ResultCode: apis.ResultCodeFailed,
Message: fmt.Sprintf("Data resource is not exist, resourceID: %s", request.ResourceID),
}, nil
}
//todo 改为异步批量操作
undoLogManager := GetUndoLogManager()
conn := resourceManager.GetConnection(request.ResourceID)
if conn == nil || !conn.IsValid() {
return &apis.BranchCommitResponse{
ResultCode: apis.ResultCodeFailed,
Message: "Connection is not valid",
}, nil
}
err := undoLogManager.DeleteUndoLog(conn, request.XID, request.BranchID)
if err != nil {
log.Errorf("[stacktrace]branchCommit failed. xid:[%s], branchID:[%d], resourceID:[%s], branchType:[%d], applicationData:[%v]",
request.XID, request.BranchID, request.ResourceID, request.BranchType, request.ApplicationData)
log.Error(err)
return &apis.BranchCommitResponse{
ResultCode: apis.ResultCodeFailed,
Message: err.Error(),
}, nil
}
return &apis.BranchCommitResponse{
ResultCode: apis.ResultCodeSuccess,
XID: request.XID,
BranchID: request.BranchID,
BranchStatus: apis.PhaseTwoCommitted,
}, nil
}
func (resourceManager DataSourceManager) BranchRollback(ctx context.Context, request *apis.BranchRollbackRequest) (*apis.BranchRollbackResponse, error) {
db := resourceManager.ResourceCache[request.ResourceID]
if db == nil {
return &apis.BranchRollbackResponse{
ResultCode: apis.ResultCodeFailed,
Message: fmt.Sprintf("Data resource is not exist, resourceID: %s", request.ResourceID),
}, nil
}
//todo 使用前镜数据覆盖当前数据
undoLogManager := GetUndoLogManager()
conn, err := db.Connect(context.Background())
defer conn.Close()
if err != nil {
log.Error(err)
return &apis.BranchRollbackResponse{
ResultCode: apis.ResultCodeSuccess,
XID: request.XID,
BranchID: request.BranchID,
BranchStatus: apis.PhaseTwoRollbackFailedRetryable,
}, nil
}
c := conn.(*mysqlConn)
err = undoLogManager.Undo(c, request.XID, request.BranchID, db.cfg.DBName)
if err != nil {
log.Errorf("[stacktrace]branchRollback failed. xid:[%s], branchID:[%d], resourceID:[%s], branchType:[%d], applicationData:[%v]",
request.XID, request.BranchID, request.ResourceID, request.BranchType, request.ApplicationData)
log.Error(err)
return &apis.BranchRollbackResponse{
ResultCode: apis.ResultCodeSuccess,
XID: request.XID,
BranchID: request.BranchID,
BranchStatus: apis.PhaseTwoRollbackFailedRetryable,
}, nil
}
return &apis.BranchRollbackResponse{
ResultCode: apis.ResultCodeSuccess,
XID: request.XID,
BranchID: request.BranchID,
BranchStatus: apis.PhaseTwoRolledBack,
}, nil
}
func (resourceManager DataSourceManager) RegisterResource(resource model.Resource) {
if connector, ok := resource.(*connector); ok {
resourceManager.ResourceCache[resource.GetResourceID()] = connector
}
}
func (resourceManager DataSourceManager) UnregisterResource(resource model.Resource) {
delete(resourceManager.ResourceCache, resource.GetResourceID())
}
func (resourceManager DataSourceManager) GetBranchType() apis.BranchSession_BranchType {
return apis.AT
}