forked from xiaoxuxiansheng/gotcc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtxmanager_test.go
More file actions
403 lines (354 loc) · 9.55 KB
/
txmanager_test.go
File metadata and controls
403 lines (354 loc) · 9.55 KB
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
package gotcc
import (
"context"
"errors"
"fmt"
"math/rand"
"sync"
"testing"
"time"
"github.com/google/uuid"
"github.com/spf13/cast"
"github.com/stretchr/testify/assert"
)
type mockTXStore struct {
mutex sync.Mutex
txs map[string]*Transaction
}
func newMockTXStore() TXStore {
return &mockTXStore{
txs: make(map[string]*Transaction),
}
}
// 创建一条事务明细记录
func (m *mockTXStore) CreateTX(ctx context.Context, components ...TCCComponent) (string, error) {
txid := uuid.NewString()
m.mutex.Lock()
defer m.mutex.Unlock()
if _, ok := m.txs[txid]; ok {
return "", fmt.Errorf("repeat txid: %s", txid)
}
componentTryEntities := make([]*ComponentTryEntity, 0, len(components))
for _, component := range components {
componentTryEntities = append(componentTryEntities, &ComponentTryEntity{
ComponentID: component.ID(),
TryStatus: TryHanging,
})
}
m.txs[txid] = &Transaction{
TXID: txid,
Status: TXHanging,
CreatedAt: time.Now(),
Components: componentTryEntities,
}
return txid, nil
}
// 更新事务进度:实际更新的是每个组件的 try 请求响应结果
func (m *mockTXStore) TXUpdate(ctx context.Context, txID string, componentID string, accept bool) error {
m.mutex.Lock()
defer m.mutex.Unlock()
tx, ok := m.txs[txID]
if !ok {
return fmt.Errorf("[TXUpdate]invalid txid: %s", txID)
}
for _, component := range tx.Components {
if component.ComponentID != componentID {
continue
}
if component.TryStatus != TryHanging {
return fmt.Errorf("invalid component status: %s, componentid: %s, txid: %s", component.TryStatus, componentID, txID)
}
if accept {
component.TryStatus = TrySucceesful
} else {
component.TryStatus = TryFailure
}
return nil
}
return fmt.Errorf("[TXUpdate]invalid component id: %s for txid: %s", componentID, txID)
}
// 提交事务的最终状态, 标识事务执行结果为成功或失败
func (m *mockTXStore) TXSubmit(ctx context.Context, txID string, success bool) error {
m.mutex.Lock()
defer m.mutex.Unlock()
tx, ok := m.txs[txID]
if !ok {
return fmt.Errorf("[TXSubmit]invalid txid: %s", txID)
}
if success {
if tx.Status != TXHanging && tx.Status != TXSuccessful {
return fmt.Errorf("invalid txstatus: %s, txid: %s", tx.Status, txID)
}
tx.Status = TXSuccessful
} else {
if tx.Status != TXHanging && tx.Status != TXFailure {
return fmt.Errorf("invalid txstatus: %s, txid: %s", tx.Status, txID)
}
tx.Status = TXFailure
}
return nil
}
// 获取到所有未完成的事务
func (m *mockTXStore) GetHangingTXs(ctx context.Context) ([]*Transaction, error) {
m.mutex.Lock()
defer m.mutex.Unlock()
var hangingTXs []*Transaction
for _, tx := range m.txs {
if tx.Status != TXHanging {
continue
}
hangingTXs = append(hangingTXs, tx)
}
return hangingTXs, nil
}
// 获取指定的一笔事务
func (m *mockTXStore) GetTX(ctx context.Context, txID string) (*Transaction, error) {
m.mutex.Lock()
defer m.mutex.Unlock()
tx, ok := m.txs[txID]
if !ok {
return nil, fmt.Errorf("[GetTX]invalid txid: %s", txID)
}
return tx, nil
}
// 锁住整个 TXStore 模块
func (m *mockTXStore) Lock(ctx context.Context, expireDuration time.Duration) error {
return nil
}
// 解锁TXStore 模块
func (m *mockTXStore) Unlock(ctx context.Context) error {
return nil
}
type Status string
const (
StatusTried = "tried"
StatusConfirmed = "confirmed"
StatusCanceled = "canceled"
)
type mockComponent struct {
id string
mutex sync.Mutex
statusMachine map[string]Status
}
func newMockComponent(id string) TCCComponent {
return &mockComponent{
id: id,
statusMachine: make(map[string]Status),
}
}
// 返回组件唯一 id
func (m *mockComponent) ID() string {
return m.id
}
// 执行第一阶段的 try 操作
func (m *mockComponent) Try(ctx context.Context, req *TCCReq) (*TCCResp, error) {
resp := TCCResp{
ComponentID: m.id,
TXID: req.TXID,
}
m.mutex.Lock()
defer m.mutex.Unlock()
if m.statusMachine[req.TXID] == StatusCanceled {
return &resp, nil
}
if req.Data["reject_flag"] == true {
m.statusMachine[req.TXID] = StatusCanceled
return &resp, nil
}
if req.Data["hanging_flag"] == true {
<-time.After(time.Second)
return &resp, nil
}
if m.statusMachine[req.TXID] != StatusConfirmed {
m.statusMachine[req.TXID] = StatusTried
}
resp.ACK = true
return &resp, nil
}
// 执行第二阶段的 confirm 操作
func (m *mockComponent) Confirm(ctx context.Context, txID string) (*TCCResp, error) {
resp := TCCResp{
ComponentID: m.id,
TXID: txID,
}
m.mutex.Lock()
defer m.mutex.Unlock()
if m.statusMachine[txID] != StatusTried && m.statusMachine[txID] != StatusConfirmed {
return &resp, nil
}
resp.ACK = true
m.statusMachine[txID] = StatusConfirmed
return &resp, nil
}
// 执行第二阶段的 cancel 操作
func (m *mockComponent) Cancel(ctx context.Context, txID string) (*TCCResp, error) {
m.mutex.Lock()
defer m.mutex.Unlock()
if m.statusMachine[txID] == StatusConfirmed {
return nil, errors.New("invalid status machine: [confirmed] when canceling")
}
m.statusMachine[txID] = StatusCanceled
return &TCCResp{
ComponentID: m.id,
ACK: true,
TXID: txID,
}, nil
}
func Test_txmanager_transaction_success(t *testing.T) {
txmanager := NewTXManager(newMockTXStore())
defer txmanager.Stop()
// 注册 5 个 component
componentsCnt := 5
componentReqs := make([]*RequestEntity, 0, componentsCnt)
ctx := context.Background()
for i := 0; i < componentsCnt; i++ {
componentID := cast.ToString(i)
if err := txmanager.Register(newMockComponent(componentID)); err != nil {
t.Error(err)
return
}
componentReqs = append(componentReqs, &RequestEntity{
ComponentID: componentID,
})
}
txid, ok, err := txmanager.Transaction(ctx, componentReqs...)
if err != nil {
t.Error(err)
return
}
assert.Equal(t, true, ok)
tx, err := txmanager.txStore.GetTX(ctx, txid)
if err != nil {
t.Error(err)
return
}
assert.Equal(t, TXSuccessful, tx.Status)
}
// 验证分布式事务失败场景
func Test_txmanager_transaction_fail(t *testing.T) {
txmanager := NewTXManager(newMockTXStore())
defer txmanager.Stop()
// 注册 5 个 component
componentsCnt := 5
componentReqs := make([]*RequestEntity, 0, componentsCnt)
ctx := context.Background()
for i := 0; i < componentsCnt; i++ {
componentID := cast.ToString(i)
if err := txmanager.Register(newMockComponent(componentID)); err != nil {
t.Error(err)
return
}
componentReqs = append(componentReqs, &RequestEntity{
ComponentID: componentID,
Request: map[string]interface{}{
"reject_flag": true,
},
})
}
txid, ok, err := txmanager.Transaction(ctx, componentReqs...)
if err != nil {
t.Error(err)
return
}
assert.Equal(t, false, ok)
tx, err := txmanager.txStore.GetTX(ctx, txid)
if err != nil {
t.Error(err)
return
}
assert.Equal(t, TXFailure, tx.Status)
}
func Test_txmanager_transaction_concurrent(t *testing.T) {
txmanager := NewTXManager(newMockTXStore(), WithMonitorTick(0), WithTimeout(0))
defer txmanager.Stop()
// 注册 10 个 component
componentsCnt := 10
for i := 0; i < componentsCnt; i++ {
componentID := cast.ToString(i)
if err := txmanager.Register(newMockComponent(componentID)); err != nil {
t.Error(err)
return
}
}
// 并发 100 个分布式事务,随机取 3 个 component
ctx := context.Background()
concurrentTXs := 100
componentReqCnt := 3
var wg sync.WaitGroup
for i := 0; i < concurrentTXs; i++ {
wg.Add(1)
go func() {
defer wg.Done()
rander := rand.New(rand.NewSource(time.Now().UnixNano()))
componentSet := make(map[string]struct{}, componentReqCnt)
for len(componentSet) < componentReqCnt {
componentID := cast.ToString(rander.Intn(componentsCnt))
componentSet[componentID] = struct{}{}
}
componentReqs := make([]*RequestEntity, 0, componentReqCnt)
for componentID := range componentSet {
componentReqs = append(componentReqs, &RequestEntity{
ComponentID: componentID,
})
}
txid, ok, err := txmanager.Transaction(ctx, componentReqs...)
if err != nil {
t.Error(err)
return
}
assert.Equal(t, true, ok)
tx, err := txmanager.txStore.GetTX(ctx, txid)
if err != nil {
t.Error(err)
return
}
assert.Equal(t, TXSuccessful, tx.Status)
}()
}
wg.Wait()
}
func Test_txmanager_transaction_advance_progress(t *testing.T) {
txmanager := NewTXManager(newMockTXStore(), WithMonitorTick(100*time.Millisecond))
defer txmanager.Stop()
// 注册 5 个 component
componentsCnt := 5
componentReqs := make([]*RequestEntity, 0, componentsCnt)
ctx := context.Background()
for i := 0; i < componentsCnt; i++ {
componentID := cast.ToString(i)
if err := txmanager.Register(newMockComponent(componentID)); err != nil {
t.Error(err)
return
}
componentReqs = append(componentReqs, &RequestEntity{
ComponentID: componentID,
Request: map[string]interface{}{
"hanging_flag": true,
},
})
}
txid, ok, err := txmanager.Transaction(ctx, componentReqs...)
if err != nil {
t.Error(err)
return
}
assert.Equal(t, false, ok)
tx, err := txmanager.txStore.GetTX(ctx, txid)
if err != nil {
t.Error(err)
return
}
assert.Equal(t, TXFailure, tx.Status)
}
func Test_txManager_backOffTick(t *testing.T) {
txManager := NewTXManager(newMockTXStore(), WithMonitorTick(time.Second))
defer txManager.stop()
got := txManager.backOffTick(time.Second)
assert.Equal(t, 2*time.Second, got)
got = txManager.backOffTick(got)
assert.Equal(t, 4*time.Second, got)
got = txManager.backOffTick(got)
assert.Equal(t, 8*time.Second, got)
got = txManager.backOffTick(got)
assert.Equal(t, 8*time.Second, got)
}