Files
tasks/service/history/ndc_standby_task_util.go
Alex ShtinandGitHub 8e8f1b0f7c Remove license header from every file (#7689)
## What changed?
<!-- Describe what has changed in this PR -->
Remove license header from every file. Because it is really hard to
follow in this PR here is the summary:
1. License header is removed from all `.go` and `.proto` files
:fireworks::fireworks:🎆.
2. `LICENSE` file in the root directory has only Temporal and Uber
copyrights.
3. 5 other `LICENSE` files added to the packages which have copyrights
different from Temporal and Uber: Datadog, Xargin, "Mat Ryer, Tyler
Bunnell and contributors".
4. `license_file` flag is removed from all code generation tools.
5. `copyright_file` flag is removed from `go:generate mockgen`
directive.
6. All copyright related targets are removed from `Makefile`.
7. Updated Temporal copyright year to 2025 everywhere.

## Why?
<!-- Tell your future self why have you made these changes -->
I double checked with legal department that it is not needed to have
license header in every file. One file per repo is enough. I put all
copyrights to the root `LICENSE` file and removed header from all other
files. Also updated tools and `Makefile`.
2025-05-01 18:50:21 -07:00

243 lines
6.9 KiB
Go

package history
import (
"context"
"errors"
"time"
commonpb "go.temporal.io/api/common/v1"
taskqueuepb "go.temporal.io/api/taskqueue/v1"
"go.temporal.io/api/workflowservice/v1"
persistencespb "go.temporal.io/server/api/persistence/v1"
taskqueuespb "go.temporal.io/server/api/taskqueue/v1"
"go.temporal.io/server/client"
"go.temporal.io/server/common"
"go.temporal.io/server/common/definition"
"go.temporal.io/server/common/log"
"go.temporal.io/server/common/log/tag"
"go.temporal.io/server/common/namespace"
"go.temporal.io/server/common/priorities"
"go.temporal.io/server/service/history/consts"
historyi "go.temporal.io/server/service/history/interfaces"
"go.temporal.io/server/service/history/tasks"
)
type (
standbyActionFn func(context.Context, historyi.WorkflowContext, historyi.MutableState) (interface{}, error)
standbyPostActionFn func(context.Context, tasks.Task, interface{}, log.Logger) error
standbyCurrentTimeFn func() time.Time
)
func standbyTaskPostActionNoOp(
_ context.Context,
_ tasks.Task,
postActionInfo interface{},
_ log.Logger,
) error {
if postActionInfo == nil {
return nil
}
if err, ok := postActionInfo.(error); ok {
return err
}
// return error so task processing logic will retry
return consts.ErrTaskRetry
}
func standbyTransferTaskPostActionTaskDiscarded(
_ context.Context,
taskInfo tasks.Task,
postActionInfo interface{},
logger log.Logger,
) error {
if postActionInfo == nil {
return nil
}
logger.Warn("Discarding standby transfer task due to task being pending for too long.", tag.Task(taskInfo))
return consts.ErrTaskDiscarded
}
func standbyTimerTaskPostActionTaskDiscarded(
_ context.Context,
taskInfo tasks.Task,
postActionInfo interface{},
logger log.Logger,
) error {
if postActionInfo == nil {
return nil
}
logger.Warn("Discarding standby timer task due to task being pending for too long.", tag.Task(taskInfo))
return consts.ErrTaskDiscarded
}
func isWorkflowExistOnSource(
ctx context.Context,
workflowKey definition.WorkflowKey,
logger log.Logger,
currentCluster string,
clientBean client.Bean,
registry namespace.Registry,
) bool {
namespaceEntry, err := registry.GetNamespaceByID(namespace.ID(workflowKey.NamespaceID))
if err != nil {
return true
}
remoteClusterName, err := getSourceClusterName(
currentCluster,
registry,
workflowKey.GetNamespaceID(),
)
if err != nil {
return true
}
_, remoteFrontend, err := clientBean.GetRemoteFrontendClient(remoteClusterName)
if err != nil {
return true
}
_, err = remoteFrontend.DescribeWorkflowExecution(ctx, &workflowservice.DescribeWorkflowExecutionRequest{
Namespace: namespaceEntry.Name().String(),
Execution: &commonpb.WorkflowExecution{
WorkflowId: workflowKey.GetWorkflowID(),
RunId: workflowKey.GetRunID(),
},
})
if err != nil {
if common.IsNotFoundError(err) {
return false
}
logger.Error("Error describe mutable state from remote.",
tag.WorkflowNamespaceID(workflowKey.GetNamespaceID()),
tag.WorkflowID(workflowKey.GetWorkflowID()),
tag.WorkflowRunID(workflowKey.GetRunID()),
tag.ClusterName(remoteClusterName),
tag.Error(err))
}
return true
}
type (
executionTimerPostActionInfo struct {
currentRunID string
}
activityTaskPostActionInfo struct {
taskQueue string
activityTaskScheduleToStartTimeout time.Duration
versionDirective *taskqueuespb.TaskVersionDirective
priority *commonpb.Priority
}
verifyCompletionRecordedPostActionInfo struct {
parentWorkflowKey *definition.WorkflowKey
}
workflowTaskPostActionInfo struct {
workflowTaskScheduleToStartTimeout time.Duration
taskqueue *taskqueuepb.TaskQueue
versionDirective *taskqueuespb.TaskVersionDirective
priority *commonpb.Priority
}
)
func newExecutionTimerPostActionInfo(
mutableState historyi.MutableState,
) (*executionTimerPostActionInfo, error) {
return &executionTimerPostActionInfo{
currentRunID: mutableState.GetExecutionState().RunId,
}, nil
}
func newActivityTaskPostActionInfo(
mutableState historyi.MutableState,
activityInfo *persistencespb.ActivityInfo,
) (*activityTaskPostActionInfo, error) {
directive := MakeDirectiveForActivityTask(mutableState, activityInfo)
priority := priorities.Merge(mutableState.GetExecutionInfo().Priority, activityInfo.Priority)
return &activityTaskPostActionInfo{
activityTaskScheduleToStartTimeout: activityInfo.ScheduleToStartTimeout.AsDuration(),
versionDirective: directive,
priority: priority,
}, nil
}
func newActivityRetryTimePostActionInfo(
mutableState historyi.MutableState,
taskQueue string,
activityScheduleToStartTimeout time.Duration,
activityInfo *persistencespb.ActivityInfo,
) (*activityTaskPostActionInfo, error) {
directive := MakeDirectiveForActivityTask(mutableState, activityInfo)
priority := priorities.Merge(mutableState.GetExecutionInfo().Priority, activityInfo.Priority)
return &activityTaskPostActionInfo{
taskQueue: taskQueue,
activityTaskScheduleToStartTimeout: activityScheduleToStartTimeout,
versionDirective: directive,
priority: priority,
}, nil
}
func newWorkflowTaskPostActionInfo(
mutableState historyi.MutableState,
workflowTaskScheduleToStartTimeout time.Duration,
taskqueue *taskqueuepb.TaskQueue,
) (*workflowTaskPostActionInfo, error) {
directive := MakeDirectiveForWorkflowTask(mutableState)
priority := mutableState.GetExecutionInfo().Priority
return &workflowTaskPostActionInfo{
workflowTaskScheduleToStartTimeout: workflowTaskScheduleToStartTimeout,
taskqueue: taskqueue,
versionDirective: directive,
priority: priority,
}, nil
}
func getStandbyPostActionFn(
taskInfo tasks.Task,
standbyNow standbyCurrentTimeFn,
standbyTaskMissingEventsDiscardDelay time.Duration,
discardTaskStandbyPostActionFn standbyPostActionFn,
) standbyPostActionFn {
// this is for task retry, use machine time
now := standbyNow()
taskTime := taskInfo.GetVisibilityTime()
discardTime := taskTime.Add(standbyTaskMissingEventsDiscardDelay)
// now < task start time + StandbyTaskMissingEventsResendDelay
if now.Before(discardTime) {
return standbyTaskPostActionNoOp
}
// task start time + StandbyTaskMissingEventsResendDelay <= now
return discardTaskStandbyPostActionFn
}
func getSourceClusterName(
currentCluster string,
registry namespace.Registry,
namespaceID string,
) (string, error) {
namespaceEntry, err := registry.GetNamespaceByID(namespace.ID(namespaceID))
if err != nil {
return "", err
}
remoteClusterName := namespaceEntry.ActiveClusterName()
if remoteClusterName == currentCluster {
// namespace has turned active, retry the task
return "", errors.New("namespace becomes active when processing task as standby")
}
return remoteClusterName, nil
}