mirror of
https://github.com/hanzoai/tasks.git
synced 2026-10-30 14:56:19 +00:00
Goes from 135 MB binary with 1153 files importing go.temporal.io and 224
importing google.golang.org/grpc to a 10 MB native binary with ZERO of
either.
Deleted:
- tasks/, tasktests/, service/, chasm/, client/, api/, proto/, tools/
- cmd/tools/, components/, docker/
- schema/{cassandra,elasticsearch,mysql,postgresql,sqlite}/
- 40 tainted common/ subdirs + 7 top-level common files
- tests/testutils (no longer referenced after surviving deps drop common)
New cmd/tasksd (~100 lines) boots tasks.Embed (zap.Node) + http.Server.
New pkg/tasks/embed.go registers ZAP handlers for the SDK opcode surface
(0x0060-0x00A5). Workflow handlers return a 501 "not yet implemented"
envelope; native engine lands behind this contract.
Build: go build ./cmd/tasksd → 10 MB
Vet: go vet ./... → clean
Boot: tasksd --zap-port 9999 --http :7243
Probes: /healthz, /v1/tasks/health → 200
UI: /_/tasks/, / → 200 (embedded Vite bundle)
ZAP: _tasks._tcp on :9999 (mDNS disabled; direct connect)
go.mod: tidy. No go.temporal.io. No google.golang.org/grpc.
270 lines
10 KiB
Go
270 lines
10 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"
|
|
"go.temporal.io/server/api/historyservice/v1"
|
|
"go.temporal.io/server/api/matchingservice/v1"
|
|
"go.temporal.io/server/chasm"
|
|
"go.temporal.io/server/common"
|
|
"go.temporal.io/server/common/definition"
|
|
"go.temporal.io/server/common/locks"
|
|
"go.temporal.io/server/common/log/tag"
|
|
"go.temporal.io/server/common/metrics"
|
|
"go.temporal.io/server/common/namespace"
|
|
"go.temporal.io/server/common/persistence/versionhistory"
|
|
"go.temporal.io/server/common/worker_versioning"
|
|
"go.temporal.io/server/service/history/api"
|
|
"go.temporal.io/server/service/history/consts"
|
|
historyi "go.temporal.io/server/service/history/interfaces"
|
|
"go.temporal.io/server/service/history/ndc"
|
|
)
|
|
|
|
func Invoke(
|
|
ctx context.Context,
|
|
resetRequest *historyservice.ResetWorkflowExecutionRequest,
|
|
shardContext historyi.ShardContext,
|
|
workflowConsistencyChecker api.WorkflowConsistencyChecker,
|
|
matchingClient matchingservice.MatchingServiceClient,
|
|
versionCache worker_versioning.VersionMembershipAndReactivationStatusCache,
|
|
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()
|
|
|
|
if rl := shardContext.BusinessIDReuseRateLimiter(namespaceID, workflowID, chasm.WorkflowArchetypeID); rl != nil && !rl.Allow() {
|
|
archetypeName, _ := shardContext.ChasmRegistry().ArchetypeDisplayName(chasm.WorkflowArchetypeID)
|
|
metrics.BusinessIDReuseRateLimited.With(shardContext.GetMetricsHandler()).Record(
|
|
1,
|
|
metrics.ResourceExhaustedCauseTag(consts.ErrBusinessIDRateLimitExceeded.Cause),
|
|
metrics.ResourceExhaustedScopeTag(consts.ErrBusinessIDRateLimitExceeded.Scope),
|
|
metrics.StringTag("archetype", archetypeName),
|
|
)
|
|
return nil, consts.ErrBusinessIDRateLimitExceeded
|
|
}
|
|
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.
|
|
shouldSkipReactivationPerOp, revisionNumberPerOp, err := validatePostResetOperationInputs(ctx, request.GetPostResetOperations(), matchingClient, versionCache,
|
|
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 i, operation := range request.GetPostResetOperations() {
|
|
if updateOpts, ok := operation.GetVariant().(*workflowpb.PostResetOperation_UpdateWorkflowOptions_); ok {
|
|
api.ReactivateVersionWorkflowIfPinned(ctx, namespaceEntry,
|
|
updateOpts.UpdateWorkflowOptions.GetWorkflowExecutionOptions().GetVersioningOverride(), reactivationSignaler, shardContext.GetConfig().EnableVersionReactivationSignals(), shouldSkipReactivationPerOp[i], revisionNumberPerOp[i])
|
|
}
|
|
}
|
|
|
|
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.
|
|
// Returns parallel slices (one entry per operation) carrying the reactivation-signal inputs
|
|
// derived from the operation's versioning override:
|
|
// - shouldSkipReactivationPerOp: whether each operation's pinned version is active or
|
|
// still draining per matching (true → no need to send a reactivation signal; false
|
|
// covers drained/inactive, unknown, not-found, and old-matching cases).
|
|
// - revisionNumberPerOp: the pinned version's revision number per matching's view, used to
|
|
// compose a stable RequestId on the reactivation signal for receiver-side dedup.
|
|
//
|
|
// Both are only populated for UpdateWorkflowOptions operations; other operation types default
|
|
// to zero values.
|
|
func validatePostResetOperationInputs(ctx context.Context,
|
|
postResetOperations []*workflowpb.PostResetOperation,
|
|
matchingClient matchingservice.MatchingServiceClient,
|
|
versionCache worker_versioning.VersionMembershipAndReactivationStatusCache,
|
|
taskQueue string,
|
|
namespaceID string) ([]bool, []int64, error) {
|
|
shouldSkipReactivationPerOp := make([]bool, len(postResetOperations))
|
|
revisionNumberPerOp := make([]int64, len(postResetOperations))
|
|
for i, operation := range postResetOperations {
|
|
switch op := operation.GetVariant().(type) {
|
|
case *workflowpb.PostResetOperation_UpdateWorkflowOptions_:
|
|
opts := op.UpdateWorkflowOptions.GetWorkflowExecutionOptions()
|
|
shouldSkipReactivation, revisionNumber, err := worker_versioning.ValidateVersioningOverrideAndGetReactivationEligibility(ctx, opts.GetVersioningOverride(), matchingClient, versionCache, taskQueue, enumspb.TASK_QUEUE_TYPE_WORKFLOW, namespaceID)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
shouldSkipReactivationPerOp[i] = shouldSkipReactivation
|
|
revisionNumberPerOp[i] = revisionNumber
|
|
default:
|
|
return nil, nil, serviceerror.NewInvalidArgumentf("unsupported post reset operation: %T", op)
|
|
}
|
|
}
|
|
return shouldSkipReactivationPerOp, revisionNumberPerOp, nil
|
|
}
|