-
Notifications
You must be signed in to change notification settings - Fork 0
/
transnode.go
444 lines (405 loc) · 10.4 KB
/
transnode.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
// transnode
package transact
import (
"time"
"github.com/breezedup/goserver.v3/core/basic"
"github.com/breezedup/goserver.v3/core/timer"
"sync"
"sync/atomic"
)
const (
///transact execute result
TransResult_Success int = iota
TransResult_Failed
TransResult_TimeOut
TransResult_Max
)
const (
///transact result
TransExeResult_Success TransExeResult = iota
TransExeResult_Failed
TransExeResult_Yield
TransExeResult_NullPointer
TransExeResult_NoStart
TransExeResult_NoSetHandler
TransExeResult_ChildNodeNotExist
TransExeResult_ChildNodeRepeateRet
TransExeResult_AsynFailed
TransExeResult_HadDone
TransExeResult_StartChildFailed
TransExeResult_UnsafeExecuteEnv
)
const (
///transact owner type
TransOwnerType_Invalid TransOwnerType = iota
TransOwnerType_Max
)
const (
///transact command type
TransCmd_Invalid TransCmd = iota
TransCmd_Commit
TransCmd_RollBack
)
const (
///transact
TransRootNodeLevel int = 0
DefaultTransactTimeout time.Duration = 30 * time.Second
)
var (
TransNodeIDNil = TransNodeID(0)
transStats = new(sync.Map)
)
type TransExeResult int
type TransOwnerType int
type TransCmd int
type TransNodeID int64
type TransNodeParam struct {
TId TransNodeID
Tt TransType
Ot TransOwnerType
Tct TransactCommitPolicy
Oid int
SkeletonID int
LevelNo int
AreaID int
TimeOut time.Duration
}
type TransResult struct {
RetCode int
RetFiels interface{}
}
const (
TransStatsOp_Exe = iota
TransStatsOp_Rollback
TransStatsOp_Commit
TransStatsOp_Yiled
TransStatsOp_Resume
TransStatsOp_Timeout
)
type TransStats struct {
ExecuteTimes int64
RollbackTimes int64
CommitTimes int64
TimeoutTimes int64
YieldTimes int64
ResumeTimes int64
TotalRuningTime int64
MaxRuningTime int64
}
func (stats *TransStats) incStats(op int) {
switch op {
case TransStatsOp_Exe:
atomic.AddInt64(&stats.ExecuteTimes, 1)
case TransStatsOp_Rollback:
atomic.AddInt64(&stats.RollbackTimes, 1)
case TransStatsOp_Commit:
atomic.AddInt64(&stats.CommitTimes, 1)
case TransStatsOp_Yiled:
atomic.AddInt64(&stats.YieldTimes, 1)
case TransStatsOp_Resume:
atomic.AddInt64(&stats.ResumeTimes, 1)
case TransStatsOp_Timeout:
atomic.AddInt64(&stats.TimeoutTimes, 1)
}
}
type TransCallback func(*TransNode)
type TransBrotherNotify func(*TransNode, TransExeResult)
type TransNode struct {
TransEnv *TransCtx
TransRep *TransResult
MyTnp *TransNodeParam
ParentTnp *TransNodeParam
ownerObj *basic.Object
Childs map[TransNodeID]*TransNodeParam
finChild map[TransNodeID]interface{}
timeHandle timer.TimerHandle
handler TransHandler
AsynCallback TransCallback
brothers map[*TransNode]TransBrotherNotify
createTime time.Time
start bool
yield bool
resume bool
done bool
owner *transactCoordinater
ud interface{}
}
func (this *TransNode) incStats(op int) {
if s, exist := transStats.Load(this.MyTnp.Tt); exist {
if stats, ok := s.(*TransStats); ok {
stats.incStats(op)
}
} else {
stats := &TransStats{}
transStats.Store(this.MyTnp.Tt, stats)
stats.incStats(op)
}
}
func (this *TransNode) statsRuningTime() {
if s, exist := transStats.Load(this.MyTnp.Tt); exist {
if stats, ok := s.(*TransStats); ok {
runingTime := int64(time.Now().Sub(this.createTime) / time.Millisecond)
if runingTime > stats.MaxRuningTime {
stats.MaxRuningTime = runingTime
}
stats.TotalRuningTime += runingTime
}
}
}
func (this *TransNode) execute(ud interface{}) TransExeResult {
if this == nil {
return TransExeResult_NullPointer
}
if this.handler == nil {
return TransExeResult_NoSetHandler
}
this.start = true
ret := this.handler.OnExcute(this, ud)
this.incStats(TransStatsOp_Exe)
if ret == TransExeResult_Yield {
return this.Yield()
}
return this.doneExecRet(ret)
}
func (this *TransNode) doneExecRet(ter TransExeResult) TransExeResult {
if this.done {
return TransExeResult_HadDone
}
if ter == TransExeResult_Success {
if len(this.Childs) == len(this.finChild) {
if this.MyTnp.LevelNo <= TransRootNodeLevel {
return this.commit()
} else {
if Config.tcs != nil {
this.TransRep.RetCode = TransResult_Success
Config.tcs.SendTransResult(this.ParentTnp, this.MyTnp, this.TransRep)
}
if this.MyTnp.Tct == TransactCommitPolicy_SelfDecide {
return this.commit()
}
}
}
} else {
if this.MyTnp.LevelNo == TransRootNodeLevel {
return this.rollback(TransNodeIDNil)
} else {
if Config.tcs != nil {
this.TransRep.RetCode = TransResult_Failed
Config.tcs.SendTransResult(this.ParentTnp, this.MyTnp, this.TransRep)
}
return this.rollback(TransNodeIDNil)
}
}
return TransExeResult_Success
}
func (this *TransNode) commit() TransExeResult {
defer this.owner.releaseTrans(this)
if this == nil {
return TransExeResult_NullPointer
}
if !this.start {
return TransExeResult_NoStart
}
if this.handler == nil {
return TransExeResult_NoSetHandler
}
if this.done {
return TransExeResult_HadDone
}
defer this.notifyBrother(TransExeResult_Success)
this.done = true
this.handler.OnCommit(this)
this.incStats(TransStatsOp_Commit)
this.statsRuningTime()
if len(this.Childs) > 0 && Config.tcs != nil {
for _, v := range this.Childs {
if v.Tct == TransactCommitPolicy_TwoPhase {
Config.tcs.SendCmdToTransNode(v, TransCmd_Commit)
}
}
}
return TransExeResult_Success
}
func (this *TransNode) rollback(exclude TransNodeID) TransExeResult {
defer this.owner.releaseTrans(this)
if this == nil {
return TransExeResult_NullPointer
}
if !this.start {
return TransExeResult_NoStart
}
if this.handler == nil {
return TransExeResult_NoSetHandler
}
if this.done {
return TransExeResult_HadDone
}
defer this.notifyBrother(TransExeResult_Failed)
this.done = true
this.handler.OnRollBack(this)
this.incStats(TransStatsOp_Rollback)
this.statsRuningTime()
if len(this.Childs) > 0 && Config.tcs != nil {
for k, v := range this.Childs {
if k != exclude && v.Tct == TransactCommitPolicy_TwoPhase {
Config.tcs.SendCmdToTransNode(v, TransCmd_RollBack)
}
}
}
return TransExeResult_Success
}
func (this *TransNode) timeout() TransExeResult {
if this == nil {
return TransExeResult_NullPointer
}
if !this.start {
return TransExeResult_NoStart
}
if this.handler == nil {
return TransExeResult_NoSetHandler
}
if this.done {
return TransExeResult_HadDone
}
if this.MyTnp.LevelNo > TransRootNodeLevel {
if Config.tcs != nil {
this.TransRep.RetCode = TransResult_TimeOut
Config.tcs.SendTransResult(this.ParentTnp, this.MyTnp, this.TransRep)
}
}
this.incStats(TransStatsOp_Timeout)
this.rollback(TransNodeIDNil)
return TransExeResult_Success
}
func (this *TransNode) childTransRep(child TransNodeID, retCode int, ud interface{}) TransExeResult {
if this == nil {
return TransExeResult_NullPointer
}
if this.handler == nil {
return TransExeResult_NoSetHandler
}
if !this.start {
return TransExeResult_NoStart
}
if this.done {
return TransExeResult_HadDone
}
if _, exist := this.Childs[child]; !exist {
return TransExeResult_ChildNodeNotExist
}
if this.finChild == nil {
this.finChild = make(map[TransNodeID]interface{})
}
if _, exist := this.finChild[child]; exist {
return TransExeResult_ChildNodeRepeateRet
}
this.finChild[child] = ud
ret := this.handler.OnChildTransRep(this, child, retCode, ud)
if retCode == TransResult_Success && ret == TransExeResult_Success {
// the child nodes are returned and also run their own end (note: they may be executed asynchronously)
if len(this.Childs) == len(this.finChild) && this.yield == this.resume {
if this.MyTnp.LevelNo == TransRootNodeLevel {
this.commit()
} else {
if Config.tcs != nil {
this.TransRep.RetCode = retCode
Config.tcs.SendTransResult(this.ParentTnp, this.MyTnp, this.TransRep)
}
if this.MyTnp.Tct == TransactCommitPolicy_SelfDecide {
this.commit()
}
}
}
} else {
// They are not the root, then the parent would like to report fails
if this.MyTnp.LevelNo > TransRootNodeLevel {
if Config.tcs != nil {
this.TransRep.RetCode = retCode
Config.tcs.SendTransResult(this.ParentTnp, this.MyTnp, this.TransRep)
}
}
var exclude TransNodeID
if retCode != TransResult_Success {
exclude = child
}
// Sub-transaction fails or times out or the results were not satisfactory, timing optimization, advance RollBack
this.rollback(exclude)
}
return TransExeResult_Success
}
func (this *TransNode) StartChildTrans(tnp *TransNodeParam, ud interface{}, timeout time.Duration) TransExeResult {
if this.done {
return TransExeResult_HadDone
}
tnp.TId = this.owner.spawnTransNodeID()
tnp.TimeOut = timeout
tnp.LevelNo = this.MyTnp.LevelNo + 1
if this.Childs == nil {
this.Childs = make(map[TransNodeID]*TransNodeParam)
}
this.Childs[tnp.TId] = tnp
if Config.tcs != nil {
Config.tcs.SendTransStart(this.MyTnp, tnp, ud)
}
return TransExeResult_Success
}
func (this *TransNode) GetChildTransParam(childid TransNodeID) *TransNodeParam {
if v, exist := this.Childs[childid]; exist {
return v
}
return nil
}
func (this *TransNode) Yield() TransExeResult {
this.yield = true
SendTranscatYield(this)
this.incStats(TransStatsOp_Yiled)
return TransExeResult_Success
}
func (this *TransNode) Resume() TransExeResult {
this.resume = true
SendTranscatResume(this)
this.incStats(TransStatsOp_Resume)
return TransExeResult_Success
}
func (this *TransNode) Go(obj *basic.Object) TransExeResult {
this.ownerObj = obj
return this.execute(this.ud)
}
func (this *TransNode) checkExeOver() {
if this.resume == this.yield {
if this.AsynCallback != nil {
this.AsynCallback(this)
}
if this.done == false {
var ter TransExeResult
if this.TransRep.RetCode == TransResult_Success {
ter = TransExeResult_Success
} else {
ter = TransExeResult_AsynFailed
}
this.doneExecRet(ter)
}
}
}
func (this *TransNode) MakeBrotherWith(brother *TransNode, tbn TransBrotherNotify) {
if this.brothers == nil {
this.brothers = make(map[*TransNode]TransBrotherNotify)
}
this.brothers[brother] = tbn
}
func (this *TransNode) notifyBrother(ter TransExeResult) {
for k, v := range this.brothers {
v(k, ter)
}
}
func Stats() map[int]TransStats {
stats := make(map[int]TransStats)
transStats.Range(func(k, v interface{}) bool {
if s, ok := v.(*TransStats); ok {
d := *s
stats[int(k.(TransType))] = d
}
return true
})
return stats
}