Files
tasks/common/persistence/history_branch_util.go
Stephan BehnkeandGitHub 7b27b31645 Unify DataBlob encoding/decoding (#8111)
## What changed?

Cleanup of `common/serialization` package:

- unify several DataBlob encode/decode functions into 3 functions (see
new codec.go)
- unify function signatures to use `DataBlob` everywhere (instead of
split []byte + EncodingType)

## Why?

- less code
- simpler
- consistent
- preparation step to allow changing proto to json encoding via env
variable for debugging purposes
  
## How did you test it?
- [ ] built
- [ ] run locally and tested manually
- [x] covered by existing tests
- [ ] added new unit test(s)
- [ ] added new functional test(s)

## Potential Risks

See the two callouts for behavior difference.
2025-08-07 17:25:29 +00:00

102 lines
2.6 KiB
Go

//go:generate mockgen -package $GOPACKAGE -source $GOFILE -destination history_branch_util_mock.go
package persistence
import (
"time"
commonpb "go.temporal.io/api/common/v1"
enumspb "go.temporal.io/api/enums/v1"
persistencespb "go.temporal.io/server/api/persistence/v1"
"go.temporal.io/server/common/persistence/serialization"
"go.temporal.io/server/common/primitives"
)
type (
HistoryBranchUtil interface {
NewHistoryBranch(
namespaceID string,
workflowID string,
runID string,
treeID string,
branchID *string,
ancestors []*persistencespb.HistoryBranchRange,
runTimeout time.Duration,
executionTimeout time.Duration,
retentionDuration time.Duration,
) ([]byte, error)
// ParseHistoryBranchInfo parses the history branch for branch information
ParseHistoryBranchInfo(
branchToken []byte,
) (*persistencespb.HistoryBranch, error)
// UpdateHistoryBranchInfo updates the history branch with branch information
UpdateHistoryBranchInfo(
branchToken []byte,
branchInfo *persistencespb.HistoryBranch,
runID string,
) ([]byte, error)
}
HistoryBranchUtilImpl struct {
}
)
func (u *HistoryBranchUtilImpl) NewHistoryBranch(
_ string, // namespaceID
_ string, // workflowID
_ string, // runID
treeID string,
branchID *string,
ancestors []*persistencespb.HistoryBranchRange,
_ time.Duration, // runTimeout
_ time.Duration, // executionTimeout
_ time.Duration, // retentionDuration
) ([]byte, error) {
var id string
if branchID == nil {
id = primitives.NewUUID().String()
} else {
id = *branchID
}
bi := &persistencespb.HistoryBranch{
TreeId: treeID,
BranchId: id,
Ancestors: ancestors,
}
data, err := serialization.HistoryBranchToBlob(bi)
if err != nil {
return nil, err
}
return data.Data, nil
}
func (u *HistoryBranchUtilImpl) ParseHistoryBranchInfo(
branchToken []byte,
) (*persistencespb.HistoryBranch, error) {
return serialization.HistoryBranchFromBlob(&commonpb.DataBlob{Data: branchToken, EncodingType: enumspb.ENCODING_TYPE_PROTO3})
}
func (u *HistoryBranchUtilImpl) UpdateHistoryBranchInfo(
branchToken []byte,
branchInfo *persistencespb.HistoryBranch,
runID string,
) ([]byte, error) {
bi, err := serialization.HistoryBranchFromBlob(&commonpb.DataBlob{Data: branchToken, EncodingType: enumspb.ENCODING_TYPE_PROTO3})
if err != nil {
return nil, err
}
bi.TreeId = branchInfo.TreeId
bi.BranchId = branchInfo.BranchId
bi.Ancestors = branchInfo.Ancestors
blob, err := serialization.HistoryBranchToBlob(bi)
if err != nil {
return nil, err
}
return blob.Data, nil
}
func (u *HistoryBranchUtilImpl) GetHistoryBranchUtil() HistoryBranchUtil {
return u
}