history_v2_persistence.go ×12

Frontier kind: Code frontier

unlabeled · c_6d847bb38e32

7 tests · 5032 LOC · 183 files · introduces 0 tests · 212 LOC · 3 files

Introduces — evidence that enters the hierarchy at this concept

Code
16 ranges212 lines · 3 files
Tests
0 tests

Contains — complete concept membership

All code (extent)
910 ranges5032 lines · 183 files · Browse complete extent
All tests (intent)
7 testsBrowse complete intent

Neighbourhood graph

The orange circle is the focus. Violet and green circles are every ancestor and descendant, broader and narrower, at any distance; blue squares and pink diamonds are the introduced files and exact introduced tests of every visible concept, not only the focus's. Arrows point from broader to narrower concepts and bridge only concepts omitted from this view. Undirected links show source or test introduction. Concept and file size follows LOC; exact test nodes use test-count units.

Introduced files, introduced tests, and structurally relevant concept specialization

In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the native relationship evidence on this page.

Graph controls are ready.

Interactive rendering requires JavaScript and WebGL. Use the native relationship evidence on this page while the interactive map is unavailable.

Native relationship evidence

Every exact file and test below is linked only from the concept that introduces it.

Introduced tests

Every collected test enters the hierarchy at exactly one concept.

No tests are introduced at this concept. Its intent tests are introduced by other concepts.

Introduced code

Every collected source range enters the hierarchy at exactly one concept.

3 files ranked by introduced lines: 212 introduced LOC across 16 ranges. Expand a file to inspect source; the > gutter marks introduced lines.

go.temporal.io/server/common/persistence/persistence-tests/history_v2_persistence.go 195 introduced LOC · 12 ranges

Open complete file

459
460 // TestConcurrentlyForkAndAppendBranches test
461 > func (s *HistoryV2PersistenceSuite) TestConcurrentlyForkAndAppendBranches() { history_v2_persistence.go
462 > treeID := uuid.NewString()
463 > wg := sync.WaitGroup{}
464 > concurrency := 10
465 > masterBr, err := s.newHistoryBranch(treeID)
466 > s.Nil(err)
467 > branches := s.descTree(treeID)
468 > s.Equal(0, len(branches))
469 >
470 > // append first batch to master branch
471 > eids := []int64{}
472 > for i := int64(1); i <= int64(concurrency)+1; i++ {
473 > eids = append(eids, i)
474 > }
475 > events := s.genRandomEvents(eids, 1)
476 > err = s.appendNewBranchAndFirstNode(masterBr, events[0:1], 1, "masterbr")
477 > s.Nil(err)
478 >
479 > readEvents := s.read(masterBr, 1, int64(concurrency)+2)
480 > s.Nil(err)
481 > s.Equal(1, len(readEvents))
482 >
483 > branches = s.descTree(treeID)
484 > s.Equal(1, len(branches))
485 > mbrID := branches[0].BranchId
486 >
487 > txn := int64(1)
488 > getTxnLock := sync.Mutex{}
489 > reserveTxn := func(count int) int64 {
490 > getTxnLock.Lock()
491 > defer getTxnLock.Unlock()
492 >
493 > ret := txn
494 > txn += int64(count)
495 > return ret
496 > }
497
498 > err = s.appendOneByOne(masterBr, events[1:], reserveTxn(len(events[1:]))) history_v2_persistence.go
499 > s.Nil(err)
500 > events = s.read(masterBr, 1, int64(concurrency)+2)
501 > s.Nil(err)
502 > s.Equal((concurrency)+1, len(events))
503 >
504 > level1ID := new(sync.Map)
505 > level1Br := new(sync.Map)
506 > // test forking from master branch and append nodes
507 > for i := range concurrency {
508 > wg.Add(1)
509 > go func(idx int) {
510 > defer wg.Done()
511 >
512 > forkNodeID := rand.Int63n(int64(concurrency)) + 2
513 > level1ID.Store(idx, forkNodeID)
514 >
515 > bi, err := s.fork(masterBr, forkNodeID)
516 > s.Nil(err)
517 > level1Br.Store(idx, bi)
518 >
519 > // cannot append to ancestors
520 > events := s.genRandomEvents([]int64{forkNodeID - 1}, 1)
521 > err = s.appendNewNode(bi, events, reserveTxn(1))
522 > _, ok := err.(*p.InvalidPersistenceRequestError)
523 > s.Equal(true, ok)
524 >
525 > // append second batch to first level
526 > eids := make([]int64, 0)
527 > for i := forkNodeID; i <= int64(concurrency)*2+1; i++ {
528 > eids = append(eids, i)
529 > }
530 > events = s.genRandomEvents(eids, 1)
531 >
532 > err = s.appendNewNode(bi, events[0:1], reserveTxn(1))
533 > s.Nil(err)
534 >
535 > err = s.appendOneByOne(bi, events[1:], reserveTxn(len(events[1:])))
536 > s.Nil(err)
537 >
538 > events = s.read(bi, 1, int64(concurrency)*2+2)
539 > s.Nil(err)
540 > s.Equal((concurrency)*2+1, len(events))
541 >
542 > if idx == 0 {
543 > err = s.deleteHistoryBranch(bi)
544 > s.Nil(err)
545 > }
546
547 }(i)
548 }
549
550 > wg.Wait() history_v2_persistence.go
551 > branches = s.descTree(treeID)
552 > s.Equal(concurrency, len(branches))
553 > forkOnLevel1 := int32(0)
554 > level2Br := new(sync.Map)
555 > wg = sync.WaitGroup{}
556 >
557 > // test forking for second level of branch
558 > for i := 1; i < concurrency; i++ {
559 > wg.Add(1)
560 > go func(idx int) {
561 > defer wg.Done()
562 >
563 > // Event we fork from level1 branch, it is possible that the new branch will fork from master branch
564 > forkNodeID := rand.Int63n(int64(concurrency)*2) + 2
565 > forkBr := s.getBranchByKey(level1Br, idx)
566 > lastForkNodeID := s.getIDByKey(level1ID, idx)
567 >
568 > if forkNodeID > lastForkNodeID {
569 > atomic.AddInt32(&forkOnLevel1, int32(1))
570 > }
571
572 > bi, err := s.fork(forkBr, forkNodeID) history_v2_persistence.go
573 > s.Nil(err)
574 > level2Br.Store(idx, bi)
575 >
576 > // append second batch to second level
577 > eids := make([]int64, 0)
578 > for i := forkNodeID; i <= int64(concurrency)*3+1; i++ {
579 > eids = append(eids, i)
580 > }
581 > events := s.genRandomEvents(eids, 1)
582 > err = s.appendNewNode(bi, events[0:1], reserveTxn(1))
583 > s.Nil(err)
584 > err = s.appendOneByOne(bi, events[1:], reserveTxn(len(events[1:])))
585 > s.Nil(err)
586 > events = s.read(bi, 1, int64(concurrency)*3+2)
587 > s.Nil(err)
588 > s.Equal((concurrency)*3+1, len(events))
589 >
590 > // try override last event
591 > events = s.genRandomEvents([]int64{int64(concurrency)*3 + 1}, 1)
592 > err = s.appendNewNode(bi, events, reserveTxn(1))
593 > s.Nil(err)
594 > events = s.read(bi, 1, int64(concurrency)*3+2)
595 > s.Nil(err)
596 > s.Equal((concurrency)*3+1, len(events))
597 >
598 > // test fork and newBranch concurrently
599 > bi, err = s.newHistoryBranch(treeID)
600 > s.Nil(err)
601 > level2Br.Store(concurrency+idx, bi)
602 >
603 > events = s.genRandomEvents([]int64{1}, 1)
604 > err = s.appendNewBranchAndFirstNode(bi, events, reserveTxn(1), "newbr")
605 > s.Nil(err)
606
607 }(i)
608 }
609
610 > wg.Wait() history_v2_persistence.go
611 > branches = s.descTree(treeID)
612 > s.Equal(concurrency*3-2, len(branches))
613 > actualForkOnLevel1 := int32(0)
614 > masterCnt := 0
615 > for _, b := range branches {
616 > if len(b.Ancestors) == 2 {
617 > actualForkOnLevel1++
618 > } else if len(b.Ancestors) == 0 {
619 > masterCnt++
620 > } else {
621 > s.Equal(1, len(b.Ancestors))
622 > s.Equal(mbrID, b.Ancestors[0].GetBranchId())
623 > }
624 }
625 > s.Equal(forkOnLevel1, actualForkOnLevel1) history_v2_persistence.go
626 > s.Equal(concurrency, masterCnt)
627 >
628 > // Finally lets clean up all branches
629 > level1Br.Range(func(k, v any) bool {
630 > br := v.([]byte)
631 > // delete old branches along with create new branches
632 > err := s.deleteHistoryBranch(br)
633 > s.Nil(err)
634 >
635 > return true
636 > })
637 > level2Br.Range(func(k, v any) bool {
638 > br := v.([]byte)
639 > // delete old branches along with create new branches
640 > err := s.deleteHistoryBranch(br)
641 > s.Nil(err)
642 >
643 > return true
644 > })
645 > err = s.deleteHistoryBranch(masterBr)
646 > s.Nil(err)
647 >
648 > branches = s.descTree(treeID)
649 > s.Equal(0, len(branches))
650
651 }
658 }
659
660 > func (s *HistoryV2PersistenceSuite) getIDByKey(m *sync.Map, k int) int64 { history_v2_persistence.go
661 > v, ok := m.Load(k)
662 > s.Equal(true, ok)
663 > id := v.(int64)
664 > return id
665 > }
666
667 func (s *HistoryV2PersistenceSuite) genRandomEvents(eventIDs []int64, version int64) []*historypb.HistoryEvent {
770 }
771
772 > func (s *HistoryV2PersistenceSuite) appendOneByOne(branch []byte, events []*historypb.HistoryEvent, txnID int64) error { history_v2_persistence.go
773 > for index, e := range events {
774 > err := s.append(branch, []*historypb.HistoryEvent{e}, txnID+int64(index), false, "")
775 > if err != nil {
776 return err
777 }
778 }
779 > return nil history_v2_persistence.go
780 }
781
808 err := backoff.ThrottleRetry(op, historyTestRetryPolicy, isConditionFail)
809 if err != nil {
810 > return err history_v2_persistence.go
811 > }
812 s.True(resp.Size > 0)
813
go.temporal.io/server/common/persistence/history_manager.go 15 introduced LOC · 3 ranges

Open complete file

55 beginNodeID := GetBeginNodeID(forkBranch)
56 if beginNodeID >= request.ForkNodeID {
57 > // this is the case that new branch's ancestors doesn't include the forking branch history_manager.go
58 > for _, br := range forkBranch.Ancestors {
59 > if br.GetEndNodeId() >= request.ForkNodeID {
60 > newAncestors = append(newAncestors, &persistencespb.HistoryBranchRange{
61 > BranchId: br.GetBranchId(),
62 > BeginNodeId: br.GetBeginNodeId(),
63 > EndNodeId: request.ForkNodeID,
64 > })
65 > break
66 } else {
67 newAncestors = append(newAncestors, br)
402
403 if nodeID < GetBeginNodeID(branch) {
404 > return nil, &InvalidPersistenceRequestError{ history_manager.go
405 > Msg: "cannot append to ancestors' nodes",
406 > }
407 > }
408
409 return req, nil
487
488 if err != nil {
489 > return nil, err history_manager.go
490 > }
491
492 err = m.persistence.AppendHistoryNodes(ctx, req)
go.temporal.io/server/common/persistence/history_manager_util.go 2 introduced LOC · 1 range

Open complete file

130 (ans)[0].BeginNodeId = int64(1)
131 for i := 1; i < len(ans); i++ {
132 > (ans)[i].BeginNodeId = (ans)[i-1].GetEndNodeId() history_manager_util.go
133 > }
134 }
135 }