-
Notifications
You must be signed in to change notification settings - Fork 787
/
historyV2PersistenceTest.go
905 lines (773 loc) · 25.7 KB
/
historyV2PersistenceTest.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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
// Copyright (c) 2017 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package persistencetests
import (
"context"
"log"
"math"
"math/rand"
"os"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/pborman/uuid"
"github.com/stretchr/testify/require"
workflow "github.com/uber/cadence/.gen/go/shared"
"github.com/uber/cadence/common"
"github.com/uber/cadence/common/backoff"
"github.com/uber/cadence/common/codec"
p "github.com/uber/cadence/common/persistence"
persistenceutils "github.com/uber/cadence/common/persistence/persistence-utils"
"github.com/uber/cadence/common/types"
"github.com/uber/cadence/common/types/mapper/thrift"
)
type (
// HistoryV2PersistenceSuite contains history persistence tests
HistoryV2PersistenceSuite struct {
*TestBase
// override suite.Suite.Assertions with require.Assertions; this means that s.NotNil(nil) will stop the test,
// not merely log an error
*require.Assertions
}
)
const testForkRunID = "11220000-0000-f000-f000-000000000000"
var (
throttleRetry = backoff.NewThrottleRetry(
backoff.WithRetryPolicy(createHistoryTestRetryPolicy()),
backoff.WithRetryableError(isConditionFail))
thriftEncoder = codec.NewThriftRWEncoder()
)
func createHistoryTestRetryPolicy() backoff.RetryPolicy {
policy := backoff.NewExponentialRetryPolicy(time.Millisecond * 50)
policy.SetMaximumInterval(time.Second * 3)
policy.SetExpirationInterval(time.Second * 30)
return policy
}
func isConditionFail(err error) bool {
switch err.(type) {
case *p.ConditionFailedError:
return true
default:
return false
}
}
// SetupSuite implementation
func (s *HistoryV2PersistenceSuite) SetupSuite() {
if testing.Verbose() {
log.SetOutput(os.Stdout)
}
}
// SetupTest implementation
func (s *HistoryV2PersistenceSuite) SetupTest() {
// Have to define our overridden assertions in the test setup. If we did it earlier, s.T() will return nil
s.Assertions = require.New(s.T())
}
// TearDownSuite implementation
func (s *HistoryV2PersistenceSuite) TearDownSuite() {
s.TearDownWorkflowStore()
}
// TestGenUUIDs testing uuid.New() can generate unique UUID
func (s *HistoryV2PersistenceSuite) TestGenUUIDs() {
wg := sync.WaitGroup{}
m := sync.Map{}
concurrency := 1000
for i := 0; i < concurrency; i++ {
wg.Add(1)
go func() {
defer wg.Done()
u := uuid.New()
m.Store(u, true)
}()
}
wg.Wait()
cnt := 0
m.Range(func(k, v interface{}) bool {
cnt++
return true
})
s.Equal(concurrency, cnt)
}
// TestScanAllTrees test
func (s *HistoryV2PersistenceSuite) TestScanAllTrees() {
if os.Getenv("SKIP_SCAN_HISTORY") != "" {
s.T().Skipf("GetAllHistoryTreeBranches not supported in %v", s.TaskMgr.GetName())
}
ctx, cancel := context.WithTimeout(context.Background(), testContextTimeout)
defer cancel()
resp, err := s.HistoryV2Mgr.GetAllHistoryTreeBranches(ctx, &p.GetAllHistoryTreeBranchesRequest{
PageSize: 1,
})
s.Nil(err)
s.Equal(0, len(resp.Branches), "some trees were leaked in other tests")
trees := map[string]bool{}
totalTrees := 1002
pgSize := 100
for i := 0; i < totalTrees; i++ {
treeID := uuid.New()
bi, err := s.newHistoryBranch(treeID)
s.Nil(err)
events := s.genRandomEvents([]int64{1, 2, 3}, 1)
err = s.appendNewBranchAndFirstNode(ctx, bi, events, 1, "branchInfo")
s.Nil(err)
trees[treeID] = true
}
var pgToken []byte
for {
resp, err := s.HistoryV2Mgr.GetAllHistoryTreeBranches(ctx, &p.GetAllHistoryTreeBranchesRequest{
PageSize: pgSize,
NextPageToken: pgToken,
})
s.Nil(err)
for _, br := range resp.Branches {
if trees[br.TreeID] {
delete(trees, br.TreeID)
s.True(br.ForkTime.UnixNano() > 0)
s.True(len(br.BranchID) > 0)
s.Equal("branchInfo", br.Info)
} else {
s.Fail("treeID not found", br.TreeID)
}
}
if len(resp.NextPageToken) == 0 {
break
}
pgToken = resp.NextPageToken
}
s.Equal(0, len(trees))
}
// TestReadBranchByPagination test
func (s *HistoryV2PersistenceSuite) TestReadBranchByPagination() {
ctx, cancel := context.WithTimeout(context.Background(), testContextTimeout)
defer cancel()
treeID := uuid.New()
bi, err := s.newHistoryBranch(treeID)
s.Nil(err)
historyW := &types.History{}
events := s.genRandomEvents([]int64{1, 2, 3}, 0)
err = s.appendNewBranchAndFirstNode(ctx, bi, events, 1, "branchInfo")
s.Nil(err)
historyW.Events = events
events = s.genRandomEvents([]int64{4}, 0)
err = s.appendNewNode(ctx, bi, events, 2)
s.Nil(err)
historyW.Events = append(historyW.Events, events...)
events = s.genRandomEvents([]int64{5, 6, 7, 8}, 4)
err = s.appendNewNode(ctx, bi, events, 6)
s.Nil(err)
historyW.Events = append(historyW.Events, events...)
// stale event batch
events = s.genRandomEvents([]int64{6, 7, 8}, 1)
err = s.appendNewNode(ctx, bi, events, 3)
s.Nil(err)
// stale event batch
events = s.genRandomEvents([]int64{6, 7, 8}, 2)
err = s.appendNewNode(ctx, bi, events, 4)
s.Nil(err)
// stale event batch
events = s.genRandomEvents([]int64{6, 7, 8}, 3)
err = s.appendNewNode(ctx, bi, events, 5)
s.Nil(err)
events = s.genRandomEvents([]int64{9}, 4)
err = s.appendNewNode(ctx, bi, events, 7)
s.Nil(err)
historyW.Events = append(historyW.Events, events...)
// Start to read from middle, should not return error, but the first batch should be ignored by application layer
req := &p.ReadHistoryBranchRequest{
BranchToken: bi,
MinEventID: 6,
MaxEventID: 10,
PageSize: 4,
NextPageToken: nil,
ShardID: common.IntPtr(s.ShardInfo.ShardID),
}
// first page
resp, err := s.HistoryV2Mgr.ReadHistoryBranch(ctx, req)
s.Nil(err)
s.Equal(4, len(resp.HistoryEvents))
s.Equal(int64(6), resp.HistoryEvents[0].ID)
events = s.genRandomEvents([]int64{10}, 4)
err = s.appendNewNode(ctx, bi, events, 8)
s.Nil(err)
historyW.Events = append(historyW.Events, events...)
events = s.genRandomEvents([]int64{11}, 4)
err = s.appendNewNode(ctx, bi, events, 9)
s.Nil(err)
historyW.Events = append(historyW.Events, events...)
events = s.genRandomEvents([]int64{12}, 4)
err = s.appendNewNode(ctx, bi, events, 10)
s.Nil(err)
historyW.Events = append(historyW.Events, events...)
events = s.genRandomEvents([]int64{13, 14, 15}, 4)
err = s.appendNewNode(ctx, bi, events, 11)
s.Nil(err)
// we don't append this batch because we will fork from 13
// historyW.Events = append(historyW.Events, events...)
// fork from here
bi2, err := s.fork(ctx, bi, 13)
s.Nil(err)
events = s.genRandomEvents([]int64{13}, 4)
err = s.appendNewNode(ctx, bi2, events, 12)
s.Nil(err)
historyW.Events = append(historyW.Events, events...)
events = s.genRandomEvents([]int64{14}, 4)
err = s.appendNewNode(ctx, bi2, events, 13)
s.Nil(err)
historyW.Events = append(historyW.Events, events...)
events = s.genRandomEvents([]int64{15, 16, 17}, 4)
err = s.appendNewNode(ctx, bi2, events, 14)
s.Nil(err)
historyW.Events = append(historyW.Events, events...)
events = s.genRandomEvents([]int64{18, 19, 20}, 4)
err = s.appendNewNode(ctx, bi2, events, 15)
s.Nil(err)
historyW.Events = append(historyW.Events, events...)
// read branch to verify
historyR := &types.History{}
req = &p.ReadHistoryBranchRequest{
BranchToken: bi2,
MinEventID: 1,
MaxEventID: 21,
PageSize: 3,
NextPageToken: nil,
ShardID: common.IntPtr(s.ShardInfo.ShardID),
}
// first page
resp, err = s.HistoryV2Mgr.ReadHistoryBranch(ctx, req)
s.Nil(err)
s.Equal(8, len(resp.HistoryEvents))
historyR.Events = append(historyR.Events, resp.HistoryEvents...)
req.NextPageToken = resp.NextPageToken
// this page is all stale batches
// doe to difference in Cassandra / MySQL pagination
// the stale event batch may get returned
resp, err = s.HistoryV2Mgr.ReadHistoryBranch(ctx, req)
s.Nil(err)
historyR.Events = append(historyR.Events, resp.HistoryEvents...)
req.NextPageToken = resp.NextPageToken
if len(resp.HistoryEvents) == 0 {
// second page
resp, err = s.HistoryV2Mgr.ReadHistoryBranch(ctx, req)
s.Nil(err)
s.Equal(3, len(resp.HistoryEvents))
historyR.Events = append(historyR.Events, resp.HistoryEvents...)
req.NextPageToken = resp.NextPageToken
} else if len(resp.HistoryEvents) == 3 {
// no op
} else {
s.Fail("should either return 0 (Cassandra) or 3 (MySQL) events")
}
// 3rd page, since we fork from nodeID=13, we can only see one batch of 12 here
resp, err = s.HistoryV2Mgr.ReadHistoryBranch(ctx, req)
s.Nil(err)
s.Equal(1, len(resp.HistoryEvents))
historyR.Events = append(historyR.Events, resp.HistoryEvents...)
req.NextPageToken = resp.NextPageToken
// 4th page, 13~17
resp, err = s.HistoryV2Mgr.ReadHistoryBranch(ctx, req)
s.Nil(err)
s.Equal(5, len(resp.HistoryEvents))
historyR.Events = append(historyR.Events, resp.HistoryEvents...)
req.NextPageToken = resp.NextPageToken
// last page: one batch of 18-20
// We have only one page left and the page size is set to one. In this case,
// persistence may or may not return a nextPageToken.
// If it does return a token, we need to ensure that if the token returned is used
// to get history again, no error and history events should be returned.
req.PageSize = 1
resp, err = s.HistoryV2Mgr.ReadHistoryBranch(ctx, req)
s.Nil(err)
s.Equal(3, len(resp.HistoryEvents))
historyR.Events = append(historyR.Events, resp.HistoryEvents...)
req.NextPageToken = resp.NextPageToken
if len(resp.NextPageToken) != 0 {
resp, err = s.HistoryV2Mgr.ReadHistoryBranch(ctx, req)
s.Nil(err)
s.Equal(0, len(resp.HistoryEvents))
}
s.Equal(historyW, historyR)
s.Equal(0, len(resp.NextPageToken))
// MinEventID is in the middle of the last batch and this is the first request (NextPageToken
// is empty), the call should return an error.
req.MinEventID = 19
req.NextPageToken = nil
_, err = s.HistoryV2Mgr.ReadHistoryBranch(ctx, req)
s.IsType(&types.EntityNotExistsError{}, err)
err = s.deleteHistoryBranch(ctx, bi2)
s.Nil(err)
err = s.deleteHistoryBranch(ctx, bi)
s.Nil(err)
branches := s.descTree(ctx, treeID)
s.Equal(0, len(branches))
}
// TestConcurrentlyCreateAndAppendBranches test
func (s *HistoryV2PersistenceSuite) TestConcurrentlyCreateAndAppendBranches() {
ctx, cancel := context.WithTimeout(context.Background(), largeTestContextTimeout)
defer cancel()
treeID := uuid.New()
wg := sync.WaitGroup{}
concurrency := 20
m := sync.Map{}
// test create new branch along with appending new nodes
for i := 0; i < concurrency; i++ {
wg.Add(1)
go func(idx int) {
defer wg.Done()
bi, err := s.newHistoryBranch(treeID)
s.Nil(err)
historyW := &types.History{}
m.Store(idx, bi)
events := s.genRandomEvents([]int64{1, 2, 3}, 1)
err = s.appendNewBranchAndFirstNode(ctx, bi, events, 1, "branchInfo")
s.Nil(err)
historyW.Events = events
events = s.genRandomEvents([]int64{4}, 1)
err = s.appendNewNode(ctx, bi, events, 2)
s.Nil(err)
historyW.Events = append(historyW.Events, events...)
events = s.genRandomEvents([]int64{5, 6, 7, 8}, 1)
err = s.appendNewNode(ctx, bi, events, 3)
s.Nil(err)
historyW.Events = append(historyW.Events, events...)
events = s.genRandomEvents([]int64{9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}, 1)
err = s.appendNewNode(ctx, bi, events, 2000)
s.Nil(err)
historyW.Events = append(historyW.Events, events...)
// read branch to verify
historyR := &types.History{}
events = s.read(ctx, bi, 1, 21)
s.Equal(20, len(events))
historyR.Events = events
s.Equal(historyW, historyR)
}(i)
}
wg.Wait()
branches := s.descTree(ctx, treeID)
s.Equal(concurrency, len(branches))
wg = sync.WaitGroup{}
// test appending nodes(override and new nodes) on each branch concurrently
for i := 0; i < concurrency; i++ {
wg.Add(1)
go func(idx int) {
defer wg.Done()
branch := s.getBranchByKey(m, idx)
// override with smaller txn_id
events := s.genRandomEvents([]int64{5}, 1)
err := s.appendNewNode(ctx, branch, events, 0)
s.Nil(err)
// it shouldn't change anything
events = s.read(ctx, branch, 1, 25)
s.Equal(20, len(events))
// override with greatest txn_id and greater version
events = s.genRandomEvents([]int64{5}, 2)
err = s.appendNewNode(ctx, branch, events, 1000)
s.Nil(err)
// read to verify override success
events = s.read(ctx, branch, 1, 25)
s.Equal(5, len(events))
// override with even larger txn_id and same version
events = s.genRandomEvents([]int64{5, 6}, 1)
err = s.appendNewNode(ctx, branch, events, 1001)
s.Nil(err)
// read to verify override success, at this point history is corrupted, missing 7/8, so we should only see 6 events
_, err = s.readWithError(ctx, branch, 1, 25)
_, ok := err.(*types.InternalDataInconsistencyError)
s.Equal(true, ok)
events = s.read(ctx, branch, 1, 7)
s.Equal(6, len(events))
// override more with larger txn_id, this would fix the corrupted hole so that we cna get 20 events again
events = s.genRandomEvents([]int64{7, 8}, 1)
err = s.appendNewNode(ctx, branch, events, 1002)
s.Nil(err)
// read to verify override
events = s.read(ctx, branch, 1, 25)
s.Equal(20, len(events))
events = s.genRandomEvents([]int64{9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23}, 1)
err = s.appendNewNode(ctx, branch, events, 2001)
s.Nil(err)
events = s.read(ctx, branch, 1, 25)
s.Equal(23, len(events))
}(i)
}
wg.Wait()
// Finally lets clean up all branches
m.Range(func(k, v interface{}) bool {
br := v.([]byte)
// delete old branches along with create new branches
err := s.deleteHistoryBranch(ctx, br)
s.Nil(err)
return true
})
branches = s.descTree(ctx, treeID)
s.Equal(0, len(branches))
}
// TestConcurrentlyForkAndAppendBranches test
func (s *HistoryV2PersistenceSuite) TestConcurrentlyForkAndAppendBranches() {
ctx, cancel := context.WithTimeout(context.Background(), testContextTimeout)
defer cancel()
treeID := uuid.New()
wg := sync.WaitGroup{}
concurrency := 10
masterBr, err := s.newHistoryBranch(treeID)
s.Nil(err)
branches := s.descTree(ctx, treeID)
s.Equal(0, len(branches))
// append first batch to master branch
eids := []int64{}
for i := int64(1); i <= int64(concurrency)+1; i++ {
eids = append(eids, i)
}
events := s.genRandomEvents(eids, 1)
err = s.appendNewBranchAndFirstNode(ctx, masterBr, events[0:1], 1, "masterbr")
s.Nil(err)
readEvents := s.read(ctx, masterBr, 1, int64(concurrency)+2)
s.Nil(err)
s.Equal(1, len(readEvents))
branches = s.descTree(ctx, treeID)
s.Equal(1, len(branches))
mbrID := *branches[0].BranchID
txn := int64(1)
getTxnLock := sync.Mutex{}
reserveTxn := func(count int) int64 {
getTxnLock.Lock()
defer getTxnLock.Unlock()
ret := txn
txn += int64(count)
return ret
}
err = s.appendOneByOne(ctx, masterBr, events[1:], reserveTxn(len(events[1:])))
s.Nil(err)
events = s.read(ctx, masterBr, 1, int64(concurrency)+2)
s.Nil(err)
s.Equal((concurrency)+1, len(events))
level1ID := sync.Map{}
level1Br := sync.Map{}
// test forking from master branch and append nodes
for i := 0; i < concurrency; i++ {
wg.Add(1)
go func(idx int) {
defer wg.Done()
forkNodeID := rand.Int63n(int64(concurrency)) + 2
level1ID.Store(idx, forkNodeID)
bi, err := s.fork(ctx, masterBr, forkNodeID)
s.Nil(err)
level1Br.Store(idx, bi)
// cannot append to ancestors
events := s.genRandomEvents([]int64{forkNodeID - 1}, 1)
err = s.appendNewNode(ctx, bi, events, reserveTxn(1))
_, ok := err.(*p.InvalidPersistenceRequestError)
s.Equal(true, ok)
// append second batch to first level
eids := make([]int64, 0)
for i := forkNodeID; i <= int64(concurrency)*2+1; i++ {
eids = append(eids, i)
}
events = s.genRandomEvents(eids, 1)
err = s.appendNewNode(ctx, bi, events[0:1], reserveTxn(1))
s.Nil(err)
err = s.appendOneByOne(ctx, bi, events[1:], reserveTxn(len(events[1:])))
s.Nil(err)
events = s.read(ctx, bi, 1, int64(concurrency)*2+2)
s.Nil(err)
s.Equal((concurrency)*2+1, len(events))
if idx == 0 {
err = s.deleteHistoryBranch(ctx, bi)
s.Nil(err)
}
}(i)
}
wg.Wait()
branches = s.descTreeByToken(ctx, masterBr)
s.Equal(concurrency, len(branches))
forkOnLevel1 := int32(0)
level2Br := sync.Map{}
wg = sync.WaitGroup{}
// test forking for second level of branch
for i := 1; i < concurrency; i++ {
wg.Add(1)
go func(idx int) {
defer wg.Done()
// Event we fork from level1 branch, it is possible that the new branch will fork from master branch
forkNodeID := rand.Int63n(int64(concurrency)*2) + 2
forkBr := s.getBranchByKey(level1Br, idx)
lastForkNodeID := s.getIDByKey(level1ID, idx)
if forkNodeID > lastForkNodeID {
atomic.AddInt32(&forkOnLevel1, int32(1))
}
bi, err := s.fork(ctx, forkBr, forkNodeID)
s.Nil(err)
level2Br.Store(idx, bi)
// append second batch to second level
eids := make([]int64, 0)
for i := forkNodeID; i <= int64(concurrency)*3+1; i++ {
eids = append(eids, i)
}
events := s.genRandomEvents(eids, 1)
err = s.appendNewNode(ctx, bi, events[0:1], reserveTxn(1))
s.Nil(err)
err = s.appendOneByOne(ctx, bi, events[1:], reserveTxn(len(events[1:])))
s.Nil(err)
events = s.read(ctx, bi, 1, int64(concurrency)*3+2)
s.Nil(err)
s.Equal((concurrency)*3+1, len(events))
// try override last event
events = s.genRandomEvents([]int64{int64(concurrency)*3 + 1}, 1)
err = s.appendNewNode(ctx, bi, events, reserveTxn(1))
s.Nil(err)
events = s.read(ctx, bi, 1, int64(concurrency)*3+2)
s.Nil(err)
s.Equal((concurrency)*3+1, len(events))
// test fork and newBranch concurrently
bi, err = s.newHistoryBranch(treeID)
s.Nil(err)
level2Br.Store(concurrency+idx, bi)
events = s.genRandomEvents([]int64{1}, 1)
err = s.appendNewBranchAndFirstNode(ctx, bi, events, reserveTxn(1), "newbr")
s.Nil(err)
}(i)
}
wg.Wait()
branches = s.descTree(ctx, treeID)
s.Equal(int(concurrency*3-2), len(branches))
actualForkOnLevel1 := int32(0)
masterCnt := 0
for _, b := range branches {
if len(b.Ancestors) == 2 {
actualForkOnLevel1++
} else if len(b.Ancestors) == 0 {
masterCnt++
} else {
s.Equal(1, len(b.Ancestors))
s.Equal(mbrID, *b.Ancestors[0].BranchID)
}
}
s.Equal(forkOnLevel1, actualForkOnLevel1)
s.Equal(concurrency, masterCnt)
// Finally lets clean up all branches
level1Br.Range(func(k, v interface{}) bool {
br := v.([]byte)
// delete old branches along with create new branches
err := s.deleteHistoryBranch(ctx, br)
s.Nil(err)
return true
})
level2Br.Range(func(k, v interface{}) bool {
br := v.([]byte)
// delete old branches along with create new branches
err := s.deleteHistoryBranch(ctx, br)
s.Nil(err)
return true
})
err = s.deleteHistoryBranch(ctx, masterBr)
s.Nil(err)
branches = s.descTree(ctx, treeID)
s.Equal(0, len(branches))
}
func (s *HistoryV2PersistenceSuite) getBranchByKey(m sync.Map, k int) []byte {
v, ok := m.Load(k)
s.Equal(true, ok)
br := v.([]byte)
return br
}
func (s *HistoryV2PersistenceSuite) getIDByKey(m sync.Map, k int) int64 {
v, ok := m.Load(k)
s.Equal(true, ok)
id := v.(int64)
return id
}
func (s *HistoryV2PersistenceSuite) genRandomEvents(eventIDs []int64, version int64) []*types.HistoryEvent {
var events []*types.HistoryEvent
timestamp := time.Now().UnixNano()
for _, eid := range eventIDs {
e := &types.HistoryEvent{ID: eid, Version: version, Timestamp: int64Ptr(timestamp)}
events = append(events, e)
}
return events
}
// persistence helper
func (s *HistoryV2PersistenceSuite) newHistoryBranch(treeID string) ([]byte, error) {
return p.NewHistoryBranchToken(treeID)
}
// persistence helper
func (s *HistoryV2PersistenceSuite) deleteHistoryBranch(ctx context.Context, branchToken []byte) error {
var branchThrift workflow.HistoryBranch
err := thriftEncoder.Decode(branchToken, &branchThrift)
if err != nil {
return err
}
branch := thrift.ToHistoryBranch(&branchThrift)
beginNodeID := persistenceutils.GetBeginNodeID(*branch)
brsToDelete := append(branch.Ancestors, &types.HistoryBranchRange{
BranchID: branch.BranchID,
BeginNodeID: beginNodeID,
})
branches := s.descTreeByToken(ctx, branchToken)
// validBRsMaxEndNode is to for each branch range that is being used, we want to know what is the max nodeID referred by other valid branch
var brs []*types.HistoryBranch
for _, br := range branches {
brs = append(brs, thrift.ToHistoryBranch(br))
}
validBRsMaxEndNode := persistenceutils.GetBranchesMaxReferredNodeIDs(brs)
minNodeID := beginNodeID
for i := len(brsToDelete) - 1; i >= 0; i-- {
br := brsToDelete[i]
maxReferredEndNodeID, ok := validBRsMaxEndNode[br.BranchID]
if ok {
minNodeID = maxReferredEndNodeID
break
} else {
minNodeID = br.BeginNodeID
}
}
domainName := s.DomainManager.GetName()
op := func() error {
err := s.HistoryV2Mgr.DeleteHistoryBranch(ctx, &p.DeleteHistoryBranchRequest{
BranchToken: branchToken,
ShardID: common.IntPtr(s.ShardInfo.ShardID),
DomainName: domainName,
})
return err
}
if err := throttleRetry.Do(ctx, op); err != nil {
return err
}
res, err := s.readWithError(ctx, branchToken, minNodeID, math.MaxInt64)
if err != nil {
s.IsType(&types.EntityNotExistsError{}, err)
} else {
s.Equal(0, len(res))
}
return nil
}
// persistence helper
func (s *HistoryV2PersistenceSuite) descTreeByToken(ctx context.Context, br []byte) []*workflow.HistoryBranch {
domainName := s.DomainManager.GetName()
resp, err := s.HistoryV2Mgr.GetHistoryTree(ctx, &p.GetHistoryTreeRequest{
BranchToken: br,
ShardID: common.IntPtr(s.ShardInfo.ShardID),
DomainName: domainName,
})
s.Nil(err)
return resp.Branches
}
func (s *HistoryV2PersistenceSuite) descTree(ctx context.Context, treeID string) []*workflow.HistoryBranch {
resp, err := s.HistoryV2Mgr.GetHistoryTree(ctx, &p.GetHistoryTreeRequest{
TreeID: treeID,
ShardID: common.IntPtr(s.ShardInfo.ShardID),
})
s.Nil(err)
return resp.Branches
}
// persistence helper
func (s *HistoryV2PersistenceSuite) read(ctx context.Context, branch []byte, minID, maxID int64) []*types.HistoryEvent {
res, err := s.readWithError(ctx, branch, minID, maxID)
s.Nil(err)
return res
}
func (s *HistoryV2PersistenceSuite) readWithError(ctx context.Context, branch []byte, minID, maxID int64) ([]*types.HistoryEvent, error) {
// use small page size to enforce pagination
randPageSize := 2
domainName := s.DomainManager.GetName()
res := make([]*types.HistoryEvent, 0)
token := []byte{}
for {
resp, err := s.HistoryV2Mgr.ReadHistoryBranch(ctx, &p.ReadHistoryBranchRequest{
BranchToken: branch,
MinEventID: minID,
MaxEventID: maxID,
PageSize: randPageSize,
NextPageToken: token,
ShardID: common.IntPtr(s.ShardInfo.ShardID),
DomainName: domainName,
})
if err != nil {
return nil, err
}
if len(resp.HistoryEvents) > 0 {
s.True(resp.Size > 0)
}
res = append(res, resp.HistoryEvents...)
token = resp.NextPageToken
if len(token) == 0 {
break
}
}
return res, nil
}
func (s *HistoryV2PersistenceSuite) appendOneByOne(ctx context.Context, branch []byte, events []*types.HistoryEvent, txnID int64) error {
for index, e := range events {
err := s.append(ctx, branch, []*types.HistoryEvent{e}, txnID+int64(index), false, "")
if err != nil {
return err
}
}
return nil
}
func (s *HistoryV2PersistenceSuite) appendNewNode(ctx context.Context, branch []byte, events []*types.HistoryEvent, txnID int64) error {
return s.append(ctx, branch, events, txnID, false, "")
}
func (s *HistoryV2PersistenceSuite) appendNewBranchAndFirstNode(ctx context.Context, branch []byte, events []*types.HistoryEvent, txnID int64, branchInfo string) error {
return s.append(ctx, branch, events, txnID, true, branchInfo)
}
// persistence helper
func (s *HistoryV2PersistenceSuite) append(ctx context.Context, branch []byte, events []*types.HistoryEvent, txnID int64, isNewBranch bool, branchInfo string) error {
var resp *p.AppendHistoryNodesResponse
domainName := s.DomainManager.GetName()
op := func() error {
var err error
resp, err = s.HistoryV2Mgr.AppendHistoryNodes(ctx, &p.AppendHistoryNodesRequest{
IsNewBranch: isNewBranch,
Info: branchInfo,
BranchToken: branch,
Events: events,
TransactionID: txnID,
Encoding: pickRandomEncoding(),
ShardID: common.IntPtr(s.ShardInfo.ShardID),
DomainName: domainName,
})
return err
}
if err := throttleRetry.Do(ctx, op); err != nil {
return err
}
s.True(len(resp.DataBlob.Data) > 0)
return nil
}
// persistence helper
func (s *HistoryV2PersistenceSuite) fork(ctx context.Context, forkBranch []byte, forkNodeID int64) ([]byte, error) {
bi := []byte{}
domainName := s.DomainManager.GetName()
op := func() error {
var err error
resp, err := s.HistoryV2Mgr.ForkHistoryBranch(ctx, &p.ForkHistoryBranchRequest{
ForkBranchToken: forkBranch,
ForkNodeID: forkNodeID,
Info: testForkRunID,
ShardID: common.IntPtr(s.ShardInfo.ShardID),
DomainName: domainName,
})
if resp != nil {
bi = resp.NewBranchToken
}
return err
}
err := throttleRetry.Do(ctx, op)
return bi, err
}