forked from apache/incubator-seata-go
/
db_based_session_manager.go
149 lines (134 loc) · 5.23 KB
/
db_based_session_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
package holder
import (
"github.com/pkg/errors"
)
import (
"github.com/dk-lockdown/seata-golang/base/meta"
"github.com/dk-lockdown/seata-golang/pkg/uuid"
"github.com/dk-lockdown/seata-golang/tc/config"
"github.com/dk-lockdown/seata-golang/tc/model"
"github.com/dk-lockdown/seata-golang/tc/session"
)
type DataBaseSessionManager struct {
TaskName string
conf config.DBStoreConfig
TransactionStoreManager TransactionStoreManager
}
func NewDataBaseSessionManager(taskName string, conf config.DBStoreConfig) SessionManager {
logStore := &LogStoreDataBaseDAO{engine: conf.Engine}
transactionStoreManager := &DBTransactionStoreManager{
logQueryLimit: conf.LogQueryLimit,
LogStore: logStore,
}
sessionManager := &DataBaseSessionManager{
TaskName: taskName,
conf: conf,
TransactionStoreManager: transactionStoreManager,
}
return sessionManager
}
func (sessionManager *DataBaseSessionManager) AddGlobalSession(session *session.GlobalSession) error {
if sessionManager.TaskName == "" {
ret := sessionManager.TransactionStoreManager.WriteSession(LogOperationGlobalAdd, session)
if !ret {
return errors.New("addGlobalSession failed.")
}
} else {
ret := sessionManager.TransactionStoreManager.WriteSession(LogOperationGlobalUpdate, session)
if !ret {
return errors.New("addGlobalSession failed.")
}
}
return nil
}
func (sessionManager *DataBaseSessionManager) FindGlobalSession(xid string) *session.GlobalSession {
return sessionManager.FindGlobalSessionWithBranchSessions(xid, true)
}
func (sessionManager *DataBaseSessionManager) FindGlobalSessionWithBranchSessions(xid string, withBranchSessions bool) *session.GlobalSession {
return sessionManager.TransactionStoreManager.ReadSessionWithBranchSessions(xid, withBranchSessions)
}
func (sessionManager *DataBaseSessionManager) UpdateGlobalSessionStatus(session *session.GlobalSession, status meta.GlobalStatus) error {
if sessionManager.TaskName != "" {
return nil
}
session.Status = status
ret := sessionManager.TransactionStoreManager.WriteSession(LogOperationGlobalUpdate, session)
if !ret {
return errors.New("updateGlobalSessionStatus failed.")
}
return nil
}
func (sessionManager *DataBaseSessionManager) RemoveGlobalSession(session *session.GlobalSession) error {
ret := sessionManager.TransactionStoreManager.WriteSession(LogOperationGlobalRemove, session)
if !ret {
return errors.New("removeGlobalSession failed.")
}
return nil
}
func (sessionManager *DataBaseSessionManager) AddBranchSession(globalSession *session.GlobalSession, session *session.BranchSession) error {
if sessionManager.TaskName != "" {
return nil
}
ret := sessionManager.TransactionStoreManager.WriteSession(LogOperationBranchAdd, session)
if !ret {
return errors.New("addBranchSession failed.")
}
return nil
}
func (sessionManager *DataBaseSessionManager) UpdateBranchSessionStatus(session *session.BranchSession, status meta.BranchStatus) error {
if sessionManager.TaskName != "" {
return nil
}
ret := sessionManager.TransactionStoreManager.WriteSession(LogOperationBranchUpdate, session)
if !ret {
return errors.New("addBranchSession failed.")
}
return nil
}
func (sessionManager *DataBaseSessionManager) RemoveBranchSession(globalSession *session.GlobalSession, session *session.BranchSession) error {
if sessionManager.TaskName != "" {
return nil
}
ret := sessionManager.TransactionStoreManager.WriteSession(LogOperationBranchRemove, session)
if !ret {
return errors.New("addBranchSession failed.")
}
return nil
}
func (sessionManager *DataBaseSessionManager) AllSessions() []*session.GlobalSession {
if sessionManager.TaskName == ASYNC_COMMITTING_SESSION_MANAGER_NAME {
return sessionManager.FindGlobalSessions(model.SessionCondition{
Statuses: []meta.GlobalStatus{meta.GlobalStatusAsyncCommitting},
})
} else if sessionManager.TaskName == RETRY_COMMITTING_SESSION_MANAGER_NAME {
return sessionManager.FindGlobalSessions(model.SessionCondition{
Statuses: []meta.GlobalStatus{meta.GlobalStatusCommitRetrying},
})
} else if sessionManager.TaskName == RETRY_ROLLBACKING_SESSION_MANAGER_NAME {
ss := sessionManager.FindGlobalSessions(model.SessionCondition{
Statuses: []meta.GlobalStatus{meta.GlobalStatusRollbackRetrying,
meta.GlobalStatusRollbacking,
meta.GlobalStatusTimeoutRollbacking,
meta.GlobalStatusTimeoutRollbackRetrying,
},
})
return ss
} else {
return sessionManager.FindGlobalSessions(model.SessionCondition{
Statuses: []meta.GlobalStatus{meta.GlobalStatusUnknown, meta.GlobalStatusBegin,
meta.GlobalStatusCommitting, meta.GlobalStatusCommitRetrying, meta.GlobalStatusRollbacking,
meta.GlobalStatusRollbackRetrying, meta.GlobalStatusTimeoutRollbacking, meta.GlobalStatusTimeoutRollbackRetrying,
meta.GlobalStatusAsyncCommitting,
},
})
}
}
func (sessionManager *DataBaseSessionManager) FindGlobalSessions(condition model.SessionCondition) []*session.GlobalSession {
return sessionManager.TransactionStoreManager.ReadSessionWithSessionCondition(condition)
}
func (sessionManager *DataBaseSessionManager) Reload() {
maxSessionId := sessionManager.TransactionStoreManager.GetCurrentMaxSessionId()
if maxSessionId > uuid.UUID {
uuid.SetUUID(uuid.UUID, maxSessionId)
}
}