mirror of
https://github.com/hanzoai/tasks.git
synced 2026-10-04 02:04:03 +00:00
<!-- Describe what has changed in this PR --> **What changed?** I renamed all of our Go source code files so that they're snake case instead of camel case. <!-- Tell your future self why have you made these changes --> **Why?** This was an annoying inconsistency we carried over from much older code in the repo. <!-- How have you verified this change? Tested locally? Added a unit test? Checked in staging env? --> **How did you test it?** The renaming process was part of an automated script (https://gist.github.com/MichaelSnowden/47576fffe27aa1cae6b26eaccec7ab55), so it should be consistent. Also, I don't see us doing anything in our codebase that involves parsing our own source files by path: <img width="687" alt="image" src="https://github.com/temporalio/temporal/assets/5942963/c6b873b0-d841-40d1-8b0f-2fe9d8079a53"> *PS: here's a link to the GPT convo I had to generate this renaming script: https://chat.openai.com/share/7f2d011a-a8da-4fa4-9c6d-e09102fdd49f* <!-- Assuming the worst case, what can be broken when deploying this change to production? --> **Potential risks** Mainly merge conflicts, but I used `git mv` so that these are renames instead of deletes/adds, so that shouldn't be a big issue. <!-- Is this PR a hotfix candidate or require that a notification be sent to the broader community? (Yes/No) --> **Is hotfix candidate?** No.
227 lines
8.2 KiB
Go
227 lines
8.2 KiB
Go
// The MIT License
|
|
//
|
|
// Copyright (c) 2020 Temporal Technologies Inc. All rights reserved.
|
|
//
|
|
// Copyright (c) 2020 Uber Technologies, Inc.
|
|
//
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
// in the Software without restriction, including without limitation the rights
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
// furnished to do so, subject to the following conditions:
|
|
//
|
|
// The above copyright notice and this permission notice shall be included in
|
|
// all copies or substantial portions of the Software.
|
|
//
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
// THE SOFTWARE.
|
|
|
|
package history
|
|
|
|
import (
|
|
"context"
|
|
|
|
enumspb "go.temporal.io/api/enums/v1"
|
|
"go.temporal.io/api/serviceerror"
|
|
|
|
enumsspb "go.temporal.io/server/api/enums/v1"
|
|
persistencespb "go.temporal.io/server/api/persistence/v1"
|
|
"go.temporal.io/server/common/log"
|
|
"go.temporal.io/server/common/log/tag"
|
|
"go.temporal.io/server/common/metrics"
|
|
"go.temporal.io/server/common/namespace"
|
|
"go.temporal.io/server/service/history/consts"
|
|
"go.temporal.io/server/service/history/shard"
|
|
"go.temporal.io/server/service/history/tasks"
|
|
"go.temporal.io/server/service/history/workflow"
|
|
)
|
|
|
|
// CheckTaskVersion will return an error if task version check fails
|
|
func CheckTaskVersion(
|
|
shard shard.Context,
|
|
logger log.Logger,
|
|
namespace *namespace.Namespace,
|
|
version int64,
|
|
taskVersion int64,
|
|
task interface{},
|
|
) error {
|
|
|
|
if !shard.GetClusterMetadata().IsGlobalNamespaceEnabled() {
|
|
return nil
|
|
}
|
|
|
|
// the first return value is whether this task is valid for further processing
|
|
if !namespace.IsGlobalNamespace() {
|
|
logger.Debug("NamespaceID is not global, task version check pass", tag.WorkflowNamespaceID(namespace.ID().String()), tag.Task(task))
|
|
return nil
|
|
} else if version != taskVersion {
|
|
logger.Debug("NamespaceID is global, task version != target version", tag.WorkflowNamespaceID(namespace.ID().String()), tag.Task(task), tag.TaskVersion(version))
|
|
return consts.ErrTaskVersionMismatch
|
|
}
|
|
logger.Debug("NamespaceID is global, task version == target version", tag.WorkflowNamespaceID(namespace.ID().String()), tag.Task(task), tag.TaskVersion(version))
|
|
return nil
|
|
}
|
|
|
|
// load mutable state, if mutable state's next event ID <= task ID, will attempt to refresh
|
|
// if still mutable state's next event ID <= task ID, will return nil, nil
|
|
func loadMutableStateForTransferTask(
|
|
ctx context.Context,
|
|
wfContext workflow.Context,
|
|
transferTask tasks.Task,
|
|
metricsHandler metrics.Handler,
|
|
logger log.Logger,
|
|
) (workflow.MutableState, error) {
|
|
logger = tasks.InitializeLogger(transferTask, logger)
|
|
mutableState, err := LoadMutableStateForTask(
|
|
ctx,
|
|
wfContext,
|
|
transferTask,
|
|
getTransferTaskEventIDAndRetryable,
|
|
metricsHandler.WithTags(metrics.OperationTag(metrics.TransferQueueProcessorScope)),
|
|
logger,
|
|
)
|
|
if err != nil {
|
|
// When standby task executor executes task in active cluster (and vice versa),
|
|
// mutable state might be already deleted by active task executor and NotFound is a valid case which shouldn't be logged.
|
|
// Unfortunately, this will also skip logging of actual errors that might happen due to serious bugs,
|
|
// but these errors, most likely, will happen for other task types too, and will be logged.
|
|
// TODO: remove this logic multi-cursor is implemented and only one task processor is running in each cluster.
|
|
skipNotFoundLog :=
|
|
transferTask.GetType() == enumsspb.TASK_TYPE_TRANSFER_CLOSE_EXECUTION ||
|
|
transferTask.GetType() == enumsspb.TASK_TYPE_TRANSFER_DELETE_EXECUTION
|
|
|
|
if !skipNotFoundLog {
|
|
switch err.(type) {
|
|
case *serviceerror.NotFound:
|
|
// NotFound error will be ignored by task error handling logic, so log it here
|
|
// for transfer tasks, mutable state should always be available
|
|
logger.Warn("Transfer Task Processor: workflow mutable state not found, skip.")
|
|
case *serviceerror.NamespaceNotFound:
|
|
// NamespaceNotFound error will be ignored by task error handling logic, so log it here
|
|
// for transfer tasks, namespace should always be available.
|
|
logger.Warn("Transfer Task Processor: namespace not found, skip.")
|
|
}
|
|
}
|
|
}
|
|
return mutableState, err
|
|
}
|
|
|
|
// load mutable state, if mutable state's next event ID <= task ID, will attempt to refresh
|
|
// if still mutable state's next event ID <= task ID, will return nil, nil
|
|
func loadMutableStateForTimerTask(
|
|
ctx context.Context,
|
|
wfContext workflow.Context,
|
|
timerTask tasks.Task,
|
|
metricsHandler metrics.Handler,
|
|
logger log.Logger,
|
|
) (workflow.MutableState, error) {
|
|
logger = tasks.InitializeLogger(timerTask, logger)
|
|
return LoadMutableStateForTask(
|
|
ctx,
|
|
wfContext,
|
|
timerTask,
|
|
getTimerTaskEventIDAndRetryable,
|
|
metricsHandler.WithTags(metrics.OperationTag(metrics.TimerQueueProcessorScope)),
|
|
logger,
|
|
)
|
|
}
|
|
|
|
func LoadMutableStateForTask(
|
|
ctx context.Context,
|
|
wfContext workflow.Context,
|
|
task tasks.Task,
|
|
taskEventIDAndRetryable func(task tasks.Task, executionInfo *persistencespb.WorkflowExecutionInfo) (int64, bool),
|
|
metricsHandler metrics.Handler,
|
|
logger log.Logger,
|
|
) (workflow.MutableState, error) {
|
|
|
|
mutableState, err := wfContext.LoadMutableState(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// check to see if cache needs to be refreshed as we could potentially have stale workflow execution
|
|
// the exception is workflow task consistently fail
|
|
// there will be no event generated, thus making the workflow task schedule ID == next event ID
|
|
eventID, retryable := taskEventIDAndRetryable(task, mutableState.GetExecutionInfo())
|
|
if eventID < mutableState.GetNextEventID() || !retryable {
|
|
return mutableState, nil
|
|
}
|
|
|
|
metricsHandler.Counter(metrics.StaleMutableStateCounter.GetMetricName()).Record(1)
|
|
wfContext.Clear()
|
|
|
|
mutableState, err = wfContext.LoadMutableState(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
// after refresh, still mutable state's next event ID <= task's event ID
|
|
if eventID >= mutableState.GetNextEventID() {
|
|
metricsHandler.Counter(metrics.TaskSkipped.GetMetricName()).Record(1)
|
|
logger.Info("Task Processor: task event ID >= MS NextEventID, skip.",
|
|
tag.WorkflowNextEventID(mutableState.GetNextEventID()),
|
|
)
|
|
return nil, nil
|
|
}
|
|
return mutableState, nil
|
|
}
|
|
|
|
func getTransferTaskEventIDAndRetryable(
|
|
transferTask tasks.Task,
|
|
executionInfo *persistencespb.WorkflowExecutionInfo,
|
|
) (int64, bool) {
|
|
eventID := tasks.GetTransferTaskEventID(transferTask)
|
|
retryable := true
|
|
|
|
if task, ok := transferTask.(*tasks.WorkflowTask); ok {
|
|
retryable = !(executionInfo.WorkflowTaskScheduledEventId == task.ScheduledEventID && executionInfo.WorkflowTaskAttempt > 1)
|
|
}
|
|
|
|
return eventID, retryable
|
|
}
|
|
|
|
func getTimerTaskEventIDAndRetryable(
|
|
timerTask tasks.Task,
|
|
executionInfo *persistencespb.WorkflowExecutionInfo,
|
|
) (int64, bool) {
|
|
eventID := tasks.GetTimerTaskEventID(timerTask)
|
|
retryable := true
|
|
|
|
if task, ok := timerTask.(*tasks.WorkflowTaskTimeoutTask); ok {
|
|
retryable = !(executionInfo.WorkflowTaskScheduledEventId == task.EventID && executionInfo.WorkflowTaskAttempt > 1) &&
|
|
executionInfo.WorkflowTaskType != enumsspb.WORKFLOW_TASK_TYPE_SPECULATIVE
|
|
}
|
|
|
|
return eventID, retryable
|
|
}
|
|
|
|
func getNamespaceTagByID(
|
|
registry namespace.Registry,
|
|
namespaceID string,
|
|
) metrics.Tag {
|
|
namespaceName, err := registry.GetNamespaceName(namespace.ID(namespaceID))
|
|
if err != nil {
|
|
return metrics.NamespaceUnknownTag()
|
|
}
|
|
|
|
return metrics.NamespaceTag(namespaceName.String())
|
|
}
|
|
|
|
func getNamespaceTagAndReplicationStateByID(
|
|
registry namespace.Registry,
|
|
namespaceID string,
|
|
) (metrics.Tag, enumspb.ReplicationState) {
|
|
namespace, err := registry.GetNamespaceByID(namespace.ID(namespaceID))
|
|
if err != nil {
|
|
return metrics.NamespaceUnknownTag(), enumspb.REPLICATION_STATE_UNSPECIFIED
|
|
}
|
|
|
|
return metrics.NamespaceTag(namespace.Name().String()), namespace.ReplicationState()
|
|
}
|