Files
tasks/service/history/api/resetworkflow/api.go
zeekay 5a601d081b rebrand: go.temporal.io/server -> github.com/hanzoai/tasks
Full module path rebrand from Temporal to Hanzo Tasks:
- go.mod module: github.com/hanzoai/tasks
- All internal Go imports updated (1906 files)
- cmd/server renamed to cmd/tasksd
- Binary name: tasksd (was temporal-server)
- Docker images: hanzoai/tasks (was temporalio/server)
- README rewritten with Hanzo Tasks branding
- LLM.md created with integration notes
- Makefile, goreleaser, CI workflows updated
- go.temporal.io/sdk and go.temporal.io/api imports preserved (separate repos)

Fork of Temporal.io (MIT license).
2026-03-19 10:36:40 -07:00

243 lines
8.5 KiB
Go

package resetworkflow
import (
"context"
"github.com/google/uuid"
enumspb "go.temporal.io/api/enums/v1"
"go.temporal.io/api/serviceerror"
workflowpb "go.temporal.io/api/workflow/v1"
"github.com/hanzoai/tasks/api/historyservice/v1"
"github.com/hanzoai/tasks/api/matchingservice/v1"
"github.com/hanzoai/tasks/common"
"github.com/hanzoai/tasks/common/definition"
"github.com/hanzoai/tasks/common/locks"
"github.com/hanzoai/tasks/common/log/tag"
"github.com/hanzoai/tasks/common/metrics"
"github.com/hanzoai/tasks/common/namespace"
"github.com/hanzoai/tasks/common/persistence/versionhistory"
"github.com/hanzoai/tasks/common/worker_versioning"
"github.com/hanzoai/tasks/service/history/api"
historyi "github.com/hanzoai/tasks/service/history/interfaces"
"github.com/hanzoai/tasks/service/history/ndc"
)
func Invoke(
ctx context.Context,
resetRequest *historyservice.ResetWorkflowExecutionRequest,
shardContext historyi.ShardContext,
workflowConsistencyChecker api.WorkflowConsistencyChecker,
matchingClient matchingservice.MatchingServiceClient,
versionMembershipCache worker_versioning.VersionMembershipCache,
reactivationSignalCache worker_versioning.ReactivationSignalCache,
reactivationSignaler api.VersionReactivationSignalerFn,
) (_ *historyservice.ResetWorkflowExecutionResponse, retError error) {
namespaceID := namespace.ID(resetRequest.GetNamespaceId())
err := api.ValidateNamespaceUUID(namespaceID)
if err != nil {
return nil, err
}
request := resetRequest.ResetRequest
workflowID := request.WorkflowExecution.GetWorkflowId()
baseRunID := request.WorkflowExecution.GetRunId()
baseWorkflowLease, err := workflowConsistencyChecker.GetWorkflowLease(
ctx,
nil,
definition.NewWorkflowKey(
namespaceID.String(),
workflowID,
baseRunID,
),
locks.PriorityHigh,
)
if err != nil {
return nil, err
}
defer func() { baseWorkflowLease.GetReleaseFn()(retError) }()
baseMutableState := baseWorkflowLease.GetMutableState()
if request.GetWorkflowTaskFinishEventId() <= common.FirstEventID ||
request.GetWorkflowTaskFinishEventId() >= baseMutableState.GetNextEventID() {
return nil, serviceerror.NewInvalidArgument("Workflow task finish ID must be > 1 && <= workflow last event ID.")
}
// Validate versioning override, if any.
err = validatePostResetOperationInputs(ctx, request.GetPostResetOperations(), matchingClient, versionMembershipCache,
baseMutableState.GetExecutionInfo().GetTaskQueue(), namespaceID.String())
if err != nil {
return nil, err
}
// also load the current run of the workflow, it can be different from the base runID
currentRunID, err := workflowConsistencyChecker.GetCurrentWorkflowRunID(
ctx,
namespaceID.String(),
request.WorkflowExecution.GetWorkflowId(),
locks.PriorityHigh,
)
if err != nil {
return nil, err
}
if baseRunID == "" {
baseRunID = currentRunID
}
var currentWorkflowLease api.WorkflowLease
if currentRunID == baseRunID {
currentWorkflowLease = baseWorkflowLease
} else {
currentWorkflowLease, err = workflowConsistencyChecker.GetWorkflowLease(
ctx,
nil,
definition.NewWorkflowKey(
namespaceID.String(),
workflowID,
currentRunID,
),
locks.PriorityHigh,
)
if err != nil {
return nil, err
}
defer func() { currentWorkflowLease.GetReleaseFn()(retError) }()
}
// dedup by requestID
if currentWorkflowLease.GetMutableState().GetExecutionState().CreateRequestId == request.GetRequestId() {
shardContext.GetLogger().Info("Duplicated reset request",
tag.WorkflowID(workflowID),
tag.WorkflowRunID(currentRunID),
tag.WorkflowNamespaceID(namespaceID.String()))
return &historyservice.ResetWorkflowExecutionResponse{
RunId: currentRunID,
}, nil
}
resetRunID := uuid.New().String()
baseRebuildLastEventID := request.GetWorkflowTaskFinishEventId() - 1
baseVersionHistories := baseMutableState.GetExecutionInfo().GetVersionHistories()
baseCurrentVersionHistory, err := versionhistory.GetCurrentVersionHistory(baseVersionHistories)
if err != nil {
return nil, err
}
baseRebuildLastEventVersion, err := versionhistory.GetVersionHistoryEventVersion(baseCurrentVersionHistory, baseRebuildLastEventID)
if err != nil {
return nil, err
}
baseCurrentBranchToken := baseCurrentVersionHistory.GetBranchToken()
baseNextEventID := baseMutableState.GetNextEventID()
baseWorkflow := ndc.NewWorkflow(
shardContext.GetClusterMetadata(),
baseWorkflowLease.GetContext(),
baseWorkflowLease.GetMutableState(),
baseWorkflowLease.GetReleaseFn(),
)
namespaceEntry, err := api.GetActiveNamespace(shardContext, namespaceID, workflowID)
if err != nil {
return nil, err
}
metrics.WorkflowResetCount.With(
shardContext.GetMetricsHandler().WithTags(
metrics.NamespaceTag(namespaceEntry.Name().String()),
metrics.OperationTag(metrics.HistoryResetWorkflowScope),
metrics.VersioningBehaviorTag(baseMutableState.GetEffectiveVersioningBehavior()),
),
).Record(1)
allowResetWithPendingChildren := shardContext.GetConfig().AllowResetWithPendingChildren(namespaceEntry.Name().String())
if err := ndc.NewWorkflowResetter(
shardContext,
workflowConsistencyChecker.GetWorkflowCache(),
shardContext.GetLogger(),
).ResetWorkflow(
ctx,
namespaceID,
workflowID,
baseRunID,
baseCurrentBranchToken,
baseRebuildLastEventID,
baseRebuildLastEventVersion,
baseNextEventID,
resetRunID,
baseWorkflow,
ndc.NewWorkflow(
shardContext.GetClusterMetadata(),
currentWorkflowLease.GetContext(),
currentWorkflowLease.GetMutableState(),
currentWorkflowLease.GetReleaseFn(),
),
request.GetReason(),
nil,
GetResetReapplyExcludeTypes(request.GetResetReapplyExcludeTypes(), request.GetResetReapplyType()),
allowResetWithPendingChildren,
resetRequest.ResetRequest.PostResetOperations,
); err != nil {
return nil, err
}
// Notify version workflow if we're pinning to a potentially drained version via post-reset operations
for _, operation := range request.GetPostResetOperations() {
if updateOpts, ok := operation.GetVariant().(*workflowpb.PostResetOperation_UpdateWorkflowOptions_); ok {
api.ReactivateVersionWorkflowIfPinned(ctx, namespaceEntry,
updateOpts.UpdateWorkflowOptions.GetWorkflowExecutionOptions().GetVersioningOverride(), reactivationSignalCache, reactivationSignaler, shardContext.GetConfig().EnableVersionReactivationSignals())
}
}
return &historyservice.ResetWorkflowExecutionResponse{
RunId: resetRunID,
}, nil
}
// GetResetReapplyExcludeTypes computes the set of requested exclude types. It
// uses the reset_reapply_exclude_types request field (a set of event types to
// exclude from reapply), as well as the deprecated reset_reapply_type request
// field (a specification of what to include).
func GetResetReapplyExcludeTypes(
excludeTypes []enumspb.ResetReapplyExcludeType,
includeType enumspb.ResetReapplyType,
) map[enumspb.ResetReapplyExcludeType]struct{} {
// A client who wishes to have reapplication of all supported event types should omit the deprecated
// reset_reapply_type field (since its default value is RESET_REAPPLY_TYPE_ALL_ELIGIBLE).
exclude := map[enumspb.ResetReapplyExcludeType]struct{}{}
switch includeType {
case enumspb.RESET_REAPPLY_TYPE_SIGNAL:
// A client sending this value of the deprecated reset_reapply_type field will not have any events other than
// signal reapplied.
exclude[enumspb.RESET_REAPPLY_EXCLUDE_TYPE_UPDATE] = struct{}{}
case enumspb.RESET_REAPPLY_TYPE_NONE:
exclude[enumspb.RESET_REAPPLY_EXCLUDE_TYPE_SIGNAL] = struct{}{}
exclude[enumspb.RESET_REAPPLY_EXCLUDE_TYPE_UPDATE] = struct{}{}
case enumspb.RESET_REAPPLY_TYPE_UNSPECIFIED, enumspb.RESET_REAPPLY_TYPE_ALL_ELIGIBLE:
// Do nothing.
}
for _, e := range excludeTypes {
exclude[e] = struct{}{}
}
return exclude
}
// validatePostResetOperationInputs validates the optional post reset operation inputs.
func validatePostResetOperationInputs(ctx context.Context,
postResetOperations []*workflowpb.PostResetOperation,
matchingClient matchingservice.MatchingServiceClient,
versionMembershipCache worker_versioning.VersionMembershipCache,
taskQueue string,
namespaceID string) error {
for _, operation := range postResetOperations {
switch op := operation.GetVariant().(type) {
case *workflowpb.PostResetOperation_UpdateWorkflowOptions_:
opts := op.UpdateWorkflowOptions.GetWorkflowExecutionOptions()
if err := worker_versioning.ValidateVersioningOverride(ctx, opts.GetVersioningOverride(), matchingClient, versionMembershipCache, taskQueue, enumspb.TASK_QUEUE_TYPE_WORKFLOW, namespaceID); err != nil {
return err
}
default:
return serviceerror.NewInvalidArgumentf("unsupported post reset operation: %T", op)
}
}
return nil
}