func (m *executionManagerImpl) DeleteHistoryBranch(
	request *DeleteHistoryBranchRequest,
) error {
	branch, err := serialization.HistoryBranchFromBlob(request.BranchToken, enumspb.ENCODING_TYPE_PROTO3.String())
	if err != nil {
		return err
	}
	brsToDelete := branch.Ancestors
	brsToDelete = append(brsToDelete, &persistencespb.HistoryBranchRange{
		BranchId:    branch.GetBranchId(),
		BeginNodeId: GetBeginNodeID(branch),
	})
	historyTreeResp, err := m.GetHistoryTree(&GetHistoryTreeRequest{
		TreeID:      branch.TreeId,
		ShardID:     &request.ShardID,
		BranchToken: request.BranchToken,
	})
	if err != nil {
		return err
	}
	usedBranches := map[string]int64{}
	for _, br := range historyTreeResp.Branches {
		if br.BranchId == branch.BranchId {
			continue
		}
		for _, ancestor := range br.Ancestors {
			if curr, ok := usedBranches[ancestor.GetBranchId()]; !ok || curr < ancestor.GetEndNodeId() {
				usedBranches[ancestor.GetBranchId()] = ancestor.GetEndNodeId()
			}
		}
	}
	var deleteRanges []InternalDeleteHistoryBranchRange
findDeleteRanges:
	for i := len(brsToDelete) - 1; i >= 0; i-- {
		br := brsToDelete[i]
		if maxEndNode, ok := usedBranches[br.GetBranchId()]; ok {
			deleteRanges = append(deleteRanges, InternalDeleteHistoryBranchRange{
				BranchId:    br.BranchId,
				BeginNodeId: maxEndNode,
			})
			break findDeleteRanges
		} else {
			deleteRanges = append(deleteRanges, InternalDeleteHistoryBranchRange{
				BranchId:    br.BranchId,
				BeginNodeId: br.BeginNodeId,
			})
		}
	}
	req := &InternalDeleteHistoryBranchRequest{
		ShardID:      request.ShardID,
		TreeId:       branch.TreeId,
		BranchId:     branch.BranchId,
		BranchRanges: deleteRanges,
	}
	return m.persistence.DeleteHistoryBranch(req)
}
DeleteHistoryBranch removes a branch
package persistence
import (
	"fmt"
	"github.com/pborman/uuid"
	commonpb "go.temporal.io/api/common/v1"
	enumspb "go.temporal.io/api/enums/v1"
	historypb "go.temporal.io/api/history/v1"
	"go.temporal.io/api/serviceerror"
	persistencespb "go.temporal.io/server/api/persistence/v1"
	"go.temporal.io/server/common"
	"go.temporal.io/server/common/log/tag"
	"go.temporal.io/server/common/persistence/serialization"
	"go.temporal.io/server/common/primitives/timestamp"
)
const (
	defaultLastNodeID        = common.FirstEventID - 1
	defaultLastTransactionID = int64(0)
	trimHistoryBranchPageSize = 1000
)
var _ ExecutionManager = (*executionManagerImpl)(nil)
func (m *executionManagerImpl) ForkHistoryBranch(
	request *ForkHistoryBranchRequest,
) (*ForkHistoryBranchResponse, error) {
	if request.ForkNodeID <= 1 {
		return nil, &InvalidPersistenceRequestError{
			Msg: fmt.Sprintf("ForkNodeID must be > 1"),
		}
	}
	forkBranch, err := m.serializer.HistoryBranchFromBlob(&commonpb.DataBlob{
		Data:         request.ForkBranchToken,
		EncodingType: enumspb.ENCODING_TYPE_PROTO3,
	})
	if err != nil {
		return nil, err
	}
	newAncestors := make([]*persistencespb.HistoryBranchRange, 0, len(forkBranch.Ancestors)+1)
	beginNodeID := GetBeginNodeID(forkBranch)
	if beginNodeID >= request.ForkNodeID {
		for _, br := range forkBranch.Ancestors {
			if br.GetEndNodeId() >= request.ForkNodeID {
				newAncestors = append(newAncestors, &persistencespb.HistoryBranchRange{
					BranchId:    br.GetBranchId(),
					BeginNodeId: br.GetBeginNodeId(),
					EndNodeId:   request.ForkNodeID,
				})
				break
			} else {
				newAncestors = append(newAncestors, br)
			}
		}
	} else {
		newAncestors = forkBranch.Ancestors
		newAncestors = append(newAncestors, &persistencespb.HistoryBranchRange{
			BranchId:    forkBranch.GetBranchId(),
			BeginNodeId: beginNodeID,
			EndNodeId:   request.ForkNodeID,
		})
	}
	newBranchInfo := &persistencespb.HistoryBranch{
		TreeId:    forkBranch.TreeId,
		BranchId:  uuid.New(),
		Ancestors: newAncestors,
	}
	treeInfo := &persistencespb.HistoryTreeInfo{
		BranchInfo: newBranchInfo,
		ForkTime:   timestamp.TimeNowPtrUtc(),
		Info:       request.Info,
	}
	treeInfoBlob, err := m.serializer.HistoryTreeInfoToBlob(treeInfo, enumspb.ENCODING_TYPE_PROTO3)
	if err != nil {
		return nil, err
	}
	req := &InternalForkHistoryBranchRequest{
		ForkBranchInfo: forkBranch,
		TreeInfo:       treeInfoBlob,
		ForkNodeID:     request.ForkNodeID,
		NewBranchID:    newBranchInfo.BranchId,
		Info:           request.Info,
		ShardID:        request.ShardID,
	}
	err = m.persistence.ForkHistoryBranch(req)
	if err != nil {
		return nil, err
	}
	branchInfoBlob, err := m.serializer.HistoryBranchToBlob(newBranchInfo, enumspb.ENCODING_TYPE_PROTO3)
	if err != nil {
		return nil, err
	}
	return &ForkHistoryBranchResponse{
		NewBranchToken: branchInfoBlob.Data,
	}, nil
}