Files
tasks/service/history/api/reapplyevents/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

184 lines
5.6 KiB
Go

package reapplyevents
import (
"context"
"github.com/google/uuid"
historypb "go.temporal.io/api/history/v1"
"go.temporal.io/api/serviceerror"
"github.com/hanzoai/tasks/common"
"github.com/hanzoai/tasks/common/definition"
"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/service/history/api"
historyi "github.com/hanzoai/tasks/service/history/interfaces"
"github.com/hanzoai/tasks/service/history/ndc"
wcache "github.com/hanzoai/tasks/service/history/workflow/cache"
)
func Invoke(
ctx context.Context,
namespaceUUID namespace.ID,
workflowID string,
runID string,
reapplyEvents []*historypb.HistoryEvent,
shardContext historyi.ShardContext,
workflowConsistencyChecker api.WorkflowConsistencyChecker,
workflowResetter ndc.WorkflowResetter,
eventsReapplier ndc.EventsReapplier,
) error {
if shardContext.GetConfig().SkipReapplicationByNamespaceID(namespaceUUID) {
return nil
}
namespaceEntry, err := api.GetActiveNamespace(shardContext, namespace.ID(namespaceUUID.String()), workflowID)
if err != nil {
return err
}
namespaceID := namespaceEntry.ID()
isGlobalNamespace := namespaceEntry.IsGlobalNamespace()
return api.GetAndUpdateWorkflowWithNew(
ctx,
nil,
definition.NewWorkflowKey(
namespaceID.String(),
workflowID,
"",
),
func(workflowLease api.WorkflowLease) (*api.UpdateWorkflowAction, error) {
context := workflowLease.GetContext()
mutableState := workflowLease.GetMutableState()
// Filter out reapply event from the same cluster
toReapplyEvents := make([]*historypb.HistoryEvent, 0, len(reapplyEvents))
clusterMetadata := shardContext.GetClusterMetadata()
currentCluster := clusterMetadata.GetCurrentClusterName()
for _, event := range reapplyEvents {
if clusterMetadata.ClusterNameForFailoverVersion(
isGlobalNamespace,
event.GetVersion(),
) == currentCluster {
// The reapply is from the same cluster. Ignoring.
continue
}
dedupResource := definition.NewEventReappliedID(runID, event.GetEventId(), event.GetVersion())
if mutableState.IsResourceDuplicated(dedupResource) {
// already apply the signal
continue
}
toReapplyEvents = append(toReapplyEvents, event)
}
if len(toReapplyEvents) == 0 {
return &api.UpdateWorkflowAction{
Noop: true,
CreateWorkflowTask: false,
}, nil
}
if !mutableState.IsWorkflowExecutionRunning() {
// need to reset target workflow (which is also the current workflow)
// to accept events to be reapplied
baseRunID := mutableState.GetExecutionState().GetRunId()
resetRunID := uuid.New()
baseRebuildLastEventID := mutableState.GetLastCompletedWorkflowTaskStartedEventId()
// TODO when https://github.com/uber/cadence/issues/2420 is finished, remove this block,
// since cannot reapply event to a finished workflow which had no workflow tasks started
if baseRebuildLastEventID == common.EmptyEventID {
shardContext.GetLogger().Warn("cannot reapply event to a finished workflow with no workflow task",
tag.WorkflowNamespaceID(namespaceID.String()),
tag.WorkflowID(workflowID),
)
metrics.EventReapplySkippedCount.With(shardContext.GetMetricsHandler()).Record(
1,
metrics.OperationTag(metrics.HistoryReapplyEventsScope))
return &api.UpdateWorkflowAction{
Noop: true,
CreateWorkflowTask: false,
}, nil
}
baseVersionHistories := mutableState.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 := mutableState.GetNextEventID()
baseWorkflow := ndc.NewWorkflow(
shardContext.GetClusterMetadata(),
context,
mutableState,
wcache.NoopReleaseFn,
)
err = workflowResetter.ResetWorkflow(
ctx,
namespaceID,
workflowID,
baseRunID,
baseCurrentBranchToken,
baseRebuildLastEventID,
baseRebuildLastEventVersion,
baseNextEventID,
resetRunID.String(),
baseWorkflow,
baseWorkflow,
ndc.EventsReapplicationResetWorkflowReason,
toReapplyEvents,
nil,
false, // allowResetWithPendingChildren
nil,
)
switch err.(type) {
case *serviceerror.InvalidArgument:
// no-op. Usually this is due to reset workflow with pending child workflows
shardContext.GetLogger().Warn("Cannot reset workflow. Ignoring reapply events.", tag.Error(err))
case nil:
// no-op
default:
return nil, err
}
return &api.UpdateWorkflowAction{
Noop: true,
CreateWorkflowTask: false,
}, nil
}
reappliedEvents, err := eventsReapplier.ReapplyEvents(
ctx,
mutableState,
context.UpdateRegistry(ctx),
toReapplyEvents,
runID,
)
if err != nil {
shardContext.GetLogger().Error("failed to re-apply stale events", tag.Error(err))
return nil, err
}
if len(reappliedEvents) == 0 {
return &api.UpdateWorkflowAction{
Noop: true,
CreateWorkflowTask: false,
}, nil
}
return &api.UpdateWorkflowAction{
Noop: false,
CreateWorkflowTask: false,
}, nil
},
nil,
shardContext,
workflowConsistencyChecker,
)
}