go.temporal.io/server@v1.23.0/common/persistence/history_branch_util.go (about)

     1  // The MIT License
     2  //
     3  // Copyright (c) 2020 Temporal Technologies Inc.  All rights reserved.
     4  //
     5  // Copyright (c) 2020 Uber Technologies, Inc.
     6  //
     7  // Permission is hereby granted, free of charge, to any person obtaining a copy
     8  // of this software and associated documentation files (the "Software"), to deal
     9  // in the Software without restriction, including without limitation the rights
    10  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    11  // copies of the Software, and to permit persons to whom the Software is
    12  // furnished to do so, subject to the following conditions:
    13  //
    14  // The above copyright notice and this permission notice shall be included in
    15  // all copies or substantial portions of the Software.
    16  //
    17  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    18  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    19  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    20  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    21  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    22  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    23  // THE SOFTWARE.
    24  
    25  //go:generate mockgen -copyright_file ../../LICENSE -package $GOPACKAGE -source $GOFILE -destination history_branch_util_mock.go
    26  
    27  package persistence
    28  
    29  import (
    30  	"time"
    31  
    32  	enumspb "go.temporal.io/api/enums/v1"
    33  
    34  	persistencespb "go.temporal.io/server/api/persistence/v1"
    35  	"go.temporal.io/server/common/persistence/serialization"
    36  	"go.temporal.io/server/common/primitives"
    37  )
    38  
    39  type (
    40  	HistoryBranchUtil interface {
    41  		NewHistoryBranch(
    42  			namespaceID string,
    43  			workflowID string,
    44  			runID string,
    45  			treeID string,
    46  			branchID *string,
    47  			ancestors []*persistencespb.HistoryBranchRange,
    48  			runTimeout time.Duration,
    49  			executionTimeout time.Duration,
    50  			retentionDuration time.Duration,
    51  		) ([]byte, error)
    52  		// ParseHistoryBranchInfo parses the history branch for branch information
    53  		ParseHistoryBranchInfo(branchToken []byte) (*persistencespb.HistoryBranch, error)
    54  		// UpdateHistoryBranchInfo updates the history branch with branch information
    55  		UpdateHistoryBranchInfo(branchToken []byte, branchInfo *persistencespb.HistoryBranch) ([]byte, error)
    56  	}
    57  
    58  	HistoryBranchUtilImpl struct {
    59  	}
    60  )
    61  
    62  func NewHistoryBranch(
    63  	treeID string,
    64  	branchID *string,
    65  	ancestors []*persistencespb.HistoryBranchRange,
    66  ) ([]byte, error) {
    67  	var id string
    68  	if branchID == nil {
    69  		id = primitives.NewUUID().String()
    70  	} else {
    71  		id = *branchID
    72  	}
    73  	bi := &persistencespb.HistoryBranch{
    74  		TreeId:    treeID,
    75  		BranchId:  id,
    76  		Ancestors: ancestors,
    77  	}
    78  	data, err := serialization.HistoryBranchToBlob(bi)
    79  	if err != nil {
    80  		return nil, err
    81  	}
    82  	return data.Data, nil
    83  }
    84  
    85  func (u *HistoryBranchUtilImpl) NewHistoryBranch(
    86  	namespaceID string,
    87  	workflowID string,
    88  	runID string,
    89  	treeID string,
    90  	branchID *string,
    91  	ancestors []*persistencespb.HistoryBranchRange,
    92  	runTimeout time.Duration,
    93  	executionTimeout time.Duration,
    94  	retentionDuration time.Duration,
    95  ) ([]byte, error) {
    96  	return NewHistoryBranch(treeID, branchID, ancestors)
    97  }
    98  
    99  func (u *HistoryBranchUtilImpl) ParseHistoryBranchInfo(branchToken []byte) (*persistencespb.HistoryBranch, error) {
   100  	return serialization.HistoryBranchFromBlob(branchToken, enumspb.ENCODING_TYPE_PROTO3.String())
   101  }
   102  
   103  func (u *HistoryBranchUtilImpl) UpdateHistoryBranchInfo(branchToken []byte, branchInfo *persistencespb.HistoryBranch) ([]byte, error) {
   104  	bi, err := serialization.HistoryBranchFromBlob(branchToken, enumspb.ENCODING_TYPE_PROTO3.String())
   105  	if err != nil {
   106  		return nil, err
   107  	}
   108  	bi.TreeId = branchInfo.TreeId
   109  	bi.BranchId = branchInfo.BranchId
   110  	bi.Ancestors = branchInfo.Ancestors
   111  
   112  	blob, err := serialization.HistoryBranchToBlob(bi)
   113  	if err != nil {
   114  		return nil, err
   115  	}
   116  	return blob.Data, nil
   117  }
   118  
   119  func (u *HistoryBranchUtilImpl) GetHistoryBranchUtil() HistoryBranchUtil {
   120  	return u
   121  }